blob: 23b0c2fcb052e3707c34be1ea7d4f95fcf3ec0ae [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050042#include <linux/ratelimit.h>
43#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sunrpc/clnt.h>
46#include <linux/nfs.h>
47#include <linux/nfs4.h>
48#include <linux/nfs_fs.h>
49#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040050#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070052#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040053#include <linux/module.h>
Trond Myklebust6926afd2012-01-07 13:22:46 -050054#include <linux/nfs_idmap.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000055#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050056#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010057#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Trond Myklebust4ce79712005-06-22 17:16:21 +000059#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050061#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050062#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040063#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040064#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040065#include "netns.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050066#include "nfs4session.h"
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69#define NFSDBG_FACILITY NFSDBG_PROC
70
Trond Myklebust2066fe82006-09-15 08:30:46 -040071#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define NFS4_POLL_RETRY_MAX (15*HZ)
73
Trond Myklebustcdd4e682006-01-03 09:55:12 +010074struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010075static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080076static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050078static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Chuck Lever81934dd2012-03-01 17:01:57 -050079static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Bryan Schumakerbae36242012-05-10 15:07:31 -040080static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *);
Trond Myklebust99367812007-07-17 21:52:41 -040081static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040082static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
83 struct nfs_fattr *fattr, struct iattr *sattr,
84 struct nfs4_state *state);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040085#ifdef CONFIG_NFS_V4_1
Bryan Schumaker1cab0652012-01-31 10:39:29 -050086static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *);
87static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040088#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070089/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050090static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Trond Myklebust52567b02009-10-23 14:46:42 -040092 if (err >= -1000)
93 return err;
94 switch (err) {
95 case -NFS4ERR_RESOURCE:
96 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +000097 case -NFS4ERR_WRONGSEC:
98 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -080099 case -NFS4ERR_BADOWNER:
100 case -NFS4ERR_BADNAME:
101 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400102 case -NFS4ERR_SHARE_DENIED:
103 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400104 case -NFS4ERR_MINOR_VERS_MISMATCH:
105 return -EPROTONOSUPPORT;
Weston Andros Adamson6168f622012-09-10 14:00:46 -0400106 case -NFS4ERR_ACCESS:
107 return -EACCES;
Trond Myklebust52567b02009-10-23 14:46:42 -0400108 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700110 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400111 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400113 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114}
115
116/*
117 * This is our standard bitmap for GETATTR requests.
118 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400119const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 FATTR4_WORD0_TYPE
121 | FATTR4_WORD0_CHANGE
122 | FATTR4_WORD0_SIZE
123 | FATTR4_WORD0_FSID
124 | FATTR4_WORD0_FILEID,
125 FATTR4_WORD1_MODE
126 | FATTR4_WORD1_NUMLINKS
127 | FATTR4_WORD1_OWNER
128 | FATTR4_WORD1_OWNER_GROUP
129 | FATTR4_WORD1_RAWDEV
130 | FATTR4_WORD1_SPACE_USED
131 | FATTR4_WORD1_TIME_ACCESS
132 | FATTR4_WORD1_TIME_METADATA
133 | FATTR4_WORD1_TIME_MODIFY
134};
135
Trond Myklebust1549210f2012-06-05 09:16:47 -0400136static const u32 nfs4_pnfs_open_bitmap[3] = {
137 FATTR4_WORD0_TYPE
138 | FATTR4_WORD0_CHANGE
139 | FATTR4_WORD0_SIZE
140 | FATTR4_WORD0_FSID
141 | FATTR4_WORD0_FILEID,
142 FATTR4_WORD1_MODE
143 | FATTR4_WORD1_NUMLINKS
144 | FATTR4_WORD1_OWNER
145 | FATTR4_WORD1_OWNER_GROUP
146 | FATTR4_WORD1_RAWDEV
147 | FATTR4_WORD1_SPACE_USED
148 | FATTR4_WORD1_TIME_ACCESS
149 | FATTR4_WORD1_TIME_METADATA
150 | FATTR4_WORD1_TIME_MODIFY,
151 FATTR4_WORD2_MDSTHRESHOLD
152};
153
Andy Adamsone23008e2012-10-02 21:07:32 -0400154static const u32 nfs4_open_noattr_bitmap[3] = {
155 FATTR4_WORD0_TYPE
156 | FATTR4_WORD0_CHANGE
157 | FATTR4_WORD0_FILEID,
158};
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160const u32 nfs4_statfs_bitmap[2] = {
161 FATTR4_WORD0_FILES_AVAIL
162 | FATTR4_WORD0_FILES_FREE
163 | FATTR4_WORD0_FILES_TOTAL,
164 FATTR4_WORD1_SPACE_AVAIL
165 | FATTR4_WORD1_SPACE_FREE
166 | FATTR4_WORD1_SPACE_TOTAL
167};
168
Trond Myklebust4ce79712005-06-22 17:16:21 +0000169const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 FATTR4_WORD0_MAXLINK
171 | FATTR4_WORD0_MAXNAME,
172 0
173};
174
Fred Isamandae100c2011-07-30 20:52:37 -0400175const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 | FATTR4_WORD0_MAXREAD
177 | FATTR4_WORD0_MAXWRITE
178 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700179 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400180 | FATTR4_WORD1_FS_LAYOUT_TYPES,
181 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182};
183
Manoj Naik830b8e32006-06-09 09:34:25 -0400184const u32 nfs4_fs_locations_bitmap[2] = {
185 FATTR4_WORD0_TYPE
186 | FATTR4_WORD0_CHANGE
187 | FATTR4_WORD0_SIZE
188 | FATTR4_WORD0_FSID
189 | FATTR4_WORD0_FILEID
190 | FATTR4_WORD0_FS_LOCATIONS,
191 FATTR4_WORD1_MODE
192 | FATTR4_WORD1_NUMLINKS
193 | FATTR4_WORD1_OWNER
194 | FATTR4_WORD1_OWNER_GROUP
195 | FATTR4_WORD1_RAWDEV
196 | FATTR4_WORD1_SPACE_USED
197 | FATTR4_WORD1_TIME_ACCESS
198 | FATTR4_WORD1_TIME_METADATA
199 | FATTR4_WORD1_TIME_MODIFY
200 | FATTR4_WORD1_MOUNTED_ON_FILEID
201};
202
Al Virobc4785c2006-10-19 23:28:51 -0700203static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 struct nfs4_readdir_arg *readdir)
205{
Al Viro0dbb4c62006-10-19 23:28:49 -0700206 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000209 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
211 return;
212 }
213
214 readdir->cookie = 0;
215 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
216 if (cookie == 2)
217 return;
218
219 /*
220 * NFSv4 servers do not return entries for '.' and '..'
221 * Therefore, we fake these entries here. We let '.'
222 * have cookie 0 and '..' have cookie 1. Note that
223 * when talking to the server, we always send cookie 0
224 * instead of 1 or 2.
225 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800226 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 if (cookie == 0) {
229 *p++ = xdr_one; /* next */
230 *p++ = xdr_zero; /* cookie, first word */
231 *p++ = xdr_one; /* cookie, second word */
232 *p++ = xdr_one; /* entry len */
233 memcpy(p, ".\0\0\0", 4); /* entry */
234 p++;
235 *p++ = xdr_one; /* bitmap length */
236 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
237 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400238 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 }
240
241 *p++ = xdr_one; /* next */
242 *p++ = xdr_zero; /* cookie, first word */
243 *p++ = xdr_two; /* cookie, second word */
244 *p++ = xdr_two; /* entry len */
245 memcpy(p, "..\0\0", 4); /* entry */
246 p++;
247 *p++ = xdr_one; /* bitmap length */
248 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
249 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400250 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 readdir->pgbase = (char *)p - (char *)start;
253 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800254 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255}
256
Trond Myklebust65de8722008-12-23 15:21:44 -0500257static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
258{
259 int res = 0;
260
261 might_sleep();
262
263 if (*timeout <= 0)
264 *timeout = NFS4_POLL_RETRY_MIN;
265 if (*timeout > NFS4_POLL_RETRY_MAX)
266 *timeout = NFS4_POLL_RETRY_MAX;
Jeff Laytond3103102011-12-01 22:44:39 +0100267 freezable_schedule_timeout_killable(*timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500268 if (fatal_signal_pending(current))
269 res = -ERESTARTSYS;
270 *timeout <<= 1;
271 return res;
272}
273
274/* This is the error handling routine for processes that are allowed
275 * to sleep.
276 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800277static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500278{
279 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500280 struct nfs4_state *state = exception->state;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500281 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500282 int ret = errorcode;
283
284 exception->retry = 0;
285 switch(errorcode) {
286 case 0:
287 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500288 case -NFS4ERR_OPENMODE:
Bryan Schumaker011e2a72012-06-20 15:53:43 -0400289 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400290 nfs4_inode_return_delegation(inode);
Trond Myklebust3114ea72012-03-07 16:39:06 -0500291 exception->retry = 1;
292 return 0;
293 }
294 if (state == NULL)
295 break;
296 nfs4_schedule_stateid_recovery(server, state);
297 goto wait_on_recovery;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500298 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500299 case -NFS4ERR_ADMIN_REVOKED:
300 case -NFS4ERR_BAD_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500301 if (state == NULL)
302 break;
Trond Myklebust14977482012-03-27 18:31:25 -0400303 nfs_remove_bad_delegation(state->inode);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500304 nfs4_schedule_stateid_recovery(server, state);
305 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400306 case -NFS4ERR_EXPIRED:
307 if (state != NULL)
308 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust65de8722008-12-23 15:21:44 -0500309 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500310 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500311 nfs4_schedule_lease_recovery(clp);
312 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500313#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400314 case -NFS4ERR_BADSESSION:
315 case -NFS4ERR_BADSLOT:
316 case -NFS4ERR_BAD_HIGH_SLOT:
317 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
318 case -NFS4ERR_DEADSESSION:
319 case -NFS4ERR_SEQ_FALSE_RETRY:
320 case -NFS4ERR_SEQ_MISORDERED:
321 dprintk("%s ERROR: %d Reset session\n", __func__,
322 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400323 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400324 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500325#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500326 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500327 if (exception->timeout > HZ) {
328 /* We have retried a decent amount, time to
329 * fail
330 */
331 ret = -EBUSY;
332 break;
333 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500334 case -NFS4ERR_GRACE:
335 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -0500336 case -EKEYEXPIRED:
Trond Myklebust65de8722008-12-23 15:21:44 -0500337 ret = nfs4_delay(server->client, &exception->timeout);
338 if (ret != 0)
339 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400340 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500341 case -NFS4ERR_OLD_STATEID:
342 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800343 break;
344 case -NFS4ERR_BADOWNER:
345 /* The following works around a Linux server bug! */
346 case -NFS4ERR_BADNAME:
347 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
348 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
349 exception->retry = 1;
350 printk(KERN_WARNING "NFS: v4 server %s "
351 "does not accept raw "
352 "uid/gids. "
353 "Reenabling the idmapper.\n",
354 server->nfs_client->cl_hostname);
355 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500356 }
357 /* We failed to handle the error */
358 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500359wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500360 ret = nfs4_wait_clnt_recover(clp);
361 if (ret == 0)
362 exception->retry = 1;
363 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500364}
365
366
Trond Myklebust452e9352010-07-31 14:29:06 -0400367static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 spin_lock(&clp->cl_lock);
370 if (time_before(clp->cl_last_renewal,timestamp))
371 clp->cl_last_renewal = timestamp;
372 spin_unlock(&clp->cl_lock);
373}
374
Trond Myklebust452e9352010-07-31 14:29:06 -0400375static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
376{
377 do_renew_lease(server->nfs_client, timestamp);
378}
379
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400380#if defined(CONFIG_NFS_V4_1)
381
Trond 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)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400516 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400517
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400518 tbl = &session->fc_slot_table;
519
Trond Myklebust69d206b2012-11-22 13:21:02 -0500520 task->tk_timeout = 0;
521
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400522 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400523 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
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;
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400566out_success:
567 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400568 return 0;
569}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000570EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400571
Trond Myklebust035168a2010-06-16 09:52:26 -0400572int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400573 struct nfs4_sequence_args *args,
574 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400575 struct rpc_task *task)
576{
Trond Myklebust035168a2010-06-16 09:52:26 -0400577 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400578 int ret = 0;
579
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400580 if (session == NULL) {
581 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400582 goto out;
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400583 }
Trond Myklebust035168a2010-06-16 09:52:26 -0400584
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500585 dprintk("--> %s clp %p session %p sr_slot %d\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400586 __func__, session->clp, session, res->sr_slot ?
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500587 res->sr_slot->slot_nr : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400588
Trond Myklebust9d12b212012-01-17 22:04:25 -0500589 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400590out:
591 dprintk("<-- %s status=%d\n", __func__, ret);
592 return ret;
593}
594
595struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400596 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400597 struct nfs4_sequence_args *seq_args;
598 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400599};
600
601static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
602{
603 struct nfs41_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400604 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400605
Trond Myklebust035168a2010-06-16 09:52:26 -0400606 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
607
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400608 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400609}
610
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800611static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
612{
613 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
614 nfs41_call_sync_prepare(task, calldata);
615}
616
Andy Adamson69ab40c2009-04-01 09:22:19 -0400617static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
618{
619 struct nfs41_call_sync_data *data = calldata;
620
Trond Myklebust14516c32010-07-31 14:29:06 -0400621 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400622}
623
Trond Myklebust17280172012-03-11 13:11:00 -0400624static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400625 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400626 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400627};
628
Trond Myklebust17280172012-03-11 13:11:00 -0400629static const struct rpc_call_ops nfs41_call_priv_sync_ops = {
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800630 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
631 .rpc_call_done = nfs41_call_sync_done,
632};
633
Bryan Schumaker7c513052011-03-24 17:12:24 +0000634static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
635 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400636 struct rpc_message *msg,
637 struct nfs4_sequence_args *args,
638 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800639 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400640{
641 int ret;
642 struct rpc_task *task;
643 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400644 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400645 .seq_args = args,
646 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400647 };
648 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000649 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400650 .rpc_message = msg,
651 .callback_ops = &nfs41_call_sync_ops,
652 .callback_data = &data
653 };
654
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800655 if (privileged)
656 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400657 task = rpc_run_task(&task_setup);
658 if (IS_ERR(task))
659 ret = PTR_ERR(task);
660 else {
661 ret = task->tk_status;
662 rpc_put_task(task);
663 }
664 return ret;
665}
666
Bryan Schumaker7c513052011-03-24 17:12:24 +0000667int _nfs4_call_sync_session(struct rpc_clnt *clnt,
668 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400669 struct rpc_message *msg,
670 struct nfs4_sequence_args *args,
671 struct nfs4_sequence_res *res,
672 int cache_reply)
673{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500674 nfs41_init_sequence(args, res, cache_reply);
675 return nfs4_call_sync_sequence(clnt, server, msg, args, res, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400676}
677
Trond Myklebustdf896452010-06-16 09:52:26 -0400678#else
Trond Myklebust9d12b212012-01-17 22:04:25 -0500679static inline
680void nfs41_init_sequence(struct nfs4_sequence_args *args,
681 struct nfs4_sequence_res *res, int cache_reply)
682{
683}
684
Trond Myklebust14516c32010-07-31 14:29:06 -0400685static int nfs4_sequence_done(struct rpc_task *task,
686 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400687{
Trond Myklebust14516c32010-07-31 14:29:06 -0400688 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400689}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400690#endif /* CONFIG_NFS_V4_1 */
691
Bryan Schumaker7c513052011-03-24 17:12:24 +0000692int _nfs4_call_sync(struct rpc_clnt *clnt,
693 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400694 struct rpc_message *msg,
695 struct nfs4_sequence_args *args,
696 struct nfs4_sequence_res *res,
697 int cache_reply)
698{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500699 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000700 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400701}
702
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000703static inline
Bryan Schumaker7c513052011-03-24 17:12:24 +0000704int nfs4_call_sync(struct rpc_clnt *clnt,
705 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000706 struct rpc_message *msg,
707 struct nfs4_sequence_args *args,
708 struct nfs4_sequence_res *res,
709 int cache_reply)
710{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000711 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
712 args, res, cache_reply);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000713}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400714
Trond Myklebust38478b22006-05-25 01:40:57 -0400715static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716{
Trond Myklebust38478b22006-05-25 01:40:57 -0400717 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
Trond Myklebust38478b22006-05-25 01:40:57 -0400719 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400720 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700721 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400722 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700723 dir->i_version = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400724 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725}
726
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100727struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400728 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100729 struct nfs_openargs o_arg;
730 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100731 struct nfs_open_confirmargs c_arg;
732 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500733 struct nfs4_string owner_name;
734 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100735 struct nfs_fattr f_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100736 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400737 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100738 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400739 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100740 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100741 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400742 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100743 int rpc_status;
744 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100745};
746
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400747
748static void nfs4_init_opendata_res(struct nfs4_opendata *p)
749{
750 p->o_res.f_attr = &p->f_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400751 p->o_res.seqid = p->o_arg.seqid;
752 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400753 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400754 p->o_res.access_request = p->o_arg.access;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400755 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500756 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400757}
758
Al Viro82a2c1b2011-06-22 18:30:55 -0400759static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500760 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400761 const struct iattr *attrs,
762 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100763{
Al Viro82a2c1b2011-06-22 18:30:55 -0400764 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100765 struct inode *dir = parent->d_inode;
766 struct nfs_server *server = NFS_SERVER(dir);
767 struct nfs4_opendata *p;
768
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400769 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100770 if (p == NULL)
771 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400772 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100773 if (p->o_arg.seqid == NULL)
774 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400775 nfs_sb_active(dentry->d_sb);
776 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100777 p->dir = parent;
778 p->owner = sp;
779 atomic_inc(&sp->so_count);
780 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500781 p->o_arg.open_flags = flags;
782 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700783 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
784 * will return permission denied for all bits until close */
785 if (!(flags & O_EXCL)) {
786 /* ask server to check for all possible rights as results
787 * are cached */
788 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
789 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700790 }
David Howells7539bba2006-08-22 20:06:09 -0400791 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400792 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
793 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400794 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100795 p->o_arg.server = server;
796 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust1549210f2012-06-05 09:16:47 -0400797 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100798 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust536e43d2012-01-17 22:04:26 -0500799 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -0500800 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400801
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100802 p->o_arg.u.attrs = &p->attrs;
803 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500804
805 verf[0] = jiffies;
806 verf[1] = current->pid;
807 memcpy(p->o_arg.u.verifier.data, verf,
808 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100809 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100810 p->c_arg.fh = &p->o_res.fh;
811 p->c_arg.stateid = &p->o_res.stateid;
812 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400813 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400814 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100815 return p;
816err_free:
817 kfree(p);
818err:
819 dput(parent);
820 return NULL;
821}
822
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400823static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100824{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400825 struct nfs4_opendata *p = container_of(kref,
826 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400827 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400828
829 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400830 if (p->state != NULL)
831 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400832 nfs4_put_state_owner(p->owner);
833 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400834 dput(p->dentry);
835 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500836 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400837 kfree(p);
838}
839
840static void nfs4_opendata_put(struct nfs4_opendata *p)
841{
842 if (p != NULL)
843 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100844}
845
Trond Myklebust06f814a2006-01-03 09:55:07 +0100846static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
847{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100848 int ret;
849
Trond Myklebust06f814a2006-01-03 09:55:07 +0100850 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100851 return ret;
852}
853
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500854static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400855{
856 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500857
Trond Myklebust536e43d2012-01-17 22:04:26 -0500858 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500859 goto out;
860 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400861 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500862 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
863 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400864 break;
865 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500866 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
867 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400868 break;
869 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500870 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
871 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400872 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500873out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400874 return ret;
875}
876
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500877static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400878{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500879 if (delegation == NULL)
880 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500881 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400882 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500883 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400884 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500885 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400886 return 1;
887}
888
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500889static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100890{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500891 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100892 case FMODE_WRITE:
893 state->n_wronly++;
894 break;
895 case FMODE_READ:
896 state->n_rdonly++;
897 break;
898 case FMODE_READ|FMODE_WRITE:
899 state->n_rdwr++;
900 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500901 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100902}
903
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500904static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400905{
906 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -0500907 nfs4_stateid_copy(&state->stateid, stateid);
908 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500909 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400910 case FMODE_READ:
911 set_bit(NFS_O_RDONLY_STATE, &state->flags);
912 break;
913 case FMODE_WRITE:
914 set_bit(NFS_O_WRONLY_STATE, &state->flags);
915 break;
916 case FMODE_READ|FMODE_WRITE:
917 set_bit(NFS_O_RDWR_STATE, &state->flags);
918 }
919}
920
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500921static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400922{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400923 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500924 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400925 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400926}
927
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500928static 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 -0700929{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400930 /*
931 * Protect the call to nfs4_state_set_mode_locked and
932 * serialise the stateid update
933 */
934 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400935 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -0500936 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -0400937 set_bit(NFS_DELEGATED_STATE, &state->flags);
938 }
939 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500940 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400941 write_sequnlock(&state->seqlock);
942 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500943 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700944 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945}
946
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500947static 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 -0500948{
949 struct nfs_inode *nfsi = NFS_I(state->inode);
950 struct nfs_delegation *deleg_cur;
951 int ret = 0;
952
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500953 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500954
955 rcu_read_lock();
956 deleg_cur = rcu_dereference(nfsi->delegation);
957 if (deleg_cur == NULL)
958 goto no_delegation;
959
960 spin_lock(&deleg_cur->lock);
961 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500962 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500963 goto no_delegation_unlock;
964
965 if (delegation == NULL)
966 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -0500967 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -0500968 goto no_delegation_unlock;
969
Trond Myklebustb7391f42008-12-23 15:21:52 -0500970 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500971 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500972 ret = 1;
973no_delegation_unlock:
974 spin_unlock(&deleg_cur->lock);
975no_delegation:
976 rcu_read_unlock();
977
978 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500979 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500980 ret = 1;
981 }
982
983 return ret;
984}
985
986
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500987static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400988{
989 struct nfs_delegation *delegation;
990
991 rcu_read_lock();
992 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500993 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -0400994 rcu_read_unlock();
995 return;
996 }
997 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400998 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400999}
1000
Trond Myklebust6ee41262007-07-08 14:11:36 -04001001static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001002{
1003 struct nfs4_state *state = opendata->state;
1004 struct nfs_inode *nfsi = NFS_I(state->inode);
1005 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -05001006 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001007 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001008 nfs4_stateid stateid;
1009 int ret = -EAGAIN;
1010
Trond Myklebustaac00a82007-07-05 19:02:21 -04001011 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001012 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001013 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001014 if (can_open_cached(state, fmode, open_mode)) {
1015 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001016 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001017 goto out_return_state;
1018 }
1019 spin_unlock(&state->owner->so_lock);
1020 }
Trond Myklebust34310432008-12-23 15:21:38 -05001021 rcu_read_lock();
1022 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001023 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001024 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001025 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001026 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001027 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001028 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001029 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001030 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001031 if (ret != 0)
1032 goto out;
1033 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001034
1035 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001036 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001037 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001038 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001039out:
1040 return ERR_PTR(ret);
1041out_return_state:
1042 atomic_inc(&state->count);
1043 return state;
1044}
1045
Andy Adamsone23008e2012-10-02 21:07:32 -04001046static void
1047nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1048{
1049 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1050 struct nfs_delegation *delegation;
1051 int delegation_flags = 0;
1052
1053 rcu_read_lock();
1054 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1055 if (delegation)
1056 delegation_flags = delegation->flags;
1057 rcu_read_unlock();
1058 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1059 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1060 "returning a delegation for "
1061 "OPEN(CLAIM_DELEGATE_CUR)\n",
1062 clp->cl_hostname);
1063 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1064 nfs_inode_set_delegation(state->inode,
1065 data->owner->so_cred,
1066 &data->o_res);
1067 else
1068 nfs_inode_reclaim_delegation(state->inode,
1069 data->owner->so_cred,
1070 &data->o_res);
1071}
1072
1073/*
1074 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1075 * and update the nfs4_state.
1076 */
1077static struct nfs4_state *
1078_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1079{
1080 struct inode *inode = data->state->inode;
1081 struct nfs4_state *state = data->state;
1082 int ret;
1083
1084 if (!data->rpc_done) {
1085 ret = data->rpc_status;
1086 goto err;
1087 }
1088
1089 ret = -ESTALE;
1090 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1091 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1092 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1093 goto err;
1094
1095 ret = -ENOMEM;
1096 state = nfs4_get_open_state(inode, data->owner);
1097 if (state == NULL)
1098 goto err;
1099
1100 ret = nfs_refresh_inode(inode, &data->f_attr);
1101 if (ret)
1102 goto err;
1103
1104 if (data->o_res.delegation_type != 0)
1105 nfs4_opendata_check_deleg(data, state);
1106 update_open_stateid(state, &data->o_res.stateid, NULL,
1107 data->o_arg.fmode);
1108
1109 return state;
1110err:
1111 return ERR_PTR(ret);
1112
1113}
1114
1115static struct nfs4_state *
1116_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001117{
1118 struct inode *inode;
1119 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001120 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001121
Trond Myklebustaac00a82007-07-05 19:02:21 -04001122 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001123 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001124 goto out;
1125 }
1126
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001127 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001128 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001129 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001130 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001131 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001132 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001133 goto err;
1134 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001135 state = nfs4_get_open_state(inode, data->owner);
1136 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001137 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001138 if (data->o_res.delegation_type != 0)
1139 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001140 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001141 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001142 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001143out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001144 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001145err_put_inode:
1146 iput(inode);
1147err:
1148 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001149}
1150
Andy Adamsone23008e2012-10-02 21:07:32 -04001151static struct nfs4_state *
1152nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1153{
1154 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1155 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1156 return _nfs4_opendata_to_nfs4_state(data);
1157}
1158
Trond Myklebust864472e2006-01-03 09:55:15 +01001159static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1160{
1161 struct nfs_inode *nfsi = NFS_I(state->inode);
1162 struct nfs_open_context *ctx;
1163
1164 spin_lock(&state->inode->i_lock);
1165 list_for_each_entry(ctx, &nfsi->open_files, list) {
1166 if (ctx->state != state)
1167 continue;
1168 get_nfs_open_context(ctx);
1169 spin_unlock(&state->inode->i_lock);
1170 return ctx;
1171 }
1172 spin_unlock(&state->inode->i_lock);
1173 return ERR_PTR(-ENOENT);
1174}
1175
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001176static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1177{
1178 struct nfs4_opendata *opendata;
1179
Al Viro3d4ff432011-06-22 18:40:12 -04001180 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001181 if (opendata == NULL)
1182 return ERR_PTR(-ENOMEM);
1183 opendata->state = state;
1184 atomic_inc(&state->count);
1185 return opendata;
1186}
1187
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001188static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001189{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001190 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001191 int ret;
1192
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001193 opendata->o_arg.open_flags = 0;
1194 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001195 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1196 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1197 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001198 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001199 if (ret != 0)
1200 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001201 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001202 if (IS_ERR(newstate))
1203 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001204 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001205 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001206 return 0;
1207}
1208
1209static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1210{
Trond Myklebust864472e2006-01-03 09:55:15 +01001211 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001212 int ret;
1213
1214 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001215 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001216 smp_rmb();
1217 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001218 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001219 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001220 if (ret != 0)
1221 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001222 if (newstate != state)
1223 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001224 }
1225 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001226 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001227 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001228 if (ret != 0)
1229 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001230 if (newstate != state)
1231 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001232 }
1233 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001234 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001235 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001236 if (ret != 0)
1237 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001238 if (newstate != state)
1239 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001240 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001241 /*
1242 * We may have performed cached opens for all three recoveries.
1243 * Check if we need to update the current stateid.
1244 */
1245 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001246 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001247 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001248 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001249 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001250 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001251 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001252 return 0;
1253}
1254
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255/*
1256 * OPEN_RECLAIM:
1257 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001259static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001261 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001262 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001263 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 int status;
1265
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001266 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1267 if (IS_ERR(opendata))
1268 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001269 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1270 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001271 rcu_read_lock();
1272 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001273 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001274 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001275 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001276 opendata->o_arg.u.delegation_type = delegation_type;
1277 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001278 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 return status;
1280}
1281
Trond Myklebust539cd032007-06-05 11:46:42 -04001282static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283{
1284 struct nfs_server *server = NFS_SERVER(state->inode);
1285 struct nfs4_exception exception = { };
1286 int err;
1287 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001288 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001289 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001290 break;
1291 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 } while (exception.retry);
1293 return err;
1294}
1295
Trond Myklebust864472e2006-01-03 09:55:15 +01001296static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1297{
1298 struct nfs_open_context *ctx;
1299 int ret;
1300
1301 ctx = nfs4_state_find_open_context(state);
1302 if (IS_ERR(ctx))
1303 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001304 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001305 put_nfs_open_context(ctx);
1306 return ret;
1307}
1308
Trond Myklebust13437e12007-07-06 15:10:43 -04001309static 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 -07001310{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001311 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001312 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001314 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1315 if (IS_ERR(opendata))
1316 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001317 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebustf597c532012-03-04 18:13:56 -05001318 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust864472e2006-01-03 09:55:15 +01001319 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001320 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001321 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322}
1323
Trond Myklebust13437e12007-07-06 15:10:43 -04001324int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325{
1326 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001327 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 int err;
1329 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001330 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 switch (err) {
1332 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001333 case -ENOENT:
1334 case -ESTALE:
1335 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001336 case -NFS4ERR_BADSESSION:
1337 case -NFS4ERR_BADSLOT:
1338 case -NFS4ERR_BAD_HIGH_SLOT:
1339 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1340 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04001341 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001342 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 case -NFS4ERR_STALE_CLIENTID:
1344 case -NFS4ERR_STALE_STATEID:
1345 case -NFS4ERR_EXPIRED:
1346 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001347 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001348 goto out;
1349 case -ERESTARTSYS:
1350 /*
1351 * The show must go on: exit, but mark the
1352 * stateid as needing recovery.
1353 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001354 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001355 case -NFS4ERR_ADMIN_REVOKED:
1356 case -NFS4ERR_BAD_STATEID:
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001357 nfs_inode_find_state_and_recover(state->inode,
1358 stateid);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001359 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001360 case -EKEYEXPIRED:
1361 /*
1362 * User RPCSEC_GSS context has expired.
1363 * We cannot recover this stateid now, so
1364 * skip it and allow recovery thread to
1365 * proceed.
1366 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001367 case -ENOMEM:
1368 err = 0;
1369 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 }
1371 err = nfs4_handle_exception(server, err, &exception);
1372 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001373out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 return err;
1375}
1376
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001377static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1378{
1379 struct nfs4_opendata *data = calldata;
1380
1381 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001382 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001383 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001384 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001385 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001386 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001387 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001388}
1389
1390static void nfs4_open_confirm_release(void *calldata)
1391{
1392 struct nfs4_opendata *data = calldata;
1393 struct nfs4_state *state = NULL;
1394
1395 /* If this request hasn't been cancelled, do nothing */
1396 if (data->cancelled == 0)
1397 goto out_free;
1398 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001399 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001400 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001401 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001402 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001403 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001404out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001405 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001406}
1407
1408static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001409 .rpc_call_done = nfs4_open_confirm_done,
1410 .rpc_release = nfs4_open_confirm_release,
1411};
1412
1413/*
1414 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1415 */
1416static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1417{
1418 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1419 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001420 struct rpc_message msg = {
1421 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1422 .rpc_argp = &data->c_arg,
1423 .rpc_resp = &data->c_res,
1424 .rpc_cred = data->owner->so_cred,
1425 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001426 struct rpc_task_setup task_setup_data = {
1427 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001428 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001429 .callback_ops = &nfs4_open_confirm_ops,
1430 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001431 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001432 .flags = RPC_TASK_ASYNC,
1433 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 int status;
1435
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001436 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001437 data->rpc_done = 0;
1438 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001439 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001440 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001441 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001442 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001443 status = nfs4_wait_for_completion_rpc_task(task);
1444 if (status != 0) {
1445 data->cancelled = 1;
1446 smp_wmb();
1447 } else
1448 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001449 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 return status;
1451}
1452
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001453static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001455 struct nfs4_opendata *data = calldata;
1456 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001457
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001458 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1459 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001460 /*
1461 * Check if we still need to send an OPEN call, or if we can use
1462 * a delegation instead.
1463 */
1464 if (data->state != NULL) {
1465 struct nfs_delegation *delegation;
1466
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001467 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001468 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001469 rcu_read_lock();
1470 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001471 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1472 can_open_delegated(delegation, data->o_arg.fmode))
1473 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001474 rcu_read_unlock();
1475 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001476 /* Update client id. */
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001477 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001478 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001479 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Andy Adamsone23008e2012-10-02 21:07:32 -04001480 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001481 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1482 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001483 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001484 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001485 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001486 &data->o_res.seq_res,
1487 task) != 0)
1488 nfs_release_seqid(data->o_arg.seqid);
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 Myklebust6926afd2012-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 Myklebust6926afd2012-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);
Chuck Levera3ca5652012-03-01 17:00:40 -05002194out:
2195 dprintk("%s: done!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196}
2197
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002198static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002199 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002200 .rpc_call_done = nfs4_close_done,
2201 .rpc_release = nfs4_free_closedata,
2202};
2203
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204/*
2205 * It is possible for data to be read/written from a mem-mapped file
2206 * after the sys_close call (which hits the vfs layer as a flush).
2207 * This means that we can't safely call nfsv4 close on a file until
2208 * the inode is cleared. This in turn means that we are not good
2209 * NFSv4 citizens - we do not indicate to the server to update the file's
2210 * share state even when we are done with one of the three share
2211 * stateid's in the inode.
2212 *
2213 * NOTE: Caller must be holding the sp->so_owner semaphore!
2214 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002215int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002217 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002219 struct nfs4_state_owner *sp = state->owner;
2220 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002221 struct rpc_message msg = {
2222 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2223 .rpc_cred = state->owner->so_cred,
2224 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002225 struct rpc_task_setup task_setup_data = {
2226 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002227 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002228 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002229 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002230 .flags = RPC_TASK_ASYNC,
2231 };
Trond Myklebust95121352005-10-18 14:20:12 -07002232 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002234 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002236 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002237 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002238 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002240 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002241 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002243 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002244 if (calldata->arg.seqid == NULL)
2245 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002246 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002247 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002248 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002249 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002250 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002251 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002252 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002253
Trond Myklebust1174dd12010-12-21 10:52:24 -05002254 msg.rpc_argp = &calldata->arg;
2255 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002256 task_setup_data.callback_data = calldata;
2257 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002258 if (IS_ERR(task))
2259 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002260 status = 0;
2261 if (wait)
2262 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002263 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002264 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002265out_free_calldata:
2266 kfree(calldata);
2267out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002268 nfs4_put_open_state(state);
2269 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002270 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271}
2272
Trond Myklebust2b484292010-09-17 10:56:51 -04002273static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002274nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 struct nfs4_state *state;
2277
Trond Myklebust565277f2007-10-15 18:17:53 -04002278 /* Protect against concurrent sillydeletes */
Andy Adamson82be4172012-05-23 05:02:35 -04002279 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2280 ctx->cred, &ctx->mdsthreshold);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002281 if (IS_ERR(state))
2282 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002283 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002284 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285}
2286
Trond Myklebust1185a552009-12-03 15:54:02 -05002287static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002288{
2289 if (ctx->state == NULL)
2290 return;
2291 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002292 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002293 else
Al Viro643168c2011-06-22 18:20:23 -04002294 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002295}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296
2297static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2298{
Benny Halevy43652ad2009-04-01 09:21:54 -04002299 struct nfs4_server_caps_arg args = {
2300 .fhandle = fhandle,
2301 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 struct nfs4_server_caps_res res = {};
2303 struct rpc_message msg = {
2304 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002305 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 .rpc_resp = &res,
2307 };
2308 int status;
2309
Bryan Schumaker7c513052011-03-24 17:12:24 +00002310 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 if (status == 0) {
2312 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002313 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2314 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2315 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2316 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2317 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2319 server->caps |= NFS_CAP_ACLS;
2320 if (res.has_links != 0)
2321 server->caps |= NFS_CAP_HARDLINKS;
2322 if (res.has_symlinks != 0)
2323 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002324 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2325 server->caps |= NFS_CAP_FILEID;
2326 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2327 server->caps |= NFS_CAP_MODE;
2328 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2329 server->caps |= NFS_CAP_NLINK;
2330 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2331 server->caps |= NFS_CAP_OWNER;
2332 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2333 server->caps |= NFS_CAP_OWNER_GROUP;
2334 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2335 server->caps |= NFS_CAP_ATIME;
2336 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2337 server->caps |= NFS_CAP_CTIME;
2338 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2339 server->caps |= NFS_CAP_MTIME;
2340
Trond Myklebusta65318b2009-03-11 14:10:28 -04002341 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2342 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2343 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002345 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002347
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 return status;
2349}
2350
Trond Myklebust55a97592006-06-09 09:34:19 -04002351int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352{
2353 struct nfs4_exception exception = { };
2354 int err;
2355 do {
2356 err = nfs4_handle_exception(server,
2357 _nfs4_server_capabilities(server, fhandle),
2358 &exception);
2359 } while (exception.retry);
2360 return err;
2361}
2362
2363static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2364 struct nfs_fsinfo *info)
2365{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 struct nfs4_lookup_root_arg args = {
2367 .bitmask = nfs4_fattr_bitmap,
2368 };
2369 struct nfs4_lookup_res res = {
2370 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002371 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 .fh = fhandle,
2373 };
2374 struct rpc_message msg = {
2375 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2376 .rpc_argp = &args,
2377 .rpc_resp = &res,
2378 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002379
Trond Myklebust0e574af2005-10-27 22:12:38 -04002380 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002381 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382}
2383
2384static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2385 struct nfs_fsinfo *info)
2386{
2387 struct nfs4_exception exception = { };
2388 int err;
2389 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002390 err = _nfs4_lookup_root(server, fhandle, info);
2391 switch (err) {
2392 case 0:
2393 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002394 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002395 default:
2396 err = nfs4_handle_exception(server, err, &exception);
2397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002399out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 return err;
2401}
2402
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002403static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2404 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2405{
2406 struct rpc_auth *auth;
2407 int ret;
2408
2409 auth = rpcauth_create(flavor, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002410 if (IS_ERR(auth)) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002411 ret = -EIO;
2412 goto out;
2413 }
2414 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002415out:
2416 return ret;
2417}
2418
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002419static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002420 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002422 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002423 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002425 len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04002426 if (len < 0)
2427 return len;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002428
2429 for (i = 0; i < len; i++) {
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002430 /* AUTH_UNIX is the default flavor if none was specified,
2431 * thus has already been tried. */
2432 if (flav_array[i] == RPC_AUTH_UNIX)
2433 continue;
2434
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002435 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002436 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002437 continue;
2438 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002439 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002440 /*
2441 * -EACCESS could mean that the user doesn't have correct permissions
2442 * to access the mount. It could also mean that we tried to mount
2443 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2444 * existing mount programs don't handle -EACCES very well so it should
2445 * be mapped to -EPERM instead.
2446 */
2447 if (status == -EACCES)
2448 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002449 return status;
2450}
2451
2452/*
2453 * get the file handle for the "/" directory on the server
2454 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002455int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2456 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002457{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002458 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002459 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002460 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2461 /*
2462 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2463 * by nfs4_map_errors() as this function exits.
2464 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002465 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 if (status == 0)
2467 status = nfs4_server_capabilities(server, fhandle);
2468 if (status == 0)
2469 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002470 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471}
2472
Bryan Schumakerbae36242012-05-10 15:07:31 -04002473static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2474 struct nfs_fsinfo *info)
2475{
2476 int error;
2477 struct nfs_fattr *fattr = info->fattr;
2478
2479 error = nfs4_server_capabilities(server, mntfh);
2480 if (error < 0) {
2481 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2482 return error;
2483 }
2484
2485 error = nfs4_proc_getattr(server, mntfh, fattr);
2486 if (error < 0) {
2487 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2488 return error;
2489 }
2490
2491 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2492 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2493 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2494
2495 return error;
2496}
2497
Manoj Naik6b97fd32006-06-09 09:34:29 -04002498/*
2499 * Get locations and (maybe) other attributes of a referral.
2500 * Note that we'll actually follow the referral later when
2501 * we detect fsid mismatch in inode revalidation
2502 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002503static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2504 const struct qstr *name, struct nfs_fattr *fattr,
2505 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002506{
2507 int status = -ENOMEM;
2508 struct page *page = NULL;
2509 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002510
2511 page = alloc_page(GFP_KERNEL);
2512 if (page == NULL)
2513 goto out;
2514 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2515 if (locations == NULL)
2516 goto out;
2517
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002518 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002519 if (status != 0)
2520 goto out;
2521 /* Make sure server returned a different fsid for the referral */
2522 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002523 dprintk("%s: server did not return a different fsid for"
2524 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002525 status = -EIO;
2526 goto out;
2527 }
Andy Adamson533eb462011-06-13 18:25:56 -04002528 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2529 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002530
Andy Adamson533eb462011-06-13 18:25:56 -04002531 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002532 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002533 memset(fhandle, 0, sizeof(struct nfs_fh));
2534out:
2535 if (page)
2536 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002537 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002538 return status;
2539}
2540
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2542{
2543 struct nfs4_getattr_arg args = {
2544 .fh = fhandle,
2545 .bitmask = server->attr_bitmask,
2546 };
2547 struct nfs4_getattr_res res = {
2548 .fattr = fattr,
2549 .server = server,
2550 };
2551 struct rpc_message msg = {
2552 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2553 .rpc_argp = &args,
2554 .rpc_resp = &res,
2555 };
2556
Trond Myklebust0e574af2005-10-27 22:12:38 -04002557 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002558 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559}
2560
2561static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2562{
2563 struct nfs4_exception exception = { };
2564 int err;
2565 do {
2566 err = nfs4_handle_exception(server,
2567 _nfs4_proc_getattr(server, fhandle, fattr),
2568 &exception);
2569 } while (exception.retry);
2570 return err;
2571}
2572
2573/*
2574 * The file is not closed if it is opened due to the a request to change
2575 * the size of the file. The open call will not be needed once the
2576 * VFS layer lookup-intents are implemented.
2577 *
2578 * Close is called when the inode is destroyed.
2579 * If we haven't opened the file for O_WRONLY, we
2580 * need to in the size_change case to obtain a stateid.
2581 *
2582 * Got race?
2583 * Because OPEN is always done by name in nfsv4, it is
2584 * possible that we opened a different file by the same
2585 * name. We can recognize this race condition, but we
2586 * can't do anything about it besides returning an error.
2587 *
2588 * This will be fixed with VFS changes (lookup-intent).
2589 */
2590static int
2591nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2592 struct iattr *sattr)
2593{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002594 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002595 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002596 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 int status;
2598
Benny Halevy8a1636c2010-07-14 15:43:57 -04002599 if (pnfs_ld_layoutret_on_setattr(inode))
2600 pnfs_return_layout(inode);
2601
Trond Myklebust0e574af2005-10-27 22:12:38 -04002602 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603
Andy Adamson26699402012-05-30 16:12:24 -04002604 /* Deal with open(O_TRUNC) */
2605 if (sattr->ia_valid & ATTR_OPEN)
2606 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2607
2608 /* Optimization: if the end result is no change, don't RPC */
2609 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2610 return 0;
2611
Trond Myklebustd5308382005-11-04 15:33:38 -05002612 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002613 if (sattr->ia_valid & ATTR_FILE) {
2614 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002615
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002616 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002617 if (ctx) {
2618 cred = ctx->cred;
2619 state = ctx->state;
2620 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002621 }
2622
2623 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002624 if (status == 0)
2625 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 return status;
2627}
2628
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002629static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2630 const struct qstr *name, struct nfs_fh *fhandle,
2631 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002632{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002633 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002634 int status;
2635 struct nfs4_lookup_arg args = {
2636 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002637 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002638 .name = name,
2639 };
2640 struct nfs4_lookup_res res = {
2641 .server = server,
2642 .fattr = fattr,
2643 .fh = fhandle,
2644 };
2645 struct rpc_message msg = {
2646 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2647 .rpc_argp = &args,
2648 .rpc_resp = &res,
2649 };
2650
2651 nfs_fattr_init(fattr);
2652
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002654 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 dprintk("NFS reply lookup: %d\n", status);
2656 return status;
2657}
2658
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002659static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002660{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002661 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002662 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002663 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2664 fattr->nlink = 2;
2665}
2666
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002667static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2668 struct qstr *name, struct nfs_fh *fhandle,
2669 struct nfs_fattr *fattr)
2670{
2671 struct nfs4_exception exception = { };
2672 struct rpc_clnt *client = *clnt;
2673 int err;
2674 do {
2675 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2676 switch (err) {
2677 case -NFS4ERR_BADNAME:
2678 err = -ENOENT;
2679 goto out;
2680 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002681 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002682 goto out;
2683 case -NFS4ERR_WRONGSEC:
2684 err = -EPERM;
2685 if (client != *clnt)
2686 goto out;
2687
2688 client = nfs4_create_sec_client(client, dir, name);
2689 if (IS_ERR(client))
2690 return PTR_ERR(client);
2691
2692 exception.retry = 1;
2693 break;
2694 default:
2695 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2696 }
2697 } while (exception.retry);
2698
2699out:
2700 if (err == 0)
2701 *clnt = client;
2702 else if (client != *clnt)
2703 rpc_shutdown_client(client);
2704
2705 return err;
2706}
2707
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002708static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002709 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002711 int status;
2712 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002713
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002714 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2715 if (client != NFS_CLIENT(dir)) {
2716 rpc_shutdown_client(client);
2717 nfs_fixup_secinfo_attributes(fattr);
2718 }
2719 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720}
2721
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002722struct rpc_clnt *
2723nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2724 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2725{
2726 int status;
2727 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2728
2729 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2730 if (status < 0) {
2731 rpc_shutdown_client(client);
2732 return ERR_PTR(status);
2733 }
2734 return client;
2735}
2736
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2738{
Trond Myklebust76b32992007-08-10 17:45:11 -04002739 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 struct nfs4_accessargs args = {
2741 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002742 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002744 struct nfs4_accessres res = {
2745 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002746 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 struct rpc_message msg = {
2748 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2749 .rpc_argp = &args,
2750 .rpc_resp = &res,
2751 .rpc_cred = entry->cred,
2752 };
2753 int mode = entry->mask;
2754 int status;
2755
2756 /*
2757 * Determine which access bits we want to ask for...
2758 */
2759 if (mode & MAY_READ)
2760 args.access |= NFS4_ACCESS_READ;
2761 if (S_ISDIR(inode->i_mode)) {
2762 if (mode & MAY_WRITE)
2763 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2764 if (mode & MAY_EXEC)
2765 args.access |= NFS4_ACCESS_LOOKUP;
2766 } else {
2767 if (mode & MAY_WRITE)
2768 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2769 if (mode & MAY_EXEC)
2770 args.access |= NFS4_ACCESS_EXECUTE;
2771 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002772
2773 res.fattr = nfs_alloc_fattr();
2774 if (res.fattr == NULL)
2775 return -ENOMEM;
2776
Bryan Schumaker7c513052011-03-24 17:12:24 +00002777 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002779 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04002780 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002782 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 return status;
2784}
2785
2786static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2787{
2788 struct nfs4_exception exception = { };
2789 int err;
2790 do {
2791 err = nfs4_handle_exception(NFS_SERVER(inode),
2792 _nfs4_proc_access(inode, entry),
2793 &exception);
2794 } while (exception.retry);
2795 return err;
2796}
2797
2798/*
2799 * TODO: For the time being, we don't try to get any attributes
2800 * along with any of the zero-copy operations READ, READDIR,
2801 * READLINK, WRITE.
2802 *
2803 * In the case of the first three, we want to put the GETATTR
2804 * after the read-type operation -- this is because it is hard
2805 * to predict the length of a GETATTR response in v4, and thus
2806 * align the READ data correctly. This means that the GETATTR
2807 * may end up partially falling into the page cache, and we should
2808 * shift it into the 'tail' of the xdr_buf before processing.
2809 * To do this efficiently, we need to know the total length
2810 * of data received, which doesn't seem to be available outside
2811 * of the RPC layer.
2812 *
2813 * In the case of WRITE, we also want to put the GETATTR after
2814 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04002815 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 *
2817 * Both of these changes to the XDR layer would in fact be quite
2818 * minor, but I decided to leave them for a subsequent patch.
2819 */
2820static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2821 unsigned int pgbase, unsigned int pglen)
2822{
2823 struct nfs4_readlink args = {
2824 .fh = NFS_FH(inode),
2825 .pgbase = pgbase,
2826 .pglen = pglen,
2827 .pages = &page,
2828 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002829 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 struct rpc_message msg = {
2831 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2832 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002833 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 };
2835
Bryan Schumaker7c513052011-03-24 17:12:24 +00002836 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837}
2838
2839static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2840 unsigned int pgbase, unsigned int pglen)
2841{
2842 struct nfs4_exception exception = { };
2843 int err;
2844 do {
2845 err = nfs4_handle_exception(NFS_SERVER(inode),
2846 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2847 &exception);
2848 } while (exception.retry);
2849 return err;
2850}
2851
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002853 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855static int
2856nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002857 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858{
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002859 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 int status = 0;
2862
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002863 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
2864 if (IS_ERR(ctx))
2865 return PTR_ERR(ctx);
2866
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002867 sattr->ia_mode &= ~current_umask();
Trond Myklebust8626e4a2012-07-16 12:01:42 -04002868 state = nfs4_do_open(dir, dentry, ctx->mode,
2869 flags, sattr, ctx->cred,
2870 &ctx->mdsthreshold);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002871 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 if (IS_ERR(state)) {
2873 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002874 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002876 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002877 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002878 ctx->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879out:
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002880 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 return status;
2882}
2883
2884static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2885{
Trond Myklebust16e42952005-10-27 22:12:44 -04002886 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002887 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07002889 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002891 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002892 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002893 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002895 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2896 .rpc_argp = &args,
2897 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 };
Trond Myklebust778d2812012-04-27 13:48:19 -04002899 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04002900
Bryan Schumaker7c513052011-03-24 17:12:24 +00002901 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04002902 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04002903 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 return status;
2905}
2906
2907static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2908{
2909 struct nfs4_exception exception = { };
2910 int err;
2911 do {
2912 err = nfs4_handle_exception(NFS_SERVER(dir),
2913 _nfs4_proc_remove(dir, name),
2914 &exception);
2915 } while (exception.retry);
2916 return err;
2917}
2918
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002919static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002921 struct nfs_server *server = NFS_SERVER(dir);
2922 struct nfs_removeargs *args = msg->rpc_argp;
2923 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002925 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05002927 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928}
2929
Bryan Schumaker34e137c2012-03-19 14:54:41 -04002930static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
2931{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04002932 nfs4_setup_sequence(NFS_SERVER(data->dir),
2933 &data->args.seq_args,
2934 &data->res.seq_res,
2935 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936}
2937
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002938static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002940 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2941
Trond Myklebust14516c32010-07-31 14:29:06 -04002942 if (!nfs4_sequence_done(task, &res->seq_res))
2943 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002944 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002945 return 0;
2946 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002947 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948}
2949
Jeff Laytond3d41522010-09-17 17:31:57 -04002950static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2951{
2952 struct nfs_server *server = NFS_SERVER(dir);
2953 struct nfs_renameargs *arg = msg->rpc_argp;
2954 struct nfs_renameres *res = msg->rpc_resp;
2955
2956 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04002957 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002958 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04002959}
2960
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04002961static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
2962{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04002963 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
2964 &data->args.seq_args,
2965 &data->res.seq_res,
2966 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04002967}
2968
2969static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2970 struct inode *new_dir)
2971{
2972 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2973
2974 if (!nfs4_sequence_done(task, &res->seq_res))
2975 return 0;
2976 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2977 return 0;
2978
2979 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04002980 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04002981 return 1;
2982}
2983
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2985 struct inode *new_dir, struct qstr *new_name)
2986{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002987 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002988 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 .old_dir = NFS_FH(old_dir),
2990 .new_dir = NFS_FH(new_dir),
2991 .old_name = old_name,
2992 .new_name = new_name,
2993 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002994 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002995 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002996 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 struct rpc_message msg = {
2998 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2999 .rpc_argp = &arg,
3000 .rpc_resp = &res,
3001 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003002 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003
Bryan Schumaker7c513052011-03-24 17:12:24 +00003004 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 if (!status) {
3006 update_changeattr(old_dir, &res.old_cinfo);
3007 update_changeattr(new_dir, &res.new_cinfo);
3008 }
3009 return status;
3010}
3011
3012static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3013 struct inode *new_dir, struct qstr *new_name)
3014{
3015 struct nfs4_exception exception = { };
3016 int err;
3017 do {
3018 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3019 _nfs4_proc_rename(old_dir, old_name,
3020 new_dir, new_name),
3021 &exception);
3022 } while (exception.retry);
3023 return err;
3024}
3025
3026static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3027{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003028 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 struct nfs4_link_arg arg = {
3030 .fh = NFS_FH(inode),
3031 .dir_fh = NFS_FH(dir),
3032 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003033 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003035 struct nfs4_link_res res = {
3036 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003037 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 struct rpc_message msg = {
3039 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3040 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003041 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003043 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044
Trond Myklebust136f2622010-04-16 16:22:49 -04003045 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003046 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003047 goto out;
3048
Bryan Schumaker7c513052011-03-24 17:12:24 +00003049 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003050 if (!status) {
3051 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04003052 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003053 }
Trond Myklebust136f2622010-04-16 16:22:49 -04003054out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003055 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 return status;
3057}
3058
3059static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3060{
3061 struct nfs4_exception exception = { };
3062 int err;
3063 do {
3064 err = nfs4_handle_exception(NFS_SERVER(inode),
3065 _nfs4_proc_link(inode, dir, name),
3066 &exception);
3067 } while (exception.retry);
3068 return err;
3069}
3070
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003071struct nfs4_createdata {
3072 struct rpc_message msg;
3073 struct nfs4_create_arg arg;
3074 struct nfs4_create_res res;
3075 struct nfs_fh fh;
3076 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003077};
3078
3079static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3080 struct qstr *name, struct iattr *sattr, u32 ftype)
3081{
3082 struct nfs4_createdata *data;
3083
3084 data = kzalloc(sizeof(*data), GFP_KERNEL);
3085 if (data != NULL) {
3086 struct nfs_server *server = NFS_SERVER(dir);
3087
3088 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3089 data->msg.rpc_argp = &data->arg;
3090 data->msg.rpc_resp = &data->res;
3091 data->arg.dir_fh = NFS_FH(dir);
3092 data->arg.server = server;
3093 data->arg.name = name;
3094 data->arg.attrs = sattr;
3095 data->arg.ftype = ftype;
3096 data->arg.bitmask = server->attr_bitmask;
3097 data->res.server = server;
3098 data->res.fh = &data->fh;
3099 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003100 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003101 }
3102 return data;
3103}
3104
3105static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3106{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003107 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003108 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003109 if (status == 0) {
3110 update_changeattr(dir, &data->res.dir_cinfo);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003111 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3112 }
3113 return status;
3114}
3115
3116static void nfs4_free_createdata(struct nfs4_createdata *data)
3117{
3118 kfree(data);
3119}
3120
Chuck Lever4f390c12006-08-22 20:06:22 -04003121static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003122 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003124 struct nfs4_createdata *data;
3125 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126
Chuck Lever94a6d752006-08-22 20:06:23 -04003127 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003128 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003129
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003130 status = -ENOMEM;
3131 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3132 if (data == NULL)
3133 goto out;
3134
3135 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3136 data->arg.u.symlink.pages = &page;
3137 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003139 status = nfs4_do_create(dir, dentry, data);
3140
3141 nfs4_free_createdata(data);
3142out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 return status;
3144}
3145
Chuck Lever4f390c12006-08-22 20:06:22 -04003146static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003147 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148{
3149 struct nfs4_exception exception = { };
3150 int err;
3151 do {
3152 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003153 _nfs4_proc_symlink(dir, dentry, page,
3154 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 &exception);
3156 } while (exception.retry);
3157 return err;
3158}
3159
3160static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3161 struct iattr *sattr)
3162{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003163 struct nfs4_createdata *data;
3164 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003166 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3167 if (data == NULL)
3168 goto out;
3169
3170 status = nfs4_do_create(dir, dentry, data);
3171
3172 nfs4_free_createdata(data);
3173out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 return status;
3175}
3176
3177static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3178 struct iattr *sattr)
3179{
3180 struct nfs4_exception exception = { };
3181 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003182
3183 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 do {
3185 err = nfs4_handle_exception(NFS_SERVER(dir),
3186 _nfs4_proc_mkdir(dir, dentry, sattr),
3187 &exception);
3188 } while (exception.retry);
3189 return err;
3190}
3191
3192static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003193 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194{
3195 struct inode *dir = dentry->d_inode;
3196 struct nfs4_readdir_arg args = {
3197 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003198 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 .pgbase = 0,
3200 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003201 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003202 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 };
3204 struct nfs4_readdir_res res;
3205 struct rpc_message msg = {
3206 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3207 .rpc_argp = &args,
3208 .rpc_resp = &res,
3209 .rpc_cred = cred,
3210 };
3211 int status;
3212
Harvey Harrison3110ff82008-05-02 13:42:44 -07003213 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003214 dentry->d_parent->d_name.name,
3215 dentry->d_name.name,
3216 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003217 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003219 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebustac396122010-11-15 20:26:22 -05003220 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003221 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003222 status += args.pgbase;
3223 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003224
3225 nfs_invalidate_atime(dir);
3226
Harvey Harrison3110ff82008-05-02 13:42:44 -07003227 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 return status;
3229}
3230
3231static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003232 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233{
3234 struct nfs4_exception exception = { };
3235 int err;
3236 do {
3237 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3238 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003239 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 &exception);
3241 } while (exception.retry);
3242 return err;
3243}
3244
3245static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3246 struct iattr *sattr, dev_t rdev)
3247{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003248 struct nfs4_createdata *data;
3249 int mode = sattr->ia_mode;
3250 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003252 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3253 if (data == NULL)
3254 goto out;
3255
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003257 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003259 data->arg.ftype = NF4BLK;
3260 data->arg.u.device.specdata1 = MAJOR(rdev);
3261 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 }
3263 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003264 data->arg.ftype = NF4CHR;
3265 data->arg.u.device.specdata1 = MAJOR(rdev);
3266 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003267 } else if (!S_ISSOCK(mode)) {
3268 status = -EINVAL;
3269 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003272 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003273out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003274 nfs4_free_createdata(data);
3275out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 return status;
3277}
3278
3279static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3280 struct iattr *sattr, dev_t rdev)
3281{
3282 struct nfs4_exception exception = { };
3283 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003284
3285 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 do {
3287 err = nfs4_handle_exception(NFS_SERVER(dir),
3288 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3289 &exception);
3290 } while (exception.retry);
3291 return err;
3292}
3293
3294static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3295 struct nfs_fsstat *fsstat)
3296{
3297 struct nfs4_statfs_arg args = {
3298 .fh = fhandle,
3299 .bitmask = server->attr_bitmask,
3300 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003301 struct nfs4_statfs_res res = {
3302 .fsstat = fsstat,
3303 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 struct rpc_message msg = {
3305 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3306 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003307 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 };
3309
Trond Myklebust0e574af2005-10-27 22:12:38 -04003310 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003311 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312}
3313
3314static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3315{
3316 struct nfs4_exception exception = { };
3317 int err;
3318 do {
3319 err = nfs4_handle_exception(server,
3320 _nfs4_proc_statfs(server, fhandle, fsstat),
3321 &exception);
3322 } while (exception.retry);
3323 return err;
3324}
3325
3326static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3327 struct nfs_fsinfo *fsinfo)
3328{
3329 struct nfs4_fsinfo_arg args = {
3330 .fh = fhandle,
3331 .bitmask = server->attr_bitmask,
3332 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003333 struct nfs4_fsinfo_res res = {
3334 .fsinfo = fsinfo,
3335 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 struct rpc_message msg = {
3337 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3338 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003339 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 };
3341
Bryan Schumaker7c513052011-03-24 17:12:24 +00003342 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343}
3344
3345static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3346{
3347 struct nfs4_exception exception = { };
3348 int err;
3349
3350 do {
3351 err = nfs4_handle_exception(server,
3352 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3353 &exception);
3354 } while (exception.retry);
3355 return err;
3356}
3357
3358static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3359{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003360 int error;
3361
Trond Myklebust0e574af2005-10-27 22:12:38 -04003362 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003363 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003364 if (error == 0) {
3365 /* block layout checks this! */
3366 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003367 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003368 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003369
3370 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371}
3372
3373static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3374 struct nfs_pathconf *pathconf)
3375{
3376 struct nfs4_pathconf_arg args = {
3377 .fh = fhandle,
3378 .bitmask = server->attr_bitmask,
3379 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003380 struct nfs4_pathconf_res res = {
3381 .pathconf = pathconf,
3382 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 struct rpc_message msg = {
3384 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3385 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003386 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 };
3388
3389 /* None of the pathconf attributes are mandatory to implement */
3390 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3391 memset(pathconf, 0, sizeof(*pathconf));
3392 return 0;
3393 }
3394
Trond Myklebust0e574af2005-10-27 22:12:38 -04003395 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003396 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397}
3398
3399static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3400 struct nfs_pathconf *pathconf)
3401{
3402 struct nfs4_exception exception = { };
3403 int err;
3404
3405 do {
3406 err = nfs4_handle_exception(server,
3407 _nfs4_proc_pathconf(server, fhandle, pathconf),
3408 &exception);
3409 } while (exception.retry);
3410 return err;
3411}
3412
Benny Halevyd20581a2011-05-22 19:52:03 +03003413void __nfs4_read_done_cb(struct nfs_read_data *data)
3414{
Fred Isamancd841602012-04-20 14:47:44 -04003415 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003416}
3417
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003418static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419{
Fred Isamancd841602012-04-20 14:47:44 -04003420 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003422 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003423 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003424 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003426
Benny Halevyd20581a2011-05-22 19:52:03 +03003427 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003429 renew_lease(server, data->timestamp);
3430 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431}
3432
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003433static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3434{
3435
3436 dprintk("--> %s\n", __func__);
3437
3438 if (!nfs4_sequence_done(task, &data->res.seq_res))
3439 return -EAGAIN;
3440
Benny Halevyd20581a2011-05-22 19:52:03 +03003441 return data->read_done_cb ? data->read_done_cb(task, data) :
3442 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003443}
3444
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003445static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003448 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003449 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003450 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451}
3452
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003453static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3454{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003455 nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3456 &data->args.seq_args,
3457 &data->res.seq_res,
3458 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459}
3460
Fred Isamanb029bc92011-03-03 15:13:42 +00003461static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462{
Fred Isamancd841602012-04-20 14:47:44 -04003463 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003465 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003466 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003467 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003469 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003471 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003472 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003473 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474}
3475
Fred Isamanb029bc92011-03-03 15:13:42 +00003476static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3477{
3478 if (!nfs4_sequence_done(task, &data->res.seq_res))
3479 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003480 return data->write_done_cb ? data->write_done_cb(task, data) :
3481 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003482}
3483
Trond Myklebust5a37f852012-04-28 14:55:16 -04003484static
3485bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003486{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003487 const struct nfs_pgio_header *hdr = data->header;
3488
3489 /* Don't request attributes for pNFS or O_DIRECT writes */
3490 if (data->ds_clp != NULL || hdr->dreq != NULL)
3491 return false;
3492 /* Otherwise, request attributes if and only if we don't hold
3493 * a delegation
3494 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04003495 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003496}
Fred Isamana69aef12011-03-03 15:13:47 +00003497
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003498static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499{
Fred Isamancd841602012-04-20 14:47:44 -04003500 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003501
Trond Myklebust5a37f852012-04-28 14:55:16 -04003502 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003503 data->args.bitmask = NULL;
3504 data->res.fattr = NULL;
3505 } else
3506 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003507
Fred Isamanb029bc92011-03-03 15:13:42 +00003508 if (!data->write_done_cb)
3509 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003510 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 data->timestamp = jiffies;
3512
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003513 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003514 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515}
3516
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003517static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3518{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003519 nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3520 &data->args.seq_args,
3521 &data->res.seq_res,
3522 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523}
3524
Fred Isaman0b7c0152012-04-20 14:47:39 -04003525static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3526{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003527 nfs4_setup_sequence(NFS_SERVER(data->inode),
3528 &data->args.seq_args,
3529 &data->res.seq_res,
3530 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531}
3532
Fred Isaman0b7c0152012-04-20 14:47:39 -04003533static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003536
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003537 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003538 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003539 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003541 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542}
3543
Fred Isaman0b7c0152012-04-20 14:47:39 -04003544static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003545{
3546 if (!nfs4_sequence_done(task, &data->res.seq_res))
3547 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003548 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003549}
3550
Fred Isaman0b7c0152012-04-20 14:47:39 -04003551static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003553 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003554
Fred Isaman0b7c0152012-04-20 14:47:39 -04003555 if (data->commit_done_cb == NULL)
3556 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003557 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003558 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003559 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560}
3561
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003562struct nfs4_renewdata {
3563 struct nfs_client *client;
3564 unsigned long timestamp;
3565};
3566
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567/*
3568 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3569 * standalone procedure for queueing an asynchronous RENEW.
3570 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003571static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003572{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003573 struct nfs4_renewdata *data = calldata;
3574 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003575
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003576 if (atomic_read(&clp->cl_count) > 1)
3577 nfs4_schedule_state_renewal(clp);
3578 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003579 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003580}
3581
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003582static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003584 struct nfs4_renewdata *data = calldata;
3585 struct nfs_client *clp = data->client;
3586 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587
3588 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003589 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003590 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3591 return;
3592 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003593 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003594 return;
3595 }
3596 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003598 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599}
3600
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003601static const struct rpc_call_ops nfs4_renew_ops = {
3602 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003603 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003604};
3605
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003606static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607{
3608 struct rpc_message msg = {
3609 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3610 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003611 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003613 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003615 if (renew_flags == 0)
3616 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003617 if (!atomic_inc_not_zero(&clp->cl_count))
3618 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003619 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003620 if (data == NULL)
3621 return -ENOMEM;
3622 data->client = clp;
3623 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003625 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626}
3627
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003628static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629{
3630 struct rpc_message msg = {
3631 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3632 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003633 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634 };
3635 unsigned long now = jiffies;
3636 int status;
3637
3638 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3639 if (status < 0)
3640 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003641 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642 return 0;
3643}
3644
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003645static inline int nfs4_server_supports_acls(struct nfs_server *server)
3646{
3647 return (server->caps & NFS_CAP_ACLS)
3648 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3649 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3650}
3651
Trond Myklebust21f498c2012-08-24 10:59:25 -04003652/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3653 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003654 * the stack.
3655 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04003656#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003657
Neil Hormane9e3d722011-03-04 19:26:03 -05003658static int buf_to_pages_noslab(const void *buf, size_t buflen,
3659 struct page **pages, unsigned int *pgbase)
3660{
3661 struct page *newpage, **spages;
3662 int rc = 0;
3663 size_t len;
3664 spages = pages;
3665
3666 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04003667 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003668 newpage = alloc_page(GFP_KERNEL);
3669
3670 if (newpage == NULL)
3671 goto unwind;
3672 memcpy(page_address(newpage), buf, len);
3673 buf += len;
3674 buflen -= len;
3675 *pages++ = newpage;
3676 rc++;
3677 } while (buflen != 0);
3678
3679 return rc;
3680
3681unwind:
3682 for(; rc > 0; rc--)
3683 __free_page(spages[rc-1]);
3684 return -ENOMEM;
3685}
3686
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003687struct nfs4_cached_acl {
3688 int cached;
3689 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003690 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003691};
3692
3693static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003694{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003695 struct nfs_inode *nfsi = NFS_I(inode);
3696
3697 spin_lock(&inode->i_lock);
3698 kfree(nfsi->nfs4_acl);
3699 nfsi->nfs4_acl = acl;
3700 spin_unlock(&inode->i_lock);
3701}
3702
3703static void nfs4_zap_acl_attr(struct inode *inode)
3704{
3705 nfs4_set_cached_acl(inode, NULL);
3706}
3707
3708static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3709{
3710 struct nfs_inode *nfsi = NFS_I(inode);
3711 struct nfs4_cached_acl *acl;
3712 int ret = -ENOENT;
3713
3714 spin_lock(&inode->i_lock);
3715 acl = nfsi->nfs4_acl;
3716 if (acl == NULL)
3717 goto out;
3718 if (buf == NULL) /* user is just asking for length */
3719 goto out_len;
3720 if (acl->cached == 0)
3721 goto out;
3722 ret = -ERANGE; /* see getxattr(2) man page */
3723 if (acl->len > buflen)
3724 goto out;
3725 memcpy(buf, acl->data, acl->len);
3726out_len:
3727 ret = acl->len;
3728out:
3729 spin_unlock(&inode->i_lock);
3730 return ret;
3731}
3732
Sachin Prabhu5794d212012-04-17 14:36:40 +01003733static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003734{
3735 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003736 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003737
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003738 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003739 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003740 if (acl == NULL)
3741 goto out;
3742 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003743 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003744 } else {
3745 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3746 if (acl == NULL)
3747 goto out;
3748 acl->cached = 0;
3749 }
3750 acl->len = acl_len;
3751out:
3752 nfs4_set_cached_acl(inode, acl);
3753}
3754
Andy Adamsonbf118a32011-12-07 11:55:27 -05003755/*
3756 * The getxattr API returns the required buffer length when called with a
3757 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3758 * the required buf. On a NULL buf, we send a page of data to the server
3759 * guessing that the ACL request can be serviced by a page. If so, we cache
3760 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3761 * the cache. If not so, we throw away the page, and cache the required
3762 * length. The next getxattr call will then produce another round trip to
3763 * the server, this time with the input buf of the required size.
3764 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003765static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003766{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003767 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003768 struct nfs_getaclargs args = {
3769 .fh = NFS_FH(inode),
3770 .acl_pages = pages,
3771 .acl_len = buflen,
3772 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003773 struct nfs_getaclres res = {
3774 .acl_len = buflen,
3775 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003776 struct rpc_message msg = {
3777 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3778 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003779 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003780 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003781 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3782 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003783
Andy Adamsonbf118a32011-12-07 11:55:27 -05003784 /* As long as we're doing a round trip to the server anyway,
3785 * let's be prepared for a page of acl data. */
3786 if (npages == 0)
3787 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003788 if (npages > ARRAY_SIZE(pages))
3789 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003790
Andy Adamsonbf118a32011-12-07 11:55:27 -05003791 for (i = 0; i < npages; i++) {
3792 pages[i] = alloc_page(GFP_KERNEL);
3793 if (!pages[i])
3794 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003795 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01003796
3797 /* for decoding across pages */
3798 res.acl_scratch = alloc_page(GFP_KERNEL);
3799 if (!res.acl_scratch)
3800 goto out_free;
3801
Andy Adamsonbf118a32011-12-07 11:55:27 -05003802 args.acl_len = npages * PAGE_SIZE;
3803 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003804
Peng Taode040be2012-01-10 22:42:47 +08003805 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003806 __func__, buf, buflen, npages, args.acl_len);
3807 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3808 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003809 if (ret)
3810 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003811
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003812 /* Handle the case where the passed-in buffer is too short */
3813 if (res.acl_flags & NFS4_ACL_TRUNC) {
3814 /* Did the user only issue a request for the acl length? */
3815 if (buf == NULL)
3816 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003817 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003818 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003819 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003820 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
3821 if (buf)
3822 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
3823out_ok:
3824 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003825out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003826 for (i = 0; i < npages; i++)
3827 if (pages[i])
3828 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05003829 if (res.acl_scratch)
3830 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003831 return ret;
3832}
3833
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003834static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3835{
3836 struct nfs4_exception exception = { };
3837 ssize_t ret;
3838 do {
3839 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3840 if (ret >= 0)
3841 break;
3842 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3843 } while (exception.retry);
3844 return ret;
3845}
3846
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003847static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3848{
3849 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003850 int ret;
3851
3852 if (!nfs4_server_supports_acls(server))
3853 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003854 ret = nfs_revalidate_inode(server, inode);
3855 if (ret < 0)
3856 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003857 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3858 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003859 ret = nfs4_read_cached_acl(inode, buf, buflen);
3860 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003861 /* -ENOENT is returned if there is no ACL or if there is an ACL
3862 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003863 return ret;
3864 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003865}
3866
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003867static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003868{
3869 struct nfs_server *server = NFS_SERVER(inode);
3870 struct page *pages[NFS4ACL_MAXPAGES];
3871 struct nfs_setaclargs arg = {
3872 .fh = NFS_FH(inode),
3873 .acl_pages = pages,
3874 .acl_len = buflen,
3875 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003876 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003877 struct rpc_message msg = {
3878 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3879 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003880 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003881 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003882 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05003883 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003884
3885 if (!nfs4_server_supports_acls(server))
3886 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003887 if (npages > ARRAY_SIZE(pages))
3888 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05003889 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3890 if (i < 0)
3891 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04003892 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003893 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003894
3895 /*
3896 * Free each page after tx, so the only ref left is
3897 * held by the network stack
3898 */
3899 for (; i > 0; i--)
3900 put_page(pages[i-1]);
3901
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003902 /*
3903 * Acl update can result in inode attribute update.
3904 * so mark the attribute cache invalid.
3905 */
3906 spin_lock(&inode->i_lock);
3907 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3908 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003909 nfs_access_zap_cache(inode);
3910 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003911 return ret;
3912}
3913
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003914static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3915{
3916 struct nfs4_exception exception = { };
3917 int err;
3918 do {
3919 err = nfs4_handle_exception(NFS_SERVER(inode),
3920 __nfs4_proc_set_acl(inode, buf, buflen),
3921 &exception);
3922 } while (exception.retry);
3923 return err;
3924}
3925
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003927nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003929 struct nfs_client *clp = server->nfs_client;
3930
3931 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932 return 0;
3933 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05003934 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003935 case -NFS4ERR_ADMIN_REVOKED:
3936 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04003937 if (state == NULL)
3938 break;
3939 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003940 case -NFS4ERR_OPENMODE:
3941 if (state == NULL)
3942 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003943 nfs4_schedule_stateid_recovery(server, state);
3944 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003945 case -NFS4ERR_EXPIRED:
3946 if (state != NULL)
3947 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003949 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003950 nfs4_schedule_lease_recovery(clp);
3951 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003952#if defined(CONFIG_NFS_V4_1)
3953 case -NFS4ERR_BADSESSION:
3954 case -NFS4ERR_BADSLOT:
3955 case -NFS4ERR_BAD_HIGH_SLOT:
3956 case -NFS4ERR_DEADSESSION:
3957 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3958 case -NFS4ERR_SEQ_FALSE_RETRY:
3959 case -NFS4ERR_SEQ_MISORDERED:
3960 dprintk("%s ERROR %d, Reset session\n", __func__,
3961 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04003962 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04003963 task->tk_status = 0;
3964 return -EAGAIN;
3965#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003967 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003968 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003969 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3971 task->tk_status = 0;
3972 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04003973 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974 case -NFS4ERR_OLD_STATEID:
3975 task->tk_status = 0;
3976 return -EAGAIN;
3977 }
3978 task->tk_status = nfs4_map_errors(task->tk_status);
3979 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003980wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003981 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003982 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3983 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3984 task->tk_status = 0;
3985 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986}
3987
Chuck Leverf0920752012-05-21 22:45:41 -04003988static void nfs4_init_boot_verifier(const struct nfs_client *clp,
3989 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05003990{
3991 __be32 verf[2];
3992
Chuck Lever2c820d92012-05-21 22:45:33 -04003993 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
3994 /* An impossible timestamp guarantees this value
3995 * will never match a generated boot time. */
3996 verf[0] = 0;
3997 verf[1] = (__be32)(NSEC_PER_SEC + 1);
3998 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04003999 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4000 verf[0] = (__be32)nn->boot_time.tv_sec;
4001 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04004002 }
Chuck Levercd937102012-03-02 17:14:31 -05004003 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4004}
4005
Chuck Levere984a552012-09-14 17:24:21 -04004006static unsigned int
4007nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4008 char *buf, size_t len)
4009{
4010 unsigned int result;
4011
4012 rcu_read_lock();
4013 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4014 clp->cl_ipaddr,
4015 rpc_peeraddr2str(clp->cl_rpcclient,
4016 RPC_DISPLAY_ADDR),
4017 rpc_peeraddr2str(clp->cl_rpcclient,
4018 RPC_DISPLAY_PROTO));
4019 rcu_read_unlock();
4020 return result;
4021}
4022
4023static unsigned int
4024nfs4_init_uniform_client_string(const struct nfs_client *clp,
4025 char *buf, size_t len)
4026{
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004027 char *nodename = clp->cl_rpcclient->cl_nodename;
4028
4029 if (nfs4_client_id_uniquifier[0] != '\0')
4030 nodename = nfs4_client_id_uniquifier;
Chuck Levere984a552012-09-14 17:24:21 -04004031 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4032 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004033 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004034}
4035
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004036/**
4037 * nfs4_proc_setclientid - Negotiate client ID
4038 * @clp: state data structure
4039 * @program: RPC program for NFSv4 callback service
4040 * @port: IP port number for NFS4 callback service
4041 * @cred: RPC credential to use for this call
4042 * @res: where to place the result
4043 *
4044 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4045 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004046int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4047 unsigned short port, struct rpc_cred *cred,
4048 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049{
4050 nfs4_verifier sc_verifier;
4051 struct nfs4_setclientid setclientid = {
4052 .sc_verifier = &sc_verifier,
4053 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004054 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055 };
4056 struct rpc_message msg = {
4057 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4058 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004059 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004060 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004062 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063
Chuck Leverde734832012-07-11 16:30:50 -04004064 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004065 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004066 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4067 setclientid.sc_name_len =
4068 nfs4_init_uniform_client_string(clp,
4069 setclientid.sc_name,
4070 sizeof(setclientid.sc_name));
4071 else
4072 setclientid.sc_name_len =
4073 nfs4_init_nonuniform_client_string(clp,
4074 setclientid.sc_name,
4075 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004076 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004077 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004078 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004079 sizeof(setclientid.sc_netid),
4080 rpc_peeraddr2str(clp->cl_rpcclient,
4081 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004082 rcu_read_unlock();
4083 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004084 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 clp->cl_ipaddr, port >> 8, port & 255);
4086
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004087 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4088 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4089 setclientid.sc_name_len, setclientid.sc_name);
4090 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4091 dprintk("NFS reply setclientid: %d\n", status);
4092 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093}
4094
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004095/**
4096 * nfs4_proc_setclientid_confirm - Confirm client ID
4097 * @clp: state data structure
4098 * @res: result of a previous SETCLIENTID
4099 * @cred: RPC credential to use for this call
4100 *
4101 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4102 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004103int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004104 struct nfs4_setclientid_res *arg,
4105 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106{
4107 struct nfs_fsinfo fsinfo;
4108 struct rpc_message msg = {
4109 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004110 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004112 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113 };
4114 unsigned long now;
4115 int status;
4116
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004117 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4118 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4119 clp->cl_clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004121 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122 if (status == 0) {
4123 spin_lock(&clp->cl_lock);
4124 clp->cl_lease_time = fsinfo.lease_time * HZ;
4125 clp->cl_last_renewal = now;
4126 spin_unlock(&clp->cl_lock);
4127 }
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004128 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 return status;
4130}
4131
Trond Myklebustfe650402006-01-03 09:55:18 +01004132struct nfs4_delegreturndata {
4133 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004134 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004135 struct nfs_fh fh;
4136 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004137 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004138 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004139 int rpc_status;
4140};
4141
Trond Myklebustfe650402006-01-03 09:55:18 +01004142static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4143{
4144 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004145
Trond Myklebust14516c32010-07-31 14:29:06 -04004146 if (!nfs4_sequence_done(task, &data->res.seq_res))
4147 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004148
Ricardo Labiaga79708862009-12-07 09:23:21 -05004149 switch (task->tk_status) {
4150 case -NFS4ERR_STALE_STATEID:
4151 case -NFS4ERR_EXPIRED:
4152 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004153 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004154 break;
4155 default:
4156 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4157 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004158 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004159 return;
4160 }
4161 }
4162 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004163}
4164
4165static void nfs4_delegreturn_release(void *calldata)
4166{
Trond Myklebustfe650402006-01-03 09:55:18 +01004167 kfree(calldata);
4168}
4169
Andy Adamson938e1012009-04-01 09:22:28 -04004170#if defined(CONFIG_NFS_V4_1)
4171static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4172{
4173 struct nfs4_delegreturndata *d_data;
4174
4175 d_data = (struct nfs4_delegreturndata *)data;
4176
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004177 nfs4_setup_sequence(d_data->res.server,
4178 &d_data->args.seq_args,
4179 &d_data->res.seq_res,
4180 task);
Andy Adamson938e1012009-04-01 09:22:28 -04004181}
4182#endif /* CONFIG_NFS_V4_1 */
4183
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004184static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004185#if defined(CONFIG_NFS_V4_1)
4186 .rpc_call_prepare = nfs4_delegreturn_prepare,
4187#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004188 .rpc_call_done = nfs4_delegreturn_done,
4189 .rpc_release = nfs4_delegreturn_release,
4190};
4191
Trond Myklebuste6f81072008-01-24 18:14:34 -05004192static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004193{
4194 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004195 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004196 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004197 struct rpc_message msg = {
4198 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4199 .rpc_cred = cred,
4200 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004201 struct rpc_task_setup task_setup_data = {
4202 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004203 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004204 .callback_ops = &nfs4_delegreturn_ops,
4205 .flags = RPC_TASK_ASYNC,
4206 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004207 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004208
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004209 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004210 if (data == NULL)
4211 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004212 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004213 data->args.fhandle = &data->fh;
4214 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004215 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004216 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004217 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004218 data->res.fattr = &data->fattr;
4219 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004220 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004221 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004222 data->rpc_status = 0;
4223
Trond Myklebustc970aa82007-07-14 15:39:59 -04004224 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004225 msg.rpc_argp = &data->args;
4226 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004227 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004228 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004229 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004230 if (!issync)
4231 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004232 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004233 if (status != 0)
4234 goto out;
4235 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004236 if (status == 0)
4237 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4238 else
4239 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004240out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004241 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004242 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243}
4244
Trond Myklebuste6f81072008-01-24 18:14:34 -05004245int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246{
4247 struct nfs_server *server = NFS_SERVER(inode);
4248 struct nfs4_exception exception = { };
4249 int err;
4250 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004251 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252 switch (err) {
4253 case -NFS4ERR_STALE_STATEID:
4254 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255 case 0:
4256 return 0;
4257 }
4258 err = nfs4_handle_exception(server, err, &exception);
4259 } while (exception.retry);
4260 return err;
4261}
4262
4263#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4264#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4265
4266/*
4267 * sleep, with exponential backoff, and retry the LOCK operation.
4268 */
4269static unsigned long
4270nfs4_set_lock_task_retry(unsigned long timeout)
4271{
Jeff Laytond3103102011-12-01 22:44:39 +01004272 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273 timeout <<= 1;
4274 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4275 return NFS4_LOCK_MAXTIMEOUT;
4276 return timeout;
4277}
4278
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4280{
4281 struct inode *inode = state->inode;
4282 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004283 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004284 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004286 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004288 struct nfs_lockt_res res = {
4289 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 };
4291 struct rpc_message msg = {
4292 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4293 .rpc_argp = &arg,
4294 .rpc_resp = &res,
4295 .rpc_cred = state->owner->so_cred,
4296 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 struct nfs4_lock_state *lsp;
4298 int status;
4299
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004300 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004301 status = nfs4_set_lock_state(state, request);
4302 if (status != 0)
4303 goto out;
4304 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004305 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004306 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004307 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004308 switch (status) {
4309 case 0:
4310 request->fl_type = F_UNLCK;
4311 break;
4312 case -NFS4ERR_DENIED:
4313 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004315 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004316out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317 return status;
4318}
4319
4320static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4321{
4322 struct nfs4_exception exception = { };
4323 int err;
4324
4325 do {
4326 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4327 _nfs4_proc_getlk(state, cmd, request),
4328 &exception);
4329 } while (exception.retry);
4330 return err;
4331}
4332
4333static int do_vfs_lock(struct file *file, struct file_lock *fl)
4334{
4335 int res = 0;
4336 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4337 case FL_POSIX:
4338 res = posix_lock_file_wait(file, fl);
4339 break;
4340 case FL_FLOCK:
4341 res = flock_lock_file_wait(file, fl);
4342 break;
4343 default:
4344 BUG();
4345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346 return res;
4347}
4348
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004349struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004350 struct nfs_locku_args arg;
4351 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004352 struct nfs4_lock_state *lsp;
4353 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004354 struct file_lock fl;
4355 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004356 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004357};
4358
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004359static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4360 struct nfs_open_context *ctx,
4361 struct nfs4_lock_state *lsp,
4362 struct nfs_seqid *seqid)
4363{
4364 struct nfs4_unlockdata *p;
4365 struct inode *inode = lsp->ls_state->inode;
4366
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004367 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004368 if (p == NULL)
4369 return NULL;
4370 p->arg.fh = NFS_FH(inode);
4371 p->arg.fl = &p->fl;
4372 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004373 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004374 p->arg.stateid = &lsp->ls_stateid;
4375 p->lsp = lsp;
4376 atomic_inc(&lsp->ls_count);
4377 /* Ensure we don't close file until we're done freeing locks! */
4378 p->ctx = get_nfs_open_context(ctx);
4379 memcpy(&p->fl, fl, sizeof(p->fl));
4380 p->server = NFS_SERVER(inode);
4381 return p;
4382}
4383
Trond Myklebust06f814a2006-01-03 09:55:07 +01004384static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004385{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004386 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004387 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004388 nfs4_put_lock_state(calldata->lsp);
4389 put_nfs_open_context(calldata->ctx);
4390 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004391}
4392
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004393static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004394{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004395 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004396
Trond Myklebust14516c32010-07-31 14:29:06 -04004397 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4398 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004399 switch (task->tk_status) {
4400 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004401 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4402 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004403 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004404 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004405 case -NFS4ERR_BAD_STATEID:
4406 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004407 case -NFS4ERR_STALE_STATEID:
4408 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004409 break;
4410 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004411 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004412 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004413 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04004414 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004415}
4416
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004417static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004418{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004419 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004421 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004422 return;
Trond Myklebust795a88c2012-09-10 13:26:49 -04004423 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004424 /* Note: exit _without_ running nfs4_locku_done */
4425 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004426 return;
4427 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004428 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004429 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004430 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004431 &calldata->res.seq_res,
4432 task) != 0)
4433 nfs_release_seqid(calldata->arg.seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434}
4435
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004436static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004437 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004438 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004439 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004440};
4441
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004442static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4443 struct nfs_open_context *ctx,
4444 struct nfs4_lock_state *lsp,
4445 struct nfs_seqid *seqid)
4446{
4447 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004448 struct rpc_message msg = {
4449 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4450 .rpc_cred = ctx->cred,
4451 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004452 struct rpc_task_setup task_setup_data = {
4453 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004454 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004455 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004456 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004457 .flags = RPC_TASK_ASYNC,
4458 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004459
Frank Filz137d6ac2007-07-09 15:32:29 -07004460 /* Ensure this is an unlock - when canceling a lock, the
4461 * canceled lock is passed in, and it won't be an unlock.
4462 */
4463 fl->fl_type = F_UNLCK;
4464
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004465 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4466 if (data == NULL) {
4467 nfs_free_seqid(seqid);
4468 return ERR_PTR(-ENOMEM);
4469 }
4470
Trond Myklebust9d12b212012-01-17 22:04:25 -05004471 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004472 msg.rpc_argp = &data->arg;
4473 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004474 task_setup_data.callback_data = data;
4475 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004476}
4477
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4479{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004480 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004481 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004482 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004483 struct rpc_task *task;
4484 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004485 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486
Trond Myklebust9b073572006-06-29 16:38:34 -04004487 status = nfs4_set_lock_state(state, request);
4488 /* Unlock _before_ we do the RPC call */
4489 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004490 down_read(&nfsi->rwsem);
4491 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4492 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004493 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004494 }
4495 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004496 if (status != 0)
4497 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004498 /* Is this a delegated lock? */
4499 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004500 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004501 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004502 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004503 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004504 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004505 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004506 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004507 status = PTR_ERR(task);
4508 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004509 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004510 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004511 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004512out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004513 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004514 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515}
4516
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004517struct nfs4_lockdata {
4518 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004519 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004520 struct nfs4_lock_state *lsp;
4521 struct nfs_open_context *ctx;
4522 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004523 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004524 int rpc_status;
4525 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004526 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004527};
4528
4529static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004530 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4531 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004532{
4533 struct nfs4_lockdata *p;
4534 struct inode *inode = lsp->ls_state->inode;
4535 struct nfs_server *server = NFS_SERVER(inode);
4536
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004537 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004538 if (p == NULL)
4539 return NULL;
4540
4541 p->arg.fh = NFS_FH(inode);
4542 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004543 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004544 if (p->arg.open_seqid == NULL)
4545 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004546 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004547 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004548 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004549 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004550 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004551 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004552 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004553 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004554 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004555 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004556 atomic_inc(&lsp->ls_count);
4557 p->ctx = get_nfs_open_context(ctx);
4558 memcpy(&p->fl, fl, sizeof(p->fl));
4559 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004560out_free_seqid:
4561 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004562out_free:
4563 kfree(p);
4564 return NULL;
4565}
4566
4567static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4568{
4569 struct nfs4_lockdata *data = calldata;
4570 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571
Harvey Harrison3110ff82008-05-02 13:42:44 -07004572 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004573 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4574 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004575 /* Do we need to do an open_to_lock_owner? */
4576 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004577 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004578 goto out_release_lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004579 data->arg.open_stateid = &state->stateid;
4580 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004581 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004582 } else
4583 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004584 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004585 if (nfs4_setup_sequence(data->server,
4586 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004587 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004588 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04004589 return;
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004590 nfs_release_seqid(data->arg.open_seqid);
4591out_release_lock_seqid:
4592 nfs_release_seqid(data->arg.lock_seqid);
4593 dprintk("%s: done!, ret = %d\n", __func__, task->tk_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004594}
4595
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004596static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4597{
4598 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4599 nfs4_lock_prepare(task, calldata);
4600}
4601
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004602static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4603{
4604 struct nfs4_lockdata *data = calldata;
4605
Harvey Harrison3110ff82008-05-02 13:42:44 -07004606 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004607
Trond Myklebust14516c32010-07-31 14:29:06 -04004608 if (!nfs4_sequence_done(task, &data->res.seq_res))
4609 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004610
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004611 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004612 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004613 if (data->rpc_status == 0)
4614 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4615 else
4616 goto out;
4617 }
4618 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004619 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004620 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04004621 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004622 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004623out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004624 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004625}
4626
4627static void nfs4_lock_release(void *calldata)
4628{
4629 struct nfs4_lockdata *data = calldata;
4630
Harvey Harrison3110ff82008-05-02 13:42:44 -07004631 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004632 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004633 if (data->cancelled != 0) {
4634 struct rpc_task *task;
4635 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4636 data->arg.lock_seqid);
4637 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004638 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004639 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004640 } else
4641 nfs_free_seqid(data->arg.lock_seqid);
4642 nfs4_put_lock_state(data->lsp);
4643 put_nfs_open_context(data->ctx);
4644 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004645 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004646}
4647
4648static const struct rpc_call_ops nfs4_lock_ops = {
4649 .rpc_call_prepare = nfs4_lock_prepare,
4650 .rpc_call_done = nfs4_lock_done,
4651 .rpc_release = nfs4_lock_release,
4652};
4653
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004654static const struct rpc_call_ops nfs4_recover_lock_ops = {
4655 .rpc_call_prepare = nfs4_recover_lock_prepare,
4656 .rpc_call_done = nfs4_lock_done,
4657 .rpc_release = nfs4_lock_release,
4658};
4659
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004660static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4661{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004662 switch (error) {
4663 case -NFS4ERR_ADMIN_REVOKED:
4664 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004665 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004666 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04004667 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004668 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004669 break;
4670 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004671 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004672 case -NFS4ERR_EXPIRED:
4673 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004674 };
4675}
4676
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004677static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004678{
4679 struct nfs4_lockdata *data;
4680 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004681 struct rpc_message msg = {
4682 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4683 .rpc_cred = state->owner->so_cred,
4684 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004685 struct rpc_task_setup task_setup_data = {
4686 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004687 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004688 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004689 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004690 .flags = RPC_TASK_ASYNC,
4691 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004692 int ret;
4693
Harvey Harrison3110ff82008-05-02 13:42:44 -07004694 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004695 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004696 fl->fl_u.nfs4_fl.owner,
4697 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004698 if (data == NULL)
4699 return -ENOMEM;
4700 if (IS_SETLKW(cmd))
4701 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004702 if (recovery_type > NFS_LOCK_NEW) {
4703 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004704 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004705 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4706 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05004707 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004708 msg.rpc_argp = &data->arg;
4709 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004710 task_setup_data.callback_data = data;
4711 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004712 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004713 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004714 ret = nfs4_wait_for_completion_rpc_task(task);
4715 if (ret == 0) {
4716 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004717 if (ret)
4718 nfs4_handle_setlk_error(data->server, data->lsp,
4719 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004720 } else
4721 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004722 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004723 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004724 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725}
4726
4727static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4728{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004729 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004730 struct nfs4_exception exception = {
4731 .inode = state->inode,
4732 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004733 int err;
4734
4735 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004736 /* Cache the lock if possible... */
4737 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4738 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004739 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004740 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004741 break;
4742 nfs4_handle_exception(server, err, &exception);
4743 } while (exception.retry);
4744 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745}
4746
4747static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4748{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004749 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004750 struct nfs4_exception exception = {
4751 .inode = state->inode,
4752 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004753 int err;
4754
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004755 err = nfs4_set_lock_state(state, request);
4756 if (err != 0)
4757 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004758 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004759 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4760 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004761 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004762 switch (err) {
4763 default:
4764 goto out;
4765 case -NFS4ERR_GRACE:
4766 case -NFS4ERR_DELAY:
4767 nfs4_handle_exception(server, err, &exception);
4768 err = 0;
4769 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004770 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004771out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004772 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773}
4774
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004775#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004776/**
4777 * nfs41_check_expired_locks - possibly free a lock stateid
4778 *
4779 * @state: NFSv4 state for an inode
4780 *
4781 * Returns NFS_OK if recovery for this stateid is now finished.
4782 * Otherwise a negative NFS4ERR value is returned.
4783 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004784static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004785{
Chuck Levereb64cf92012-07-11 16:30:05 -04004786 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004787 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004788 struct nfs_server *server = NFS_SERVER(state->inode);
4789
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004790 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04004791 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004792 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4793 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004794 /* Free the stateid unless the server
4795 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04004796 if (status != -NFS4ERR_BAD_STATEID)
4797 nfs41_free_stateid(server,
4798 &lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004799 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004800 ret = status;
4801 }
4802 }
4803 };
4804
4805 return ret;
4806}
4807
4808static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4809{
4810 int status = NFS_OK;
4811
4812 if (test_bit(LK_STATE_IN_USE, &state->flags))
4813 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04004814 if (status != NFS_OK)
4815 status = nfs4_lock_expired(state, request);
4816 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004817}
4818#endif
4819
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4821{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004822 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004823 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004824 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004826 if ((fl_flags & FL_POSIX) &&
4827 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4828 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004829 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004830 status = nfs4_set_lock_state(state, request);
4831 if (status != 0)
4832 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004833 request->fl_flags |= FL_ACCESS;
4834 status = do_vfs_lock(request->fl_file, request);
4835 if (status < 0)
4836 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004837 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004838 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004839 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004840 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004841 request->fl_flags = fl_flags & ~FL_SLEEP;
4842 status = do_vfs_lock(request->fl_file, request);
4843 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004844 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004845 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004846 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004847 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004848 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004849 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004850 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004851 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4852 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004853out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004854 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004855out:
4856 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857 return status;
4858}
4859
4860static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4861{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004862 struct nfs4_exception exception = {
4863 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04004864 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004865 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866 int err;
4867
4868 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004869 err = _nfs4_proc_setlk(state, cmd, request);
4870 if (err == -NFS4ERR_DENIED)
4871 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004873 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874 } while (exception.retry);
4875 return err;
4876}
4877
4878static int
4879nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4880{
4881 struct nfs_open_context *ctx;
4882 struct nfs4_state *state;
4883 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4884 int status;
4885
4886 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004887 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888 state = ctx->state;
4889
4890 if (request->fl_start < 0 || request->fl_end < 0)
4891 return -EINVAL;
4892
Trond Myklebustd9531262009-07-21 19:22:38 -04004893 if (IS_GETLK(cmd)) {
4894 if (state != NULL)
4895 return nfs4_proc_getlk(state, F_GETLK, request);
4896 return 0;
4897 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898
4899 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4900 return -EINVAL;
4901
Trond Myklebustd9531262009-07-21 19:22:38 -04004902 if (request->fl_type == F_UNLCK) {
4903 if (state != NULL)
4904 return nfs4_proc_unlck(state, cmd, request);
4905 return 0;
4906 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907
Trond Myklebustd9531262009-07-21 19:22:38 -04004908 if (state == NULL)
4909 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04004910 /*
4911 * Don't rely on the VFS having checked the file open mode,
4912 * since it won't do this for flock() locks.
4913 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04004914 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04004915 case F_RDLCK:
4916 if (!(filp->f_mode & FMODE_READ))
4917 return -EBADF;
4918 break;
4919 case F_WRLCK:
4920 if (!(filp->f_mode & FMODE_WRITE))
4921 return -EBADF;
4922 }
4923
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924 do {
4925 status = nfs4_proc_setlk(state, cmd, request);
4926 if ((status != -EAGAIN) || IS_SETLK(cmd))
4927 break;
4928 timeout = nfs4_set_lock_task_retry(timeout);
4929 status = -ERESTARTSYS;
4930 if (signalled())
4931 break;
4932 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933 return status;
4934}
4935
Trond Myklebust888e6942005-11-04 15:38:11 -05004936int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4937{
4938 struct nfs_server *server = NFS_SERVER(state->inode);
4939 struct nfs4_exception exception = { };
4940 int err;
4941
4942 err = nfs4_set_lock_state(state, fl);
4943 if (err != 0)
4944 goto out;
4945 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004946 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004947 switch (err) {
4948 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004949 printk(KERN_ERR "NFS: %s: unhandled error "
4950 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004951 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004952 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004953 goto out;
4954 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004955 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07004956 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004957 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004958 nfs4_schedule_lease_recovery(server->nfs_client);
4959 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004960 case -NFS4ERR_BADSESSION:
4961 case -NFS4ERR_BADSLOT:
4962 case -NFS4ERR_BAD_HIGH_SLOT:
4963 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4964 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04004965 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004966 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004967 case -ERESTARTSYS:
4968 /*
4969 * The show must go on: exit, but mark the
4970 * stateid as needing recovery.
4971 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004972 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004973 case -NFS4ERR_ADMIN_REVOKED:
4974 case -NFS4ERR_BAD_STATEID:
4975 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004976 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004977 err = 0;
4978 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004979 case -EKEYEXPIRED:
4980 /*
4981 * User RPCSEC_GSS context has expired.
4982 * We cannot recover this stateid now, so
4983 * skip it and allow recovery thread to
4984 * proceed.
4985 */
4986 err = 0;
4987 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004988 case -ENOMEM:
4989 case -NFS4ERR_DENIED:
4990 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4991 err = 0;
4992 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004993 case -NFS4ERR_DELAY:
4994 break;
4995 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004996 err = nfs4_handle_exception(server, err, &exception);
4997 } while (exception.retry);
4998out:
4999 return err;
5000}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005001
Trond Myklebustcf470c32012-03-07 13:49:12 -05005002struct nfs_release_lockowner_data {
5003 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005004 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005005 struct nfs_release_lockowner_args args;
5006};
5007
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005008static void nfs4_release_lockowner_release(void *calldata)
5009{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005010 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005011 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005012 kfree(calldata);
5013}
5014
Trond Myklebust17280172012-03-11 13:11:00 -04005015static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005016 .rpc_release = nfs4_release_lockowner_release,
5017};
5018
Trond Myklebustcf470c32012-03-07 13:49:12 -05005019int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005020{
5021 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005022 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005023 struct rpc_message msg = {
5024 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5025 };
5026
5027 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005028 return -EINVAL;
5029 data = kmalloc(sizeof(*data), GFP_NOFS);
5030 if (!data)
5031 return -ENOMEM;
5032 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005033 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005034 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5035 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5036 data->args.lock_owner.s_dev = server->s_dev;
5037 msg.rpc_argp = &data->args;
5038 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5039 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005040}
5041
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005042#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5043
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005044static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5045 const void *buf, size_t buflen,
5046 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005047{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005048 if (strcmp(key, "") != 0)
5049 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005050
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005051 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005052}
5053
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005054static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5055 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005056{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005057 if (strcmp(key, "") != 0)
5058 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005059
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005060 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005061}
5062
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005063static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5064 size_t list_len, const char *name,
5065 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005066{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005067 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005068
J. Bruce Fields096455a2006-03-20 23:23:42 -05005069 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5070 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005071
5072 if (list && len <= list_len)
5073 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005074 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005075}
5076
Andy Adamson533eb462011-06-13 18:25:56 -04005077/*
5078 * nfs_fhget will use either the mounted_on_fileid or the fileid
5079 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005080static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5081{
Andy Adamson533eb462011-06-13 18:25:56 -04005082 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5083 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5084 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005085 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005086 return;
5087
5088 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005089 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005090 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5091 fattr->nlink = 2;
5092}
5093
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005094static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5095 const struct qstr *name,
5096 struct nfs4_fs_locations *fs_locations,
5097 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005098{
5099 struct nfs_server *server = NFS_SERVER(dir);
5100 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005101 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005102 };
5103 struct nfs4_fs_locations_arg args = {
5104 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005105 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005106 .page = page,
5107 .bitmask = bitmask,
5108 };
Benny Halevy22958462009-04-01 09:22:02 -04005109 struct nfs4_fs_locations_res res = {
5110 .fs_locations = fs_locations,
5111 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005112 struct rpc_message msg = {
5113 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5114 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005115 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005116 };
5117 int status;
5118
Harvey Harrison3110ff82008-05-02 13:42:44 -07005119 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005120
5121 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5122 * is not supported */
5123 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5124 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5125 else
5126 bitmask[0] |= FATTR4_WORD0_FILEID;
5127
Trond Myklebustc228fd32007-01-13 02:28:11 -05005128 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005129 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005130 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005131 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005132 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005133 return status;
5134}
5135
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005136int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5137 const struct qstr *name,
5138 struct nfs4_fs_locations *fs_locations,
5139 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005140{
5141 struct nfs4_exception exception = { };
5142 int err;
5143 do {
5144 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005145 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005146 &exception);
5147 } while (exception.retry);
5148 return err;
5149}
5150
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005151static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5152{
5153 int status;
5154 struct nfs4_secinfo_arg args = {
5155 .dir_fh = NFS_FH(dir),
5156 .name = name,
5157 };
5158 struct nfs4_secinfo_res res = {
5159 .flavors = flavors,
5160 };
5161 struct rpc_message msg = {
5162 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5163 .rpc_argp = &args,
5164 .rpc_resp = &res,
5165 };
5166
5167 dprintk("NFS call secinfo %s\n", name->name);
5168 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5169 dprintk("NFS reply secinfo: %d\n", status);
5170 return status;
5171}
5172
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005173int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5174 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005175{
5176 struct nfs4_exception exception = { };
5177 int err;
5178 do {
5179 err = nfs4_handle_exception(NFS_SERVER(dir),
5180 _nfs4_proc_secinfo(dir, name, flavors),
5181 &exception);
5182 } while (exception.retry);
5183 return err;
5184}
5185
Andy Adamson557134a2009-04-01 09:21:53 -04005186#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005187/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005188 * Check the exchange flags returned by the server for invalid flags, having
5189 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5190 * DS flags set.
5191 */
5192static int nfs4_check_cl_exchange_flags(u32 flags)
5193{
5194 if (flags & ~EXCHGID4_FLAG_MASK_R)
5195 goto out_inval;
5196 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5197 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5198 goto out_inval;
5199 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5200 goto out_inval;
5201 return NFS_OK;
5202out_inval:
5203 return -NFS4ERR_INVAL;
5204}
5205
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005206static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005207nfs41_same_server_scope(struct nfs41_server_scope *a,
5208 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005209{
5210 if (a->server_scope_sz == b->server_scope_sz &&
5211 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5212 return true;
5213
5214 return false;
5215}
5216
Andy Adamson357f54d2010-12-14 10:11:57 -05005217/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005218 * nfs4_proc_bind_conn_to_session()
5219 *
5220 * The 4.1 client currently uses the same TCP connection for the
5221 * fore and backchannel.
5222 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005223int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005224{
5225 int status;
5226 struct nfs41_bind_conn_to_session_res res;
5227 struct rpc_message msg = {
5228 .rpc_proc =
5229 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5230 .rpc_argp = clp,
5231 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005232 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005233 };
5234
5235 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005236
5237 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5238 if (unlikely(res.session == NULL)) {
5239 status = -ENOMEM;
5240 goto out;
5241 }
5242
5243 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5244 if (status == 0) {
5245 if (memcmp(res.session->sess_id.data,
5246 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5247 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5248 status = -EIO;
5249 goto out_session;
5250 }
5251 if (res.dir != NFS4_CDFS4_BOTH) {
5252 dprintk("NFS: %s: Unexpected direction from server\n",
5253 __func__);
5254 status = -EIO;
5255 goto out_session;
5256 }
5257 if (res.use_conn_in_rdma_mode) {
5258 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5259 __func__);
5260 status = -EIO;
5261 goto out_session;
5262 }
5263 }
5264out_session:
5265 kfree(res.session);
5266out:
5267 dprintk("<-- %s status= %d\n", __func__, status);
5268 return status;
5269}
5270
5271/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005272 * nfs4_proc_exchange_id()
5273 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005274 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5275 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04005276 * Since the clientid has expired, all compounds using sessions
5277 * associated with the stale clientid will be returning
5278 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5279 * be in some phase of session reset.
5280 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005281int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005282{
5283 nfs4_verifier verifier;
5284 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005285 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005286 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005287 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005288 };
5289 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005290 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005291 };
5292 int status;
5293 struct rpc_message msg = {
5294 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5295 .rpc_argp = &args,
5296 .rpc_resp = &res,
5297 .rpc_cred = cred,
5298 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005299
Chuck Leverf0920752012-05-21 22:45:41 -04005300 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04005301 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5302 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005303 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5304 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5305 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005306
Chuck Leveracdeb692012-05-21 22:46:16 -04005307 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005308 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005309 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005310 status = -ENOMEM;
5311 goto out;
5312 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005313
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005314 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005315 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005316 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005317 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005318 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005319 }
5320
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005321 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005322 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005323 status = -ENOMEM;
5324 goto out_server_scope;
5325 }
5326
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005327 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005328 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005329 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005330
Chuck Lever177313f2012-05-21 22:44:58 -04005331 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005332 clp->cl_clientid = res.clientid;
5333 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5334 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5335 clp->cl_seqid = res.seqid;
5336
Chuck Leveracdeb692012-05-21 22:46:16 -04005337 kfree(clp->cl_serverowner);
5338 clp->cl_serverowner = res.server_owner;
5339 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005340
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005341 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005342 kfree(clp->cl_implid);
5343 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005344
Chuck Lever177313f2012-05-21 22:44:58 -04005345 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005346 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005347 res.server_scope)) {
5348 dprintk("%s: server_scope mismatch detected\n",
5349 __func__);
5350 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005351 kfree(clp->cl_serverscope);
5352 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005353 }
5354
Chuck Lever177313f2012-05-21 22:44:58 -04005355 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005356 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005357 goto out;
5358 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005359 } else
5360 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005361
Chuck Leveracdeb692012-05-21 22:46:16 -04005362out_server_owner:
5363 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005364out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005365 kfree(res.server_scope);
5366out:
Chuck Lever177313f2012-05-21 22:44:58 -04005367 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005368 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005369 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005370 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04005371 clp->cl_implid->date.seconds,
5372 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005373 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005374 return status;
5375}
5376
Trond Myklebust66245532012-05-25 17:18:09 -04005377static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5378 struct rpc_cred *cred)
5379{
5380 struct rpc_message msg = {
5381 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5382 .rpc_argp = clp,
5383 .rpc_cred = cred,
5384 };
5385 int status;
5386
5387 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5388 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04005389 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04005390 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5391 return status;
5392}
5393
5394static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5395 struct rpc_cred *cred)
5396{
5397 unsigned int loop;
5398 int ret;
5399
5400 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5401 ret = _nfs4_proc_destroy_clientid(clp, cred);
5402 switch (ret) {
5403 case -NFS4ERR_DELAY:
5404 case -NFS4ERR_CLIENTID_BUSY:
5405 ssleep(1);
5406 break;
5407 default:
5408 return ret;
5409 }
5410 }
5411 return 0;
5412}
5413
5414int nfs4_destroy_clientid(struct nfs_client *clp)
5415{
5416 struct rpc_cred *cred;
5417 int ret = 0;
5418
5419 if (clp->cl_mvops->minor_version < 1)
5420 goto out;
5421 if (clp->cl_exchange_flags == 0)
5422 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04005423 if (clp->cl_preserve_clid)
5424 goto out;
Trond Myklebust66245532012-05-25 17:18:09 -04005425 cred = nfs4_get_exchange_id_cred(clp);
5426 ret = nfs4_proc_destroy_clientid(clp, cred);
5427 if (cred)
5428 put_rpccred(cred);
5429 switch (ret) {
5430 case 0:
5431 case -NFS4ERR_STALE_CLIENTID:
5432 clp->cl_exchange_flags = 0;
5433 }
5434out:
5435 return ret;
5436}
5437
Andy Adamson2050f0c2009-04-01 09:22:30 -04005438struct nfs4_get_lease_time_data {
5439 struct nfs4_get_lease_time_args *args;
5440 struct nfs4_get_lease_time_res *res;
5441 struct nfs_client *clp;
5442};
5443
5444static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5445 void *calldata)
5446{
Andy Adamson2050f0c2009-04-01 09:22:30 -04005447 struct nfs4_get_lease_time_data *data =
5448 (struct nfs4_get_lease_time_data *)calldata;
5449
5450 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005451 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005452 /* just setup sequence, do not trigger session recovery
5453 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005454 nfs41_setup_sequence(data->clp->cl_session,
5455 &data->args->la_seq_args,
5456 &data->res->lr_seq_res,
5457 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005458 dprintk("<-- %s\n", __func__);
5459}
5460
5461/*
5462 * Called from nfs4_state_manager thread for session setup, so don't recover
5463 * from sequence operation or clientid errors.
5464 */
5465static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5466{
5467 struct nfs4_get_lease_time_data *data =
5468 (struct nfs4_get_lease_time_data *)calldata;
5469
5470 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005471 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5472 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005473 switch (task->tk_status) {
5474 case -NFS4ERR_DELAY:
5475 case -NFS4ERR_GRACE:
5476 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5477 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5478 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005479 /* fall through */
5480 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005481 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005482 return;
5483 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005484 dprintk("<-- %s\n", __func__);
5485}
5486
Trond Myklebust17280172012-03-11 13:11:00 -04005487static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005488 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5489 .rpc_call_done = nfs4_get_lease_time_done,
5490};
5491
5492int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5493{
5494 struct rpc_task *task;
5495 struct nfs4_get_lease_time_args args;
5496 struct nfs4_get_lease_time_res res = {
5497 .lr_fsinfo = fsinfo,
5498 };
5499 struct nfs4_get_lease_time_data data = {
5500 .args = &args,
5501 .res = &res,
5502 .clp = clp,
5503 };
5504 struct rpc_message msg = {
5505 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5506 .rpc_argp = &args,
5507 .rpc_resp = &res,
5508 };
5509 struct rpc_task_setup task_setup = {
5510 .rpc_client = clp->cl_rpcclient,
5511 .rpc_message = &msg,
5512 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005513 .callback_data = &data,
5514 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005515 };
5516 int status;
5517
Trond Myklebust9d12b212012-01-17 22:04:25 -05005518 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005519 dprintk("--> %s\n", __func__);
5520 task = rpc_run_task(&task_setup);
5521
5522 if (IS_ERR(task))
5523 status = PTR_ERR(task);
5524 else {
5525 status = task->tk_status;
5526 rpc_put_task(task);
5527 }
5528 dprintk("<-- %s return %d\n", __func__, status);
5529
5530 return status;
5531}
5532
Andy Adamsonfc931582009-04-01 09:22:31 -04005533/*
5534 * Initialize the values to be used by the client in CREATE_SESSION
5535 * If nfs4_init_session set the fore channel request and response sizes,
5536 * use them.
5537 *
5538 * Set the back channel max_resp_sz_cached to zero to force the client to
5539 * always set csa_cachethis to FALSE because the current implementation
5540 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5541 */
5542static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5543{
5544 struct nfs4_session *session = args->client->cl_session;
Trond Myklebustae72ae672012-11-20 11:02:55 -05005545 unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
5546 mxresp_sz = session->fc_target_max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005547
5548 if (mxrqst_sz == 0)
5549 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5550 if (mxresp_sz == 0)
5551 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5552 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005553 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5554 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005555 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005556 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005557
5558 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005559 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005560 __func__,
5561 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005562 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005563
5564 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005565 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5566 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5567 args->bc_attrs.max_resp_sz_cached = 0;
5568 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5569 args->bc_attrs.max_reqs = 1;
5570
5571 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5572 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5573 __func__,
5574 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5575 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5576 args->bc_attrs.max_reqs);
5577}
5578
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005579static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005580{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005581 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5582 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5583
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005584 if (rcvd->max_resp_sz > sent->max_resp_sz)
5585 return -EINVAL;
5586 /*
5587 * Our requested max_ops is the minimum we need; we're not
5588 * prepared to break up compounds into smaller pieces than that.
5589 * So, no point even trying to continue if the server won't
5590 * cooperate:
5591 */
5592 if (rcvd->max_ops < sent->max_ops)
5593 return -EINVAL;
5594 if (rcvd->max_reqs == 0)
5595 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005596 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5597 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005598 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005599}
5600
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005601static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5602{
5603 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5604 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005605
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005606 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5607 return -EINVAL;
5608 if (rcvd->max_resp_sz < sent->max_resp_sz)
5609 return -EINVAL;
5610 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5611 return -EINVAL;
5612 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005613 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005614 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005615 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005616 return -EINVAL;
5617 return 0;
5618}
Andy Adamson8d353012009-04-01 09:22:32 -04005619
Andy Adamson8d353012009-04-01 09:22:32 -04005620static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5621 struct nfs4_session *session)
5622{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005623 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005624
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005625 ret = nfs4_verify_fore_channel_attrs(args, session);
5626 if (ret)
5627 return ret;
5628 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005629}
5630
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005631static int _nfs4_proc_create_session(struct nfs_client *clp,
5632 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005633{
5634 struct nfs4_session *session = clp->cl_session;
5635 struct nfs41_create_session_args args = {
5636 .client = clp,
5637 .cb_program = NFS4_CALLBACK,
5638 };
5639 struct nfs41_create_session_res res = {
5640 .client = clp,
5641 };
5642 struct rpc_message msg = {
5643 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5644 .rpc_argp = &args,
5645 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005646 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04005647 };
5648 int status;
5649
5650 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005651 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005652
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005653 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005654
Trond Myklebust43095d32012-11-20 11:13:12 -05005655 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04005656 /* Verify the session's negotiated channel_attrs values */
5657 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005658 /* Increment the clientid slot sequence id */
5659 clp->cl_seqid++;
5660 }
5661
5662 return status;
5663}
5664
5665/*
5666 * Issues a CREATE_SESSION operation to the server.
5667 * It is the responsibility of the caller to verify the session is
5668 * expired before calling this routine.
5669 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005670int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005671{
5672 int status;
5673 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005674 struct nfs4_session *session = clp->cl_session;
5675
5676 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5677
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005678 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04005679 if (status)
5680 goto out;
5681
Andy Adamsonaacd5532011-11-09 13:58:21 -05005682 /* Init or reset the session slot tables */
5683 status = nfs4_setup_session_slot_tables(session);
5684 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005685 if (status)
5686 goto out;
5687
5688 ptr = (unsigned *)&session->sess_id.data[0];
5689 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5690 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005691out:
5692 dprintk("<-- %s\n", __func__);
5693 return status;
5694}
5695
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005696/*
5697 * Issue the over-the-wire RPC DESTROY_SESSION.
5698 * The caller must serialize access to this routine.
5699 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005700int nfs4_proc_destroy_session(struct nfs4_session *session,
5701 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005702{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005703 struct rpc_message msg = {
5704 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5705 .rpc_argp = session,
5706 .rpc_cred = cred,
5707 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005708 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005709
5710 dprintk("--> nfs4_proc_destroy_session\n");
5711
5712 /* session is still being setup */
5713 if (session->clp->cl_cons_state != NFS_CS_READY)
5714 return status;
5715
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005716 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005717
5718 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04005719 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005720 "Session has been destroyed regardless...\n", status);
5721
5722 dprintk("<-- nfs4_proc_destroy_session\n");
5723 return status;
5724}
5725
Trond Myklebust7b38c362012-05-23 13:23:31 -04005726/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005727 * Renew the cl_session lease.
5728 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005729struct nfs4_sequence_data {
5730 struct nfs_client *clp;
5731 struct nfs4_sequence_args args;
5732 struct nfs4_sequence_res res;
5733};
5734
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005735static void nfs41_sequence_release(void *data)
5736{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005737 struct nfs4_sequence_data *calldata = data;
5738 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005739
Alexandros Batsakis71358402010-02-05 03:45:05 -08005740 if (atomic_read(&clp->cl_count) > 1)
5741 nfs4_schedule_state_renewal(clp);
5742 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005743 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005744}
5745
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005746static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5747{
5748 switch(task->tk_status) {
5749 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005750 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5751 return -EAGAIN;
5752 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005753 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005754 }
5755 return 0;
5756}
5757
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005758static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005759{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005760 struct nfs4_sequence_data *calldata = data;
5761 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005762
Trond Myklebust14516c32010-07-31 14:29:06 -04005763 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5764 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005765
5766 if (task->tk_status < 0) {
5767 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005768 if (atomic_read(&clp->cl_count) == 1)
5769 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005770
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005771 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5772 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005773 return;
5774 }
5775 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005776 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005777out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005778 dprintk("<-- %s\n", __func__);
5779}
5780
5781static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5782{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005783 struct nfs4_sequence_data *calldata = data;
5784 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005785 struct nfs4_sequence_args *args;
5786 struct nfs4_sequence_res *res;
5787
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005788 args = task->tk_msg.rpc_argp;
5789 res = task->tk_msg.rpc_resp;
5790
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005791 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005792}
5793
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05005794static void nfs41_sequence_prepare_privileged(struct rpc_task *task, void *data)
5795{
5796 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
5797 nfs41_sequence_prepare(task, data);
5798}
5799
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005800static const struct rpc_call_ops nfs41_sequence_ops = {
5801 .rpc_call_done = nfs41_sequence_call_done,
5802 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005803 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005804};
5805
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05005806static const struct rpc_call_ops nfs41_sequence_privileged_ops = {
5807 .rpc_call_done = nfs41_sequence_call_done,
5808 .rpc_call_prepare = nfs41_sequence_prepare_privileged,
5809 .rpc_release = nfs41_sequence_release,
5810};
5811
5812static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred,
5813 const struct rpc_call_ops *seq_ops)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005814{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005815 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005816 struct rpc_message msg = {
5817 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5818 .rpc_cred = cred,
5819 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005820 struct rpc_task_setup task_setup_data = {
5821 .rpc_client = clp->cl_rpcclient,
5822 .rpc_message = &msg,
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05005823 .callback_ops = seq_ops,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005824 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5825 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005826
Alexandros Batsakis71358402010-02-05 03:45:05 -08005827 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005828 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005829 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005830 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005831 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005832 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005833 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05005834 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005835 msg.rpc_argp = &calldata->args;
5836 msg.rpc_resp = &calldata->res;
5837 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005838 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005839
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005840 return rpc_run_task(&task_setup_data);
5841}
5842
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005843static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005844{
5845 struct rpc_task *task;
5846 int ret = 0;
5847
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005848 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5849 return 0;
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05005850 task = _nfs41_proc_sequence(clp, cred, &nfs41_sequence_ops);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005851 if (IS_ERR(task))
5852 ret = PTR_ERR(task);
5853 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005854 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005855 dprintk("<-- %s status=%d\n", __func__, ret);
5856 return ret;
5857}
5858
5859static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5860{
5861 struct rpc_task *task;
5862 int ret;
5863
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05005864 task = _nfs41_proc_sequence(clp, cred, &nfs41_sequence_privileged_ops);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005865 if (IS_ERR(task)) {
5866 ret = PTR_ERR(task);
5867 goto out;
5868 }
5869 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005870 if (!ret) {
5871 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5872
5873 if (task->tk_status == 0)
5874 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005875 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005876 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005877 rpc_put_task(task);
5878out:
5879 dprintk("<-- %s status=%d\n", __func__, ret);
5880 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005881}
5882
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005883struct nfs4_reclaim_complete_data {
5884 struct nfs_client *clp;
5885 struct nfs41_reclaim_complete_args arg;
5886 struct nfs41_reclaim_complete_res res;
5887};
5888
5889static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5890{
5891 struct nfs4_reclaim_complete_data *calldata = data;
5892
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005893 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005894 nfs41_setup_sequence(calldata->clp->cl_session,
5895 &calldata->arg.seq_args,
5896 &calldata->res.seq_res,
5897 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005898}
5899
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005900static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5901{
5902 switch(task->tk_status) {
5903 case 0:
5904 case -NFS4ERR_COMPLETE_ALREADY:
5905 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5906 break;
5907 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005908 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005909 /* fall through */
5910 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005911 return -EAGAIN;
5912 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005913 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005914 }
5915 return 0;
5916}
5917
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005918static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5919{
5920 struct nfs4_reclaim_complete_data *calldata = data;
5921 struct nfs_client *clp = calldata->clp;
5922 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5923
5924 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005925 if (!nfs41_sequence_done(task, res))
5926 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005927
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005928 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5929 rpc_restart_call_prepare(task);
5930 return;
5931 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005932 dprintk("<-- %s\n", __func__);
5933}
5934
5935static void nfs4_free_reclaim_complete_data(void *data)
5936{
5937 struct nfs4_reclaim_complete_data *calldata = data;
5938
5939 kfree(calldata);
5940}
5941
5942static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5943 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5944 .rpc_call_done = nfs4_reclaim_complete_done,
5945 .rpc_release = nfs4_free_reclaim_complete_data,
5946};
5947
5948/*
5949 * Issue a global reclaim complete.
5950 */
5951static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5952{
5953 struct nfs4_reclaim_complete_data *calldata;
5954 struct rpc_task *task;
5955 struct rpc_message msg = {
5956 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5957 };
5958 struct rpc_task_setup task_setup_data = {
5959 .rpc_client = clp->cl_rpcclient,
5960 .rpc_message = &msg,
5961 .callback_ops = &nfs4_reclaim_complete_call_ops,
5962 .flags = RPC_TASK_ASYNC,
5963 };
5964 int status = -ENOMEM;
5965
5966 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005967 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005968 if (calldata == NULL)
5969 goto out;
5970 calldata->clp = clp;
5971 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005972
Trond Myklebust9d12b212012-01-17 22:04:25 -05005973 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005974 msg.rpc_argp = &calldata->arg;
5975 msg.rpc_resp = &calldata->res;
5976 task_setup_data.callback_data = calldata;
5977 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005978 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005979 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005980 goto out;
5981 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05005982 status = nfs4_wait_for_completion_rpc_task(task);
5983 if (status == 0)
5984 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005985 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005986 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005987out:
5988 dprintk("<-- %s status=%d\n", __func__, status);
5989 return status;
5990}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005991
5992static void
5993nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5994{
5995 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00005996 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04005997 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005998
5999 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00006000 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6001 * right now covering the LAYOUTGET we are about to send.
6002 * However, that is not so catastrophic, and there seems
6003 * to be no way to prevent it completely.
6004 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006005 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006006 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006007 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006008 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6009 NFS_I(lgp->args.inode)->layout,
6010 lgp->args.ctx->state)) {
6011 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00006012 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006013}
6014
6015static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6016{
6017 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07006018 struct inode *inode = lgp->args.inode;
6019 struct nfs_server *server = NFS_SERVER(inode);
6020 struct pnfs_layout_hdr *lo;
6021 struct nfs4_state *state = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006022
6023 dprintk("--> %s\n", __func__);
6024
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006025 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07006026 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006027
6028 switch (task->tk_status) {
6029 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07006030 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006031 case -NFS4ERR_LAYOUTTRYLATER:
6032 case -NFS4ERR_RECALLCONFLICT:
6033 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07006034 break;
6035 case -NFS4ERR_EXPIRED:
6036 case -NFS4ERR_BAD_STATEID:
6037 spin_lock(&inode->i_lock);
6038 lo = NFS_I(inode)->layout;
6039 if (!lo || list_empty(&lo->plh_segs)) {
6040 spin_unlock(&inode->i_lock);
6041 /* If the open stateid was bad, then recover it. */
6042 state = lgp->args.ctx->state;
6043 } else {
6044 LIST_HEAD(head);
6045
6046 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6047 spin_unlock(&inode->i_lock);
6048 /* Mark the bad layout state as invalid, then
6049 * retry using the open stateid. */
6050 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006051 }
6052 }
Trond Myklebustee314c22012-10-01 17:25:48 -07006053 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6054 rpc_restart_call_prepare(task);
6055out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006056 dprintk("<-- %s\n", __func__);
6057}
6058
Idan Kedar85541162012-08-02 11:47:10 +03006059static size_t max_response_pages(struct nfs_server *server)
6060{
6061 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6062 return nfs_page_array_len(0, max_resp_sz);
6063}
6064
6065static void nfs4_free_pages(struct page **pages, size_t size)
6066{
6067 int i;
6068
6069 if (!pages)
6070 return;
6071
6072 for (i = 0; i < size; i++) {
6073 if (!pages[i])
6074 break;
6075 __free_page(pages[i]);
6076 }
6077 kfree(pages);
6078}
6079
6080static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6081{
6082 struct page **pages;
6083 int i;
6084
6085 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6086 if (!pages) {
6087 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6088 return NULL;
6089 }
6090
6091 for (i = 0; i < size; i++) {
6092 pages[i] = alloc_page(gfp_flags);
6093 if (!pages[i]) {
6094 dprintk("%s: failed to allocate page\n", __func__);
6095 nfs4_free_pages(pages, size);
6096 return NULL;
6097 }
6098 }
6099
6100 return pages;
6101}
6102
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006103static void nfs4_layoutget_release(void *calldata)
6104{
6105 struct nfs4_layoutget *lgp = calldata;
Idan Kedar85541162012-08-02 11:47:10 +03006106 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6107 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006108
6109 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006110 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006111 put_nfs_open_context(lgp->args.ctx);
6112 kfree(calldata);
6113 dprintk("<-- %s\n", __func__);
6114}
6115
6116static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6117 .rpc_call_prepare = nfs4_layoutget_prepare,
6118 .rpc_call_done = nfs4_layoutget_done,
6119 .rpc_release = nfs4_layoutget_release,
6120};
6121
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006122struct pnfs_layout_segment *
6123nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006124{
6125 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Idan Kedar85541162012-08-02 11:47:10 +03006126 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006127 struct rpc_task *task;
6128 struct rpc_message msg = {
6129 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6130 .rpc_argp = &lgp->args,
6131 .rpc_resp = &lgp->res,
6132 };
6133 struct rpc_task_setup task_setup_data = {
6134 .rpc_client = server->client,
6135 .rpc_message = &msg,
6136 .callback_ops = &nfs4_layoutget_call_ops,
6137 .callback_data = lgp,
6138 .flags = RPC_TASK_ASYNC,
6139 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006140 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006141 int status = 0;
6142
6143 dprintk("--> %s\n", __func__);
6144
Idan Kedar85541162012-08-02 11:47:10 +03006145 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6146 if (!lgp->args.layout.pages) {
6147 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006148 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006149 }
6150 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
6151
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006152 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006153 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006154 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006155 task = rpc_run_task(&task_setup_data);
6156 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006157 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006158 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006159 if (status == 0)
6160 status = task->tk_status;
6161 if (status == 0)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006162 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006163 rpc_put_task(task);
6164 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006165 if (status)
6166 return ERR_PTR(status);
6167 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006168}
6169
Benny Halevycbe82602011-05-22 19:52:37 +03006170static void
6171nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6172{
6173 struct nfs4_layoutreturn *lrp = calldata;
6174
6175 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006176 nfs41_setup_sequence(lrp->clp->cl_session,
6177 &lrp->args.seq_args,
6178 &lrp->res.seq_res,
6179 task);
Benny Halevycbe82602011-05-22 19:52:37 +03006180}
6181
6182static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6183{
6184 struct nfs4_layoutreturn *lrp = calldata;
6185 struct nfs_server *server;
6186
6187 dprintk("--> %s\n", __func__);
6188
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006189 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03006190 return;
6191
6192 server = NFS_SERVER(lrp->args.inode);
6193 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006194 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006195 return;
6196 }
Benny Halevycbe82602011-05-22 19:52:37 +03006197 dprintk("<-- %s\n", __func__);
6198}
6199
6200static void nfs4_layoutreturn_release(void *calldata)
6201{
6202 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04006203 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006204
6205 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04006206 spin_lock(&lo->plh_inode->i_lock);
6207 if (lrp->res.lrs_present)
6208 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6209 lo->plh_block_lgets--;
6210 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006211 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006212 kfree(calldata);
6213 dprintk("<-- %s\n", __func__);
6214}
6215
6216static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6217 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6218 .rpc_call_done = nfs4_layoutreturn_done,
6219 .rpc_release = nfs4_layoutreturn_release,
6220};
6221
6222int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6223{
6224 struct rpc_task *task;
6225 struct rpc_message msg = {
6226 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6227 .rpc_argp = &lrp->args,
6228 .rpc_resp = &lrp->res,
6229 };
6230 struct rpc_task_setup task_setup_data = {
6231 .rpc_client = lrp->clp->cl_rpcclient,
6232 .rpc_message = &msg,
6233 .callback_ops = &nfs4_layoutreturn_call_ops,
6234 .callback_data = lrp,
6235 };
6236 int status;
6237
6238 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006239 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006240 task = rpc_run_task(&task_setup_data);
6241 if (IS_ERR(task))
6242 return PTR_ERR(task);
6243 status = task->tk_status;
6244 dprintk("<-- %s status=%d\n", __func__, status);
6245 rpc_put_task(task);
6246 return status;
6247}
6248
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006249/*
6250 * Retrieve the list of Data Server devices from the MDS.
6251 */
6252static int _nfs4_getdevicelist(struct nfs_server *server,
6253 const struct nfs_fh *fh,
6254 struct pnfs_devicelist *devlist)
6255{
6256 struct nfs4_getdevicelist_args args = {
6257 .fh = fh,
6258 .layoutclass = server->pnfs_curr_ld->id,
6259 };
6260 struct nfs4_getdevicelist_res res = {
6261 .devlist = devlist,
6262 };
6263 struct rpc_message msg = {
6264 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6265 .rpc_argp = &args,
6266 .rpc_resp = &res,
6267 };
6268 int status;
6269
6270 dprintk("--> %s\n", __func__);
6271 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6272 &res.seq_res, 0);
6273 dprintk("<-- %s status=%d\n", __func__, status);
6274 return status;
6275}
6276
6277int nfs4_proc_getdevicelist(struct nfs_server *server,
6278 const struct nfs_fh *fh,
6279 struct pnfs_devicelist *devlist)
6280{
6281 struct nfs4_exception exception = { };
6282 int err;
6283
6284 do {
6285 err = nfs4_handle_exception(server,
6286 _nfs4_getdevicelist(server, fh, devlist),
6287 &exception);
6288 } while (exception.retry);
6289
6290 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6291 err, devlist->num_devs);
6292
6293 return err;
6294}
6295EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6296
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006297static int
6298_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6299{
6300 struct nfs4_getdeviceinfo_args args = {
6301 .pdev = pdev,
6302 };
6303 struct nfs4_getdeviceinfo_res res = {
6304 .pdev = pdev,
6305 };
6306 struct rpc_message msg = {
6307 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6308 .rpc_argp = &args,
6309 .rpc_resp = &res,
6310 };
6311 int status;
6312
6313 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006314 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006315 dprintk("<-- %s status=%d\n", __func__, status);
6316
6317 return status;
6318}
6319
6320int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6321{
6322 struct nfs4_exception exception = { };
6323 int err;
6324
6325 do {
6326 err = nfs4_handle_exception(server,
6327 _nfs4_proc_getdeviceinfo(server, pdev),
6328 &exception);
6329 } while (exception.retry);
6330 return err;
6331}
6332EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6333
Andy Adamson863a3c62011-03-23 13:27:54 +00006334static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6335{
6336 struct nfs4_layoutcommit_data *data = calldata;
6337 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006338 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00006339
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006340 nfs41_setup_sequence(session,
6341 &data->args.seq_args,
6342 &data->res.seq_res,
6343 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00006344}
6345
6346static void
6347nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6348{
6349 struct nfs4_layoutcommit_data *data = calldata;
6350 struct nfs_server *server = NFS_SERVER(data->args.inode);
6351
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006352 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00006353 return;
6354
6355 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006356 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6357 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6358 case -NFS4ERR_BADLAYOUT: /* no layout */
6359 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006360 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006361 break;
6362 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006363 nfs_post_op_update_inode_force_wcc(data->args.inode,
6364 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006365 break;
6366 default:
6367 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6368 rpc_restart_call_prepare(task);
6369 return;
6370 }
6371 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006372}
6373
6374static void nfs4_layoutcommit_release(void *calldata)
6375{
6376 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04006377 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07006378 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00006379
Andy Adamsondb29c082011-07-30 20:52:38 -04006380 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006381 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04006382 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6383 list_del_init(&lseg->pls_lc_list);
6384 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6385 &lseg->pls_flags))
Trond Myklebust9369a432012-09-18 20:57:08 -04006386 pnfs_put_lseg(lseg);
Peng Taoa9bae562011-07-30 20:52:33 -04006387 }
Peng Tao92407e72011-10-23 20:21:17 -07006388
6389 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6390 smp_mb__after_clear_bit();
6391 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6392
Andy Adamson863a3c62011-03-23 13:27:54 +00006393 put_rpccred(data->cred);
6394 kfree(data);
6395}
6396
6397static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6398 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6399 .rpc_call_done = nfs4_layoutcommit_done,
6400 .rpc_release = nfs4_layoutcommit_release,
6401};
6402
6403int
Andy Adamsonef311532011-03-12 02:58:10 -05006404nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006405{
6406 struct rpc_message msg = {
6407 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6408 .rpc_argp = &data->args,
6409 .rpc_resp = &data->res,
6410 .rpc_cred = data->cred,
6411 };
6412 struct rpc_task_setup task_setup_data = {
6413 .task = &data->task,
6414 .rpc_client = NFS_CLIENT(data->args.inode),
6415 .rpc_message = &msg,
6416 .callback_ops = &nfs4_layoutcommit_ops,
6417 .callback_data = data,
6418 .flags = RPC_TASK_ASYNC,
6419 };
6420 struct rpc_task *task;
6421 int status = 0;
6422
6423 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6424 "lbw: %llu inode %lu\n",
6425 data->task.tk_pid, sync,
6426 data->args.lastbytewritten,
6427 data->args.inode->i_ino);
6428
Trond Myklebust9d12b212012-01-17 22:04:25 -05006429 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006430 task = rpc_run_task(&task_setup_data);
6431 if (IS_ERR(task))
6432 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006433 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006434 goto out;
6435 status = nfs4_wait_for_completion_rpc_task(task);
6436 if (status != 0)
6437 goto out;
6438 status = task->tk_status;
6439out:
6440 dprintk("%s: status %d\n", __func__, status);
6441 rpc_put_task(task);
6442 return status;
6443}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006444
6445static int
6446_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6447 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6448{
6449 struct nfs41_secinfo_no_name_args args = {
6450 .style = SECINFO_STYLE_CURRENT_FH,
6451 };
6452 struct nfs4_secinfo_res res = {
6453 .flavors = flavors,
6454 };
6455 struct rpc_message msg = {
6456 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6457 .rpc_argp = &args,
6458 .rpc_resp = &res,
6459 };
6460 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6461}
6462
6463static int
6464nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6465 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6466{
6467 struct nfs4_exception exception = { };
6468 int err;
6469 do {
6470 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6471 switch (err) {
6472 case 0:
6473 case -NFS4ERR_WRONGSEC:
6474 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006475 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006476 default:
6477 err = nfs4_handle_exception(server, err, &exception);
6478 }
6479 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006480out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006481 return err;
6482}
6483
6484static int
6485nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6486 struct nfs_fsinfo *info)
6487{
6488 int err;
6489 struct page *page;
6490 rpc_authflavor_t flavor;
6491 struct nfs4_secinfo_flavors *flavors;
6492
6493 page = alloc_page(GFP_KERNEL);
6494 if (!page) {
6495 err = -ENOMEM;
6496 goto out;
6497 }
6498
6499 flavors = page_address(page);
6500 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6501
6502 /*
6503 * Fall back on "guess and check" method if
6504 * the server doesn't support SECINFO_NO_NAME
6505 */
6506 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6507 err = nfs4_find_root_sec(server, fhandle, info);
6508 goto out_freepage;
6509 }
6510 if (err)
6511 goto out_freepage;
6512
6513 flavor = nfs_find_best_sec(flavors);
6514 if (err == 0)
6515 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6516
6517out_freepage:
6518 put_page(page);
6519 if (err == -EACCES)
6520 return -EPERM;
6521out:
6522 return err;
6523}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006524
6525static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006526{
6527 int status;
6528 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006529 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006530 };
6531 struct nfs41_test_stateid_res res;
6532 struct rpc_message msg = {
6533 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6534 .rpc_argp = &args,
6535 .rpc_resp = &res,
6536 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006537
Chuck Lever38527b12012-07-11 16:30:23 -04006538 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006539 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006540 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Chuck Lever38527b12012-07-11 16:30:23 -04006541 if (status != NFS_OK) {
6542 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04006543 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04006544 }
6545 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04006546 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006547}
6548
Chuck Lever38527b12012-07-11 16:30:23 -04006549/**
6550 * nfs41_test_stateid - perform a TEST_STATEID operation
6551 *
6552 * @server: server / transport on which to perform the operation
6553 * @stateid: state ID to test
6554 *
6555 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6556 * Otherwise a negative NFS4ERR value is returned if the operation
6557 * failed or the state ID is not currently valid.
6558 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006559static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006560{
6561 struct nfs4_exception exception = { };
6562 int err;
6563 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006564 err = _nfs41_test_stateid(server, stateid);
6565 if (err != -NFS4ERR_DELAY)
6566 break;
6567 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04006568 } while (exception.retry);
6569 return err;
6570}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006571
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006572static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006573{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006574 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006575 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006576 };
6577 struct nfs41_free_stateid_res res;
6578 struct rpc_message msg = {
6579 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6580 .rpc_argp = &args,
6581 .rpc_resp = &res,
6582 };
Chuck Lever38527b12012-07-11 16:30:23 -04006583 int status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006584
Chuck Lever38527b12012-07-11 16:30:23 -04006585 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006586 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Chuck Lever38527b12012-07-11 16:30:23 -04006587 status = nfs4_call_sync_sequence(server->client, server, &msg,
Chuck Lever377e5072012-07-11 16:29:45 -04006588 &args.seq_args, &res.seq_res, 1);
Chuck Lever38527b12012-07-11 16:30:23 -04006589 dprintk("NFS reply free_stateid: %d\n", status);
6590 return status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006591}
6592
Chuck Lever38527b12012-07-11 16:30:23 -04006593/**
6594 * nfs41_free_stateid - perform a FREE_STATEID operation
6595 *
6596 * @server: server / transport on which to perform the operation
6597 * @stateid: state ID to release
6598 *
6599 * Returns NFS_OK if the server freed "stateid". Otherwise a
6600 * negative NFS4ERR value is returned.
6601 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006602static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006603{
6604 struct nfs4_exception exception = { };
6605 int err;
6606 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006607 err = _nfs4_free_stateid(server, stateid);
6608 if (err != -NFS4ERR_DELAY)
6609 break;
6610 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006611 } while (exception.retry);
6612 return err;
6613}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006614
6615static bool nfs41_match_stateid(const nfs4_stateid *s1,
6616 const nfs4_stateid *s2)
6617{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006618 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006619 return false;
6620
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006621 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006622 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006623 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006624 return true;
6625
6626 return false;
6627}
6628
Andy Adamson557134a2009-04-01 09:21:53 -04006629#endif /* CONFIG_NFS_V4_1 */
6630
Trond Myklebust36281ca2012-03-04 18:13:56 -05006631static bool nfs4_match_stateid(const nfs4_stateid *s1,
6632 const nfs4_stateid *s2)
6633{
Trond Myklebustf597c532012-03-04 18:13:56 -05006634 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006635}
6636
6637
Trond Myklebust17280172012-03-11 13:11:00 -04006638static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006639 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006640 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006641 .recover_open = nfs4_open_reclaim,
6642 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006643 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006644 .get_clid_cred = nfs4_get_setclientid_cred,
Chuck Lever05f4c352012-09-14 17:24:32 -04006645 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006646};
6647
Andy Adamson591d71c2009-04-01 09:22:47 -04006648#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006649static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006650 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6651 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6652 .recover_open = nfs4_open_reclaim,
6653 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006654 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006655 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006656 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04006657 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04006658};
6659#endif /* CONFIG_NFS_V4_1 */
6660
Trond Myklebust17280172012-03-11 13:11:00 -04006661static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006662 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006663 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006664 .recover_open = nfs4_open_expired,
6665 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006666 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006667 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006668};
6669
Andy Adamson591d71c2009-04-01 09:22:47 -04006670#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006671static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006672 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6673 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006674 .recover_open = nfs41_open_expired,
6675 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006676 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006677 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006678};
6679#endif /* CONFIG_NFS_V4_1 */
6680
Trond Myklebust17280172012-03-11 13:11:00 -04006681static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006682 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006683 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006684 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006685};
6686
6687#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006688static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006689 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006690 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006691 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006692};
6693#endif
6694
Trond Myklebust97dc1352010-06-16 09:52:26 -04006695static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6696 .minor_version = 0,
6697 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006698 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006699 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006700 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6701 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6702 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006703};
6704
6705#if defined(CONFIG_NFS_V4_1)
6706static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6707 .minor_version = 1,
6708 .call_sync = _nfs4_call_sync_session,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006709 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006710 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006711 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6712 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6713 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006714};
6715#endif
6716
Trond Myklebust97dc1352010-06-16 09:52:26 -04006717const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6718 [0] = &nfs_v4_0_minor_ops,
6719#if defined(CONFIG_NFS_V4_1)
6720 [1] = &nfs_v4_1_minor_ops,
6721#endif
6722};
6723
Bryan Schumaker73a79702012-07-16 16:39:12 -04006724const struct inode_operations nfs4_dir_inode_operations = {
6725 .create = nfs_create,
6726 .lookup = nfs_lookup,
6727 .atomic_open = nfs_atomic_open,
6728 .link = nfs_link,
6729 .unlink = nfs_unlink,
6730 .symlink = nfs_symlink,
6731 .mkdir = nfs_mkdir,
6732 .rmdir = nfs_rmdir,
6733 .mknod = nfs_mknod,
6734 .rename = nfs_rename,
6735 .permission = nfs_permission,
6736 .getattr = nfs_getattr,
6737 .setattr = nfs_setattr,
6738 .getxattr = generic_getxattr,
6739 .setxattr = generic_setxattr,
6740 .listxattr = generic_listxattr,
6741 .removexattr = generic_removexattr,
6742};
6743
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006744static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006745 .permission = nfs_permission,
6746 .getattr = nfs_getattr,
6747 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006748 .getxattr = generic_getxattr,
6749 .setxattr = generic_setxattr,
6750 .listxattr = generic_listxattr,
6751 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006752};
6753
David Howells509de812006-08-22 20:06:11 -04006754const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006755 .version = 4, /* protocol version */
6756 .dentry_ops = &nfs4_dentry_operations,
6757 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006758 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006759 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006760 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04006761 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04006762 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006763 .getattr = nfs4_proc_getattr,
6764 .setattr = nfs4_proc_setattr,
6765 .lookup = nfs4_proc_lookup,
6766 .access = nfs4_proc_access,
6767 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006768 .create = nfs4_proc_create,
6769 .remove = nfs4_proc_remove,
6770 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04006771 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006772 .unlink_done = nfs4_proc_unlink_done,
6773 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006774 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04006775 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04006776 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006777 .link = nfs4_proc_link,
6778 .symlink = nfs4_proc_symlink,
6779 .mkdir = nfs4_proc_mkdir,
6780 .rmdir = nfs4_proc_remove,
6781 .readdir = nfs4_proc_readdir,
6782 .mknod = nfs4_proc_mknod,
6783 .statfs = nfs4_proc_statfs,
6784 .fsinfo = nfs4_proc_fsinfo,
6785 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006786 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006787 .decode_dirent = nfs4_decode_dirent,
6788 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04006789 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04006790 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05006791 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006792 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04006793 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04006794 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006795 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006796 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04006797 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006798 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006799 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006800 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006801 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006802 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04006803 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04006804 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04006805 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00006806 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04006807 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04006808 .create_server = nfs4_create_server,
6809 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006810};
6811
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006812static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6813 .prefix = XATTR_NAME_NFSV4_ACL,
6814 .list = nfs4_xattr_list_nfs4_acl,
6815 .get = nfs4_xattr_get_nfs4_acl,
6816 .set = nfs4_xattr_set_nfs4_acl,
6817};
6818
6819const struct xattr_handler *nfs4_xattr_handlers[] = {
6820 &nfs4_xattr_nfs4_acl_handler,
6821 NULL
6822};
6823
Linus Torvalds1da177e2005-04-16 15:20:36 -07006824/*
6825 * Local variables:
6826 * c-basic-offset: 8
6827 * End:
6828 */