blob: a4692e97bc199c11386527b414d0f857eebb79af [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050042#include <linux/ratelimit.h>
43#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sunrpc/clnt.h>
46#include <linux/nfs.h>
47#include <linux/nfs4.h>
48#include <linux/nfs_fs.h>
49#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040050#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070052#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040053#include <linux/module.h>
Trond Myklebust6926afd2012-01-07 13:22:46 -050054#include <linux/nfs_idmap.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000055#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050056#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010057#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Trond Myklebust4ce79712005-06-22 17:16:21 +000059#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050061#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050062#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040063#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040064#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040065#include "netns.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050066#include "nfs4session.h"
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69#define NFSDBG_FACILITY NFSDBG_PROC
70
Trond Myklebust2066fe82006-09-15 08:30:46 -040071#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define NFS4_POLL_RETRY_MAX (15*HZ)
73
Trond Myklebustcdd4e682006-01-03 09:55:12 +010074struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010075static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080076static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050078static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Chuck Lever81934dd2012-03-01 17:01:57 -050079static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Bryan Schumakerbae36242012-05-10 15:07:31 -040080static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *);
Trond Myklebust99367812007-07-17 21:52:41 -040081static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040082static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
83 struct nfs_fattr *fattr, struct iattr *sattr,
84 struct nfs4_state *state);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040085#ifdef CONFIG_NFS_V4_1
Bryan Schumaker1cab0652012-01-31 10:39:29 -050086static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *);
87static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040088#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070089/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050090static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Trond Myklebust52567b02009-10-23 14:46:42 -040092 if (err >= -1000)
93 return err;
94 switch (err) {
95 case -NFS4ERR_RESOURCE:
96 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +000097 case -NFS4ERR_WRONGSEC:
98 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -080099 case -NFS4ERR_BADOWNER:
100 case -NFS4ERR_BADNAME:
101 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400102 case -NFS4ERR_SHARE_DENIED:
103 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400104 case -NFS4ERR_MINOR_VERS_MISMATCH:
105 return -EPROTONOSUPPORT;
Weston Andros Adamson6168f622012-09-10 14:00:46 -0400106 case -NFS4ERR_ACCESS:
107 return -EACCES;
Trond Myklebust52567b02009-10-23 14:46:42 -0400108 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700110 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400111 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400113 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114}
115
116/*
117 * This is our standard bitmap for GETATTR requests.
118 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400119const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 FATTR4_WORD0_TYPE
121 | FATTR4_WORD0_CHANGE
122 | FATTR4_WORD0_SIZE
123 | FATTR4_WORD0_FSID
124 | FATTR4_WORD0_FILEID,
125 FATTR4_WORD1_MODE
126 | FATTR4_WORD1_NUMLINKS
127 | FATTR4_WORD1_OWNER
128 | FATTR4_WORD1_OWNER_GROUP
129 | FATTR4_WORD1_RAWDEV
130 | FATTR4_WORD1_SPACE_USED
131 | FATTR4_WORD1_TIME_ACCESS
132 | FATTR4_WORD1_TIME_METADATA
133 | FATTR4_WORD1_TIME_MODIFY
134};
135
Trond Myklebust1549210f2012-06-05 09:16:47 -0400136static const u32 nfs4_pnfs_open_bitmap[3] = {
137 FATTR4_WORD0_TYPE
138 | FATTR4_WORD0_CHANGE
139 | FATTR4_WORD0_SIZE
140 | FATTR4_WORD0_FSID
141 | FATTR4_WORD0_FILEID,
142 FATTR4_WORD1_MODE
143 | FATTR4_WORD1_NUMLINKS
144 | FATTR4_WORD1_OWNER
145 | FATTR4_WORD1_OWNER_GROUP
146 | FATTR4_WORD1_RAWDEV
147 | FATTR4_WORD1_SPACE_USED
148 | FATTR4_WORD1_TIME_ACCESS
149 | FATTR4_WORD1_TIME_METADATA
150 | FATTR4_WORD1_TIME_MODIFY,
151 FATTR4_WORD2_MDSTHRESHOLD
152};
153
Andy Adamsone23008e2012-10-02 21:07:32 -0400154static const u32 nfs4_open_noattr_bitmap[3] = {
155 FATTR4_WORD0_TYPE
156 | FATTR4_WORD0_CHANGE
157 | FATTR4_WORD0_FILEID,
158};
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160const u32 nfs4_statfs_bitmap[2] = {
161 FATTR4_WORD0_FILES_AVAIL
162 | FATTR4_WORD0_FILES_FREE
163 | FATTR4_WORD0_FILES_TOTAL,
164 FATTR4_WORD1_SPACE_AVAIL
165 | FATTR4_WORD1_SPACE_FREE
166 | FATTR4_WORD1_SPACE_TOTAL
167};
168
Trond Myklebust4ce79712005-06-22 17:16:21 +0000169const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 FATTR4_WORD0_MAXLINK
171 | FATTR4_WORD0_MAXNAME,
172 0
173};
174
Fred Isamandae100c2011-07-30 20:52:37 -0400175const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 | FATTR4_WORD0_MAXREAD
177 | FATTR4_WORD0_MAXWRITE
178 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700179 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400180 | FATTR4_WORD1_FS_LAYOUT_TYPES,
181 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182};
183
Manoj Naik830b8e32006-06-09 09:34:25 -0400184const u32 nfs4_fs_locations_bitmap[2] = {
185 FATTR4_WORD0_TYPE
186 | FATTR4_WORD0_CHANGE
187 | FATTR4_WORD0_SIZE
188 | FATTR4_WORD0_FSID
189 | FATTR4_WORD0_FILEID
190 | FATTR4_WORD0_FS_LOCATIONS,
191 FATTR4_WORD1_MODE
192 | FATTR4_WORD1_NUMLINKS
193 | FATTR4_WORD1_OWNER
194 | FATTR4_WORD1_OWNER_GROUP
195 | FATTR4_WORD1_RAWDEV
196 | FATTR4_WORD1_SPACE_USED
197 | FATTR4_WORD1_TIME_ACCESS
198 | FATTR4_WORD1_TIME_METADATA
199 | FATTR4_WORD1_TIME_MODIFY
200 | FATTR4_WORD1_MOUNTED_ON_FILEID
201};
202
Al Virobc4785c2006-10-19 23:28:51 -0700203static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 struct nfs4_readdir_arg *readdir)
205{
Al Viro0dbb4c62006-10-19 23:28:49 -0700206 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000209 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
211 return;
212 }
213
214 readdir->cookie = 0;
215 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
216 if (cookie == 2)
217 return;
218
219 /*
220 * NFSv4 servers do not return entries for '.' and '..'
221 * Therefore, we fake these entries here. We let '.'
222 * have cookie 0 and '..' have cookie 1. Note that
223 * when talking to the server, we always send cookie 0
224 * instead of 1 or 2.
225 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800226 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 if (cookie == 0) {
229 *p++ = xdr_one; /* next */
230 *p++ = xdr_zero; /* cookie, first word */
231 *p++ = xdr_one; /* cookie, second word */
232 *p++ = xdr_one; /* entry len */
233 memcpy(p, ".\0\0\0", 4); /* entry */
234 p++;
235 *p++ = xdr_one; /* bitmap length */
236 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
237 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400238 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 }
240
241 *p++ = xdr_one; /* next */
242 *p++ = xdr_zero; /* cookie, first word */
243 *p++ = xdr_two; /* cookie, second word */
244 *p++ = xdr_two; /* entry len */
245 memcpy(p, "..\0\0", 4); /* entry */
246 p++;
247 *p++ = xdr_one; /* bitmap length */
248 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
249 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400250 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 readdir->pgbase = (char *)p - (char *)start;
253 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800254 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255}
256
Trond Myklebust65de8722008-12-23 15:21:44 -0500257static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
258{
259 int res = 0;
260
261 might_sleep();
262
263 if (*timeout <= 0)
264 *timeout = NFS4_POLL_RETRY_MIN;
265 if (*timeout > NFS4_POLL_RETRY_MAX)
266 *timeout = NFS4_POLL_RETRY_MAX;
Jeff Laytond3103102011-12-01 22:44:39 +0100267 freezable_schedule_timeout_killable(*timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500268 if (fatal_signal_pending(current))
269 res = -ERESTARTSYS;
270 *timeout <<= 1;
271 return res;
272}
273
274/* This is the error handling routine for processes that are allowed
275 * to sleep.
276 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800277static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500278{
279 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500280 struct nfs4_state *state = exception->state;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500281 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500282 int ret = errorcode;
283
284 exception->retry = 0;
285 switch(errorcode) {
286 case 0:
287 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500288 case -NFS4ERR_OPENMODE:
Bryan Schumaker011e2a72012-06-20 15:53:43 -0400289 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400290 nfs4_inode_return_delegation(inode);
Trond Myklebust3114ea72012-03-07 16:39:06 -0500291 exception->retry = 1;
292 return 0;
293 }
294 if (state == NULL)
295 break;
296 nfs4_schedule_stateid_recovery(server, state);
297 goto wait_on_recovery;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500298 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500299 case -NFS4ERR_ADMIN_REVOKED:
300 case -NFS4ERR_BAD_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500301 if (state == NULL)
302 break;
Trond Myklebust14977482012-03-27 18:31:25 -0400303 nfs_remove_bad_delegation(state->inode);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500304 nfs4_schedule_stateid_recovery(server, state);
305 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400306 case -NFS4ERR_EXPIRED:
307 if (state != NULL)
308 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust65de8722008-12-23 15:21:44 -0500309 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500310 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500311 nfs4_schedule_lease_recovery(clp);
312 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500313#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400314 case -NFS4ERR_BADSESSION:
315 case -NFS4ERR_BADSLOT:
316 case -NFS4ERR_BAD_HIGH_SLOT:
317 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
318 case -NFS4ERR_DEADSESSION:
319 case -NFS4ERR_SEQ_FALSE_RETRY:
320 case -NFS4ERR_SEQ_MISORDERED:
321 dprintk("%s ERROR: %d Reset session\n", __func__,
322 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400323 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400324 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500325#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500326 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500327 if (exception->timeout > HZ) {
328 /* We have retried a decent amount, time to
329 * fail
330 */
331 ret = -EBUSY;
332 break;
333 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500334 case -NFS4ERR_GRACE:
335 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -0500336 case -EKEYEXPIRED:
Trond Myklebust65de8722008-12-23 15:21:44 -0500337 ret = nfs4_delay(server->client, &exception->timeout);
338 if (ret != 0)
339 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400340 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500341 case -NFS4ERR_OLD_STATEID:
342 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800343 break;
344 case -NFS4ERR_BADOWNER:
345 /* The following works around a Linux server bug! */
346 case -NFS4ERR_BADNAME:
347 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
348 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
349 exception->retry = 1;
350 printk(KERN_WARNING "NFS: v4 server %s "
351 "does not accept raw "
352 "uid/gids. "
353 "Reenabling the idmapper.\n",
354 server->nfs_client->cl_hostname);
355 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500356 }
357 /* We failed to handle the error */
358 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500359wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500360 ret = nfs4_wait_clnt_recover(clp);
361 if (ret == 0)
362 exception->retry = 1;
363 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500364}
365
366
Trond Myklebust452e9352010-07-31 14:29:06 -0400367static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 spin_lock(&clp->cl_lock);
370 if (time_before(clp->cl_last_renewal,timestamp))
371 clp->cl_last_renewal = timestamp;
372 spin_unlock(&clp->cl_lock);
373}
374
Trond Myklebust452e9352010-07-31 14:29:06 -0400375static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
376{
377 do_renew_lease(server->nfs_client, timestamp);
378}
379
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400380#if defined(CONFIG_NFS_V4_1)
381
Trond Myklebustd185a332010-06-16 09:52:25 -0400382static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400383{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500384 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400385 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500386 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400387
Benny Halevydfb4f3092010-09-24 09:17:01 -0400388 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400389 /* just wake up the next guy waiting since
390 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500391 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500392 return;
Andy Adamson13615872009-04-01 09:22:17 -0400393 }
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500394 tbl = res->sr_slot->table;
395 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500396
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500397 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500398 /* Be nice to the server: try to ensure that the last transmitted
399 * value for highest_user_slotid <= target_highest_slotid
400 */
401 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
402 send_new_highest_used_slotid = true;
403
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500404 if (nfs41_wake_and_assign_slot(tbl, res->sr_slot)) {
405 send_new_highest_used_slotid = false;
406 goto out_unlock;
407 }
Trond Myklebustf4af6e2a2012-11-20 14:17:32 -0500408 nfs4_free_slot(tbl, res->sr_slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500409
410 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
411 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500412out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500413 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400414 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500415 if (send_new_highest_used_slotid)
416 nfs41_server_notify_highest_slotid_update(session->clp);
Andy Adamson13615872009-04-01 09:22:17 -0400417}
418
Trond Myklebust14516c32010-07-31 14:29:06 -0400419static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400420{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500421 struct nfs4_session *session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500422 struct nfs4_slot *slot;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400423 unsigned long timestamp;
Trond Myklebust14516c32010-07-31 14:29:06 -0400424 struct nfs_client *clp;
Trond Myklebust85563072012-12-11 10:31:12 -0500425 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400426
427 /*
428 * sr_status remains 1 if an RPC level error occurred. The server
429 * may or may not have processed the sequence operation..
430 * Proceed as if the server received and processed the sequence
431 * operation.
432 */
433 if (res->sr_status == 1)
434 res->sr_status = NFS_OK;
435
Bryan Schumaker468f8612011-04-18 15:57:32 -0400436 /* don't increment the sequence number if the task wasn't sent */
437 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400438 goto out;
439
Trond Myklebust933602e2012-11-16 12:12:38 -0500440 slot = res->sr_slot;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500441 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500442
Andy Adamson691daf32009-12-04 15:55:39 -0500443 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400444 switch (res->sr_status) {
445 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400446 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust933602e2012-11-16 12:12:38 -0500447 ++slot->seq_nr;
448 timestamp = slot->renewal_time;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500449 clp = session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400450 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500451 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500452 if (res->sr_status_flags != 0)
453 nfs4_schedule_lease_recovery(clp);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500454 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400455 break;
456 case -NFS4ERR_DELAY:
457 /* The server detected a resend of the RPC call and
458 * returned NFS4ERR_DELAY as per Section 2.10.6.2
459 * of RFC5661.
460 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500461 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400462 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500463 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500464 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400465 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500466 case -NFS4ERR_BADSLOT:
467 /*
468 * The slot id we used was probably retired. Try again
469 * using a different slot id.
470 */
471 if (rpc_restart_call_prepare(task)) {
472 task->tk_status = 0;
473 ret = 0;
474 }
475 break;
Trond Myklebust14516c32010-07-31 14:29:06 -0400476 default:
477 /* Just update the slot sequence no. */
Trond Myklebust933602e2012-11-16 12:12:38 -0500478 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400479 }
480out:
481 /* The session may be reset by one of the error handlers. */
482 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400483 nfs41_sequence_free_slot(res);
Trond Myklebust85563072012-12-11 10:31:12 -0500484 return ret;
Trond Myklebust14516c32010-07-31 14:29:06 -0400485out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400486 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400487 goto out;
488 rpc_delay(task, NFS4_POLL_RETRY_MAX);
489 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400490}
491
Trond Myklebust14516c32010-07-31 14:29:06 -0400492static int nfs4_sequence_done(struct rpc_task *task,
493 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400494{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500495 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400496 return 1;
497 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400498}
499
Trond Myklebust9d12b212012-01-17 22:04:25 -0500500static void nfs41_init_sequence(struct nfs4_sequence_args *args,
501 struct nfs4_sequence_res *res, int cache_reply)
502{
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500503 args->sa_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500504 args->sa_cache_this = 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400505 args->sa_privileged = 0;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500506 if (cache_reply)
507 args->sa_cache_this = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500508 res->sr_slot = NULL;
509}
510
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400511static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
512{
513 args->sa_privileged = 1;
514}
515
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000516int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400517 struct nfs4_sequence_args *args,
518 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400519 struct rpc_task *task)
520{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400521 struct nfs4_slot *slot;
522 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400523
524 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400525 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400526 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400527 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400528
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400529 tbl = &session->fc_slot_table;
530
Trond Myklebust69d206b2012-11-22 13:21:02 -0500531 task->tk_timeout = 0;
532
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400533 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400534 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400535 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500536 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500537 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400538 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400539 }
540
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500541 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500542 if (IS_ERR(slot)) {
543 /* If out of memory, try again in 1/4 second */
544 if (slot == ERR_PTR(-ENOMEM))
545 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400546 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400547 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400548 }
549 spin_unlock(&tbl->slot_tbl_lock);
550
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500551 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400552
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500553 dprintk("<-- %s slotid=%d seqid=%d\n", __func__,
554 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400555
Benny Halevydfb4f3092010-09-24 09:17:01 -0400556 res->sr_slot = slot;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400557 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400558 /*
559 * sr_status is only set in decode_sequence, and so will remain
560 * set to 1 if an rpc level failure occurs.
561 */
562 res->sr_status = 1;
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400563out_success:
564 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400565 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400566out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400567 /* Privileged tasks are queued with top priority */
568 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400569 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
570 NULL, RPC_PRIORITY_PRIVILEGED);
571 else
572 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400573 spin_unlock(&tbl->slot_tbl_lock);
574 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400575}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000576EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400577
Trond Myklebust035168a2010-06-16 09:52:26 -0400578int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400579 struct nfs4_sequence_args *args,
580 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400581 struct rpc_task *task)
582{
Trond Myklebust035168a2010-06-16 09:52:26 -0400583 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400584 int ret = 0;
585
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400586 if (session == NULL) {
587 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400588 goto out;
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400589 }
Trond Myklebust035168a2010-06-16 09:52:26 -0400590
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500591 dprintk("--> %s clp %p session %p sr_slot %d\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400592 __func__, session->clp, session, res->sr_slot ?
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500593 res->sr_slot->slot_nr : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400594
Trond Myklebust9d12b212012-01-17 22:04:25 -0500595 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400596out:
597 dprintk("<-- %s status=%d\n", __func__, ret);
598 return ret;
599}
600
601struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400602 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400603 struct nfs4_sequence_args *seq_args;
604 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400605};
606
607static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
608{
609 struct nfs41_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400610 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400611
Trond Myklebust035168a2010-06-16 09:52:26 -0400612 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
613
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400614 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400615}
616
Andy Adamson69ab40c2009-04-01 09:22:19 -0400617static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
618{
619 struct nfs41_call_sync_data *data = calldata;
620
Trond Myklebust14516c32010-07-31 14:29:06 -0400621 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400622}
623
Trond Myklebust17280172012-03-11 13:11:00 -0400624static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400625 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400626 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400627};
628
Bryan Schumaker7c513052011-03-24 17:12:24 +0000629static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
630 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400631 struct rpc_message *msg,
632 struct nfs4_sequence_args *args,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400633 struct nfs4_sequence_res *res)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400634{
635 int ret;
636 struct rpc_task *task;
637 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400638 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400639 .seq_args = args,
640 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400641 };
642 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000643 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400644 .rpc_message = msg,
645 .callback_ops = &nfs41_call_sync_ops,
646 .callback_data = &data
647 };
648
Andy Adamsonce5039c2009-04-01 09:22:13 -0400649 task = rpc_run_task(&task_setup);
650 if (IS_ERR(task))
651 ret = PTR_ERR(task);
652 else {
653 ret = task->tk_status;
654 rpc_put_task(task);
655 }
656 return ret;
657}
658
Trond Myklebustdf896452010-06-16 09:52:26 -0400659#else
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400660static
Trond Myklebust9d12b212012-01-17 22:04:25 -0500661void nfs41_init_sequence(struct nfs4_sequence_args *args,
662 struct nfs4_sequence_res *res, int cache_reply)
663{
664}
665
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400666static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
667{
668}
669
670
Trond Myklebust14516c32010-07-31 14:29:06 -0400671static int nfs4_sequence_done(struct rpc_task *task,
672 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400673{
Trond Myklebust14516c32010-07-31 14:29:06 -0400674 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400675}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400676#endif /* CONFIG_NFS_V4_1 */
677
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400678static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000679int _nfs4_call_sync(struct rpc_clnt *clnt,
680 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400681 struct rpc_message *msg,
682 struct nfs4_sequence_args *args,
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400683 struct nfs4_sequence_res *res)
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400684{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000685 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400686}
687
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400688static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000689int nfs4_call_sync(struct rpc_clnt *clnt,
690 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000691 struct rpc_message *msg,
692 struct nfs4_sequence_args *args,
693 struct nfs4_sequence_res *res,
694 int cache_reply)
695{
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400696 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000697 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400698 args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000699}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400700
Trond Myklebust38478b22006-05-25 01:40:57 -0400701static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
Trond Myklebust38478b22006-05-25 01:40:57 -0400703 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
Trond Myklebust38478b22006-05-25 01:40:57 -0400705 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400706 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700707 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400708 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700709 dir->i_version = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400710 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711}
712
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100713struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400714 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100715 struct nfs_openargs o_arg;
716 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100717 struct nfs_open_confirmargs c_arg;
718 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500719 struct nfs4_string owner_name;
720 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100721 struct nfs_fattr f_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100722 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400723 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100724 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400725 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100726 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100727 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400728 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100729 int rpc_status;
730 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100731};
732
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400733
734static void nfs4_init_opendata_res(struct nfs4_opendata *p)
735{
736 p->o_res.f_attr = &p->f_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400737 p->o_res.seqid = p->o_arg.seqid;
738 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400739 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400740 p->o_res.access_request = p->o_arg.access;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400741 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500742 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400743}
744
Al Viro82a2c1b2011-06-22 18:30:55 -0400745static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500746 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400747 const struct iattr *attrs,
748 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100749{
Al Viro82a2c1b2011-06-22 18:30:55 -0400750 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100751 struct inode *dir = parent->d_inode;
752 struct nfs_server *server = NFS_SERVER(dir);
753 struct nfs4_opendata *p;
754
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400755 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100756 if (p == NULL)
757 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400758 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100759 if (p->o_arg.seqid == NULL)
760 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400761 nfs_sb_active(dentry->d_sb);
762 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100763 p->dir = parent;
764 p->owner = sp;
765 atomic_inc(&sp->so_count);
766 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500767 p->o_arg.open_flags = flags;
768 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700769 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
770 * will return permission denied for all bits until close */
771 if (!(flags & O_EXCL)) {
772 /* ask server to check for all possible rights as results
773 * are cached */
774 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
775 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700776 }
David Howells7539bba2006-08-22 20:06:09 -0400777 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400778 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
779 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400780 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100781 p->o_arg.server = server;
782 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust1549210f2012-06-05 09:16:47 -0400783 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100784 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust536e43d2012-01-17 22:04:26 -0500785 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -0500786 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400787
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100788 p->o_arg.u.attrs = &p->attrs;
789 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500790
791 verf[0] = jiffies;
792 verf[1] = current->pid;
793 memcpy(p->o_arg.u.verifier.data, verf,
794 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100795 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100796 p->c_arg.fh = &p->o_res.fh;
797 p->c_arg.stateid = &p->o_res.stateid;
798 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400799 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400800 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100801 return p;
802err_free:
803 kfree(p);
804err:
805 dput(parent);
806 return NULL;
807}
808
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400809static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100810{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400811 struct nfs4_opendata *p = container_of(kref,
812 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400813 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400814
815 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400816 if (p->state != NULL)
817 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400818 nfs4_put_state_owner(p->owner);
819 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400820 dput(p->dentry);
821 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500822 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400823 kfree(p);
824}
825
826static void nfs4_opendata_put(struct nfs4_opendata *p)
827{
828 if (p != NULL)
829 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100830}
831
Trond Myklebust06f814a2006-01-03 09:55:07 +0100832static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
833{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100834 int ret;
835
Trond Myklebust06f814a2006-01-03 09:55:07 +0100836 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100837 return ret;
838}
839
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500840static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400841{
842 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500843
Trond Myklebust536e43d2012-01-17 22:04:26 -0500844 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500845 goto out;
846 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400847 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500848 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
849 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400850 break;
851 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500852 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
853 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400854 break;
855 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500856 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
857 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400858 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500859out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400860 return ret;
861}
862
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500863static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400864{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500865 if (delegation == NULL)
866 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500867 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400868 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500869 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400870 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500871 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400872 return 1;
873}
874
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500875static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100876{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500877 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100878 case FMODE_WRITE:
879 state->n_wronly++;
880 break;
881 case FMODE_READ:
882 state->n_rdonly++;
883 break;
884 case FMODE_READ|FMODE_WRITE:
885 state->n_rdwr++;
886 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500887 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100888}
889
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500890static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400891{
892 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -0500893 nfs4_stateid_copy(&state->stateid, stateid);
894 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500895 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400896 case FMODE_READ:
897 set_bit(NFS_O_RDONLY_STATE, &state->flags);
898 break;
899 case FMODE_WRITE:
900 set_bit(NFS_O_WRONLY_STATE, &state->flags);
901 break;
902 case FMODE_READ|FMODE_WRITE:
903 set_bit(NFS_O_RDWR_STATE, &state->flags);
904 }
905}
906
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500907static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400908{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400909 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500910 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400911 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400912}
913
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500914static 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 -0700915{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400916 /*
917 * Protect the call to nfs4_state_set_mode_locked and
918 * serialise the stateid update
919 */
920 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400921 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -0500922 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -0400923 set_bit(NFS_DELEGATED_STATE, &state->flags);
924 }
925 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500926 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400927 write_sequnlock(&state->seqlock);
928 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500929 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700930 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931}
932
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500933static 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 -0500934{
935 struct nfs_inode *nfsi = NFS_I(state->inode);
936 struct nfs_delegation *deleg_cur;
937 int ret = 0;
938
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500939 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500940
941 rcu_read_lock();
942 deleg_cur = rcu_dereference(nfsi->delegation);
943 if (deleg_cur == NULL)
944 goto no_delegation;
945
946 spin_lock(&deleg_cur->lock);
947 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500948 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500949 goto no_delegation_unlock;
950
951 if (delegation == NULL)
952 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -0500953 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -0500954 goto no_delegation_unlock;
955
Trond Myklebustb7391f42008-12-23 15:21:52 -0500956 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500957 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500958 ret = 1;
959no_delegation_unlock:
960 spin_unlock(&deleg_cur->lock);
961no_delegation:
962 rcu_read_unlock();
963
964 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500965 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500966 ret = 1;
967 }
968
969 return ret;
970}
971
972
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500973static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400974{
975 struct nfs_delegation *delegation;
976
977 rcu_read_lock();
978 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500979 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -0400980 rcu_read_unlock();
981 return;
982 }
983 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400984 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400985}
986
Trond Myklebust6ee41262007-07-08 14:11:36 -0400987static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400988{
989 struct nfs4_state *state = opendata->state;
990 struct nfs_inode *nfsi = NFS_I(state->inode);
991 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -0500992 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500993 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400994 nfs4_stateid stateid;
995 int ret = -EAGAIN;
996
Trond Myklebustaac00a82007-07-05 19:02:21 -0400997 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500998 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400999 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001000 if (can_open_cached(state, fmode, open_mode)) {
1001 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001002 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001003 goto out_return_state;
1004 }
1005 spin_unlock(&state->owner->so_lock);
1006 }
Trond Myklebust34310432008-12-23 15:21:38 -05001007 rcu_read_lock();
1008 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001009 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001010 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001011 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001012 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001013 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001014 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001015 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001016 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001017 if (ret != 0)
1018 goto out;
1019 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001020
1021 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001022 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001023 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001024 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001025out:
1026 return ERR_PTR(ret);
1027out_return_state:
1028 atomic_inc(&state->count);
1029 return state;
1030}
1031
Andy Adamsone23008e2012-10-02 21:07:32 -04001032static void
1033nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1034{
1035 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1036 struct nfs_delegation *delegation;
1037 int delegation_flags = 0;
1038
1039 rcu_read_lock();
1040 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1041 if (delegation)
1042 delegation_flags = delegation->flags;
1043 rcu_read_unlock();
1044 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1045 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1046 "returning a delegation for "
1047 "OPEN(CLAIM_DELEGATE_CUR)\n",
1048 clp->cl_hostname);
1049 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1050 nfs_inode_set_delegation(state->inode,
1051 data->owner->so_cred,
1052 &data->o_res);
1053 else
1054 nfs_inode_reclaim_delegation(state->inode,
1055 data->owner->so_cred,
1056 &data->o_res);
1057}
1058
1059/*
1060 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1061 * and update the nfs4_state.
1062 */
1063static struct nfs4_state *
1064_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1065{
1066 struct inode *inode = data->state->inode;
1067 struct nfs4_state *state = data->state;
1068 int ret;
1069
1070 if (!data->rpc_done) {
1071 ret = data->rpc_status;
1072 goto err;
1073 }
1074
1075 ret = -ESTALE;
1076 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1077 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1078 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1079 goto err;
1080
1081 ret = -ENOMEM;
1082 state = nfs4_get_open_state(inode, data->owner);
1083 if (state == NULL)
1084 goto err;
1085
1086 ret = nfs_refresh_inode(inode, &data->f_attr);
1087 if (ret)
1088 goto err;
1089
1090 if (data->o_res.delegation_type != 0)
1091 nfs4_opendata_check_deleg(data, state);
1092 update_open_stateid(state, &data->o_res.stateid, NULL,
1093 data->o_arg.fmode);
1094
1095 return state;
1096err:
1097 return ERR_PTR(ret);
1098
1099}
1100
1101static struct nfs4_state *
1102_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001103{
1104 struct inode *inode;
1105 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001106 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001107
Trond Myklebustaac00a82007-07-05 19:02:21 -04001108 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001109 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001110 goto out;
1111 }
1112
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001113 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001114 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001115 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001116 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001117 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001118 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001119 goto err;
1120 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001121 state = nfs4_get_open_state(inode, data->owner);
1122 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001123 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001124 if (data->o_res.delegation_type != 0)
1125 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001126 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001127 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001128 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001129out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001130 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001131err_put_inode:
1132 iput(inode);
1133err:
1134 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001135}
1136
Andy Adamsone23008e2012-10-02 21:07:32 -04001137static struct nfs4_state *
1138nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1139{
1140 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1141 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1142 return _nfs4_opendata_to_nfs4_state(data);
1143}
1144
Trond Myklebust864472e2006-01-03 09:55:15 +01001145static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1146{
1147 struct nfs_inode *nfsi = NFS_I(state->inode);
1148 struct nfs_open_context *ctx;
1149
1150 spin_lock(&state->inode->i_lock);
1151 list_for_each_entry(ctx, &nfsi->open_files, list) {
1152 if (ctx->state != state)
1153 continue;
1154 get_nfs_open_context(ctx);
1155 spin_unlock(&state->inode->i_lock);
1156 return ctx;
1157 }
1158 spin_unlock(&state->inode->i_lock);
1159 return ERR_PTR(-ENOENT);
1160}
1161
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001162static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1163{
1164 struct nfs4_opendata *opendata;
1165
Al Viro3d4ff432011-06-22 18:40:12 -04001166 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001167 if (opendata == NULL)
1168 return ERR_PTR(-ENOMEM);
1169 opendata->state = state;
1170 atomic_inc(&state->count);
1171 return opendata;
1172}
1173
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001174static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001175{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001176 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001177 int ret;
1178
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001179 opendata->o_arg.open_flags = 0;
1180 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001181 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1182 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1183 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001184 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001185 if (ret != 0)
1186 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001187 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001188 if (IS_ERR(newstate))
1189 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001190 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001191 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001192 return 0;
1193}
1194
1195static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1196{
Trond Myklebust864472e2006-01-03 09:55:15 +01001197 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001198 int ret;
1199
1200 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001201 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001202 smp_rmb();
1203 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001204 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001205 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001206 if (ret != 0)
1207 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001208 if (newstate != state)
1209 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001210 }
1211 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001212 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001213 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001214 if (ret != 0)
1215 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001216 if (newstate != state)
1217 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001218 }
1219 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001220 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001221 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001222 if (ret != 0)
1223 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001224 if (newstate != state)
1225 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001226 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001227 /*
1228 * We may have performed cached opens for all three recoveries.
1229 * Check if we need to update the current stateid.
1230 */
1231 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001232 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001233 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001234 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001235 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001236 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001237 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001238 return 0;
1239}
1240
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241/*
1242 * OPEN_RECLAIM:
1243 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001245static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001247 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001248 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001249 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 int status;
1251
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001252 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1253 if (IS_ERR(opendata))
1254 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001255 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1256 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001257 rcu_read_lock();
1258 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001259 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001260 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001261 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001262 opendata->o_arg.u.delegation_type = delegation_type;
1263 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001264 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 return status;
1266}
1267
Trond Myklebust539cd032007-06-05 11:46:42 -04001268static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269{
1270 struct nfs_server *server = NFS_SERVER(state->inode);
1271 struct nfs4_exception exception = { };
1272 int err;
1273 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001274 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001275 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001276 break;
1277 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 } while (exception.retry);
1279 return err;
1280}
1281
Trond Myklebust864472e2006-01-03 09:55:15 +01001282static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1283{
1284 struct nfs_open_context *ctx;
1285 int ret;
1286
1287 ctx = nfs4_state_find_open_context(state);
1288 if (IS_ERR(ctx))
1289 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001290 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001291 put_nfs_open_context(ctx);
1292 return ret;
1293}
1294
Trond Myklebust13437e12007-07-06 15:10:43 -04001295static 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 -07001296{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001297 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001298 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001300 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1301 if (IS_ERR(opendata))
1302 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001303 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebustf597c532012-03-04 18:13:56 -05001304 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust864472e2006-01-03 09:55:15 +01001305 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001306 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001307 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308}
1309
Trond Myklebust13437e12007-07-06 15:10:43 -04001310int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311{
1312 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001313 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 int err;
1315 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001316 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 switch (err) {
1318 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001319 case -ENOENT:
1320 case -ESTALE:
1321 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001322 case -NFS4ERR_BADSESSION:
1323 case -NFS4ERR_BADSLOT:
1324 case -NFS4ERR_BAD_HIGH_SLOT:
1325 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1326 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04001327 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001328 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 case -NFS4ERR_STALE_CLIENTID:
1330 case -NFS4ERR_STALE_STATEID:
1331 case -NFS4ERR_EXPIRED:
1332 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001333 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001334 goto out;
1335 case -ERESTARTSYS:
1336 /*
1337 * The show must go on: exit, but mark the
1338 * stateid as needing recovery.
1339 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001340 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001341 case -NFS4ERR_ADMIN_REVOKED:
1342 case -NFS4ERR_BAD_STATEID:
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001343 nfs_inode_find_state_and_recover(state->inode,
1344 stateid);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001345 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001346 case -EKEYEXPIRED:
1347 /*
1348 * User RPCSEC_GSS context has expired.
1349 * We cannot recover this stateid now, so
1350 * skip it and allow recovery thread to
1351 * proceed.
1352 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001353 case -ENOMEM:
1354 err = 0;
1355 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 }
1357 err = nfs4_handle_exception(server, err, &exception);
1358 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001359out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 return err;
1361}
1362
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001363static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1364{
1365 struct nfs4_opendata *data = calldata;
1366
1367 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001368 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001369 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001370 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001371 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001372 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001373 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001374}
1375
1376static void nfs4_open_confirm_release(void *calldata)
1377{
1378 struct nfs4_opendata *data = calldata;
1379 struct nfs4_state *state = NULL;
1380
1381 /* If this request hasn't been cancelled, do nothing */
1382 if (data->cancelled == 0)
1383 goto out_free;
1384 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001385 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001386 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001387 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001388 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001389 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001390out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001391 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001392}
1393
1394static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001395 .rpc_call_done = nfs4_open_confirm_done,
1396 .rpc_release = nfs4_open_confirm_release,
1397};
1398
1399/*
1400 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1401 */
1402static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1403{
1404 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1405 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001406 struct rpc_message msg = {
1407 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1408 .rpc_argp = &data->c_arg,
1409 .rpc_resp = &data->c_res,
1410 .rpc_cred = data->owner->so_cred,
1411 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001412 struct rpc_task_setup task_setup_data = {
1413 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001414 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001415 .callback_ops = &nfs4_open_confirm_ops,
1416 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001417 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001418 .flags = RPC_TASK_ASYNC,
1419 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 int status;
1421
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001422 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001423 data->rpc_done = 0;
1424 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001425 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001426 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001427 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001428 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001429 status = nfs4_wait_for_completion_rpc_task(task);
1430 if (status != 0) {
1431 data->cancelled = 1;
1432 smp_wmb();
1433 } else
1434 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001435 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 return status;
1437}
1438
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001439static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001441 struct nfs4_opendata *data = calldata;
1442 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001443
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001444 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1445 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001446 /*
1447 * Check if we still need to send an OPEN call, or if we can use
1448 * a delegation instead.
1449 */
1450 if (data->state != NULL) {
1451 struct nfs_delegation *delegation;
1452
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001453 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001454 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001455 rcu_read_lock();
1456 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001457 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1458 can_open_delegated(delegation, data->o_arg.fmode))
1459 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001460 rcu_read_unlock();
1461 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001462 /* Update client id. */
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001463 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001464 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001465 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Andy Adamsone23008e2012-10-02 21:07:32 -04001466 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001467 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1468 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001469 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001470 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001471 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001472 &data->o_res.seq_res,
1473 task) != 0)
1474 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001475 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001476unlock_no_action:
1477 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001478out_no_action:
1479 task->tk_action = NULL;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05001480 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001481}
1482
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001483static void nfs4_open_done(struct rpc_task *task, void *calldata)
1484{
1485 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001487 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001488
Trond Myklebust14516c32010-07-31 14:29:06 -04001489 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1490 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001491
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001492 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001493 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1494 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001495 case S_IFREG:
1496 break;
1497 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001498 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001499 break;
1500 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001501 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001502 break;
1503 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001504 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001505 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001506 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001507 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001508 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1509 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001510 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001511 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001512}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001513
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001514static void nfs4_open_release(void *calldata)
1515{
1516 struct nfs4_opendata *data = calldata;
1517 struct nfs4_state *state = NULL;
1518
1519 /* If this request hasn't been cancelled, do nothing */
1520 if (data->cancelled == 0)
1521 goto out_free;
1522 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001523 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001524 goto out_free;
1525 /* In case we need an open_confirm, no cleanup! */
1526 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1527 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001528 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001529 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001530 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001531out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001532 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001533}
1534
1535static const struct rpc_call_ops nfs4_open_ops = {
1536 .rpc_call_prepare = nfs4_open_prepare,
1537 .rpc_call_done = nfs4_open_done,
1538 .rpc_release = nfs4_open_release,
1539};
1540
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001541static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001542{
1543 struct inode *dir = data->dir->d_inode;
1544 struct nfs_server *server = NFS_SERVER(dir);
1545 struct nfs_openargs *o_arg = &data->o_arg;
1546 struct nfs_openres *o_res = &data->o_res;
1547 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001548 struct rpc_message msg = {
1549 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1550 .rpc_argp = o_arg,
1551 .rpc_resp = o_res,
1552 .rpc_cred = data->owner->so_cred,
1553 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001554 struct rpc_task_setup task_setup_data = {
1555 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001556 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001557 .callback_ops = &nfs4_open_ops,
1558 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001559 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001560 .flags = RPC_TASK_ASYNC,
1561 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001562 int status;
1563
Trond Myklebust9d12b212012-01-17 22:04:25 -05001564 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001565 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001566 data->rpc_done = 0;
1567 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001568 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001569 if (isrecover)
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001570 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04001571 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001572 if (IS_ERR(task))
1573 return PTR_ERR(task);
1574 status = nfs4_wait_for_completion_rpc_task(task);
1575 if (status != 0) {
1576 data->cancelled = 1;
1577 smp_wmb();
1578 } else
1579 status = data->rpc_status;
1580 rpc_put_task(task);
1581
1582 return status;
1583}
1584
1585static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1586{
1587 struct inode *dir = data->dir->d_inode;
1588 struct nfs_openres *o_res = &data->o_res;
1589 int status;
1590
1591 status = nfs4_run_open_task(data, 1);
1592 if (status != 0 || !data->rpc_done)
1593 return status;
1594
Trond Myklebust6926afd2012-01-07 13:22:46 -05001595 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1596
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001597 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1598 status = _nfs4_proc_open_confirm(data);
1599 if (status != 0)
1600 return status;
1601 }
1602
1603 return status;
1604}
1605
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001606static int nfs4_opendata_access(struct rpc_cred *cred,
1607 struct nfs4_opendata *opendata,
1608 struct nfs4_state *state, fmode_t fmode)
1609{
1610 struct nfs_access_entry cache;
1611 u32 mask;
1612
1613 /* access call failed or for some reason the server doesn't
1614 * support any access modes -- defer access call until later */
1615 if (opendata->o_res.access_supported == 0)
1616 return 0;
1617
1618 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001619 /* don't check MAY_WRITE - a newly created file may not have
1620 * write mode bits, but POSIX allows the creating process to write */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001621 if (fmode & FMODE_READ)
1622 mask |= MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001623 if (fmode & FMODE_EXEC)
1624 mask |= MAY_EXEC;
1625
1626 cache.cred = cred;
1627 cache.jiffies = jiffies;
1628 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1629 nfs_access_add_cache(state->inode, &cache);
1630
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001631 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001632 return 0;
1633
1634 /* even though OPEN succeeded, access is denied. Close the file */
1635 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001636 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001637}
1638
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001639/*
1640 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1641 */
1642static int _nfs4_proc_open(struct nfs4_opendata *data)
1643{
1644 struct inode *dir = data->dir->d_inode;
1645 struct nfs_server *server = NFS_SERVER(dir);
1646 struct nfs_openargs *o_arg = &data->o_arg;
1647 struct nfs_openres *o_res = &data->o_res;
1648 int status;
1649
1650 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001651 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001652 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001653 if (status != 0) {
1654 if (status == -NFS4ERR_BADNAME &&
1655 !(o_arg->open_flags & O_CREAT))
1656 return -ENOENT;
1657 return status;
1658 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001659
Trond Myklebust6926afd2012-01-07 13:22:46 -05001660 nfs_fattr_map_and_free_names(server, &data->f_attr);
1661
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001662 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001663 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001664 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1665 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001667 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001669 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 }
1671 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001672 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001673 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674}
1675
Andy Adamsond83217c2011-03-01 01:34:17 +00001676static int nfs4_recover_expired_lease(struct nfs_server *server)
1677{
1678 return nfs4_client_recover_expired_lease(server->nfs_client);
1679}
1680
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681/*
1682 * OPEN_EXPIRED:
1683 * reclaim state on the server after a network partition.
1684 * Assumes caller holds the appropriate lock
1685 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001686static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001688 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001689 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001691 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1692 if (IS_ERR(opendata))
1693 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001694 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001695 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001696 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001697 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001698 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699}
1700
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001701static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001702{
Trond Myklebust539cd032007-06-05 11:46:42 -04001703 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001704 struct nfs4_exception exception = { };
1705 int err;
1706
1707 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001708 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001709 switch (err) {
1710 default:
1711 goto out;
1712 case -NFS4ERR_GRACE:
1713 case -NFS4ERR_DELAY:
1714 nfs4_handle_exception(server, err, &exception);
1715 err = 0;
1716 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001717 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001718out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001719 return err;
1720}
1721
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1723{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001725 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
Trond Myklebust864472e2006-01-03 09:55:15 +01001727 ctx = nfs4_state_find_open_context(state);
1728 if (IS_ERR(ctx))
1729 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001730 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001731 put_nfs_open_context(ctx);
1732 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733}
1734
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001735#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001736static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001737{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001738 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001739 nfs4_stateid *stateid = &state->stateid;
1740 int status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001741
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001742 /* If a state reset has been done, test_stateid is unneeded */
1743 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1744 return;
1745
1746 status = nfs41_test_stateid(server, stateid);
1747 if (status != NFS_OK) {
1748 /* Free the stateid unless the server explicitly
1749 * informs us the stateid is unrecognized. */
1750 if (status != -NFS4ERR_BAD_STATEID)
1751 nfs41_free_stateid(server, stateid);
Bryan Schumaker69388672012-09-26 15:25:52 -04001752 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001753
Bryan Schumaker69388672012-09-26 15:25:52 -04001754 write_seqlock(&state->seqlock);
1755 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1756 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001757 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1758 }
1759}
1760
1761/**
1762 * nfs41_check_open_stateid - possibly free an open stateid
1763 *
1764 * @state: NFSv4 state for an inode
1765 *
1766 * Returns NFS_OK if recovery for this stateid is now finished.
1767 * Otherwise a negative NFS4ERR value is returned.
1768 */
1769static int nfs41_check_open_stateid(struct nfs4_state *state)
1770{
1771 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04001772 nfs4_stateid *stateid = &state->open_stateid;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001773 int status;
1774
1775 /* If a state reset has been done, test_stateid is unneeded */
1776 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1777 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1778 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1779 return -NFS4ERR_BAD_STATEID;
1780
1781 status = nfs41_test_stateid(server, stateid);
1782 if (status != NFS_OK) {
1783 /* Free the stateid unless the server explicitly
1784 * informs us the stateid is unrecognized. */
1785 if (status != -NFS4ERR_BAD_STATEID)
1786 nfs41_free_stateid(server, stateid);
1787
1788 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1789 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1790 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001791 }
1792 return status;
1793}
1794
1795static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1796{
Chuck Levereb64cf92012-07-11 16:30:05 -04001797 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001798
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001799 nfs41_clear_delegation_stateid(state);
1800 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04001801 if (status != NFS_OK)
1802 status = nfs4_open_expired(sp, state);
1803 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001804}
1805#endif
1806
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001808 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1809 * fields corresponding to attributes that were used to store the verifier.
1810 * Make sure we clobber those fields in the later setattr call
1811 */
1812static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1813{
1814 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1815 !(sattr->ia_valid & ATTR_ATIME_SET))
1816 sattr->ia_valid |= ATTR_ATIME;
1817
1818 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1819 !(sattr->ia_valid & ATTR_MTIME_SET))
1820 sattr->ia_valid |= ATTR_MTIME;
1821}
1822
1823/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001824 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 */
Andy Adamson82be4172012-05-23 05:02:35 -04001826static int _nfs4_do_open(struct inode *dir,
1827 struct dentry *dentry,
1828 fmode_t fmode,
1829 int flags,
1830 struct iattr *sattr,
1831 struct rpc_cred *cred,
1832 struct nfs4_state **res,
1833 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834{
1835 struct nfs4_state_owner *sp;
1836 struct nfs4_state *state = NULL;
1837 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001838 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001839 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
1841 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001843 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1844 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1846 goto out_err;
1847 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001848 status = nfs4_recover_expired_lease(server);
1849 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001850 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001851 if (dentry->d_inode != NULL)
1852 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001853 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001854 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001855 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001856 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
Andy Adamson82be4172012-05-23 05:02:35 -04001858 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
1859 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
1860 if (!opendata->f_attr.mdsthreshold)
1861 goto err_opendata_put;
Trond Myklebust1549210f2012-06-05 09:16:47 -04001862 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04001863 }
Al Viro82a2c1b2011-06-22 18:30:55 -04001864 if (dentry->d_inode != NULL)
1865 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001866
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001867 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001869 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001871 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001872 status = PTR_ERR(state);
1873 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001874 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001875 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001876 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001877
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001878 status = nfs4_opendata_access(cred, opendata, state, fmode);
1879 if (status != 0)
1880 goto err_opendata_put;
1881
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001882 if (opendata->o_arg.open_flags & O_EXCL) {
1883 nfs4_exclusive_attrset(opendata, sattr);
1884
1885 nfs_fattr_init(opendata->o_res.f_attr);
1886 status = nfs4_do_setattr(state->inode, cred,
1887 opendata->o_res.f_attr, sattr,
1888 state);
1889 if (status == 0)
1890 nfs_setattr_update_inode(state->inode, sattr);
1891 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1892 }
Andy Adamson82be4172012-05-23 05:02:35 -04001893
1894 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
1895 *ctx_th = opendata->f_attr.mdsthreshold;
1896 else
1897 kfree(opendata->f_attr.mdsthreshold);
1898 opendata->f_attr.mdsthreshold = NULL;
1899
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001900 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 *res = state;
1903 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001904err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04001905 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001906 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001907err_put_state_owner:
1908 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 *res = NULL;
1911 return status;
1912}
1913
1914
Andy Adamson82be4172012-05-23 05:02:35 -04001915static struct nfs4_state *nfs4_do_open(struct inode *dir,
1916 struct dentry *dentry,
1917 fmode_t fmode,
1918 int flags,
1919 struct iattr *sattr,
1920 struct rpc_cred *cred,
1921 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922{
1923 struct nfs4_exception exception = { };
1924 struct nfs4_state *res;
1925 int status;
1926
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001927 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 do {
Andy Adamson82be4172012-05-23 05:02:35 -04001929 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
1930 &res, ctx_th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 if (status == 0)
1932 break;
1933 /* NOTE: BAD_SEQID means the server and client disagree about the
1934 * book-keeping w.r.t. state-changing operations
1935 * (OPEN/CLOSE/LOCK/LOCKU...)
1936 * It is actually a sign of a bug on the client or on the server.
1937 *
1938 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001939 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 * have unhashed the old state_owner for us, and that we can
1941 * therefore safely retry using a new one. We should still warn
1942 * the user though...
1943 */
1944 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04001945 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001946 " returned a bad sequence-id error!\n",
1947 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 exception.retry = 1;
1949 continue;
1950 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001951 /*
1952 * BAD_STATEID on OPEN means that the server cancelled our
1953 * state before it received the OPEN_CONFIRM.
1954 * Recover by retrying the request as per the discussion
1955 * on Page 181 of RFC3530.
1956 */
1957 if (status == -NFS4ERR_BAD_STATEID) {
1958 exception.retry = 1;
1959 continue;
1960 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001961 if (status == -EAGAIN) {
1962 /* We must have found a delegation */
1963 exception.retry = 1;
1964 continue;
1965 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1967 status, &exception));
1968 } while (exception.retry);
1969 return res;
1970}
1971
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001972static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1973 struct nfs_fattr *fattr, struct iattr *sattr,
1974 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001976 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001978 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 .iap = sattr,
1980 .server = server,
1981 .bitmask = server->attr_bitmask,
1982 };
1983 struct nfs_setattrres res = {
1984 .fattr = fattr,
1985 .server = server,
1986 };
1987 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001988 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1989 .rpc_argp = &arg,
1990 .rpc_resp = &res,
1991 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001993 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001994 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
Trond Myklebust0e574af2005-10-27 22:12:38 -04001996 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
Trond Myklebust4fc87962012-03-08 17:42:01 -05001998 if (state != NULL) {
Trond Myklebust2a369152012-08-13 18:54:45 -04001999 struct nfs_lockowner lockowner = {
2000 .l_owner = current->files,
2001 .l_pid = current->tgid,
2002 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002003 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002004 &lockowner);
Trond Myklebust4fc87962012-03-08 17:42:01 -05002005 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2006 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002007 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002008 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002009 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
Bryan Schumaker7c513052011-03-24 17:12:24 +00002011 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002012 if (status == 0 && state != NULL)
2013 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002014 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015}
2016
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002017static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2018 struct nfs_fattr *fattr, struct iattr *sattr,
2019 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002021 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002022 struct nfs4_exception exception = {
2023 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002024 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002025 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 int err;
2027 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04002028 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2029 switch (err) {
2030 case -NFS4ERR_OPENMODE:
2031 if (state && !(state->state & FMODE_WRITE)) {
2032 err = -EBADF;
2033 if (sattr->ia_valid & ATTR_OPEN)
2034 err = -EACCES;
2035 goto out;
2036 }
2037 }
2038 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002040out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 return err;
2042}
2043
2044struct nfs4_closedata {
2045 struct inode *inode;
2046 struct nfs4_state *state;
2047 struct nfs_closeargs arg;
2048 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002049 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002050 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002051 bool roc;
2052 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053};
2054
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002055static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002056{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002057 struct nfs4_closedata *calldata = data;
2058 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002059 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002060
Fred Isamanf7e89172011-01-06 11:36:32 +00002061 if (calldata->roc)
2062 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002063 nfs4_put_open_state(calldata->state);
2064 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002065 nfs4_put_state_owner(sp);
Weston Andros Adamson324d0032012-10-30 17:01:39 -04002066 nfs_sb_deactive_async(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002067 kfree(calldata);
2068}
2069
Trond Myklebust88069f72009-12-08 08:33:16 -05002070static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2071 fmode_t fmode)
2072{
2073 spin_lock(&state->owner->so_lock);
2074 if (!(fmode & FMODE_READ))
2075 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2076 if (!(fmode & FMODE_WRITE))
2077 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2078 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2079 spin_unlock(&state->owner->so_lock);
2080}
2081
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002082static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002084 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 struct nfs_server *server = NFS_SERVER(calldata->inode);
2087
Chuck Levera3ca5652012-03-01 17:00:40 -05002088 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002089 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2090 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 /* hmm. we are done with the inode, and in the process of freeing
2092 * the state_owner. we keep this around to process errors
2093 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 switch (task->tk_status) {
2095 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002096 if (calldata->roc)
2097 pnfs_roc_set_barrier(state->inode,
2098 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002099 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002100 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002101 nfs4_close_clear_stateid_flags(state,
2102 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 break;
2104 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002105 case -NFS4ERR_OLD_STATEID:
2106 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002108 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002109 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002111 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2112 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002114 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002115 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002116 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117}
2118
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002119static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002121 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002122 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002123 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002124 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002125
Chuck Levera3ca5652012-03-01 17:00:40 -05002126 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002127 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07002128 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04002129
Trond Myklebust88069f72009-12-08 08:33:16 -05002130 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2131 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002132 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002133 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002134 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002135 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002136 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2137 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2138 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002139 }
2140 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002141 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2142 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2143 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002144 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002145 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002146 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002147
2148 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002149 /* Note: exit _without_ calling nfs4_close_done */
2150 task->tk_action = NULL;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002151 nfs4_sequence_done(task, &calldata->res.seq_res);
Chuck Levera3ca5652012-03-01 17:00:40 -05002152 goto out;
Trond Myklebust95121352005-10-18 14:20:12 -07002153 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002154
Fred Isamanf7e89172011-01-06 11:36:32 +00002155 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002156 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002157 if (calldata->roc &&
Trond Myklebust7fdab062012-09-20 20:15:57 -04002158 pnfs_roc_drain(inode, &calldata->roc_barrier, task))
Chuck Levera3ca5652012-03-01 17:00:40 -05002159 goto out;
Fred Isamanf7e89172011-01-06 11:36:32 +00002160 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002161
Trond Myklebust516a6af2005-10-27 22:12:41 -04002162 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002163 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002164 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002165 &calldata->arg.seq_args,
2166 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002167 task) != 0)
2168 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002169out:
2170 dprintk("%s: done!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171}
2172
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002173static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002174 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002175 .rpc_call_done = nfs4_close_done,
2176 .rpc_release = nfs4_free_closedata,
2177};
2178
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179/*
2180 * It is possible for data to be read/written from a mem-mapped file
2181 * after the sys_close call (which hits the vfs layer as a flush).
2182 * This means that we can't safely call nfsv4 close on a file until
2183 * the inode is cleared. This in turn means that we are not good
2184 * NFSv4 citizens - we do not indicate to the server to update the file's
2185 * share state even when we are done with one of the three share
2186 * stateid's in the inode.
2187 *
2188 * NOTE: Caller must be holding the sp->so_owner semaphore!
2189 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002190int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002192 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002194 struct nfs4_state_owner *sp = state->owner;
2195 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002196 struct rpc_message msg = {
2197 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2198 .rpc_cred = state->owner->so_cred,
2199 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002200 struct rpc_task_setup task_setup_data = {
2201 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002202 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002203 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002204 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002205 .flags = RPC_TASK_ASYNC,
2206 };
Trond Myklebust95121352005-10-18 14:20:12 -07002207 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002209 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002211 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002212 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002213 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002215 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002216 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002218 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002219 if (calldata->arg.seqid == NULL)
2220 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002221 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002222 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002223 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002224 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002225 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002226 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002227 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002228
Trond Myklebust1174dd12010-12-21 10:52:24 -05002229 msg.rpc_argp = &calldata->arg;
2230 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002231 task_setup_data.callback_data = calldata;
2232 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002233 if (IS_ERR(task))
2234 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002235 status = 0;
2236 if (wait)
2237 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002238 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002239 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002240out_free_calldata:
2241 kfree(calldata);
2242out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002243 nfs4_put_open_state(state);
2244 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002245 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246}
2247
Trond Myklebust2b484292010-09-17 10:56:51 -04002248static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002249nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 struct nfs4_state *state;
2252
Trond Myklebust565277f2007-10-15 18:17:53 -04002253 /* Protect against concurrent sillydeletes */
Andy Adamson82be4172012-05-23 05:02:35 -04002254 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2255 ctx->cred, &ctx->mdsthreshold);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002256 if (IS_ERR(state))
2257 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002258 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002259 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260}
2261
Trond Myklebust1185a552009-12-03 15:54:02 -05002262static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002263{
2264 if (ctx->state == NULL)
2265 return;
2266 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002267 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002268 else
Al Viro643168c2011-06-22 18:20:23 -04002269 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002270}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271
2272static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2273{
Benny Halevy43652ad2009-04-01 09:21:54 -04002274 struct nfs4_server_caps_arg args = {
2275 .fhandle = fhandle,
2276 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 struct nfs4_server_caps_res res = {};
2278 struct rpc_message msg = {
2279 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002280 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 .rpc_resp = &res,
2282 };
2283 int status;
2284
Bryan Schumaker7c513052011-03-24 17:12:24 +00002285 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 if (status == 0) {
2287 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002288 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2289 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2290 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2291 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2292 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2294 server->caps |= NFS_CAP_ACLS;
2295 if (res.has_links != 0)
2296 server->caps |= NFS_CAP_HARDLINKS;
2297 if (res.has_symlinks != 0)
2298 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002299 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2300 server->caps |= NFS_CAP_FILEID;
2301 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2302 server->caps |= NFS_CAP_MODE;
2303 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2304 server->caps |= NFS_CAP_NLINK;
2305 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2306 server->caps |= NFS_CAP_OWNER;
2307 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2308 server->caps |= NFS_CAP_OWNER_GROUP;
2309 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2310 server->caps |= NFS_CAP_ATIME;
2311 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2312 server->caps |= NFS_CAP_CTIME;
2313 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2314 server->caps |= NFS_CAP_MTIME;
2315
Trond Myklebusta65318b2009-03-11 14:10:28 -04002316 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2317 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2318 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002320 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002322
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 return status;
2324}
2325
Trond Myklebust55a97592006-06-09 09:34:19 -04002326int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327{
2328 struct nfs4_exception exception = { };
2329 int err;
2330 do {
2331 err = nfs4_handle_exception(server,
2332 _nfs4_server_capabilities(server, fhandle),
2333 &exception);
2334 } while (exception.retry);
2335 return err;
2336}
2337
2338static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2339 struct nfs_fsinfo *info)
2340{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 struct nfs4_lookup_root_arg args = {
2342 .bitmask = nfs4_fattr_bitmap,
2343 };
2344 struct nfs4_lookup_res res = {
2345 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002346 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 .fh = fhandle,
2348 };
2349 struct rpc_message msg = {
2350 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2351 .rpc_argp = &args,
2352 .rpc_resp = &res,
2353 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002354
Trond Myklebust0e574af2005-10-27 22:12:38 -04002355 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002356 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357}
2358
2359static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2360 struct nfs_fsinfo *info)
2361{
2362 struct nfs4_exception exception = { };
2363 int err;
2364 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002365 err = _nfs4_lookup_root(server, fhandle, info);
2366 switch (err) {
2367 case 0:
2368 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002369 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002370 default:
2371 err = nfs4_handle_exception(server, err, &exception);
2372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002374out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 return err;
2376}
2377
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002378static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2379 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2380{
2381 struct rpc_auth *auth;
2382 int ret;
2383
2384 auth = rpcauth_create(flavor, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002385 if (IS_ERR(auth)) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002386 ret = -EIO;
2387 goto out;
2388 }
2389 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002390out:
2391 return ret;
2392}
2393
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002394static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002395 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002397 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002398 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002400 len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04002401 if (len < 0)
2402 return len;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002403
2404 for (i = 0; i < len; i++) {
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002405 /* AUTH_UNIX is the default flavor if none was specified,
2406 * thus has already been tried. */
2407 if (flav_array[i] == RPC_AUTH_UNIX)
2408 continue;
2409
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002410 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002411 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002412 continue;
2413 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002414 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002415 /*
2416 * -EACCESS could mean that the user doesn't have correct permissions
2417 * to access the mount. It could also mean that we tried to mount
2418 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2419 * existing mount programs don't handle -EACCES very well so it should
2420 * be mapped to -EPERM instead.
2421 */
2422 if (status == -EACCES)
2423 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002424 return status;
2425}
2426
2427/*
2428 * get the file handle for the "/" directory on the server
2429 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002430int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2431 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002432{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002433 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002434 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002435 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2436 /*
2437 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2438 * by nfs4_map_errors() as this function exits.
2439 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002440 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 if (status == 0)
2442 status = nfs4_server_capabilities(server, fhandle);
2443 if (status == 0)
2444 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002445 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446}
2447
Bryan Schumakerbae36242012-05-10 15:07:31 -04002448static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2449 struct nfs_fsinfo *info)
2450{
2451 int error;
2452 struct nfs_fattr *fattr = info->fattr;
2453
2454 error = nfs4_server_capabilities(server, mntfh);
2455 if (error < 0) {
2456 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2457 return error;
2458 }
2459
2460 error = nfs4_proc_getattr(server, mntfh, fattr);
2461 if (error < 0) {
2462 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2463 return error;
2464 }
2465
2466 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2467 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2468 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2469
2470 return error;
2471}
2472
Manoj Naik6b97fd32006-06-09 09:34:29 -04002473/*
2474 * Get locations and (maybe) other attributes of a referral.
2475 * Note that we'll actually follow the referral later when
2476 * we detect fsid mismatch in inode revalidation
2477 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002478static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2479 const struct qstr *name, struct nfs_fattr *fattr,
2480 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002481{
2482 int status = -ENOMEM;
2483 struct page *page = NULL;
2484 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002485
2486 page = alloc_page(GFP_KERNEL);
2487 if (page == NULL)
2488 goto out;
2489 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2490 if (locations == NULL)
2491 goto out;
2492
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002493 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002494 if (status != 0)
2495 goto out;
2496 /* Make sure server returned a different fsid for the referral */
2497 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002498 dprintk("%s: server did not return a different fsid for"
2499 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002500 status = -EIO;
2501 goto out;
2502 }
Andy Adamson533eb462011-06-13 18:25:56 -04002503 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2504 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002505
Andy Adamson533eb462011-06-13 18:25:56 -04002506 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002507 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002508 memset(fhandle, 0, sizeof(struct nfs_fh));
2509out:
2510 if (page)
2511 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002512 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002513 return status;
2514}
2515
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2517{
2518 struct nfs4_getattr_arg args = {
2519 .fh = fhandle,
2520 .bitmask = server->attr_bitmask,
2521 };
2522 struct nfs4_getattr_res res = {
2523 .fattr = fattr,
2524 .server = server,
2525 };
2526 struct rpc_message msg = {
2527 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2528 .rpc_argp = &args,
2529 .rpc_resp = &res,
2530 };
2531
Trond Myklebust0e574af2005-10-27 22:12:38 -04002532 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002533 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534}
2535
2536static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2537{
2538 struct nfs4_exception exception = { };
2539 int err;
2540 do {
2541 err = nfs4_handle_exception(server,
2542 _nfs4_proc_getattr(server, fhandle, fattr),
2543 &exception);
2544 } while (exception.retry);
2545 return err;
2546}
2547
2548/*
2549 * The file is not closed if it is opened due to the a request to change
2550 * the size of the file. The open call will not be needed once the
2551 * VFS layer lookup-intents are implemented.
2552 *
2553 * Close is called when the inode is destroyed.
2554 * If we haven't opened the file for O_WRONLY, we
2555 * need to in the size_change case to obtain a stateid.
2556 *
2557 * Got race?
2558 * Because OPEN is always done by name in nfsv4, it is
2559 * possible that we opened a different file by the same
2560 * name. We can recognize this race condition, but we
2561 * can't do anything about it besides returning an error.
2562 *
2563 * This will be fixed with VFS changes (lookup-intent).
2564 */
2565static int
2566nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2567 struct iattr *sattr)
2568{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002569 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002570 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002571 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 int status;
2573
Benny Halevy8a1636c2010-07-14 15:43:57 -04002574 if (pnfs_ld_layoutret_on_setattr(inode))
2575 pnfs_return_layout(inode);
2576
Trond Myklebust0e574af2005-10-27 22:12:38 -04002577 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578
Andy Adamson26699402012-05-30 16:12:24 -04002579 /* Deal with open(O_TRUNC) */
2580 if (sattr->ia_valid & ATTR_OPEN)
2581 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2582
2583 /* Optimization: if the end result is no change, don't RPC */
2584 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2585 return 0;
2586
Trond Myklebustd5308382005-11-04 15:33:38 -05002587 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002588 if (sattr->ia_valid & ATTR_FILE) {
2589 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002590
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002591 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002592 if (ctx) {
2593 cred = ctx->cred;
2594 state = ctx->state;
2595 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002596 }
2597
2598 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002599 if (status == 0)
2600 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 return status;
2602}
2603
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002604static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2605 const struct qstr *name, struct nfs_fh *fhandle,
2606 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002607{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002608 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002609 int status;
2610 struct nfs4_lookup_arg args = {
2611 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002612 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002613 .name = name,
2614 };
2615 struct nfs4_lookup_res res = {
2616 .server = server,
2617 .fattr = fattr,
2618 .fh = fhandle,
2619 };
2620 struct rpc_message msg = {
2621 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2622 .rpc_argp = &args,
2623 .rpc_resp = &res,
2624 };
2625
2626 nfs_fattr_init(fattr);
2627
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002629 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 dprintk("NFS reply lookup: %d\n", status);
2631 return status;
2632}
2633
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002634static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002635{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002636 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002637 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002638 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2639 fattr->nlink = 2;
2640}
2641
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002642static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2643 struct qstr *name, struct nfs_fh *fhandle,
2644 struct nfs_fattr *fattr)
2645{
2646 struct nfs4_exception exception = { };
2647 struct rpc_clnt *client = *clnt;
2648 int err;
2649 do {
2650 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2651 switch (err) {
2652 case -NFS4ERR_BADNAME:
2653 err = -ENOENT;
2654 goto out;
2655 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002656 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002657 goto out;
2658 case -NFS4ERR_WRONGSEC:
2659 err = -EPERM;
2660 if (client != *clnt)
2661 goto out;
2662
2663 client = nfs4_create_sec_client(client, dir, name);
2664 if (IS_ERR(client))
2665 return PTR_ERR(client);
2666
2667 exception.retry = 1;
2668 break;
2669 default:
2670 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2671 }
2672 } while (exception.retry);
2673
2674out:
2675 if (err == 0)
2676 *clnt = client;
2677 else if (client != *clnt)
2678 rpc_shutdown_client(client);
2679
2680 return err;
2681}
2682
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002683static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002684 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002686 int status;
2687 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002688
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002689 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2690 if (client != NFS_CLIENT(dir)) {
2691 rpc_shutdown_client(client);
2692 nfs_fixup_secinfo_attributes(fattr);
2693 }
2694 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695}
2696
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002697struct rpc_clnt *
2698nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2699 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2700{
2701 int status;
2702 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2703
2704 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2705 if (status < 0) {
2706 rpc_shutdown_client(client);
2707 return ERR_PTR(status);
2708 }
2709 return client;
2710}
2711
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2713{
Trond Myklebust76b32992007-08-10 17:45:11 -04002714 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 struct nfs4_accessargs args = {
2716 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002717 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002719 struct nfs4_accessres res = {
2720 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002721 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 struct rpc_message msg = {
2723 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2724 .rpc_argp = &args,
2725 .rpc_resp = &res,
2726 .rpc_cred = entry->cred,
2727 };
2728 int mode = entry->mask;
2729 int status;
2730
2731 /*
2732 * Determine which access bits we want to ask for...
2733 */
2734 if (mode & MAY_READ)
2735 args.access |= NFS4_ACCESS_READ;
2736 if (S_ISDIR(inode->i_mode)) {
2737 if (mode & MAY_WRITE)
2738 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2739 if (mode & MAY_EXEC)
2740 args.access |= NFS4_ACCESS_LOOKUP;
2741 } else {
2742 if (mode & MAY_WRITE)
2743 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2744 if (mode & MAY_EXEC)
2745 args.access |= NFS4_ACCESS_EXECUTE;
2746 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002747
2748 res.fattr = nfs_alloc_fattr();
2749 if (res.fattr == NULL)
2750 return -ENOMEM;
2751
Bryan Schumaker7c513052011-03-24 17:12:24 +00002752 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002754 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04002755 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002757 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 return status;
2759}
2760
2761static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2762{
2763 struct nfs4_exception exception = { };
2764 int err;
2765 do {
2766 err = nfs4_handle_exception(NFS_SERVER(inode),
2767 _nfs4_proc_access(inode, entry),
2768 &exception);
2769 } while (exception.retry);
2770 return err;
2771}
2772
2773/*
2774 * TODO: For the time being, we don't try to get any attributes
2775 * along with any of the zero-copy operations READ, READDIR,
2776 * READLINK, WRITE.
2777 *
2778 * In the case of the first three, we want to put the GETATTR
2779 * after the read-type operation -- this is because it is hard
2780 * to predict the length of a GETATTR response in v4, and thus
2781 * align the READ data correctly. This means that the GETATTR
2782 * may end up partially falling into the page cache, and we should
2783 * shift it into the 'tail' of the xdr_buf before processing.
2784 * To do this efficiently, we need to know the total length
2785 * of data received, which doesn't seem to be available outside
2786 * of the RPC layer.
2787 *
2788 * In the case of WRITE, we also want to put the GETATTR after
2789 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04002790 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 *
2792 * Both of these changes to the XDR layer would in fact be quite
2793 * minor, but I decided to leave them for a subsequent patch.
2794 */
2795static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2796 unsigned int pgbase, unsigned int pglen)
2797{
2798 struct nfs4_readlink args = {
2799 .fh = NFS_FH(inode),
2800 .pgbase = pgbase,
2801 .pglen = pglen,
2802 .pages = &page,
2803 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002804 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 struct rpc_message msg = {
2806 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2807 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002808 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 };
2810
Bryan Schumaker7c513052011-03-24 17:12:24 +00002811 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 -07002812}
2813
2814static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2815 unsigned int pgbase, unsigned int pglen)
2816{
2817 struct nfs4_exception exception = { };
2818 int err;
2819 do {
2820 err = nfs4_handle_exception(NFS_SERVER(inode),
2821 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2822 &exception);
2823 } while (exception.retry);
2824 return err;
2825}
2826
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002828 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830static int
2831nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002832 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833{
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002834 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 int status = 0;
2837
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002838 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
2839 if (IS_ERR(ctx))
2840 return PTR_ERR(ctx);
2841
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002842 sattr->ia_mode &= ~current_umask();
Trond Myklebust8626e4a2012-07-16 12:01:42 -04002843 state = nfs4_do_open(dir, dentry, ctx->mode,
2844 flags, sattr, ctx->cred,
2845 &ctx->mdsthreshold);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002846 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 if (IS_ERR(state)) {
2848 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002849 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002851 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002852 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002853 ctx->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854out:
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002855 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 return status;
2857}
2858
2859static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2860{
Trond Myklebust16e42952005-10-27 22:12:44 -04002861 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002862 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07002864 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002866 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002867 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002868 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002870 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2871 .rpc_argp = &args,
2872 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 };
Trond Myklebust778d2812012-04-27 13:48:19 -04002874 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04002875
Bryan Schumaker7c513052011-03-24 17:12:24 +00002876 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04002877 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04002878 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 return status;
2880}
2881
2882static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2883{
2884 struct nfs4_exception exception = { };
2885 int err;
2886 do {
2887 err = nfs4_handle_exception(NFS_SERVER(dir),
2888 _nfs4_proc_remove(dir, name),
2889 &exception);
2890 } while (exception.retry);
2891 return err;
2892}
2893
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002894static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002896 struct nfs_server *server = NFS_SERVER(dir);
2897 struct nfs_removeargs *args = msg->rpc_argp;
2898 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002900 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05002902 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903}
2904
Bryan Schumaker34e137c2012-03-19 14:54:41 -04002905static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
2906{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04002907 nfs4_setup_sequence(NFS_SERVER(data->dir),
2908 &data->args.seq_args,
2909 &data->res.seq_res,
2910 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911}
2912
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002913static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002915 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2916
Trond Myklebust14516c32010-07-31 14:29:06 -04002917 if (!nfs4_sequence_done(task, &res->seq_res))
2918 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002919 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002920 return 0;
2921 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002922 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923}
2924
Jeff Laytond3d41522010-09-17 17:31:57 -04002925static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2926{
2927 struct nfs_server *server = NFS_SERVER(dir);
2928 struct nfs_renameargs *arg = msg->rpc_argp;
2929 struct nfs_renameres *res = msg->rpc_resp;
2930
2931 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04002932 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002933 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04002934}
2935
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04002936static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
2937{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04002938 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
2939 &data->args.seq_args,
2940 &data->res.seq_res,
2941 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04002942}
2943
2944static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2945 struct inode *new_dir)
2946{
2947 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2948
2949 if (!nfs4_sequence_done(task, &res->seq_res))
2950 return 0;
2951 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2952 return 0;
2953
2954 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04002955 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04002956 return 1;
2957}
2958
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2960 struct inode *new_dir, struct qstr *new_name)
2961{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002962 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002963 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 .old_dir = NFS_FH(old_dir),
2965 .new_dir = NFS_FH(new_dir),
2966 .old_name = old_name,
2967 .new_name = new_name,
2968 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002969 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002970 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002971 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 struct rpc_message msg = {
2973 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2974 .rpc_argp = &arg,
2975 .rpc_resp = &res,
2976 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002977 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978
Bryan Schumaker7c513052011-03-24 17:12:24 +00002979 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 if (!status) {
2981 update_changeattr(old_dir, &res.old_cinfo);
2982 update_changeattr(new_dir, &res.new_cinfo);
2983 }
2984 return status;
2985}
2986
2987static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2988 struct inode *new_dir, struct qstr *new_name)
2989{
2990 struct nfs4_exception exception = { };
2991 int err;
2992 do {
2993 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2994 _nfs4_proc_rename(old_dir, old_name,
2995 new_dir, new_name),
2996 &exception);
2997 } while (exception.retry);
2998 return err;
2999}
3000
3001static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3002{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003003 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 struct nfs4_link_arg arg = {
3005 .fh = NFS_FH(inode),
3006 .dir_fh = NFS_FH(dir),
3007 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003008 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003010 struct nfs4_link_res res = {
3011 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003012 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 struct rpc_message msg = {
3014 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3015 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003016 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003018 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019
Trond Myklebust136f2622010-04-16 16:22:49 -04003020 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003021 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003022 goto out;
3023
Bryan Schumaker7c513052011-03-24 17:12:24 +00003024 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003025 if (!status) {
3026 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04003027 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003028 }
Trond Myklebust136f2622010-04-16 16:22:49 -04003029out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003030 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 return status;
3032}
3033
3034static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3035{
3036 struct nfs4_exception exception = { };
3037 int err;
3038 do {
3039 err = nfs4_handle_exception(NFS_SERVER(inode),
3040 _nfs4_proc_link(inode, dir, name),
3041 &exception);
3042 } while (exception.retry);
3043 return err;
3044}
3045
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003046struct nfs4_createdata {
3047 struct rpc_message msg;
3048 struct nfs4_create_arg arg;
3049 struct nfs4_create_res res;
3050 struct nfs_fh fh;
3051 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003052};
3053
3054static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3055 struct qstr *name, struct iattr *sattr, u32 ftype)
3056{
3057 struct nfs4_createdata *data;
3058
3059 data = kzalloc(sizeof(*data), GFP_KERNEL);
3060 if (data != NULL) {
3061 struct nfs_server *server = NFS_SERVER(dir);
3062
3063 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3064 data->msg.rpc_argp = &data->arg;
3065 data->msg.rpc_resp = &data->res;
3066 data->arg.dir_fh = NFS_FH(dir);
3067 data->arg.server = server;
3068 data->arg.name = name;
3069 data->arg.attrs = sattr;
3070 data->arg.ftype = ftype;
3071 data->arg.bitmask = server->attr_bitmask;
3072 data->res.server = server;
3073 data->res.fh = &data->fh;
3074 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003075 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003076 }
3077 return data;
3078}
3079
3080static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3081{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003082 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003083 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003084 if (status == 0) {
3085 update_changeattr(dir, &data->res.dir_cinfo);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003086 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3087 }
3088 return status;
3089}
3090
3091static void nfs4_free_createdata(struct nfs4_createdata *data)
3092{
3093 kfree(data);
3094}
3095
Chuck Lever4f390c12006-08-22 20:06:22 -04003096static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003097 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003099 struct nfs4_createdata *data;
3100 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101
Chuck Lever94a6d752006-08-22 20:06:23 -04003102 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003103 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003104
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003105 status = -ENOMEM;
3106 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3107 if (data == NULL)
3108 goto out;
3109
3110 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3111 data->arg.u.symlink.pages = &page;
3112 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003114 status = nfs4_do_create(dir, dentry, data);
3115
3116 nfs4_free_createdata(data);
3117out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 return status;
3119}
3120
Chuck Lever4f390c12006-08-22 20:06:22 -04003121static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003122 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123{
3124 struct nfs4_exception exception = { };
3125 int err;
3126 do {
3127 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003128 _nfs4_proc_symlink(dir, dentry, page,
3129 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 &exception);
3131 } while (exception.retry);
3132 return err;
3133}
3134
3135static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3136 struct iattr *sattr)
3137{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003138 struct nfs4_createdata *data;
3139 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003141 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3142 if (data == NULL)
3143 goto out;
3144
3145 status = nfs4_do_create(dir, dentry, data);
3146
3147 nfs4_free_createdata(data);
3148out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 return status;
3150}
3151
3152static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3153 struct iattr *sattr)
3154{
3155 struct nfs4_exception exception = { };
3156 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003157
3158 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 do {
3160 err = nfs4_handle_exception(NFS_SERVER(dir),
3161 _nfs4_proc_mkdir(dir, dentry, sattr),
3162 &exception);
3163 } while (exception.retry);
3164 return err;
3165}
3166
3167static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003168 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169{
3170 struct inode *dir = dentry->d_inode;
3171 struct nfs4_readdir_arg args = {
3172 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003173 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 .pgbase = 0,
3175 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003176 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003177 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 };
3179 struct nfs4_readdir_res res;
3180 struct rpc_message msg = {
3181 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3182 .rpc_argp = &args,
3183 .rpc_resp = &res,
3184 .rpc_cred = cred,
3185 };
3186 int status;
3187
Harvey Harrison3110ff82008-05-02 13:42:44 -07003188 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003189 dentry->d_parent->d_name.name,
3190 dentry->d_name.name,
3191 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003192 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003194 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 -05003195 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003196 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003197 status += args.pgbase;
3198 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003199
3200 nfs_invalidate_atime(dir);
3201
Harvey Harrison3110ff82008-05-02 13:42:44 -07003202 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 return status;
3204}
3205
3206static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003207 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208{
3209 struct nfs4_exception exception = { };
3210 int err;
3211 do {
3212 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3213 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003214 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 &exception);
3216 } while (exception.retry);
3217 return err;
3218}
3219
3220static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3221 struct iattr *sattr, dev_t rdev)
3222{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003223 struct nfs4_createdata *data;
3224 int mode = sattr->ia_mode;
3225 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003227 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3228 if (data == NULL)
3229 goto out;
3230
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003232 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003234 data->arg.ftype = NF4BLK;
3235 data->arg.u.device.specdata1 = MAJOR(rdev);
3236 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 }
3238 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003239 data->arg.ftype = NF4CHR;
3240 data->arg.u.device.specdata1 = MAJOR(rdev);
3241 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003242 } else if (!S_ISSOCK(mode)) {
3243 status = -EINVAL;
3244 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003247 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003248out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003249 nfs4_free_createdata(data);
3250out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 return status;
3252}
3253
3254static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3255 struct iattr *sattr, dev_t rdev)
3256{
3257 struct nfs4_exception exception = { };
3258 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003259
3260 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 do {
3262 err = nfs4_handle_exception(NFS_SERVER(dir),
3263 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3264 &exception);
3265 } while (exception.retry);
3266 return err;
3267}
3268
3269static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3270 struct nfs_fsstat *fsstat)
3271{
3272 struct nfs4_statfs_arg args = {
3273 .fh = fhandle,
3274 .bitmask = server->attr_bitmask,
3275 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003276 struct nfs4_statfs_res res = {
3277 .fsstat = fsstat,
3278 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 struct rpc_message msg = {
3280 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3281 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003282 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 };
3284
Trond Myklebust0e574af2005-10-27 22:12:38 -04003285 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003286 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287}
3288
3289static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3290{
3291 struct nfs4_exception exception = { };
3292 int err;
3293 do {
3294 err = nfs4_handle_exception(server,
3295 _nfs4_proc_statfs(server, fhandle, fsstat),
3296 &exception);
3297 } while (exception.retry);
3298 return err;
3299}
3300
3301static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3302 struct nfs_fsinfo *fsinfo)
3303{
3304 struct nfs4_fsinfo_arg args = {
3305 .fh = fhandle,
3306 .bitmask = server->attr_bitmask,
3307 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003308 struct nfs4_fsinfo_res res = {
3309 .fsinfo = fsinfo,
3310 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 struct rpc_message msg = {
3312 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3313 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003314 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 };
3316
Bryan Schumaker7c513052011-03-24 17:12:24 +00003317 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318}
3319
3320static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3321{
3322 struct nfs4_exception exception = { };
3323 int err;
3324
3325 do {
3326 err = nfs4_handle_exception(server,
3327 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3328 &exception);
3329 } while (exception.retry);
3330 return err;
3331}
3332
3333static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3334{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003335 int error;
3336
Trond Myklebust0e574af2005-10-27 22:12:38 -04003337 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003338 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003339 if (error == 0) {
3340 /* block layout checks this! */
3341 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003342 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003343 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003344
3345 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346}
3347
3348static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3349 struct nfs_pathconf *pathconf)
3350{
3351 struct nfs4_pathconf_arg args = {
3352 .fh = fhandle,
3353 .bitmask = server->attr_bitmask,
3354 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003355 struct nfs4_pathconf_res res = {
3356 .pathconf = pathconf,
3357 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 struct rpc_message msg = {
3359 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3360 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003361 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 };
3363
3364 /* None of the pathconf attributes are mandatory to implement */
3365 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3366 memset(pathconf, 0, sizeof(*pathconf));
3367 return 0;
3368 }
3369
Trond Myklebust0e574af2005-10-27 22:12:38 -04003370 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003371 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372}
3373
3374static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3375 struct nfs_pathconf *pathconf)
3376{
3377 struct nfs4_exception exception = { };
3378 int err;
3379
3380 do {
3381 err = nfs4_handle_exception(server,
3382 _nfs4_proc_pathconf(server, fhandle, pathconf),
3383 &exception);
3384 } while (exception.retry);
3385 return err;
3386}
3387
Benny Halevyd20581a2011-05-22 19:52:03 +03003388void __nfs4_read_done_cb(struct nfs_read_data *data)
3389{
Fred Isamancd841602012-04-20 14:47:44 -04003390 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003391}
3392
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003393static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394{
Fred Isamancd841602012-04-20 14:47:44 -04003395 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003397 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003398 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003399 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003401
Benny Halevyd20581a2011-05-22 19:52:03 +03003402 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003404 renew_lease(server, data->timestamp);
3405 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406}
3407
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003408static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3409{
3410
3411 dprintk("--> %s\n", __func__);
3412
3413 if (!nfs4_sequence_done(task, &data->res.seq_res))
3414 return -EAGAIN;
3415
Benny Halevyd20581a2011-05-22 19:52:03 +03003416 return data->read_done_cb ? data->read_done_cb(task, data) :
3417 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003418}
3419
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003420static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003423 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003424 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003425 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426}
3427
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003428static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3429{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003430 nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3431 &data->args.seq_args,
3432 &data->res.seq_res,
3433 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434}
3435
Fred Isamanb029bc92011-03-03 15:13:42 +00003436static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437{
Fred Isamancd841602012-04-20 14:47:44 -04003438 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003440 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003441 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003442 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003444 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003446 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003447 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003448 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449}
3450
Fred Isamanb029bc92011-03-03 15:13:42 +00003451static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3452{
3453 if (!nfs4_sequence_done(task, &data->res.seq_res))
3454 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003455 return data->write_done_cb ? data->write_done_cb(task, data) :
3456 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003457}
3458
Trond Myklebust5a37f852012-04-28 14:55:16 -04003459static
3460bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003461{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003462 const struct nfs_pgio_header *hdr = data->header;
3463
3464 /* Don't request attributes for pNFS or O_DIRECT writes */
3465 if (data->ds_clp != NULL || hdr->dreq != NULL)
3466 return false;
3467 /* Otherwise, request attributes if and only if we don't hold
3468 * a delegation
3469 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04003470 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003471}
Fred Isamana69aef12011-03-03 15:13:47 +00003472
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003473static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474{
Fred Isamancd841602012-04-20 14:47:44 -04003475 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003476
Trond Myklebust5a37f852012-04-28 14:55:16 -04003477 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003478 data->args.bitmask = NULL;
3479 data->res.fattr = NULL;
3480 } else
3481 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003482
Fred Isamanb029bc92011-03-03 15:13:42 +00003483 if (!data->write_done_cb)
3484 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003485 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 data->timestamp = jiffies;
3487
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003488 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003489 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490}
3491
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003492static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3493{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003494 nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3495 &data->args.seq_args,
3496 &data->res.seq_res,
3497 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498}
3499
Fred Isaman0b7c0152012-04-20 14:47:39 -04003500static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3501{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003502 nfs4_setup_sequence(NFS_SERVER(data->inode),
3503 &data->args.seq_args,
3504 &data->res.seq_res,
3505 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506}
3507
Fred Isaman0b7c0152012-04-20 14:47:39 -04003508static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003511
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003512 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003513 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003514 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003516 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517}
3518
Fred Isaman0b7c0152012-04-20 14:47:39 -04003519static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003520{
3521 if (!nfs4_sequence_done(task, &data->res.seq_res))
3522 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003523 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003524}
3525
Fred Isaman0b7c0152012-04-20 14:47:39 -04003526static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003528 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003529
Fred Isaman0b7c0152012-04-20 14:47:39 -04003530 if (data->commit_done_cb == NULL)
3531 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003532 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003533 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003534 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535}
3536
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003537struct nfs4_renewdata {
3538 struct nfs_client *client;
3539 unsigned long timestamp;
3540};
3541
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542/*
3543 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3544 * standalone procedure for queueing an asynchronous RENEW.
3545 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003546static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003547{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003548 struct nfs4_renewdata *data = calldata;
3549 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003550
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003551 if (atomic_read(&clp->cl_count) > 1)
3552 nfs4_schedule_state_renewal(clp);
3553 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003554 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003555}
3556
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003557static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003559 struct nfs4_renewdata *data = calldata;
3560 struct nfs_client *clp = data->client;
3561 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562
3563 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003564 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003565 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3566 return;
3567 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003568 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003569 return;
3570 }
3571 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003573 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574}
3575
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003576static const struct rpc_call_ops nfs4_renew_ops = {
3577 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003578 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003579};
3580
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003581static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582{
3583 struct rpc_message msg = {
3584 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3585 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003586 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003588 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003590 if (renew_flags == 0)
3591 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003592 if (!atomic_inc_not_zero(&clp->cl_count))
3593 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003594 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003595 if (data == NULL)
3596 return -ENOMEM;
3597 data->client = clp;
3598 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003600 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601}
3602
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003603static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604{
3605 struct rpc_message msg = {
3606 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3607 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003608 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 };
3610 unsigned long now = jiffies;
3611 int status;
3612
3613 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3614 if (status < 0)
3615 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003616 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 return 0;
3618}
3619
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003620static inline int nfs4_server_supports_acls(struct nfs_server *server)
3621{
3622 return (server->caps & NFS_CAP_ACLS)
3623 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3624 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3625}
3626
Trond Myklebust21f498c2012-08-24 10:59:25 -04003627/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3628 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003629 * the stack.
3630 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04003631#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003632
Neil Hormane9e3d722011-03-04 19:26:03 -05003633static int buf_to_pages_noslab(const void *buf, size_t buflen,
3634 struct page **pages, unsigned int *pgbase)
3635{
3636 struct page *newpage, **spages;
3637 int rc = 0;
3638 size_t len;
3639 spages = pages;
3640
3641 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04003642 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003643 newpage = alloc_page(GFP_KERNEL);
3644
3645 if (newpage == NULL)
3646 goto unwind;
3647 memcpy(page_address(newpage), buf, len);
3648 buf += len;
3649 buflen -= len;
3650 *pages++ = newpage;
3651 rc++;
3652 } while (buflen != 0);
3653
3654 return rc;
3655
3656unwind:
3657 for(; rc > 0; rc--)
3658 __free_page(spages[rc-1]);
3659 return -ENOMEM;
3660}
3661
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003662struct nfs4_cached_acl {
3663 int cached;
3664 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003665 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003666};
3667
3668static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003669{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003670 struct nfs_inode *nfsi = NFS_I(inode);
3671
3672 spin_lock(&inode->i_lock);
3673 kfree(nfsi->nfs4_acl);
3674 nfsi->nfs4_acl = acl;
3675 spin_unlock(&inode->i_lock);
3676}
3677
3678static void nfs4_zap_acl_attr(struct inode *inode)
3679{
3680 nfs4_set_cached_acl(inode, NULL);
3681}
3682
3683static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3684{
3685 struct nfs_inode *nfsi = NFS_I(inode);
3686 struct nfs4_cached_acl *acl;
3687 int ret = -ENOENT;
3688
3689 spin_lock(&inode->i_lock);
3690 acl = nfsi->nfs4_acl;
3691 if (acl == NULL)
3692 goto out;
3693 if (buf == NULL) /* user is just asking for length */
3694 goto out_len;
3695 if (acl->cached == 0)
3696 goto out;
3697 ret = -ERANGE; /* see getxattr(2) man page */
3698 if (acl->len > buflen)
3699 goto out;
3700 memcpy(buf, acl->data, acl->len);
3701out_len:
3702 ret = acl->len;
3703out:
3704 spin_unlock(&inode->i_lock);
3705 return ret;
3706}
3707
Sachin Prabhu5794d212012-04-17 14:36:40 +01003708static 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 +00003709{
3710 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003711 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003712
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003713 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003714 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003715 if (acl == NULL)
3716 goto out;
3717 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003718 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003719 } else {
3720 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3721 if (acl == NULL)
3722 goto out;
3723 acl->cached = 0;
3724 }
3725 acl->len = acl_len;
3726out:
3727 nfs4_set_cached_acl(inode, acl);
3728}
3729
Andy Adamsonbf118a32011-12-07 11:55:27 -05003730/*
3731 * The getxattr API returns the required buffer length when called with a
3732 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3733 * the required buf. On a NULL buf, we send a page of data to the server
3734 * guessing that the ACL request can be serviced by a page. If so, we cache
3735 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3736 * the cache. If not so, we throw away the page, and cache the required
3737 * length. The next getxattr call will then produce another round trip to
3738 * the server, this time with the input buf of the required size.
3739 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003740static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003741{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003742 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003743 struct nfs_getaclargs args = {
3744 .fh = NFS_FH(inode),
3745 .acl_pages = pages,
3746 .acl_len = buflen,
3747 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003748 struct nfs_getaclres res = {
3749 .acl_len = buflen,
3750 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003751 struct rpc_message msg = {
3752 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3753 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003754 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003755 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003756 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3757 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003758
Andy Adamsonbf118a32011-12-07 11:55:27 -05003759 /* As long as we're doing a round trip to the server anyway,
3760 * let's be prepared for a page of acl data. */
3761 if (npages == 0)
3762 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003763 if (npages > ARRAY_SIZE(pages))
3764 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003765
Andy Adamsonbf118a32011-12-07 11:55:27 -05003766 for (i = 0; i < npages; i++) {
3767 pages[i] = alloc_page(GFP_KERNEL);
3768 if (!pages[i])
3769 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003770 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01003771
3772 /* for decoding across pages */
3773 res.acl_scratch = alloc_page(GFP_KERNEL);
3774 if (!res.acl_scratch)
3775 goto out_free;
3776
Andy Adamsonbf118a32011-12-07 11:55:27 -05003777 args.acl_len = npages * PAGE_SIZE;
3778 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003779
Peng Taode040be2012-01-10 22:42:47 +08003780 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003781 __func__, buf, buflen, npages, args.acl_len);
3782 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3783 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003784 if (ret)
3785 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003786
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003787 /* Handle the case where the passed-in buffer is too short */
3788 if (res.acl_flags & NFS4_ACL_TRUNC) {
3789 /* Did the user only issue a request for the acl length? */
3790 if (buf == NULL)
3791 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003792 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003793 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003794 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003795 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01003796 if (buf) {
3797 if (res.acl_len > buflen) {
3798 ret = -ERANGE;
3799 goto out_free;
3800 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003801 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01003802 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003803out_ok:
3804 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003805out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003806 for (i = 0; i < npages; i++)
3807 if (pages[i])
3808 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05003809 if (res.acl_scratch)
3810 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003811 return ret;
3812}
3813
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003814static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3815{
3816 struct nfs4_exception exception = { };
3817 ssize_t ret;
3818 do {
3819 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3820 if (ret >= 0)
3821 break;
3822 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3823 } while (exception.retry);
3824 return ret;
3825}
3826
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003827static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3828{
3829 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003830 int ret;
3831
3832 if (!nfs4_server_supports_acls(server))
3833 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003834 ret = nfs_revalidate_inode(server, inode);
3835 if (ret < 0)
3836 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003837 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3838 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003839 ret = nfs4_read_cached_acl(inode, buf, buflen);
3840 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003841 /* -ENOENT is returned if there is no ACL or if there is an ACL
3842 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003843 return ret;
3844 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003845}
3846
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003847static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003848{
3849 struct nfs_server *server = NFS_SERVER(inode);
3850 struct page *pages[NFS4ACL_MAXPAGES];
3851 struct nfs_setaclargs arg = {
3852 .fh = NFS_FH(inode),
3853 .acl_pages = pages,
3854 .acl_len = buflen,
3855 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003856 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003857 struct rpc_message msg = {
3858 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3859 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003860 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003861 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003862 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05003863 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003864
3865 if (!nfs4_server_supports_acls(server))
3866 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003867 if (npages > ARRAY_SIZE(pages))
3868 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05003869 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3870 if (i < 0)
3871 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04003872 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003873 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003874
3875 /*
3876 * Free each page after tx, so the only ref left is
3877 * held by the network stack
3878 */
3879 for (; i > 0; i--)
3880 put_page(pages[i-1]);
3881
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003882 /*
3883 * Acl update can result in inode attribute update.
3884 * so mark the attribute cache invalid.
3885 */
3886 spin_lock(&inode->i_lock);
3887 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3888 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003889 nfs_access_zap_cache(inode);
3890 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003891 return ret;
3892}
3893
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003894static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3895{
3896 struct nfs4_exception exception = { };
3897 int err;
3898 do {
3899 err = nfs4_handle_exception(NFS_SERVER(inode),
3900 __nfs4_proc_set_acl(inode, buf, buflen),
3901 &exception);
3902 } while (exception.retry);
3903 return err;
3904}
3905
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003907nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003909 struct nfs_client *clp = server->nfs_client;
3910
3911 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912 return 0;
3913 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05003914 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003915 case -NFS4ERR_ADMIN_REVOKED:
3916 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04003917 if (state == NULL)
3918 break;
3919 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003920 case -NFS4ERR_OPENMODE:
3921 if (state == NULL)
3922 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003923 nfs4_schedule_stateid_recovery(server, state);
3924 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003925 case -NFS4ERR_EXPIRED:
3926 if (state != NULL)
3927 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003929 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003930 nfs4_schedule_lease_recovery(clp);
3931 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003932#if defined(CONFIG_NFS_V4_1)
3933 case -NFS4ERR_BADSESSION:
3934 case -NFS4ERR_BADSLOT:
3935 case -NFS4ERR_BAD_HIGH_SLOT:
3936 case -NFS4ERR_DEADSESSION:
3937 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3938 case -NFS4ERR_SEQ_FALSE_RETRY:
3939 case -NFS4ERR_SEQ_MISORDERED:
3940 dprintk("%s ERROR %d, Reset session\n", __func__,
3941 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04003942 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04003943 task->tk_status = 0;
3944 return -EAGAIN;
3945#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003947 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003948 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003949 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3951 task->tk_status = 0;
3952 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04003953 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954 case -NFS4ERR_OLD_STATEID:
3955 task->tk_status = 0;
3956 return -EAGAIN;
3957 }
3958 task->tk_status = nfs4_map_errors(task->tk_status);
3959 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003960wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003961 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003962 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3963 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3964 task->tk_status = 0;
3965 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966}
3967
Chuck Leverf0920752012-05-21 22:45:41 -04003968static void nfs4_init_boot_verifier(const struct nfs_client *clp,
3969 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05003970{
3971 __be32 verf[2];
3972
Chuck Lever2c820d92012-05-21 22:45:33 -04003973 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
3974 /* An impossible timestamp guarantees this value
3975 * will never match a generated boot time. */
3976 verf[0] = 0;
3977 verf[1] = (__be32)(NSEC_PER_SEC + 1);
3978 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04003979 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
3980 verf[0] = (__be32)nn->boot_time.tv_sec;
3981 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04003982 }
Chuck Levercd937102012-03-02 17:14:31 -05003983 memcpy(bootverf->data, verf, sizeof(bootverf->data));
3984}
3985
Chuck Levere984a552012-09-14 17:24:21 -04003986static unsigned int
3987nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
3988 char *buf, size_t len)
3989{
3990 unsigned int result;
3991
3992 rcu_read_lock();
3993 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
3994 clp->cl_ipaddr,
3995 rpc_peeraddr2str(clp->cl_rpcclient,
3996 RPC_DISPLAY_ADDR),
3997 rpc_peeraddr2str(clp->cl_rpcclient,
3998 RPC_DISPLAY_PROTO));
3999 rcu_read_unlock();
4000 return result;
4001}
4002
4003static unsigned int
4004nfs4_init_uniform_client_string(const struct nfs_client *clp,
4005 char *buf, size_t len)
4006{
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004007 char *nodename = clp->cl_rpcclient->cl_nodename;
4008
4009 if (nfs4_client_id_uniquifier[0] != '\0')
4010 nodename = nfs4_client_id_uniquifier;
Chuck Levere984a552012-09-14 17:24:21 -04004011 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4012 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004013 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004014}
4015
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004016/**
4017 * nfs4_proc_setclientid - Negotiate client ID
4018 * @clp: state data structure
4019 * @program: RPC program for NFSv4 callback service
4020 * @port: IP port number for NFS4 callback service
4021 * @cred: RPC credential to use for this call
4022 * @res: where to place the result
4023 *
4024 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4025 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004026int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4027 unsigned short port, struct rpc_cred *cred,
4028 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029{
4030 nfs4_verifier sc_verifier;
4031 struct nfs4_setclientid setclientid = {
4032 .sc_verifier = &sc_verifier,
4033 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004034 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035 };
4036 struct rpc_message msg = {
4037 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4038 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004039 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004040 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004042 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043
Chuck Leverde734832012-07-11 16:30:50 -04004044 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004045 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004046 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4047 setclientid.sc_name_len =
4048 nfs4_init_uniform_client_string(clp,
4049 setclientid.sc_name,
4050 sizeof(setclientid.sc_name));
4051 else
4052 setclientid.sc_name_len =
4053 nfs4_init_nonuniform_client_string(clp,
4054 setclientid.sc_name,
4055 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004056 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004057 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004058 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004059 sizeof(setclientid.sc_netid),
4060 rpc_peeraddr2str(clp->cl_rpcclient,
4061 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004062 rcu_read_unlock();
4063 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004064 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 clp->cl_ipaddr, port >> 8, port & 255);
4066
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004067 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4068 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4069 setclientid.sc_name_len, setclientid.sc_name);
4070 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4071 dprintk("NFS reply setclientid: %d\n", status);
4072 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073}
4074
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004075/**
4076 * nfs4_proc_setclientid_confirm - Confirm client ID
4077 * @clp: state data structure
4078 * @res: result of a previous SETCLIENTID
4079 * @cred: RPC credential to use for this call
4080 *
4081 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4082 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004083int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004084 struct nfs4_setclientid_res *arg,
4085 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086{
4087 struct nfs_fsinfo fsinfo;
4088 struct rpc_message msg = {
4089 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004090 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004092 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093 };
4094 unsigned long now;
4095 int status;
4096
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004097 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4098 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4099 clp->cl_clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004101 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102 if (status == 0) {
4103 spin_lock(&clp->cl_lock);
4104 clp->cl_lease_time = fsinfo.lease_time * HZ;
4105 clp->cl_last_renewal = now;
4106 spin_unlock(&clp->cl_lock);
4107 }
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004108 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109 return status;
4110}
4111
Trond Myklebustfe650402006-01-03 09:55:18 +01004112struct nfs4_delegreturndata {
4113 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004114 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004115 struct nfs_fh fh;
4116 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004117 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004118 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004119 int rpc_status;
4120};
4121
Trond Myklebustfe650402006-01-03 09:55:18 +01004122static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4123{
4124 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004125
Trond Myklebust14516c32010-07-31 14:29:06 -04004126 if (!nfs4_sequence_done(task, &data->res.seq_res))
4127 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004128
Ricardo Labiaga79708862009-12-07 09:23:21 -05004129 switch (task->tk_status) {
4130 case -NFS4ERR_STALE_STATEID:
4131 case -NFS4ERR_EXPIRED:
4132 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004133 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004134 break;
4135 default:
4136 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4137 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004138 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004139 return;
4140 }
4141 }
4142 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004143}
4144
4145static void nfs4_delegreturn_release(void *calldata)
4146{
Trond Myklebustfe650402006-01-03 09:55:18 +01004147 kfree(calldata);
4148}
4149
Andy Adamson938e1012009-04-01 09:22:28 -04004150#if defined(CONFIG_NFS_V4_1)
4151static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4152{
4153 struct nfs4_delegreturndata *d_data;
4154
4155 d_data = (struct nfs4_delegreturndata *)data;
4156
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004157 nfs4_setup_sequence(d_data->res.server,
4158 &d_data->args.seq_args,
4159 &d_data->res.seq_res,
4160 task);
Andy Adamson938e1012009-04-01 09:22:28 -04004161}
4162#endif /* CONFIG_NFS_V4_1 */
4163
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004164static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004165#if defined(CONFIG_NFS_V4_1)
4166 .rpc_call_prepare = nfs4_delegreturn_prepare,
4167#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004168 .rpc_call_done = nfs4_delegreturn_done,
4169 .rpc_release = nfs4_delegreturn_release,
4170};
4171
Trond Myklebuste6f81072008-01-24 18:14:34 -05004172static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004173{
4174 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004175 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004176 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004177 struct rpc_message msg = {
4178 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4179 .rpc_cred = cred,
4180 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004181 struct rpc_task_setup task_setup_data = {
4182 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004183 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004184 .callback_ops = &nfs4_delegreturn_ops,
4185 .flags = RPC_TASK_ASYNC,
4186 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004187 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004188
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004189 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004190 if (data == NULL)
4191 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004192 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004193 data->args.fhandle = &data->fh;
4194 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004195 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004196 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004197 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004198 data->res.fattr = &data->fattr;
4199 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004200 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004201 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004202 data->rpc_status = 0;
4203
Trond Myklebustc970aa82007-07-14 15:39:59 -04004204 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004205 msg.rpc_argp = &data->args;
4206 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004207 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004208 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004209 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004210 if (!issync)
4211 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004212 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004213 if (status != 0)
4214 goto out;
4215 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004216 if (status == 0)
4217 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4218 else
4219 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004220out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004221 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004222 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223}
4224
Trond Myklebuste6f81072008-01-24 18:14:34 -05004225int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226{
4227 struct nfs_server *server = NFS_SERVER(inode);
4228 struct nfs4_exception exception = { };
4229 int err;
4230 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004231 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 switch (err) {
4233 case -NFS4ERR_STALE_STATEID:
4234 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235 case 0:
4236 return 0;
4237 }
4238 err = nfs4_handle_exception(server, err, &exception);
4239 } while (exception.retry);
4240 return err;
4241}
4242
4243#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4244#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4245
4246/*
4247 * sleep, with exponential backoff, and retry the LOCK operation.
4248 */
4249static unsigned long
4250nfs4_set_lock_task_retry(unsigned long timeout)
4251{
Jeff Laytond3103102011-12-01 22:44:39 +01004252 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253 timeout <<= 1;
4254 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4255 return NFS4_LOCK_MAXTIMEOUT;
4256 return timeout;
4257}
4258
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4260{
4261 struct inode *inode = state->inode;
4262 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004263 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004264 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004266 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004268 struct nfs_lockt_res res = {
4269 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270 };
4271 struct rpc_message msg = {
4272 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4273 .rpc_argp = &arg,
4274 .rpc_resp = &res,
4275 .rpc_cred = state->owner->so_cred,
4276 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277 struct nfs4_lock_state *lsp;
4278 int status;
4279
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004280 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004281 status = nfs4_set_lock_state(state, request);
4282 if (status != 0)
4283 goto out;
4284 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004285 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004286 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004287 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004288 switch (status) {
4289 case 0:
4290 request->fl_type = F_UNLCK;
4291 break;
4292 case -NFS4ERR_DENIED:
4293 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004295 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004296out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 return status;
4298}
4299
4300static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4301{
4302 struct nfs4_exception exception = { };
4303 int err;
4304
4305 do {
4306 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4307 _nfs4_proc_getlk(state, cmd, request),
4308 &exception);
4309 } while (exception.retry);
4310 return err;
4311}
4312
4313static int do_vfs_lock(struct file *file, struct file_lock *fl)
4314{
4315 int res = 0;
4316 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4317 case FL_POSIX:
4318 res = posix_lock_file_wait(file, fl);
4319 break;
4320 case FL_FLOCK:
4321 res = flock_lock_file_wait(file, fl);
4322 break;
4323 default:
4324 BUG();
4325 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326 return res;
4327}
4328
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004329struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004330 struct nfs_locku_args arg;
4331 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004332 struct nfs4_lock_state *lsp;
4333 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004334 struct file_lock fl;
4335 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004336 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004337};
4338
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004339static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4340 struct nfs_open_context *ctx,
4341 struct nfs4_lock_state *lsp,
4342 struct nfs_seqid *seqid)
4343{
4344 struct nfs4_unlockdata *p;
4345 struct inode *inode = lsp->ls_state->inode;
4346
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004347 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004348 if (p == NULL)
4349 return NULL;
4350 p->arg.fh = NFS_FH(inode);
4351 p->arg.fl = &p->fl;
4352 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004353 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004354 p->arg.stateid = &lsp->ls_stateid;
4355 p->lsp = lsp;
4356 atomic_inc(&lsp->ls_count);
4357 /* Ensure we don't close file until we're done freeing locks! */
4358 p->ctx = get_nfs_open_context(ctx);
4359 memcpy(&p->fl, fl, sizeof(p->fl));
4360 p->server = NFS_SERVER(inode);
4361 return p;
4362}
4363
Trond Myklebust06f814a2006-01-03 09:55:07 +01004364static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004365{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004366 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004367 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004368 nfs4_put_lock_state(calldata->lsp);
4369 put_nfs_open_context(calldata->ctx);
4370 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004371}
4372
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004373static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004374{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004375 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004376
Trond Myklebust14516c32010-07-31 14:29:06 -04004377 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4378 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004379 switch (task->tk_status) {
4380 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004381 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4382 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004383 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004384 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004385 case -NFS4ERR_BAD_STATEID:
4386 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004387 case -NFS4ERR_STALE_STATEID:
4388 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004389 break;
4390 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004391 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004392 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004393 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04004394 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004395}
4396
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004397static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004398{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004399 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004401 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004402 return;
Trond Myklebust795a88c2012-09-10 13:26:49 -04004403 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004404 /* Note: exit _without_ running nfs4_locku_done */
4405 task->tk_action = NULL;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05004406 nfs4_sequence_done(task, &calldata->res.seq_res);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004407 return;
4408 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004409 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004410 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004411 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004412 &calldata->res.seq_res,
4413 task) != 0)
4414 nfs_release_seqid(calldata->arg.seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415}
4416
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004417static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004418 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004419 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004420 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004421};
4422
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004423static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4424 struct nfs_open_context *ctx,
4425 struct nfs4_lock_state *lsp,
4426 struct nfs_seqid *seqid)
4427{
4428 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004429 struct rpc_message msg = {
4430 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4431 .rpc_cred = ctx->cred,
4432 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004433 struct rpc_task_setup task_setup_data = {
4434 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004435 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004436 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004437 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004438 .flags = RPC_TASK_ASYNC,
4439 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004440
Frank Filz137d6ac2007-07-09 15:32:29 -07004441 /* Ensure this is an unlock - when canceling a lock, the
4442 * canceled lock is passed in, and it won't be an unlock.
4443 */
4444 fl->fl_type = F_UNLCK;
4445
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004446 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4447 if (data == NULL) {
4448 nfs_free_seqid(seqid);
4449 return ERR_PTR(-ENOMEM);
4450 }
4451
Trond Myklebust9d12b212012-01-17 22:04:25 -05004452 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004453 msg.rpc_argp = &data->arg;
4454 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004455 task_setup_data.callback_data = data;
4456 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004457}
4458
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4460{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004461 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004462 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004463 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004464 struct rpc_task *task;
4465 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004466 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467
Trond Myklebust9b073572006-06-29 16:38:34 -04004468 status = nfs4_set_lock_state(state, request);
4469 /* Unlock _before_ we do the RPC call */
4470 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004471 down_read(&nfsi->rwsem);
4472 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4473 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004474 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004475 }
4476 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004477 if (status != 0)
4478 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004479 /* Is this a delegated lock? */
4480 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004481 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004482 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004483 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004484 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004485 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004486 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004487 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004488 status = PTR_ERR(task);
4489 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004490 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004491 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004492 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004493out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004494 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004495 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496}
4497
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004498struct nfs4_lockdata {
4499 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004500 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004501 struct nfs4_lock_state *lsp;
4502 struct nfs_open_context *ctx;
4503 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004504 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004505 int rpc_status;
4506 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004507 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004508};
4509
4510static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004511 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4512 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004513{
4514 struct nfs4_lockdata *p;
4515 struct inode *inode = lsp->ls_state->inode;
4516 struct nfs_server *server = NFS_SERVER(inode);
4517
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004518 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004519 if (p == NULL)
4520 return NULL;
4521
4522 p->arg.fh = NFS_FH(inode);
4523 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004524 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004525 if (p->arg.open_seqid == NULL)
4526 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004527 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004528 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004529 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004530 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004531 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004532 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004533 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004534 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004535 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004536 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004537 atomic_inc(&lsp->ls_count);
4538 p->ctx = get_nfs_open_context(ctx);
4539 memcpy(&p->fl, fl, sizeof(p->fl));
4540 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004541out_free_seqid:
4542 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004543out_free:
4544 kfree(p);
4545 return NULL;
4546}
4547
4548static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4549{
4550 struct nfs4_lockdata *data = calldata;
4551 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552
Harvey Harrison3110ff82008-05-02 13:42:44 -07004553 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004554 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4555 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004556 /* Do we need to do an open_to_lock_owner? */
4557 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004558 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004559 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004560 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004561 data->arg.open_stateid = &state->stateid;
4562 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004563 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004564 } else
4565 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004566 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004567 if (nfs4_setup_sequence(data->server,
4568 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004569 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004570 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04004571 return;
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004572 nfs_release_seqid(data->arg.open_seqid);
4573out_release_lock_seqid:
4574 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004575 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004576}
4577
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004578static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4579{
4580 struct nfs4_lockdata *data = calldata;
4581
Harvey Harrison3110ff82008-05-02 13:42:44 -07004582 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004583
Trond Myklebust14516c32010-07-31 14:29:06 -04004584 if (!nfs4_sequence_done(task, &data->res.seq_res))
4585 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004586
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004587 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004588 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004589 if (data->rpc_status == 0)
4590 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4591 else
4592 goto out;
4593 }
4594 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004595 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004596 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04004597 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004598 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004599out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004600 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004601}
4602
4603static void nfs4_lock_release(void *calldata)
4604{
4605 struct nfs4_lockdata *data = calldata;
4606
Harvey Harrison3110ff82008-05-02 13:42:44 -07004607 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004608 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004609 if (data->cancelled != 0) {
4610 struct rpc_task *task;
4611 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4612 data->arg.lock_seqid);
4613 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004614 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004615 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004616 } else
4617 nfs_free_seqid(data->arg.lock_seqid);
4618 nfs4_put_lock_state(data->lsp);
4619 put_nfs_open_context(data->ctx);
4620 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004621 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004622}
4623
4624static const struct rpc_call_ops nfs4_lock_ops = {
4625 .rpc_call_prepare = nfs4_lock_prepare,
4626 .rpc_call_done = nfs4_lock_done,
4627 .rpc_release = nfs4_lock_release,
4628};
4629
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004630static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4631{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004632 switch (error) {
4633 case -NFS4ERR_ADMIN_REVOKED:
4634 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004635 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004636 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04004637 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004638 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004639 break;
4640 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004641 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004642 case -NFS4ERR_EXPIRED:
4643 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004644 };
4645}
4646
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004647static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004648{
4649 struct nfs4_lockdata *data;
4650 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004651 struct rpc_message msg = {
4652 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4653 .rpc_cred = state->owner->so_cred,
4654 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004655 struct rpc_task_setup task_setup_data = {
4656 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004657 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004658 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004659 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004660 .flags = RPC_TASK_ASYNC,
4661 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004662 int ret;
4663
Harvey Harrison3110ff82008-05-02 13:42:44 -07004664 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004665 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004666 fl->fl_u.nfs4_fl.owner,
4667 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004668 if (data == NULL)
4669 return -ENOMEM;
4670 if (IS_SETLKW(cmd))
4671 data->arg.block = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004672 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004673 msg.rpc_argp = &data->arg;
4674 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004675 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004676 if (recovery_type > NFS_LOCK_NEW) {
4677 if (recovery_type == NFS_LOCK_RECLAIM)
4678 data->arg.reclaim = NFS_LOCK_RECLAIM;
4679 nfs4_set_sequence_privileged(&data->arg.seq_args);
4680 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04004681 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004682 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004683 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004684 ret = nfs4_wait_for_completion_rpc_task(task);
4685 if (ret == 0) {
4686 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004687 if (ret)
4688 nfs4_handle_setlk_error(data->server, data->lsp,
4689 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004690 } else
4691 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004692 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004693 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004694 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695}
4696
4697static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4698{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004699 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004700 struct nfs4_exception exception = {
4701 .inode = state->inode,
4702 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004703 int err;
4704
4705 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004706 /* Cache the lock if possible... */
4707 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4708 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004709 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004710 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004711 break;
4712 nfs4_handle_exception(server, err, &exception);
4713 } while (exception.retry);
4714 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715}
4716
4717static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4718{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004719 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004720 struct nfs4_exception exception = {
4721 .inode = state->inode,
4722 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004723 int err;
4724
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004725 err = nfs4_set_lock_state(state, request);
4726 if (err != 0)
4727 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004728 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004729 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4730 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004731 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004732 switch (err) {
4733 default:
4734 goto out;
4735 case -NFS4ERR_GRACE:
4736 case -NFS4ERR_DELAY:
4737 nfs4_handle_exception(server, err, &exception);
4738 err = 0;
4739 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004740 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004741out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004742 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743}
4744
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004745#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004746/**
4747 * nfs41_check_expired_locks - possibly free a lock stateid
4748 *
4749 * @state: NFSv4 state for an inode
4750 *
4751 * Returns NFS_OK if recovery for this stateid is now finished.
4752 * Otherwise a negative NFS4ERR value is returned.
4753 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004754static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004755{
Chuck Levereb64cf92012-07-11 16:30:05 -04004756 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004757 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004758 struct nfs_server *server = NFS_SERVER(state->inode);
4759
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004760 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04004761 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004762 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4763 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004764 /* Free the stateid unless the server
4765 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04004766 if (status != -NFS4ERR_BAD_STATEID)
4767 nfs41_free_stateid(server,
4768 &lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004769 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004770 ret = status;
4771 }
4772 }
4773 };
4774
4775 return ret;
4776}
4777
4778static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4779{
4780 int status = NFS_OK;
4781
4782 if (test_bit(LK_STATE_IN_USE, &state->flags))
4783 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04004784 if (status != NFS_OK)
4785 status = nfs4_lock_expired(state, request);
4786 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004787}
4788#endif
4789
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4791{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004792 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004793 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004794 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004796 if ((fl_flags & FL_POSIX) &&
4797 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4798 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004799 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004800 status = nfs4_set_lock_state(state, request);
4801 if (status != 0)
4802 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004803 request->fl_flags |= FL_ACCESS;
4804 status = do_vfs_lock(request->fl_file, request);
4805 if (status < 0)
4806 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004807 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004808 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004809 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004810 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004811 request->fl_flags = fl_flags & ~FL_SLEEP;
4812 status = do_vfs_lock(request->fl_file, request);
4813 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004814 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004815 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004816 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004817 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004818 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004819 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004820 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004821 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4822 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004823out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004824 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004825out:
4826 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827 return status;
4828}
4829
4830static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4831{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004832 struct nfs4_exception exception = {
4833 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04004834 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004835 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836 int err;
4837
4838 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004839 err = _nfs4_proc_setlk(state, cmd, request);
4840 if (err == -NFS4ERR_DENIED)
4841 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004843 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844 } while (exception.retry);
4845 return err;
4846}
4847
4848static int
4849nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4850{
4851 struct nfs_open_context *ctx;
4852 struct nfs4_state *state;
4853 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4854 int status;
4855
4856 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004857 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858 state = ctx->state;
4859
4860 if (request->fl_start < 0 || request->fl_end < 0)
4861 return -EINVAL;
4862
Trond Myklebustd9531262009-07-21 19:22:38 -04004863 if (IS_GETLK(cmd)) {
4864 if (state != NULL)
4865 return nfs4_proc_getlk(state, F_GETLK, request);
4866 return 0;
4867 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868
4869 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4870 return -EINVAL;
4871
Trond Myklebustd9531262009-07-21 19:22:38 -04004872 if (request->fl_type == F_UNLCK) {
4873 if (state != NULL)
4874 return nfs4_proc_unlck(state, cmd, request);
4875 return 0;
4876 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877
Trond Myklebustd9531262009-07-21 19:22:38 -04004878 if (state == NULL)
4879 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04004880 /*
4881 * Don't rely on the VFS having checked the file open mode,
4882 * since it won't do this for flock() locks.
4883 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04004884 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04004885 case F_RDLCK:
4886 if (!(filp->f_mode & FMODE_READ))
4887 return -EBADF;
4888 break;
4889 case F_WRLCK:
4890 if (!(filp->f_mode & FMODE_WRITE))
4891 return -EBADF;
4892 }
4893
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894 do {
4895 status = nfs4_proc_setlk(state, cmd, request);
4896 if ((status != -EAGAIN) || IS_SETLK(cmd))
4897 break;
4898 timeout = nfs4_set_lock_task_retry(timeout);
4899 status = -ERESTARTSYS;
4900 if (signalled())
4901 break;
4902 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903 return status;
4904}
4905
Trond Myklebust888e6942005-11-04 15:38:11 -05004906int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4907{
4908 struct nfs_server *server = NFS_SERVER(state->inode);
4909 struct nfs4_exception exception = { };
4910 int err;
4911
4912 err = nfs4_set_lock_state(state, fl);
4913 if (err != 0)
4914 goto out;
4915 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004916 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004917 switch (err) {
4918 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004919 printk(KERN_ERR "NFS: %s: unhandled error "
4920 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004921 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004922 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004923 goto out;
4924 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004925 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07004926 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004927 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004928 nfs4_schedule_lease_recovery(server->nfs_client);
4929 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004930 case -NFS4ERR_BADSESSION:
4931 case -NFS4ERR_BADSLOT:
4932 case -NFS4ERR_BAD_HIGH_SLOT:
4933 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4934 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04004935 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004936 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004937 case -ERESTARTSYS:
4938 /*
4939 * The show must go on: exit, but mark the
4940 * stateid as needing recovery.
4941 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004942 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004943 case -NFS4ERR_ADMIN_REVOKED:
4944 case -NFS4ERR_BAD_STATEID:
4945 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004946 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004947 err = 0;
4948 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004949 case -EKEYEXPIRED:
4950 /*
4951 * User RPCSEC_GSS context has expired.
4952 * We cannot recover this stateid now, so
4953 * skip it and allow recovery thread to
4954 * proceed.
4955 */
4956 err = 0;
4957 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004958 case -ENOMEM:
4959 case -NFS4ERR_DENIED:
4960 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4961 err = 0;
4962 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004963 case -NFS4ERR_DELAY:
4964 break;
4965 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004966 err = nfs4_handle_exception(server, err, &exception);
4967 } while (exception.retry);
4968out:
4969 return err;
4970}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004971
Trond Myklebustcf470c32012-03-07 13:49:12 -05004972struct nfs_release_lockowner_data {
4973 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004974 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004975 struct nfs_release_lockowner_args args;
4976};
4977
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004978static void nfs4_release_lockowner_release(void *calldata)
4979{
Trond Myklebustcf470c32012-03-07 13:49:12 -05004980 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004981 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004982 kfree(calldata);
4983}
4984
Trond Myklebust17280172012-03-11 13:11:00 -04004985static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004986 .rpc_release = nfs4_release_lockowner_release,
4987};
4988
Trond Myklebustcf470c32012-03-07 13:49:12 -05004989int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004990{
4991 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004992 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004993 struct rpc_message msg = {
4994 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4995 };
4996
4997 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05004998 return -EINVAL;
4999 data = kmalloc(sizeof(*data), GFP_NOFS);
5000 if (!data)
5001 return -ENOMEM;
5002 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005003 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005004 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5005 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5006 data->args.lock_owner.s_dev = server->s_dev;
5007 msg.rpc_argp = &data->args;
5008 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5009 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005010}
5011
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005012#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5013
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005014static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5015 const void *buf, size_t buflen,
5016 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005017{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005018 if (strcmp(key, "") != 0)
5019 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005020
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005021 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005022}
5023
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005024static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5025 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005026{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005027 if (strcmp(key, "") != 0)
5028 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005029
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005030 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005031}
5032
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005033static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5034 size_t list_len, const char *name,
5035 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005036{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005037 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005038
J. Bruce Fields096455a2006-03-20 23:23:42 -05005039 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5040 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005041
5042 if (list && len <= list_len)
5043 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005044 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005045}
5046
Andy Adamson533eb462011-06-13 18:25:56 -04005047/*
5048 * nfs_fhget will use either the mounted_on_fileid or the fileid
5049 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005050static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5051{
Andy Adamson533eb462011-06-13 18:25:56 -04005052 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5053 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5054 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005055 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005056 return;
5057
5058 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005059 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005060 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5061 fattr->nlink = 2;
5062}
5063
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005064static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5065 const struct qstr *name,
5066 struct nfs4_fs_locations *fs_locations,
5067 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005068{
5069 struct nfs_server *server = NFS_SERVER(dir);
5070 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005071 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005072 };
5073 struct nfs4_fs_locations_arg args = {
5074 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005075 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005076 .page = page,
5077 .bitmask = bitmask,
5078 };
Benny Halevy22958462009-04-01 09:22:02 -04005079 struct nfs4_fs_locations_res res = {
5080 .fs_locations = fs_locations,
5081 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005082 struct rpc_message msg = {
5083 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5084 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005085 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005086 };
5087 int status;
5088
Harvey Harrison3110ff82008-05-02 13:42:44 -07005089 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005090
5091 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5092 * is not supported */
5093 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5094 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5095 else
5096 bitmask[0] |= FATTR4_WORD0_FILEID;
5097
Trond Myklebustc228fd32007-01-13 02:28:11 -05005098 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005099 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005100 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005101 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005102 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005103 return status;
5104}
5105
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005106int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5107 const struct qstr *name,
5108 struct nfs4_fs_locations *fs_locations,
5109 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005110{
5111 struct nfs4_exception exception = { };
5112 int err;
5113 do {
5114 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005115 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005116 &exception);
5117 } while (exception.retry);
5118 return err;
5119}
5120
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005121static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5122{
5123 int status;
5124 struct nfs4_secinfo_arg args = {
5125 .dir_fh = NFS_FH(dir),
5126 .name = name,
5127 };
5128 struct nfs4_secinfo_res res = {
5129 .flavors = flavors,
5130 };
5131 struct rpc_message msg = {
5132 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5133 .rpc_argp = &args,
5134 .rpc_resp = &res,
5135 };
5136
5137 dprintk("NFS call secinfo %s\n", name->name);
5138 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5139 dprintk("NFS reply secinfo: %d\n", status);
5140 return status;
5141}
5142
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005143int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5144 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005145{
5146 struct nfs4_exception exception = { };
5147 int err;
5148 do {
5149 err = nfs4_handle_exception(NFS_SERVER(dir),
5150 _nfs4_proc_secinfo(dir, name, flavors),
5151 &exception);
5152 } while (exception.retry);
5153 return err;
5154}
5155
Andy Adamson557134a2009-04-01 09:21:53 -04005156#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005157/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005158 * Check the exchange flags returned by the server for invalid flags, having
5159 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5160 * DS flags set.
5161 */
5162static int nfs4_check_cl_exchange_flags(u32 flags)
5163{
5164 if (flags & ~EXCHGID4_FLAG_MASK_R)
5165 goto out_inval;
5166 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5167 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5168 goto out_inval;
5169 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5170 goto out_inval;
5171 return NFS_OK;
5172out_inval:
5173 return -NFS4ERR_INVAL;
5174}
5175
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005176static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005177nfs41_same_server_scope(struct nfs41_server_scope *a,
5178 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005179{
5180 if (a->server_scope_sz == b->server_scope_sz &&
5181 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5182 return true;
5183
5184 return false;
5185}
5186
Andy Adamson357f54d2010-12-14 10:11:57 -05005187/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005188 * nfs4_proc_bind_conn_to_session()
5189 *
5190 * The 4.1 client currently uses the same TCP connection for the
5191 * fore and backchannel.
5192 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005193int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005194{
5195 int status;
5196 struct nfs41_bind_conn_to_session_res res;
5197 struct rpc_message msg = {
5198 .rpc_proc =
5199 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5200 .rpc_argp = clp,
5201 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005202 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005203 };
5204
5205 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005206
5207 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5208 if (unlikely(res.session == NULL)) {
5209 status = -ENOMEM;
5210 goto out;
5211 }
5212
5213 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5214 if (status == 0) {
5215 if (memcmp(res.session->sess_id.data,
5216 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5217 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5218 status = -EIO;
5219 goto out_session;
5220 }
5221 if (res.dir != NFS4_CDFS4_BOTH) {
5222 dprintk("NFS: %s: Unexpected direction from server\n",
5223 __func__);
5224 status = -EIO;
5225 goto out_session;
5226 }
5227 if (res.use_conn_in_rdma_mode) {
5228 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5229 __func__);
5230 status = -EIO;
5231 goto out_session;
5232 }
5233 }
5234out_session:
5235 kfree(res.session);
5236out:
5237 dprintk("<-- %s status= %d\n", __func__, status);
5238 return status;
5239}
5240
5241/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005242 * nfs4_proc_exchange_id()
5243 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005244 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5245 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04005246 * Since the clientid has expired, all compounds using sessions
5247 * associated with the stale clientid will be returning
5248 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5249 * be in some phase of session reset.
5250 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005251int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005252{
5253 nfs4_verifier verifier;
5254 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005255 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005256 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005257 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005258 };
5259 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005260 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005261 };
5262 int status;
5263 struct rpc_message msg = {
5264 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5265 .rpc_argp = &args,
5266 .rpc_resp = &res,
5267 .rpc_cred = cred,
5268 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005269
Chuck Leverf0920752012-05-21 22:45:41 -04005270 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04005271 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5272 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005273 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5274 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5275 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005276
Chuck Leveracdeb692012-05-21 22:46:16 -04005277 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005278 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005279 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005280 status = -ENOMEM;
5281 goto out;
5282 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005283
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005284 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005285 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005286 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005287 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005288 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005289 }
5290
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005291 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005292 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005293 status = -ENOMEM;
5294 goto out_server_scope;
5295 }
5296
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005297 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005298 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005299 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005300
Chuck Lever177313f2012-05-21 22:44:58 -04005301 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005302 clp->cl_clientid = res.clientid;
5303 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5304 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5305 clp->cl_seqid = res.seqid;
5306
Chuck Leveracdeb692012-05-21 22:46:16 -04005307 kfree(clp->cl_serverowner);
5308 clp->cl_serverowner = res.server_owner;
5309 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005310
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005311 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005312 kfree(clp->cl_implid);
5313 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005314
Chuck Lever177313f2012-05-21 22:44:58 -04005315 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005316 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005317 res.server_scope)) {
5318 dprintk("%s: server_scope mismatch detected\n",
5319 __func__);
5320 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005321 kfree(clp->cl_serverscope);
5322 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005323 }
5324
Chuck Lever177313f2012-05-21 22:44:58 -04005325 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005326 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005327 goto out;
5328 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005329 } else
5330 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005331
Chuck Leveracdeb692012-05-21 22:46:16 -04005332out_server_owner:
5333 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005334out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005335 kfree(res.server_scope);
5336out:
Chuck Lever177313f2012-05-21 22:44:58 -04005337 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005338 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005339 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005340 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04005341 clp->cl_implid->date.seconds,
5342 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005343 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005344 return status;
5345}
5346
Trond Myklebust66245532012-05-25 17:18:09 -04005347static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5348 struct rpc_cred *cred)
5349{
5350 struct rpc_message msg = {
5351 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5352 .rpc_argp = clp,
5353 .rpc_cred = cred,
5354 };
5355 int status;
5356
5357 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5358 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04005359 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04005360 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5361 return status;
5362}
5363
5364static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5365 struct rpc_cred *cred)
5366{
5367 unsigned int loop;
5368 int ret;
5369
5370 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5371 ret = _nfs4_proc_destroy_clientid(clp, cred);
5372 switch (ret) {
5373 case -NFS4ERR_DELAY:
5374 case -NFS4ERR_CLIENTID_BUSY:
5375 ssleep(1);
5376 break;
5377 default:
5378 return ret;
5379 }
5380 }
5381 return 0;
5382}
5383
5384int nfs4_destroy_clientid(struct nfs_client *clp)
5385{
5386 struct rpc_cred *cred;
5387 int ret = 0;
5388
5389 if (clp->cl_mvops->minor_version < 1)
5390 goto out;
5391 if (clp->cl_exchange_flags == 0)
5392 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04005393 if (clp->cl_preserve_clid)
5394 goto out;
Trond Myklebust66245532012-05-25 17:18:09 -04005395 cred = nfs4_get_exchange_id_cred(clp);
5396 ret = nfs4_proc_destroy_clientid(clp, cred);
5397 if (cred)
5398 put_rpccred(cred);
5399 switch (ret) {
5400 case 0:
5401 case -NFS4ERR_STALE_CLIENTID:
5402 clp->cl_exchange_flags = 0;
5403 }
5404out:
5405 return ret;
5406}
5407
Andy Adamson2050f0c2009-04-01 09:22:30 -04005408struct nfs4_get_lease_time_data {
5409 struct nfs4_get_lease_time_args *args;
5410 struct nfs4_get_lease_time_res *res;
5411 struct nfs_client *clp;
5412};
5413
5414static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5415 void *calldata)
5416{
Andy Adamson2050f0c2009-04-01 09:22:30 -04005417 struct nfs4_get_lease_time_data *data =
5418 (struct nfs4_get_lease_time_data *)calldata;
5419
5420 dprintk("--> %s\n", __func__);
5421 /* just setup sequence, do not trigger session recovery
5422 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005423 nfs41_setup_sequence(data->clp->cl_session,
5424 &data->args->la_seq_args,
5425 &data->res->lr_seq_res,
5426 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005427 dprintk("<-- %s\n", __func__);
5428}
5429
5430/*
5431 * Called from nfs4_state_manager thread for session setup, so don't recover
5432 * from sequence operation or clientid errors.
5433 */
5434static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5435{
5436 struct nfs4_get_lease_time_data *data =
5437 (struct nfs4_get_lease_time_data *)calldata;
5438
5439 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005440 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5441 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005442 switch (task->tk_status) {
5443 case -NFS4ERR_DELAY:
5444 case -NFS4ERR_GRACE:
5445 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5446 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5447 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005448 /* fall through */
5449 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005450 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005451 return;
5452 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005453 dprintk("<-- %s\n", __func__);
5454}
5455
Trond Myklebust17280172012-03-11 13:11:00 -04005456static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005457 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5458 .rpc_call_done = nfs4_get_lease_time_done,
5459};
5460
5461int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5462{
5463 struct rpc_task *task;
5464 struct nfs4_get_lease_time_args args;
5465 struct nfs4_get_lease_time_res res = {
5466 .lr_fsinfo = fsinfo,
5467 };
5468 struct nfs4_get_lease_time_data data = {
5469 .args = &args,
5470 .res = &res,
5471 .clp = clp,
5472 };
5473 struct rpc_message msg = {
5474 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5475 .rpc_argp = &args,
5476 .rpc_resp = &res,
5477 };
5478 struct rpc_task_setup task_setup = {
5479 .rpc_client = clp->cl_rpcclient,
5480 .rpc_message = &msg,
5481 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005482 .callback_data = &data,
5483 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005484 };
5485 int status;
5486
Trond Myklebust9d12b212012-01-17 22:04:25 -05005487 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005488 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005489 dprintk("--> %s\n", __func__);
5490 task = rpc_run_task(&task_setup);
5491
5492 if (IS_ERR(task))
5493 status = PTR_ERR(task);
5494 else {
5495 status = task->tk_status;
5496 rpc_put_task(task);
5497 }
5498 dprintk("<-- %s return %d\n", __func__, status);
5499
5500 return status;
5501}
5502
Andy Adamsonfc931582009-04-01 09:22:31 -04005503/*
5504 * Initialize the values to be used by the client in CREATE_SESSION
5505 * If nfs4_init_session set the fore channel request and response sizes,
5506 * use them.
5507 *
5508 * Set the back channel max_resp_sz_cached to zero to force the client to
5509 * always set csa_cachethis to FALSE because the current implementation
5510 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5511 */
5512static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5513{
5514 struct nfs4_session *session = args->client->cl_session;
Trond Myklebustae72ae672012-11-20 11:02:55 -05005515 unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
5516 mxresp_sz = session->fc_target_max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005517
5518 if (mxrqst_sz == 0)
5519 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5520 if (mxresp_sz == 0)
5521 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5522 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005523 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5524 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005525 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005526 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005527
5528 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005529 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005530 __func__,
5531 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005532 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005533
5534 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005535 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5536 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5537 args->bc_attrs.max_resp_sz_cached = 0;
5538 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5539 args->bc_attrs.max_reqs = 1;
5540
5541 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5542 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5543 __func__,
5544 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5545 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5546 args->bc_attrs.max_reqs);
5547}
5548
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005549static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005550{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005551 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5552 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5553
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005554 if (rcvd->max_resp_sz > sent->max_resp_sz)
5555 return -EINVAL;
5556 /*
5557 * Our requested max_ops is the minimum we need; we're not
5558 * prepared to break up compounds into smaller pieces than that.
5559 * So, no point even trying to continue if the server won't
5560 * cooperate:
5561 */
5562 if (rcvd->max_ops < sent->max_ops)
5563 return -EINVAL;
5564 if (rcvd->max_reqs == 0)
5565 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005566 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5567 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005568 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005569}
5570
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005571static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5572{
5573 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5574 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005575
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005576 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5577 return -EINVAL;
5578 if (rcvd->max_resp_sz < sent->max_resp_sz)
5579 return -EINVAL;
5580 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5581 return -EINVAL;
5582 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005583 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005584 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005585 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005586 return -EINVAL;
5587 return 0;
5588}
Andy Adamson8d353012009-04-01 09:22:32 -04005589
Andy Adamson8d353012009-04-01 09:22:32 -04005590static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5591 struct nfs4_session *session)
5592{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005593 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005594
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005595 ret = nfs4_verify_fore_channel_attrs(args, session);
5596 if (ret)
5597 return ret;
5598 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005599}
5600
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005601static int _nfs4_proc_create_session(struct nfs_client *clp,
5602 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005603{
5604 struct nfs4_session *session = clp->cl_session;
5605 struct nfs41_create_session_args args = {
5606 .client = clp,
5607 .cb_program = NFS4_CALLBACK,
5608 };
5609 struct nfs41_create_session_res res = {
5610 .client = clp,
5611 };
5612 struct rpc_message msg = {
5613 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5614 .rpc_argp = &args,
5615 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005616 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04005617 };
5618 int status;
5619
5620 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005621 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005622
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005623 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005624
Trond Myklebust43095d32012-11-20 11:13:12 -05005625 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04005626 /* Verify the session's negotiated channel_attrs values */
5627 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005628 /* Increment the clientid slot sequence id */
5629 clp->cl_seqid++;
5630 }
5631
5632 return status;
5633}
5634
5635/*
5636 * Issues a CREATE_SESSION operation to the server.
5637 * It is the responsibility of the caller to verify the session is
5638 * expired before calling this routine.
5639 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005640int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005641{
5642 int status;
5643 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005644 struct nfs4_session *session = clp->cl_session;
5645
5646 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5647
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005648 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04005649 if (status)
5650 goto out;
5651
Andy Adamsonaacd5532011-11-09 13:58:21 -05005652 /* Init or reset the session slot tables */
5653 status = nfs4_setup_session_slot_tables(session);
5654 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005655 if (status)
5656 goto out;
5657
5658 ptr = (unsigned *)&session->sess_id.data[0];
5659 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5660 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005661out:
5662 dprintk("<-- %s\n", __func__);
5663 return status;
5664}
5665
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005666/*
5667 * Issue the over-the-wire RPC DESTROY_SESSION.
5668 * The caller must serialize access to this routine.
5669 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005670int nfs4_proc_destroy_session(struct nfs4_session *session,
5671 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005672{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005673 struct rpc_message msg = {
5674 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5675 .rpc_argp = session,
5676 .rpc_cred = cred,
5677 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005678 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005679
5680 dprintk("--> nfs4_proc_destroy_session\n");
5681
5682 /* session is still being setup */
5683 if (session->clp->cl_cons_state != NFS_CS_READY)
5684 return status;
5685
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005686 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005687
5688 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04005689 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005690 "Session has been destroyed regardless...\n", status);
5691
5692 dprintk("<-- nfs4_proc_destroy_session\n");
5693 return status;
5694}
5695
Trond Myklebust7b38c362012-05-23 13:23:31 -04005696/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005697 * Renew the cl_session lease.
5698 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005699struct nfs4_sequence_data {
5700 struct nfs_client *clp;
5701 struct nfs4_sequence_args args;
5702 struct nfs4_sequence_res res;
5703};
5704
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005705static void nfs41_sequence_release(void *data)
5706{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005707 struct nfs4_sequence_data *calldata = data;
5708 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005709
Alexandros Batsakis71358402010-02-05 03:45:05 -08005710 if (atomic_read(&clp->cl_count) > 1)
5711 nfs4_schedule_state_renewal(clp);
5712 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005713 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005714}
5715
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005716static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5717{
5718 switch(task->tk_status) {
5719 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005720 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5721 return -EAGAIN;
5722 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005723 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005724 }
5725 return 0;
5726}
5727
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005728static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005729{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005730 struct nfs4_sequence_data *calldata = data;
5731 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005732
Trond Myklebust14516c32010-07-31 14:29:06 -04005733 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5734 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005735
5736 if (task->tk_status < 0) {
5737 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005738 if (atomic_read(&clp->cl_count) == 1)
5739 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005740
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005741 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5742 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005743 return;
5744 }
5745 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005746 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005747out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005748 dprintk("<-- %s\n", __func__);
5749}
5750
5751static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5752{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005753 struct nfs4_sequence_data *calldata = data;
5754 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005755 struct nfs4_sequence_args *args;
5756 struct nfs4_sequence_res *res;
5757
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005758 args = task->tk_msg.rpc_argp;
5759 res = task->tk_msg.rpc_resp;
5760
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005761 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005762}
5763
5764static const struct rpc_call_ops nfs41_sequence_ops = {
5765 .rpc_call_done = nfs41_sequence_call_done,
5766 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005767 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005768};
5769
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005770static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
5771 struct rpc_cred *cred,
5772 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005773{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005774 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005775 struct rpc_message msg = {
5776 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5777 .rpc_cred = cred,
5778 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005779 struct rpc_task_setup task_setup_data = {
5780 .rpc_client = clp->cl_rpcclient,
5781 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005782 .callback_ops = &nfs41_sequence_ops,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005783 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5784 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005785
Alexandros Batsakis71358402010-02-05 03:45:05 -08005786 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005787 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005788 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005789 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005790 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005791 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005792 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05005793 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005794 if (is_privileged)
5795 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005796 msg.rpc_argp = &calldata->args;
5797 msg.rpc_resp = &calldata->res;
5798 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005799 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005800
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005801 return rpc_run_task(&task_setup_data);
5802}
5803
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005804static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005805{
5806 struct rpc_task *task;
5807 int ret = 0;
5808
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005809 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5810 return 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005811 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005812 if (IS_ERR(task))
5813 ret = PTR_ERR(task);
5814 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005815 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005816 dprintk("<-- %s status=%d\n", __func__, ret);
5817 return ret;
5818}
5819
5820static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5821{
5822 struct rpc_task *task;
5823 int ret;
5824
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005825 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005826 if (IS_ERR(task)) {
5827 ret = PTR_ERR(task);
5828 goto out;
5829 }
5830 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005831 if (!ret) {
5832 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5833
5834 if (task->tk_status == 0)
5835 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005836 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005837 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005838 rpc_put_task(task);
5839out:
5840 dprintk("<-- %s status=%d\n", __func__, ret);
5841 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005842}
5843
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005844struct nfs4_reclaim_complete_data {
5845 struct nfs_client *clp;
5846 struct nfs41_reclaim_complete_args arg;
5847 struct nfs41_reclaim_complete_res res;
5848};
5849
5850static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5851{
5852 struct nfs4_reclaim_complete_data *calldata = data;
5853
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005854 nfs41_setup_sequence(calldata->clp->cl_session,
5855 &calldata->arg.seq_args,
5856 &calldata->res.seq_res,
5857 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005858}
5859
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005860static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5861{
5862 switch(task->tk_status) {
5863 case 0:
5864 case -NFS4ERR_COMPLETE_ALREADY:
5865 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5866 break;
5867 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005868 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005869 /* fall through */
5870 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005871 return -EAGAIN;
5872 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005873 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005874 }
5875 return 0;
5876}
5877
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005878static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5879{
5880 struct nfs4_reclaim_complete_data *calldata = data;
5881 struct nfs_client *clp = calldata->clp;
5882 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5883
5884 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005885 if (!nfs41_sequence_done(task, res))
5886 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005887
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005888 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5889 rpc_restart_call_prepare(task);
5890 return;
5891 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005892 dprintk("<-- %s\n", __func__);
5893}
5894
5895static void nfs4_free_reclaim_complete_data(void *data)
5896{
5897 struct nfs4_reclaim_complete_data *calldata = data;
5898
5899 kfree(calldata);
5900}
5901
5902static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5903 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5904 .rpc_call_done = nfs4_reclaim_complete_done,
5905 .rpc_release = nfs4_free_reclaim_complete_data,
5906};
5907
5908/*
5909 * Issue a global reclaim complete.
5910 */
5911static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5912{
5913 struct nfs4_reclaim_complete_data *calldata;
5914 struct rpc_task *task;
5915 struct rpc_message msg = {
5916 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5917 };
5918 struct rpc_task_setup task_setup_data = {
5919 .rpc_client = clp->cl_rpcclient,
5920 .rpc_message = &msg,
5921 .callback_ops = &nfs4_reclaim_complete_call_ops,
5922 .flags = RPC_TASK_ASYNC,
5923 };
5924 int status = -ENOMEM;
5925
5926 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005927 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005928 if (calldata == NULL)
5929 goto out;
5930 calldata->clp = clp;
5931 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005932
Trond Myklebust9d12b212012-01-17 22:04:25 -05005933 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005934 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005935 msg.rpc_argp = &calldata->arg;
5936 msg.rpc_resp = &calldata->res;
5937 task_setup_data.callback_data = calldata;
5938 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005939 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005940 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005941 goto out;
5942 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05005943 status = nfs4_wait_for_completion_rpc_task(task);
5944 if (status == 0)
5945 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005946 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005947 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005948out:
5949 dprintk("<-- %s status=%d\n", __func__, status);
5950 return status;
5951}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005952
5953static void
5954nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5955{
5956 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00005957 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04005958 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005959
5960 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00005961 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5962 * right now covering the LAYOUTGET we are about to send.
5963 * However, that is not so catastrophic, and there seems
5964 * to be no way to prevent it completely.
5965 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04005966 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005967 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005968 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00005969 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5970 NFS_I(lgp->args.inode)->layout,
5971 lgp->args.ctx->state)) {
5972 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00005973 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005974}
5975
5976static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5977{
5978 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07005979 struct inode *inode = lgp->args.inode;
5980 struct nfs_server *server = NFS_SERVER(inode);
5981 struct pnfs_layout_hdr *lo;
5982 struct nfs4_state *state = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005983
5984 dprintk("--> %s\n", __func__);
5985
Trond Myklebust6ba7db32012-10-22 20:07:20 -04005986 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07005987 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005988
5989 switch (task->tk_status) {
5990 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07005991 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005992 case -NFS4ERR_LAYOUTTRYLATER:
5993 case -NFS4ERR_RECALLCONFLICT:
5994 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07005995 break;
5996 case -NFS4ERR_EXPIRED:
5997 case -NFS4ERR_BAD_STATEID:
5998 spin_lock(&inode->i_lock);
5999 lo = NFS_I(inode)->layout;
6000 if (!lo || list_empty(&lo->plh_segs)) {
6001 spin_unlock(&inode->i_lock);
6002 /* If the open stateid was bad, then recover it. */
6003 state = lgp->args.ctx->state;
6004 } else {
6005 LIST_HEAD(head);
6006
6007 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6008 spin_unlock(&inode->i_lock);
6009 /* Mark the bad layout state as invalid, then
6010 * retry using the open stateid. */
6011 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006012 }
6013 }
Trond Myklebustee314c22012-10-01 17:25:48 -07006014 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6015 rpc_restart_call_prepare(task);
6016out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006017 dprintk("<-- %s\n", __func__);
6018}
6019
Idan Kedar85541162012-08-02 11:47:10 +03006020static size_t max_response_pages(struct nfs_server *server)
6021{
6022 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6023 return nfs_page_array_len(0, max_resp_sz);
6024}
6025
6026static void nfs4_free_pages(struct page **pages, size_t size)
6027{
6028 int i;
6029
6030 if (!pages)
6031 return;
6032
6033 for (i = 0; i < size; i++) {
6034 if (!pages[i])
6035 break;
6036 __free_page(pages[i]);
6037 }
6038 kfree(pages);
6039}
6040
6041static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6042{
6043 struct page **pages;
6044 int i;
6045
6046 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6047 if (!pages) {
6048 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6049 return NULL;
6050 }
6051
6052 for (i = 0; i < size; i++) {
6053 pages[i] = alloc_page(gfp_flags);
6054 if (!pages[i]) {
6055 dprintk("%s: failed to allocate page\n", __func__);
6056 nfs4_free_pages(pages, size);
6057 return NULL;
6058 }
6059 }
6060
6061 return pages;
6062}
6063
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006064static void nfs4_layoutget_release(void *calldata)
6065{
6066 struct nfs4_layoutget *lgp = calldata;
Idan Kedar85541162012-08-02 11:47:10 +03006067 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6068 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006069
6070 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006071 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006072 put_nfs_open_context(lgp->args.ctx);
6073 kfree(calldata);
6074 dprintk("<-- %s\n", __func__);
6075}
6076
6077static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6078 .rpc_call_prepare = nfs4_layoutget_prepare,
6079 .rpc_call_done = nfs4_layoutget_done,
6080 .rpc_release = nfs4_layoutget_release,
6081};
6082
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006083struct pnfs_layout_segment *
6084nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006085{
6086 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Idan Kedar85541162012-08-02 11:47:10 +03006087 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006088 struct rpc_task *task;
6089 struct rpc_message msg = {
6090 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6091 .rpc_argp = &lgp->args,
6092 .rpc_resp = &lgp->res,
6093 };
6094 struct rpc_task_setup task_setup_data = {
6095 .rpc_client = server->client,
6096 .rpc_message = &msg,
6097 .callback_ops = &nfs4_layoutget_call_ops,
6098 .callback_data = lgp,
6099 .flags = RPC_TASK_ASYNC,
6100 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006101 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006102 int status = 0;
6103
6104 dprintk("--> %s\n", __func__);
6105
Idan Kedar85541162012-08-02 11:47:10 +03006106 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6107 if (!lgp->args.layout.pages) {
6108 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006109 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006110 }
6111 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
6112
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006113 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006114 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006115 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006116 task = rpc_run_task(&task_setup_data);
6117 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006118 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006119 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006120 if (status == 0)
6121 status = task->tk_status;
6122 if (status == 0)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006123 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006124 rpc_put_task(task);
6125 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006126 if (status)
6127 return ERR_PTR(status);
6128 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006129}
6130
Benny Halevycbe82602011-05-22 19:52:37 +03006131static void
6132nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6133{
6134 struct nfs4_layoutreturn *lrp = calldata;
6135
6136 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006137 nfs41_setup_sequence(lrp->clp->cl_session,
6138 &lrp->args.seq_args,
6139 &lrp->res.seq_res,
6140 task);
Benny Halevycbe82602011-05-22 19:52:37 +03006141}
6142
6143static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6144{
6145 struct nfs4_layoutreturn *lrp = calldata;
6146 struct nfs_server *server;
6147
6148 dprintk("--> %s\n", __func__);
6149
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006150 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03006151 return;
6152
6153 server = NFS_SERVER(lrp->args.inode);
6154 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006155 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006156 return;
6157 }
Benny Halevycbe82602011-05-22 19:52:37 +03006158 dprintk("<-- %s\n", __func__);
6159}
6160
6161static void nfs4_layoutreturn_release(void *calldata)
6162{
6163 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04006164 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006165
6166 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04006167 spin_lock(&lo->plh_inode->i_lock);
6168 if (lrp->res.lrs_present)
6169 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6170 lo->plh_block_lgets--;
6171 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006172 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006173 kfree(calldata);
6174 dprintk("<-- %s\n", __func__);
6175}
6176
6177static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6178 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6179 .rpc_call_done = nfs4_layoutreturn_done,
6180 .rpc_release = nfs4_layoutreturn_release,
6181};
6182
6183int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6184{
6185 struct rpc_task *task;
6186 struct rpc_message msg = {
6187 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6188 .rpc_argp = &lrp->args,
6189 .rpc_resp = &lrp->res,
6190 };
6191 struct rpc_task_setup task_setup_data = {
6192 .rpc_client = lrp->clp->cl_rpcclient,
6193 .rpc_message = &msg,
6194 .callback_ops = &nfs4_layoutreturn_call_ops,
6195 .callback_data = lrp,
6196 };
6197 int status;
6198
6199 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006200 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006201 task = rpc_run_task(&task_setup_data);
6202 if (IS_ERR(task))
6203 return PTR_ERR(task);
6204 status = task->tk_status;
6205 dprintk("<-- %s status=%d\n", __func__, status);
6206 rpc_put_task(task);
6207 return status;
6208}
6209
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006210/*
6211 * Retrieve the list of Data Server devices from the MDS.
6212 */
6213static int _nfs4_getdevicelist(struct nfs_server *server,
6214 const struct nfs_fh *fh,
6215 struct pnfs_devicelist *devlist)
6216{
6217 struct nfs4_getdevicelist_args args = {
6218 .fh = fh,
6219 .layoutclass = server->pnfs_curr_ld->id,
6220 };
6221 struct nfs4_getdevicelist_res res = {
6222 .devlist = devlist,
6223 };
6224 struct rpc_message msg = {
6225 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6226 .rpc_argp = &args,
6227 .rpc_resp = &res,
6228 };
6229 int status;
6230
6231 dprintk("--> %s\n", __func__);
6232 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6233 &res.seq_res, 0);
6234 dprintk("<-- %s status=%d\n", __func__, status);
6235 return status;
6236}
6237
6238int nfs4_proc_getdevicelist(struct nfs_server *server,
6239 const struct nfs_fh *fh,
6240 struct pnfs_devicelist *devlist)
6241{
6242 struct nfs4_exception exception = { };
6243 int err;
6244
6245 do {
6246 err = nfs4_handle_exception(server,
6247 _nfs4_getdevicelist(server, fh, devlist),
6248 &exception);
6249 } while (exception.retry);
6250
6251 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6252 err, devlist->num_devs);
6253
6254 return err;
6255}
6256EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6257
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006258static int
6259_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6260{
6261 struct nfs4_getdeviceinfo_args args = {
6262 .pdev = pdev,
6263 };
6264 struct nfs4_getdeviceinfo_res res = {
6265 .pdev = pdev,
6266 };
6267 struct rpc_message msg = {
6268 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6269 .rpc_argp = &args,
6270 .rpc_resp = &res,
6271 };
6272 int status;
6273
6274 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006275 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006276 dprintk("<-- %s status=%d\n", __func__, status);
6277
6278 return status;
6279}
6280
6281int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6282{
6283 struct nfs4_exception exception = { };
6284 int err;
6285
6286 do {
6287 err = nfs4_handle_exception(server,
6288 _nfs4_proc_getdeviceinfo(server, pdev),
6289 &exception);
6290 } while (exception.retry);
6291 return err;
6292}
6293EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6294
Andy Adamson863a3c62011-03-23 13:27:54 +00006295static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6296{
6297 struct nfs4_layoutcommit_data *data = calldata;
6298 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006299 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00006300
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006301 nfs41_setup_sequence(session,
6302 &data->args.seq_args,
6303 &data->res.seq_res,
6304 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00006305}
6306
6307static void
6308nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6309{
6310 struct nfs4_layoutcommit_data *data = calldata;
6311 struct nfs_server *server = NFS_SERVER(data->args.inode);
6312
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006313 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00006314 return;
6315
6316 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006317 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6318 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6319 case -NFS4ERR_BADLAYOUT: /* no layout */
6320 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006321 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006322 break;
6323 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006324 nfs_post_op_update_inode_force_wcc(data->args.inode,
6325 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006326 break;
6327 default:
6328 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6329 rpc_restart_call_prepare(task);
6330 return;
6331 }
6332 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006333}
6334
6335static void nfs4_layoutcommit_release(void *calldata)
6336{
6337 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04006338 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07006339 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00006340
Andy Adamsondb29c082011-07-30 20:52:38 -04006341 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006342 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04006343 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6344 list_del_init(&lseg->pls_lc_list);
6345 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6346 &lseg->pls_flags))
Trond Myklebust9369a432012-09-18 20:57:08 -04006347 pnfs_put_lseg(lseg);
Peng Taoa9bae562011-07-30 20:52:33 -04006348 }
Peng Tao92407e72011-10-23 20:21:17 -07006349
6350 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6351 smp_mb__after_clear_bit();
6352 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6353
Andy Adamson863a3c62011-03-23 13:27:54 +00006354 put_rpccred(data->cred);
6355 kfree(data);
6356}
6357
6358static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6359 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6360 .rpc_call_done = nfs4_layoutcommit_done,
6361 .rpc_release = nfs4_layoutcommit_release,
6362};
6363
6364int
Andy Adamsonef311532011-03-12 02:58:10 -05006365nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006366{
6367 struct rpc_message msg = {
6368 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6369 .rpc_argp = &data->args,
6370 .rpc_resp = &data->res,
6371 .rpc_cred = data->cred,
6372 };
6373 struct rpc_task_setup task_setup_data = {
6374 .task = &data->task,
6375 .rpc_client = NFS_CLIENT(data->args.inode),
6376 .rpc_message = &msg,
6377 .callback_ops = &nfs4_layoutcommit_ops,
6378 .callback_data = data,
6379 .flags = RPC_TASK_ASYNC,
6380 };
6381 struct rpc_task *task;
6382 int status = 0;
6383
6384 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6385 "lbw: %llu inode %lu\n",
6386 data->task.tk_pid, sync,
6387 data->args.lastbytewritten,
6388 data->args.inode->i_ino);
6389
Trond Myklebust9d12b212012-01-17 22:04:25 -05006390 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006391 task = rpc_run_task(&task_setup_data);
6392 if (IS_ERR(task))
6393 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006394 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006395 goto out;
6396 status = nfs4_wait_for_completion_rpc_task(task);
6397 if (status != 0)
6398 goto out;
6399 status = task->tk_status;
6400out:
6401 dprintk("%s: status %d\n", __func__, status);
6402 rpc_put_task(task);
6403 return status;
6404}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006405
6406static int
6407_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6408 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6409{
6410 struct nfs41_secinfo_no_name_args args = {
6411 .style = SECINFO_STYLE_CURRENT_FH,
6412 };
6413 struct nfs4_secinfo_res res = {
6414 .flavors = flavors,
6415 };
6416 struct rpc_message msg = {
6417 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6418 .rpc_argp = &args,
6419 .rpc_resp = &res,
6420 };
6421 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6422}
6423
6424static int
6425nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6426 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6427{
6428 struct nfs4_exception exception = { };
6429 int err;
6430 do {
6431 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6432 switch (err) {
6433 case 0:
6434 case -NFS4ERR_WRONGSEC:
6435 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006436 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006437 default:
6438 err = nfs4_handle_exception(server, err, &exception);
6439 }
6440 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006441out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006442 return err;
6443}
6444
6445static int
6446nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6447 struct nfs_fsinfo *info)
6448{
6449 int err;
6450 struct page *page;
6451 rpc_authflavor_t flavor;
6452 struct nfs4_secinfo_flavors *flavors;
6453
6454 page = alloc_page(GFP_KERNEL);
6455 if (!page) {
6456 err = -ENOMEM;
6457 goto out;
6458 }
6459
6460 flavors = page_address(page);
6461 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6462
6463 /*
6464 * Fall back on "guess and check" method if
6465 * the server doesn't support SECINFO_NO_NAME
6466 */
6467 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6468 err = nfs4_find_root_sec(server, fhandle, info);
6469 goto out_freepage;
6470 }
6471 if (err)
6472 goto out_freepage;
6473
6474 flavor = nfs_find_best_sec(flavors);
6475 if (err == 0)
6476 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6477
6478out_freepage:
6479 put_page(page);
6480 if (err == -EACCES)
6481 return -EPERM;
6482out:
6483 return err;
6484}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006485
6486static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006487{
6488 int status;
6489 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006490 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006491 };
6492 struct nfs41_test_stateid_res res;
6493 struct rpc_message msg = {
6494 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6495 .rpc_argp = &args,
6496 .rpc_resp = &res,
6497 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006498
Chuck Lever38527b12012-07-11 16:30:23 -04006499 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006500 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006501 nfs4_set_sequence_privileged(&args.seq_args);
6502 status = nfs4_call_sync_sequence(server->client, server, &msg,
6503 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04006504 if (status != NFS_OK) {
6505 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04006506 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04006507 }
6508 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04006509 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006510}
6511
Chuck Lever38527b12012-07-11 16:30:23 -04006512/**
6513 * nfs41_test_stateid - perform a TEST_STATEID operation
6514 *
6515 * @server: server / transport on which to perform the operation
6516 * @stateid: state ID to test
6517 *
6518 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6519 * Otherwise a negative NFS4ERR value is returned if the operation
6520 * failed or the state ID is not currently valid.
6521 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006522static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006523{
6524 struct nfs4_exception exception = { };
6525 int err;
6526 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006527 err = _nfs41_test_stateid(server, stateid);
6528 if (err != -NFS4ERR_DELAY)
6529 break;
6530 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04006531 } while (exception.retry);
6532 return err;
6533}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006534
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006535static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006536{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006537 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006538 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006539 };
6540 struct nfs41_free_stateid_res res;
6541 struct rpc_message msg = {
6542 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6543 .rpc_argp = &args,
6544 .rpc_resp = &res,
6545 };
Chuck Lever38527b12012-07-11 16:30:23 -04006546 int status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006547
Chuck Lever38527b12012-07-11 16:30:23 -04006548 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006549 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006550 nfs4_set_sequence_privileged(&args.seq_args);
Chuck Lever38527b12012-07-11 16:30:23 -04006551 status = nfs4_call_sync_sequence(server->client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006552 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04006553 dprintk("NFS reply free_stateid: %d\n", status);
6554 return status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006555}
6556
Chuck Lever38527b12012-07-11 16:30:23 -04006557/**
6558 * nfs41_free_stateid - perform a FREE_STATEID operation
6559 *
6560 * @server: server / transport on which to perform the operation
6561 * @stateid: state ID to release
6562 *
6563 * Returns NFS_OK if the server freed "stateid". Otherwise a
6564 * negative NFS4ERR value is returned.
6565 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006566static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006567{
6568 struct nfs4_exception exception = { };
6569 int err;
6570 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006571 err = _nfs4_free_stateid(server, stateid);
6572 if (err != -NFS4ERR_DELAY)
6573 break;
6574 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006575 } while (exception.retry);
6576 return err;
6577}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006578
6579static bool nfs41_match_stateid(const nfs4_stateid *s1,
6580 const nfs4_stateid *s2)
6581{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006582 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006583 return false;
6584
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006585 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006586 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006587 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006588 return true;
6589
6590 return false;
6591}
6592
Andy Adamson557134a2009-04-01 09:21:53 -04006593#endif /* CONFIG_NFS_V4_1 */
6594
Trond Myklebust36281ca2012-03-04 18:13:56 -05006595static bool nfs4_match_stateid(const nfs4_stateid *s1,
6596 const nfs4_stateid *s2)
6597{
Trond Myklebustf597c532012-03-04 18:13:56 -05006598 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006599}
6600
6601
Trond Myklebust17280172012-03-11 13:11:00 -04006602static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006603 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006604 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006605 .recover_open = nfs4_open_reclaim,
6606 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006607 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006608 .get_clid_cred = nfs4_get_setclientid_cred,
Chuck Lever05f4c352012-09-14 17:24:32 -04006609 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006610};
6611
Andy Adamson591d71c2009-04-01 09:22:47 -04006612#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006613static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006614 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6615 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6616 .recover_open = nfs4_open_reclaim,
6617 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006618 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006619 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006620 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04006621 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04006622};
6623#endif /* CONFIG_NFS_V4_1 */
6624
Trond Myklebust17280172012-03-11 13:11:00 -04006625static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006626 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006627 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006628 .recover_open = nfs4_open_expired,
6629 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006630 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006631 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006632};
6633
Andy Adamson591d71c2009-04-01 09:22:47 -04006634#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006635static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006636 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6637 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006638 .recover_open = nfs41_open_expired,
6639 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006640 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006641 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006642};
6643#endif /* CONFIG_NFS_V4_1 */
6644
Trond Myklebust17280172012-03-11 13:11:00 -04006645static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006646 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006647 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006648 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006649};
6650
6651#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006652static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006653 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006654 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006655 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006656};
6657#endif
6658
Trond Myklebust97dc1352010-06-16 09:52:26 -04006659static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6660 .minor_version = 0,
6661 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006662 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006663 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006664 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6665 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6666 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006667};
6668
6669#if defined(CONFIG_NFS_V4_1)
6670static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6671 .minor_version = 1,
Bryan Schumaker104287c2012-11-12 14:13:13 -05006672 .call_sync = nfs4_call_sync_sequence,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006673 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006674 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006675 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6676 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6677 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006678};
6679#endif
6680
Trond Myklebust97dc1352010-06-16 09:52:26 -04006681const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6682 [0] = &nfs_v4_0_minor_ops,
6683#if defined(CONFIG_NFS_V4_1)
6684 [1] = &nfs_v4_1_minor_ops,
6685#endif
6686};
6687
Bryan Schumaker73a79702012-07-16 16:39:12 -04006688const struct inode_operations nfs4_dir_inode_operations = {
6689 .create = nfs_create,
6690 .lookup = nfs_lookup,
6691 .atomic_open = nfs_atomic_open,
6692 .link = nfs_link,
6693 .unlink = nfs_unlink,
6694 .symlink = nfs_symlink,
6695 .mkdir = nfs_mkdir,
6696 .rmdir = nfs_rmdir,
6697 .mknod = nfs_mknod,
6698 .rename = nfs_rename,
6699 .permission = nfs_permission,
6700 .getattr = nfs_getattr,
6701 .setattr = nfs_setattr,
6702 .getxattr = generic_getxattr,
6703 .setxattr = generic_setxattr,
6704 .listxattr = generic_listxattr,
6705 .removexattr = generic_removexattr,
6706};
6707
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006708static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006709 .permission = nfs_permission,
6710 .getattr = nfs_getattr,
6711 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006712 .getxattr = generic_getxattr,
6713 .setxattr = generic_setxattr,
6714 .listxattr = generic_listxattr,
6715 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006716};
6717
David Howells509de812006-08-22 20:06:11 -04006718const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006719 .version = 4, /* protocol version */
6720 .dentry_ops = &nfs4_dentry_operations,
6721 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006722 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006723 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006724 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04006725 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04006726 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006727 .getattr = nfs4_proc_getattr,
6728 .setattr = nfs4_proc_setattr,
6729 .lookup = nfs4_proc_lookup,
6730 .access = nfs4_proc_access,
6731 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006732 .create = nfs4_proc_create,
6733 .remove = nfs4_proc_remove,
6734 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04006735 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006736 .unlink_done = nfs4_proc_unlink_done,
6737 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006738 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04006739 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04006740 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006741 .link = nfs4_proc_link,
6742 .symlink = nfs4_proc_symlink,
6743 .mkdir = nfs4_proc_mkdir,
6744 .rmdir = nfs4_proc_remove,
6745 .readdir = nfs4_proc_readdir,
6746 .mknod = nfs4_proc_mknod,
6747 .statfs = nfs4_proc_statfs,
6748 .fsinfo = nfs4_proc_fsinfo,
6749 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006750 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006751 .decode_dirent = nfs4_decode_dirent,
6752 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04006753 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04006754 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05006755 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006756 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04006757 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04006758 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006759 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006760 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04006761 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006762 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006763 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006764 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006765 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006766 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04006767 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04006768 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04006769 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00006770 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04006771 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04006772 .create_server = nfs4_create_server,
6773 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006774};
6775
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006776static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6777 .prefix = XATTR_NAME_NFSV4_ACL,
6778 .list = nfs4_xattr_list_nfs4_acl,
6779 .get = nfs4_xattr_get_nfs4_acl,
6780 .set = nfs4_xattr_set_nfs4_acl,
6781};
6782
6783const struct xattr_handler *nfs4_xattr_handlers[] = {
6784 &nfs4_xattr_nfs4_acl_handler,
6785 NULL
6786};
6787
Linus Torvalds1da177e2005-04-16 15:20:36 -07006788/*
6789 * Local variables:
6790 * c-basic-offset: 8
6791 * End:
6792 */