blob: ecd4ed3a4f6537afbea7edc9ecebba3f54c97517 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050042#include <linux/ratelimit.h>
43#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sunrpc/clnt.h>
46#include <linux/nfs.h>
47#include <linux/nfs4.h>
48#include <linux/nfs_fs.h>
49#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040050#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070052#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040053#include <linux/module.h>
Trond Myklebust6926afd12012-01-07 13:22:46 -050054#include <linux/nfs_idmap.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000055#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050056#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010057#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Trond Myklebust4ce79712005-06-22 17:16:21 +000059#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050061#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050062#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040063#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040064#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040065#include "netns.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050066#include "nfs4session.h"
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69#define NFSDBG_FACILITY NFSDBG_PROC
70
Trond Myklebust2066fe82006-09-15 08:30:46 -040071#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define NFS4_POLL_RETRY_MAX (15*HZ)
73
Trond Myklebustcdd4e682006-01-03 09:55:12 +010074struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010075static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080076static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050078static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Chuck Lever81934dd2012-03-01 17:01:57 -050079static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Bryan Schumakerbae36242012-05-10 15:07:31 -040080static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *);
Trond Myklebust99367812007-07-17 21:52:41 -040081static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040082static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
83 struct nfs_fattr *fattr, struct iattr *sattr,
84 struct nfs4_state *state);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040085#ifdef CONFIG_NFS_V4_1
Bryan Schumaker1cab0652012-01-31 10:39:29 -050086static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *);
87static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040088#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070089/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050090static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Trond Myklebust52567b02009-10-23 14:46:42 -040092 if (err >= -1000)
93 return err;
94 switch (err) {
95 case -NFS4ERR_RESOURCE:
96 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +000097 case -NFS4ERR_WRONGSEC:
98 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -080099 case -NFS4ERR_BADOWNER:
100 case -NFS4ERR_BADNAME:
101 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400102 case -NFS4ERR_SHARE_DENIED:
103 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400104 case -NFS4ERR_MINOR_VERS_MISMATCH:
105 return -EPROTONOSUPPORT;
Weston Andros Adamson6168f622012-09-10 14:00:46 -0400106 case -NFS4ERR_ACCESS:
107 return -EACCES;
Trond Myklebust52567b02009-10-23 14:46:42 -0400108 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700110 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400111 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400113 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114}
115
116/*
117 * This is our standard bitmap for GETATTR requests.
118 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400119const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 FATTR4_WORD0_TYPE
121 | FATTR4_WORD0_CHANGE
122 | FATTR4_WORD0_SIZE
123 | FATTR4_WORD0_FSID
124 | FATTR4_WORD0_FILEID,
125 FATTR4_WORD1_MODE
126 | FATTR4_WORD1_NUMLINKS
127 | FATTR4_WORD1_OWNER
128 | FATTR4_WORD1_OWNER_GROUP
129 | FATTR4_WORD1_RAWDEV
130 | FATTR4_WORD1_SPACE_USED
131 | FATTR4_WORD1_TIME_ACCESS
132 | FATTR4_WORD1_TIME_METADATA
133 | FATTR4_WORD1_TIME_MODIFY
134};
135
Trond Myklebust1549210f2012-06-05 09:16:47 -0400136static const u32 nfs4_pnfs_open_bitmap[3] = {
137 FATTR4_WORD0_TYPE
138 | FATTR4_WORD0_CHANGE
139 | FATTR4_WORD0_SIZE
140 | FATTR4_WORD0_FSID
141 | FATTR4_WORD0_FILEID,
142 FATTR4_WORD1_MODE
143 | FATTR4_WORD1_NUMLINKS
144 | FATTR4_WORD1_OWNER
145 | FATTR4_WORD1_OWNER_GROUP
146 | FATTR4_WORD1_RAWDEV
147 | FATTR4_WORD1_SPACE_USED
148 | FATTR4_WORD1_TIME_ACCESS
149 | FATTR4_WORD1_TIME_METADATA
150 | FATTR4_WORD1_TIME_MODIFY,
151 FATTR4_WORD2_MDSTHRESHOLD
152};
153
Andy Adamsone23008e2012-10-02 21:07:32 -0400154static const u32 nfs4_open_noattr_bitmap[3] = {
155 FATTR4_WORD0_TYPE
156 | FATTR4_WORD0_CHANGE
157 | FATTR4_WORD0_FILEID,
158};
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160const u32 nfs4_statfs_bitmap[2] = {
161 FATTR4_WORD0_FILES_AVAIL
162 | FATTR4_WORD0_FILES_FREE
163 | FATTR4_WORD0_FILES_TOTAL,
164 FATTR4_WORD1_SPACE_AVAIL
165 | FATTR4_WORD1_SPACE_FREE
166 | FATTR4_WORD1_SPACE_TOTAL
167};
168
Trond Myklebust4ce79712005-06-22 17:16:21 +0000169const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 FATTR4_WORD0_MAXLINK
171 | FATTR4_WORD0_MAXNAME,
172 0
173};
174
Fred Isamandae100c2011-07-30 20:52:37 -0400175const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 | FATTR4_WORD0_MAXREAD
177 | FATTR4_WORD0_MAXWRITE
178 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700179 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400180 | FATTR4_WORD1_FS_LAYOUT_TYPES,
181 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182};
183
Manoj Naik830b8e32006-06-09 09:34:25 -0400184const u32 nfs4_fs_locations_bitmap[2] = {
185 FATTR4_WORD0_TYPE
186 | FATTR4_WORD0_CHANGE
187 | FATTR4_WORD0_SIZE
188 | FATTR4_WORD0_FSID
189 | FATTR4_WORD0_FILEID
190 | FATTR4_WORD0_FS_LOCATIONS,
191 FATTR4_WORD1_MODE
192 | FATTR4_WORD1_NUMLINKS
193 | FATTR4_WORD1_OWNER
194 | FATTR4_WORD1_OWNER_GROUP
195 | FATTR4_WORD1_RAWDEV
196 | FATTR4_WORD1_SPACE_USED
197 | FATTR4_WORD1_TIME_ACCESS
198 | FATTR4_WORD1_TIME_METADATA
199 | FATTR4_WORD1_TIME_MODIFY
200 | FATTR4_WORD1_MOUNTED_ON_FILEID
201};
202
Al Virobc4785c2006-10-19 23:28:51 -0700203static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 struct nfs4_readdir_arg *readdir)
205{
Al Viro0dbb4c62006-10-19 23:28:49 -0700206 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000209 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
211 return;
212 }
213
214 readdir->cookie = 0;
215 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
216 if (cookie == 2)
217 return;
218
219 /*
220 * NFSv4 servers do not return entries for '.' and '..'
221 * Therefore, we fake these entries here. We let '.'
222 * have cookie 0 and '..' have cookie 1. Note that
223 * when talking to the server, we always send cookie 0
224 * instead of 1 or 2.
225 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800226 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 if (cookie == 0) {
229 *p++ = xdr_one; /* next */
230 *p++ = xdr_zero; /* cookie, first word */
231 *p++ = xdr_one; /* cookie, second word */
232 *p++ = xdr_one; /* entry len */
233 memcpy(p, ".\0\0\0", 4); /* entry */
234 p++;
235 *p++ = xdr_one; /* bitmap length */
236 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
237 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400238 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 }
240
241 *p++ = xdr_one; /* next */
242 *p++ = xdr_zero; /* cookie, first word */
243 *p++ = xdr_two; /* cookie, second word */
244 *p++ = xdr_two; /* entry len */
245 memcpy(p, "..\0\0", 4); /* entry */
246 p++;
247 *p++ = xdr_one; /* bitmap length */
248 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
249 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400250 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 readdir->pgbase = (char *)p - (char *)start;
253 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800254 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255}
256
Trond Myklebust65de8722008-12-23 15:21:44 -0500257static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
258{
259 int res = 0;
260
261 might_sleep();
262
263 if (*timeout <= 0)
264 *timeout = NFS4_POLL_RETRY_MIN;
265 if (*timeout > NFS4_POLL_RETRY_MAX)
266 *timeout = NFS4_POLL_RETRY_MAX;
Jeff Laytond3103102011-12-01 22:44:39 +0100267 freezable_schedule_timeout_killable(*timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500268 if (fatal_signal_pending(current))
269 res = -ERESTARTSYS;
270 *timeout <<= 1;
271 return res;
272}
273
274/* This is the error handling routine for processes that are allowed
275 * to sleep.
276 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800277static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500278{
279 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500280 struct nfs4_state *state = exception->state;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500281 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500282 int ret = errorcode;
283
284 exception->retry = 0;
285 switch(errorcode) {
286 case 0:
287 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500288 case -NFS4ERR_OPENMODE:
Bryan Schumaker011e2a72012-06-20 15:53:43 -0400289 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400290 nfs4_inode_return_delegation(inode);
Trond Myklebust3114ea72012-03-07 16:39:06 -0500291 exception->retry = 1;
292 return 0;
293 }
294 if (state == NULL)
295 break;
296 nfs4_schedule_stateid_recovery(server, state);
297 goto wait_on_recovery;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500298 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500299 case -NFS4ERR_ADMIN_REVOKED:
300 case -NFS4ERR_BAD_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500301 if (state == NULL)
302 break;
Trond Myklebust14977482012-03-27 18:31:25 -0400303 nfs_remove_bad_delegation(state->inode);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500304 nfs4_schedule_stateid_recovery(server, state);
305 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400306 case -NFS4ERR_EXPIRED:
307 if (state != NULL)
308 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust65de8722008-12-23 15:21:44 -0500309 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500310 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500311 nfs4_schedule_lease_recovery(clp);
312 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500313#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400314 case -NFS4ERR_BADSESSION:
315 case -NFS4ERR_BADSLOT:
316 case -NFS4ERR_BAD_HIGH_SLOT:
317 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
318 case -NFS4ERR_DEADSESSION:
319 case -NFS4ERR_SEQ_FALSE_RETRY:
320 case -NFS4ERR_SEQ_MISORDERED:
321 dprintk("%s ERROR: %d Reset session\n", __func__,
322 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400323 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400324 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500325#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500326 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500327 if (exception->timeout > HZ) {
328 /* We have retried a decent amount, time to
329 * fail
330 */
331 ret = -EBUSY;
332 break;
333 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500334 case -NFS4ERR_GRACE:
335 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -0500336 case -EKEYEXPIRED:
Trond Myklebust65de8722008-12-23 15:21:44 -0500337 ret = nfs4_delay(server->client, &exception->timeout);
338 if (ret != 0)
339 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400340 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500341 case -NFS4ERR_OLD_STATEID:
342 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800343 break;
344 case -NFS4ERR_BADOWNER:
345 /* The following works around a Linux server bug! */
346 case -NFS4ERR_BADNAME:
347 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
348 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
349 exception->retry = 1;
350 printk(KERN_WARNING "NFS: v4 server %s "
351 "does not accept raw "
352 "uid/gids. "
353 "Reenabling the idmapper.\n",
354 server->nfs_client->cl_hostname);
355 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500356 }
357 /* We failed to handle the error */
358 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500359wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500360 ret = nfs4_wait_clnt_recover(clp);
361 if (ret == 0)
362 exception->retry = 1;
363 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500364}
365
366
Trond Myklebust452e9352010-07-31 14:29:06 -0400367static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 spin_lock(&clp->cl_lock);
370 if (time_before(clp->cl_last_renewal,timestamp))
371 clp->cl_last_renewal = timestamp;
372 spin_unlock(&clp->cl_lock);
373}
374
Trond Myklebust452e9352010-07-31 14:29:06 -0400375static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
376{
377 do_renew_lease(server->nfs_client, timestamp);
378}
379
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400380#if defined(CONFIG_NFS_V4_1)
381
Trond Myklebust961a8282012-01-17 22:57:37 -0500382bool nfs4_set_task_privileged(struct rpc_task *task, void *dummy)
383{
384 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
385 return true;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400386}
387
Trond Myklebustd185a332010-06-16 09:52:25 -0400388static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400389{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500390 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400391 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500392 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400393
Benny Halevydfb4f3092010-09-24 09:17:01 -0400394 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400395 /* just wake up the next guy waiting since
396 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500397 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500398 return;
Andy Adamson13615872009-04-01 09:22:17 -0400399 }
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500400 tbl = res->sr_slot->table;
401 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500402
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500403 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500404 /* Be nice to the server: try to ensure that the last transmitted
405 * value for highest_user_slotid <= target_highest_slotid
406 */
407 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
408 send_new_highest_used_slotid = true;
409
Trond Myklebustf4af6e2a2012-11-20 14:17:32 -0500410 nfs4_free_slot(tbl, res->sr_slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500411
412 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
413 send_new_highest_used_slotid = false;
414 if (!nfs4_session_draining(session)) {
415 if (rpc_wake_up_first(&tbl->slot_tbl_waitq,
416 nfs4_set_task_privileged, NULL) != NULL)
417 send_new_highest_used_slotid = false;
418 }
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500419 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400420 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500421 if (send_new_highest_used_slotid)
422 nfs41_server_notify_highest_slotid_update(session->clp);
Andy Adamson13615872009-04-01 09:22:17 -0400423}
424
Trond Myklebust14516c32010-07-31 14:29:06 -0400425static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400426{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500427 struct nfs4_session *session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500428 struct nfs4_slot *slot;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400429 unsigned long timestamp;
Trond Myklebust14516c32010-07-31 14:29:06 -0400430 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400431
432 /*
433 * sr_status remains 1 if an RPC level error occurred. The server
434 * may or may not have processed the sequence operation..
435 * Proceed as if the server received and processed the sequence
436 * operation.
437 */
438 if (res->sr_status == 1)
439 res->sr_status = NFS_OK;
440
Bryan Schumaker468f8612011-04-18 15:57:32 -0400441 /* don't increment the sequence number if the task wasn't sent */
442 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400443 goto out;
444
Trond Myklebust933602e2012-11-16 12:12:38 -0500445 slot = res->sr_slot;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500446 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500447
Andy Adamson691daf32009-12-04 15:55:39 -0500448 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400449 switch (res->sr_status) {
450 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400451 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust933602e2012-11-16 12:12:38 -0500452 ++slot->seq_nr;
453 timestamp = slot->renewal_time;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500454 clp = session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400455 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500456 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500457 if (res->sr_status_flags != 0)
458 nfs4_schedule_lease_recovery(clp);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500459 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400460 break;
461 case -NFS4ERR_DELAY:
462 /* The server detected a resend of the RPC call and
463 * returned NFS4ERR_DELAY as per Section 2.10.6.2
464 * of RFC5661.
465 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500466 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400467 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500468 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500469 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400470 goto out_retry;
471 default:
472 /* Just update the slot sequence no. */
Trond Myklebust933602e2012-11-16 12:12:38 -0500473 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400474 }
475out:
476 /* The session may be reset by one of the error handlers. */
477 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400478 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400479 return 1;
480out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400481 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400482 goto out;
483 rpc_delay(task, NFS4_POLL_RETRY_MAX);
484 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400485}
486
Trond Myklebust14516c32010-07-31 14:29:06 -0400487static int nfs4_sequence_done(struct rpc_task *task,
488 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400489{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500490 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400491 return 1;
492 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400493}
494
Trond Myklebust9d12b212012-01-17 22:04:25 -0500495static void nfs41_init_sequence(struct nfs4_sequence_args *args,
496 struct nfs4_sequence_res *res, int cache_reply)
497{
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500498 args->sa_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500499 args->sa_cache_this = 0;
500 if (cache_reply)
501 args->sa_cache_this = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500502 res->sr_slot = NULL;
503}
504
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000505int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400506 struct nfs4_sequence_args *args,
507 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400508 struct rpc_task *task)
509{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400510 struct nfs4_slot *slot;
511 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400512
513 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400514 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400515 if (res->sr_slot != NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400516 return 0;
517
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400518 tbl = &session->fc_slot_table;
519
Trond Myklebust69d206b2012-11-22 13:21:02 -0500520 task->tk_timeout = 0;
521
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400522 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400523 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800524 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500525 /* The state manager will wait until the slot table is empty */
Andy Adamson05f0d232009-12-04 15:55:37 -0500526 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400527 spin_unlock(&tbl->slot_tbl_lock);
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500528 dprintk("%s session is draining\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500529 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400530 }
531
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800532 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
533 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
534 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
535 spin_unlock(&tbl->slot_tbl_lock);
536 dprintk("%s enforce FIFO order\n", __func__);
537 return -EAGAIN;
538 }
539
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500540 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500541 if (IS_ERR(slot)) {
542 /* If out of memory, try again in 1/4 second */
543 if (slot == ERR_PTR(-ENOMEM))
544 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400545 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
546 spin_unlock(&tbl->slot_tbl_lock);
547 dprintk("<-- %s: no free slots\n", __func__);
548 return -EAGAIN;
549 }
550 spin_unlock(&tbl->slot_tbl_lock);
551
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800552 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500553
554 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400555
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500556 dprintk("<-- %s slotid=%d seqid=%d\n", __func__,
557 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400558
Benny Halevydfb4f3092010-09-24 09:17:01 -0400559 res->sr_slot = slot;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400560 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400561 /*
562 * sr_status is only set in decode_sequence, and so will remain
563 * set to 1 if an rpc level failure occurs.
564 */
565 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400566 return 0;
567}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000568EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400569
Trond Myklebust035168ab2010-06-16 09:52:26 -0400570int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400571 struct nfs4_sequence_args *args,
572 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400573 struct rpc_task *task)
574{
Trond Myklebust035168ab2010-06-16 09:52:26 -0400575 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400576 int ret = 0;
577
Trond Myklebust9d12b212012-01-17 22:04:25 -0500578 if (session == NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400579 goto out;
Trond Myklebust035168ab2010-06-16 09:52:26 -0400580
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500581 dprintk("--> %s clp %p session %p sr_slot %d\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400582 __func__, session->clp, session, res->sr_slot ?
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500583 res->sr_slot->slot_nr : -1);
Trond Myklebust035168ab2010-06-16 09:52:26 -0400584
Trond Myklebust9d12b212012-01-17 22:04:25 -0500585 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400586out:
587 dprintk("<-- %s status=%d\n", __func__, ret);
588 return ret;
589}
590
591struct nfs41_call_sync_data {
Trond Myklebust035168ab2010-06-16 09:52:26 -0400592 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400593 struct nfs4_sequence_args *seq_args;
594 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400595};
596
597static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
598{
599 struct nfs41_call_sync_data *data = calldata;
600
Trond Myklebust035168ab2010-06-16 09:52:26 -0400601 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
602
603 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -0500604 data->seq_res, task))
Andy Adamsonce5039c2009-04-01 09:22:13 -0400605 return;
606 rpc_call_start(task);
607}
608
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800609static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
610{
611 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
612 nfs41_call_sync_prepare(task, calldata);
613}
614
Andy Adamson69ab40c2009-04-01 09:22:19 -0400615static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
616{
617 struct nfs41_call_sync_data *data = calldata;
618
Trond Myklebust14516c32010-07-31 14:29:06 -0400619 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400620}
621
Trond Myklebust17280172012-03-11 13:11:00 -0400622static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400623 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400624 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400625};
626
Trond Myklebust17280172012-03-11 13:11:00 -0400627static const struct rpc_call_ops nfs41_call_priv_sync_ops = {
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800628 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
629 .rpc_call_done = nfs41_call_sync_done,
630};
631
Bryan Schumaker7c513052011-03-24 17:12:24 +0000632static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
633 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400634 struct rpc_message *msg,
635 struct nfs4_sequence_args *args,
636 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800637 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400638{
639 int ret;
640 struct rpc_task *task;
641 struct nfs41_call_sync_data data = {
Trond Myklebust035168ab2010-06-16 09:52:26 -0400642 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400643 .seq_args = args,
644 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400645 };
646 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000647 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400648 .rpc_message = msg,
649 .callback_ops = &nfs41_call_sync_ops,
650 .callback_data = &data
651 };
652
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800653 if (privileged)
654 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400655 task = rpc_run_task(&task_setup);
656 if (IS_ERR(task))
657 ret = PTR_ERR(task);
658 else {
659 ret = task->tk_status;
660 rpc_put_task(task);
661 }
662 return ret;
663}
664
Bryan Schumaker7c513052011-03-24 17:12:24 +0000665int _nfs4_call_sync_session(struct rpc_clnt *clnt,
666 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400667 struct rpc_message *msg,
668 struct nfs4_sequence_args *args,
669 struct nfs4_sequence_res *res,
670 int cache_reply)
671{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500672 nfs41_init_sequence(args, res, cache_reply);
673 return nfs4_call_sync_sequence(clnt, server, msg, args, res, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400674}
675
Trond Myklebustdf896452010-06-16 09:52:26 -0400676#else
Trond Myklebust9d12b212012-01-17 22:04:25 -0500677static inline
678void nfs41_init_sequence(struct nfs4_sequence_args *args,
679 struct nfs4_sequence_res *res, int cache_reply)
680{
681}
682
Trond Myklebust14516c32010-07-31 14:29:06 -0400683static int nfs4_sequence_done(struct rpc_task *task,
684 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400685{
Trond Myklebust14516c32010-07-31 14:29:06 -0400686 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400687}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400688#endif /* CONFIG_NFS_V4_1 */
689
Bryan Schumaker7c513052011-03-24 17:12:24 +0000690int _nfs4_call_sync(struct rpc_clnt *clnt,
691 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400692 struct rpc_message *msg,
693 struct nfs4_sequence_args *args,
694 struct nfs4_sequence_res *res,
695 int cache_reply)
696{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500697 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000698 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400699}
700
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000701static inline
Bryan Schumaker7c513052011-03-24 17:12:24 +0000702int nfs4_call_sync(struct rpc_clnt *clnt,
703 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000704 struct rpc_message *msg,
705 struct nfs4_sequence_args *args,
706 struct nfs4_sequence_res *res,
707 int cache_reply)
708{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000709 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
710 args, res, cache_reply);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000711}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400712
Trond Myklebust38478b22006-05-25 01:40:57 -0400713static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
Trond Myklebust38478b22006-05-25 01:40:57 -0400715 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Trond Myklebust38478b22006-05-25 01:40:57 -0400717 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400718 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700719 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400720 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700721 dir->i_version = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400722 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723}
724
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100725struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400726 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100727 struct nfs_openargs o_arg;
728 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100729 struct nfs_open_confirmargs c_arg;
730 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd12012-01-07 13:22:46 -0500731 struct nfs4_string owner_name;
732 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100733 struct nfs_fattr f_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100734 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400735 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100736 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400737 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100738 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100739 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400740 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100741 int rpc_status;
742 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100743};
744
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400745
746static void nfs4_init_opendata_res(struct nfs4_opendata *p)
747{
748 p->o_res.f_attr = &p->f_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400749 p->o_res.seqid = p->o_arg.seqid;
750 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400751 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400752 p->o_res.access_request = p->o_arg.access;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400753 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd12012-01-07 13:22:46 -0500754 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400755}
756
Al Viro82a2c1b2011-06-22 18:30:55 -0400757static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500758 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400759 const struct iattr *attrs,
760 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100761{
Al Viro82a2c1b2011-06-22 18:30:55 -0400762 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100763 struct inode *dir = parent->d_inode;
764 struct nfs_server *server = NFS_SERVER(dir);
765 struct nfs4_opendata *p;
766
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400767 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100768 if (p == NULL)
769 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400770 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100771 if (p->o_arg.seqid == NULL)
772 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400773 nfs_sb_active(dentry->d_sb);
774 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100775 p->dir = parent;
776 p->owner = sp;
777 atomic_inc(&sp->so_count);
778 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500779 p->o_arg.open_flags = flags;
780 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700781 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
782 * will return permission denied for all bits until close */
783 if (!(flags & O_EXCL)) {
784 /* ask server to check for all possible rights as results
785 * are cached */
786 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
787 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700788 }
David Howells7539bba2006-08-22 20:06:09 -0400789 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400790 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
791 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400792 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100793 p->o_arg.server = server;
794 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust1549210f2012-06-05 09:16:47 -0400795 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100796 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust536e43d2012-01-17 22:04:26 -0500797 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -0500798 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400799
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100800 p->o_arg.u.attrs = &p->attrs;
801 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500802
803 verf[0] = jiffies;
804 verf[1] = current->pid;
805 memcpy(p->o_arg.u.verifier.data, verf,
806 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100807 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100808 p->c_arg.fh = &p->o_res.fh;
809 p->c_arg.stateid = &p->o_res.stateid;
810 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400811 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400812 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100813 return p;
814err_free:
815 kfree(p);
816err:
817 dput(parent);
818 return NULL;
819}
820
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400821static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100822{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400823 struct nfs4_opendata *p = container_of(kref,
824 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400825 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400826
827 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400828 if (p->state != NULL)
829 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400830 nfs4_put_state_owner(p->owner);
831 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400832 dput(p->dentry);
833 nfs_sb_deactive(sb);
Trond Myklebust6926afd12012-01-07 13:22:46 -0500834 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400835 kfree(p);
836}
837
838static void nfs4_opendata_put(struct nfs4_opendata *p)
839{
840 if (p != NULL)
841 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100842}
843
Trond Myklebust06f814a2006-01-03 09:55:07 +0100844static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
845{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100846 int ret;
847
Trond Myklebust06f814a2006-01-03 09:55:07 +0100848 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100849 return ret;
850}
851
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500852static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400853{
854 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500855
Trond Myklebust536e43d2012-01-17 22:04:26 -0500856 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500857 goto out;
858 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400859 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500860 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
861 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400862 break;
863 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500864 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
865 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400866 break;
867 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500868 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
869 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400870 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500871out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400872 return ret;
873}
874
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500875static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400876{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500877 if (delegation == NULL)
878 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500879 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400880 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500881 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400882 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500883 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400884 return 1;
885}
886
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500887static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100888{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500889 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100890 case FMODE_WRITE:
891 state->n_wronly++;
892 break;
893 case FMODE_READ:
894 state->n_rdonly++;
895 break;
896 case FMODE_READ|FMODE_WRITE:
897 state->n_rdwr++;
898 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500899 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100900}
901
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500902static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400903{
904 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -0500905 nfs4_stateid_copy(&state->stateid, stateid);
906 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500907 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400908 case FMODE_READ:
909 set_bit(NFS_O_RDONLY_STATE, &state->flags);
910 break;
911 case FMODE_WRITE:
912 set_bit(NFS_O_WRONLY_STATE, &state->flags);
913 break;
914 case FMODE_READ|FMODE_WRITE:
915 set_bit(NFS_O_RDWR_STATE, &state->flags);
916 }
917}
918
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500919static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400920{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400921 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500922 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400923 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400924}
925
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500926static 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 -0700927{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400928 /*
929 * Protect the call to nfs4_state_set_mode_locked and
930 * serialise the stateid update
931 */
932 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400933 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -0500934 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -0400935 set_bit(NFS_DELEGATED_STATE, &state->flags);
936 }
937 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500938 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400939 write_sequnlock(&state->seqlock);
940 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500941 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700942 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943}
944
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500945static 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 -0500946{
947 struct nfs_inode *nfsi = NFS_I(state->inode);
948 struct nfs_delegation *deleg_cur;
949 int ret = 0;
950
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500951 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500952
953 rcu_read_lock();
954 deleg_cur = rcu_dereference(nfsi->delegation);
955 if (deleg_cur == NULL)
956 goto no_delegation;
957
958 spin_lock(&deleg_cur->lock);
959 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500960 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500961 goto no_delegation_unlock;
962
963 if (delegation == NULL)
964 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -0500965 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -0500966 goto no_delegation_unlock;
967
Trond Myklebustb7391f42008-12-23 15:21:52 -0500968 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500969 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500970 ret = 1;
971no_delegation_unlock:
972 spin_unlock(&deleg_cur->lock);
973no_delegation:
974 rcu_read_unlock();
975
976 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500977 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500978 ret = 1;
979 }
980
981 return ret;
982}
983
984
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500985static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400986{
987 struct nfs_delegation *delegation;
988
989 rcu_read_lock();
990 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500991 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -0400992 rcu_read_unlock();
993 return;
994 }
995 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400996 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400997}
998
Trond Myklebust6ee41262007-07-08 14:11:36 -0400999static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001000{
1001 struct nfs4_state *state = opendata->state;
1002 struct nfs_inode *nfsi = NFS_I(state->inode);
1003 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -05001004 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001005 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001006 nfs4_stateid stateid;
1007 int ret = -EAGAIN;
1008
Trond Myklebustaac00a82007-07-05 19:02:21 -04001009 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001010 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001011 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001012 if (can_open_cached(state, fmode, open_mode)) {
1013 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001014 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001015 goto out_return_state;
1016 }
1017 spin_unlock(&state->owner->so_lock);
1018 }
Trond Myklebust34310432008-12-23 15:21:38 -05001019 rcu_read_lock();
1020 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001021 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001022 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001023 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001024 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001025 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001026 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001027 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001028 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001029 if (ret != 0)
1030 goto out;
1031 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001032
1033 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001034 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001035 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001036 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001037out:
1038 return ERR_PTR(ret);
1039out_return_state:
1040 atomic_inc(&state->count);
1041 return state;
1042}
1043
Andy Adamsone23008e2012-10-02 21:07:32 -04001044static void
1045nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1046{
1047 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1048 struct nfs_delegation *delegation;
1049 int delegation_flags = 0;
1050
1051 rcu_read_lock();
1052 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1053 if (delegation)
1054 delegation_flags = delegation->flags;
1055 rcu_read_unlock();
1056 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1057 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1058 "returning a delegation for "
1059 "OPEN(CLAIM_DELEGATE_CUR)\n",
1060 clp->cl_hostname);
1061 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1062 nfs_inode_set_delegation(state->inode,
1063 data->owner->so_cred,
1064 &data->o_res);
1065 else
1066 nfs_inode_reclaim_delegation(state->inode,
1067 data->owner->so_cred,
1068 &data->o_res);
1069}
1070
1071/*
1072 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1073 * and update the nfs4_state.
1074 */
1075static struct nfs4_state *
1076_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1077{
1078 struct inode *inode = data->state->inode;
1079 struct nfs4_state *state = data->state;
1080 int ret;
1081
1082 if (!data->rpc_done) {
1083 ret = data->rpc_status;
1084 goto err;
1085 }
1086
1087 ret = -ESTALE;
1088 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1089 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1090 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1091 goto err;
1092
1093 ret = -ENOMEM;
1094 state = nfs4_get_open_state(inode, data->owner);
1095 if (state == NULL)
1096 goto err;
1097
1098 ret = nfs_refresh_inode(inode, &data->f_attr);
1099 if (ret)
1100 goto err;
1101
1102 if (data->o_res.delegation_type != 0)
1103 nfs4_opendata_check_deleg(data, state);
1104 update_open_stateid(state, &data->o_res.stateid, NULL,
1105 data->o_arg.fmode);
1106
1107 return state;
1108err:
1109 return ERR_PTR(ret);
1110
1111}
1112
1113static struct nfs4_state *
1114_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001115{
1116 struct inode *inode;
1117 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001118 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001119
Trond Myklebustaac00a82007-07-05 19:02:21 -04001120 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001121 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001122 goto out;
1123 }
1124
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001125 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001126 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001127 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001128 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001129 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001130 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001131 goto err;
1132 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001133 state = nfs4_get_open_state(inode, data->owner);
1134 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001135 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001136 if (data->o_res.delegation_type != 0)
1137 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001138 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001139 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001140 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001141out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001142 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001143err_put_inode:
1144 iput(inode);
1145err:
1146 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001147}
1148
Andy Adamsone23008e2012-10-02 21:07:32 -04001149static struct nfs4_state *
1150nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1151{
1152 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1153 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1154 return _nfs4_opendata_to_nfs4_state(data);
1155}
1156
Trond Myklebust864472e2006-01-03 09:55:15 +01001157static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1158{
1159 struct nfs_inode *nfsi = NFS_I(state->inode);
1160 struct nfs_open_context *ctx;
1161
1162 spin_lock(&state->inode->i_lock);
1163 list_for_each_entry(ctx, &nfsi->open_files, list) {
1164 if (ctx->state != state)
1165 continue;
1166 get_nfs_open_context(ctx);
1167 spin_unlock(&state->inode->i_lock);
1168 return ctx;
1169 }
1170 spin_unlock(&state->inode->i_lock);
1171 return ERR_PTR(-ENOENT);
1172}
1173
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001174static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1175{
1176 struct nfs4_opendata *opendata;
1177
Al Viro3d4ff432011-06-22 18:40:12 -04001178 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001179 if (opendata == NULL)
1180 return ERR_PTR(-ENOMEM);
1181 opendata->state = state;
1182 atomic_inc(&state->count);
1183 return opendata;
1184}
1185
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001186static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001187{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001188 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001189 int ret;
1190
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001191 opendata->o_arg.open_flags = 0;
1192 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001193 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1194 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1195 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001196 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001197 if (ret != 0)
1198 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001199 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001200 if (IS_ERR(newstate))
1201 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001202 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001203 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001204 return 0;
1205}
1206
1207static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1208{
Trond Myklebust864472e2006-01-03 09:55:15 +01001209 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001210 int ret;
1211
1212 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001213 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001214 smp_rmb();
1215 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001216 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001217 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001218 if (ret != 0)
1219 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001220 if (newstate != state)
1221 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001222 }
1223 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001224 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001225 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001226 if (ret != 0)
1227 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001228 if (newstate != state)
1229 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001230 }
1231 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001232 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001233 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001234 if (ret != 0)
1235 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001236 if (newstate != state)
1237 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001238 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001239 /*
1240 * We may have performed cached opens for all three recoveries.
1241 * Check if we need to update the current stateid.
1242 */
1243 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001244 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001245 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001246 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001247 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001248 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001249 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001250 return 0;
1251}
1252
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253/*
1254 * OPEN_RECLAIM:
1255 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001257static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001259 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001260 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001261 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 int status;
1263
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001264 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1265 if (IS_ERR(opendata))
1266 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001267 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1268 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001269 rcu_read_lock();
1270 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001271 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001272 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001273 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001274 opendata->o_arg.u.delegation_type = delegation_type;
1275 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001276 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 return status;
1278}
1279
Trond Myklebust539cd032007-06-05 11:46:42 -04001280static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281{
1282 struct nfs_server *server = NFS_SERVER(state->inode);
1283 struct nfs4_exception exception = { };
1284 int err;
1285 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001286 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001287 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001288 break;
1289 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 } while (exception.retry);
1291 return err;
1292}
1293
Trond Myklebust864472e2006-01-03 09:55:15 +01001294static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1295{
1296 struct nfs_open_context *ctx;
1297 int ret;
1298
1299 ctx = nfs4_state_find_open_context(state);
1300 if (IS_ERR(ctx))
1301 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001302 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001303 put_nfs_open_context(ctx);
1304 return ret;
1305}
1306
Trond Myklebust13437e12007-07-06 15:10:43 -04001307static 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 -07001308{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001309 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001310 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001312 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1313 if (IS_ERR(opendata))
1314 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001315 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebustf597c532012-03-04 18:13:56 -05001316 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust864472e2006-01-03 09:55:15 +01001317 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001318 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001319 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320}
1321
Trond Myklebust13437e12007-07-06 15:10:43 -04001322int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323{
1324 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001325 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 int err;
1327 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001328 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 switch (err) {
1330 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001331 case -ENOENT:
1332 case -ESTALE:
1333 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001334 case -NFS4ERR_BADSESSION:
1335 case -NFS4ERR_BADSLOT:
1336 case -NFS4ERR_BAD_HIGH_SLOT:
1337 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1338 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04001339 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001340 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 case -NFS4ERR_STALE_CLIENTID:
1342 case -NFS4ERR_STALE_STATEID:
1343 case -NFS4ERR_EXPIRED:
1344 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001345 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001346 goto out;
1347 case -ERESTARTSYS:
1348 /*
1349 * The show must go on: exit, but mark the
1350 * stateid as needing recovery.
1351 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001352 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001353 case -NFS4ERR_ADMIN_REVOKED:
1354 case -NFS4ERR_BAD_STATEID:
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001355 nfs_inode_find_state_and_recover(state->inode,
1356 stateid);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001357 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001358 case -EKEYEXPIRED:
1359 /*
1360 * User RPCSEC_GSS context has expired.
1361 * We cannot recover this stateid now, so
1362 * skip it and allow recovery thread to
1363 * proceed.
1364 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001365 case -ENOMEM:
1366 err = 0;
1367 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 }
1369 err = nfs4_handle_exception(server, err, &exception);
1370 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001371out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 return err;
1373}
1374
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001375static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1376{
1377 struct nfs4_opendata *data = calldata;
1378
1379 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001380 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001381 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001382 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001383 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001384 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001385 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001386}
1387
1388static void nfs4_open_confirm_release(void *calldata)
1389{
1390 struct nfs4_opendata *data = calldata;
1391 struct nfs4_state *state = NULL;
1392
1393 /* If this request hasn't been cancelled, do nothing */
1394 if (data->cancelled == 0)
1395 goto out_free;
1396 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001397 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001398 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001399 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001400 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001401 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001402out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001403 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001404}
1405
1406static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001407 .rpc_call_done = nfs4_open_confirm_done,
1408 .rpc_release = nfs4_open_confirm_release,
1409};
1410
1411/*
1412 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1413 */
1414static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1415{
1416 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1417 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001418 struct rpc_message msg = {
1419 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1420 .rpc_argp = &data->c_arg,
1421 .rpc_resp = &data->c_res,
1422 .rpc_cred = data->owner->so_cred,
1423 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001424 struct rpc_task_setup task_setup_data = {
1425 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001426 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001427 .callback_ops = &nfs4_open_confirm_ops,
1428 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001429 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001430 .flags = RPC_TASK_ASYNC,
1431 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 int status;
1433
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001434 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001435 data->rpc_done = 0;
1436 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001437 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001438 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001439 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001440 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001441 status = nfs4_wait_for_completion_rpc_task(task);
1442 if (status != 0) {
1443 data->cancelled = 1;
1444 smp_wmb();
1445 } else
1446 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001447 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 return status;
1449}
1450
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001451static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001453 struct nfs4_opendata *data = calldata;
1454 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001455
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001456 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1457 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001458 /*
1459 * Check if we still need to send an OPEN call, or if we can use
1460 * a delegation instead.
1461 */
1462 if (data->state != NULL) {
1463 struct nfs_delegation *delegation;
1464
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001465 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001466 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001467 rcu_read_lock();
1468 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001469 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1470 can_open_delegated(delegation, data->o_arg.fmode))
1471 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001472 rcu_read_unlock();
1473 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001474 /* Update client id. */
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001475 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001476 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001477 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Andy Adamsone23008e2012-10-02 21:07:32 -04001478 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001479 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1480 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001481 data->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04001482 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001483 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001484 &data->o_res.seq_res,
1485 task) != 0)
1486 nfs_release_seqid(data->o_arg.seqid);
1487 else
1488 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001489 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001490unlock_no_action:
1491 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001492out_no_action:
1493 task->tk_action = NULL;
1494
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001495}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001497static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1498{
1499 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1500 nfs4_open_prepare(task, calldata);
1501}
1502
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001503static void nfs4_open_done(struct rpc_task *task, void *calldata)
1504{
1505 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001507 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001508
Trond Myklebust14516c32010-07-31 14:29:06 -04001509 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1510 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001511
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001512 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001513 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1514 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001515 case S_IFREG:
1516 break;
1517 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001518 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001519 break;
1520 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001521 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001522 break;
1523 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001524 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001525 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001526 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001527 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001528 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1529 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001530 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001531 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001532}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001533
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001534static void nfs4_open_release(void *calldata)
1535{
1536 struct nfs4_opendata *data = calldata;
1537 struct nfs4_state *state = NULL;
1538
1539 /* If this request hasn't been cancelled, do nothing */
1540 if (data->cancelled == 0)
1541 goto out_free;
1542 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001543 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001544 goto out_free;
1545 /* In case we need an open_confirm, no cleanup! */
1546 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1547 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001548 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001549 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001550 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001551out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001552 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001553}
1554
1555static const struct rpc_call_ops nfs4_open_ops = {
1556 .rpc_call_prepare = nfs4_open_prepare,
1557 .rpc_call_done = nfs4_open_done,
1558 .rpc_release = nfs4_open_release,
1559};
1560
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001561static const struct rpc_call_ops nfs4_recover_open_ops = {
1562 .rpc_call_prepare = nfs4_recover_open_prepare,
1563 .rpc_call_done = nfs4_open_done,
1564 .rpc_release = nfs4_open_release,
1565};
1566
1567static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001568{
1569 struct inode *dir = data->dir->d_inode;
1570 struct nfs_server *server = NFS_SERVER(dir);
1571 struct nfs_openargs *o_arg = &data->o_arg;
1572 struct nfs_openres *o_res = &data->o_res;
1573 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001574 struct rpc_message msg = {
1575 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1576 .rpc_argp = o_arg,
1577 .rpc_resp = o_res,
1578 .rpc_cred = data->owner->so_cred,
1579 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001580 struct rpc_task_setup task_setup_data = {
1581 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001582 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001583 .callback_ops = &nfs4_open_ops,
1584 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001585 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001586 .flags = RPC_TASK_ASYNC,
1587 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001588 int status;
1589
Trond Myklebust9d12b212012-01-17 22:04:25 -05001590 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001591 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001592 data->rpc_done = 0;
1593 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001594 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001595 if (isrecover)
1596 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001597 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001598 if (IS_ERR(task))
1599 return PTR_ERR(task);
1600 status = nfs4_wait_for_completion_rpc_task(task);
1601 if (status != 0) {
1602 data->cancelled = 1;
1603 smp_wmb();
1604 } else
1605 status = data->rpc_status;
1606 rpc_put_task(task);
1607
1608 return status;
1609}
1610
1611static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1612{
1613 struct inode *dir = data->dir->d_inode;
1614 struct nfs_openres *o_res = &data->o_res;
1615 int status;
1616
1617 status = nfs4_run_open_task(data, 1);
1618 if (status != 0 || !data->rpc_done)
1619 return status;
1620
Trond Myklebust6926afd12012-01-07 13:22:46 -05001621 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1622
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001623 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1624 status = _nfs4_proc_open_confirm(data);
1625 if (status != 0)
1626 return status;
1627 }
1628
1629 return status;
1630}
1631
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001632static int nfs4_opendata_access(struct rpc_cred *cred,
1633 struct nfs4_opendata *opendata,
1634 struct nfs4_state *state, fmode_t fmode)
1635{
1636 struct nfs_access_entry cache;
1637 u32 mask;
1638
1639 /* access call failed or for some reason the server doesn't
1640 * support any access modes -- defer access call until later */
1641 if (opendata->o_res.access_supported == 0)
1642 return 0;
1643
1644 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001645 /* don't check MAY_WRITE - a newly created file may not have
1646 * write mode bits, but POSIX allows the creating process to write */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001647 if (fmode & FMODE_READ)
1648 mask |= MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001649 if (fmode & FMODE_EXEC)
1650 mask |= MAY_EXEC;
1651
1652 cache.cred = cred;
1653 cache.jiffies = jiffies;
1654 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1655 nfs_access_add_cache(state->inode, &cache);
1656
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001657 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001658 return 0;
1659
1660 /* even though OPEN succeeded, access is denied. Close the file */
1661 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001662 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001663}
1664
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001665/*
1666 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1667 */
1668static int _nfs4_proc_open(struct nfs4_opendata *data)
1669{
1670 struct inode *dir = data->dir->d_inode;
1671 struct nfs_server *server = NFS_SERVER(dir);
1672 struct nfs_openargs *o_arg = &data->o_arg;
1673 struct nfs_openres *o_res = &data->o_res;
1674 int status;
1675
1676 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001677 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001678 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001679 if (status != 0) {
1680 if (status == -NFS4ERR_BADNAME &&
1681 !(o_arg->open_flags & O_CREAT))
1682 return -ENOENT;
1683 return status;
1684 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001685
Trond Myklebust6926afd12012-01-07 13:22:46 -05001686 nfs_fattr_map_and_free_names(server, &data->f_attr);
1687
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001688 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001689 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001690 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1691 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001693 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001695 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 }
1697 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001698 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001699 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700}
1701
Andy Adamsond83217c2011-03-01 01:34:17 +00001702static int nfs4_recover_expired_lease(struct nfs_server *server)
1703{
1704 return nfs4_client_recover_expired_lease(server->nfs_client);
1705}
1706
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707/*
1708 * OPEN_EXPIRED:
1709 * reclaim state on the server after a network partition.
1710 * Assumes caller holds the appropriate lock
1711 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001712static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001714 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001715 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001717 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1718 if (IS_ERR(opendata))
1719 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001720 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001721 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001722 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001723 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001724 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725}
1726
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001727static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001728{
Trond Myklebust539cd032007-06-05 11:46:42 -04001729 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001730 struct nfs4_exception exception = { };
1731 int err;
1732
1733 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001734 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001735 switch (err) {
1736 default:
1737 goto out;
1738 case -NFS4ERR_GRACE:
1739 case -NFS4ERR_DELAY:
1740 nfs4_handle_exception(server, err, &exception);
1741 err = 0;
1742 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001743 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001744out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001745 return err;
1746}
1747
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1749{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001751 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
Trond Myklebust864472e2006-01-03 09:55:15 +01001753 ctx = nfs4_state_find_open_context(state);
1754 if (IS_ERR(ctx))
1755 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001756 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001757 put_nfs_open_context(ctx);
1758 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759}
1760
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001761#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001762static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001763{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001764 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001765 nfs4_stateid *stateid = &state->stateid;
1766 int status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001767
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001768 /* If a state reset has been done, test_stateid is unneeded */
1769 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1770 return;
1771
1772 status = nfs41_test_stateid(server, stateid);
1773 if (status != NFS_OK) {
1774 /* Free the stateid unless the server explicitly
1775 * informs us the stateid is unrecognized. */
1776 if (status != -NFS4ERR_BAD_STATEID)
1777 nfs41_free_stateid(server, stateid);
Bryan Schumaker69388672012-09-26 15:25:52 -04001778 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001779
Bryan Schumaker69388672012-09-26 15:25:52 -04001780 write_seqlock(&state->seqlock);
1781 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1782 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001783 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1784 }
1785}
1786
1787/**
1788 * nfs41_check_open_stateid - possibly free an open stateid
1789 *
1790 * @state: NFSv4 state for an inode
1791 *
1792 * Returns NFS_OK if recovery for this stateid is now finished.
1793 * Otherwise a negative NFS4ERR value is returned.
1794 */
1795static int nfs41_check_open_stateid(struct nfs4_state *state)
1796{
1797 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04001798 nfs4_stateid *stateid = &state->open_stateid;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001799 int status;
1800
1801 /* If a state reset has been done, test_stateid is unneeded */
1802 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1803 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1804 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1805 return -NFS4ERR_BAD_STATEID;
1806
1807 status = nfs41_test_stateid(server, stateid);
1808 if (status != NFS_OK) {
1809 /* Free the stateid unless the server explicitly
1810 * informs us the stateid is unrecognized. */
1811 if (status != -NFS4ERR_BAD_STATEID)
1812 nfs41_free_stateid(server, stateid);
1813
1814 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1815 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1816 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001817 }
1818 return status;
1819}
1820
1821static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1822{
Chuck Levereb64cf92012-07-11 16:30:05 -04001823 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001824
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001825 nfs41_clear_delegation_stateid(state);
1826 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04001827 if (status != NFS_OK)
1828 status = nfs4_open_expired(sp, state);
1829 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001830}
1831#endif
1832
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001834 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1835 * fields corresponding to attributes that were used to store the verifier.
1836 * Make sure we clobber those fields in the later setattr call
1837 */
1838static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1839{
1840 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1841 !(sattr->ia_valid & ATTR_ATIME_SET))
1842 sattr->ia_valid |= ATTR_ATIME;
1843
1844 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1845 !(sattr->ia_valid & ATTR_MTIME_SET))
1846 sattr->ia_valid |= ATTR_MTIME;
1847}
1848
1849/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001850 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 */
Andy Adamson82be4172012-05-23 05:02:35 -04001852static int _nfs4_do_open(struct inode *dir,
1853 struct dentry *dentry,
1854 fmode_t fmode,
1855 int flags,
1856 struct iattr *sattr,
1857 struct rpc_cred *cred,
1858 struct nfs4_state **res,
1859 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860{
1861 struct nfs4_state_owner *sp;
1862 struct nfs4_state *state = NULL;
1863 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001864 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001865 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
1867 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001869 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1870 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1872 goto out_err;
1873 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001874 status = nfs4_recover_expired_lease(server);
1875 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001876 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001877 if (dentry->d_inode != NULL)
1878 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001879 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001880 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001881 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001882 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
Andy Adamson82be4172012-05-23 05:02:35 -04001884 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
1885 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
1886 if (!opendata->f_attr.mdsthreshold)
1887 goto err_opendata_put;
Trond Myklebust1549210f2012-06-05 09:16:47 -04001888 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04001889 }
Al Viro82a2c1b2011-06-22 18:30:55 -04001890 if (dentry->d_inode != NULL)
1891 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001892
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001893 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001895 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001897 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001898 status = PTR_ERR(state);
1899 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001900 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001901 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001902 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001903
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001904 status = nfs4_opendata_access(cred, opendata, state, fmode);
1905 if (status != 0)
1906 goto err_opendata_put;
1907
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001908 if (opendata->o_arg.open_flags & O_EXCL) {
1909 nfs4_exclusive_attrset(opendata, sattr);
1910
1911 nfs_fattr_init(opendata->o_res.f_attr);
1912 status = nfs4_do_setattr(state->inode, cred,
1913 opendata->o_res.f_attr, sattr,
1914 state);
1915 if (status == 0)
1916 nfs_setattr_update_inode(state->inode, sattr);
1917 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1918 }
Andy Adamson82be4172012-05-23 05:02:35 -04001919
1920 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
1921 *ctx_th = opendata->f_attr.mdsthreshold;
1922 else
1923 kfree(opendata->f_attr.mdsthreshold);
1924 opendata->f_attr.mdsthreshold = NULL;
1925
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001926 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 *res = state;
1929 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001930err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04001931 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001932 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001933err_put_state_owner:
1934 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 *res = NULL;
1937 return status;
1938}
1939
1940
Andy Adamson82be4172012-05-23 05:02:35 -04001941static struct nfs4_state *nfs4_do_open(struct inode *dir,
1942 struct dentry *dentry,
1943 fmode_t fmode,
1944 int flags,
1945 struct iattr *sattr,
1946 struct rpc_cred *cred,
1947 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948{
1949 struct nfs4_exception exception = { };
1950 struct nfs4_state *res;
1951 int status;
1952
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001953 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 do {
Andy Adamson82be4172012-05-23 05:02:35 -04001955 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
1956 &res, ctx_th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 if (status == 0)
1958 break;
1959 /* NOTE: BAD_SEQID means the server and client disagree about the
1960 * book-keeping w.r.t. state-changing operations
1961 * (OPEN/CLOSE/LOCK/LOCKU...)
1962 * It is actually a sign of a bug on the client or on the server.
1963 *
1964 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001965 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 * have unhashed the old state_owner for us, and that we can
1967 * therefore safely retry using a new one. We should still warn
1968 * the user though...
1969 */
1970 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04001971 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001972 " returned a bad sequence-id error!\n",
1973 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 exception.retry = 1;
1975 continue;
1976 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001977 /*
1978 * BAD_STATEID on OPEN means that the server cancelled our
1979 * state before it received the OPEN_CONFIRM.
1980 * Recover by retrying the request as per the discussion
1981 * on Page 181 of RFC3530.
1982 */
1983 if (status == -NFS4ERR_BAD_STATEID) {
1984 exception.retry = 1;
1985 continue;
1986 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001987 if (status == -EAGAIN) {
1988 /* We must have found a delegation */
1989 exception.retry = 1;
1990 continue;
1991 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1993 status, &exception));
1994 } while (exception.retry);
1995 return res;
1996}
1997
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001998static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1999 struct nfs_fattr *fattr, struct iattr *sattr,
2000 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002002 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002004 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 .iap = sattr,
2006 .server = server,
2007 .bitmask = server->attr_bitmask,
2008 };
2009 struct nfs_setattrres res = {
2010 .fattr = fattr,
2011 .server = server,
2012 };
2013 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002014 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2015 .rpc_argp = &arg,
2016 .rpc_resp = &res,
2017 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002019 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04002020 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
Trond Myklebust0e574af2005-10-27 22:12:38 -04002022 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023
Trond Myklebust4fc87962012-03-08 17:42:01 -05002024 if (state != NULL) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002025 struct nfs_lockowner lockowner = {
2026 .l_owner = current->files,
2027 .l_pid = current->tgid,
2028 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002029 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002030 &lockowner);
Trond Myklebust4fc87962012-03-08 17:42:01 -05002031 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2032 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002033 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002034 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002035 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
Bryan Schumaker7c513052011-03-24 17:12:24 +00002037 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002038 if (status == 0 && state != NULL)
2039 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002040 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041}
2042
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002043static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2044 struct nfs_fattr *fattr, struct iattr *sattr,
2045 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002047 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002048 struct nfs4_exception exception = {
2049 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002050 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002051 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 int err;
2053 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04002054 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2055 switch (err) {
2056 case -NFS4ERR_OPENMODE:
2057 if (state && !(state->state & FMODE_WRITE)) {
2058 err = -EBADF;
2059 if (sattr->ia_valid & ATTR_OPEN)
2060 err = -EACCES;
2061 goto out;
2062 }
2063 }
2064 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002066out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 return err;
2068}
2069
2070struct nfs4_closedata {
2071 struct inode *inode;
2072 struct nfs4_state *state;
2073 struct nfs_closeargs arg;
2074 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002075 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002076 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002077 bool roc;
2078 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079};
2080
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002081static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002082{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002083 struct nfs4_closedata *calldata = data;
2084 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002085 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002086
Fred Isamanf7e89172011-01-06 11:36:32 +00002087 if (calldata->roc)
2088 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002089 nfs4_put_open_state(calldata->state);
2090 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002091 nfs4_put_state_owner(sp);
Weston Andros Adamson324d0032012-10-30 17:01:39 -04002092 nfs_sb_deactive_async(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002093 kfree(calldata);
2094}
2095
Trond Myklebust88069f72009-12-08 08:33:16 -05002096static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2097 fmode_t fmode)
2098{
2099 spin_lock(&state->owner->so_lock);
2100 if (!(fmode & FMODE_READ))
2101 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2102 if (!(fmode & FMODE_WRITE))
2103 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2104 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2105 spin_unlock(&state->owner->so_lock);
2106}
2107
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002108static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002110 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 struct nfs_server *server = NFS_SERVER(calldata->inode);
2113
Chuck Levera3ca5652012-03-01 17:00:40 -05002114 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002115 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2116 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 /* hmm. we are done with the inode, and in the process of freeing
2118 * the state_owner. we keep this around to process errors
2119 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 switch (task->tk_status) {
2121 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002122 if (calldata->roc)
2123 pnfs_roc_set_barrier(state->inode,
2124 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002125 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002126 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002127 nfs4_close_clear_stateid_flags(state,
2128 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 break;
2130 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002131 case -NFS4ERR_OLD_STATEID:
2132 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002134 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002135 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002137 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2138 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002140 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002141 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002142 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143}
2144
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002145static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002147 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002148 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002149 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002150 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002151
Chuck Levera3ca5652012-03-01 17:00:40 -05002152 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002153 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07002154 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04002155
Trond Myklebust88069f72009-12-08 08:33:16 -05002156 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2157 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002158 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002159 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002160 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002161 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002162 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2163 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2164 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002165 }
2166 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002167 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2168 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2169 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002170 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002171 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002172 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002173
2174 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002175 /* Note: exit _without_ calling nfs4_close_done */
2176 task->tk_action = NULL;
Chuck Levera3ca5652012-03-01 17:00:40 -05002177 goto out;
Trond Myklebust95121352005-10-18 14:20:12 -07002178 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002179
Fred Isamanf7e89172011-01-06 11:36:32 +00002180 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002181 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002182 if (calldata->roc &&
Trond Myklebust7fdab062012-09-20 20:15:57 -04002183 pnfs_roc_drain(inode, &calldata->roc_barrier, task))
Chuck Levera3ca5652012-03-01 17:00:40 -05002184 goto out;
Fred Isamanf7e89172011-01-06 11:36:32 +00002185 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002186
Trond Myklebust516a6af2005-10-27 22:12:41 -04002187 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002188 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002189 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002190 &calldata->arg.seq_args,
2191 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002192 task) != 0)
2193 nfs_release_seqid(calldata->arg.seqid);
2194 else
2195 rpc_call_start(task);
Chuck Levera3ca5652012-03-01 17:00:40 -05002196out:
2197 dprintk("%s: done!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198}
2199
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002200static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002201 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002202 .rpc_call_done = nfs4_close_done,
2203 .rpc_release = nfs4_free_closedata,
2204};
2205
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206/*
2207 * It is possible for data to be read/written from a mem-mapped file
2208 * after the sys_close call (which hits the vfs layer as a flush).
2209 * This means that we can't safely call nfsv4 close on a file until
2210 * the inode is cleared. This in turn means that we are not good
2211 * NFSv4 citizens - we do not indicate to the server to update the file's
2212 * share state even when we are done with one of the three share
2213 * stateid's in the inode.
2214 *
2215 * NOTE: Caller must be holding the sp->so_owner semaphore!
2216 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002217int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002219 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002221 struct nfs4_state_owner *sp = state->owner;
2222 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002223 struct rpc_message msg = {
2224 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2225 .rpc_cred = state->owner->so_cred,
2226 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002227 struct rpc_task_setup task_setup_data = {
2228 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002229 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002230 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002231 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002232 .flags = RPC_TASK_ASYNC,
2233 };
Trond Myklebust95121352005-10-18 14:20:12 -07002234 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002236 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002238 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002239 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002240 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002242 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002243 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002245 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002246 if (calldata->arg.seqid == NULL)
2247 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002248 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002249 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002250 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002251 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002252 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002253 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002254 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002255
Trond Myklebust1174dd12010-12-21 10:52:24 -05002256 msg.rpc_argp = &calldata->arg;
2257 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002258 task_setup_data.callback_data = calldata;
2259 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002260 if (IS_ERR(task))
2261 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002262 status = 0;
2263 if (wait)
2264 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002265 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002266 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002267out_free_calldata:
2268 kfree(calldata);
2269out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002270 nfs4_put_open_state(state);
2271 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002272 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273}
2274
Trond Myklebust2b484292010-09-17 10:56:51 -04002275static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002276nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 struct nfs4_state *state;
2279
Trond Myklebust565277f2007-10-15 18:17:53 -04002280 /* Protect against concurrent sillydeletes */
Andy Adamson82be4172012-05-23 05:02:35 -04002281 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2282 ctx->cred, &ctx->mdsthreshold);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002283 if (IS_ERR(state))
2284 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002285 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002286 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287}
2288
Trond Myklebust1185a552009-12-03 15:54:02 -05002289static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002290{
2291 if (ctx->state == NULL)
2292 return;
2293 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002294 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002295 else
Al Viro643168c2011-06-22 18:20:23 -04002296 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002297}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298
2299static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2300{
Benny Halevy43652ad2009-04-01 09:21:54 -04002301 struct nfs4_server_caps_arg args = {
2302 .fhandle = fhandle,
2303 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 struct nfs4_server_caps_res res = {};
2305 struct rpc_message msg = {
2306 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002307 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 .rpc_resp = &res,
2309 };
2310 int status;
2311
Bryan Schumaker7c513052011-03-24 17:12:24 +00002312 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 if (status == 0) {
2314 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04002315 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2316 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2317 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2318 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2319 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2321 server->caps |= NFS_CAP_ACLS;
2322 if (res.has_links != 0)
2323 server->caps |= NFS_CAP_HARDLINKS;
2324 if (res.has_symlinks != 0)
2325 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04002326 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2327 server->caps |= NFS_CAP_FILEID;
2328 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2329 server->caps |= NFS_CAP_MODE;
2330 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2331 server->caps |= NFS_CAP_NLINK;
2332 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2333 server->caps |= NFS_CAP_OWNER;
2334 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2335 server->caps |= NFS_CAP_OWNER_GROUP;
2336 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2337 server->caps |= NFS_CAP_ATIME;
2338 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2339 server->caps |= NFS_CAP_CTIME;
2340 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2341 server->caps |= NFS_CAP_MTIME;
2342
Trond Myklebusta65318b2009-03-11 14:10:28 -04002343 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2344 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2345 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002347 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002349
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 return status;
2351}
2352
Trond Myklebust55a97592006-06-09 09:34:19 -04002353int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354{
2355 struct nfs4_exception exception = { };
2356 int err;
2357 do {
2358 err = nfs4_handle_exception(server,
2359 _nfs4_server_capabilities(server, fhandle),
2360 &exception);
2361 } while (exception.retry);
2362 return err;
2363}
2364
2365static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2366 struct nfs_fsinfo *info)
2367{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 struct nfs4_lookup_root_arg args = {
2369 .bitmask = nfs4_fattr_bitmap,
2370 };
2371 struct nfs4_lookup_res res = {
2372 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002373 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 .fh = fhandle,
2375 };
2376 struct rpc_message msg = {
2377 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2378 .rpc_argp = &args,
2379 .rpc_resp = &res,
2380 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002381
Trond Myklebust0e574af2005-10-27 22:12:38 -04002382 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002383 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384}
2385
2386static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2387 struct nfs_fsinfo *info)
2388{
2389 struct nfs4_exception exception = { };
2390 int err;
2391 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002392 err = _nfs4_lookup_root(server, fhandle, info);
2393 switch (err) {
2394 case 0:
2395 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002396 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002397 default:
2398 err = nfs4_handle_exception(server, err, &exception);
2399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002401out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 return err;
2403}
2404
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002405static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2406 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2407{
2408 struct rpc_auth *auth;
2409 int ret;
2410
2411 auth = rpcauth_create(flavor, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002412 if (IS_ERR(auth)) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002413 ret = -EIO;
2414 goto out;
2415 }
2416 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002417out:
2418 return ret;
2419}
2420
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002421static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002422 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002424 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002425 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002427 len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04002428 if (len < 0)
2429 return len;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002430
2431 for (i = 0; i < len; i++) {
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002432 /* AUTH_UNIX is the default flavor if none was specified,
2433 * thus has already been tried. */
2434 if (flav_array[i] == RPC_AUTH_UNIX)
2435 continue;
2436
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002437 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002438 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002439 continue;
2440 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002441 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002442 /*
2443 * -EACCESS could mean that the user doesn't have correct permissions
2444 * to access the mount. It could also mean that we tried to mount
2445 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2446 * existing mount programs don't handle -EACCES very well so it should
2447 * be mapped to -EPERM instead.
2448 */
2449 if (status == -EACCES)
2450 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002451 return status;
2452}
2453
2454/*
2455 * get the file handle for the "/" directory on the server
2456 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002457int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2458 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002459{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002460 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002461 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002462 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2463 /*
2464 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2465 * by nfs4_map_errors() as this function exits.
2466 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002467 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 if (status == 0)
2469 status = nfs4_server_capabilities(server, fhandle);
2470 if (status == 0)
2471 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002472 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473}
2474
Bryan Schumakerbae36242012-05-10 15:07:31 -04002475static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2476 struct nfs_fsinfo *info)
2477{
2478 int error;
2479 struct nfs_fattr *fattr = info->fattr;
2480
2481 error = nfs4_server_capabilities(server, mntfh);
2482 if (error < 0) {
2483 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2484 return error;
2485 }
2486
2487 error = nfs4_proc_getattr(server, mntfh, fattr);
2488 if (error < 0) {
2489 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2490 return error;
2491 }
2492
2493 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2494 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2495 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2496
2497 return error;
2498}
2499
Manoj Naik6b97fd32006-06-09 09:34:29 -04002500/*
2501 * Get locations and (maybe) other attributes of a referral.
2502 * Note that we'll actually follow the referral later when
2503 * we detect fsid mismatch in inode revalidation
2504 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002505static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2506 const struct qstr *name, struct nfs_fattr *fattr,
2507 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002508{
2509 int status = -ENOMEM;
2510 struct page *page = NULL;
2511 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002512
2513 page = alloc_page(GFP_KERNEL);
2514 if (page == NULL)
2515 goto out;
2516 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2517 if (locations == NULL)
2518 goto out;
2519
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002520 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002521 if (status != 0)
2522 goto out;
2523 /* Make sure server returned a different fsid for the referral */
2524 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002525 dprintk("%s: server did not return a different fsid for"
2526 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002527 status = -EIO;
2528 goto out;
2529 }
Andy Adamson533eb462011-06-13 18:25:56 -04002530 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2531 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002532
Andy Adamson533eb462011-06-13 18:25:56 -04002533 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002534 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002535 memset(fhandle, 0, sizeof(struct nfs_fh));
2536out:
2537 if (page)
2538 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002539 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002540 return status;
2541}
2542
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2544{
2545 struct nfs4_getattr_arg args = {
2546 .fh = fhandle,
2547 .bitmask = server->attr_bitmask,
2548 };
2549 struct nfs4_getattr_res res = {
2550 .fattr = fattr,
2551 .server = server,
2552 };
2553 struct rpc_message msg = {
2554 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2555 .rpc_argp = &args,
2556 .rpc_resp = &res,
2557 };
2558
Trond Myklebust0e574af2005-10-27 22:12:38 -04002559 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002560 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561}
2562
2563static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2564{
2565 struct nfs4_exception exception = { };
2566 int err;
2567 do {
2568 err = nfs4_handle_exception(server,
2569 _nfs4_proc_getattr(server, fhandle, fattr),
2570 &exception);
2571 } while (exception.retry);
2572 return err;
2573}
2574
2575/*
2576 * The file is not closed if it is opened due to the a request to change
2577 * the size of the file. The open call will not be needed once the
2578 * VFS layer lookup-intents are implemented.
2579 *
2580 * Close is called when the inode is destroyed.
2581 * If we haven't opened the file for O_WRONLY, we
2582 * need to in the size_change case to obtain a stateid.
2583 *
2584 * Got race?
2585 * Because OPEN is always done by name in nfsv4, it is
2586 * possible that we opened a different file by the same
2587 * name. We can recognize this race condition, but we
2588 * can't do anything about it besides returning an error.
2589 *
2590 * This will be fixed with VFS changes (lookup-intent).
2591 */
2592static int
2593nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2594 struct iattr *sattr)
2595{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002596 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002597 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002598 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 int status;
2600
Benny Halevy8a1636c2010-07-14 15:43:57 -04002601 if (pnfs_ld_layoutret_on_setattr(inode))
2602 pnfs_return_layout(inode);
2603
Trond Myklebust0e574af2005-10-27 22:12:38 -04002604 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605
Andy Adamson26699402012-05-30 16:12:24 -04002606 /* Deal with open(O_TRUNC) */
2607 if (sattr->ia_valid & ATTR_OPEN)
2608 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2609
2610 /* Optimization: if the end result is no change, don't RPC */
2611 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2612 return 0;
2613
Trond Myklebustd5308382005-11-04 15:33:38 -05002614 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002615 if (sattr->ia_valid & ATTR_FILE) {
2616 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002617
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002618 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002619 if (ctx) {
2620 cred = ctx->cred;
2621 state = ctx->state;
2622 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002623 }
2624
2625 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002626 if (status == 0)
2627 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 return status;
2629}
2630
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002631static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2632 const struct qstr *name, struct nfs_fh *fhandle,
2633 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002634{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002635 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002636 int status;
2637 struct nfs4_lookup_arg args = {
2638 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002639 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002640 .name = name,
2641 };
2642 struct nfs4_lookup_res res = {
2643 .server = server,
2644 .fattr = fattr,
2645 .fh = fhandle,
2646 };
2647 struct rpc_message msg = {
2648 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2649 .rpc_argp = &args,
2650 .rpc_resp = &res,
2651 };
2652
2653 nfs_fattr_init(fattr);
2654
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002656 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 dprintk("NFS reply lookup: %d\n", status);
2658 return status;
2659}
2660
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002661static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002662{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002663 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002664 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002665 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2666 fattr->nlink = 2;
2667}
2668
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002669static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2670 struct qstr *name, struct nfs_fh *fhandle,
2671 struct nfs_fattr *fattr)
2672{
2673 struct nfs4_exception exception = { };
2674 struct rpc_clnt *client = *clnt;
2675 int err;
2676 do {
2677 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2678 switch (err) {
2679 case -NFS4ERR_BADNAME:
2680 err = -ENOENT;
2681 goto out;
2682 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002683 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002684 goto out;
2685 case -NFS4ERR_WRONGSEC:
2686 err = -EPERM;
2687 if (client != *clnt)
2688 goto out;
2689
2690 client = nfs4_create_sec_client(client, dir, name);
2691 if (IS_ERR(client))
2692 return PTR_ERR(client);
2693
2694 exception.retry = 1;
2695 break;
2696 default:
2697 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2698 }
2699 } while (exception.retry);
2700
2701out:
2702 if (err == 0)
2703 *clnt = client;
2704 else if (client != *clnt)
2705 rpc_shutdown_client(client);
2706
2707 return err;
2708}
2709
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002710static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002711 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002713 int status;
2714 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002715
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002716 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2717 if (client != NFS_CLIENT(dir)) {
2718 rpc_shutdown_client(client);
2719 nfs_fixup_secinfo_attributes(fattr);
2720 }
2721 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722}
2723
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002724struct rpc_clnt *
2725nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2726 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2727{
2728 int status;
2729 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2730
2731 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2732 if (status < 0) {
2733 rpc_shutdown_client(client);
2734 return ERR_PTR(status);
2735 }
2736 return client;
2737}
2738
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2740{
Trond Myklebust76b32992007-08-10 17:45:11 -04002741 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 struct nfs4_accessargs args = {
2743 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002744 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002746 struct nfs4_accessres res = {
2747 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002748 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 struct rpc_message msg = {
2750 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2751 .rpc_argp = &args,
2752 .rpc_resp = &res,
2753 .rpc_cred = entry->cred,
2754 };
2755 int mode = entry->mask;
2756 int status;
2757
2758 /*
2759 * Determine which access bits we want to ask for...
2760 */
2761 if (mode & MAY_READ)
2762 args.access |= NFS4_ACCESS_READ;
2763 if (S_ISDIR(inode->i_mode)) {
2764 if (mode & MAY_WRITE)
2765 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2766 if (mode & MAY_EXEC)
2767 args.access |= NFS4_ACCESS_LOOKUP;
2768 } else {
2769 if (mode & MAY_WRITE)
2770 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2771 if (mode & MAY_EXEC)
2772 args.access |= NFS4_ACCESS_EXECUTE;
2773 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002774
2775 res.fattr = nfs_alloc_fattr();
2776 if (res.fattr == NULL)
2777 return -ENOMEM;
2778
Bryan Schumaker7c513052011-03-24 17:12:24 +00002779 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002781 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04002782 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002784 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 return status;
2786}
2787
2788static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2789{
2790 struct nfs4_exception exception = { };
2791 int err;
2792 do {
2793 err = nfs4_handle_exception(NFS_SERVER(inode),
2794 _nfs4_proc_access(inode, entry),
2795 &exception);
2796 } while (exception.retry);
2797 return err;
2798}
2799
2800/*
2801 * TODO: For the time being, we don't try to get any attributes
2802 * along with any of the zero-copy operations READ, READDIR,
2803 * READLINK, WRITE.
2804 *
2805 * In the case of the first three, we want to put the GETATTR
2806 * after the read-type operation -- this is because it is hard
2807 * to predict the length of a GETATTR response in v4, and thus
2808 * align the READ data correctly. This means that the GETATTR
2809 * may end up partially falling into the page cache, and we should
2810 * shift it into the 'tail' of the xdr_buf before processing.
2811 * To do this efficiently, we need to know the total length
2812 * of data received, which doesn't seem to be available outside
2813 * of the RPC layer.
2814 *
2815 * In the case of WRITE, we also want to put the GETATTR after
2816 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04002817 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 *
2819 * Both of these changes to the XDR layer would in fact be quite
2820 * minor, but I decided to leave them for a subsequent patch.
2821 */
2822static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2823 unsigned int pgbase, unsigned int pglen)
2824{
2825 struct nfs4_readlink args = {
2826 .fh = NFS_FH(inode),
2827 .pgbase = pgbase,
2828 .pglen = pglen,
2829 .pages = &page,
2830 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002831 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 struct rpc_message msg = {
2833 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2834 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002835 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 };
2837
Bryan Schumaker7c513052011-03-24 17:12:24 +00002838 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 -07002839}
2840
2841static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2842 unsigned int pgbase, unsigned int pglen)
2843{
2844 struct nfs4_exception exception = { };
2845 int err;
2846 do {
2847 err = nfs4_handle_exception(NFS_SERVER(inode),
2848 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2849 &exception);
2850 } while (exception.retry);
2851 return err;
2852}
2853
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002855 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857static int
2858nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002859 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860{
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002861 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 int status = 0;
2864
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002865 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
2866 if (IS_ERR(ctx))
2867 return PTR_ERR(ctx);
2868
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002869 sattr->ia_mode &= ~current_umask();
Trond Myklebust8626e4a2012-07-16 12:01:42 -04002870 state = nfs4_do_open(dir, dentry, ctx->mode,
2871 flags, sattr, ctx->cred,
2872 &ctx->mdsthreshold);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002873 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 if (IS_ERR(state)) {
2875 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002876 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002878 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002879 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002880 ctx->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881out:
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002882 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 return status;
2884}
2885
2886static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2887{
Trond Myklebust16e42952005-10-27 22:12:44 -04002888 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002889 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07002891 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002893 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002894 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002895 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002897 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2898 .rpc_argp = &args,
2899 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 };
Trond Myklebust778d2812012-04-27 13:48:19 -04002901 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04002902
Bryan Schumaker7c513052011-03-24 17:12:24 +00002903 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04002904 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04002905 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 return status;
2907}
2908
2909static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2910{
2911 struct nfs4_exception exception = { };
2912 int err;
2913 do {
2914 err = nfs4_handle_exception(NFS_SERVER(dir),
2915 _nfs4_proc_remove(dir, name),
2916 &exception);
2917 } while (exception.retry);
2918 return err;
2919}
2920
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002921static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002923 struct nfs_server *server = NFS_SERVER(dir);
2924 struct nfs_removeargs *args = msg->rpc_argp;
2925 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002927 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05002929 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930}
2931
Bryan Schumaker34e137c2012-03-19 14:54:41 -04002932static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
2933{
2934 if (nfs4_setup_sequence(NFS_SERVER(data->dir),
2935 &data->args.seq_args,
2936 &data->res.seq_res,
2937 task))
2938 return;
2939 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940}
2941
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002942static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002944 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2945
Trond Myklebust14516c32010-07-31 14:29:06 -04002946 if (!nfs4_sequence_done(task, &res->seq_res))
2947 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002948 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002949 return 0;
2950 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002951 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952}
2953
Jeff Laytond3d41522010-09-17 17:31:57 -04002954static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2955{
2956 struct nfs_server *server = NFS_SERVER(dir);
2957 struct nfs_renameargs *arg = msg->rpc_argp;
2958 struct nfs_renameres *res = msg->rpc_resp;
2959
2960 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04002961 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002962 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04002963}
2964
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04002965static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
2966{
2967 if (nfs4_setup_sequence(NFS_SERVER(data->old_dir),
2968 &data->args.seq_args,
2969 &data->res.seq_res,
2970 task))
2971 return;
2972 rpc_call_start(task);
Jeff Laytond3d41522010-09-17 17:31:57 -04002973}
2974
2975static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2976 struct inode *new_dir)
2977{
2978 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2979
2980 if (!nfs4_sequence_done(task, &res->seq_res))
2981 return 0;
2982 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2983 return 0;
2984
2985 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04002986 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04002987 return 1;
2988}
2989
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2991 struct inode *new_dir, struct qstr *new_name)
2992{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002993 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002994 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 .old_dir = NFS_FH(old_dir),
2996 .new_dir = NFS_FH(new_dir),
2997 .old_name = old_name,
2998 .new_name = new_name,
2999 };
Jeff Laytone8582a82010-09-17 17:31:06 -04003000 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003001 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003002 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 struct rpc_message msg = {
3004 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3005 .rpc_argp = &arg,
3006 .rpc_resp = &res,
3007 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003008 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009
Bryan Schumaker7c513052011-03-24 17:12:24 +00003010 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 if (!status) {
3012 update_changeattr(old_dir, &res.old_cinfo);
3013 update_changeattr(new_dir, &res.new_cinfo);
3014 }
3015 return status;
3016}
3017
3018static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3019 struct inode *new_dir, struct qstr *new_name)
3020{
3021 struct nfs4_exception exception = { };
3022 int err;
3023 do {
3024 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3025 _nfs4_proc_rename(old_dir, old_name,
3026 new_dir, new_name),
3027 &exception);
3028 } while (exception.retry);
3029 return err;
3030}
3031
3032static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3033{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003034 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 struct nfs4_link_arg arg = {
3036 .fh = NFS_FH(inode),
3037 .dir_fh = NFS_FH(dir),
3038 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003039 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003041 struct nfs4_link_res res = {
3042 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003043 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 struct rpc_message msg = {
3045 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3046 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003047 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003049 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050
Trond Myklebust136f2622010-04-16 16:22:49 -04003051 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003052 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003053 goto out;
3054
Bryan Schumaker7c513052011-03-24 17:12:24 +00003055 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003056 if (!status) {
3057 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04003058 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003059 }
Trond Myklebust136f2622010-04-16 16:22:49 -04003060out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003061 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 return status;
3063}
3064
3065static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3066{
3067 struct nfs4_exception exception = { };
3068 int err;
3069 do {
3070 err = nfs4_handle_exception(NFS_SERVER(inode),
3071 _nfs4_proc_link(inode, dir, name),
3072 &exception);
3073 } while (exception.retry);
3074 return err;
3075}
3076
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003077struct nfs4_createdata {
3078 struct rpc_message msg;
3079 struct nfs4_create_arg arg;
3080 struct nfs4_create_res res;
3081 struct nfs_fh fh;
3082 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003083};
3084
3085static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3086 struct qstr *name, struct iattr *sattr, u32 ftype)
3087{
3088 struct nfs4_createdata *data;
3089
3090 data = kzalloc(sizeof(*data), GFP_KERNEL);
3091 if (data != NULL) {
3092 struct nfs_server *server = NFS_SERVER(dir);
3093
3094 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3095 data->msg.rpc_argp = &data->arg;
3096 data->msg.rpc_resp = &data->res;
3097 data->arg.dir_fh = NFS_FH(dir);
3098 data->arg.server = server;
3099 data->arg.name = name;
3100 data->arg.attrs = sattr;
3101 data->arg.ftype = ftype;
3102 data->arg.bitmask = server->attr_bitmask;
3103 data->res.server = server;
3104 data->res.fh = &data->fh;
3105 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003106 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003107 }
3108 return data;
3109}
3110
3111static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3112{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003113 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003114 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003115 if (status == 0) {
3116 update_changeattr(dir, &data->res.dir_cinfo);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003117 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3118 }
3119 return status;
3120}
3121
3122static void nfs4_free_createdata(struct nfs4_createdata *data)
3123{
3124 kfree(data);
3125}
3126
Chuck Lever4f390c12006-08-22 20:06:22 -04003127static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003128 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003130 struct nfs4_createdata *data;
3131 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132
Chuck Lever94a6d752006-08-22 20:06:23 -04003133 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003134 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003135
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003136 status = -ENOMEM;
3137 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3138 if (data == NULL)
3139 goto out;
3140
3141 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3142 data->arg.u.symlink.pages = &page;
3143 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003145 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
Chuck Lever4f390c12006-08-22 20:06:22 -04003152static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003153 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154{
3155 struct nfs4_exception exception = { };
3156 int err;
3157 do {
3158 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003159 _nfs4_proc_symlink(dir, dentry, page,
3160 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 &exception);
3162 } while (exception.retry);
3163 return err;
3164}
3165
3166static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3167 struct iattr *sattr)
3168{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003169 struct nfs4_createdata *data;
3170 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003172 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3173 if (data == NULL)
3174 goto out;
3175
3176 status = nfs4_do_create(dir, dentry, data);
3177
3178 nfs4_free_createdata(data);
3179out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 return status;
3181}
3182
3183static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3184 struct iattr *sattr)
3185{
3186 struct nfs4_exception exception = { };
3187 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003188
3189 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 do {
3191 err = nfs4_handle_exception(NFS_SERVER(dir),
3192 _nfs4_proc_mkdir(dir, dentry, sattr),
3193 &exception);
3194 } while (exception.retry);
3195 return err;
3196}
3197
3198static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003199 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200{
3201 struct inode *dir = dentry->d_inode;
3202 struct nfs4_readdir_arg args = {
3203 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003204 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 .pgbase = 0,
3206 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003207 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003208 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 };
3210 struct nfs4_readdir_res res;
3211 struct rpc_message msg = {
3212 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3213 .rpc_argp = &args,
3214 .rpc_resp = &res,
3215 .rpc_cred = cred,
3216 };
3217 int status;
3218
Harvey Harrison3110ff82008-05-02 13:42:44 -07003219 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003220 dentry->d_parent->d_name.name,
3221 dentry->d_name.name,
3222 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003223 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003225 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 -05003226 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003227 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003228 status += args.pgbase;
3229 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003230
3231 nfs_invalidate_atime(dir);
3232
Harvey Harrison3110ff82008-05-02 13:42:44 -07003233 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 return status;
3235}
3236
3237static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003238 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239{
3240 struct nfs4_exception exception = { };
3241 int err;
3242 do {
3243 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3244 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003245 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 &exception);
3247 } while (exception.retry);
3248 return err;
3249}
3250
3251static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3252 struct iattr *sattr, dev_t rdev)
3253{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003254 struct nfs4_createdata *data;
3255 int mode = sattr->ia_mode;
3256 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003258 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3259 if (data == NULL)
3260 goto out;
3261
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003263 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003265 data->arg.ftype = NF4BLK;
3266 data->arg.u.device.specdata1 = MAJOR(rdev);
3267 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 }
3269 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003270 data->arg.ftype = NF4CHR;
3271 data->arg.u.device.specdata1 = MAJOR(rdev);
3272 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003273 } else if (!S_ISSOCK(mode)) {
3274 status = -EINVAL;
3275 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003278 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003279out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003280 nfs4_free_createdata(data);
3281out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 return status;
3283}
3284
3285static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3286 struct iattr *sattr, dev_t rdev)
3287{
3288 struct nfs4_exception exception = { };
3289 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003290
3291 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 do {
3293 err = nfs4_handle_exception(NFS_SERVER(dir),
3294 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3295 &exception);
3296 } while (exception.retry);
3297 return err;
3298}
3299
3300static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3301 struct nfs_fsstat *fsstat)
3302{
3303 struct nfs4_statfs_arg args = {
3304 .fh = fhandle,
3305 .bitmask = server->attr_bitmask,
3306 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003307 struct nfs4_statfs_res res = {
3308 .fsstat = fsstat,
3309 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 struct rpc_message msg = {
3311 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3312 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003313 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 };
3315
Trond Myklebust0e574af2005-10-27 22:12:38 -04003316 nfs_fattr_init(fsstat->fattr);
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_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3321{
3322 struct nfs4_exception exception = { };
3323 int err;
3324 do {
3325 err = nfs4_handle_exception(server,
3326 _nfs4_proc_statfs(server, fhandle, fsstat),
3327 &exception);
3328 } while (exception.retry);
3329 return err;
3330}
3331
3332static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3333 struct nfs_fsinfo *fsinfo)
3334{
3335 struct nfs4_fsinfo_arg args = {
3336 .fh = fhandle,
3337 .bitmask = server->attr_bitmask,
3338 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003339 struct nfs4_fsinfo_res res = {
3340 .fsinfo = fsinfo,
3341 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 struct rpc_message msg = {
3343 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3344 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003345 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 };
3347
Bryan Schumaker7c513052011-03-24 17:12:24 +00003348 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349}
3350
3351static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3352{
3353 struct nfs4_exception exception = { };
3354 int err;
3355
3356 do {
3357 err = nfs4_handle_exception(server,
3358 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3359 &exception);
3360 } while (exception.retry);
3361 return err;
3362}
3363
3364static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3365{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003366 int error;
3367
Trond Myklebust0e574af2005-10-27 22:12:38 -04003368 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003369 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003370 if (error == 0) {
3371 /* block layout checks this! */
3372 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003373 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003374 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003375
3376 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377}
3378
3379static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3380 struct nfs_pathconf *pathconf)
3381{
3382 struct nfs4_pathconf_arg args = {
3383 .fh = fhandle,
3384 .bitmask = server->attr_bitmask,
3385 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003386 struct nfs4_pathconf_res res = {
3387 .pathconf = pathconf,
3388 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 struct rpc_message msg = {
3390 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3391 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003392 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 };
3394
3395 /* None of the pathconf attributes are mandatory to implement */
3396 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3397 memset(pathconf, 0, sizeof(*pathconf));
3398 return 0;
3399 }
3400
Trond Myklebust0e574af2005-10-27 22:12:38 -04003401 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003402 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403}
3404
3405static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3406 struct nfs_pathconf *pathconf)
3407{
3408 struct nfs4_exception exception = { };
3409 int err;
3410
3411 do {
3412 err = nfs4_handle_exception(server,
3413 _nfs4_proc_pathconf(server, fhandle, pathconf),
3414 &exception);
3415 } while (exception.retry);
3416 return err;
3417}
3418
Benny Halevyd20581a2011-05-22 19:52:03 +03003419void __nfs4_read_done_cb(struct nfs_read_data *data)
3420{
Fred Isamancd841602012-04-20 14:47:44 -04003421 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003422}
3423
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003424static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425{
Fred Isamancd841602012-04-20 14:47:44 -04003426 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003428 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003429 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003430 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003432
Benny Halevyd20581a2011-05-22 19:52:03 +03003433 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003435 renew_lease(server, data->timestamp);
3436 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437}
3438
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003439static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3440{
3441
3442 dprintk("--> %s\n", __func__);
3443
3444 if (!nfs4_sequence_done(task, &data->res.seq_res))
3445 return -EAGAIN;
3446
Benny Halevyd20581a2011-05-22 19:52:03 +03003447 return data->read_done_cb ? data->read_done_cb(task, data) :
3448 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003449}
3450
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003451static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 data->timestamp = jiffies;
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003454 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003455 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003456 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457}
3458
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003459static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3460{
Fred Isamancd841602012-04-20 14:47:44 -04003461 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003462 &data->args.seq_args,
3463 &data->res.seq_res,
3464 task))
3465 return;
3466 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467}
3468
Fred Isamanb029bc92011-03-03 15:13:42 +00003469static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470{
Fred Isamancd841602012-04-20 14:47:44 -04003471 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003473 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003474 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003475 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003477 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003479 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003480 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003481 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482}
3483
Fred Isamanb029bc92011-03-03 15:13:42 +00003484static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3485{
3486 if (!nfs4_sequence_done(task, &data->res.seq_res))
3487 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003488 return data->write_done_cb ? data->write_done_cb(task, data) :
3489 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003490}
3491
Trond Myklebust5a37f852012-04-28 14:55:16 -04003492static
3493bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003494{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003495 const struct nfs_pgio_header *hdr = data->header;
3496
3497 /* Don't request attributes for pNFS or O_DIRECT writes */
3498 if (data->ds_clp != NULL || hdr->dreq != NULL)
3499 return false;
3500 /* Otherwise, request attributes if and only if we don't hold
3501 * a delegation
3502 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04003503 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003504}
Fred Isamana69aef12011-03-03 15:13:47 +00003505
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003506static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507{
Fred Isamancd841602012-04-20 14:47:44 -04003508 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003509
Trond Myklebust5a37f852012-04-28 14:55:16 -04003510 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003511 data->args.bitmask = NULL;
3512 data->res.fattr = NULL;
3513 } else
3514 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003515
Fred Isamanb029bc92011-03-03 15:13:42 +00003516 if (!data->write_done_cb)
3517 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003518 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 data->timestamp = jiffies;
3520
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003521 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003522 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523}
3524
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003525static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3526{
Fred Isamancd841602012-04-20 14:47:44 -04003527 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003528 &data->args.seq_args,
3529 &data->res.seq_res,
3530 task))
3531 return;
3532 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533}
3534
Fred Isaman0b7c0152012-04-20 14:47:39 -04003535static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3536{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3538 &data->args.seq_args,
Trond Myklebust788e7a82006-03-20 13:44:27 -05003539 &data->res.seq_res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 return;
3542 rpc_call_start(task);
Trond Myklebust14516c32010-07-31 14:29:06 -04003543}
3544
Fred Isaman0b7c0152012-04-20 14:47:39 -04003545static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546{
3547 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003548
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003549 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003550 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003551 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003553 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554}
3555
Fred Isaman0b7c0152012-04-20 14:47:39 -04003556static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003557{
3558 if (!nfs4_sequence_done(task, &data->res.seq_res))
3559 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003560 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003561}
3562
Fred Isaman0b7c0152012-04-20 14:47:39 -04003563static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003565 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003566
Fred Isaman0b7c0152012-04-20 14:47:39 -04003567 if (data->commit_done_cb == NULL)
3568 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003569 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003570 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003571 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572}
3573
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003574struct nfs4_renewdata {
3575 struct nfs_client *client;
3576 unsigned long timestamp;
3577};
3578
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579/*
3580 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3581 * standalone procedure for queueing an asynchronous RENEW.
3582 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003583static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003584{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003585 struct nfs4_renewdata *data = calldata;
3586 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003587
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003588 if (atomic_read(&clp->cl_count) > 1)
3589 nfs4_schedule_state_renewal(clp);
3590 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003591 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003592}
3593
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003594static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003596 struct nfs4_renewdata *data = calldata;
3597 struct nfs_client *clp = data->client;
3598 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599
3600 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003601 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003602 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3603 return;
3604 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003605 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003606 return;
3607 }
3608 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003610 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611}
3612
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003613static const struct rpc_call_ops nfs4_renew_ops = {
3614 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003615 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003616};
3617
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003618static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619{
3620 struct rpc_message msg = {
3621 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3622 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003623 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003625 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003627 if (renew_flags == 0)
3628 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003629 if (!atomic_inc_not_zero(&clp->cl_count))
3630 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003631 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003632 if (data == NULL)
3633 return -ENOMEM;
3634 data->client = clp;
3635 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003637 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638}
3639
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003640static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641{
3642 struct rpc_message msg = {
3643 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3644 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003645 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 };
3647 unsigned long now = jiffies;
3648 int status;
3649
3650 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3651 if (status < 0)
3652 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003653 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654 return 0;
3655}
3656
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003657static inline int nfs4_server_supports_acls(struct nfs_server *server)
3658{
3659 return (server->caps & NFS_CAP_ACLS)
3660 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3661 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3662}
3663
Trond Myklebust21f498c2012-08-24 10:59:25 -04003664/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3665 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003666 * the stack.
3667 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04003668#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003669
Neil Hormane9e3d722011-03-04 19:26:03 -05003670static int buf_to_pages_noslab(const void *buf, size_t buflen,
3671 struct page **pages, unsigned int *pgbase)
3672{
3673 struct page *newpage, **spages;
3674 int rc = 0;
3675 size_t len;
3676 spages = pages;
3677
3678 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04003679 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003680 newpage = alloc_page(GFP_KERNEL);
3681
3682 if (newpage == NULL)
3683 goto unwind;
3684 memcpy(page_address(newpage), buf, len);
3685 buf += len;
3686 buflen -= len;
3687 *pages++ = newpage;
3688 rc++;
3689 } while (buflen != 0);
3690
3691 return rc;
3692
3693unwind:
3694 for(; rc > 0; rc--)
3695 __free_page(spages[rc-1]);
3696 return -ENOMEM;
3697}
3698
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003699struct nfs4_cached_acl {
3700 int cached;
3701 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003702 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003703};
3704
3705static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003706{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003707 struct nfs_inode *nfsi = NFS_I(inode);
3708
3709 spin_lock(&inode->i_lock);
3710 kfree(nfsi->nfs4_acl);
3711 nfsi->nfs4_acl = acl;
3712 spin_unlock(&inode->i_lock);
3713}
3714
3715static void nfs4_zap_acl_attr(struct inode *inode)
3716{
3717 nfs4_set_cached_acl(inode, NULL);
3718}
3719
3720static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3721{
3722 struct nfs_inode *nfsi = NFS_I(inode);
3723 struct nfs4_cached_acl *acl;
3724 int ret = -ENOENT;
3725
3726 spin_lock(&inode->i_lock);
3727 acl = nfsi->nfs4_acl;
3728 if (acl == NULL)
3729 goto out;
3730 if (buf == NULL) /* user is just asking for length */
3731 goto out_len;
3732 if (acl->cached == 0)
3733 goto out;
3734 ret = -ERANGE; /* see getxattr(2) man page */
3735 if (acl->len > buflen)
3736 goto out;
3737 memcpy(buf, acl->data, acl->len);
3738out_len:
3739 ret = acl->len;
3740out:
3741 spin_unlock(&inode->i_lock);
3742 return ret;
3743}
3744
Sachin Prabhu5794d212012-04-17 14:36:40 +01003745static 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 +00003746{
3747 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003748 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003749
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003750 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003751 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003752 if (acl == NULL)
3753 goto out;
3754 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003755 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003756 } else {
3757 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3758 if (acl == NULL)
3759 goto out;
3760 acl->cached = 0;
3761 }
3762 acl->len = acl_len;
3763out:
3764 nfs4_set_cached_acl(inode, acl);
3765}
3766
Andy Adamsonbf118a32011-12-07 11:55:27 -05003767/*
3768 * The getxattr API returns the required buffer length when called with a
3769 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3770 * the required buf. On a NULL buf, we send a page of data to the server
3771 * guessing that the ACL request can be serviced by a page. If so, we cache
3772 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3773 * the cache. If not so, we throw away the page, and cache the required
3774 * length. The next getxattr call will then produce another round trip to
3775 * the server, this time with the input buf of the required size.
3776 */
Trond Myklebust16b42892006-08-24 12:27:15 -04003777static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003778{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003779 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003780 struct nfs_getaclargs args = {
3781 .fh = NFS_FH(inode),
3782 .acl_pages = pages,
3783 .acl_len = buflen,
3784 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003785 struct nfs_getaclres res = {
3786 .acl_len = buflen,
3787 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003788 struct rpc_message msg = {
3789 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3790 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003791 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003792 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003793 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3794 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003795
Andy Adamsonbf118a32011-12-07 11:55:27 -05003796 /* As long as we're doing a round trip to the server anyway,
3797 * let's be prepared for a page of acl data. */
3798 if (npages == 0)
3799 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003800 if (npages > ARRAY_SIZE(pages))
3801 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003802
Andy Adamsonbf118a32011-12-07 11:55:27 -05003803 for (i = 0; i < npages; i++) {
3804 pages[i] = alloc_page(GFP_KERNEL);
3805 if (!pages[i])
3806 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003807 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01003808
3809 /* for decoding across pages */
3810 res.acl_scratch = alloc_page(GFP_KERNEL);
3811 if (!res.acl_scratch)
3812 goto out_free;
3813
Andy Adamsonbf118a32011-12-07 11:55:27 -05003814 args.acl_len = npages * PAGE_SIZE;
3815 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003816
Peng Taode040be2012-01-10 22:42:47 +08003817 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003818 __func__, buf, buflen, npages, args.acl_len);
3819 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3820 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003821 if (ret)
3822 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003823
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003824 /* Handle the case where the passed-in buffer is too short */
3825 if (res.acl_flags & NFS4_ACL_TRUNC) {
3826 /* Did the user only issue a request for the acl length? */
3827 if (buf == NULL)
3828 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003829 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003830 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003831 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003832 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
3833 if (buf)
3834 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
3835out_ok:
3836 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003837out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003838 for (i = 0; i < npages; i++)
3839 if (pages[i])
3840 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05003841 if (res.acl_scratch)
3842 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003843 return ret;
3844}
3845
Trond Myklebust16b42892006-08-24 12:27:15 -04003846static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3847{
3848 struct nfs4_exception exception = { };
3849 ssize_t ret;
3850 do {
3851 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3852 if (ret >= 0)
3853 break;
3854 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3855 } while (exception.retry);
3856 return ret;
3857}
3858
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003859static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3860{
3861 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003862 int ret;
3863
3864 if (!nfs4_server_supports_acls(server))
3865 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003866 ret = nfs_revalidate_inode(server, inode);
3867 if (ret < 0)
3868 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003869 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3870 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003871 ret = nfs4_read_cached_acl(inode, buf, buflen);
3872 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003873 /* -ENOENT is returned if there is no ACL or if there is an ACL
3874 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003875 return ret;
3876 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003877}
3878
Trond Myklebust16b42892006-08-24 12:27:15 -04003879static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003880{
3881 struct nfs_server *server = NFS_SERVER(inode);
3882 struct page *pages[NFS4ACL_MAXPAGES];
3883 struct nfs_setaclargs arg = {
3884 .fh = NFS_FH(inode),
3885 .acl_pages = pages,
3886 .acl_len = buflen,
3887 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003888 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003889 struct rpc_message msg = {
3890 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3891 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003892 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003893 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003894 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05003895 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003896
3897 if (!nfs4_server_supports_acls(server))
3898 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003899 if (npages > ARRAY_SIZE(pages))
3900 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05003901 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3902 if (i < 0)
3903 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04003904 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003905 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003906
3907 /*
3908 * Free each page after tx, so the only ref left is
3909 * held by the network stack
3910 */
3911 for (; i > 0; i--)
3912 put_page(pages[i-1]);
3913
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003914 /*
3915 * Acl update can result in inode attribute update.
3916 * so mark the attribute cache invalid.
3917 */
3918 spin_lock(&inode->i_lock);
3919 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3920 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003921 nfs_access_zap_cache(inode);
3922 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003923 return ret;
3924}
3925
Trond Myklebust16b42892006-08-24 12:27:15 -04003926static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3927{
3928 struct nfs4_exception exception = { };
3929 int err;
3930 do {
3931 err = nfs4_handle_exception(NFS_SERVER(inode),
3932 __nfs4_proc_set_acl(inode, buf, buflen),
3933 &exception);
3934 } while (exception.retry);
3935 return err;
3936}
3937
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003939nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003941 struct nfs_client *clp = server->nfs_client;
3942
3943 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 return 0;
3945 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05003946 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003947 case -NFS4ERR_ADMIN_REVOKED:
3948 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04003949 if (state == NULL)
3950 break;
3951 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003952 case -NFS4ERR_OPENMODE:
3953 if (state == NULL)
3954 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003955 nfs4_schedule_stateid_recovery(server, state);
3956 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003957 case -NFS4ERR_EXPIRED:
3958 if (state != NULL)
3959 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003961 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003962 nfs4_schedule_lease_recovery(clp);
3963 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003964#if defined(CONFIG_NFS_V4_1)
3965 case -NFS4ERR_BADSESSION:
3966 case -NFS4ERR_BADSLOT:
3967 case -NFS4ERR_BAD_HIGH_SLOT:
3968 case -NFS4ERR_DEADSESSION:
3969 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3970 case -NFS4ERR_SEQ_FALSE_RETRY:
3971 case -NFS4ERR_SEQ_MISORDERED:
3972 dprintk("%s ERROR %d, Reset session\n", __func__,
3973 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04003974 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04003975 task->tk_status = 0;
3976 return -EAGAIN;
3977#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003979 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003980 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003981 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3983 task->tk_status = 0;
3984 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04003985 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986 case -NFS4ERR_OLD_STATEID:
3987 task->tk_status = 0;
3988 return -EAGAIN;
3989 }
3990 task->tk_status = nfs4_map_errors(task->tk_status);
3991 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003992wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003993 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003994 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3995 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3996 task->tk_status = 0;
3997 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998}
3999
Chuck Leverf0920752012-05-21 22:45:41 -04004000static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4001 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004002{
4003 __be32 verf[2];
4004
Chuck Lever2c820d92012-05-21 22:45:33 -04004005 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4006 /* An impossible timestamp guarantees this value
4007 * will never match a generated boot time. */
4008 verf[0] = 0;
4009 verf[1] = (__be32)(NSEC_PER_SEC + 1);
4010 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004011 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4012 verf[0] = (__be32)nn->boot_time.tv_sec;
4013 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04004014 }
Chuck Levercd937102012-03-02 17:14:31 -05004015 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4016}
4017
Chuck Levere984a552012-09-14 17:24:21 -04004018static unsigned int
4019nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4020 char *buf, size_t len)
4021{
4022 unsigned int result;
4023
4024 rcu_read_lock();
4025 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4026 clp->cl_ipaddr,
4027 rpc_peeraddr2str(clp->cl_rpcclient,
4028 RPC_DISPLAY_ADDR),
4029 rpc_peeraddr2str(clp->cl_rpcclient,
4030 RPC_DISPLAY_PROTO));
4031 rcu_read_unlock();
4032 return result;
4033}
4034
4035static unsigned int
4036nfs4_init_uniform_client_string(const struct nfs_client *clp,
4037 char *buf, size_t len)
4038{
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004039 char *nodename = clp->cl_rpcclient->cl_nodename;
4040
4041 if (nfs4_client_id_uniquifier[0] != '\0')
4042 nodename = nfs4_client_id_uniquifier;
Chuck Levere984a552012-09-14 17:24:21 -04004043 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4044 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004045 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004046}
4047
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004048/**
4049 * nfs4_proc_setclientid - Negotiate client ID
4050 * @clp: state data structure
4051 * @program: RPC program for NFSv4 callback service
4052 * @port: IP port number for NFS4 callback service
4053 * @cred: RPC credential to use for this call
4054 * @res: where to place the result
4055 *
4056 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4057 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004058int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4059 unsigned short port, struct rpc_cred *cred,
4060 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061{
4062 nfs4_verifier sc_verifier;
4063 struct nfs4_setclientid setclientid = {
4064 .sc_verifier = &sc_verifier,
4065 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004066 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067 };
4068 struct rpc_message msg = {
4069 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4070 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004071 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004072 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004074 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075
Chuck Leverde734832012-07-11 16:30:50 -04004076 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004077 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004078 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4079 setclientid.sc_name_len =
4080 nfs4_init_uniform_client_string(clp,
4081 setclientid.sc_name,
4082 sizeof(setclientid.sc_name));
4083 else
4084 setclientid.sc_name_len =
4085 nfs4_init_nonuniform_client_string(clp,
4086 setclientid.sc_name,
4087 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004088 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004089 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004090 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004091 sizeof(setclientid.sc_netid),
4092 rpc_peeraddr2str(clp->cl_rpcclient,
4093 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004094 rcu_read_unlock();
4095 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004096 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097 clp->cl_ipaddr, port >> 8, port & 255);
4098
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004099 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4100 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4101 setclientid.sc_name_len, setclientid.sc_name);
4102 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4103 dprintk("NFS reply setclientid: %d\n", status);
4104 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105}
4106
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004107/**
4108 * nfs4_proc_setclientid_confirm - Confirm client ID
4109 * @clp: state data structure
4110 * @res: result of a previous SETCLIENTID
4111 * @cred: RPC credential to use for this call
4112 *
4113 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4114 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004115int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004116 struct nfs4_setclientid_res *arg,
4117 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118{
4119 struct nfs_fsinfo fsinfo;
4120 struct rpc_message msg = {
4121 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004122 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004124 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125 };
4126 unsigned long now;
4127 int status;
4128
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004129 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4130 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4131 clp->cl_clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004133 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 if (status == 0) {
4135 spin_lock(&clp->cl_lock);
4136 clp->cl_lease_time = fsinfo.lease_time * HZ;
4137 clp->cl_last_renewal = now;
4138 spin_unlock(&clp->cl_lock);
4139 }
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004140 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 return status;
4142}
4143
Trond Myklebustfe650402006-01-03 09:55:18 +01004144struct nfs4_delegreturndata {
4145 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004146 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004147 struct nfs_fh fh;
4148 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004149 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004150 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004151 int rpc_status;
4152};
4153
Trond Myklebustfe650402006-01-03 09:55:18 +01004154static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4155{
4156 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004157
Trond Myklebust14516c32010-07-31 14:29:06 -04004158 if (!nfs4_sequence_done(task, &data->res.seq_res))
4159 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004160
Ricardo Labiaga79708862009-12-07 09:23:21 -05004161 switch (task->tk_status) {
4162 case -NFS4ERR_STALE_STATEID:
4163 case -NFS4ERR_EXPIRED:
4164 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004165 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004166 break;
4167 default:
4168 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4169 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004170 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004171 return;
4172 }
4173 }
4174 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004175}
4176
4177static void nfs4_delegreturn_release(void *calldata)
4178{
Trond Myklebustfe650402006-01-03 09:55:18 +01004179 kfree(calldata);
4180}
4181
Andy Adamson938e1012009-04-01 09:22:28 -04004182#if defined(CONFIG_NFS_V4_1)
4183static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4184{
4185 struct nfs4_delegreturndata *d_data;
4186
4187 d_data = (struct nfs4_delegreturndata *)data;
4188
Trond Myklebust035168ab2010-06-16 09:52:26 -04004189 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04004190 &d_data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004191 &d_data->res.seq_res, task))
Andy Adamson938e1012009-04-01 09:22:28 -04004192 return;
4193 rpc_call_start(task);
4194}
4195#endif /* CONFIG_NFS_V4_1 */
4196
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004197static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004198#if defined(CONFIG_NFS_V4_1)
4199 .rpc_call_prepare = nfs4_delegreturn_prepare,
4200#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004201 .rpc_call_done = nfs4_delegreturn_done,
4202 .rpc_release = nfs4_delegreturn_release,
4203};
4204
Trond Myklebuste6f81072008-01-24 18:14:34 -05004205static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004206{
4207 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004208 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004209 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004210 struct rpc_message msg = {
4211 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4212 .rpc_cred = cred,
4213 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004214 struct rpc_task_setup task_setup_data = {
4215 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004216 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004217 .callback_ops = &nfs4_delegreturn_ops,
4218 .flags = RPC_TASK_ASYNC,
4219 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004220 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004221
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004222 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004223 if (data == NULL)
4224 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004225 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004226 data->args.fhandle = &data->fh;
4227 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004228 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004229 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004230 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004231 data->res.fattr = &data->fattr;
4232 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004233 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004234 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004235 data->rpc_status = 0;
4236
Trond Myklebustc970aa82007-07-14 15:39:59 -04004237 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004238 msg.rpc_argp = &data->args;
4239 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004240 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004241 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004242 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004243 if (!issync)
4244 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004245 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004246 if (status != 0)
4247 goto out;
4248 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004249 if (status == 0)
4250 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4251 else
4252 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004253out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004254 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004255 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256}
4257
Trond Myklebuste6f81072008-01-24 18:14:34 -05004258int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259{
4260 struct nfs_server *server = NFS_SERVER(inode);
4261 struct nfs4_exception exception = { };
4262 int err;
4263 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004264 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 switch (err) {
4266 case -NFS4ERR_STALE_STATEID:
4267 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268 case 0:
4269 return 0;
4270 }
4271 err = nfs4_handle_exception(server, err, &exception);
4272 } while (exception.retry);
4273 return err;
4274}
4275
4276#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4277#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4278
4279/*
4280 * sleep, with exponential backoff, and retry the LOCK operation.
4281 */
4282static unsigned long
4283nfs4_set_lock_task_retry(unsigned long timeout)
4284{
Jeff Laytond3103102011-12-01 22:44:39 +01004285 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 timeout <<= 1;
4287 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4288 return NFS4_LOCK_MAXTIMEOUT;
4289 return timeout;
4290}
4291
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4293{
4294 struct inode *inode = state->inode;
4295 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004296 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004297 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004299 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004301 struct nfs_lockt_res res = {
4302 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 };
4304 struct rpc_message msg = {
4305 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4306 .rpc_argp = &arg,
4307 .rpc_resp = &res,
4308 .rpc_cred = state->owner->so_cred,
4309 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310 struct nfs4_lock_state *lsp;
4311 int status;
4312
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004313 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004314 status = nfs4_set_lock_state(state, request);
4315 if (status != 0)
4316 goto out;
4317 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004318 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004319 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004320 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004321 switch (status) {
4322 case 0:
4323 request->fl_type = F_UNLCK;
4324 break;
4325 case -NFS4ERR_DENIED:
4326 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004328 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004329out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 return status;
4331}
4332
4333static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4334{
4335 struct nfs4_exception exception = { };
4336 int err;
4337
4338 do {
4339 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4340 _nfs4_proc_getlk(state, cmd, request),
4341 &exception);
4342 } while (exception.retry);
4343 return err;
4344}
4345
4346static int do_vfs_lock(struct file *file, struct file_lock *fl)
4347{
4348 int res = 0;
4349 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4350 case FL_POSIX:
4351 res = posix_lock_file_wait(file, fl);
4352 break;
4353 case FL_FLOCK:
4354 res = flock_lock_file_wait(file, fl);
4355 break;
4356 default:
4357 BUG();
4358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359 return res;
4360}
4361
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004362struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004363 struct nfs_locku_args arg;
4364 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004365 struct nfs4_lock_state *lsp;
4366 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004367 struct file_lock fl;
4368 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004369 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004370};
4371
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004372static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4373 struct nfs_open_context *ctx,
4374 struct nfs4_lock_state *lsp,
4375 struct nfs_seqid *seqid)
4376{
4377 struct nfs4_unlockdata *p;
4378 struct inode *inode = lsp->ls_state->inode;
4379
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004380 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004381 if (p == NULL)
4382 return NULL;
4383 p->arg.fh = NFS_FH(inode);
4384 p->arg.fl = &p->fl;
4385 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004386 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004387 p->arg.stateid = &lsp->ls_stateid;
4388 p->lsp = lsp;
4389 atomic_inc(&lsp->ls_count);
4390 /* Ensure we don't close file until we're done freeing locks! */
4391 p->ctx = get_nfs_open_context(ctx);
4392 memcpy(&p->fl, fl, sizeof(p->fl));
4393 p->server = NFS_SERVER(inode);
4394 return p;
4395}
4396
Trond Myklebust06f814a2006-01-03 09:55:07 +01004397static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004398{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004399 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004400 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004401 nfs4_put_lock_state(calldata->lsp);
4402 put_nfs_open_context(calldata->ctx);
4403 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004404}
4405
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004406static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004407{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004408 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004409
Trond Myklebust14516c32010-07-31 14:29:06 -04004410 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4411 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004412 switch (task->tk_status) {
4413 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004414 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4415 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004416 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004417 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004418 case -NFS4ERR_BAD_STATEID:
4419 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004420 case -NFS4ERR_STALE_STATEID:
4421 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004422 break;
4423 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004424 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004425 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004426 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04004427 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004428}
4429
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004430static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004431{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004432 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004434 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004435 return;
Trond Myklebust795a88c2012-09-10 13:26:49 -04004436 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004437 /* Note: exit _without_ running nfs4_locku_done */
4438 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004439 return;
4440 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004441 calldata->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04004442 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004443 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004444 &calldata->res.seq_res,
4445 task) != 0)
4446 nfs_release_seqid(calldata->arg.seqid);
4447 else
4448 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449}
4450
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004451static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004452 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004453 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004454 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004455};
4456
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004457static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4458 struct nfs_open_context *ctx,
4459 struct nfs4_lock_state *lsp,
4460 struct nfs_seqid *seqid)
4461{
4462 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004463 struct rpc_message msg = {
4464 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4465 .rpc_cred = ctx->cred,
4466 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004467 struct rpc_task_setup task_setup_data = {
4468 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004469 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004470 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004471 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004472 .flags = RPC_TASK_ASYNC,
4473 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004474
Frank Filz137d6ac2007-07-09 15:32:29 -07004475 /* Ensure this is an unlock - when canceling a lock, the
4476 * canceled lock is passed in, and it won't be an unlock.
4477 */
4478 fl->fl_type = F_UNLCK;
4479
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004480 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4481 if (data == NULL) {
4482 nfs_free_seqid(seqid);
4483 return ERR_PTR(-ENOMEM);
4484 }
4485
Trond Myklebust9d12b212012-01-17 22:04:25 -05004486 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004487 msg.rpc_argp = &data->arg;
4488 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004489 task_setup_data.callback_data = data;
4490 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004491}
4492
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4494{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004495 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004496 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004497 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004498 struct rpc_task *task;
4499 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004500 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501
Trond Myklebust9b073572006-06-29 16:38:34 -04004502 status = nfs4_set_lock_state(state, request);
4503 /* Unlock _before_ we do the RPC call */
4504 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004505 down_read(&nfsi->rwsem);
4506 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4507 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004508 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004509 }
4510 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004511 if (status != 0)
4512 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004513 /* Is this a delegated lock? */
4514 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004515 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004516 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004517 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004518 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004519 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004520 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004521 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004522 status = PTR_ERR(task);
4523 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004524 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004525 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004526 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004527out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004528 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004529 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530}
4531
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004532struct nfs4_lockdata {
4533 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004534 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004535 struct nfs4_lock_state *lsp;
4536 struct nfs_open_context *ctx;
4537 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004538 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004539 int rpc_status;
4540 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004541 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004542};
4543
4544static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004545 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4546 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004547{
4548 struct nfs4_lockdata *p;
4549 struct inode *inode = lsp->ls_state->inode;
4550 struct nfs_server *server = NFS_SERVER(inode);
4551
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004552 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004553 if (p == NULL)
4554 return NULL;
4555
4556 p->arg.fh = NFS_FH(inode);
4557 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004558 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004559 if (p->arg.open_seqid == NULL)
4560 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004561 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004562 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004563 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004564 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004565 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004566 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004567 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004568 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004569 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004570 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004571 atomic_inc(&lsp->ls_count);
4572 p->ctx = get_nfs_open_context(ctx);
4573 memcpy(&p->fl, fl, sizeof(p->fl));
4574 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004575out_free_seqid:
4576 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004577out_free:
4578 kfree(p);
4579 return NULL;
4580}
4581
4582static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4583{
4584 struct nfs4_lockdata *data = calldata;
4585 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586
Harvey Harrison3110ff82008-05-02 13:42:44 -07004587 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004588 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4589 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004590 /* Do we need to do an open_to_lock_owner? */
4591 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004592 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004593 goto out_release_lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004594 data->arg.open_stateid = &state->stateid;
4595 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004596 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004597 } else
4598 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004599 data->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04004600 if (nfs4_setup_sequence(data->server,
4601 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004602 &data->res.seq_res,
4603 task) == 0) {
4604 rpc_call_start(task);
Andy Adamson66179ef2009-04-01 09:22:22 -04004605 return;
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004606 }
4607 nfs_release_seqid(data->arg.open_seqid);
4608out_release_lock_seqid:
4609 nfs_release_seqid(data->arg.lock_seqid);
4610 dprintk("%s: done!, ret = %d\n", __func__, task->tk_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004611}
4612
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004613static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4614{
4615 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4616 nfs4_lock_prepare(task, calldata);
4617}
4618
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004619static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4620{
4621 struct nfs4_lockdata *data = calldata;
4622
Harvey Harrison3110ff82008-05-02 13:42:44 -07004623 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004624
Trond Myklebust14516c32010-07-31 14:29:06 -04004625 if (!nfs4_sequence_done(task, &data->res.seq_res))
4626 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004627
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004628 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004629 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004630 if (data->rpc_status == 0)
4631 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4632 else
4633 goto out;
4634 }
4635 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004636 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004637 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04004638 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004639 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004640out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004641 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004642}
4643
4644static void nfs4_lock_release(void *calldata)
4645{
4646 struct nfs4_lockdata *data = calldata;
4647
Harvey Harrison3110ff82008-05-02 13:42:44 -07004648 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004649 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004650 if (data->cancelled != 0) {
4651 struct rpc_task *task;
4652 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4653 data->arg.lock_seqid);
4654 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004655 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004656 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004657 } else
4658 nfs_free_seqid(data->arg.lock_seqid);
4659 nfs4_put_lock_state(data->lsp);
4660 put_nfs_open_context(data->ctx);
4661 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004662 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004663}
4664
4665static const struct rpc_call_ops nfs4_lock_ops = {
4666 .rpc_call_prepare = nfs4_lock_prepare,
4667 .rpc_call_done = nfs4_lock_done,
4668 .rpc_release = nfs4_lock_release,
4669};
4670
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004671static const struct rpc_call_ops nfs4_recover_lock_ops = {
4672 .rpc_call_prepare = nfs4_recover_lock_prepare,
4673 .rpc_call_done = nfs4_lock_done,
4674 .rpc_release = nfs4_lock_release,
4675};
4676
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004677static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4678{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004679 switch (error) {
4680 case -NFS4ERR_ADMIN_REVOKED:
4681 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004682 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004683 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04004684 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004685 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004686 break;
4687 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004688 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004689 case -NFS4ERR_EXPIRED:
4690 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004691 };
4692}
4693
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004694static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004695{
4696 struct nfs4_lockdata *data;
4697 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004698 struct rpc_message msg = {
4699 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4700 .rpc_cred = state->owner->so_cred,
4701 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004702 struct rpc_task_setup task_setup_data = {
4703 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004704 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004705 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004706 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004707 .flags = RPC_TASK_ASYNC,
4708 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004709 int ret;
4710
Harvey Harrison3110ff82008-05-02 13:42:44 -07004711 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004712 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004713 fl->fl_u.nfs4_fl.owner,
4714 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004715 if (data == NULL)
4716 return -ENOMEM;
4717 if (IS_SETLKW(cmd))
4718 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004719 if (recovery_type > NFS_LOCK_NEW) {
4720 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004721 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004722 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4723 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05004724 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004725 msg.rpc_argp = &data->arg;
4726 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004727 task_setup_data.callback_data = data;
4728 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004729 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004730 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004731 ret = nfs4_wait_for_completion_rpc_task(task);
4732 if (ret == 0) {
4733 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004734 if (ret)
4735 nfs4_handle_setlk_error(data->server, data->lsp,
4736 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004737 } else
4738 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004739 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004740 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004741 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742}
4743
4744static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4745{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004746 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004747 struct nfs4_exception exception = {
4748 .inode = state->inode,
4749 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004750 int err;
4751
4752 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004753 /* Cache the lock if possible... */
4754 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4755 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004756 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004757 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004758 break;
4759 nfs4_handle_exception(server, err, &exception);
4760 } while (exception.retry);
4761 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762}
4763
4764static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4765{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004766 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004767 struct nfs4_exception exception = {
4768 .inode = state->inode,
4769 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004770 int err;
4771
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004772 err = nfs4_set_lock_state(state, request);
4773 if (err != 0)
4774 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004775 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004776 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4777 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004778 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004779 switch (err) {
4780 default:
4781 goto out;
4782 case -NFS4ERR_GRACE:
4783 case -NFS4ERR_DELAY:
4784 nfs4_handle_exception(server, err, &exception);
4785 err = 0;
4786 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004787 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004788out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004789 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790}
4791
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004792#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004793/**
4794 * nfs41_check_expired_locks - possibly free a lock stateid
4795 *
4796 * @state: NFSv4 state for an inode
4797 *
4798 * Returns NFS_OK if recovery for this stateid is now finished.
4799 * Otherwise a negative NFS4ERR value is returned.
4800 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004801static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004802{
Chuck Levereb64cf92012-07-11 16:30:05 -04004803 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004804 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004805 struct nfs_server *server = NFS_SERVER(state->inode);
4806
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004807 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04004808 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004809 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4810 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004811 /* Free the stateid unless the server
4812 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04004813 if (status != -NFS4ERR_BAD_STATEID)
4814 nfs41_free_stateid(server,
4815 &lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004816 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004817 ret = status;
4818 }
4819 }
4820 };
4821
4822 return ret;
4823}
4824
4825static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4826{
4827 int status = NFS_OK;
4828
4829 if (test_bit(LK_STATE_IN_USE, &state->flags))
4830 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04004831 if (status != NFS_OK)
4832 status = nfs4_lock_expired(state, request);
4833 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004834}
4835#endif
4836
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4838{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004839 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004840 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004841 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004843 if ((fl_flags & FL_POSIX) &&
4844 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4845 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004846 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004847 status = nfs4_set_lock_state(state, request);
4848 if (status != 0)
4849 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004850 request->fl_flags |= FL_ACCESS;
4851 status = do_vfs_lock(request->fl_file, request);
4852 if (status < 0)
4853 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004854 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004855 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004856 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004857 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004858 request->fl_flags = fl_flags & ~FL_SLEEP;
4859 status = do_vfs_lock(request->fl_file, request);
4860 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004861 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004862 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004863 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004864 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004865 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004866 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004867 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004868 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4869 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004870out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004871 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004872out:
4873 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874 return status;
4875}
4876
4877static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4878{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004879 struct nfs4_exception exception = {
4880 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04004881 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004882 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883 int err;
4884
4885 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004886 err = _nfs4_proc_setlk(state, cmd, request);
4887 if (err == -NFS4ERR_DENIED)
4888 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004890 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891 } while (exception.retry);
4892 return err;
4893}
4894
4895static int
4896nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4897{
4898 struct nfs_open_context *ctx;
4899 struct nfs4_state *state;
4900 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4901 int status;
4902
4903 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004904 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905 state = ctx->state;
4906
4907 if (request->fl_start < 0 || request->fl_end < 0)
4908 return -EINVAL;
4909
Trond Myklebustd9531262009-07-21 19:22:38 -04004910 if (IS_GETLK(cmd)) {
4911 if (state != NULL)
4912 return nfs4_proc_getlk(state, F_GETLK, request);
4913 return 0;
4914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915
4916 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4917 return -EINVAL;
4918
Trond Myklebustd9531262009-07-21 19:22:38 -04004919 if (request->fl_type == F_UNLCK) {
4920 if (state != NULL)
4921 return nfs4_proc_unlck(state, cmd, request);
4922 return 0;
4923 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924
Trond Myklebustd9531262009-07-21 19:22:38 -04004925 if (state == NULL)
4926 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04004927 /*
4928 * Don't rely on the VFS having checked the file open mode,
4929 * since it won't do this for flock() locks.
4930 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04004931 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04004932 case F_RDLCK:
4933 if (!(filp->f_mode & FMODE_READ))
4934 return -EBADF;
4935 break;
4936 case F_WRLCK:
4937 if (!(filp->f_mode & FMODE_WRITE))
4938 return -EBADF;
4939 }
4940
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941 do {
4942 status = nfs4_proc_setlk(state, cmd, request);
4943 if ((status != -EAGAIN) || IS_SETLK(cmd))
4944 break;
4945 timeout = nfs4_set_lock_task_retry(timeout);
4946 status = -ERESTARTSYS;
4947 if (signalled())
4948 break;
4949 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950 return status;
4951}
4952
Trond Myklebust888e6942005-11-04 15:38:11 -05004953int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4954{
4955 struct nfs_server *server = NFS_SERVER(state->inode);
4956 struct nfs4_exception exception = { };
4957 int err;
4958
4959 err = nfs4_set_lock_state(state, fl);
4960 if (err != 0)
4961 goto out;
4962 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004963 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004964 switch (err) {
4965 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004966 printk(KERN_ERR "NFS: %s: unhandled error "
4967 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004968 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004969 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004970 goto out;
4971 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004972 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07004973 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004974 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004975 nfs4_schedule_lease_recovery(server->nfs_client);
4976 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004977 case -NFS4ERR_BADSESSION:
4978 case -NFS4ERR_BADSLOT:
4979 case -NFS4ERR_BAD_HIGH_SLOT:
4980 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4981 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04004982 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004983 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004984 case -ERESTARTSYS:
4985 /*
4986 * The show must go on: exit, but mark the
4987 * stateid as needing recovery.
4988 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004989 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004990 case -NFS4ERR_ADMIN_REVOKED:
4991 case -NFS4ERR_BAD_STATEID:
4992 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004993 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004994 err = 0;
4995 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004996 case -EKEYEXPIRED:
4997 /*
4998 * User RPCSEC_GSS context has expired.
4999 * We cannot recover this stateid now, so
5000 * skip it and allow recovery thread to
5001 * proceed.
5002 */
5003 err = 0;
5004 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07005005 case -ENOMEM:
5006 case -NFS4ERR_DENIED:
5007 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
5008 err = 0;
5009 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07005010 case -NFS4ERR_DELAY:
5011 break;
5012 }
Trond Myklebust888e6942005-11-04 15:38:11 -05005013 err = nfs4_handle_exception(server, err, &exception);
5014 } while (exception.retry);
5015out:
5016 return err;
5017}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005018
Trond Myklebustcf470c32012-03-07 13:49:12 -05005019struct nfs_release_lockowner_data {
5020 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005021 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005022 struct nfs_release_lockowner_args args;
5023};
5024
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005025static void nfs4_release_lockowner_release(void *calldata)
5026{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005027 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005028 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005029 kfree(calldata);
5030}
5031
Trond Myklebust17280172012-03-11 13:11:00 -04005032static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005033 .rpc_release = nfs4_release_lockowner_release,
5034};
5035
Trond Myklebustcf470c32012-03-07 13:49:12 -05005036int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005037{
5038 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005039 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005040 struct rpc_message msg = {
5041 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5042 };
5043
5044 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005045 return -EINVAL;
5046 data = kmalloc(sizeof(*data), GFP_NOFS);
5047 if (!data)
5048 return -ENOMEM;
5049 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005050 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005051 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5052 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5053 data->args.lock_owner.s_dev = server->s_dev;
5054 msg.rpc_argp = &data->args;
5055 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5056 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005057}
5058
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005059#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5060
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005061static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5062 const void *buf, size_t buflen,
5063 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005064{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005065 if (strcmp(key, "") != 0)
5066 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005067
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005068 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005069}
5070
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005071static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5072 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005073{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005074 if (strcmp(key, "") != 0)
5075 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005076
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005077 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005078}
5079
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005080static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5081 size_t list_len, const char *name,
5082 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005083{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005084 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005085
J. Bruce Fields096455a2006-03-20 23:23:42 -05005086 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5087 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005088
5089 if (list && len <= list_len)
5090 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005091 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005092}
5093
Andy Adamson533eb462011-06-13 18:25:56 -04005094/*
5095 * nfs_fhget will use either the mounted_on_fileid or the fileid
5096 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005097static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5098{
Andy Adamson533eb462011-06-13 18:25:56 -04005099 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5100 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5101 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005102 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005103 return;
5104
5105 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005106 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005107 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5108 fattr->nlink = 2;
5109}
5110
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005111static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5112 const struct qstr *name,
5113 struct nfs4_fs_locations *fs_locations,
5114 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005115{
5116 struct nfs_server *server = NFS_SERVER(dir);
5117 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005118 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005119 };
5120 struct nfs4_fs_locations_arg args = {
5121 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005122 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005123 .page = page,
5124 .bitmask = bitmask,
5125 };
Benny Halevy22958462009-04-01 09:22:02 -04005126 struct nfs4_fs_locations_res res = {
5127 .fs_locations = fs_locations,
5128 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005129 struct rpc_message msg = {
5130 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5131 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005132 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005133 };
5134 int status;
5135
Harvey Harrison3110ff82008-05-02 13:42:44 -07005136 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005137
5138 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5139 * is not supported */
5140 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5141 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5142 else
5143 bitmask[0] |= FATTR4_WORD0_FILEID;
5144
Trond Myklebustc228fd32007-01-13 02:28:11 -05005145 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005146 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005147 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005148 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005149 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005150 return status;
5151}
5152
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005153int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5154 const struct qstr *name,
5155 struct nfs4_fs_locations *fs_locations,
5156 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005157{
5158 struct nfs4_exception exception = { };
5159 int err;
5160 do {
5161 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005162 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005163 &exception);
5164 } while (exception.retry);
5165 return err;
5166}
5167
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005168static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5169{
5170 int status;
5171 struct nfs4_secinfo_arg args = {
5172 .dir_fh = NFS_FH(dir),
5173 .name = name,
5174 };
5175 struct nfs4_secinfo_res res = {
5176 .flavors = flavors,
5177 };
5178 struct rpc_message msg = {
5179 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5180 .rpc_argp = &args,
5181 .rpc_resp = &res,
5182 };
5183
5184 dprintk("NFS call secinfo %s\n", name->name);
5185 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5186 dprintk("NFS reply secinfo: %d\n", status);
5187 return status;
5188}
5189
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005190int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5191 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005192{
5193 struct nfs4_exception exception = { };
5194 int err;
5195 do {
5196 err = nfs4_handle_exception(NFS_SERVER(dir),
5197 _nfs4_proc_secinfo(dir, name, flavors),
5198 &exception);
5199 } while (exception.retry);
5200 return err;
5201}
5202
Andy Adamson557134a2009-04-01 09:21:53 -04005203#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005204/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005205 * Check the exchange flags returned by the server for invalid flags, having
5206 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5207 * DS flags set.
5208 */
5209static int nfs4_check_cl_exchange_flags(u32 flags)
5210{
5211 if (flags & ~EXCHGID4_FLAG_MASK_R)
5212 goto out_inval;
5213 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5214 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5215 goto out_inval;
5216 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5217 goto out_inval;
5218 return NFS_OK;
5219out_inval:
5220 return -NFS4ERR_INVAL;
5221}
5222
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005223static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005224nfs41_same_server_scope(struct nfs41_server_scope *a,
5225 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005226{
5227 if (a->server_scope_sz == b->server_scope_sz &&
5228 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5229 return true;
5230
5231 return false;
5232}
5233
Andy Adamson357f54d2010-12-14 10:11:57 -05005234/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005235 * nfs4_proc_bind_conn_to_session()
5236 *
5237 * The 4.1 client currently uses the same TCP connection for the
5238 * fore and backchannel.
5239 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005240int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005241{
5242 int status;
5243 struct nfs41_bind_conn_to_session_res res;
5244 struct rpc_message msg = {
5245 .rpc_proc =
5246 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5247 .rpc_argp = clp,
5248 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005249 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005250 };
5251
5252 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005253
5254 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5255 if (unlikely(res.session == NULL)) {
5256 status = -ENOMEM;
5257 goto out;
5258 }
5259
5260 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5261 if (status == 0) {
5262 if (memcmp(res.session->sess_id.data,
5263 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5264 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5265 status = -EIO;
5266 goto out_session;
5267 }
5268 if (res.dir != NFS4_CDFS4_BOTH) {
5269 dprintk("NFS: %s: Unexpected direction from server\n",
5270 __func__);
5271 status = -EIO;
5272 goto out_session;
5273 }
5274 if (res.use_conn_in_rdma_mode) {
5275 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5276 __func__);
5277 status = -EIO;
5278 goto out_session;
5279 }
5280 }
5281out_session:
5282 kfree(res.session);
5283out:
5284 dprintk("<-- %s status= %d\n", __func__, status);
5285 return status;
5286}
5287
5288/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005289 * nfs4_proc_exchange_id()
5290 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005291 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5292 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04005293 * Since the clientid has expired, all compounds using sessions
5294 * associated with the stale clientid will be returning
5295 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5296 * be in some phase of session reset.
5297 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005298int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005299{
5300 nfs4_verifier verifier;
5301 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005302 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005303 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005304 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005305 };
5306 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005307 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005308 };
5309 int status;
5310 struct rpc_message msg = {
5311 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5312 .rpc_argp = &args,
5313 .rpc_resp = &res,
5314 .rpc_cred = cred,
5315 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005316
Chuck Leverf0920752012-05-21 22:45:41 -04005317 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04005318 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5319 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005320 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5321 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5322 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005323
Chuck Leveracdeb692012-05-21 22:46:16 -04005324 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005325 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005326 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005327 status = -ENOMEM;
5328 goto out;
5329 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005330
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005331 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005332 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005333 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005334 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005335 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005336 }
5337
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005338 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005339 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005340 status = -ENOMEM;
5341 goto out_server_scope;
5342 }
5343
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005344 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005345 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005346 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005347
Chuck Lever177313f2012-05-21 22:44:58 -04005348 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005349 clp->cl_clientid = res.clientid;
5350 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5351 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5352 clp->cl_seqid = res.seqid;
5353
Chuck Leveracdeb692012-05-21 22:46:16 -04005354 kfree(clp->cl_serverowner);
5355 clp->cl_serverowner = res.server_owner;
5356 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005357
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005358 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005359 kfree(clp->cl_implid);
5360 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005361
Chuck Lever177313f2012-05-21 22:44:58 -04005362 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005363 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005364 res.server_scope)) {
5365 dprintk("%s: server_scope mismatch detected\n",
5366 __func__);
5367 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005368 kfree(clp->cl_serverscope);
5369 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005370 }
5371
Chuck Lever177313f2012-05-21 22:44:58 -04005372 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005373 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005374 goto out;
5375 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005376 } else
5377 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005378
Chuck Leveracdeb692012-05-21 22:46:16 -04005379out_server_owner:
5380 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005381out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005382 kfree(res.server_scope);
5383out:
Chuck Lever177313f2012-05-21 22:44:58 -04005384 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005385 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005386 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005387 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04005388 clp->cl_implid->date.seconds,
5389 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005390 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005391 return status;
5392}
5393
Trond Myklebust66245532012-05-25 17:18:09 -04005394static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5395 struct rpc_cred *cred)
5396{
5397 struct rpc_message msg = {
5398 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5399 .rpc_argp = clp,
5400 .rpc_cred = cred,
5401 };
5402 int status;
5403
5404 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5405 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04005406 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04005407 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5408 return status;
5409}
5410
5411static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5412 struct rpc_cred *cred)
5413{
5414 unsigned int loop;
5415 int ret;
5416
5417 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5418 ret = _nfs4_proc_destroy_clientid(clp, cred);
5419 switch (ret) {
5420 case -NFS4ERR_DELAY:
5421 case -NFS4ERR_CLIENTID_BUSY:
5422 ssleep(1);
5423 break;
5424 default:
5425 return ret;
5426 }
5427 }
5428 return 0;
5429}
5430
5431int nfs4_destroy_clientid(struct nfs_client *clp)
5432{
5433 struct rpc_cred *cred;
5434 int ret = 0;
5435
5436 if (clp->cl_mvops->minor_version < 1)
5437 goto out;
5438 if (clp->cl_exchange_flags == 0)
5439 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04005440 if (clp->cl_preserve_clid)
5441 goto out;
Trond Myklebust66245532012-05-25 17:18:09 -04005442 cred = nfs4_get_exchange_id_cred(clp);
5443 ret = nfs4_proc_destroy_clientid(clp, cred);
5444 if (cred)
5445 put_rpccred(cred);
5446 switch (ret) {
5447 case 0:
5448 case -NFS4ERR_STALE_CLIENTID:
5449 clp->cl_exchange_flags = 0;
5450 }
5451out:
5452 return ret;
5453}
5454
Andy Adamson2050f0c2009-04-01 09:22:30 -04005455struct nfs4_get_lease_time_data {
5456 struct nfs4_get_lease_time_args *args;
5457 struct nfs4_get_lease_time_res *res;
5458 struct nfs_client *clp;
5459};
5460
5461static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5462 void *calldata)
5463{
5464 int ret;
5465 struct nfs4_get_lease_time_data *data =
5466 (struct nfs4_get_lease_time_data *)calldata;
5467
5468 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005469 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005470 /* just setup sequence, do not trigger session recovery
5471 since we're invoked within one */
5472 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005473 &data->args->la_seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005474 &data->res->lr_seq_res, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005475
Trond Myklebustd3edcf92012-10-15 13:14:43 -04005476 if (ret != -EAGAIN)
5477 rpc_call_start(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005478 dprintk("<-- %s\n", __func__);
5479}
5480
5481/*
5482 * Called from nfs4_state_manager thread for session setup, so don't recover
5483 * from sequence operation or clientid errors.
5484 */
5485static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5486{
5487 struct nfs4_get_lease_time_data *data =
5488 (struct nfs4_get_lease_time_data *)calldata;
5489
5490 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005491 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5492 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005493 switch (task->tk_status) {
5494 case -NFS4ERR_DELAY:
5495 case -NFS4ERR_GRACE:
5496 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5497 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5498 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005499 /* fall through */
5500 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005501 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005502 return;
5503 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005504 dprintk("<-- %s\n", __func__);
5505}
5506
Trond Myklebust17280172012-03-11 13:11:00 -04005507static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005508 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5509 .rpc_call_done = nfs4_get_lease_time_done,
5510};
5511
5512int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5513{
5514 struct rpc_task *task;
5515 struct nfs4_get_lease_time_args args;
5516 struct nfs4_get_lease_time_res res = {
5517 .lr_fsinfo = fsinfo,
5518 };
5519 struct nfs4_get_lease_time_data data = {
5520 .args = &args,
5521 .res = &res,
5522 .clp = clp,
5523 };
5524 struct rpc_message msg = {
5525 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5526 .rpc_argp = &args,
5527 .rpc_resp = &res,
5528 };
5529 struct rpc_task_setup task_setup = {
5530 .rpc_client = clp->cl_rpcclient,
5531 .rpc_message = &msg,
5532 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005533 .callback_data = &data,
5534 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005535 };
5536 int status;
5537
Trond Myklebust9d12b212012-01-17 22:04:25 -05005538 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005539 dprintk("--> %s\n", __func__);
5540 task = rpc_run_task(&task_setup);
5541
5542 if (IS_ERR(task))
5543 status = PTR_ERR(task);
5544 else {
5545 status = task->tk_status;
5546 rpc_put_task(task);
5547 }
5548 dprintk("<-- %s return %d\n", __func__, status);
5549
5550 return status;
5551}
5552
Andy Adamsonfc931582009-04-01 09:22:31 -04005553/*
5554 * Initialize the values to be used by the client in CREATE_SESSION
5555 * If nfs4_init_session set the fore channel request and response sizes,
5556 * use them.
5557 *
5558 * Set the back channel max_resp_sz_cached to zero to force the client to
5559 * always set csa_cachethis to FALSE because the current implementation
5560 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5561 */
5562static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5563{
5564 struct nfs4_session *session = args->client->cl_session;
Trond Myklebustae72ae672012-11-20 11:02:55 -05005565 unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
5566 mxresp_sz = session->fc_target_max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005567
5568 if (mxrqst_sz == 0)
5569 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5570 if (mxresp_sz == 0)
5571 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5572 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005573 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5574 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005575 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005576 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005577
5578 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005579 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005580 __func__,
5581 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005582 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005583
5584 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005585 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5586 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5587 args->bc_attrs.max_resp_sz_cached = 0;
5588 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5589 args->bc_attrs.max_reqs = 1;
5590
5591 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5592 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5593 __func__,
5594 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5595 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5596 args->bc_attrs.max_reqs);
5597}
5598
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005599static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005600{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005601 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5602 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5603
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005604 if (rcvd->max_resp_sz > sent->max_resp_sz)
5605 return -EINVAL;
5606 /*
5607 * Our requested max_ops is the minimum we need; we're not
5608 * prepared to break up compounds into smaller pieces than that.
5609 * So, no point even trying to continue if the server won't
5610 * cooperate:
5611 */
5612 if (rcvd->max_ops < sent->max_ops)
5613 return -EINVAL;
5614 if (rcvd->max_reqs == 0)
5615 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005616 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5617 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005618 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005619}
5620
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005621static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5622{
5623 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5624 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005625
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005626 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5627 return -EINVAL;
5628 if (rcvd->max_resp_sz < sent->max_resp_sz)
5629 return -EINVAL;
5630 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5631 return -EINVAL;
5632 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005633 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005634 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005635 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005636 return -EINVAL;
5637 return 0;
5638}
Andy Adamson8d353012009-04-01 09:22:32 -04005639
Andy Adamson8d353012009-04-01 09:22:32 -04005640static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5641 struct nfs4_session *session)
5642{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005643 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005644
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005645 ret = nfs4_verify_fore_channel_attrs(args, session);
5646 if (ret)
5647 return ret;
5648 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005649}
5650
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005651static int _nfs4_proc_create_session(struct nfs_client *clp,
5652 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005653{
5654 struct nfs4_session *session = clp->cl_session;
5655 struct nfs41_create_session_args args = {
5656 .client = clp,
5657 .cb_program = NFS4_CALLBACK,
5658 };
5659 struct nfs41_create_session_res res = {
5660 .client = clp,
5661 };
5662 struct rpc_message msg = {
5663 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5664 .rpc_argp = &args,
5665 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005666 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04005667 };
5668 int status;
5669
5670 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005671 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005672
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005673 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005674
Trond Myklebust43095d32012-11-20 11:13:12 -05005675 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04005676 /* Verify the session's negotiated channel_attrs values */
5677 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005678 /* Increment the clientid slot sequence id */
5679 clp->cl_seqid++;
5680 }
5681
5682 return status;
5683}
5684
5685/*
5686 * Issues a CREATE_SESSION operation to the server.
5687 * It is the responsibility of the caller to verify the session is
5688 * expired before calling this routine.
5689 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005690int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005691{
5692 int status;
5693 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005694 struct nfs4_session *session = clp->cl_session;
5695
5696 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5697
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005698 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04005699 if (status)
5700 goto out;
5701
Andy Adamsonaacd5532011-11-09 13:58:21 -05005702 /* Init or reset the session slot tables */
5703 status = nfs4_setup_session_slot_tables(session);
5704 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005705 if (status)
5706 goto out;
5707
5708 ptr = (unsigned *)&session->sess_id.data[0];
5709 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5710 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005711out:
5712 dprintk("<-- %s\n", __func__);
5713 return status;
5714}
5715
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005716/*
5717 * Issue the over-the-wire RPC DESTROY_SESSION.
5718 * The caller must serialize access to this routine.
5719 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005720int nfs4_proc_destroy_session(struct nfs4_session *session,
5721 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005722{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005723 struct rpc_message msg = {
5724 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5725 .rpc_argp = session,
5726 .rpc_cred = cred,
5727 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005728 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005729
5730 dprintk("--> nfs4_proc_destroy_session\n");
5731
5732 /* session is still being setup */
5733 if (session->clp->cl_cons_state != NFS_CS_READY)
5734 return status;
5735
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005736 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005737
5738 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04005739 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005740 "Session has been destroyed regardless...\n", status);
5741
5742 dprintk("<-- nfs4_proc_destroy_session\n");
5743 return status;
5744}
5745
Trond Myklebust7b38c362012-05-23 13:23:31 -04005746/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005747 * Renew the cl_session lease.
5748 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005749struct nfs4_sequence_data {
5750 struct nfs_client *clp;
5751 struct nfs4_sequence_args args;
5752 struct nfs4_sequence_res res;
5753};
5754
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005755static void nfs41_sequence_release(void *data)
5756{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005757 struct nfs4_sequence_data *calldata = data;
5758 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005759
Alexandros Batsakis71358402010-02-05 03:45:05 -08005760 if (atomic_read(&clp->cl_count) > 1)
5761 nfs4_schedule_state_renewal(clp);
5762 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005763 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005764}
5765
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005766static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5767{
5768 switch(task->tk_status) {
5769 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005770 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5771 return -EAGAIN;
5772 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005773 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005774 }
5775 return 0;
5776}
5777
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005778static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005779{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005780 struct nfs4_sequence_data *calldata = data;
5781 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005782
Trond Myklebust14516c32010-07-31 14:29:06 -04005783 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5784 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005785
5786 if (task->tk_status < 0) {
5787 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005788 if (atomic_read(&clp->cl_count) == 1)
5789 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005790
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005791 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5792 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005793 return;
5794 }
5795 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005796 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005797out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005798 dprintk("<-- %s\n", __func__);
5799}
5800
5801static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5802{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005803 struct nfs4_sequence_data *calldata = data;
5804 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005805 struct nfs4_sequence_args *args;
5806 struct nfs4_sequence_res *res;
5807
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005808 args = task->tk_msg.rpc_argp;
5809 res = task->tk_msg.rpc_resp;
5810
Trond Myklebust9d12b212012-01-17 22:04:25 -05005811 if (nfs41_setup_sequence(clp->cl_session, args, res, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005812 return;
5813 rpc_call_start(task);
5814}
5815
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05005816static void nfs41_sequence_prepare_privileged(struct rpc_task *task, void *data)
5817{
5818 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
5819 nfs41_sequence_prepare(task, data);
5820}
5821
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005822static const struct rpc_call_ops nfs41_sequence_ops = {
5823 .rpc_call_done = nfs41_sequence_call_done,
5824 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005825 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005826};
5827
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05005828static const struct rpc_call_ops nfs41_sequence_privileged_ops = {
5829 .rpc_call_done = nfs41_sequence_call_done,
5830 .rpc_call_prepare = nfs41_sequence_prepare_privileged,
5831 .rpc_release = nfs41_sequence_release,
5832};
5833
5834static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred,
5835 const struct rpc_call_ops *seq_ops)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005836{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005837 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005838 struct rpc_message msg = {
5839 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5840 .rpc_cred = cred,
5841 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005842 struct rpc_task_setup task_setup_data = {
5843 .rpc_client = clp->cl_rpcclient,
5844 .rpc_message = &msg,
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05005845 .callback_ops = seq_ops,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005846 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5847 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005848
Alexandros Batsakis71358402010-02-05 03:45:05 -08005849 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005850 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005851 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005852 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005853 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005854 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005855 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05005856 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005857 msg.rpc_argp = &calldata->args;
5858 msg.rpc_resp = &calldata->res;
5859 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005860 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005861
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005862 return rpc_run_task(&task_setup_data);
5863}
5864
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005865static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005866{
5867 struct rpc_task *task;
5868 int ret = 0;
5869
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005870 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5871 return 0;
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05005872 task = _nfs41_proc_sequence(clp, cred, &nfs41_sequence_ops);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005873 if (IS_ERR(task))
5874 ret = PTR_ERR(task);
5875 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005876 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005877 dprintk("<-- %s status=%d\n", __func__, ret);
5878 return ret;
5879}
5880
5881static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5882{
5883 struct rpc_task *task;
5884 int ret;
5885
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05005886 task = _nfs41_proc_sequence(clp, cred, &nfs41_sequence_privileged_ops);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005887 if (IS_ERR(task)) {
5888 ret = PTR_ERR(task);
5889 goto out;
5890 }
5891 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005892 if (!ret) {
5893 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5894
5895 if (task->tk_status == 0)
5896 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005897 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005898 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005899 rpc_put_task(task);
5900out:
5901 dprintk("<-- %s status=%d\n", __func__, ret);
5902 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005903}
5904
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005905struct nfs4_reclaim_complete_data {
5906 struct nfs_client *clp;
5907 struct nfs41_reclaim_complete_args arg;
5908 struct nfs41_reclaim_complete_res res;
5909};
5910
5911static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5912{
5913 struct nfs4_reclaim_complete_data *calldata = data;
5914
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005915 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168ab2010-06-16 09:52:26 -04005916 if (nfs41_setup_sequence(calldata->clp->cl_session,
5917 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005918 &calldata->res.seq_res, task))
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005919 return;
5920
5921 rpc_call_start(task);
5922}
5923
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005924static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5925{
5926 switch(task->tk_status) {
5927 case 0:
5928 case -NFS4ERR_COMPLETE_ALREADY:
5929 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5930 break;
5931 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005932 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005933 /* fall through */
5934 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005935 return -EAGAIN;
5936 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005937 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005938 }
5939 return 0;
5940}
5941
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005942static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5943{
5944 struct nfs4_reclaim_complete_data *calldata = data;
5945 struct nfs_client *clp = calldata->clp;
5946 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5947
5948 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005949 if (!nfs41_sequence_done(task, res))
5950 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005951
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005952 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5953 rpc_restart_call_prepare(task);
5954 return;
5955 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005956 dprintk("<-- %s\n", __func__);
5957}
5958
5959static void nfs4_free_reclaim_complete_data(void *data)
5960{
5961 struct nfs4_reclaim_complete_data *calldata = data;
5962
5963 kfree(calldata);
5964}
5965
5966static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5967 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5968 .rpc_call_done = nfs4_reclaim_complete_done,
5969 .rpc_release = nfs4_free_reclaim_complete_data,
5970};
5971
5972/*
5973 * Issue a global reclaim complete.
5974 */
5975static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5976{
5977 struct nfs4_reclaim_complete_data *calldata;
5978 struct rpc_task *task;
5979 struct rpc_message msg = {
5980 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5981 };
5982 struct rpc_task_setup task_setup_data = {
5983 .rpc_client = clp->cl_rpcclient,
5984 .rpc_message = &msg,
5985 .callback_ops = &nfs4_reclaim_complete_call_ops,
5986 .flags = RPC_TASK_ASYNC,
5987 };
5988 int status = -ENOMEM;
5989
5990 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005991 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005992 if (calldata == NULL)
5993 goto out;
5994 calldata->clp = clp;
5995 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005996
Trond Myklebust9d12b212012-01-17 22:04:25 -05005997 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005998 msg.rpc_argp = &calldata->arg;
5999 msg.rpc_resp = &calldata->res;
6000 task_setup_data.callback_data = calldata;
6001 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006002 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006003 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006004 goto out;
6005 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05006006 status = nfs4_wait_for_completion_rpc_task(task);
6007 if (status == 0)
6008 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006009 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006010 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006011out:
6012 dprintk("<-- %s status=%d\n", __func__, status);
6013 return status;
6014}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006015
6016static void
6017nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6018{
6019 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00006020 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006021
6022 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00006023 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6024 * right now covering the LAYOUTGET we are about to send.
6025 * However, that is not so catastrophic, and there seems
6026 * to be no way to prevent it completely.
6027 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006028 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006029 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006030 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006031 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6032 NFS_I(lgp->args.inode)->layout,
6033 lgp->args.ctx->state)) {
6034 rpc_exit(task, NFS4_OK);
6035 return;
6036 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006037 rpc_call_start(task);
6038}
6039
6040static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6041{
6042 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07006043 struct inode *inode = lgp->args.inode;
6044 struct nfs_server *server = NFS_SERVER(inode);
6045 struct pnfs_layout_hdr *lo;
6046 struct nfs4_state *state = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006047
6048 dprintk("--> %s\n", __func__);
6049
6050 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07006051 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006052
6053 switch (task->tk_status) {
6054 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07006055 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006056 case -NFS4ERR_LAYOUTTRYLATER:
6057 case -NFS4ERR_RECALLCONFLICT:
6058 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07006059 break;
6060 case -NFS4ERR_EXPIRED:
6061 case -NFS4ERR_BAD_STATEID:
6062 spin_lock(&inode->i_lock);
6063 lo = NFS_I(inode)->layout;
6064 if (!lo || list_empty(&lo->plh_segs)) {
6065 spin_unlock(&inode->i_lock);
6066 /* If the open stateid was bad, then recover it. */
6067 state = lgp->args.ctx->state;
6068 } else {
6069 LIST_HEAD(head);
6070
6071 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6072 spin_unlock(&inode->i_lock);
6073 /* Mark the bad layout state as invalid, then
6074 * retry using the open stateid. */
6075 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006076 }
6077 }
Trond Myklebustee314c22012-10-01 17:25:48 -07006078 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6079 rpc_restart_call_prepare(task);
6080out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006081 dprintk("<-- %s\n", __func__);
6082}
6083
Idan Kedar85541162012-08-02 11:47:10 +03006084static size_t max_response_pages(struct nfs_server *server)
6085{
6086 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6087 return nfs_page_array_len(0, max_resp_sz);
6088}
6089
6090static void nfs4_free_pages(struct page **pages, size_t size)
6091{
6092 int i;
6093
6094 if (!pages)
6095 return;
6096
6097 for (i = 0; i < size; i++) {
6098 if (!pages[i])
6099 break;
6100 __free_page(pages[i]);
6101 }
6102 kfree(pages);
6103}
6104
6105static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6106{
6107 struct page **pages;
6108 int i;
6109
6110 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6111 if (!pages) {
6112 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6113 return NULL;
6114 }
6115
6116 for (i = 0; i < size; i++) {
6117 pages[i] = alloc_page(gfp_flags);
6118 if (!pages[i]) {
6119 dprintk("%s: failed to allocate page\n", __func__);
6120 nfs4_free_pages(pages, size);
6121 return NULL;
6122 }
6123 }
6124
6125 return pages;
6126}
6127
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006128static void nfs4_layoutget_release(void *calldata)
6129{
6130 struct nfs4_layoutget *lgp = calldata;
Idan Kedar85541162012-08-02 11:47:10 +03006131 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6132 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006133
6134 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006135 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006136 put_nfs_open_context(lgp->args.ctx);
6137 kfree(calldata);
6138 dprintk("<-- %s\n", __func__);
6139}
6140
6141static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6142 .rpc_call_prepare = nfs4_layoutget_prepare,
6143 .rpc_call_done = nfs4_layoutget_done,
6144 .rpc_release = nfs4_layoutget_release,
6145};
6146
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006147struct pnfs_layout_segment *
6148nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006149{
6150 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Idan Kedar85541162012-08-02 11:47:10 +03006151 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006152 struct rpc_task *task;
6153 struct rpc_message msg = {
6154 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6155 .rpc_argp = &lgp->args,
6156 .rpc_resp = &lgp->res,
6157 };
6158 struct rpc_task_setup task_setup_data = {
6159 .rpc_client = server->client,
6160 .rpc_message = &msg,
6161 .callback_ops = &nfs4_layoutget_call_ops,
6162 .callback_data = lgp,
6163 .flags = RPC_TASK_ASYNC,
6164 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006165 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006166 int status = 0;
6167
6168 dprintk("--> %s\n", __func__);
6169
Idan Kedar85541162012-08-02 11:47:10 +03006170 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6171 if (!lgp->args.layout.pages) {
6172 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006173 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006174 }
6175 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
6176
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006177 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006178 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006179 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006180 task = rpc_run_task(&task_setup_data);
6181 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006182 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006183 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006184 if (status == 0)
6185 status = task->tk_status;
6186 if (status == 0)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006187 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006188 rpc_put_task(task);
6189 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006190 if (status)
6191 return ERR_PTR(status);
6192 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006193}
6194
Benny Halevycbe82602011-05-22 19:52:37 +03006195static void
6196nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6197{
6198 struct nfs4_layoutreturn *lrp = calldata;
6199
6200 dprintk("--> %s\n", __func__);
6201 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006202 &lrp->res.seq_res, task))
Benny Halevycbe82602011-05-22 19:52:37 +03006203 return;
6204 rpc_call_start(task);
6205}
6206
6207static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6208{
6209 struct nfs4_layoutreturn *lrp = calldata;
6210 struct nfs_server *server;
6211
6212 dprintk("--> %s\n", __func__);
6213
6214 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
6215 return;
6216
6217 server = NFS_SERVER(lrp->args.inode);
6218 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006219 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006220 return;
6221 }
Benny Halevycbe82602011-05-22 19:52:37 +03006222 dprintk("<-- %s\n", __func__);
6223}
6224
6225static void nfs4_layoutreturn_release(void *calldata)
6226{
6227 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04006228 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006229
6230 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04006231 spin_lock(&lo->plh_inode->i_lock);
6232 if (lrp->res.lrs_present)
6233 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6234 lo->plh_block_lgets--;
6235 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006236 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006237 kfree(calldata);
6238 dprintk("<-- %s\n", __func__);
6239}
6240
6241static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6242 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6243 .rpc_call_done = nfs4_layoutreturn_done,
6244 .rpc_release = nfs4_layoutreturn_release,
6245};
6246
6247int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6248{
6249 struct rpc_task *task;
6250 struct rpc_message msg = {
6251 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6252 .rpc_argp = &lrp->args,
6253 .rpc_resp = &lrp->res,
6254 };
6255 struct rpc_task_setup task_setup_data = {
6256 .rpc_client = lrp->clp->cl_rpcclient,
6257 .rpc_message = &msg,
6258 .callback_ops = &nfs4_layoutreturn_call_ops,
6259 .callback_data = lrp,
6260 };
6261 int status;
6262
6263 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006264 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006265 task = rpc_run_task(&task_setup_data);
6266 if (IS_ERR(task))
6267 return PTR_ERR(task);
6268 status = task->tk_status;
6269 dprintk("<-- %s status=%d\n", __func__, status);
6270 rpc_put_task(task);
6271 return status;
6272}
6273
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006274/*
6275 * Retrieve the list of Data Server devices from the MDS.
6276 */
6277static int _nfs4_getdevicelist(struct nfs_server *server,
6278 const struct nfs_fh *fh,
6279 struct pnfs_devicelist *devlist)
6280{
6281 struct nfs4_getdevicelist_args args = {
6282 .fh = fh,
6283 .layoutclass = server->pnfs_curr_ld->id,
6284 };
6285 struct nfs4_getdevicelist_res res = {
6286 .devlist = devlist,
6287 };
6288 struct rpc_message msg = {
6289 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6290 .rpc_argp = &args,
6291 .rpc_resp = &res,
6292 };
6293 int status;
6294
6295 dprintk("--> %s\n", __func__);
6296 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6297 &res.seq_res, 0);
6298 dprintk("<-- %s status=%d\n", __func__, status);
6299 return status;
6300}
6301
6302int nfs4_proc_getdevicelist(struct nfs_server *server,
6303 const struct nfs_fh *fh,
6304 struct pnfs_devicelist *devlist)
6305{
6306 struct nfs4_exception exception = { };
6307 int err;
6308
6309 do {
6310 err = nfs4_handle_exception(server,
6311 _nfs4_getdevicelist(server, fh, devlist),
6312 &exception);
6313 } while (exception.retry);
6314
6315 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6316 err, devlist->num_devs);
6317
6318 return err;
6319}
6320EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6321
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006322static int
6323_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6324{
6325 struct nfs4_getdeviceinfo_args args = {
6326 .pdev = pdev,
6327 };
6328 struct nfs4_getdeviceinfo_res res = {
6329 .pdev = pdev,
6330 };
6331 struct rpc_message msg = {
6332 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6333 .rpc_argp = &args,
6334 .rpc_resp = &res,
6335 };
6336 int status;
6337
6338 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006339 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006340 dprintk("<-- %s status=%d\n", __func__, status);
6341
6342 return status;
6343}
6344
6345int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6346{
6347 struct nfs4_exception exception = { };
6348 int err;
6349
6350 do {
6351 err = nfs4_handle_exception(server,
6352 _nfs4_proc_getdeviceinfo(server, pdev),
6353 &exception);
6354 } while (exception.retry);
6355 return err;
6356}
6357EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6358
Andy Adamson863a3c62011-03-23 13:27:54 +00006359static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6360{
6361 struct nfs4_layoutcommit_data *data = calldata;
6362 struct nfs_server *server = NFS_SERVER(data->args.inode);
6363
6364 if (nfs4_setup_sequence(server, &data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006365 &data->res.seq_res, task))
Andy Adamson863a3c62011-03-23 13:27:54 +00006366 return;
6367 rpc_call_start(task);
6368}
6369
6370static void
6371nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6372{
6373 struct nfs4_layoutcommit_data *data = calldata;
6374 struct nfs_server *server = NFS_SERVER(data->args.inode);
6375
6376 if (!nfs4_sequence_done(task, &data->res.seq_res))
6377 return;
6378
6379 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006380 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6381 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6382 case -NFS4ERR_BADLAYOUT: /* no layout */
6383 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006384 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006385 break;
6386 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006387 nfs_post_op_update_inode_force_wcc(data->args.inode,
6388 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006389 break;
6390 default:
6391 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6392 rpc_restart_call_prepare(task);
6393 return;
6394 }
6395 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006396}
6397
6398static void nfs4_layoutcommit_release(void *calldata)
6399{
6400 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04006401 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07006402 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00006403
Andy Adamsondb29c082011-07-30 20:52:38 -04006404 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006405 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04006406 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6407 list_del_init(&lseg->pls_lc_list);
6408 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6409 &lseg->pls_flags))
Trond Myklebust9369a432012-09-18 20:57:08 -04006410 pnfs_put_lseg(lseg);
Peng Taoa9bae562011-07-30 20:52:33 -04006411 }
Peng Tao92407e72011-10-23 20:21:17 -07006412
6413 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6414 smp_mb__after_clear_bit();
6415 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6416
Andy Adamson863a3c62011-03-23 13:27:54 +00006417 put_rpccred(data->cred);
6418 kfree(data);
6419}
6420
6421static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6422 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6423 .rpc_call_done = nfs4_layoutcommit_done,
6424 .rpc_release = nfs4_layoutcommit_release,
6425};
6426
6427int
Andy Adamsonef311532011-03-12 02:58:10 -05006428nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006429{
6430 struct rpc_message msg = {
6431 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6432 .rpc_argp = &data->args,
6433 .rpc_resp = &data->res,
6434 .rpc_cred = data->cred,
6435 };
6436 struct rpc_task_setup task_setup_data = {
6437 .task = &data->task,
6438 .rpc_client = NFS_CLIENT(data->args.inode),
6439 .rpc_message = &msg,
6440 .callback_ops = &nfs4_layoutcommit_ops,
6441 .callback_data = data,
6442 .flags = RPC_TASK_ASYNC,
6443 };
6444 struct rpc_task *task;
6445 int status = 0;
6446
6447 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6448 "lbw: %llu inode %lu\n",
6449 data->task.tk_pid, sync,
6450 data->args.lastbytewritten,
6451 data->args.inode->i_ino);
6452
Trond Myklebust9d12b212012-01-17 22:04:25 -05006453 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006454 task = rpc_run_task(&task_setup_data);
6455 if (IS_ERR(task))
6456 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006457 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006458 goto out;
6459 status = nfs4_wait_for_completion_rpc_task(task);
6460 if (status != 0)
6461 goto out;
6462 status = task->tk_status;
6463out:
6464 dprintk("%s: status %d\n", __func__, status);
6465 rpc_put_task(task);
6466 return status;
6467}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006468
6469static int
6470_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6471 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6472{
6473 struct nfs41_secinfo_no_name_args args = {
6474 .style = SECINFO_STYLE_CURRENT_FH,
6475 };
6476 struct nfs4_secinfo_res res = {
6477 .flavors = flavors,
6478 };
6479 struct rpc_message msg = {
6480 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6481 .rpc_argp = &args,
6482 .rpc_resp = &res,
6483 };
6484 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6485}
6486
6487static int
6488nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6489 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6490{
6491 struct nfs4_exception exception = { };
6492 int err;
6493 do {
6494 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6495 switch (err) {
6496 case 0:
6497 case -NFS4ERR_WRONGSEC:
6498 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006499 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006500 default:
6501 err = nfs4_handle_exception(server, err, &exception);
6502 }
6503 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006504out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006505 return err;
6506}
6507
6508static int
6509nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6510 struct nfs_fsinfo *info)
6511{
6512 int err;
6513 struct page *page;
6514 rpc_authflavor_t flavor;
6515 struct nfs4_secinfo_flavors *flavors;
6516
6517 page = alloc_page(GFP_KERNEL);
6518 if (!page) {
6519 err = -ENOMEM;
6520 goto out;
6521 }
6522
6523 flavors = page_address(page);
6524 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6525
6526 /*
6527 * Fall back on "guess and check" method if
6528 * the server doesn't support SECINFO_NO_NAME
6529 */
6530 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6531 err = nfs4_find_root_sec(server, fhandle, info);
6532 goto out_freepage;
6533 }
6534 if (err)
6535 goto out_freepage;
6536
6537 flavor = nfs_find_best_sec(flavors);
6538 if (err == 0)
6539 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6540
6541out_freepage:
6542 put_page(page);
6543 if (err == -EACCES)
6544 return -EPERM;
6545out:
6546 return err;
6547}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006548
6549static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006550{
6551 int status;
6552 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006553 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006554 };
6555 struct nfs41_test_stateid_res res;
6556 struct rpc_message msg = {
6557 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6558 .rpc_argp = &args,
6559 .rpc_resp = &res,
6560 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006561
Chuck Lever38527b12012-07-11 16:30:23 -04006562 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006563 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006564 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Chuck Lever38527b12012-07-11 16:30:23 -04006565 if (status != NFS_OK) {
6566 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04006567 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04006568 }
6569 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04006570 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006571}
6572
Chuck Lever38527b12012-07-11 16:30:23 -04006573/**
6574 * nfs41_test_stateid - perform a TEST_STATEID operation
6575 *
6576 * @server: server / transport on which to perform the operation
6577 * @stateid: state ID to test
6578 *
6579 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6580 * Otherwise a negative NFS4ERR value is returned if the operation
6581 * failed or the state ID is not currently valid.
6582 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006583static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006584{
6585 struct nfs4_exception exception = { };
6586 int err;
6587 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006588 err = _nfs41_test_stateid(server, stateid);
6589 if (err != -NFS4ERR_DELAY)
6590 break;
6591 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04006592 } while (exception.retry);
6593 return err;
6594}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006595
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006596static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006597{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006598 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006599 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006600 };
6601 struct nfs41_free_stateid_res res;
6602 struct rpc_message msg = {
6603 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6604 .rpc_argp = &args,
6605 .rpc_resp = &res,
6606 };
Chuck Lever38527b12012-07-11 16:30:23 -04006607 int status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006608
Chuck Lever38527b12012-07-11 16:30:23 -04006609 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006610 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Chuck Lever38527b12012-07-11 16:30:23 -04006611 status = nfs4_call_sync_sequence(server->client, server, &msg,
Chuck Lever377e5072012-07-11 16:29:45 -04006612 &args.seq_args, &res.seq_res, 1);
Chuck Lever38527b12012-07-11 16:30:23 -04006613 dprintk("NFS reply free_stateid: %d\n", status);
6614 return status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006615}
6616
Chuck Lever38527b12012-07-11 16:30:23 -04006617/**
6618 * nfs41_free_stateid - perform a FREE_STATEID operation
6619 *
6620 * @server: server / transport on which to perform the operation
6621 * @stateid: state ID to release
6622 *
6623 * Returns NFS_OK if the server freed "stateid". Otherwise a
6624 * negative NFS4ERR value is returned.
6625 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006626static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006627{
6628 struct nfs4_exception exception = { };
6629 int err;
6630 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006631 err = _nfs4_free_stateid(server, stateid);
6632 if (err != -NFS4ERR_DELAY)
6633 break;
6634 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006635 } while (exception.retry);
6636 return err;
6637}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006638
6639static bool nfs41_match_stateid(const nfs4_stateid *s1,
6640 const nfs4_stateid *s2)
6641{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006642 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006643 return false;
6644
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006645 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006646 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006647 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006648 return true;
6649
6650 return false;
6651}
6652
Andy Adamson557134a2009-04-01 09:21:53 -04006653#endif /* CONFIG_NFS_V4_1 */
6654
Trond Myklebust36281ca2012-03-04 18:13:56 -05006655static bool nfs4_match_stateid(const nfs4_stateid *s1,
6656 const nfs4_stateid *s2)
6657{
Trond Myklebustf597c532012-03-04 18:13:56 -05006658 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006659}
6660
6661
Trond Myklebust17280172012-03-11 13:11:00 -04006662static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006663 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006664 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006665 .recover_open = nfs4_open_reclaim,
6666 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006667 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006668 .get_clid_cred = nfs4_get_setclientid_cred,
Chuck Lever05f4c352012-09-14 17:24:32 -04006669 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006670};
6671
Andy Adamson591d71c2009-04-01 09:22:47 -04006672#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006673static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006674 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6675 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6676 .recover_open = nfs4_open_reclaim,
6677 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006678 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006679 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006680 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04006681 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04006682};
6683#endif /* CONFIG_NFS_V4_1 */
6684
Trond Myklebust17280172012-03-11 13:11:00 -04006685static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006686 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006687 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006688 .recover_open = nfs4_open_expired,
6689 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006690 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006691 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006692};
6693
Andy Adamson591d71c2009-04-01 09:22:47 -04006694#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006695static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006696 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6697 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006698 .recover_open = nfs41_open_expired,
6699 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006700 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006701 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006702};
6703#endif /* CONFIG_NFS_V4_1 */
6704
Trond Myklebust17280172012-03-11 13:11:00 -04006705static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006706 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006707 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006708 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006709};
6710
6711#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006712static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006713 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006714 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006715 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006716};
6717#endif
6718
Trond Myklebust97dc1352010-06-16 09:52:26 -04006719static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6720 .minor_version = 0,
6721 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006722 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006723 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006724 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6725 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6726 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006727};
6728
6729#if defined(CONFIG_NFS_V4_1)
6730static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6731 .minor_version = 1,
6732 .call_sync = _nfs4_call_sync_session,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006733 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006734 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006735 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6736 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6737 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006738};
6739#endif
6740
Trond Myklebust97dc1352010-06-16 09:52:26 -04006741const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6742 [0] = &nfs_v4_0_minor_ops,
6743#if defined(CONFIG_NFS_V4_1)
6744 [1] = &nfs_v4_1_minor_ops,
6745#endif
6746};
6747
Bryan Schumaker73a79702012-07-16 16:39:12 -04006748const struct inode_operations nfs4_dir_inode_operations = {
6749 .create = nfs_create,
6750 .lookup = nfs_lookup,
6751 .atomic_open = nfs_atomic_open,
6752 .link = nfs_link,
6753 .unlink = nfs_unlink,
6754 .symlink = nfs_symlink,
6755 .mkdir = nfs_mkdir,
6756 .rmdir = nfs_rmdir,
6757 .mknod = nfs_mknod,
6758 .rename = nfs_rename,
6759 .permission = nfs_permission,
6760 .getattr = nfs_getattr,
6761 .setattr = nfs_setattr,
6762 .getxattr = generic_getxattr,
6763 .setxattr = generic_setxattr,
6764 .listxattr = generic_listxattr,
6765 .removexattr = generic_removexattr,
6766};
6767
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006768static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006769 .permission = nfs_permission,
6770 .getattr = nfs_getattr,
6771 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006772 .getxattr = generic_getxattr,
6773 .setxattr = generic_setxattr,
6774 .listxattr = generic_listxattr,
6775 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006776};
6777
David Howells509de812006-08-22 20:06:11 -04006778const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006779 .version = 4, /* protocol version */
6780 .dentry_ops = &nfs4_dentry_operations,
6781 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006782 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006783 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006784 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04006785 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04006786 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006787 .getattr = nfs4_proc_getattr,
6788 .setattr = nfs4_proc_setattr,
6789 .lookup = nfs4_proc_lookup,
6790 .access = nfs4_proc_access,
6791 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006792 .create = nfs4_proc_create,
6793 .remove = nfs4_proc_remove,
6794 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04006795 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006796 .unlink_done = nfs4_proc_unlink_done,
6797 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006798 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04006799 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04006800 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006801 .link = nfs4_proc_link,
6802 .symlink = nfs4_proc_symlink,
6803 .mkdir = nfs4_proc_mkdir,
6804 .rmdir = nfs4_proc_remove,
6805 .readdir = nfs4_proc_readdir,
6806 .mknod = nfs4_proc_mknod,
6807 .statfs = nfs4_proc_statfs,
6808 .fsinfo = nfs4_proc_fsinfo,
6809 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006810 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006811 .decode_dirent = nfs4_decode_dirent,
6812 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04006813 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04006814 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05006815 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006816 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04006817 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04006818 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006819 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006820 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04006821 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006822 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006823 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006824 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006825 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006826 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04006827 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04006828 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04006829 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00006830 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04006831 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04006832 .create_server = nfs4_create_server,
6833 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006834};
6835
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006836static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6837 .prefix = XATTR_NAME_NFSV4_ACL,
6838 .list = nfs4_xattr_list_nfs4_acl,
6839 .get = nfs4_xattr_get_nfs4_acl,
6840 .set = nfs4_xattr_set_nfs4_acl,
6841};
6842
6843const struct xattr_handler *nfs4_xattr_handlers[] = {
6844 &nfs4_xattr_nfs4_acl_handler,
6845 NULL
6846};
6847
Linus Torvalds1da177e2005-04-16 15:20:36 -07006848/*
6849 * Local variables:
6850 * c-basic-offset: 8
6851 * End:
6852 */