blob: 38a709d78594bad54087752ddfcda9a2fbbd9834 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050042#include <linux/ratelimit.h>
43#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sunrpc/clnt.h>
46#include <linux/nfs.h>
47#include <linux/nfs4.h>
48#include <linux/nfs_fs.h>
49#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040050#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070052#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040053#include <linux/module.h>
Trond Myklebust6926afd2012-01-07 13:22:46 -050054#include <linux/nfs_idmap.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000055#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050056#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010057#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Trond Myklebust4ce79712005-06-22 17:16:21 +000059#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050061#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050062#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040063#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040064#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040065#include "netns.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050066#include "nfs4session.h"
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69#define NFSDBG_FACILITY NFSDBG_PROC
70
Trond Myklebust2066fe82006-09-15 08:30:46 -040071#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define NFS4_POLL_RETRY_MAX (15*HZ)
73
Trond Myklebustcdd4e682006-01-03 09:55:12 +010074struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010075static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080076static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050078static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Chuck Lever81934dd2012-03-01 17:01:57 -050079static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Bryan Schumakerbae36242012-05-10 15:07:31 -040080static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *);
Trond Myklebust99367812007-07-17 21:52:41 -040081static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040082static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
83 struct nfs_fattr *fattr, struct iattr *sattr,
84 struct nfs4_state *state);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040085#ifdef CONFIG_NFS_V4_1
Bryan Schumaker1cab0652012-01-31 10:39:29 -050086static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *);
87static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040088#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070089/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050090static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Trond Myklebust52567b02009-10-23 14:46:42 -040092 if (err >= -1000)
93 return err;
94 switch (err) {
95 case -NFS4ERR_RESOURCE:
96 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +000097 case -NFS4ERR_WRONGSEC:
98 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -080099 case -NFS4ERR_BADOWNER:
100 case -NFS4ERR_BADNAME:
101 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400102 case -NFS4ERR_SHARE_DENIED:
103 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400104 case -NFS4ERR_MINOR_VERS_MISMATCH:
105 return -EPROTONOSUPPORT;
Weston Andros Adamson6168f622012-09-10 14:00:46 -0400106 case -NFS4ERR_ACCESS:
107 return -EACCES;
Trond Myklebust52567b02009-10-23 14:46:42 -0400108 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700110 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400111 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400113 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114}
115
116/*
117 * This is our standard bitmap for GETATTR requests.
118 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400119const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 FATTR4_WORD0_TYPE
121 | FATTR4_WORD0_CHANGE
122 | FATTR4_WORD0_SIZE
123 | FATTR4_WORD0_FSID
124 | FATTR4_WORD0_FILEID,
125 FATTR4_WORD1_MODE
126 | FATTR4_WORD1_NUMLINKS
127 | FATTR4_WORD1_OWNER
128 | FATTR4_WORD1_OWNER_GROUP
129 | FATTR4_WORD1_RAWDEV
130 | FATTR4_WORD1_SPACE_USED
131 | FATTR4_WORD1_TIME_ACCESS
132 | FATTR4_WORD1_TIME_METADATA
133 | FATTR4_WORD1_TIME_MODIFY
134};
135
Trond Myklebust1549210f2012-06-05 09:16:47 -0400136static const u32 nfs4_pnfs_open_bitmap[3] = {
137 FATTR4_WORD0_TYPE
138 | FATTR4_WORD0_CHANGE
139 | FATTR4_WORD0_SIZE
140 | FATTR4_WORD0_FSID
141 | FATTR4_WORD0_FILEID,
142 FATTR4_WORD1_MODE
143 | FATTR4_WORD1_NUMLINKS
144 | FATTR4_WORD1_OWNER
145 | FATTR4_WORD1_OWNER_GROUP
146 | FATTR4_WORD1_RAWDEV
147 | FATTR4_WORD1_SPACE_USED
148 | FATTR4_WORD1_TIME_ACCESS
149 | FATTR4_WORD1_TIME_METADATA
150 | FATTR4_WORD1_TIME_MODIFY,
151 FATTR4_WORD2_MDSTHRESHOLD
152};
153
Andy Adamsone23008e2012-10-02 21:07:32 -0400154static const u32 nfs4_open_noattr_bitmap[3] = {
155 FATTR4_WORD0_TYPE
156 | FATTR4_WORD0_CHANGE
157 | FATTR4_WORD0_FILEID,
158};
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160const u32 nfs4_statfs_bitmap[2] = {
161 FATTR4_WORD0_FILES_AVAIL
162 | FATTR4_WORD0_FILES_FREE
163 | FATTR4_WORD0_FILES_TOTAL,
164 FATTR4_WORD1_SPACE_AVAIL
165 | FATTR4_WORD1_SPACE_FREE
166 | FATTR4_WORD1_SPACE_TOTAL
167};
168
Trond Myklebust4ce79712005-06-22 17:16:21 +0000169const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 FATTR4_WORD0_MAXLINK
171 | FATTR4_WORD0_MAXNAME,
172 0
173};
174
Fred Isamandae100c2011-07-30 20:52:37 -0400175const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 | FATTR4_WORD0_MAXREAD
177 | FATTR4_WORD0_MAXWRITE
178 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700179 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400180 | FATTR4_WORD1_FS_LAYOUT_TYPES,
181 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182};
183
Manoj Naik830b8e32006-06-09 09:34:25 -0400184const u32 nfs4_fs_locations_bitmap[2] = {
185 FATTR4_WORD0_TYPE
186 | FATTR4_WORD0_CHANGE
187 | FATTR4_WORD0_SIZE
188 | FATTR4_WORD0_FSID
189 | FATTR4_WORD0_FILEID
190 | FATTR4_WORD0_FS_LOCATIONS,
191 FATTR4_WORD1_MODE
192 | FATTR4_WORD1_NUMLINKS
193 | FATTR4_WORD1_OWNER
194 | FATTR4_WORD1_OWNER_GROUP
195 | FATTR4_WORD1_RAWDEV
196 | FATTR4_WORD1_SPACE_USED
197 | FATTR4_WORD1_TIME_ACCESS
198 | FATTR4_WORD1_TIME_METADATA
199 | FATTR4_WORD1_TIME_MODIFY
200 | FATTR4_WORD1_MOUNTED_ON_FILEID
201};
202
Al Virobc4785c2006-10-19 23:28:51 -0700203static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 struct nfs4_readdir_arg *readdir)
205{
Al Viro0dbb4c62006-10-19 23:28:49 -0700206 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000209 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
211 return;
212 }
213
214 readdir->cookie = 0;
215 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
216 if (cookie == 2)
217 return;
218
219 /*
220 * NFSv4 servers do not return entries for '.' and '..'
221 * Therefore, we fake these entries here. We let '.'
222 * have cookie 0 and '..' have cookie 1. Note that
223 * when talking to the server, we always send cookie 0
224 * instead of 1 or 2.
225 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800226 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 if (cookie == 0) {
229 *p++ = xdr_one; /* next */
230 *p++ = xdr_zero; /* cookie, first word */
231 *p++ = xdr_one; /* cookie, second word */
232 *p++ = xdr_one; /* entry len */
233 memcpy(p, ".\0\0\0", 4); /* entry */
234 p++;
235 *p++ = xdr_one; /* bitmap length */
236 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
237 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400238 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 }
240
241 *p++ = xdr_one; /* next */
242 *p++ = xdr_zero; /* cookie, first word */
243 *p++ = xdr_two; /* cookie, second word */
244 *p++ = xdr_two; /* entry len */
245 memcpy(p, "..\0\0", 4); /* entry */
246 p++;
247 *p++ = xdr_one; /* bitmap length */
248 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
249 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400250 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 readdir->pgbase = (char *)p - (char *)start;
253 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800254 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255}
256
Trond Myklebust65de8722008-12-23 15:21:44 -0500257static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
258{
259 int res = 0;
260
261 might_sleep();
262
263 if (*timeout <= 0)
264 *timeout = NFS4_POLL_RETRY_MIN;
265 if (*timeout > NFS4_POLL_RETRY_MAX)
266 *timeout = NFS4_POLL_RETRY_MAX;
Jeff Laytond3103102011-12-01 22:44:39 +0100267 freezable_schedule_timeout_killable(*timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500268 if (fatal_signal_pending(current))
269 res = -ERESTARTSYS;
270 *timeout <<= 1;
271 return res;
272}
273
274/* This is the error handling routine for processes that are allowed
275 * to sleep.
276 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800277static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500278{
279 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500280 struct nfs4_state *state = exception->state;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500281 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500282 int ret = errorcode;
283
284 exception->retry = 0;
285 switch(errorcode) {
286 case 0:
287 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500288 case -NFS4ERR_OPENMODE:
Bryan Schumaker011e2a72012-06-20 15:53:43 -0400289 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400290 nfs4_inode_return_delegation(inode);
Trond Myklebust3114ea72012-03-07 16:39:06 -0500291 exception->retry = 1;
292 return 0;
293 }
294 if (state == NULL)
295 break;
296 nfs4_schedule_stateid_recovery(server, state);
297 goto wait_on_recovery;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500298 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500299 case -NFS4ERR_ADMIN_REVOKED:
300 case -NFS4ERR_BAD_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500301 if (state == NULL)
302 break;
Trond Myklebust14977482012-03-27 18:31:25 -0400303 nfs_remove_bad_delegation(state->inode);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500304 nfs4_schedule_stateid_recovery(server, state);
305 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400306 case -NFS4ERR_EXPIRED:
307 if (state != NULL)
308 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust65de8722008-12-23 15:21:44 -0500309 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500310 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500311 nfs4_schedule_lease_recovery(clp);
312 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500313#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400314 case -NFS4ERR_BADSESSION:
315 case -NFS4ERR_BADSLOT:
316 case -NFS4ERR_BAD_HIGH_SLOT:
317 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
318 case -NFS4ERR_DEADSESSION:
319 case -NFS4ERR_SEQ_FALSE_RETRY:
320 case -NFS4ERR_SEQ_MISORDERED:
321 dprintk("%s ERROR: %d Reset session\n", __func__,
322 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400323 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400324 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500325#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500326 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500327 if (exception->timeout > HZ) {
328 /* We have retried a decent amount, time to
329 * fail
330 */
331 ret = -EBUSY;
332 break;
333 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500334 case -NFS4ERR_GRACE:
335 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -0500336 case -EKEYEXPIRED:
Trond Myklebust65de8722008-12-23 15:21:44 -0500337 ret = nfs4_delay(server->client, &exception->timeout);
338 if (ret != 0)
339 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400340 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500341 case -NFS4ERR_OLD_STATEID:
342 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800343 break;
344 case -NFS4ERR_BADOWNER:
345 /* The following works around a Linux server bug! */
346 case -NFS4ERR_BADNAME:
347 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
348 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
349 exception->retry = 1;
350 printk(KERN_WARNING "NFS: v4 server %s "
351 "does not accept raw "
352 "uid/gids. "
353 "Reenabling the idmapper.\n",
354 server->nfs_client->cl_hostname);
355 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500356 }
357 /* We failed to handle the error */
358 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500359wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500360 ret = nfs4_wait_clnt_recover(clp);
361 if (ret == 0)
362 exception->retry = 1;
363 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500364}
365
366
Trond Myklebust452e9352010-07-31 14:29:06 -0400367static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 spin_lock(&clp->cl_lock);
370 if (time_before(clp->cl_last_renewal,timestamp))
371 clp->cl_last_renewal = timestamp;
372 spin_unlock(&clp->cl_lock);
373}
374
Trond Myklebust452e9352010-07-31 14:29:06 -0400375static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
376{
377 do_renew_lease(server->nfs_client, timestamp);
378}
379
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400380#if defined(CONFIG_NFS_V4_1)
381
Trond Myklebustd185a332010-06-16 09:52:25 -0400382static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400383{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500384 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400385 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500386 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400387
Benny Halevydfb4f3092010-09-24 09:17:01 -0400388 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400389 /* just wake up the next guy waiting since
390 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500391 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500392 return;
Andy Adamson13615872009-04-01 09:22:17 -0400393 }
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500394 tbl = res->sr_slot->table;
395 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500396
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500397 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500398 /* Be nice to the server: try to ensure that the last transmitted
399 * value for highest_user_slotid <= target_highest_slotid
400 */
401 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
402 send_new_highest_used_slotid = true;
403
Trond Myklebustf4af6e2a2012-11-20 14:17:32 -0500404 nfs4_free_slot(tbl, res->sr_slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500405
406 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
407 send_new_highest_used_slotid = false;
408 if (!nfs4_session_draining(session)) {
Trond Myklebust275e7e22012-11-01 17:07:07 -0400409 if (rpc_wake_up_next(&tbl->slot_tbl_waitq) != NULL)
Trond Myklebustc10e4492012-11-26 16:16:54 -0500410 send_new_highest_used_slotid = false;
411 }
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500412 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400413 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500414 if (send_new_highest_used_slotid)
415 nfs41_server_notify_highest_slotid_update(session->clp);
Andy Adamson13615872009-04-01 09:22:17 -0400416}
417
Trond Myklebust14516c32010-07-31 14:29:06 -0400418static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400419{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500420 struct nfs4_session *session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500421 struct nfs4_slot *slot;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400422 unsigned long timestamp;
Trond Myklebust14516c32010-07-31 14:29:06 -0400423 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400424
425 /*
426 * sr_status remains 1 if an RPC level error occurred. The server
427 * may or may not have processed the sequence operation..
428 * Proceed as if the server received and processed the sequence
429 * operation.
430 */
431 if (res->sr_status == 1)
432 res->sr_status = NFS_OK;
433
Bryan Schumaker468f8612011-04-18 15:57:32 -0400434 /* don't increment the sequence number if the task wasn't sent */
435 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400436 goto out;
437
Trond Myklebust933602e2012-11-16 12:12:38 -0500438 slot = res->sr_slot;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500439 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500440
Andy Adamson691daf32009-12-04 15:55:39 -0500441 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400442 switch (res->sr_status) {
443 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400444 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust933602e2012-11-16 12:12:38 -0500445 ++slot->seq_nr;
446 timestamp = slot->renewal_time;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500447 clp = session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400448 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500449 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500450 if (res->sr_status_flags != 0)
451 nfs4_schedule_lease_recovery(clp);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500452 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400453 break;
454 case -NFS4ERR_DELAY:
455 /* The server detected a resend of the RPC call and
456 * returned NFS4ERR_DELAY as per Section 2.10.6.2
457 * of RFC5661.
458 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500459 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400460 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500461 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500462 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400463 goto out_retry;
464 default:
465 /* Just update the slot sequence no. */
Trond Myklebust933602e2012-11-16 12:12:38 -0500466 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400467 }
468out:
469 /* The session may be reset by one of the error handlers. */
470 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400471 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400472 return 1;
473out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400474 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400475 goto out;
476 rpc_delay(task, NFS4_POLL_RETRY_MAX);
477 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400478}
479
Trond Myklebust14516c32010-07-31 14:29:06 -0400480static int nfs4_sequence_done(struct rpc_task *task,
481 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400482{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500483 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400484 return 1;
485 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400486}
487
Trond Myklebust9d12b212012-01-17 22:04:25 -0500488static void nfs41_init_sequence(struct nfs4_sequence_args *args,
489 struct nfs4_sequence_res *res, int cache_reply)
490{
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500491 args->sa_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500492 args->sa_cache_this = 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400493 args->sa_privileged = 0;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500494 if (cache_reply)
495 args->sa_cache_this = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500496 res->sr_slot = NULL;
497}
498
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400499static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
500{
501 args->sa_privileged = 1;
502}
503
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000504int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400505 struct nfs4_sequence_args *args,
506 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400507 struct rpc_task *task)
508{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400509 struct nfs4_slot *slot;
510 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400511
512 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400513 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400514 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400515 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400516
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400517 tbl = &session->fc_slot_table;
518
Trond Myklebust69d206b2012-11-22 13:21:02 -0500519 task->tk_timeout = 0;
520
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400521 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400522 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400523 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500524 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500525 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400526 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400527 }
528
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500529 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500530 if (IS_ERR(slot)) {
531 /* If out of memory, try again in 1/4 second */
532 if (slot == ERR_PTR(-ENOMEM))
533 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400534 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400535 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400536 }
537 spin_unlock(&tbl->slot_tbl_lock);
538
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800539 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500540
541 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400542
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500543 dprintk("<-- %s slotid=%d seqid=%d\n", __func__,
544 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400545
Benny Halevydfb4f3092010-09-24 09:17:01 -0400546 res->sr_slot = slot;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400547 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400548 /*
549 * sr_status is only set in decode_sequence, and so will remain
550 * set to 1 if an rpc level failure occurs.
551 */
552 res->sr_status = 1;
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400553out_success:
554 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400555 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400556out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400557 /* Privileged tasks are queued with top priority */
558 if (args->sa_privileged)
559 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400560 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
561 spin_unlock(&tbl->slot_tbl_lock);
562 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400563}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000564EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400565
Trond Myklebust035168a2010-06-16 09:52:26 -0400566int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400567 struct nfs4_sequence_args *args,
568 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400569 struct rpc_task *task)
570{
Trond Myklebust035168a2010-06-16 09:52:26 -0400571 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400572 int ret = 0;
573
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400574 if (session == NULL) {
575 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400576 goto out;
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400577 }
Trond Myklebust035168a2010-06-16 09:52:26 -0400578
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500579 dprintk("--> %s clp %p session %p sr_slot %d\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400580 __func__, session->clp, session, res->sr_slot ?
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500581 res->sr_slot->slot_nr : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400582
Trond Myklebust9d12b212012-01-17 22:04:25 -0500583 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400584out:
585 dprintk("<-- %s status=%d\n", __func__, ret);
586 return ret;
587}
588
589struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400590 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400591 struct nfs4_sequence_args *seq_args;
592 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400593};
594
595static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
596{
597 struct nfs41_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400598 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400599
Trond Myklebust035168a2010-06-16 09:52:26 -0400600 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
601
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400602 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400603}
604
Andy Adamson69ab40c2009-04-01 09:22:19 -0400605static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
606{
607 struct nfs41_call_sync_data *data = calldata;
608
Trond Myklebust14516c32010-07-31 14:29:06 -0400609 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400610}
611
Trond Myklebust17280172012-03-11 13:11:00 -0400612static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400613 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400614 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400615};
616
Bryan Schumaker7c513052011-03-24 17:12:24 +0000617static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
618 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400619 struct rpc_message *msg,
620 struct nfs4_sequence_args *args,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400621 struct nfs4_sequence_res *res)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400622{
623 int ret;
624 struct rpc_task *task;
625 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400626 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400627 .seq_args = args,
628 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400629 };
630 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000631 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400632 .rpc_message = msg,
633 .callback_ops = &nfs41_call_sync_ops,
634 .callback_data = &data
635 };
636
Andy Adamsonce5039c2009-04-01 09:22:13 -0400637 task = rpc_run_task(&task_setup);
638 if (IS_ERR(task))
639 ret = PTR_ERR(task);
640 else {
641 ret = task->tk_status;
642 rpc_put_task(task);
643 }
644 return ret;
645}
646
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400647static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000648int _nfs4_call_sync_session(struct rpc_clnt *clnt,
649 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400650 struct rpc_message *msg,
651 struct nfs4_sequence_args *args,
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400652 struct nfs4_sequence_res *res)
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400653{
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400654 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400655}
656
Trond Myklebustdf896452010-06-16 09:52:26 -0400657#else
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400658static
Trond Myklebust9d12b212012-01-17 22:04:25 -0500659void nfs41_init_sequence(struct nfs4_sequence_args *args,
660 struct nfs4_sequence_res *res, int cache_reply)
661{
662}
663
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400664static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
665{
666}
667
668
Trond Myklebust14516c32010-07-31 14:29:06 -0400669static int nfs4_sequence_done(struct rpc_task *task,
670 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400671{
Trond Myklebust14516c32010-07-31 14:29:06 -0400672 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400673}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400674#endif /* CONFIG_NFS_V4_1 */
675
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400676static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000677int _nfs4_call_sync(struct rpc_clnt *clnt,
678 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400679 struct rpc_message *msg,
680 struct nfs4_sequence_args *args,
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400681 struct nfs4_sequence_res *res)
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400682{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000683 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400684}
685
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400686static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000687int nfs4_call_sync(struct rpc_clnt *clnt,
688 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000689 struct rpc_message *msg,
690 struct nfs4_sequence_args *args,
691 struct nfs4_sequence_res *res,
692 int cache_reply)
693{
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400694 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000695 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400696 args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000697}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400698
Trond Myklebust38478b22006-05-25 01:40:57 -0400699static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
Trond Myklebust38478b22006-05-25 01:40:57 -0400701 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Trond Myklebust38478b22006-05-25 01:40:57 -0400703 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400704 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700705 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400706 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700707 dir->i_version = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400708 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709}
710
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100711struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400712 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100713 struct nfs_openargs o_arg;
714 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100715 struct nfs_open_confirmargs c_arg;
716 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500717 struct nfs4_string owner_name;
718 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100719 struct nfs_fattr f_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100720 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400721 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100722 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400723 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100724 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100725 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400726 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100727 int rpc_status;
728 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100729};
730
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400731
732static void nfs4_init_opendata_res(struct nfs4_opendata *p)
733{
734 p->o_res.f_attr = &p->f_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400735 p->o_res.seqid = p->o_arg.seqid;
736 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400737 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400738 p->o_res.access_request = p->o_arg.access;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400739 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500740 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400741}
742
Al Viro82a2c1b2011-06-22 18:30:55 -0400743static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500744 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400745 const struct iattr *attrs,
746 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100747{
Al Viro82a2c1b2011-06-22 18:30:55 -0400748 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100749 struct inode *dir = parent->d_inode;
750 struct nfs_server *server = NFS_SERVER(dir);
751 struct nfs4_opendata *p;
752
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400753 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100754 if (p == NULL)
755 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400756 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100757 if (p->o_arg.seqid == NULL)
758 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400759 nfs_sb_active(dentry->d_sb);
760 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100761 p->dir = parent;
762 p->owner = sp;
763 atomic_inc(&sp->so_count);
764 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500765 p->o_arg.open_flags = flags;
766 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700767 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
768 * will return permission denied for all bits until close */
769 if (!(flags & O_EXCL)) {
770 /* ask server to check for all possible rights as results
771 * are cached */
772 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
773 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700774 }
David Howells7539bba2006-08-22 20:06:09 -0400775 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400776 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
777 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400778 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100779 p->o_arg.server = server;
780 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust1549210f2012-06-05 09:16:47 -0400781 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100782 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust536e43d2012-01-17 22:04:26 -0500783 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -0500784 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400785
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100786 p->o_arg.u.attrs = &p->attrs;
787 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500788
789 verf[0] = jiffies;
790 verf[1] = current->pid;
791 memcpy(p->o_arg.u.verifier.data, verf,
792 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100793 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100794 p->c_arg.fh = &p->o_res.fh;
795 p->c_arg.stateid = &p->o_res.stateid;
796 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400797 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400798 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100799 return p;
800err_free:
801 kfree(p);
802err:
803 dput(parent);
804 return NULL;
805}
806
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400807static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100808{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400809 struct nfs4_opendata *p = container_of(kref,
810 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400811 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400812
813 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400814 if (p->state != NULL)
815 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400816 nfs4_put_state_owner(p->owner);
817 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400818 dput(p->dentry);
819 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500820 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400821 kfree(p);
822}
823
824static void nfs4_opendata_put(struct nfs4_opendata *p)
825{
826 if (p != NULL)
827 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100828}
829
Trond Myklebust06f814a2006-01-03 09:55:07 +0100830static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
831{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100832 int ret;
833
Trond Myklebust06f814a2006-01-03 09:55:07 +0100834 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100835 return ret;
836}
837
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500838static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400839{
840 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500841
Trond Myklebust536e43d2012-01-17 22:04:26 -0500842 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500843 goto out;
844 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400845 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500846 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
847 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400848 break;
849 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500850 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
851 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400852 break;
853 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500854 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
855 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400856 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500857out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400858 return ret;
859}
860
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500861static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400862{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500863 if (delegation == NULL)
864 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500865 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400866 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500867 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400868 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500869 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400870 return 1;
871}
872
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500873static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100874{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500875 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100876 case FMODE_WRITE:
877 state->n_wronly++;
878 break;
879 case FMODE_READ:
880 state->n_rdonly++;
881 break;
882 case FMODE_READ|FMODE_WRITE:
883 state->n_rdwr++;
884 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500885 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100886}
887
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500888static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400889{
890 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -0500891 nfs4_stateid_copy(&state->stateid, stateid);
892 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500893 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400894 case FMODE_READ:
895 set_bit(NFS_O_RDONLY_STATE, &state->flags);
896 break;
897 case FMODE_WRITE:
898 set_bit(NFS_O_WRONLY_STATE, &state->flags);
899 break;
900 case FMODE_READ|FMODE_WRITE:
901 set_bit(NFS_O_RDWR_STATE, &state->flags);
902 }
903}
904
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500905static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400906{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400907 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500908 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400909 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400910}
911
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500912static 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 -0700913{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400914 /*
915 * Protect the call to nfs4_state_set_mode_locked and
916 * serialise the stateid update
917 */
918 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400919 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -0500920 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -0400921 set_bit(NFS_DELEGATED_STATE, &state->flags);
922 }
923 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500924 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400925 write_sequnlock(&state->seqlock);
926 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500927 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700928 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929}
930
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500931static 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 -0500932{
933 struct nfs_inode *nfsi = NFS_I(state->inode);
934 struct nfs_delegation *deleg_cur;
935 int ret = 0;
936
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500937 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500938
939 rcu_read_lock();
940 deleg_cur = rcu_dereference(nfsi->delegation);
941 if (deleg_cur == NULL)
942 goto no_delegation;
943
944 spin_lock(&deleg_cur->lock);
945 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500946 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500947 goto no_delegation_unlock;
948
949 if (delegation == NULL)
950 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -0500951 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -0500952 goto no_delegation_unlock;
953
Trond Myklebustb7391f42008-12-23 15:21:52 -0500954 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500955 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500956 ret = 1;
957no_delegation_unlock:
958 spin_unlock(&deleg_cur->lock);
959no_delegation:
960 rcu_read_unlock();
961
962 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500963 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500964 ret = 1;
965 }
966
967 return ret;
968}
969
970
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500971static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400972{
973 struct nfs_delegation *delegation;
974
975 rcu_read_lock();
976 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500977 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -0400978 rcu_read_unlock();
979 return;
980 }
981 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400982 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400983}
984
Trond Myklebust6ee41262007-07-08 14:11:36 -0400985static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400986{
987 struct nfs4_state *state = opendata->state;
988 struct nfs_inode *nfsi = NFS_I(state->inode);
989 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -0500990 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500991 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400992 nfs4_stateid stateid;
993 int ret = -EAGAIN;
994
Trond Myklebustaac00a82007-07-05 19:02:21 -0400995 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500996 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400997 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500998 if (can_open_cached(state, fmode, open_mode)) {
999 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001000 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001001 goto out_return_state;
1002 }
1003 spin_unlock(&state->owner->so_lock);
1004 }
Trond Myklebust34310432008-12-23 15:21:38 -05001005 rcu_read_lock();
1006 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001007 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001008 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001009 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001010 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001011 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001012 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001013 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001014 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001015 if (ret != 0)
1016 goto out;
1017 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001018
1019 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001020 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001021 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001022 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001023out:
1024 return ERR_PTR(ret);
1025out_return_state:
1026 atomic_inc(&state->count);
1027 return state;
1028}
1029
Andy Adamsone23008e2012-10-02 21:07:32 -04001030static void
1031nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1032{
1033 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1034 struct nfs_delegation *delegation;
1035 int delegation_flags = 0;
1036
1037 rcu_read_lock();
1038 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1039 if (delegation)
1040 delegation_flags = delegation->flags;
1041 rcu_read_unlock();
1042 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1043 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1044 "returning a delegation for "
1045 "OPEN(CLAIM_DELEGATE_CUR)\n",
1046 clp->cl_hostname);
1047 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1048 nfs_inode_set_delegation(state->inode,
1049 data->owner->so_cred,
1050 &data->o_res);
1051 else
1052 nfs_inode_reclaim_delegation(state->inode,
1053 data->owner->so_cred,
1054 &data->o_res);
1055}
1056
1057/*
1058 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1059 * and update the nfs4_state.
1060 */
1061static struct nfs4_state *
1062_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1063{
1064 struct inode *inode = data->state->inode;
1065 struct nfs4_state *state = data->state;
1066 int ret;
1067
1068 if (!data->rpc_done) {
1069 ret = data->rpc_status;
1070 goto err;
1071 }
1072
1073 ret = -ESTALE;
1074 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1075 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1076 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1077 goto err;
1078
1079 ret = -ENOMEM;
1080 state = nfs4_get_open_state(inode, data->owner);
1081 if (state == NULL)
1082 goto err;
1083
1084 ret = nfs_refresh_inode(inode, &data->f_attr);
1085 if (ret)
1086 goto err;
1087
1088 if (data->o_res.delegation_type != 0)
1089 nfs4_opendata_check_deleg(data, state);
1090 update_open_stateid(state, &data->o_res.stateid, NULL,
1091 data->o_arg.fmode);
1092
1093 return state;
1094err:
1095 return ERR_PTR(ret);
1096
1097}
1098
1099static struct nfs4_state *
1100_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001101{
1102 struct inode *inode;
1103 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001104 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001105
Trond Myklebustaac00a82007-07-05 19:02:21 -04001106 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001107 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001108 goto out;
1109 }
1110
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001111 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001112 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001113 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001114 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001115 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001116 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001117 goto err;
1118 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001119 state = nfs4_get_open_state(inode, data->owner);
1120 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001121 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001122 if (data->o_res.delegation_type != 0)
1123 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001124 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001125 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001126 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001127out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001128 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001129err_put_inode:
1130 iput(inode);
1131err:
1132 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001133}
1134
Andy Adamsone23008e2012-10-02 21:07:32 -04001135static struct nfs4_state *
1136nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1137{
1138 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1139 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1140 return _nfs4_opendata_to_nfs4_state(data);
1141}
1142
Trond Myklebust864472e2006-01-03 09:55:15 +01001143static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1144{
1145 struct nfs_inode *nfsi = NFS_I(state->inode);
1146 struct nfs_open_context *ctx;
1147
1148 spin_lock(&state->inode->i_lock);
1149 list_for_each_entry(ctx, &nfsi->open_files, list) {
1150 if (ctx->state != state)
1151 continue;
1152 get_nfs_open_context(ctx);
1153 spin_unlock(&state->inode->i_lock);
1154 return ctx;
1155 }
1156 spin_unlock(&state->inode->i_lock);
1157 return ERR_PTR(-ENOENT);
1158}
1159
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001160static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1161{
1162 struct nfs4_opendata *opendata;
1163
Al Viro3d4ff432011-06-22 18:40:12 -04001164 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001165 if (opendata == NULL)
1166 return ERR_PTR(-ENOMEM);
1167 opendata->state = state;
1168 atomic_inc(&state->count);
1169 return opendata;
1170}
1171
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001172static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001173{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001174 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001175 int ret;
1176
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001177 opendata->o_arg.open_flags = 0;
1178 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001179 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1180 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1181 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001182 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001183 if (ret != 0)
1184 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001185 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001186 if (IS_ERR(newstate))
1187 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001188 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001189 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001190 return 0;
1191}
1192
1193static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1194{
Trond Myklebust864472e2006-01-03 09:55:15 +01001195 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001196 int ret;
1197
1198 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001199 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001200 smp_rmb();
1201 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001202 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001203 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001204 if (ret != 0)
1205 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001206 if (newstate != state)
1207 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001208 }
1209 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001210 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001211 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001212 if (ret != 0)
1213 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001214 if (newstate != state)
1215 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001216 }
1217 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001218 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001219 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &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 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001225 /*
1226 * We may have performed cached opens for all three recoveries.
1227 * Check if we need to update the current stateid.
1228 */
1229 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001230 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001231 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001232 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001233 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001234 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001235 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001236 return 0;
1237}
1238
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239/*
1240 * OPEN_RECLAIM:
1241 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001243static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001245 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001246 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001247 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 int status;
1249
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001250 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1251 if (IS_ERR(opendata))
1252 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001253 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1254 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001255 rcu_read_lock();
1256 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001257 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001258 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001259 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001260 opendata->o_arg.u.delegation_type = delegation_type;
1261 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001262 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 return status;
1264}
1265
Trond Myklebust539cd032007-06-05 11:46:42 -04001266static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267{
1268 struct nfs_server *server = NFS_SERVER(state->inode);
1269 struct nfs4_exception exception = { };
1270 int err;
1271 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001272 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001273 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001274 break;
1275 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 } while (exception.retry);
1277 return err;
1278}
1279
Trond Myklebust864472e2006-01-03 09:55:15 +01001280static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1281{
1282 struct nfs_open_context *ctx;
1283 int ret;
1284
1285 ctx = nfs4_state_find_open_context(state);
1286 if (IS_ERR(ctx))
1287 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001288 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001289 put_nfs_open_context(ctx);
1290 return ret;
1291}
1292
Trond Myklebust13437e12007-07-06 15:10:43 -04001293static 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 -07001294{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001295 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001296 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001298 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1299 if (IS_ERR(opendata))
1300 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001301 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebustf597c532012-03-04 18:13:56 -05001302 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust864472e2006-01-03 09:55:15 +01001303 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001304 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001305 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306}
1307
Trond Myklebust13437e12007-07-06 15:10:43 -04001308int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309{
1310 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001311 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 int err;
1313 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001314 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 switch (err) {
1316 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001317 case -ENOENT:
1318 case -ESTALE:
1319 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001320 case -NFS4ERR_BADSESSION:
1321 case -NFS4ERR_BADSLOT:
1322 case -NFS4ERR_BAD_HIGH_SLOT:
1323 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1324 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04001325 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001326 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 case -NFS4ERR_STALE_CLIENTID:
1328 case -NFS4ERR_STALE_STATEID:
1329 case -NFS4ERR_EXPIRED:
1330 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001331 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001332 goto out;
1333 case -ERESTARTSYS:
1334 /*
1335 * The show must go on: exit, but mark the
1336 * stateid as needing recovery.
1337 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001338 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001339 case -NFS4ERR_ADMIN_REVOKED:
1340 case -NFS4ERR_BAD_STATEID:
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001341 nfs_inode_find_state_and_recover(state->inode,
1342 stateid);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001343 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001344 case -EKEYEXPIRED:
1345 /*
1346 * User RPCSEC_GSS context has expired.
1347 * We cannot recover this stateid now, so
1348 * skip it and allow recovery thread to
1349 * proceed.
1350 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001351 case -ENOMEM:
1352 err = 0;
1353 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 }
1355 err = nfs4_handle_exception(server, err, &exception);
1356 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001357out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 return err;
1359}
1360
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001361static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1362{
1363 struct nfs4_opendata *data = calldata;
1364
1365 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001366 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001367 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001368 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001369 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001370 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001371 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001372}
1373
1374static void nfs4_open_confirm_release(void *calldata)
1375{
1376 struct nfs4_opendata *data = calldata;
1377 struct nfs4_state *state = NULL;
1378
1379 /* If this request hasn't been cancelled, do nothing */
1380 if (data->cancelled == 0)
1381 goto out_free;
1382 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001383 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001384 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001385 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001386 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001387 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001388out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001389 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001390}
1391
1392static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001393 .rpc_call_done = nfs4_open_confirm_done,
1394 .rpc_release = nfs4_open_confirm_release,
1395};
1396
1397/*
1398 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1399 */
1400static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1401{
1402 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1403 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001404 struct rpc_message msg = {
1405 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1406 .rpc_argp = &data->c_arg,
1407 .rpc_resp = &data->c_res,
1408 .rpc_cred = data->owner->so_cred,
1409 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001410 struct rpc_task_setup task_setup_data = {
1411 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001412 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001413 .callback_ops = &nfs4_open_confirm_ops,
1414 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001415 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001416 .flags = RPC_TASK_ASYNC,
1417 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 int status;
1419
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001420 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001421 data->rpc_done = 0;
1422 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001423 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001424 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001425 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001426 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001427 status = nfs4_wait_for_completion_rpc_task(task);
1428 if (status != 0) {
1429 data->cancelled = 1;
1430 smp_wmb();
1431 } else
1432 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001433 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 return status;
1435}
1436
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001437static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001439 struct nfs4_opendata *data = calldata;
1440 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001441
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001442 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1443 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001444 /*
1445 * Check if we still need to send an OPEN call, or if we can use
1446 * a delegation instead.
1447 */
1448 if (data->state != NULL) {
1449 struct nfs_delegation *delegation;
1450
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001451 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001452 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001453 rcu_read_lock();
1454 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001455 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1456 can_open_delegated(delegation, data->o_arg.fmode))
1457 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001458 rcu_read_unlock();
1459 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001460 /* Update client id. */
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001461 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001462 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001463 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Andy Adamsone23008e2012-10-02 21:07:32 -04001464 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001465 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1466 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001467 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001468 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001469 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001470 &data->o_res.seq_res,
1471 task) != 0)
1472 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001473 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001474unlock_no_action:
1475 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001476out_no_action:
1477 task->tk_action = NULL;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001478}
1479
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001480static void nfs4_open_done(struct rpc_task *task, void *calldata)
1481{
1482 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001484 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001485
Trond Myklebust14516c32010-07-31 14:29:06 -04001486 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1487 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001488
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001489 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001490 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1491 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001492 case S_IFREG:
1493 break;
1494 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001495 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001496 break;
1497 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001498 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001499 break;
1500 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001501 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001502 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001503 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001504 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001505 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1506 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001507 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001508 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001509}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001510
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001511static void nfs4_open_release(void *calldata)
1512{
1513 struct nfs4_opendata *data = calldata;
1514 struct nfs4_state *state = NULL;
1515
1516 /* If this request hasn't been cancelled, do nothing */
1517 if (data->cancelled == 0)
1518 goto out_free;
1519 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001520 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001521 goto out_free;
1522 /* In case we need an open_confirm, no cleanup! */
1523 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1524 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001525 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001526 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001527 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001528out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001529 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001530}
1531
1532static const struct rpc_call_ops nfs4_open_ops = {
1533 .rpc_call_prepare = nfs4_open_prepare,
1534 .rpc_call_done = nfs4_open_done,
1535 .rpc_release = nfs4_open_release,
1536};
1537
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001538static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001539{
1540 struct inode *dir = data->dir->d_inode;
1541 struct nfs_server *server = NFS_SERVER(dir);
1542 struct nfs_openargs *o_arg = &data->o_arg;
1543 struct nfs_openres *o_res = &data->o_res;
1544 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001545 struct rpc_message msg = {
1546 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1547 .rpc_argp = o_arg,
1548 .rpc_resp = o_res,
1549 .rpc_cred = data->owner->so_cred,
1550 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001551 struct rpc_task_setup task_setup_data = {
1552 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001553 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001554 .callback_ops = &nfs4_open_ops,
1555 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001556 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001557 .flags = RPC_TASK_ASYNC,
1558 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001559 int status;
1560
Trond Myklebust9d12b212012-01-17 22:04:25 -05001561 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001562 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001563 data->rpc_done = 0;
1564 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001565 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001566 if (isrecover)
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001567 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04001568 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001569 if (IS_ERR(task))
1570 return PTR_ERR(task);
1571 status = nfs4_wait_for_completion_rpc_task(task);
1572 if (status != 0) {
1573 data->cancelled = 1;
1574 smp_wmb();
1575 } else
1576 status = data->rpc_status;
1577 rpc_put_task(task);
1578
1579 return status;
1580}
1581
1582static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1583{
1584 struct inode *dir = data->dir->d_inode;
1585 struct nfs_openres *o_res = &data->o_res;
1586 int status;
1587
1588 status = nfs4_run_open_task(data, 1);
1589 if (status != 0 || !data->rpc_done)
1590 return status;
1591
Trond Myklebust6926afd2012-01-07 13:22:46 -05001592 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1593
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001594 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1595 status = _nfs4_proc_open_confirm(data);
1596 if (status != 0)
1597 return status;
1598 }
1599
1600 return status;
1601}
1602
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001603static int nfs4_opendata_access(struct rpc_cred *cred,
1604 struct nfs4_opendata *opendata,
1605 struct nfs4_state *state, fmode_t fmode)
1606{
1607 struct nfs_access_entry cache;
1608 u32 mask;
1609
1610 /* access call failed or for some reason the server doesn't
1611 * support any access modes -- defer access call until later */
1612 if (opendata->o_res.access_supported == 0)
1613 return 0;
1614
1615 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001616 /* don't check MAY_WRITE - a newly created file may not have
1617 * write mode bits, but POSIX allows the creating process to write */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001618 if (fmode & FMODE_READ)
1619 mask |= MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001620 if (fmode & FMODE_EXEC)
1621 mask |= MAY_EXEC;
1622
1623 cache.cred = cred;
1624 cache.jiffies = jiffies;
1625 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1626 nfs_access_add_cache(state->inode, &cache);
1627
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001628 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001629 return 0;
1630
1631 /* even though OPEN succeeded, access is denied. Close the file */
1632 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001633 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001634}
1635
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001636/*
1637 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1638 */
1639static int _nfs4_proc_open(struct nfs4_opendata *data)
1640{
1641 struct inode *dir = data->dir->d_inode;
1642 struct nfs_server *server = NFS_SERVER(dir);
1643 struct nfs_openargs *o_arg = &data->o_arg;
1644 struct nfs_openres *o_res = &data->o_res;
1645 int status;
1646
1647 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001648 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001649 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001650 if (status != 0) {
1651 if (status == -NFS4ERR_BADNAME &&
1652 !(o_arg->open_flags & O_CREAT))
1653 return -ENOENT;
1654 return status;
1655 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001656
Trond Myklebust6926afd2012-01-07 13:22:46 -05001657 nfs_fattr_map_and_free_names(server, &data->f_attr);
1658
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001659 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001660 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001661 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1662 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001664 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001666 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 }
1668 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001669 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001670 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671}
1672
Andy Adamsond83217c2011-03-01 01:34:17 +00001673static int nfs4_recover_expired_lease(struct nfs_server *server)
1674{
1675 return nfs4_client_recover_expired_lease(server->nfs_client);
1676}
1677
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678/*
1679 * OPEN_EXPIRED:
1680 * reclaim state on the server after a network partition.
1681 * Assumes caller holds the appropriate lock
1682 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001683static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001685 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001686 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001688 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1689 if (IS_ERR(opendata))
1690 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001691 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001692 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001693 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001694 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001695 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696}
1697
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001698static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001699{
Trond Myklebust539cd032007-06-05 11:46:42 -04001700 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001701 struct nfs4_exception exception = { };
1702 int err;
1703
1704 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001705 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001706 switch (err) {
1707 default:
1708 goto out;
1709 case -NFS4ERR_GRACE:
1710 case -NFS4ERR_DELAY:
1711 nfs4_handle_exception(server, err, &exception);
1712 err = 0;
1713 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001714 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001715out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001716 return err;
1717}
1718
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1720{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001722 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
Trond Myklebust864472e2006-01-03 09:55:15 +01001724 ctx = nfs4_state_find_open_context(state);
1725 if (IS_ERR(ctx))
1726 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001727 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001728 put_nfs_open_context(ctx);
1729 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730}
1731
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001732#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001733static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001734{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001735 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001736 nfs4_stateid *stateid = &state->stateid;
1737 int status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001738
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001739 /* If a state reset has been done, test_stateid is unneeded */
1740 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1741 return;
1742
1743 status = nfs41_test_stateid(server, stateid);
1744 if (status != NFS_OK) {
1745 /* Free the stateid unless the server explicitly
1746 * informs us the stateid is unrecognized. */
1747 if (status != -NFS4ERR_BAD_STATEID)
1748 nfs41_free_stateid(server, stateid);
Bryan Schumaker69388672012-09-26 15:25:52 -04001749 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001750
Bryan Schumaker69388672012-09-26 15:25:52 -04001751 write_seqlock(&state->seqlock);
1752 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1753 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001754 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1755 }
1756}
1757
1758/**
1759 * nfs41_check_open_stateid - possibly free an open stateid
1760 *
1761 * @state: NFSv4 state for an inode
1762 *
1763 * Returns NFS_OK if recovery for this stateid is now finished.
1764 * Otherwise a negative NFS4ERR value is returned.
1765 */
1766static int nfs41_check_open_stateid(struct nfs4_state *state)
1767{
1768 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04001769 nfs4_stateid *stateid = &state->open_stateid;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001770 int status;
1771
1772 /* If a state reset has been done, test_stateid is unneeded */
1773 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1774 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1775 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1776 return -NFS4ERR_BAD_STATEID;
1777
1778 status = nfs41_test_stateid(server, stateid);
1779 if (status != NFS_OK) {
1780 /* Free the stateid unless the server explicitly
1781 * informs us the stateid is unrecognized. */
1782 if (status != -NFS4ERR_BAD_STATEID)
1783 nfs41_free_stateid(server, stateid);
1784
1785 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1786 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1787 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001788 }
1789 return status;
1790}
1791
1792static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1793{
Chuck Levereb64cf92012-07-11 16:30:05 -04001794 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001795
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001796 nfs41_clear_delegation_stateid(state);
1797 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04001798 if (status != NFS_OK)
1799 status = nfs4_open_expired(sp, state);
1800 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001801}
1802#endif
1803
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001805 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1806 * fields corresponding to attributes that were used to store the verifier.
1807 * Make sure we clobber those fields in the later setattr call
1808 */
1809static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1810{
1811 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1812 !(sattr->ia_valid & ATTR_ATIME_SET))
1813 sattr->ia_valid |= ATTR_ATIME;
1814
1815 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1816 !(sattr->ia_valid & ATTR_MTIME_SET))
1817 sattr->ia_valid |= ATTR_MTIME;
1818}
1819
1820/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001821 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 */
Andy Adamson82be4172012-05-23 05:02:35 -04001823static int _nfs4_do_open(struct inode *dir,
1824 struct dentry *dentry,
1825 fmode_t fmode,
1826 int flags,
1827 struct iattr *sattr,
1828 struct rpc_cred *cred,
1829 struct nfs4_state **res,
1830 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831{
1832 struct nfs4_state_owner *sp;
1833 struct nfs4_state *state = NULL;
1834 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001835 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001836 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
1838 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001840 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1841 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1843 goto out_err;
1844 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001845 status = nfs4_recover_expired_lease(server);
1846 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001847 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001848 if (dentry->d_inode != NULL)
1849 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001850 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001851 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001852 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001853 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
Andy Adamson82be4172012-05-23 05:02:35 -04001855 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
1856 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
1857 if (!opendata->f_attr.mdsthreshold)
1858 goto err_opendata_put;
Trond Myklebust1549210f2012-06-05 09:16:47 -04001859 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04001860 }
Al Viro82a2c1b2011-06-22 18:30:55 -04001861 if (dentry->d_inode != NULL)
1862 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001863
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001864 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001866 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001868 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001869 status = PTR_ERR(state);
1870 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001871 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001872 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001873 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001874
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001875 status = nfs4_opendata_access(cred, opendata, state, fmode);
1876 if (status != 0)
1877 goto err_opendata_put;
1878
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001879 if (opendata->o_arg.open_flags & O_EXCL) {
1880 nfs4_exclusive_attrset(opendata, sattr);
1881
1882 nfs_fattr_init(opendata->o_res.f_attr);
1883 status = nfs4_do_setattr(state->inode, cred,
1884 opendata->o_res.f_attr, sattr,
1885 state);
1886 if (status == 0)
1887 nfs_setattr_update_inode(state->inode, sattr);
1888 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1889 }
Andy Adamson82be4172012-05-23 05:02:35 -04001890
1891 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
1892 *ctx_th = opendata->f_attr.mdsthreshold;
1893 else
1894 kfree(opendata->f_attr.mdsthreshold);
1895 opendata->f_attr.mdsthreshold = NULL;
1896
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001897 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 *res = state;
1900 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001901err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04001902 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001903 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001904err_put_state_owner:
1905 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 *res = NULL;
1908 return status;
1909}
1910
1911
Andy Adamson82be4172012-05-23 05:02:35 -04001912static struct nfs4_state *nfs4_do_open(struct inode *dir,
1913 struct dentry *dentry,
1914 fmode_t fmode,
1915 int flags,
1916 struct iattr *sattr,
1917 struct rpc_cred *cred,
1918 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919{
1920 struct nfs4_exception exception = { };
1921 struct nfs4_state *res;
1922 int status;
1923
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001924 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 do {
Andy Adamson82be4172012-05-23 05:02:35 -04001926 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
1927 &res, ctx_th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 if (status == 0)
1929 break;
1930 /* NOTE: BAD_SEQID means the server and client disagree about the
1931 * book-keeping w.r.t. state-changing operations
1932 * (OPEN/CLOSE/LOCK/LOCKU...)
1933 * It is actually a sign of a bug on the client or on the server.
1934 *
1935 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001936 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 * have unhashed the old state_owner for us, and that we can
1938 * therefore safely retry using a new one. We should still warn
1939 * the user though...
1940 */
1941 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04001942 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001943 " returned a bad sequence-id error!\n",
1944 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 exception.retry = 1;
1946 continue;
1947 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001948 /*
1949 * BAD_STATEID on OPEN means that the server cancelled our
1950 * state before it received the OPEN_CONFIRM.
1951 * Recover by retrying the request as per the discussion
1952 * on Page 181 of RFC3530.
1953 */
1954 if (status == -NFS4ERR_BAD_STATEID) {
1955 exception.retry = 1;
1956 continue;
1957 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001958 if (status == -EAGAIN) {
1959 /* We must have found a delegation */
1960 exception.retry = 1;
1961 continue;
1962 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1964 status, &exception));
1965 } while (exception.retry);
1966 return res;
1967}
1968
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001969static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1970 struct nfs_fattr *fattr, struct iattr *sattr,
1971 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001973 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001975 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 .iap = sattr,
1977 .server = server,
1978 .bitmask = server->attr_bitmask,
1979 };
1980 struct nfs_setattrres res = {
1981 .fattr = fattr,
1982 .server = server,
1983 };
1984 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001985 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1986 .rpc_argp = &arg,
1987 .rpc_resp = &res,
1988 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001990 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001991 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
Trond Myklebust0e574af2005-10-27 22:12:38 -04001993 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
Trond Myklebust4fc87962012-03-08 17:42:01 -05001995 if (state != NULL) {
Trond Myklebust2a369152012-08-13 18:54:45 -04001996 struct nfs_lockowner lockowner = {
1997 .l_owner = current->files,
1998 .l_pid = current->tgid,
1999 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002000 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002001 &lockowner);
Trond Myklebust4fc87962012-03-08 17:42:01 -05002002 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2003 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002004 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002005 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002006 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
Bryan Schumaker7c513052011-03-24 17:12:24 +00002008 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002009 if (status == 0 && state != NULL)
2010 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002011 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012}
2013
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002014static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2015 struct nfs_fattr *fattr, struct iattr *sattr,
2016 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002018 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002019 struct nfs4_exception exception = {
2020 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002021 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002022 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 int err;
2024 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04002025 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2026 switch (err) {
2027 case -NFS4ERR_OPENMODE:
2028 if (state && !(state->state & FMODE_WRITE)) {
2029 err = -EBADF;
2030 if (sattr->ia_valid & ATTR_OPEN)
2031 err = -EACCES;
2032 goto out;
2033 }
2034 }
2035 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002037out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 return err;
2039}
2040
2041struct nfs4_closedata {
2042 struct inode *inode;
2043 struct nfs4_state *state;
2044 struct nfs_closeargs arg;
2045 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002046 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002047 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002048 bool roc;
2049 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050};
2051
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002052static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002053{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002054 struct nfs4_closedata *calldata = data;
2055 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002056 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002057
Fred Isamanf7e89172011-01-06 11:36:32 +00002058 if (calldata->roc)
2059 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002060 nfs4_put_open_state(calldata->state);
2061 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002062 nfs4_put_state_owner(sp);
Weston Andros Adamson324d0032012-10-30 17:01:39 -04002063 nfs_sb_deactive_async(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002064 kfree(calldata);
2065}
2066
Trond Myklebust88069f72009-12-08 08:33:16 -05002067static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2068 fmode_t fmode)
2069{
2070 spin_lock(&state->owner->so_lock);
2071 if (!(fmode & FMODE_READ))
2072 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2073 if (!(fmode & FMODE_WRITE))
2074 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2075 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2076 spin_unlock(&state->owner->so_lock);
2077}
2078
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002079static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002081 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 struct nfs_server *server = NFS_SERVER(calldata->inode);
2084
Chuck Levera3ca5652012-03-01 17:00:40 -05002085 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002086 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2087 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 /* hmm. we are done with the inode, and in the process of freeing
2089 * the state_owner. we keep this around to process errors
2090 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 switch (task->tk_status) {
2092 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002093 if (calldata->roc)
2094 pnfs_roc_set_barrier(state->inode,
2095 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002096 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002097 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002098 nfs4_close_clear_stateid_flags(state,
2099 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 break;
2101 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002102 case -NFS4ERR_OLD_STATEID:
2103 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002105 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002106 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002108 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2109 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002111 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002112 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002113 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114}
2115
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002116static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002118 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002119 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002120 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002121 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002122
Chuck Levera3ca5652012-03-01 17:00:40 -05002123 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002124 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07002125 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04002126
Trond Myklebust88069f72009-12-08 08:33:16 -05002127 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2128 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002129 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002130 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002131 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002132 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002133 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2134 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2135 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002136 }
2137 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002138 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2139 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2140 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002141 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002142 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002143 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002144
2145 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002146 /* Note: exit _without_ calling nfs4_close_done */
2147 task->tk_action = NULL;
Chuck Levera3ca5652012-03-01 17:00:40 -05002148 goto out;
Trond Myklebust95121352005-10-18 14:20:12 -07002149 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002150
Fred Isamanf7e89172011-01-06 11:36:32 +00002151 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002152 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002153 if (calldata->roc &&
Trond Myklebust7fdab062012-09-20 20:15:57 -04002154 pnfs_roc_drain(inode, &calldata->roc_barrier, task))
Chuck Levera3ca5652012-03-01 17:00:40 -05002155 goto out;
Fred Isamanf7e89172011-01-06 11:36:32 +00002156 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002157
Trond Myklebust516a6af2005-10-27 22:12:41 -04002158 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002159 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002160 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002161 &calldata->arg.seq_args,
2162 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002163 task) != 0)
2164 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002165out:
2166 dprintk("%s: done!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167}
2168
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002169static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002170 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002171 .rpc_call_done = nfs4_close_done,
2172 .rpc_release = nfs4_free_closedata,
2173};
2174
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175/*
2176 * It is possible for data to be read/written from a mem-mapped file
2177 * after the sys_close call (which hits the vfs layer as a flush).
2178 * This means that we can't safely call nfsv4 close on a file until
2179 * the inode is cleared. This in turn means that we are not good
2180 * NFSv4 citizens - we do not indicate to the server to update the file's
2181 * share state even when we are done with one of the three share
2182 * stateid's in the inode.
2183 *
2184 * NOTE: Caller must be holding the sp->so_owner semaphore!
2185 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002186int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002188 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002190 struct nfs4_state_owner *sp = state->owner;
2191 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002192 struct rpc_message msg = {
2193 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2194 .rpc_cred = state->owner->so_cred,
2195 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002196 struct rpc_task_setup task_setup_data = {
2197 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002198 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002199 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002200 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002201 .flags = RPC_TASK_ASYNC,
2202 };
Trond Myklebust95121352005-10-18 14:20:12 -07002203 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002205 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002207 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002208 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002209 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002211 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002212 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002214 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002215 if (calldata->arg.seqid == NULL)
2216 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002217 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002218 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002219 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002220 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002221 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002222 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002223 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002224
Trond Myklebust1174dd12010-12-21 10:52:24 -05002225 msg.rpc_argp = &calldata->arg;
2226 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002227 task_setup_data.callback_data = calldata;
2228 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002229 if (IS_ERR(task))
2230 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002231 status = 0;
2232 if (wait)
2233 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002234 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002235 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002236out_free_calldata:
2237 kfree(calldata);
2238out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002239 nfs4_put_open_state(state);
2240 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002241 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242}
2243
Trond Myklebust2b484292010-09-17 10:56:51 -04002244static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002245nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 struct nfs4_state *state;
2248
Trond Myklebust565277f2007-10-15 18:17:53 -04002249 /* Protect against concurrent sillydeletes */
Andy Adamson82be4172012-05-23 05:02:35 -04002250 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2251 ctx->cred, &ctx->mdsthreshold);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002252 if (IS_ERR(state))
2253 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002254 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002255 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256}
2257
Trond Myklebust1185a552009-12-03 15:54:02 -05002258static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002259{
2260 if (ctx->state == NULL)
2261 return;
2262 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002263 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002264 else
Al Viro643168c2011-06-22 18:20:23 -04002265 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002266}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267
2268static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2269{
Benny Halevy43652ad2009-04-01 09:21:54 -04002270 struct nfs4_server_caps_arg args = {
2271 .fhandle = fhandle,
2272 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 struct nfs4_server_caps_res res = {};
2274 struct rpc_message msg = {
2275 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002276 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 .rpc_resp = &res,
2278 };
2279 int status;
2280
Bryan Schumaker7c513052011-03-24 17:12:24 +00002281 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 if (status == 0) {
2283 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002284 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2285 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2286 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2287 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2288 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2290 server->caps |= NFS_CAP_ACLS;
2291 if (res.has_links != 0)
2292 server->caps |= NFS_CAP_HARDLINKS;
2293 if (res.has_symlinks != 0)
2294 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002295 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2296 server->caps |= NFS_CAP_FILEID;
2297 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2298 server->caps |= NFS_CAP_MODE;
2299 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2300 server->caps |= NFS_CAP_NLINK;
2301 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2302 server->caps |= NFS_CAP_OWNER;
2303 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2304 server->caps |= NFS_CAP_OWNER_GROUP;
2305 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2306 server->caps |= NFS_CAP_ATIME;
2307 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2308 server->caps |= NFS_CAP_CTIME;
2309 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2310 server->caps |= NFS_CAP_MTIME;
2311
Trond Myklebusta65318b2009-03-11 14:10:28 -04002312 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2313 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2314 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002316 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002318
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 return status;
2320}
2321
Trond Myklebust55a97592006-06-09 09:34:19 -04002322int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323{
2324 struct nfs4_exception exception = { };
2325 int err;
2326 do {
2327 err = nfs4_handle_exception(server,
2328 _nfs4_server_capabilities(server, fhandle),
2329 &exception);
2330 } while (exception.retry);
2331 return err;
2332}
2333
2334static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2335 struct nfs_fsinfo *info)
2336{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 struct nfs4_lookup_root_arg args = {
2338 .bitmask = nfs4_fattr_bitmap,
2339 };
2340 struct nfs4_lookup_res res = {
2341 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002342 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 .fh = fhandle,
2344 };
2345 struct rpc_message msg = {
2346 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2347 .rpc_argp = &args,
2348 .rpc_resp = &res,
2349 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002350
Trond Myklebust0e574af2005-10-27 22:12:38 -04002351 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002352 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353}
2354
2355static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2356 struct nfs_fsinfo *info)
2357{
2358 struct nfs4_exception exception = { };
2359 int err;
2360 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002361 err = _nfs4_lookup_root(server, fhandle, info);
2362 switch (err) {
2363 case 0:
2364 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002365 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002366 default:
2367 err = nfs4_handle_exception(server, err, &exception);
2368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002370out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 return err;
2372}
2373
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002374static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2375 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2376{
2377 struct rpc_auth *auth;
2378 int ret;
2379
2380 auth = rpcauth_create(flavor, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002381 if (IS_ERR(auth)) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002382 ret = -EIO;
2383 goto out;
2384 }
2385 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002386out:
2387 return ret;
2388}
2389
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002390static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002391 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002393 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002394 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002396 len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04002397 if (len < 0)
2398 return len;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002399
2400 for (i = 0; i < len; i++) {
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002401 /* AUTH_UNIX is the default flavor if none was specified,
2402 * thus has already been tried. */
2403 if (flav_array[i] == RPC_AUTH_UNIX)
2404 continue;
2405
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002406 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002407 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002408 continue;
2409 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002410 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002411 /*
2412 * -EACCESS could mean that the user doesn't have correct permissions
2413 * to access the mount. It could also mean that we tried to mount
2414 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2415 * existing mount programs don't handle -EACCES very well so it should
2416 * be mapped to -EPERM instead.
2417 */
2418 if (status == -EACCES)
2419 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002420 return status;
2421}
2422
2423/*
2424 * get the file handle for the "/" directory on the server
2425 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002426int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2427 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002428{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002429 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002430 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002431 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2432 /*
2433 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2434 * by nfs4_map_errors() as this function exits.
2435 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002436 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 if (status == 0)
2438 status = nfs4_server_capabilities(server, fhandle);
2439 if (status == 0)
2440 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002441 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442}
2443
Bryan Schumakerbae36242012-05-10 15:07:31 -04002444static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2445 struct nfs_fsinfo *info)
2446{
2447 int error;
2448 struct nfs_fattr *fattr = info->fattr;
2449
2450 error = nfs4_server_capabilities(server, mntfh);
2451 if (error < 0) {
2452 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2453 return error;
2454 }
2455
2456 error = nfs4_proc_getattr(server, mntfh, fattr);
2457 if (error < 0) {
2458 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2459 return error;
2460 }
2461
2462 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2463 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2464 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2465
2466 return error;
2467}
2468
Manoj Naik6b97fd32006-06-09 09:34:29 -04002469/*
2470 * Get locations and (maybe) other attributes of a referral.
2471 * Note that we'll actually follow the referral later when
2472 * we detect fsid mismatch in inode revalidation
2473 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002474static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2475 const struct qstr *name, struct nfs_fattr *fattr,
2476 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002477{
2478 int status = -ENOMEM;
2479 struct page *page = NULL;
2480 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002481
2482 page = alloc_page(GFP_KERNEL);
2483 if (page == NULL)
2484 goto out;
2485 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2486 if (locations == NULL)
2487 goto out;
2488
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002489 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002490 if (status != 0)
2491 goto out;
2492 /* Make sure server returned a different fsid for the referral */
2493 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002494 dprintk("%s: server did not return a different fsid for"
2495 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002496 status = -EIO;
2497 goto out;
2498 }
Andy Adamson533eb462011-06-13 18:25:56 -04002499 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2500 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002501
Andy Adamson533eb462011-06-13 18:25:56 -04002502 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002503 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002504 memset(fhandle, 0, sizeof(struct nfs_fh));
2505out:
2506 if (page)
2507 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002508 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002509 return status;
2510}
2511
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2513{
2514 struct nfs4_getattr_arg args = {
2515 .fh = fhandle,
2516 .bitmask = server->attr_bitmask,
2517 };
2518 struct nfs4_getattr_res res = {
2519 .fattr = fattr,
2520 .server = server,
2521 };
2522 struct rpc_message msg = {
2523 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2524 .rpc_argp = &args,
2525 .rpc_resp = &res,
2526 };
2527
Trond Myklebust0e574af2005-10-27 22:12:38 -04002528 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002529 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530}
2531
2532static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2533{
2534 struct nfs4_exception exception = { };
2535 int err;
2536 do {
2537 err = nfs4_handle_exception(server,
2538 _nfs4_proc_getattr(server, fhandle, fattr),
2539 &exception);
2540 } while (exception.retry);
2541 return err;
2542}
2543
2544/*
2545 * The file is not closed if it is opened due to the a request to change
2546 * the size of the file. The open call will not be needed once the
2547 * VFS layer lookup-intents are implemented.
2548 *
2549 * Close is called when the inode is destroyed.
2550 * If we haven't opened the file for O_WRONLY, we
2551 * need to in the size_change case to obtain a stateid.
2552 *
2553 * Got race?
2554 * Because OPEN is always done by name in nfsv4, it is
2555 * possible that we opened a different file by the same
2556 * name. We can recognize this race condition, but we
2557 * can't do anything about it besides returning an error.
2558 *
2559 * This will be fixed with VFS changes (lookup-intent).
2560 */
2561static int
2562nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2563 struct iattr *sattr)
2564{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002565 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002566 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002567 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 int status;
2569
Benny Halevy8a1636c2010-07-14 15:43:57 -04002570 if (pnfs_ld_layoutret_on_setattr(inode))
2571 pnfs_return_layout(inode);
2572
Trond Myklebust0e574af2005-10-27 22:12:38 -04002573 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574
Andy Adamson26699402012-05-30 16:12:24 -04002575 /* Deal with open(O_TRUNC) */
2576 if (sattr->ia_valid & ATTR_OPEN)
2577 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2578
2579 /* Optimization: if the end result is no change, don't RPC */
2580 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2581 return 0;
2582
Trond Myklebustd5308382005-11-04 15:33:38 -05002583 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002584 if (sattr->ia_valid & ATTR_FILE) {
2585 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002586
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002587 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002588 if (ctx) {
2589 cred = ctx->cred;
2590 state = ctx->state;
2591 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002592 }
2593
2594 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002595 if (status == 0)
2596 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 return status;
2598}
2599
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002600static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2601 const struct qstr *name, struct nfs_fh *fhandle,
2602 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002603{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002604 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002605 int status;
2606 struct nfs4_lookup_arg args = {
2607 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002608 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002609 .name = name,
2610 };
2611 struct nfs4_lookup_res res = {
2612 .server = server,
2613 .fattr = fattr,
2614 .fh = fhandle,
2615 };
2616 struct rpc_message msg = {
2617 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2618 .rpc_argp = &args,
2619 .rpc_resp = &res,
2620 };
2621
2622 nfs_fattr_init(fattr);
2623
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002625 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 dprintk("NFS reply lookup: %d\n", status);
2627 return status;
2628}
2629
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002630static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002631{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002632 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002633 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002634 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2635 fattr->nlink = 2;
2636}
2637
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002638static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2639 struct qstr *name, struct nfs_fh *fhandle,
2640 struct nfs_fattr *fattr)
2641{
2642 struct nfs4_exception exception = { };
2643 struct rpc_clnt *client = *clnt;
2644 int err;
2645 do {
2646 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2647 switch (err) {
2648 case -NFS4ERR_BADNAME:
2649 err = -ENOENT;
2650 goto out;
2651 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002652 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002653 goto out;
2654 case -NFS4ERR_WRONGSEC:
2655 err = -EPERM;
2656 if (client != *clnt)
2657 goto out;
2658
2659 client = nfs4_create_sec_client(client, dir, name);
2660 if (IS_ERR(client))
2661 return PTR_ERR(client);
2662
2663 exception.retry = 1;
2664 break;
2665 default:
2666 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2667 }
2668 } while (exception.retry);
2669
2670out:
2671 if (err == 0)
2672 *clnt = client;
2673 else if (client != *clnt)
2674 rpc_shutdown_client(client);
2675
2676 return err;
2677}
2678
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002679static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002680 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002682 int status;
2683 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002684
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002685 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2686 if (client != NFS_CLIENT(dir)) {
2687 rpc_shutdown_client(client);
2688 nfs_fixup_secinfo_attributes(fattr);
2689 }
2690 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691}
2692
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002693struct rpc_clnt *
2694nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2695 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2696{
2697 int status;
2698 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2699
2700 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2701 if (status < 0) {
2702 rpc_shutdown_client(client);
2703 return ERR_PTR(status);
2704 }
2705 return client;
2706}
2707
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2709{
Trond Myklebust76b32992007-08-10 17:45:11 -04002710 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 struct nfs4_accessargs args = {
2712 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002713 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002715 struct nfs4_accessres res = {
2716 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002717 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 struct rpc_message msg = {
2719 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2720 .rpc_argp = &args,
2721 .rpc_resp = &res,
2722 .rpc_cred = entry->cred,
2723 };
2724 int mode = entry->mask;
2725 int status;
2726
2727 /*
2728 * Determine which access bits we want to ask for...
2729 */
2730 if (mode & MAY_READ)
2731 args.access |= NFS4_ACCESS_READ;
2732 if (S_ISDIR(inode->i_mode)) {
2733 if (mode & MAY_WRITE)
2734 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2735 if (mode & MAY_EXEC)
2736 args.access |= NFS4_ACCESS_LOOKUP;
2737 } else {
2738 if (mode & MAY_WRITE)
2739 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2740 if (mode & MAY_EXEC)
2741 args.access |= NFS4_ACCESS_EXECUTE;
2742 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002743
2744 res.fattr = nfs_alloc_fattr();
2745 if (res.fattr == NULL)
2746 return -ENOMEM;
2747
Bryan Schumaker7c513052011-03-24 17:12:24 +00002748 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002750 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04002751 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002753 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 return status;
2755}
2756
2757static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2758{
2759 struct nfs4_exception exception = { };
2760 int err;
2761 do {
2762 err = nfs4_handle_exception(NFS_SERVER(inode),
2763 _nfs4_proc_access(inode, entry),
2764 &exception);
2765 } while (exception.retry);
2766 return err;
2767}
2768
2769/*
2770 * TODO: For the time being, we don't try to get any attributes
2771 * along with any of the zero-copy operations READ, READDIR,
2772 * READLINK, WRITE.
2773 *
2774 * In the case of the first three, we want to put the GETATTR
2775 * after the read-type operation -- this is because it is hard
2776 * to predict the length of a GETATTR response in v4, and thus
2777 * align the READ data correctly. This means that the GETATTR
2778 * may end up partially falling into the page cache, and we should
2779 * shift it into the 'tail' of the xdr_buf before processing.
2780 * To do this efficiently, we need to know the total length
2781 * of data received, which doesn't seem to be available outside
2782 * of the RPC layer.
2783 *
2784 * In the case of WRITE, we also want to put the GETATTR after
2785 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04002786 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 *
2788 * Both of these changes to the XDR layer would in fact be quite
2789 * minor, but I decided to leave them for a subsequent patch.
2790 */
2791static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2792 unsigned int pgbase, unsigned int pglen)
2793{
2794 struct nfs4_readlink args = {
2795 .fh = NFS_FH(inode),
2796 .pgbase = pgbase,
2797 .pglen = pglen,
2798 .pages = &page,
2799 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002800 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 struct rpc_message msg = {
2802 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2803 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002804 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 };
2806
Bryan Schumaker7c513052011-03-24 17:12:24 +00002807 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 -07002808}
2809
2810static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2811 unsigned int pgbase, unsigned int pglen)
2812{
2813 struct nfs4_exception exception = { };
2814 int err;
2815 do {
2816 err = nfs4_handle_exception(NFS_SERVER(inode),
2817 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2818 &exception);
2819 } while (exception.retry);
2820 return err;
2821}
2822
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002824 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826static int
2827nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002828 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829{
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002830 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 int status = 0;
2833
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002834 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
2835 if (IS_ERR(ctx))
2836 return PTR_ERR(ctx);
2837
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002838 sattr->ia_mode &= ~current_umask();
Trond Myklebust8626e4a2012-07-16 12:01:42 -04002839 state = nfs4_do_open(dir, dentry, ctx->mode,
2840 flags, sattr, ctx->cred,
2841 &ctx->mdsthreshold);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002842 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 if (IS_ERR(state)) {
2844 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002845 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002847 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002848 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002849 ctx->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850out:
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002851 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 return status;
2853}
2854
2855static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2856{
Trond Myklebust16e42952005-10-27 22:12:44 -04002857 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002858 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07002860 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002862 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002863 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002864 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002866 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2867 .rpc_argp = &args,
2868 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 };
Trond Myklebust778d2812012-04-27 13:48:19 -04002870 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04002871
Bryan Schumaker7c513052011-03-24 17:12:24 +00002872 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04002873 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04002874 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 return status;
2876}
2877
2878static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2879{
2880 struct nfs4_exception exception = { };
2881 int err;
2882 do {
2883 err = nfs4_handle_exception(NFS_SERVER(dir),
2884 _nfs4_proc_remove(dir, name),
2885 &exception);
2886 } while (exception.retry);
2887 return err;
2888}
2889
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002890static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002892 struct nfs_server *server = NFS_SERVER(dir);
2893 struct nfs_removeargs *args = msg->rpc_argp;
2894 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002896 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05002898 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899}
2900
Bryan Schumaker34e137c2012-03-19 14:54:41 -04002901static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
2902{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04002903 nfs4_setup_sequence(NFS_SERVER(data->dir),
2904 &data->args.seq_args,
2905 &data->res.seq_res,
2906 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907}
2908
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002909static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002911 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2912
Trond Myklebust14516c32010-07-31 14:29:06 -04002913 if (!nfs4_sequence_done(task, &res->seq_res))
2914 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002915 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002916 return 0;
2917 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002918 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919}
2920
Jeff Laytond3d41522010-09-17 17:31:57 -04002921static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2922{
2923 struct nfs_server *server = NFS_SERVER(dir);
2924 struct nfs_renameargs *arg = msg->rpc_argp;
2925 struct nfs_renameres *res = msg->rpc_resp;
2926
2927 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04002928 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002929 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04002930}
2931
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04002932static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
2933{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04002934 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
2935 &data->args.seq_args,
2936 &data->res.seq_res,
2937 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04002938}
2939
2940static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2941 struct inode *new_dir)
2942{
2943 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2944
2945 if (!nfs4_sequence_done(task, &res->seq_res))
2946 return 0;
2947 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2948 return 0;
2949
2950 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04002951 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04002952 return 1;
2953}
2954
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2956 struct inode *new_dir, struct qstr *new_name)
2957{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002958 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002959 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 .old_dir = NFS_FH(old_dir),
2961 .new_dir = NFS_FH(new_dir),
2962 .old_name = old_name,
2963 .new_name = new_name,
2964 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002965 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002966 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002967 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 struct rpc_message msg = {
2969 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2970 .rpc_argp = &arg,
2971 .rpc_resp = &res,
2972 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002973 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974
Bryan Schumaker7c513052011-03-24 17:12:24 +00002975 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 if (!status) {
2977 update_changeattr(old_dir, &res.old_cinfo);
2978 update_changeattr(new_dir, &res.new_cinfo);
2979 }
2980 return status;
2981}
2982
2983static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2984 struct inode *new_dir, struct qstr *new_name)
2985{
2986 struct nfs4_exception exception = { };
2987 int err;
2988 do {
2989 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2990 _nfs4_proc_rename(old_dir, old_name,
2991 new_dir, new_name),
2992 &exception);
2993 } while (exception.retry);
2994 return err;
2995}
2996
2997static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2998{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002999 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 struct nfs4_link_arg arg = {
3001 .fh = NFS_FH(inode),
3002 .dir_fh = NFS_FH(dir),
3003 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003004 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003006 struct nfs4_link_res res = {
3007 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003008 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 struct rpc_message msg = {
3010 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3011 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003012 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003014 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015
Trond Myklebust136f2622010-04-16 16:22:49 -04003016 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003017 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003018 goto out;
3019
Bryan Schumaker7c513052011-03-24 17:12:24 +00003020 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003021 if (!status) {
3022 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04003023 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003024 }
Trond Myklebust136f2622010-04-16 16:22:49 -04003025out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003026 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 return status;
3028}
3029
3030static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3031{
3032 struct nfs4_exception exception = { };
3033 int err;
3034 do {
3035 err = nfs4_handle_exception(NFS_SERVER(inode),
3036 _nfs4_proc_link(inode, dir, name),
3037 &exception);
3038 } while (exception.retry);
3039 return err;
3040}
3041
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003042struct nfs4_createdata {
3043 struct rpc_message msg;
3044 struct nfs4_create_arg arg;
3045 struct nfs4_create_res res;
3046 struct nfs_fh fh;
3047 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003048};
3049
3050static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3051 struct qstr *name, struct iattr *sattr, u32 ftype)
3052{
3053 struct nfs4_createdata *data;
3054
3055 data = kzalloc(sizeof(*data), GFP_KERNEL);
3056 if (data != NULL) {
3057 struct nfs_server *server = NFS_SERVER(dir);
3058
3059 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3060 data->msg.rpc_argp = &data->arg;
3061 data->msg.rpc_resp = &data->res;
3062 data->arg.dir_fh = NFS_FH(dir);
3063 data->arg.server = server;
3064 data->arg.name = name;
3065 data->arg.attrs = sattr;
3066 data->arg.ftype = ftype;
3067 data->arg.bitmask = server->attr_bitmask;
3068 data->res.server = server;
3069 data->res.fh = &data->fh;
3070 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003071 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003072 }
3073 return data;
3074}
3075
3076static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3077{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003078 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003079 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003080 if (status == 0) {
3081 update_changeattr(dir, &data->res.dir_cinfo);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003082 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3083 }
3084 return status;
3085}
3086
3087static void nfs4_free_createdata(struct nfs4_createdata *data)
3088{
3089 kfree(data);
3090}
3091
Chuck Lever4f390c12006-08-22 20:06:22 -04003092static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003093 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003095 struct nfs4_createdata *data;
3096 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097
Chuck Lever94a6d752006-08-22 20:06:23 -04003098 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003099 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003100
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003101 status = -ENOMEM;
3102 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3103 if (data == NULL)
3104 goto out;
3105
3106 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3107 data->arg.u.symlink.pages = &page;
3108 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003110 status = nfs4_do_create(dir, dentry, data);
3111
3112 nfs4_free_createdata(data);
3113out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 return status;
3115}
3116
Chuck Lever4f390c12006-08-22 20:06:22 -04003117static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003118 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119{
3120 struct nfs4_exception exception = { };
3121 int err;
3122 do {
3123 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003124 _nfs4_proc_symlink(dir, dentry, page,
3125 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 &exception);
3127 } while (exception.retry);
3128 return err;
3129}
3130
3131static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3132 struct iattr *sattr)
3133{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003134 struct nfs4_createdata *data;
3135 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003137 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3138 if (data == NULL)
3139 goto out;
3140
3141 status = nfs4_do_create(dir, dentry, data);
3142
3143 nfs4_free_createdata(data);
3144out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 return status;
3146}
3147
3148static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3149 struct iattr *sattr)
3150{
3151 struct nfs4_exception exception = { };
3152 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003153
3154 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 do {
3156 err = nfs4_handle_exception(NFS_SERVER(dir),
3157 _nfs4_proc_mkdir(dir, dentry, sattr),
3158 &exception);
3159 } while (exception.retry);
3160 return err;
3161}
3162
3163static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003164 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165{
3166 struct inode *dir = dentry->d_inode;
3167 struct nfs4_readdir_arg args = {
3168 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003169 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 .pgbase = 0,
3171 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003172 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003173 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 };
3175 struct nfs4_readdir_res res;
3176 struct rpc_message msg = {
3177 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3178 .rpc_argp = &args,
3179 .rpc_resp = &res,
3180 .rpc_cred = cred,
3181 };
3182 int status;
3183
Harvey Harrison3110ff82008-05-02 13:42:44 -07003184 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003185 dentry->d_parent->d_name.name,
3186 dentry->d_name.name,
3187 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003188 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003190 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 -05003191 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003192 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003193 status += args.pgbase;
3194 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003195
3196 nfs_invalidate_atime(dir);
3197
Harvey Harrison3110ff82008-05-02 13:42:44 -07003198 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 return status;
3200}
3201
3202static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003203 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204{
3205 struct nfs4_exception exception = { };
3206 int err;
3207 do {
3208 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3209 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003210 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 &exception);
3212 } while (exception.retry);
3213 return err;
3214}
3215
3216static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3217 struct iattr *sattr, dev_t rdev)
3218{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003219 struct nfs4_createdata *data;
3220 int mode = sattr->ia_mode;
3221 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003223 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3224 if (data == NULL)
3225 goto out;
3226
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003228 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003230 data->arg.ftype = NF4BLK;
3231 data->arg.u.device.specdata1 = MAJOR(rdev);
3232 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 }
3234 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003235 data->arg.ftype = NF4CHR;
3236 data->arg.u.device.specdata1 = MAJOR(rdev);
3237 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003238 } else if (!S_ISSOCK(mode)) {
3239 status = -EINVAL;
3240 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003243 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003244out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003245 nfs4_free_createdata(data);
3246out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 return status;
3248}
3249
3250static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3251 struct iattr *sattr, dev_t rdev)
3252{
3253 struct nfs4_exception exception = { };
3254 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003255
3256 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 do {
3258 err = nfs4_handle_exception(NFS_SERVER(dir),
3259 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3260 &exception);
3261 } while (exception.retry);
3262 return err;
3263}
3264
3265static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3266 struct nfs_fsstat *fsstat)
3267{
3268 struct nfs4_statfs_arg args = {
3269 .fh = fhandle,
3270 .bitmask = server->attr_bitmask,
3271 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003272 struct nfs4_statfs_res res = {
3273 .fsstat = fsstat,
3274 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 struct rpc_message msg = {
3276 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3277 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003278 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 };
3280
Trond Myklebust0e574af2005-10-27 22:12:38 -04003281 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003282 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283}
3284
3285static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3286{
3287 struct nfs4_exception exception = { };
3288 int err;
3289 do {
3290 err = nfs4_handle_exception(server,
3291 _nfs4_proc_statfs(server, fhandle, fsstat),
3292 &exception);
3293 } while (exception.retry);
3294 return err;
3295}
3296
3297static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3298 struct nfs_fsinfo *fsinfo)
3299{
3300 struct nfs4_fsinfo_arg args = {
3301 .fh = fhandle,
3302 .bitmask = server->attr_bitmask,
3303 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003304 struct nfs4_fsinfo_res res = {
3305 .fsinfo = fsinfo,
3306 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 struct rpc_message msg = {
3308 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3309 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003310 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 };
3312
Bryan Schumaker7c513052011-03-24 17:12:24 +00003313 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314}
3315
3316static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3317{
3318 struct nfs4_exception exception = { };
3319 int err;
3320
3321 do {
3322 err = nfs4_handle_exception(server,
3323 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3324 &exception);
3325 } while (exception.retry);
3326 return err;
3327}
3328
3329static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3330{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003331 int error;
3332
Trond Myklebust0e574af2005-10-27 22:12:38 -04003333 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003334 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003335 if (error == 0) {
3336 /* block layout checks this! */
3337 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003338 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003339 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003340
3341 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342}
3343
3344static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3345 struct nfs_pathconf *pathconf)
3346{
3347 struct nfs4_pathconf_arg args = {
3348 .fh = fhandle,
3349 .bitmask = server->attr_bitmask,
3350 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003351 struct nfs4_pathconf_res res = {
3352 .pathconf = pathconf,
3353 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 struct rpc_message msg = {
3355 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3356 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003357 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 };
3359
3360 /* None of the pathconf attributes are mandatory to implement */
3361 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3362 memset(pathconf, 0, sizeof(*pathconf));
3363 return 0;
3364 }
3365
Trond Myklebust0e574af2005-10-27 22:12:38 -04003366 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003367 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368}
3369
3370static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3371 struct nfs_pathconf *pathconf)
3372{
3373 struct nfs4_exception exception = { };
3374 int err;
3375
3376 do {
3377 err = nfs4_handle_exception(server,
3378 _nfs4_proc_pathconf(server, fhandle, pathconf),
3379 &exception);
3380 } while (exception.retry);
3381 return err;
3382}
3383
Benny Halevyd20581a2011-05-22 19:52:03 +03003384void __nfs4_read_done_cb(struct nfs_read_data *data)
3385{
Fred Isamancd841602012-04-20 14:47:44 -04003386 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003387}
3388
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003389static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390{
Fred Isamancd841602012-04-20 14:47:44 -04003391 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003393 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003394 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003395 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003397
Benny Halevyd20581a2011-05-22 19:52:03 +03003398 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003400 renew_lease(server, data->timestamp);
3401 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402}
3403
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003404static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3405{
3406
3407 dprintk("--> %s\n", __func__);
3408
3409 if (!nfs4_sequence_done(task, &data->res.seq_res))
3410 return -EAGAIN;
3411
Benny Halevyd20581a2011-05-22 19:52:03 +03003412 return data->read_done_cb ? data->read_done_cb(task, data) :
3413 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003414}
3415
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003416static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003419 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003420 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003421 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422}
3423
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003424static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3425{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003426 nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3427 &data->args.seq_args,
3428 &data->res.seq_res,
3429 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430}
3431
Fred Isamanb029bc92011-03-03 15:13:42 +00003432static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433{
Fred Isamancd841602012-04-20 14:47:44 -04003434 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003436 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003437 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003438 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003440 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003442 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003443 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003444 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445}
3446
Fred Isamanb029bc92011-03-03 15:13:42 +00003447static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3448{
3449 if (!nfs4_sequence_done(task, &data->res.seq_res))
3450 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003451 return data->write_done_cb ? data->write_done_cb(task, data) :
3452 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003453}
3454
Trond Myklebust5a37f852012-04-28 14:55:16 -04003455static
3456bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003457{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003458 const struct nfs_pgio_header *hdr = data->header;
3459
3460 /* Don't request attributes for pNFS or O_DIRECT writes */
3461 if (data->ds_clp != NULL || hdr->dreq != NULL)
3462 return false;
3463 /* Otherwise, request attributes if and only if we don't hold
3464 * a delegation
3465 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04003466 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003467}
Fred Isamana69aef12011-03-03 15:13:47 +00003468
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003469static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470{
Fred Isamancd841602012-04-20 14:47:44 -04003471 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003472
Trond Myklebust5a37f852012-04-28 14:55:16 -04003473 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003474 data->args.bitmask = NULL;
3475 data->res.fattr = NULL;
3476 } else
3477 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003478
Fred Isamanb029bc92011-03-03 15:13:42 +00003479 if (!data->write_done_cb)
3480 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003481 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482 data->timestamp = jiffies;
3483
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003484 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003485 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486}
3487
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003488static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3489{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003490 nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3491 &data->args.seq_args,
3492 &data->res.seq_res,
3493 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494}
3495
Fred Isaman0b7c0152012-04-20 14:47:39 -04003496static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3497{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003498 nfs4_setup_sequence(NFS_SERVER(data->inode),
3499 &data->args.seq_args,
3500 &data->res.seq_res,
3501 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502}
3503
Fred Isaman0b7c0152012-04-20 14:47:39 -04003504static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003507
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003508 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003509 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003510 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003512 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513}
3514
Fred Isaman0b7c0152012-04-20 14:47:39 -04003515static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003516{
3517 if (!nfs4_sequence_done(task, &data->res.seq_res))
3518 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003519 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003520}
3521
Fred Isaman0b7c0152012-04-20 14:47:39 -04003522static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003524 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003525
Fred Isaman0b7c0152012-04-20 14:47:39 -04003526 if (data->commit_done_cb == NULL)
3527 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003528 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003529 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003530 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531}
3532
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003533struct nfs4_renewdata {
3534 struct nfs_client *client;
3535 unsigned long timestamp;
3536};
3537
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538/*
3539 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3540 * standalone procedure for queueing an asynchronous RENEW.
3541 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003542static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003543{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003544 struct nfs4_renewdata *data = calldata;
3545 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003546
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003547 if (atomic_read(&clp->cl_count) > 1)
3548 nfs4_schedule_state_renewal(clp);
3549 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003550 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003551}
3552
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003553static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003555 struct nfs4_renewdata *data = calldata;
3556 struct nfs_client *clp = data->client;
3557 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558
3559 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003560 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003561 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3562 return;
3563 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003564 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003565 return;
3566 }
3567 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003569 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570}
3571
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003572static const struct rpc_call_ops nfs4_renew_ops = {
3573 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003574 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003575};
3576
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003577static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578{
3579 struct rpc_message msg = {
3580 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3581 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003582 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003584 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003586 if (renew_flags == 0)
3587 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003588 if (!atomic_inc_not_zero(&clp->cl_count))
3589 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003590 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003591 if (data == NULL)
3592 return -ENOMEM;
3593 data->client = clp;
3594 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003596 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597}
3598
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003599static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600{
3601 struct rpc_message msg = {
3602 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3603 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003604 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 };
3606 unsigned long now = jiffies;
3607 int status;
3608
3609 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3610 if (status < 0)
3611 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003612 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 return 0;
3614}
3615
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003616static inline int nfs4_server_supports_acls(struct nfs_server *server)
3617{
3618 return (server->caps & NFS_CAP_ACLS)
3619 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3620 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3621}
3622
Trond Myklebust21f498c2012-08-24 10:59:25 -04003623/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3624 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003625 * the stack.
3626 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04003627#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003628
Neil Hormane9e3d722011-03-04 19:26:03 -05003629static int buf_to_pages_noslab(const void *buf, size_t buflen,
3630 struct page **pages, unsigned int *pgbase)
3631{
3632 struct page *newpage, **spages;
3633 int rc = 0;
3634 size_t len;
3635 spages = pages;
3636
3637 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04003638 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003639 newpage = alloc_page(GFP_KERNEL);
3640
3641 if (newpage == NULL)
3642 goto unwind;
3643 memcpy(page_address(newpage), buf, len);
3644 buf += len;
3645 buflen -= len;
3646 *pages++ = newpage;
3647 rc++;
3648 } while (buflen != 0);
3649
3650 return rc;
3651
3652unwind:
3653 for(; rc > 0; rc--)
3654 __free_page(spages[rc-1]);
3655 return -ENOMEM;
3656}
3657
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003658struct nfs4_cached_acl {
3659 int cached;
3660 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003661 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003662};
3663
3664static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003665{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003666 struct nfs_inode *nfsi = NFS_I(inode);
3667
3668 spin_lock(&inode->i_lock);
3669 kfree(nfsi->nfs4_acl);
3670 nfsi->nfs4_acl = acl;
3671 spin_unlock(&inode->i_lock);
3672}
3673
3674static void nfs4_zap_acl_attr(struct inode *inode)
3675{
3676 nfs4_set_cached_acl(inode, NULL);
3677}
3678
3679static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3680{
3681 struct nfs_inode *nfsi = NFS_I(inode);
3682 struct nfs4_cached_acl *acl;
3683 int ret = -ENOENT;
3684
3685 spin_lock(&inode->i_lock);
3686 acl = nfsi->nfs4_acl;
3687 if (acl == NULL)
3688 goto out;
3689 if (buf == NULL) /* user is just asking for length */
3690 goto out_len;
3691 if (acl->cached == 0)
3692 goto out;
3693 ret = -ERANGE; /* see getxattr(2) man page */
3694 if (acl->len > buflen)
3695 goto out;
3696 memcpy(buf, acl->data, acl->len);
3697out_len:
3698 ret = acl->len;
3699out:
3700 spin_unlock(&inode->i_lock);
3701 return ret;
3702}
3703
Sachin Prabhu5794d212012-04-17 14:36:40 +01003704static 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 +00003705{
3706 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003707 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003708
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003709 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003710 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003711 if (acl == NULL)
3712 goto out;
3713 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003714 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003715 } else {
3716 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3717 if (acl == NULL)
3718 goto out;
3719 acl->cached = 0;
3720 }
3721 acl->len = acl_len;
3722out:
3723 nfs4_set_cached_acl(inode, acl);
3724}
3725
Andy Adamsonbf118a32011-12-07 11:55:27 -05003726/*
3727 * The getxattr API returns the required buffer length when called with a
3728 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3729 * the required buf. On a NULL buf, we send a page of data to the server
3730 * guessing that the ACL request can be serviced by a page. If so, we cache
3731 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3732 * the cache. If not so, we throw away the page, and cache the required
3733 * length. The next getxattr call will then produce another round trip to
3734 * the server, this time with the input buf of the required size.
3735 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003736static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003737{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003738 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003739 struct nfs_getaclargs args = {
3740 .fh = NFS_FH(inode),
3741 .acl_pages = pages,
3742 .acl_len = buflen,
3743 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003744 struct nfs_getaclres res = {
3745 .acl_len = buflen,
3746 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003747 struct rpc_message msg = {
3748 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3749 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003750 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003751 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003752 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3753 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003754
Andy Adamsonbf118a32011-12-07 11:55:27 -05003755 /* As long as we're doing a round trip to the server anyway,
3756 * let's be prepared for a page of acl data. */
3757 if (npages == 0)
3758 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003759 if (npages > ARRAY_SIZE(pages))
3760 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003761
Andy Adamsonbf118a32011-12-07 11:55:27 -05003762 for (i = 0; i < npages; i++) {
3763 pages[i] = alloc_page(GFP_KERNEL);
3764 if (!pages[i])
3765 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003766 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01003767
3768 /* for decoding across pages */
3769 res.acl_scratch = alloc_page(GFP_KERNEL);
3770 if (!res.acl_scratch)
3771 goto out_free;
3772
Andy Adamsonbf118a32011-12-07 11:55:27 -05003773 args.acl_len = npages * PAGE_SIZE;
3774 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003775
Peng Taode040be2012-01-10 22:42:47 +08003776 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003777 __func__, buf, buflen, npages, args.acl_len);
3778 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3779 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003780 if (ret)
3781 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003782
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003783 /* Handle the case where the passed-in buffer is too short */
3784 if (res.acl_flags & NFS4_ACL_TRUNC) {
3785 /* Did the user only issue a request for the acl length? */
3786 if (buf == NULL)
3787 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003788 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003789 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003790 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003791 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
3792 if (buf)
3793 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
3794out_ok:
3795 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003796out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003797 for (i = 0; i < npages; i++)
3798 if (pages[i])
3799 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05003800 if (res.acl_scratch)
3801 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003802 return ret;
3803}
3804
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003805static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3806{
3807 struct nfs4_exception exception = { };
3808 ssize_t ret;
3809 do {
3810 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3811 if (ret >= 0)
3812 break;
3813 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3814 } while (exception.retry);
3815 return ret;
3816}
3817
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003818static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3819{
3820 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003821 int ret;
3822
3823 if (!nfs4_server_supports_acls(server))
3824 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003825 ret = nfs_revalidate_inode(server, inode);
3826 if (ret < 0)
3827 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003828 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3829 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003830 ret = nfs4_read_cached_acl(inode, buf, buflen);
3831 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003832 /* -ENOENT is returned if there is no ACL or if there is an ACL
3833 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003834 return ret;
3835 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003836}
3837
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003838static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003839{
3840 struct nfs_server *server = NFS_SERVER(inode);
3841 struct page *pages[NFS4ACL_MAXPAGES];
3842 struct nfs_setaclargs arg = {
3843 .fh = NFS_FH(inode),
3844 .acl_pages = pages,
3845 .acl_len = buflen,
3846 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003847 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003848 struct rpc_message msg = {
3849 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3850 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003851 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003852 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003853 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05003854 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003855
3856 if (!nfs4_server_supports_acls(server))
3857 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003858 if (npages > ARRAY_SIZE(pages))
3859 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05003860 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3861 if (i < 0)
3862 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04003863 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003864 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003865
3866 /*
3867 * Free each page after tx, so the only ref left is
3868 * held by the network stack
3869 */
3870 for (; i > 0; i--)
3871 put_page(pages[i-1]);
3872
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003873 /*
3874 * Acl update can result in inode attribute update.
3875 * so mark the attribute cache invalid.
3876 */
3877 spin_lock(&inode->i_lock);
3878 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3879 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003880 nfs_access_zap_cache(inode);
3881 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003882 return ret;
3883}
3884
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003885static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3886{
3887 struct nfs4_exception exception = { };
3888 int err;
3889 do {
3890 err = nfs4_handle_exception(NFS_SERVER(inode),
3891 __nfs4_proc_set_acl(inode, buf, buflen),
3892 &exception);
3893 } while (exception.retry);
3894 return err;
3895}
3896
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003898nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003900 struct nfs_client *clp = server->nfs_client;
3901
3902 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903 return 0;
3904 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05003905 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003906 case -NFS4ERR_ADMIN_REVOKED:
3907 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04003908 if (state == NULL)
3909 break;
3910 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003911 case -NFS4ERR_OPENMODE:
3912 if (state == NULL)
3913 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003914 nfs4_schedule_stateid_recovery(server, state);
3915 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003916 case -NFS4ERR_EXPIRED:
3917 if (state != NULL)
3918 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003920 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003921 nfs4_schedule_lease_recovery(clp);
3922 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003923#if defined(CONFIG_NFS_V4_1)
3924 case -NFS4ERR_BADSESSION:
3925 case -NFS4ERR_BADSLOT:
3926 case -NFS4ERR_BAD_HIGH_SLOT:
3927 case -NFS4ERR_DEADSESSION:
3928 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3929 case -NFS4ERR_SEQ_FALSE_RETRY:
3930 case -NFS4ERR_SEQ_MISORDERED:
3931 dprintk("%s ERROR %d, Reset session\n", __func__,
3932 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04003933 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04003934 task->tk_status = 0;
3935 return -EAGAIN;
3936#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003938 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003939 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003940 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3942 task->tk_status = 0;
3943 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04003944 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945 case -NFS4ERR_OLD_STATEID:
3946 task->tk_status = 0;
3947 return -EAGAIN;
3948 }
3949 task->tk_status = nfs4_map_errors(task->tk_status);
3950 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003951wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003952 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003953 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3954 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3955 task->tk_status = 0;
3956 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957}
3958
Chuck Leverf0920752012-05-21 22:45:41 -04003959static void nfs4_init_boot_verifier(const struct nfs_client *clp,
3960 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05003961{
3962 __be32 verf[2];
3963
Chuck Lever2c820d92012-05-21 22:45:33 -04003964 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
3965 /* An impossible timestamp guarantees this value
3966 * will never match a generated boot time. */
3967 verf[0] = 0;
3968 verf[1] = (__be32)(NSEC_PER_SEC + 1);
3969 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04003970 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
3971 verf[0] = (__be32)nn->boot_time.tv_sec;
3972 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04003973 }
Chuck Levercd937102012-03-02 17:14:31 -05003974 memcpy(bootverf->data, verf, sizeof(bootverf->data));
3975}
3976
Chuck Levere984a552012-09-14 17:24:21 -04003977static unsigned int
3978nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
3979 char *buf, size_t len)
3980{
3981 unsigned int result;
3982
3983 rcu_read_lock();
3984 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
3985 clp->cl_ipaddr,
3986 rpc_peeraddr2str(clp->cl_rpcclient,
3987 RPC_DISPLAY_ADDR),
3988 rpc_peeraddr2str(clp->cl_rpcclient,
3989 RPC_DISPLAY_PROTO));
3990 rcu_read_unlock();
3991 return result;
3992}
3993
3994static unsigned int
3995nfs4_init_uniform_client_string(const struct nfs_client *clp,
3996 char *buf, size_t len)
3997{
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04003998 char *nodename = clp->cl_rpcclient->cl_nodename;
3999
4000 if (nfs4_client_id_uniquifier[0] != '\0')
4001 nodename = nfs4_client_id_uniquifier;
Chuck Levere984a552012-09-14 17:24:21 -04004002 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4003 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004004 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004005}
4006
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004007/**
4008 * nfs4_proc_setclientid - Negotiate client ID
4009 * @clp: state data structure
4010 * @program: RPC program for NFSv4 callback service
4011 * @port: IP port number for NFS4 callback service
4012 * @cred: RPC credential to use for this call
4013 * @res: where to place the result
4014 *
4015 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4016 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004017int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4018 unsigned short port, struct rpc_cred *cred,
4019 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020{
4021 nfs4_verifier sc_verifier;
4022 struct nfs4_setclientid setclientid = {
4023 .sc_verifier = &sc_verifier,
4024 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004025 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026 };
4027 struct rpc_message msg = {
4028 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4029 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004030 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004031 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004033 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034
Chuck Leverde734832012-07-11 16:30:50 -04004035 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004036 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004037 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4038 setclientid.sc_name_len =
4039 nfs4_init_uniform_client_string(clp,
4040 setclientid.sc_name,
4041 sizeof(setclientid.sc_name));
4042 else
4043 setclientid.sc_name_len =
4044 nfs4_init_nonuniform_client_string(clp,
4045 setclientid.sc_name,
4046 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004047 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004048 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004049 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004050 sizeof(setclientid.sc_netid),
4051 rpc_peeraddr2str(clp->cl_rpcclient,
4052 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004053 rcu_read_unlock();
4054 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004055 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056 clp->cl_ipaddr, port >> 8, port & 255);
4057
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004058 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4059 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4060 setclientid.sc_name_len, setclientid.sc_name);
4061 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4062 dprintk("NFS reply setclientid: %d\n", status);
4063 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064}
4065
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004066/**
4067 * nfs4_proc_setclientid_confirm - Confirm client ID
4068 * @clp: state data structure
4069 * @res: result of a previous SETCLIENTID
4070 * @cred: RPC credential to use for this call
4071 *
4072 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4073 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004074int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004075 struct nfs4_setclientid_res *arg,
4076 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077{
4078 struct nfs_fsinfo fsinfo;
4079 struct rpc_message msg = {
4080 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004081 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004083 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084 };
4085 unsigned long now;
4086 int status;
4087
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004088 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4089 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4090 clp->cl_clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004092 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093 if (status == 0) {
4094 spin_lock(&clp->cl_lock);
4095 clp->cl_lease_time = fsinfo.lease_time * HZ;
4096 clp->cl_last_renewal = now;
4097 spin_unlock(&clp->cl_lock);
4098 }
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004099 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100 return status;
4101}
4102
Trond Myklebustfe650402006-01-03 09:55:18 +01004103struct nfs4_delegreturndata {
4104 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004105 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004106 struct nfs_fh fh;
4107 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004108 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004109 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004110 int rpc_status;
4111};
4112
Trond Myklebustfe650402006-01-03 09:55:18 +01004113static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4114{
4115 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004116
Trond Myklebust14516c32010-07-31 14:29:06 -04004117 if (!nfs4_sequence_done(task, &data->res.seq_res))
4118 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004119
Ricardo Labiaga79708862009-12-07 09:23:21 -05004120 switch (task->tk_status) {
4121 case -NFS4ERR_STALE_STATEID:
4122 case -NFS4ERR_EXPIRED:
4123 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004124 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004125 break;
4126 default:
4127 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4128 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004129 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004130 return;
4131 }
4132 }
4133 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004134}
4135
4136static void nfs4_delegreturn_release(void *calldata)
4137{
Trond Myklebustfe650402006-01-03 09:55:18 +01004138 kfree(calldata);
4139}
4140
Andy Adamson938e1012009-04-01 09:22:28 -04004141#if defined(CONFIG_NFS_V4_1)
4142static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4143{
4144 struct nfs4_delegreturndata *d_data;
4145
4146 d_data = (struct nfs4_delegreturndata *)data;
4147
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004148 nfs4_setup_sequence(d_data->res.server,
4149 &d_data->args.seq_args,
4150 &d_data->res.seq_res,
4151 task);
Andy Adamson938e1012009-04-01 09:22:28 -04004152}
4153#endif /* CONFIG_NFS_V4_1 */
4154
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004155static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004156#if defined(CONFIG_NFS_V4_1)
4157 .rpc_call_prepare = nfs4_delegreturn_prepare,
4158#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004159 .rpc_call_done = nfs4_delegreturn_done,
4160 .rpc_release = nfs4_delegreturn_release,
4161};
4162
Trond Myklebuste6f81072008-01-24 18:14:34 -05004163static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004164{
4165 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004166 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004167 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004168 struct rpc_message msg = {
4169 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4170 .rpc_cred = cred,
4171 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004172 struct rpc_task_setup task_setup_data = {
4173 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004174 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004175 .callback_ops = &nfs4_delegreturn_ops,
4176 .flags = RPC_TASK_ASYNC,
4177 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004178 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004179
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004180 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004181 if (data == NULL)
4182 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004183 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004184 data->args.fhandle = &data->fh;
4185 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004186 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004187 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004188 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004189 data->res.fattr = &data->fattr;
4190 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004191 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004192 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004193 data->rpc_status = 0;
4194
Trond Myklebustc970aa82007-07-14 15:39:59 -04004195 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004196 msg.rpc_argp = &data->args;
4197 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004198 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004199 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004200 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004201 if (!issync)
4202 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004203 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004204 if (status != 0)
4205 goto out;
4206 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004207 if (status == 0)
4208 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4209 else
4210 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004211out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004212 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004213 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214}
4215
Trond Myklebuste6f81072008-01-24 18:14:34 -05004216int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217{
4218 struct nfs_server *server = NFS_SERVER(inode);
4219 struct nfs4_exception exception = { };
4220 int err;
4221 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004222 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 switch (err) {
4224 case -NFS4ERR_STALE_STATEID:
4225 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 case 0:
4227 return 0;
4228 }
4229 err = nfs4_handle_exception(server, err, &exception);
4230 } while (exception.retry);
4231 return err;
4232}
4233
4234#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4235#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4236
4237/*
4238 * sleep, with exponential backoff, and retry the LOCK operation.
4239 */
4240static unsigned long
4241nfs4_set_lock_task_retry(unsigned long timeout)
4242{
Jeff Laytond3103102011-12-01 22:44:39 +01004243 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244 timeout <<= 1;
4245 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4246 return NFS4_LOCK_MAXTIMEOUT;
4247 return timeout;
4248}
4249
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4251{
4252 struct inode *inode = state->inode;
4253 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004254 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004255 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004257 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004259 struct nfs_lockt_res res = {
4260 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261 };
4262 struct rpc_message msg = {
4263 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4264 .rpc_argp = &arg,
4265 .rpc_resp = &res,
4266 .rpc_cred = state->owner->so_cred,
4267 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268 struct nfs4_lock_state *lsp;
4269 int status;
4270
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004271 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004272 status = nfs4_set_lock_state(state, request);
4273 if (status != 0)
4274 goto out;
4275 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004276 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004277 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004278 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004279 switch (status) {
4280 case 0:
4281 request->fl_type = F_UNLCK;
4282 break;
4283 case -NFS4ERR_DENIED:
4284 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004286 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004287out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288 return status;
4289}
4290
4291static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4292{
4293 struct nfs4_exception exception = { };
4294 int err;
4295
4296 do {
4297 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4298 _nfs4_proc_getlk(state, cmd, request),
4299 &exception);
4300 } while (exception.retry);
4301 return err;
4302}
4303
4304static int do_vfs_lock(struct file *file, struct file_lock *fl)
4305{
4306 int res = 0;
4307 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4308 case FL_POSIX:
4309 res = posix_lock_file_wait(file, fl);
4310 break;
4311 case FL_FLOCK:
4312 res = flock_lock_file_wait(file, fl);
4313 break;
4314 default:
4315 BUG();
4316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317 return res;
4318}
4319
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004320struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004321 struct nfs_locku_args arg;
4322 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004323 struct nfs4_lock_state *lsp;
4324 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004325 struct file_lock fl;
4326 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004327 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004328};
4329
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004330static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4331 struct nfs_open_context *ctx,
4332 struct nfs4_lock_state *lsp,
4333 struct nfs_seqid *seqid)
4334{
4335 struct nfs4_unlockdata *p;
4336 struct inode *inode = lsp->ls_state->inode;
4337
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004338 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004339 if (p == NULL)
4340 return NULL;
4341 p->arg.fh = NFS_FH(inode);
4342 p->arg.fl = &p->fl;
4343 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004344 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004345 p->arg.stateid = &lsp->ls_stateid;
4346 p->lsp = lsp;
4347 atomic_inc(&lsp->ls_count);
4348 /* Ensure we don't close file until we're done freeing locks! */
4349 p->ctx = get_nfs_open_context(ctx);
4350 memcpy(&p->fl, fl, sizeof(p->fl));
4351 p->server = NFS_SERVER(inode);
4352 return p;
4353}
4354
Trond Myklebust06f814a2006-01-03 09:55:07 +01004355static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004356{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004357 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004358 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004359 nfs4_put_lock_state(calldata->lsp);
4360 put_nfs_open_context(calldata->ctx);
4361 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004362}
4363
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004364static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004365{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004366 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004367
Trond Myklebust14516c32010-07-31 14:29:06 -04004368 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4369 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004370 switch (task->tk_status) {
4371 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004372 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4373 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004374 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004375 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004376 case -NFS4ERR_BAD_STATEID:
4377 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004378 case -NFS4ERR_STALE_STATEID:
4379 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004380 break;
4381 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004382 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004383 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004384 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04004385 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004386}
4387
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004388static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004389{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004390 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004392 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004393 return;
Trond Myklebust795a88c2012-09-10 13:26:49 -04004394 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004395 /* Note: exit _without_ running nfs4_locku_done */
4396 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004397 return;
4398 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004399 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004400 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004401 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004402 &calldata->res.seq_res,
4403 task) != 0)
4404 nfs_release_seqid(calldata->arg.seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405}
4406
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004407static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004408 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004409 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004410 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004411};
4412
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004413static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4414 struct nfs_open_context *ctx,
4415 struct nfs4_lock_state *lsp,
4416 struct nfs_seqid *seqid)
4417{
4418 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004419 struct rpc_message msg = {
4420 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4421 .rpc_cred = ctx->cred,
4422 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004423 struct rpc_task_setup task_setup_data = {
4424 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004425 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004426 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004427 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004428 .flags = RPC_TASK_ASYNC,
4429 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004430
Frank Filz137d6ac2007-07-09 15:32:29 -07004431 /* Ensure this is an unlock - when canceling a lock, the
4432 * canceled lock is passed in, and it won't be an unlock.
4433 */
4434 fl->fl_type = F_UNLCK;
4435
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004436 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4437 if (data == NULL) {
4438 nfs_free_seqid(seqid);
4439 return ERR_PTR(-ENOMEM);
4440 }
4441
Trond Myklebust9d12b212012-01-17 22:04:25 -05004442 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004443 msg.rpc_argp = &data->arg;
4444 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004445 task_setup_data.callback_data = data;
4446 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004447}
4448
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4450{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004451 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004452 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004453 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004454 struct rpc_task *task;
4455 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004456 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457
Trond Myklebust9b073572006-06-29 16:38:34 -04004458 status = nfs4_set_lock_state(state, request);
4459 /* Unlock _before_ we do the RPC call */
4460 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004461 down_read(&nfsi->rwsem);
4462 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4463 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004464 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004465 }
4466 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004467 if (status != 0)
4468 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004469 /* Is this a delegated lock? */
4470 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004471 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004472 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004473 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004474 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004475 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004476 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004477 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004478 status = PTR_ERR(task);
4479 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004480 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004481 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004482 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004483out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004484 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004485 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486}
4487
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004488struct nfs4_lockdata {
4489 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004490 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004491 struct nfs4_lock_state *lsp;
4492 struct nfs_open_context *ctx;
4493 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004494 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004495 int rpc_status;
4496 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004497 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004498};
4499
4500static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004501 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4502 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004503{
4504 struct nfs4_lockdata *p;
4505 struct inode *inode = lsp->ls_state->inode;
4506 struct nfs_server *server = NFS_SERVER(inode);
4507
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004508 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004509 if (p == NULL)
4510 return NULL;
4511
4512 p->arg.fh = NFS_FH(inode);
4513 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004514 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004515 if (p->arg.open_seqid == NULL)
4516 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004517 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004518 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004519 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004520 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004521 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004522 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004523 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004524 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004525 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004526 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004527 atomic_inc(&lsp->ls_count);
4528 p->ctx = get_nfs_open_context(ctx);
4529 memcpy(&p->fl, fl, sizeof(p->fl));
4530 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004531out_free_seqid:
4532 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004533out_free:
4534 kfree(p);
4535 return NULL;
4536}
4537
4538static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4539{
4540 struct nfs4_lockdata *data = calldata;
4541 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542
Harvey Harrison3110ff82008-05-02 13:42:44 -07004543 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004544 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4545 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004546 /* Do we need to do an open_to_lock_owner? */
4547 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004548 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004549 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004550 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004551 data->arg.open_stateid = &state->stateid;
4552 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004553 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004554 } else
4555 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004556 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004557 if (nfs4_setup_sequence(data->server,
4558 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004559 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004560 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04004561 return;
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004562 nfs_release_seqid(data->arg.open_seqid);
4563out_release_lock_seqid:
4564 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004565 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004566}
4567
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004568static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4569{
4570 struct nfs4_lockdata *data = calldata;
4571
Harvey Harrison3110ff82008-05-02 13:42:44 -07004572 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004573
Trond Myklebust14516c32010-07-31 14:29:06 -04004574 if (!nfs4_sequence_done(task, &data->res.seq_res))
4575 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004576
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004577 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004578 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004579 if (data->rpc_status == 0)
4580 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4581 else
4582 goto out;
4583 }
4584 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004585 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004586 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04004587 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004588 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004589out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004590 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004591}
4592
4593static void nfs4_lock_release(void *calldata)
4594{
4595 struct nfs4_lockdata *data = calldata;
4596
Harvey Harrison3110ff82008-05-02 13:42:44 -07004597 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004598 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004599 if (data->cancelled != 0) {
4600 struct rpc_task *task;
4601 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4602 data->arg.lock_seqid);
4603 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004604 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004605 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004606 } else
4607 nfs_free_seqid(data->arg.lock_seqid);
4608 nfs4_put_lock_state(data->lsp);
4609 put_nfs_open_context(data->ctx);
4610 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004611 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004612}
4613
4614static const struct rpc_call_ops nfs4_lock_ops = {
4615 .rpc_call_prepare = nfs4_lock_prepare,
4616 .rpc_call_done = nfs4_lock_done,
4617 .rpc_release = nfs4_lock_release,
4618};
4619
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004620static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4621{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004622 switch (error) {
4623 case -NFS4ERR_ADMIN_REVOKED:
4624 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004625 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004626 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04004627 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004628 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004629 break;
4630 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004631 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004632 case -NFS4ERR_EXPIRED:
4633 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004634 };
4635}
4636
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004637static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004638{
4639 struct nfs4_lockdata *data;
4640 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004641 struct rpc_message msg = {
4642 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4643 .rpc_cred = state->owner->so_cred,
4644 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004645 struct rpc_task_setup task_setup_data = {
4646 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004647 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004648 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004649 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004650 .flags = RPC_TASK_ASYNC,
4651 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004652 int ret;
4653
Harvey Harrison3110ff82008-05-02 13:42:44 -07004654 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004655 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004656 fl->fl_u.nfs4_fl.owner,
4657 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004658 if (data == NULL)
4659 return -ENOMEM;
4660 if (IS_SETLKW(cmd))
4661 data->arg.block = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004662 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004663 msg.rpc_argp = &data->arg;
4664 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004665 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004666 if (recovery_type > NFS_LOCK_NEW) {
4667 if (recovery_type == NFS_LOCK_RECLAIM)
4668 data->arg.reclaim = NFS_LOCK_RECLAIM;
4669 nfs4_set_sequence_privileged(&data->arg.seq_args);
4670 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04004671 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004672 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004673 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004674 ret = nfs4_wait_for_completion_rpc_task(task);
4675 if (ret == 0) {
4676 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004677 if (ret)
4678 nfs4_handle_setlk_error(data->server, data->lsp,
4679 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004680 } else
4681 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004682 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004683 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004684 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685}
4686
4687static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4688{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004689 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004690 struct nfs4_exception exception = {
4691 .inode = state->inode,
4692 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004693 int err;
4694
4695 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004696 /* Cache the lock if possible... */
4697 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4698 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004699 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004700 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004701 break;
4702 nfs4_handle_exception(server, err, &exception);
4703 } while (exception.retry);
4704 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705}
4706
4707static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4708{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004709 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004710 struct nfs4_exception exception = {
4711 .inode = state->inode,
4712 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004713 int err;
4714
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004715 err = nfs4_set_lock_state(state, request);
4716 if (err != 0)
4717 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004718 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004719 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4720 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004721 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004722 switch (err) {
4723 default:
4724 goto out;
4725 case -NFS4ERR_GRACE:
4726 case -NFS4ERR_DELAY:
4727 nfs4_handle_exception(server, err, &exception);
4728 err = 0;
4729 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004730 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004731out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004732 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733}
4734
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004735#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004736/**
4737 * nfs41_check_expired_locks - possibly free a lock stateid
4738 *
4739 * @state: NFSv4 state for an inode
4740 *
4741 * Returns NFS_OK if recovery for this stateid is now finished.
4742 * Otherwise a negative NFS4ERR value is returned.
4743 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004744static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004745{
Chuck Levereb64cf92012-07-11 16:30:05 -04004746 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004747 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004748 struct nfs_server *server = NFS_SERVER(state->inode);
4749
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004750 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04004751 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004752 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4753 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004754 /* Free the stateid unless the server
4755 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04004756 if (status != -NFS4ERR_BAD_STATEID)
4757 nfs41_free_stateid(server,
4758 &lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004759 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004760 ret = status;
4761 }
4762 }
4763 };
4764
4765 return ret;
4766}
4767
4768static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4769{
4770 int status = NFS_OK;
4771
4772 if (test_bit(LK_STATE_IN_USE, &state->flags))
4773 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04004774 if (status != NFS_OK)
4775 status = nfs4_lock_expired(state, request);
4776 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004777}
4778#endif
4779
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4781{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004782 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004783 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004784 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004786 if ((fl_flags & FL_POSIX) &&
4787 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4788 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004789 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004790 status = nfs4_set_lock_state(state, request);
4791 if (status != 0)
4792 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004793 request->fl_flags |= FL_ACCESS;
4794 status = do_vfs_lock(request->fl_file, request);
4795 if (status < 0)
4796 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004797 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004798 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004799 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004800 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004801 request->fl_flags = fl_flags & ~FL_SLEEP;
4802 status = do_vfs_lock(request->fl_file, request);
4803 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004804 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004805 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004806 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004807 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004808 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004809 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004810 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004811 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4812 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004813out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004814 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004815out:
4816 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817 return status;
4818}
4819
4820static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4821{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004822 struct nfs4_exception exception = {
4823 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04004824 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004825 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826 int err;
4827
4828 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004829 err = _nfs4_proc_setlk(state, cmd, request);
4830 if (err == -NFS4ERR_DENIED)
4831 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004833 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834 } while (exception.retry);
4835 return err;
4836}
4837
4838static int
4839nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4840{
4841 struct nfs_open_context *ctx;
4842 struct nfs4_state *state;
4843 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4844 int status;
4845
4846 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004847 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848 state = ctx->state;
4849
4850 if (request->fl_start < 0 || request->fl_end < 0)
4851 return -EINVAL;
4852
Trond Myklebustd9531262009-07-21 19:22:38 -04004853 if (IS_GETLK(cmd)) {
4854 if (state != NULL)
4855 return nfs4_proc_getlk(state, F_GETLK, request);
4856 return 0;
4857 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858
4859 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4860 return -EINVAL;
4861
Trond Myklebustd9531262009-07-21 19:22:38 -04004862 if (request->fl_type == F_UNLCK) {
4863 if (state != NULL)
4864 return nfs4_proc_unlck(state, cmd, request);
4865 return 0;
4866 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867
Trond Myklebustd9531262009-07-21 19:22:38 -04004868 if (state == NULL)
4869 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04004870 /*
4871 * Don't rely on the VFS having checked the file open mode,
4872 * since it won't do this for flock() locks.
4873 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04004874 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04004875 case F_RDLCK:
4876 if (!(filp->f_mode & FMODE_READ))
4877 return -EBADF;
4878 break;
4879 case F_WRLCK:
4880 if (!(filp->f_mode & FMODE_WRITE))
4881 return -EBADF;
4882 }
4883
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884 do {
4885 status = nfs4_proc_setlk(state, cmd, request);
4886 if ((status != -EAGAIN) || IS_SETLK(cmd))
4887 break;
4888 timeout = nfs4_set_lock_task_retry(timeout);
4889 status = -ERESTARTSYS;
4890 if (signalled())
4891 break;
4892 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893 return status;
4894}
4895
Trond Myklebust888e6942005-11-04 15:38:11 -05004896int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4897{
4898 struct nfs_server *server = NFS_SERVER(state->inode);
4899 struct nfs4_exception exception = { };
4900 int err;
4901
4902 err = nfs4_set_lock_state(state, fl);
4903 if (err != 0)
4904 goto out;
4905 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004906 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004907 switch (err) {
4908 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004909 printk(KERN_ERR "NFS: %s: unhandled error "
4910 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004911 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004912 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004913 goto out;
4914 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004915 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07004916 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004917 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004918 nfs4_schedule_lease_recovery(server->nfs_client);
4919 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004920 case -NFS4ERR_BADSESSION:
4921 case -NFS4ERR_BADSLOT:
4922 case -NFS4ERR_BAD_HIGH_SLOT:
4923 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4924 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04004925 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004926 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004927 case -ERESTARTSYS:
4928 /*
4929 * The show must go on: exit, but mark the
4930 * stateid as needing recovery.
4931 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004932 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004933 case -NFS4ERR_ADMIN_REVOKED:
4934 case -NFS4ERR_BAD_STATEID:
4935 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004936 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004937 err = 0;
4938 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004939 case -EKEYEXPIRED:
4940 /*
4941 * User RPCSEC_GSS context has expired.
4942 * We cannot recover this stateid now, so
4943 * skip it and allow recovery thread to
4944 * proceed.
4945 */
4946 err = 0;
4947 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004948 case -ENOMEM:
4949 case -NFS4ERR_DENIED:
4950 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4951 err = 0;
4952 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004953 case -NFS4ERR_DELAY:
4954 break;
4955 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004956 err = nfs4_handle_exception(server, err, &exception);
4957 } while (exception.retry);
4958out:
4959 return err;
4960}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004961
Trond Myklebustcf470c32012-03-07 13:49:12 -05004962struct nfs_release_lockowner_data {
4963 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004964 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004965 struct nfs_release_lockowner_args args;
4966};
4967
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004968static void nfs4_release_lockowner_release(void *calldata)
4969{
Trond Myklebustcf470c32012-03-07 13:49:12 -05004970 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004971 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004972 kfree(calldata);
4973}
4974
Trond Myklebust17280172012-03-11 13:11:00 -04004975static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004976 .rpc_release = nfs4_release_lockowner_release,
4977};
4978
Trond Myklebustcf470c32012-03-07 13:49:12 -05004979int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004980{
4981 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004982 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004983 struct rpc_message msg = {
4984 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4985 };
4986
4987 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05004988 return -EINVAL;
4989 data = kmalloc(sizeof(*data), GFP_NOFS);
4990 if (!data)
4991 return -ENOMEM;
4992 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004993 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004994 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
4995 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
4996 data->args.lock_owner.s_dev = server->s_dev;
4997 msg.rpc_argp = &data->args;
4998 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
4999 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005000}
5001
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005002#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5003
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005004static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5005 const void *buf, size_t buflen,
5006 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005007{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005008 if (strcmp(key, "") != 0)
5009 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005010
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005011 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005012}
5013
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005014static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5015 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005016{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005017 if (strcmp(key, "") != 0)
5018 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005019
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005020 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005021}
5022
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005023static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5024 size_t list_len, const char *name,
5025 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005026{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005027 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005028
J. Bruce Fields096455a2006-03-20 23:23:42 -05005029 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5030 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005031
5032 if (list && len <= list_len)
5033 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005034 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005035}
5036
Andy Adamson533eb462011-06-13 18:25:56 -04005037/*
5038 * nfs_fhget will use either the mounted_on_fileid or the fileid
5039 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005040static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5041{
Andy Adamson533eb462011-06-13 18:25:56 -04005042 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5043 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5044 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005045 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005046 return;
5047
5048 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005049 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005050 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5051 fattr->nlink = 2;
5052}
5053
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005054static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5055 const struct qstr *name,
5056 struct nfs4_fs_locations *fs_locations,
5057 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005058{
5059 struct nfs_server *server = NFS_SERVER(dir);
5060 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005061 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005062 };
5063 struct nfs4_fs_locations_arg args = {
5064 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005065 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005066 .page = page,
5067 .bitmask = bitmask,
5068 };
Benny Halevy22958462009-04-01 09:22:02 -04005069 struct nfs4_fs_locations_res res = {
5070 .fs_locations = fs_locations,
5071 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005072 struct rpc_message msg = {
5073 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5074 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005075 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005076 };
5077 int status;
5078
Harvey Harrison3110ff82008-05-02 13:42:44 -07005079 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005080
5081 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5082 * is not supported */
5083 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5084 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5085 else
5086 bitmask[0] |= FATTR4_WORD0_FILEID;
5087
Trond Myklebustc228fd32007-01-13 02:28:11 -05005088 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005089 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005090 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005091 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005092 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005093 return status;
5094}
5095
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005096int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5097 const struct qstr *name,
5098 struct nfs4_fs_locations *fs_locations,
5099 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005100{
5101 struct nfs4_exception exception = { };
5102 int err;
5103 do {
5104 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005105 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005106 &exception);
5107 } while (exception.retry);
5108 return err;
5109}
5110
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005111static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5112{
5113 int status;
5114 struct nfs4_secinfo_arg args = {
5115 .dir_fh = NFS_FH(dir),
5116 .name = name,
5117 };
5118 struct nfs4_secinfo_res res = {
5119 .flavors = flavors,
5120 };
5121 struct rpc_message msg = {
5122 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5123 .rpc_argp = &args,
5124 .rpc_resp = &res,
5125 };
5126
5127 dprintk("NFS call secinfo %s\n", name->name);
5128 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5129 dprintk("NFS reply secinfo: %d\n", status);
5130 return status;
5131}
5132
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005133int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5134 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005135{
5136 struct nfs4_exception exception = { };
5137 int err;
5138 do {
5139 err = nfs4_handle_exception(NFS_SERVER(dir),
5140 _nfs4_proc_secinfo(dir, name, flavors),
5141 &exception);
5142 } while (exception.retry);
5143 return err;
5144}
5145
Andy Adamson557134a2009-04-01 09:21:53 -04005146#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005147/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005148 * Check the exchange flags returned by the server for invalid flags, having
5149 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5150 * DS flags set.
5151 */
5152static int nfs4_check_cl_exchange_flags(u32 flags)
5153{
5154 if (flags & ~EXCHGID4_FLAG_MASK_R)
5155 goto out_inval;
5156 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5157 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5158 goto out_inval;
5159 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5160 goto out_inval;
5161 return NFS_OK;
5162out_inval:
5163 return -NFS4ERR_INVAL;
5164}
5165
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005166static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005167nfs41_same_server_scope(struct nfs41_server_scope *a,
5168 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005169{
5170 if (a->server_scope_sz == b->server_scope_sz &&
5171 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5172 return true;
5173
5174 return false;
5175}
5176
Andy Adamson357f54d2010-12-14 10:11:57 -05005177/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005178 * nfs4_proc_bind_conn_to_session()
5179 *
5180 * The 4.1 client currently uses the same TCP connection for the
5181 * fore and backchannel.
5182 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005183int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005184{
5185 int status;
5186 struct nfs41_bind_conn_to_session_res res;
5187 struct rpc_message msg = {
5188 .rpc_proc =
5189 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5190 .rpc_argp = clp,
5191 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005192 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005193 };
5194
5195 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005196
5197 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5198 if (unlikely(res.session == NULL)) {
5199 status = -ENOMEM;
5200 goto out;
5201 }
5202
5203 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5204 if (status == 0) {
5205 if (memcmp(res.session->sess_id.data,
5206 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5207 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5208 status = -EIO;
5209 goto out_session;
5210 }
5211 if (res.dir != NFS4_CDFS4_BOTH) {
5212 dprintk("NFS: %s: Unexpected direction from server\n",
5213 __func__);
5214 status = -EIO;
5215 goto out_session;
5216 }
5217 if (res.use_conn_in_rdma_mode) {
5218 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5219 __func__);
5220 status = -EIO;
5221 goto out_session;
5222 }
5223 }
5224out_session:
5225 kfree(res.session);
5226out:
5227 dprintk("<-- %s status= %d\n", __func__, status);
5228 return status;
5229}
5230
5231/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005232 * nfs4_proc_exchange_id()
5233 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005234 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5235 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04005236 * Since the clientid has expired, all compounds using sessions
5237 * associated with the stale clientid will be returning
5238 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5239 * be in some phase of session reset.
5240 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005241int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005242{
5243 nfs4_verifier verifier;
5244 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005245 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005246 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005247 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005248 };
5249 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005250 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005251 };
5252 int status;
5253 struct rpc_message msg = {
5254 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5255 .rpc_argp = &args,
5256 .rpc_resp = &res,
5257 .rpc_cred = cred,
5258 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005259
Chuck Leverf0920752012-05-21 22:45:41 -04005260 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04005261 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5262 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005263 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5264 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5265 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005266
Chuck Leveracdeb692012-05-21 22:46:16 -04005267 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005268 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005269 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005270 status = -ENOMEM;
5271 goto out;
5272 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005273
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005274 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005275 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005276 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005277 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005278 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005279 }
5280
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005281 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005282 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005283 status = -ENOMEM;
5284 goto out_server_scope;
5285 }
5286
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005287 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005288 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005289 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005290
Chuck Lever177313f2012-05-21 22:44:58 -04005291 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005292 clp->cl_clientid = res.clientid;
5293 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5294 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5295 clp->cl_seqid = res.seqid;
5296
Chuck Leveracdeb692012-05-21 22:46:16 -04005297 kfree(clp->cl_serverowner);
5298 clp->cl_serverowner = res.server_owner;
5299 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005300
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005301 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005302 kfree(clp->cl_implid);
5303 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005304
Chuck Lever177313f2012-05-21 22:44:58 -04005305 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005306 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005307 res.server_scope)) {
5308 dprintk("%s: server_scope mismatch detected\n",
5309 __func__);
5310 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005311 kfree(clp->cl_serverscope);
5312 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005313 }
5314
Chuck Lever177313f2012-05-21 22:44:58 -04005315 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005316 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005317 goto out;
5318 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005319 } else
5320 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005321
Chuck Leveracdeb692012-05-21 22:46:16 -04005322out_server_owner:
5323 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005324out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005325 kfree(res.server_scope);
5326out:
Chuck Lever177313f2012-05-21 22:44:58 -04005327 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005328 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005329 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005330 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04005331 clp->cl_implid->date.seconds,
5332 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005333 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005334 return status;
5335}
5336
Trond Myklebust66245532012-05-25 17:18:09 -04005337static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5338 struct rpc_cred *cred)
5339{
5340 struct rpc_message msg = {
5341 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5342 .rpc_argp = clp,
5343 .rpc_cred = cred,
5344 };
5345 int status;
5346
5347 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5348 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04005349 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04005350 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5351 return status;
5352}
5353
5354static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5355 struct rpc_cred *cred)
5356{
5357 unsigned int loop;
5358 int ret;
5359
5360 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5361 ret = _nfs4_proc_destroy_clientid(clp, cred);
5362 switch (ret) {
5363 case -NFS4ERR_DELAY:
5364 case -NFS4ERR_CLIENTID_BUSY:
5365 ssleep(1);
5366 break;
5367 default:
5368 return ret;
5369 }
5370 }
5371 return 0;
5372}
5373
5374int nfs4_destroy_clientid(struct nfs_client *clp)
5375{
5376 struct rpc_cred *cred;
5377 int ret = 0;
5378
5379 if (clp->cl_mvops->minor_version < 1)
5380 goto out;
5381 if (clp->cl_exchange_flags == 0)
5382 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04005383 if (clp->cl_preserve_clid)
5384 goto out;
Trond Myklebust66245532012-05-25 17:18:09 -04005385 cred = nfs4_get_exchange_id_cred(clp);
5386 ret = nfs4_proc_destroy_clientid(clp, cred);
5387 if (cred)
5388 put_rpccred(cred);
5389 switch (ret) {
5390 case 0:
5391 case -NFS4ERR_STALE_CLIENTID:
5392 clp->cl_exchange_flags = 0;
5393 }
5394out:
5395 return ret;
5396}
5397
Andy Adamson2050f0c2009-04-01 09:22:30 -04005398struct nfs4_get_lease_time_data {
5399 struct nfs4_get_lease_time_args *args;
5400 struct nfs4_get_lease_time_res *res;
5401 struct nfs_client *clp;
5402};
5403
5404static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5405 void *calldata)
5406{
Andy Adamson2050f0c2009-04-01 09:22:30 -04005407 struct nfs4_get_lease_time_data *data =
5408 (struct nfs4_get_lease_time_data *)calldata;
5409
5410 dprintk("--> %s\n", __func__);
5411 /* just setup sequence, do not trigger session recovery
5412 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005413 nfs41_setup_sequence(data->clp->cl_session,
5414 &data->args->la_seq_args,
5415 &data->res->lr_seq_res,
5416 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005417 dprintk("<-- %s\n", __func__);
5418}
5419
5420/*
5421 * Called from nfs4_state_manager thread for session setup, so don't recover
5422 * from sequence operation or clientid errors.
5423 */
5424static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5425{
5426 struct nfs4_get_lease_time_data *data =
5427 (struct nfs4_get_lease_time_data *)calldata;
5428
5429 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005430 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5431 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005432 switch (task->tk_status) {
5433 case -NFS4ERR_DELAY:
5434 case -NFS4ERR_GRACE:
5435 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5436 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5437 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005438 /* fall through */
5439 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005440 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005441 return;
5442 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005443 dprintk("<-- %s\n", __func__);
5444}
5445
Trond Myklebust17280172012-03-11 13:11:00 -04005446static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005447 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5448 .rpc_call_done = nfs4_get_lease_time_done,
5449};
5450
5451int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5452{
5453 struct rpc_task *task;
5454 struct nfs4_get_lease_time_args args;
5455 struct nfs4_get_lease_time_res res = {
5456 .lr_fsinfo = fsinfo,
5457 };
5458 struct nfs4_get_lease_time_data data = {
5459 .args = &args,
5460 .res = &res,
5461 .clp = clp,
5462 };
5463 struct rpc_message msg = {
5464 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5465 .rpc_argp = &args,
5466 .rpc_resp = &res,
5467 };
5468 struct rpc_task_setup task_setup = {
5469 .rpc_client = clp->cl_rpcclient,
5470 .rpc_message = &msg,
5471 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005472 .callback_data = &data,
5473 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005474 };
5475 int status;
5476
Trond Myklebust9d12b212012-01-17 22:04:25 -05005477 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005478 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005479 dprintk("--> %s\n", __func__);
5480 task = rpc_run_task(&task_setup);
5481
5482 if (IS_ERR(task))
5483 status = PTR_ERR(task);
5484 else {
5485 status = task->tk_status;
5486 rpc_put_task(task);
5487 }
5488 dprintk("<-- %s return %d\n", __func__, status);
5489
5490 return status;
5491}
5492
Andy Adamsonfc931582009-04-01 09:22:31 -04005493/*
5494 * Initialize the values to be used by the client in CREATE_SESSION
5495 * If nfs4_init_session set the fore channel request and response sizes,
5496 * use them.
5497 *
5498 * Set the back channel max_resp_sz_cached to zero to force the client to
5499 * always set csa_cachethis to FALSE because the current implementation
5500 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5501 */
5502static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5503{
5504 struct nfs4_session *session = args->client->cl_session;
Trond Myklebustae72ae672012-11-20 11:02:55 -05005505 unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
5506 mxresp_sz = session->fc_target_max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005507
5508 if (mxrqst_sz == 0)
5509 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5510 if (mxresp_sz == 0)
5511 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5512 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005513 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5514 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005515 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005516 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005517
5518 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005519 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005520 __func__,
5521 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005522 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005523
5524 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005525 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5526 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5527 args->bc_attrs.max_resp_sz_cached = 0;
5528 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5529 args->bc_attrs.max_reqs = 1;
5530
5531 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5532 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5533 __func__,
5534 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5535 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5536 args->bc_attrs.max_reqs);
5537}
5538
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005539static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005540{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005541 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5542 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5543
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005544 if (rcvd->max_resp_sz > sent->max_resp_sz)
5545 return -EINVAL;
5546 /*
5547 * Our requested max_ops is the minimum we need; we're not
5548 * prepared to break up compounds into smaller pieces than that.
5549 * So, no point even trying to continue if the server won't
5550 * cooperate:
5551 */
5552 if (rcvd->max_ops < sent->max_ops)
5553 return -EINVAL;
5554 if (rcvd->max_reqs == 0)
5555 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005556 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5557 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005558 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005559}
5560
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005561static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5562{
5563 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5564 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005565
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005566 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5567 return -EINVAL;
5568 if (rcvd->max_resp_sz < sent->max_resp_sz)
5569 return -EINVAL;
5570 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5571 return -EINVAL;
5572 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005573 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005574 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005575 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005576 return -EINVAL;
5577 return 0;
5578}
Andy Adamson8d353012009-04-01 09:22:32 -04005579
Andy Adamson8d353012009-04-01 09:22:32 -04005580static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5581 struct nfs4_session *session)
5582{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005583 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005584
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005585 ret = nfs4_verify_fore_channel_attrs(args, session);
5586 if (ret)
5587 return ret;
5588 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005589}
5590
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005591static int _nfs4_proc_create_session(struct nfs_client *clp,
5592 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005593{
5594 struct nfs4_session *session = clp->cl_session;
5595 struct nfs41_create_session_args args = {
5596 .client = clp,
5597 .cb_program = NFS4_CALLBACK,
5598 };
5599 struct nfs41_create_session_res res = {
5600 .client = clp,
5601 };
5602 struct rpc_message msg = {
5603 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5604 .rpc_argp = &args,
5605 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005606 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04005607 };
5608 int status;
5609
5610 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005611 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005612
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005613 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005614
Trond Myklebust43095d32012-11-20 11:13:12 -05005615 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04005616 /* Verify the session's negotiated channel_attrs values */
5617 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005618 /* Increment the clientid slot sequence id */
5619 clp->cl_seqid++;
5620 }
5621
5622 return status;
5623}
5624
5625/*
5626 * Issues a CREATE_SESSION operation to the server.
5627 * It is the responsibility of the caller to verify the session is
5628 * expired before calling this routine.
5629 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005630int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005631{
5632 int status;
5633 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005634 struct nfs4_session *session = clp->cl_session;
5635
5636 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5637
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005638 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04005639 if (status)
5640 goto out;
5641
Andy Adamsonaacd5532011-11-09 13:58:21 -05005642 /* Init or reset the session slot tables */
5643 status = nfs4_setup_session_slot_tables(session);
5644 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005645 if (status)
5646 goto out;
5647
5648 ptr = (unsigned *)&session->sess_id.data[0];
5649 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5650 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005651out:
5652 dprintk("<-- %s\n", __func__);
5653 return status;
5654}
5655
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005656/*
5657 * Issue the over-the-wire RPC DESTROY_SESSION.
5658 * The caller must serialize access to this routine.
5659 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005660int nfs4_proc_destroy_session(struct nfs4_session *session,
5661 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005662{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005663 struct rpc_message msg = {
5664 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5665 .rpc_argp = session,
5666 .rpc_cred = cred,
5667 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005668 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005669
5670 dprintk("--> nfs4_proc_destroy_session\n");
5671
5672 /* session is still being setup */
5673 if (session->clp->cl_cons_state != NFS_CS_READY)
5674 return status;
5675
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005676 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005677
5678 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04005679 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005680 "Session has been destroyed regardless...\n", status);
5681
5682 dprintk("<-- nfs4_proc_destroy_session\n");
5683 return status;
5684}
5685
Trond Myklebust7b38c362012-05-23 13:23:31 -04005686/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005687 * Renew the cl_session lease.
5688 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005689struct nfs4_sequence_data {
5690 struct nfs_client *clp;
5691 struct nfs4_sequence_args args;
5692 struct nfs4_sequence_res res;
5693};
5694
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005695static void nfs41_sequence_release(void *data)
5696{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005697 struct nfs4_sequence_data *calldata = data;
5698 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005699
Alexandros Batsakis71358402010-02-05 03:45:05 -08005700 if (atomic_read(&clp->cl_count) > 1)
5701 nfs4_schedule_state_renewal(clp);
5702 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005703 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005704}
5705
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005706static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5707{
5708 switch(task->tk_status) {
5709 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005710 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5711 return -EAGAIN;
5712 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005713 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005714 }
5715 return 0;
5716}
5717
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005718static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005719{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005720 struct nfs4_sequence_data *calldata = data;
5721 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005722
Trond Myklebust14516c32010-07-31 14:29:06 -04005723 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5724 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005725
5726 if (task->tk_status < 0) {
5727 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005728 if (atomic_read(&clp->cl_count) == 1)
5729 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005730
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005731 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5732 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005733 return;
5734 }
5735 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005736 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005737out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005738 dprintk("<-- %s\n", __func__);
5739}
5740
5741static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5742{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005743 struct nfs4_sequence_data *calldata = data;
5744 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005745 struct nfs4_sequence_args *args;
5746 struct nfs4_sequence_res *res;
5747
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005748 args = task->tk_msg.rpc_argp;
5749 res = task->tk_msg.rpc_resp;
5750
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005751 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005752}
5753
5754static const struct rpc_call_ops nfs41_sequence_ops = {
5755 .rpc_call_done = nfs41_sequence_call_done,
5756 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005757 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005758};
5759
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005760static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
5761 struct rpc_cred *cred,
5762 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005763{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005764 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005765 struct rpc_message msg = {
5766 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5767 .rpc_cred = cred,
5768 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005769 struct rpc_task_setup task_setup_data = {
5770 .rpc_client = clp->cl_rpcclient,
5771 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005772 .callback_ops = &nfs41_sequence_ops,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005773 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5774 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005775
Alexandros Batsakis71358402010-02-05 03:45:05 -08005776 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005777 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005778 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005779 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005780 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005781 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005782 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05005783 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005784 if (is_privileged)
5785 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005786 msg.rpc_argp = &calldata->args;
5787 msg.rpc_resp = &calldata->res;
5788 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005789 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005790
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005791 return rpc_run_task(&task_setup_data);
5792}
5793
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005794static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005795{
5796 struct rpc_task *task;
5797 int ret = 0;
5798
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005799 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5800 return 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005801 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005802 if (IS_ERR(task))
5803 ret = PTR_ERR(task);
5804 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005805 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005806 dprintk("<-- %s status=%d\n", __func__, ret);
5807 return ret;
5808}
5809
5810static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5811{
5812 struct rpc_task *task;
5813 int ret;
5814
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005815 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005816 if (IS_ERR(task)) {
5817 ret = PTR_ERR(task);
5818 goto out;
5819 }
5820 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005821 if (!ret) {
5822 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5823
5824 if (task->tk_status == 0)
5825 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005826 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005827 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005828 rpc_put_task(task);
5829out:
5830 dprintk("<-- %s status=%d\n", __func__, ret);
5831 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005832}
5833
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005834struct nfs4_reclaim_complete_data {
5835 struct nfs_client *clp;
5836 struct nfs41_reclaim_complete_args arg;
5837 struct nfs41_reclaim_complete_res res;
5838};
5839
5840static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5841{
5842 struct nfs4_reclaim_complete_data *calldata = data;
5843
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005844 nfs41_setup_sequence(calldata->clp->cl_session,
5845 &calldata->arg.seq_args,
5846 &calldata->res.seq_res,
5847 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005848}
5849
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005850static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5851{
5852 switch(task->tk_status) {
5853 case 0:
5854 case -NFS4ERR_COMPLETE_ALREADY:
5855 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5856 break;
5857 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005858 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005859 /* fall through */
5860 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005861 return -EAGAIN;
5862 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005863 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005864 }
5865 return 0;
5866}
5867
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005868static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5869{
5870 struct nfs4_reclaim_complete_data *calldata = data;
5871 struct nfs_client *clp = calldata->clp;
5872 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5873
5874 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005875 if (!nfs41_sequence_done(task, res))
5876 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005877
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005878 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5879 rpc_restart_call_prepare(task);
5880 return;
5881 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005882 dprintk("<-- %s\n", __func__);
5883}
5884
5885static void nfs4_free_reclaim_complete_data(void *data)
5886{
5887 struct nfs4_reclaim_complete_data *calldata = data;
5888
5889 kfree(calldata);
5890}
5891
5892static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5893 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5894 .rpc_call_done = nfs4_reclaim_complete_done,
5895 .rpc_release = nfs4_free_reclaim_complete_data,
5896};
5897
5898/*
5899 * Issue a global reclaim complete.
5900 */
5901static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5902{
5903 struct nfs4_reclaim_complete_data *calldata;
5904 struct rpc_task *task;
5905 struct rpc_message msg = {
5906 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5907 };
5908 struct rpc_task_setup task_setup_data = {
5909 .rpc_client = clp->cl_rpcclient,
5910 .rpc_message = &msg,
5911 .callback_ops = &nfs4_reclaim_complete_call_ops,
5912 .flags = RPC_TASK_ASYNC,
5913 };
5914 int status = -ENOMEM;
5915
5916 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005917 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005918 if (calldata == NULL)
5919 goto out;
5920 calldata->clp = clp;
5921 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005922
Trond Myklebust9d12b212012-01-17 22:04:25 -05005923 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005924 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005925 msg.rpc_argp = &calldata->arg;
5926 msg.rpc_resp = &calldata->res;
5927 task_setup_data.callback_data = calldata;
5928 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005929 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005930 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005931 goto out;
5932 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05005933 status = nfs4_wait_for_completion_rpc_task(task);
5934 if (status == 0)
5935 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005936 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005937 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005938out:
5939 dprintk("<-- %s status=%d\n", __func__, status);
5940 return status;
5941}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005942
5943static void
5944nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5945{
5946 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00005947 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04005948 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005949
5950 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00005951 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5952 * right now covering the LAYOUTGET we are about to send.
5953 * However, that is not so catastrophic, and there seems
5954 * to be no way to prevent it completely.
5955 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04005956 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005957 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005958 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00005959 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5960 NFS_I(lgp->args.inode)->layout,
5961 lgp->args.ctx->state)) {
5962 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00005963 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005964}
5965
5966static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5967{
5968 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07005969 struct inode *inode = lgp->args.inode;
5970 struct nfs_server *server = NFS_SERVER(inode);
5971 struct pnfs_layout_hdr *lo;
5972 struct nfs4_state *state = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005973
5974 dprintk("--> %s\n", __func__);
5975
Trond Myklebust6ba7db32012-10-22 20:07:20 -04005976 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07005977 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005978
5979 switch (task->tk_status) {
5980 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07005981 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005982 case -NFS4ERR_LAYOUTTRYLATER:
5983 case -NFS4ERR_RECALLCONFLICT:
5984 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07005985 break;
5986 case -NFS4ERR_EXPIRED:
5987 case -NFS4ERR_BAD_STATEID:
5988 spin_lock(&inode->i_lock);
5989 lo = NFS_I(inode)->layout;
5990 if (!lo || list_empty(&lo->plh_segs)) {
5991 spin_unlock(&inode->i_lock);
5992 /* If the open stateid was bad, then recover it. */
5993 state = lgp->args.ctx->state;
5994 } else {
5995 LIST_HEAD(head);
5996
5997 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
5998 spin_unlock(&inode->i_lock);
5999 /* Mark the bad layout state as invalid, then
6000 * retry using the open stateid. */
6001 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006002 }
6003 }
Trond Myklebustee314c22012-10-01 17:25:48 -07006004 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6005 rpc_restart_call_prepare(task);
6006out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006007 dprintk("<-- %s\n", __func__);
6008}
6009
Idan Kedar85541162012-08-02 11:47:10 +03006010static size_t max_response_pages(struct nfs_server *server)
6011{
6012 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6013 return nfs_page_array_len(0, max_resp_sz);
6014}
6015
6016static void nfs4_free_pages(struct page **pages, size_t size)
6017{
6018 int i;
6019
6020 if (!pages)
6021 return;
6022
6023 for (i = 0; i < size; i++) {
6024 if (!pages[i])
6025 break;
6026 __free_page(pages[i]);
6027 }
6028 kfree(pages);
6029}
6030
6031static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6032{
6033 struct page **pages;
6034 int i;
6035
6036 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6037 if (!pages) {
6038 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6039 return NULL;
6040 }
6041
6042 for (i = 0; i < size; i++) {
6043 pages[i] = alloc_page(gfp_flags);
6044 if (!pages[i]) {
6045 dprintk("%s: failed to allocate page\n", __func__);
6046 nfs4_free_pages(pages, size);
6047 return NULL;
6048 }
6049 }
6050
6051 return pages;
6052}
6053
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006054static void nfs4_layoutget_release(void *calldata)
6055{
6056 struct nfs4_layoutget *lgp = calldata;
Idan Kedar85541162012-08-02 11:47:10 +03006057 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6058 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006059
6060 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006061 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006062 put_nfs_open_context(lgp->args.ctx);
6063 kfree(calldata);
6064 dprintk("<-- %s\n", __func__);
6065}
6066
6067static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6068 .rpc_call_prepare = nfs4_layoutget_prepare,
6069 .rpc_call_done = nfs4_layoutget_done,
6070 .rpc_release = nfs4_layoutget_release,
6071};
6072
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006073struct pnfs_layout_segment *
6074nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006075{
6076 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Idan Kedar85541162012-08-02 11:47:10 +03006077 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006078 struct rpc_task *task;
6079 struct rpc_message msg = {
6080 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6081 .rpc_argp = &lgp->args,
6082 .rpc_resp = &lgp->res,
6083 };
6084 struct rpc_task_setup task_setup_data = {
6085 .rpc_client = server->client,
6086 .rpc_message = &msg,
6087 .callback_ops = &nfs4_layoutget_call_ops,
6088 .callback_data = lgp,
6089 .flags = RPC_TASK_ASYNC,
6090 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006091 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006092 int status = 0;
6093
6094 dprintk("--> %s\n", __func__);
6095
Idan Kedar85541162012-08-02 11:47:10 +03006096 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6097 if (!lgp->args.layout.pages) {
6098 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006099 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006100 }
6101 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
6102
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006103 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006104 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006105 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006106 task = rpc_run_task(&task_setup_data);
6107 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006108 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006109 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006110 if (status == 0)
6111 status = task->tk_status;
6112 if (status == 0)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006113 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006114 rpc_put_task(task);
6115 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006116 if (status)
6117 return ERR_PTR(status);
6118 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006119}
6120
Benny Halevycbe82602011-05-22 19:52:37 +03006121static void
6122nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6123{
6124 struct nfs4_layoutreturn *lrp = calldata;
6125
6126 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006127 nfs41_setup_sequence(lrp->clp->cl_session,
6128 &lrp->args.seq_args,
6129 &lrp->res.seq_res,
6130 task);
Benny Halevycbe82602011-05-22 19:52:37 +03006131}
6132
6133static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6134{
6135 struct nfs4_layoutreturn *lrp = calldata;
6136 struct nfs_server *server;
6137
6138 dprintk("--> %s\n", __func__);
6139
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006140 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03006141 return;
6142
6143 server = NFS_SERVER(lrp->args.inode);
6144 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006145 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006146 return;
6147 }
Benny Halevycbe82602011-05-22 19:52:37 +03006148 dprintk("<-- %s\n", __func__);
6149}
6150
6151static void nfs4_layoutreturn_release(void *calldata)
6152{
6153 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04006154 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006155
6156 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04006157 spin_lock(&lo->plh_inode->i_lock);
6158 if (lrp->res.lrs_present)
6159 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6160 lo->plh_block_lgets--;
6161 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006162 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006163 kfree(calldata);
6164 dprintk("<-- %s\n", __func__);
6165}
6166
6167static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6168 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6169 .rpc_call_done = nfs4_layoutreturn_done,
6170 .rpc_release = nfs4_layoutreturn_release,
6171};
6172
6173int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6174{
6175 struct rpc_task *task;
6176 struct rpc_message msg = {
6177 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6178 .rpc_argp = &lrp->args,
6179 .rpc_resp = &lrp->res,
6180 };
6181 struct rpc_task_setup task_setup_data = {
6182 .rpc_client = lrp->clp->cl_rpcclient,
6183 .rpc_message = &msg,
6184 .callback_ops = &nfs4_layoutreturn_call_ops,
6185 .callback_data = lrp,
6186 };
6187 int status;
6188
6189 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006190 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006191 task = rpc_run_task(&task_setup_data);
6192 if (IS_ERR(task))
6193 return PTR_ERR(task);
6194 status = task->tk_status;
6195 dprintk("<-- %s status=%d\n", __func__, status);
6196 rpc_put_task(task);
6197 return status;
6198}
6199
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006200/*
6201 * Retrieve the list of Data Server devices from the MDS.
6202 */
6203static int _nfs4_getdevicelist(struct nfs_server *server,
6204 const struct nfs_fh *fh,
6205 struct pnfs_devicelist *devlist)
6206{
6207 struct nfs4_getdevicelist_args args = {
6208 .fh = fh,
6209 .layoutclass = server->pnfs_curr_ld->id,
6210 };
6211 struct nfs4_getdevicelist_res res = {
6212 .devlist = devlist,
6213 };
6214 struct rpc_message msg = {
6215 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6216 .rpc_argp = &args,
6217 .rpc_resp = &res,
6218 };
6219 int status;
6220
6221 dprintk("--> %s\n", __func__);
6222 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6223 &res.seq_res, 0);
6224 dprintk("<-- %s status=%d\n", __func__, status);
6225 return status;
6226}
6227
6228int nfs4_proc_getdevicelist(struct nfs_server *server,
6229 const struct nfs_fh *fh,
6230 struct pnfs_devicelist *devlist)
6231{
6232 struct nfs4_exception exception = { };
6233 int err;
6234
6235 do {
6236 err = nfs4_handle_exception(server,
6237 _nfs4_getdevicelist(server, fh, devlist),
6238 &exception);
6239 } while (exception.retry);
6240
6241 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6242 err, devlist->num_devs);
6243
6244 return err;
6245}
6246EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6247
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006248static int
6249_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6250{
6251 struct nfs4_getdeviceinfo_args args = {
6252 .pdev = pdev,
6253 };
6254 struct nfs4_getdeviceinfo_res res = {
6255 .pdev = pdev,
6256 };
6257 struct rpc_message msg = {
6258 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6259 .rpc_argp = &args,
6260 .rpc_resp = &res,
6261 };
6262 int status;
6263
6264 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006265 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006266 dprintk("<-- %s status=%d\n", __func__, status);
6267
6268 return status;
6269}
6270
6271int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6272{
6273 struct nfs4_exception exception = { };
6274 int err;
6275
6276 do {
6277 err = nfs4_handle_exception(server,
6278 _nfs4_proc_getdeviceinfo(server, pdev),
6279 &exception);
6280 } while (exception.retry);
6281 return err;
6282}
6283EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6284
Andy Adamson863a3c62011-03-23 13:27:54 +00006285static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6286{
6287 struct nfs4_layoutcommit_data *data = calldata;
6288 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006289 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00006290
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006291 nfs41_setup_sequence(session,
6292 &data->args.seq_args,
6293 &data->res.seq_res,
6294 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00006295}
6296
6297static void
6298nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6299{
6300 struct nfs4_layoutcommit_data *data = calldata;
6301 struct nfs_server *server = NFS_SERVER(data->args.inode);
6302
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006303 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00006304 return;
6305
6306 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006307 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6308 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6309 case -NFS4ERR_BADLAYOUT: /* no layout */
6310 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006311 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006312 break;
6313 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006314 nfs_post_op_update_inode_force_wcc(data->args.inode,
6315 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006316 break;
6317 default:
6318 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6319 rpc_restart_call_prepare(task);
6320 return;
6321 }
6322 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006323}
6324
6325static void nfs4_layoutcommit_release(void *calldata)
6326{
6327 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04006328 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07006329 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00006330
Andy Adamsondb29c082011-07-30 20:52:38 -04006331 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006332 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04006333 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6334 list_del_init(&lseg->pls_lc_list);
6335 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6336 &lseg->pls_flags))
Trond Myklebust9369a432012-09-18 20:57:08 -04006337 pnfs_put_lseg(lseg);
Peng Taoa9bae562011-07-30 20:52:33 -04006338 }
Peng Tao92407e72011-10-23 20:21:17 -07006339
6340 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6341 smp_mb__after_clear_bit();
6342 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6343
Andy Adamson863a3c62011-03-23 13:27:54 +00006344 put_rpccred(data->cred);
6345 kfree(data);
6346}
6347
6348static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6349 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6350 .rpc_call_done = nfs4_layoutcommit_done,
6351 .rpc_release = nfs4_layoutcommit_release,
6352};
6353
6354int
Andy Adamsonef311532011-03-12 02:58:10 -05006355nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006356{
6357 struct rpc_message msg = {
6358 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6359 .rpc_argp = &data->args,
6360 .rpc_resp = &data->res,
6361 .rpc_cred = data->cred,
6362 };
6363 struct rpc_task_setup task_setup_data = {
6364 .task = &data->task,
6365 .rpc_client = NFS_CLIENT(data->args.inode),
6366 .rpc_message = &msg,
6367 .callback_ops = &nfs4_layoutcommit_ops,
6368 .callback_data = data,
6369 .flags = RPC_TASK_ASYNC,
6370 };
6371 struct rpc_task *task;
6372 int status = 0;
6373
6374 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6375 "lbw: %llu inode %lu\n",
6376 data->task.tk_pid, sync,
6377 data->args.lastbytewritten,
6378 data->args.inode->i_ino);
6379
Trond Myklebust9d12b212012-01-17 22:04:25 -05006380 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006381 task = rpc_run_task(&task_setup_data);
6382 if (IS_ERR(task))
6383 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006384 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006385 goto out;
6386 status = nfs4_wait_for_completion_rpc_task(task);
6387 if (status != 0)
6388 goto out;
6389 status = task->tk_status;
6390out:
6391 dprintk("%s: status %d\n", __func__, status);
6392 rpc_put_task(task);
6393 return status;
6394}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006395
6396static int
6397_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6398 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6399{
6400 struct nfs41_secinfo_no_name_args args = {
6401 .style = SECINFO_STYLE_CURRENT_FH,
6402 };
6403 struct nfs4_secinfo_res res = {
6404 .flavors = flavors,
6405 };
6406 struct rpc_message msg = {
6407 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6408 .rpc_argp = &args,
6409 .rpc_resp = &res,
6410 };
6411 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6412}
6413
6414static int
6415nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6416 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6417{
6418 struct nfs4_exception exception = { };
6419 int err;
6420 do {
6421 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6422 switch (err) {
6423 case 0:
6424 case -NFS4ERR_WRONGSEC:
6425 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006426 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006427 default:
6428 err = nfs4_handle_exception(server, err, &exception);
6429 }
6430 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006431out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006432 return err;
6433}
6434
6435static int
6436nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6437 struct nfs_fsinfo *info)
6438{
6439 int err;
6440 struct page *page;
6441 rpc_authflavor_t flavor;
6442 struct nfs4_secinfo_flavors *flavors;
6443
6444 page = alloc_page(GFP_KERNEL);
6445 if (!page) {
6446 err = -ENOMEM;
6447 goto out;
6448 }
6449
6450 flavors = page_address(page);
6451 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6452
6453 /*
6454 * Fall back on "guess and check" method if
6455 * the server doesn't support SECINFO_NO_NAME
6456 */
6457 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6458 err = nfs4_find_root_sec(server, fhandle, info);
6459 goto out_freepage;
6460 }
6461 if (err)
6462 goto out_freepage;
6463
6464 flavor = nfs_find_best_sec(flavors);
6465 if (err == 0)
6466 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6467
6468out_freepage:
6469 put_page(page);
6470 if (err == -EACCES)
6471 return -EPERM;
6472out:
6473 return err;
6474}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006475
6476static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006477{
6478 int status;
6479 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006480 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006481 };
6482 struct nfs41_test_stateid_res res;
6483 struct rpc_message msg = {
6484 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6485 .rpc_argp = &args,
6486 .rpc_resp = &res,
6487 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006488
Chuck Lever38527b12012-07-11 16:30:23 -04006489 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006490 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006491 nfs4_set_sequence_privileged(&args.seq_args);
6492 status = nfs4_call_sync_sequence(server->client, server, &msg,
6493 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04006494 if (status != NFS_OK) {
6495 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04006496 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04006497 }
6498 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04006499 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006500}
6501
Chuck Lever38527b12012-07-11 16:30:23 -04006502/**
6503 * nfs41_test_stateid - perform a TEST_STATEID operation
6504 *
6505 * @server: server / transport on which to perform the operation
6506 * @stateid: state ID to test
6507 *
6508 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6509 * Otherwise a negative NFS4ERR value is returned if the operation
6510 * failed or the state ID is not currently valid.
6511 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006512static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006513{
6514 struct nfs4_exception exception = { };
6515 int err;
6516 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006517 err = _nfs41_test_stateid(server, stateid);
6518 if (err != -NFS4ERR_DELAY)
6519 break;
6520 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04006521 } while (exception.retry);
6522 return err;
6523}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006524
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006525static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006526{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006527 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006528 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006529 };
6530 struct nfs41_free_stateid_res res;
6531 struct rpc_message msg = {
6532 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6533 .rpc_argp = &args,
6534 .rpc_resp = &res,
6535 };
Chuck Lever38527b12012-07-11 16:30:23 -04006536 int status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006537
Chuck Lever38527b12012-07-11 16:30:23 -04006538 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006539 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006540 nfs4_set_sequence_privileged(&args.seq_args);
Chuck Lever38527b12012-07-11 16:30:23 -04006541 status = nfs4_call_sync_sequence(server->client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006542 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04006543 dprintk("NFS reply free_stateid: %d\n", status);
6544 return status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006545}
6546
Chuck Lever38527b12012-07-11 16:30:23 -04006547/**
6548 * nfs41_free_stateid - perform a FREE_STATEID operation
6549 *
6550 * @server: server / transport on which to perform the operation
6551 * @stateid: state ID to release
6552 *
6553 * Returns NFS_OK if the server freed "stateid". Otherwise a
6554 * negative NFS4ERR value is returned.
6555 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006556static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006557{
6558 struct nfs4_exception exception = { };
6559 int err;
6560 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006561 err = _nfs4_free_stateid(server, stateid);
6562 if (err != -NFS4ERR_DELAY)
6563 break;
6564 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006565 } while (exception.retry);
6566 return err;
6567}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006568
6569static bool nfs41_match_stateid(const nfs4_stateid *s1,
6570 const nfs4_stateid *s2)
6571{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006572 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006573 return false;
6574
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006575 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006576 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006577 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006578 return true;
6579
6580 return false;
6581}
6582
Andy Adamson557134a2009-04-01 09:21:53 -04006583#endif /* CONFIG_NFS_V4_1 */
6584
Trond Myklebust36281ca2012-03-04 18:13:56 -05006585static bool nfs4_match_stateid(const nfs4_stateid *s1,
6586 const nfs4_stateid *s2)
6587{
Trond Myklebustf597c532012-03-04 18:13:56 -05006588 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006589}
6590
6591
Trond Myklebust17280172012-03-11 13:11:00 -04006592static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006593 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006594 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006595 .recover_open = nfs4_open_reclaim,
6596 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006597 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006598 .get_clid_cred = nfs4_get_setclientid_cred,
Chuck Lever05f4c352012-09-14 17:24:32 -04006599 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006600};
6601
Andy Adamson591d71c2009-04-01 09:22:47 -04006602#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006603static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006604 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6605 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6606 .recover_open = nfs4_open_reclaim,
6607 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006608 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006609 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006610 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04006611 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04006612};
6613#endif /* CONFIG_NFS_V4_1 */
6614
Trond Myklebust17280172012-03-11 13:11:00 -04006615static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006616 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006617 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006618 .recover_open = nfs4_open_expired,
6619 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006620 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006621 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006622};
6623
Andy Adamson591d71c2009-04-01 09:22:47 -04006624#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006625static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006626 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6627 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006628 .recover_open = nfs41_open_expired,
6629 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006630 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006631 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006632};
6633#endif /* CONFIG_NFS_V4_1 */
6634
Trond Myklebust17280172012-03-11 13:11:00 -04006635static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006636 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006637 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006638 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006639};
6640
6641#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006642static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006643 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006644 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006645 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006646};
6647#endif
6648
Trond Myklebust97dc1352010-06-16 09:52:26 -04006649static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6650 .minor_version = 0,
6651 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006652 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006653 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006654 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6655 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6656 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006657};
6658
6659#if defined(CONFIG_NFS_V4_1)
6660static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6661 .minor_version = 1,
6662 .call_sync = _nfs4_call_sync_session,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006663 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006664 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006665 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6666 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6667 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006668};
6669#endif
6670
Trond Myklebust97dc1352010-06-16 09:52:26 -04006671const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6672 [0] = &nfs_v4_0_minor_ops,
6673#if defined(CONFIG_NFS_V4_1)
6674 [1] = &nfs_v4_1_minor_ops,
6675#endif
6676};
6677
Bryan Schumaker73a79702012-07-16 16:39:12 -04006678const struct inode_operations nfs4_dir_inode_operations = {
6679 .create = nfs_create,
6680 .lookup = nfs_lookup,
6681 .atomic_open = nfs_atomic_open,
6682 .link = nfs_link,
6683 .unlink = nfs_unlink,
6684 .symlink = nfs_symlink,
6685 .mkdir = nfs_mkdir,
6686 .rmdir = nfs_rmdir,
6687 .mknod = nfs_mknod,
6688 .rename = nfs_rename,
6689 .permission = nfs_permission,
6690 .getattr = nfs_getattr,
6691 .setattr = nfs_setattr,
6692 .getxattr = generic_getxattr,
6693 .setxattr = generic_setxattr,
6694 .listxattr = generic_listxattr,
6695 .removexattr = generic_removexattr,
6696};
6697
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006698static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006699 .permission = nfs_permission,
6700 .getattr = nfs_getattr,
6701 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006702 .getxattr = generic_getxattr,
6703 .setxattr = generic_setxattr,
6704 .listxattr = generic_listxattr,
6705 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006706};
6707
David Howells509de812006-08-22 20:06:11 -04006708const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006709 .version = 4, /* protocol version */
6710 .dentry_ops = &nfs4_dentry_operations,
6711 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006712 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006713 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006714 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04006715 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04006716 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006717 .getattr = nfs4_proc_getattr,
6718 .setattr = nfs4_proc_setattr,
6719 .lookup = nfs4_proc_lookup,
6720 .access = nfs4_proc_access,
6721 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006722 .create = nfs4_proc_create,
6723 .remove = nfs4_proc_remove,
6724 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04006725 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006726 .unlink_done = nfs4_proc_unlink_done,
6727 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006728 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04006729 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04006730 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006731 .link = nfs4_proc_link,
6732 .symlink = nfs4_proc_symlink,
6733 .mkdir = nfs4_proc_mkdir,
6734 .rmdir = nfs4_proc_remove,
6735 .readdir = nfs4_proc_readdir,
6736 .mknod = nfs4_proc_mknod,
6737 .statfs = nfs4_proc_statfs,
6738 .fsinfo = nfs4_proc_fsinfo,
6739 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006740 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006741 .decode_dirent = nfs4_decode_dirent,
6742 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04006743 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04006744 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05006745 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006746 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04006747 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04006748 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006749 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006750 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04006751 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006752 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006753 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006754 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006755 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006756 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04006757 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04006758 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04006759 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00006760 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04006761 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04006762 .create_server = nfs4_create_server,
6763 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006764};
6765
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006766static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6767 .prefix = XATTR_NAME_NFSV4_ACL,
6768 .list = nfs4_xattr_list_nfs4_acl,
6769 .get = nfs4_xattr_get_nfs4_acl,
6770 .set = nfs4_xattr_set_nfs4_acl,
6771};
6772
6773const struct xattr_handler *nfs4_xattr_handlers[] = {
6774 &nfs4_xattr_nfs4_acl_handler,
6775 NULL
6776};
6777
Linus Torvalds1da177e2005-04-16 15:20:36 -07006778/*
6779 * Local variables:
6780 * c-basic-offset: 8
6781 * End:
6782 */