blob: 9003b8f6b77fe25c3954323fda5feda6cdecc9f1 [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:
336 ret = nfs4_delay(server->client, &exception->timeout);
337 if (ret != 0)
338 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400339 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500340 case -NFS4ERR_OLD_STATEID:
341 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800342 break;
343 case -NFS4ERR_BADOWNER:
344 /* The following works around a Linux server bug! */
345 case -NFS4ERR_BADNAME:
346 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
347 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
348 exception->retry = 1;
349 printk(KERN_WARNING "NFS: v4 server %s "
350 "does not accept raw "
351 "uid/gids. "
352 "Reenabling the idmapper.\n",
353 server->nfs_client->cl_hostname);
354 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500355 }
356 /* We failed to handle the error */
357 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500358wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500359 ret = nfs4_wait_clnt_recover(clp);
360 if (ret == 0)
361 exception->retry = 1;
362 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500363}
364
365
Trond Myklebust452e9352010-07-31 14:29:06 -0400366static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 spin_lock(&clp->cl_lock);
369 if (time_before(clp->cl_last_renewal,timestamp))
370 clp->cl_last_renewal = timestamp;
371 spin_unlock(&clp->cl_lock);
372}
373
Trond Myklebust452e9352010-07-31 14:29:06 -0400374static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
375{
376 do_renew_lease(server->nfs_client, timestamp);
377}
378
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400379#if defined(CONFIG_NFS_V4_1)
380
Trond Myklebustd185a332010-06-16 09:52:25 -0400381static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400382{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500383 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400384 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500385 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400386
Benny Halevydfb4f3092010-09-24 09:17:01 -0400387 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400388 /* just wake up the next guy waiting since
389 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500390 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500391 return;
Andy Adamson13615872009-04-01 09:22:17 -0400392 }
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500393 tbl = res->sr_slot->table;
394 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500395
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500396 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500397 /* Be nice to the server: try to ensure that the last transmitted
398 * value for highest_user_slotid <= target_highest_slotid
399 */
400 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
401 send_new_highest_used_slotid = true;
402
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500403 if (nfs41_wake_and_assign_slot(tbl, res->sr_slot)) {
404 send_new_highest_used_slotid = false;
405 goto out_unlock;
406 }
Trond Myklebustf4af6e2a2012-11-20 14:17:32 -0500407 nfs4_free_slot(tbl, res->sr_slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500408
409 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
410 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500411out_unlock:
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;
Trond Myklebust85563072012-12-11 10:31:12 -0500424 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400425
426 /*
427 * sr_status remains 1 if an RPC level error occurred. The server
428 * may or may not have processed the sequence operation..
429 * Proceed as if the server received and processed the sequence
430 * operation.
431 */
432 if (res->sr_status == 1)
433 res->sr_status = NFS_OK;
434
Bryan Schumaker468f8612011-04-18 15:57:32 -0400435 /* don't increment the sequence number if the task wasn't sent */
436 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400437 goto out;
438
Trond Myklebust933602e2012-11-16 12:12:38 -0500439 slot = res->sr_slot;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500440 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500441
Andy Adamson691daf32009-12-04 15:55:39 -0500442 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400443 switch (res->sr_status) {
444 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400445 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust933602e2012-11-16 12:12:38 -0500446 ++slot->seq_nr;
447 timestamp = slot->renewal_time;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500448 clp = session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400449 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500450 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500451 if (res->sr_status_flags != 0)
452 nfs4_schedule_lease_recovery(clp);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500453 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400454 break;
455 case -NFS4ERR_DELAY:
456 /* The server detected a resend of the RPC call and
457 * returned NFS4ERR_DELAY as per Section 2.10.6.2
458 * of RFC5661.
459 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500460 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400461 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500462 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500463 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400464 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500465 case -NFS4ERR_BADSLOT:
466 /*
467 * The slot id we used was probably retired. Try again
468 * using a different slot id.
469 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500470 goto retry_nowait;
471 case -NFS4ERR_SEQ_MISORDERED:
472 /*
473 * Could this slot have been previously retired?
474 * If so, then the server may be expecting seq_nr = 1!
475 */
476 if (slot->seq_nr == 1)
477 break;
478 slot->seq_nr = 1;
479 goto retry_nowait;
480 case -NFS4ERR_SEQ_FALSE_RETRY:
481 ++slot->seq_nr;
482 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400483 default:
484 /* Just update the slot sequence no. */
Trond Myklebust933602e2012-11-16 12:12:38 -0500485 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400486 }
487out:
488 /* The session may be reset by one of the error handlers. */
489 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400490 nfs41_sequence_free_slot(res);
Trond Myklebust85563072012-12-11 10:31:12 -0500491 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500492retry_nowait:
493 if (rpc_restart_call_prepare(task)) {
494 task->tk_status = 0;
495 ret = 0;
496 }
497 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400498out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400499 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400500 goto out;
501 rpc_delay(task, NFS4_POLL_RETRY_MAX);
502 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400503}
504
Trond Myklebust14516c32010-07-31 14:29:06 -0400505static int nfs4_sequence_done(struct rpc_task *task,
506 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400507{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500508 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400509 return 1;
510 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400511}
512
Trond Myklebust9d12b212012-01-17 22:04:25 -0500513static void nfs41_init_sequence(struct nfs4_sequence_args *args,
514 struct nfs4_sequence_res *res, int cache_reply)
515{
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500516 args->sa_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500517 args->sa_cache_this = 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400518 args->sa_privileged = 0;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500519 if (cache_reply)
520 args->sa_cache_this = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500521 res->sr_slot = NULL;
522}
523
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400524static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
525{
526 args->sa_privileged = 1;
527}
528
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000529int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400530 struct nfs4_sequence_args *args,
531 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400532 struct rpc_task *task)
533{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400534 struct nfs4_slot *slot;
535 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400536
537 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400538 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400539 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400540 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400541
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400542 tbl = &session->fc_slot_table;
543
Trond Myklebust69d206b2012-11-22 13:21:02 -0500544 task->tk_timeout = 0;
545
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400546 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400547 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400548 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500549 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500550 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400551 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400552 }
553
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500554 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500555 if (IS_ERR(slot)) {
556 /* If out of memory, try again in 1/4 second */
557 if (slot == ERR_PTR(-ENOMEM))
558 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400559 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400560 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400561 }
562 spin_unlock(&tbl->slot_tbl_lock);
563
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500564 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400565
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500566 dprintk("<-- %s slotid=%d seqid=%d\n", __func__,
567 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400568
Benny Halevydfb4f3092010-09-24 09:17:01 -0400569 res->sr_slot = slot;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400570 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400571 /*
572 * sr_status is only set in decode_sequence, and so will remain
573 * set to 1 if an rpc level failure occurs.
574 */
575 res->sr_status = 1;
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400576out_success:
577 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400578 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400579out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400580 /* Privileged tasks are queued with top priority */
581 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400582 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
583 NULL, RPC_PRIORITY_PRIVILEGED);
584 else
585 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400586 spin_unlock(&tbl->slot_tbl_lock);
587 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400588}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000589EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400590
Trond Myklebust035168a2010-06-16 09:52:26 -0400591int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400592 struct nfs4_sequence_args *args,
593 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400594 struct rpc_task *task)
595{
Trond Myklebust035168a2010-06-16 09:52:26 -0400596 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400597 int ret = 0;
598
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400599 if (session == NULL) {
600 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400601 goto out;
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400602 }
Trond Myklebust035168a2010-06-16 09:52:26 -0400603
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500604 dprintk("--> %s clp %p session %p sr_slot %d\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400605 __func__, session->clp, session, res->sr_slot ?
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500606 res->sr_slot->slot_nr : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400607
Trond Myklebust9d12b212012-01-17 22:04:25 -0500608 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400609out:
610 dprintk("<-- %s status=%d\n", __func__, ret);
611 return ret;
612}
613
614struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400615 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400616 struct nfs4_sequence_args *seq_args;
617 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400618};
619
620static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
621{
622 struct nfs41_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400623 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400624
Trond Myklebust035168a2010-06-16 09:52:26 -0400625 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
626
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400627 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400628}
629
Andy Adamson69ab40c2009-04-01 09:22:19 -0400630static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
631{
632 struct nfs41_call_sync_data *data = calldata;
633
Trond Myklebust14516c32010-07-31 14:29:06 -0400634 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400635}
636
Trond Myklebust17280172012-03-11 13:11:00 -0400637static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400638 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400639 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400640};
641
Bryan Schumaker7c513052011-03-24 17:12:24 +0000642static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
643 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400644 struct rpc_message *msg,
645 struct nfs4_sequence_args *args,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400646 struct nfs4_sequence_res *res)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400647{
648 int ret;
649 struct rpc_task *task;
650 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400651 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400652 .seq_args = args,
653 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400654 };
655 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000656 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400657 .rpc_message = msg,
658 .callback_ops = &nfs41_call_sync_ops,
659 .callback_data = &data
660 };
661
Andy Adamsonce5039c2009-04-01 09:22:13 -0400662 task = rpc_run_task(&task_setup);
663 if (IS_ERR(task))
664 ret = PTR_ERR(task);
665 else {
666 ret = task->tk_status;
667 rpc_put_task(task);
668 }
669 return ret;
670}
671
Trond Myklebustdf896452010-06-16 09:52:26 -0400672#else
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400673static
Trond Myklebust9d12b212012-01-17 22:04:25 -0500674void nfs41_init_sequence(struct nfs4_sequence_args *args,
675 struct nfs4_sequence_res *res, int cache_reply)
676{
677}
678
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400679static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
680{
681}
682
683
Trond Myklebust14516c32010-07-31 14:29:06 -0400684static int nfs4_sequence_done(struct rpc_task *task,
685 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400686{
Trond Myklebust14516c32010-07-31 14:29:06 -0400687 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400688}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400689#endif /* CONFIG_NFS_V4_1 */
690
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400691static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000692int _nfs4_call_sync(struct rpc_clnt *clnt,
693 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400694 struct rpc_message *msg,
695 struct nfs4_sequence_args *args,
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400696 struct nfs4_sequence_res *res)
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400697{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000698 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400699}
700
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400701static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000702int nfs4_call_sync(struct rpc_clnt *clnt,
703 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000704 struct rpc_message *msg,
705 struct nfs4_sequence_args *args,
706 struct nfs4_sequence_res *res,
707 int cache_reply)
708{
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400709 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000710 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400711 args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000712}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400713
Trond Myklebust38478b22006-05-25 01:40:57 -0400714static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
Trond Myklebust38478b22006-05-25 01:40:57 -0400716 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Trond Myklebust38478b22006-05-25 01:40:57 -0400718 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400719 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700720 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400721 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700722 dir->i_version = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400723 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724}
725
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100726struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400727 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100728 struct nfs_openargs o_arg;
729 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100730 struct nfs_open_confirmargs c_arg;
731 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500732 struct nfs4_string owner_name;
733 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100734 struct nfs_fattr f_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100735 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400736 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100737 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400738 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100739 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100740 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400741 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100742 int rpc_status;
743 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100744};
745
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400746
747static void nfs4_init_opendata_res(struct nfs4_opendata *p)
748{
749 p->o_res.f_attr = &p->f_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400750 p->o_res.seqid = p->o_arg.seqid;
751 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400752 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400753 p->o_res.access_request = p->o_arg.access;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400754 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500755 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400756}
757
Al Viro82a2c1b2011-06-22 18:30:55 -0400758static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500759 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400760 const struct iattr *attrs,
761 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100762{
Al Viro82a2c1b2011-06-22 18:30:55 -0400763 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100764 struct inode *dir = parent->d_inode;
765 struct nfs_server *server = NFS_SERVER(dir);
766 struct nfs4_opendata *p;
767
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400768 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100769 if (p == NULL)
770 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400771 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100772 if (p->o_arg.seqid == NULL)
773 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400774 nfs_sb_active(dentry->d_sb);
775 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100776 p->dir = parent;
777 p->owner = sp;
778 atomic_inc(&sp->so_count);
779 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500780 p->o_arg.open_flags = flags;
781 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700782 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
783 * will return permission denied for all bits until close */
784 if (!(flags & O_EXCL)) {
785 /* ask server to check for all possible rights as results
786 * are cached */
787 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
788 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700789 }
David Howells7539bba2006-08-22 20:06:09 -0400790 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400791 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
792 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400793 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100794 p->o_arg.server = server;
795 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust1549210f2012-06-05 09:16:47 -0400796 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100797 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust536e43d2012-01-17 22:04:26 -0500798 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -0500799 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400800
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100801 p->o_arg.u.attrs = &p->attrs;
802 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500803
804 verf[0] = jiffies;
805 verf[1] = current->pid;
806 memcpy(p->o_arg.u.verifier.data, verf,
807 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100808 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100809 p->c_arg.fh = &p->o_res.fh;
810 p->c_arg.stateid = &p->o_res.stateid;
811 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400812 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400813 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100814 return p;
815err_free:
816 kfree(p);
817err:
818 dput(parent);
819 return NULL;
820}
821
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400822static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100823{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400824 struct nfs4_opendata *p = container_of(kref,
825 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400826 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400827
828 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400829 if (p->state != NULL)
830 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400831 nfs4_put_state_owner(p->owner);
832 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400833 dput(p->dentry);
834 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500835 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400836 kfree(p);
837}
838
839static void nfs4_opendata_put(struct nfs4_opendata *p)
840{
841 if (p != NULL)
842 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100843}
844
Trond Myklebust06f814a2006-01-03 09:55:07 +0100845static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
846{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100847 int ret;
848
Trond Myklebust06f814a2006-01-03 09:55:07 +0100849 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100850 return ret;
851}
852
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500853static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400854{
855 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500856
Trond Myklebust536e43d2012-01-17 22:04:26 -0500857 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500858 goto out;
859 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400860 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500861 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
862 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400863 break;
864 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500865 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
866 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400867 break;
868 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500869 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
870 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400871 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500872out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400873 return ret;
874}
875
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500876static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400877{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500878 if (delegation == NULL)
879 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500880 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400881 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500882 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400883 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500884 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400885 return 1;
886}
887
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500888static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100889{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500890 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100891 case FMODE_WRITE:
892 state->n_wronly++;
893 break;
894 case FMODE_READ:
895 state->n_rdonly++;
896 break;
897 case FMODE_READ|FMODE_WRITE:
898 state->n_rdwr++;
899 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500900 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100901}
902
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500903static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400904{
905 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -0500906 nfs4_stateid_copy(&state->stateid, stateid);
907 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500908 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400909 case FMODE_READ:
910 set_bit(NFS_O_RDONLY_STATE, &state->flags);
911 break;
912 case FMODE_WRITE:
913 set_bit(NFS_O_WRONLY_STATE, &state->flags);
914 break;
915 case FMODE_READ|FMODE_WRITE:
916 set_bit(NFS_O_RDWR_STATE, &state->flags);
917 }
918}
919
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500920static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400921{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400922 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500923 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400924 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400925}
926
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500927static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400929 /*
930 * Protect the call to nfs4_state_set_mode_locked and
931 * serialise the stateid update
932 */
933 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400934 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -0500935 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -0400936 set_bit(NFS_DELEGATED_STATE, &state->flags);
937 }
938 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500939 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400940 write_sequnlock(&state->seqlock);
941 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500942 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700943 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944}
945
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500946static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500947{
948 struct nfs_inode *nfsi = NFS_I(state->inode);
949 struct nfs_delegation *deleg_cur;
950 int ret = 0;
951
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500952 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500953
954 rcu_read_lock();
955 deleg_cur = rcu_dereference(nfsi->delegation);
956 if (deleg_cur == NULL)
957 goto no_delegation;
958
959 spin_lock(&deleg_cur->lock);
960 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500961 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500962 goto no_delegation_unlock;
963
964 if (delegation == NULL)
965 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -0500966 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -0500967 goto no_delegation_unlock;
968
Trond Myklebustb7391f42008-12-23 15:21:52 -0500969 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500970 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500971 ret = 1;
972no_delegation_unlock:
973 spin_unlock(&deleg_cur->lock);
974no_delegation:
975 rcu_read_unlock();
976
977 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500978 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500979 ret = 1;
980 }
981
982 return ret;
983}
984
985
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500986static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400987{
988 struct nfs_delegation *delegation;
989
990 rcu_read_lock();
991 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500992 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -0400993 rcu_read_unlock();
994 return;
995 }
996 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400997 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400998}
999
Trond Myklebust6ee41262007-07-08 14:11:36 -04001000static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001001{
1002 struct nfs4_state *state = opendata->state;
1003 struct nfs_inode *nfsi = NFS_I(state->inode);
1004 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -05001005 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001006 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001007 nfs4_stateid stateid;
1008 int ret = -EAGAIN;
1009
Trond Myklebustaac00a82007-07-05 19:02:21 -04001010 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001011 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001012 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001013 if (can_open_cached(state, fmode, open_mode)) {
1014 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001015 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001016 goto out_return_state;
1017 }
1018 spin_unlock(&state->owner->so_lock);
1019 }
Trond Myklebust34310432008-12-23 15:21:38 -05001020 rcu_read_lock();
1021 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001022 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001023 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001024 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001025 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001026 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001027 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001028 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001029 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001030 if (ret != 0)
1031 goto out;
1032 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001033
1034 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001035 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001036 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001037 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001038out:
1039 return ERR_PTR(ret);
1040out_return_state:
1041 atomic_inc(&state->count);
1042 return state;
1043}
1044
Andy Adamsone23008e2012-10-02 21:07:32 -04001045static void
1046nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1047{
1048 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1049 struct nfs_delegation *delegation;
1050 int delegation_flags = 0;
1051
1052 rcu_read_lock();
1053 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1054 if (delegation)
1055 delegation_flags = delegation->flags;
1056 rcu_read_unlock();
1057 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1058 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1059 "returning a delegation for "
1060 "OPEN(CLAIM_DELEGATE_CUR)\n",
1061 clp->cl_hostname);
1062 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1063 nfs_inode_set_delegation(state->inode,
1064 data->owner->so_cred,
1065 &data->o_res);
1066 else
1067 nfs_inode_reclaim_delegation(state->inode,
1068 data->owner->so_cred,
1069 &data->o_res);
1070}
1071
1072/*
1073 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1074 * and update the nfs4_state.
1075 */
1076static struct nfs4_state *
1077_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1078{
1079 struct inode *inode = data->state->inode;
1080 struct nfs4_state *state = data->state;
1081 int ret;
1082
1083 if (!data->rpc_done) {
1084 ret = data->rpc_status;
1085 goto err;
1086 }
1087
1088 ret = -ESTALE;
1089 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1090 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1091 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1092 goto err;
1093
1094 ret = -ENOMEM;
1095 state = nfs4_get_open_state(inode, data->owner);
1096 if (state == NULL)
1097 goto err;
1098
1099 ret = nfs_refresh_inode(inode, &data->f_attr);
1100 if (ret)
1101 goto err;
1102
1103 if (data->o_res.delegation_type != 0)
1104 nfs4_opendata_check_deleg(data, state);
1105 update_open_stateid(state, &data->o_res.stateid, NULL,
1106 data->o_arg.fmode);
1107
1108 return state;
1109err:
1110 return ERR_PTR(ret);
1111
1112}
1113
1114static struct nfs4_state *
1115_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001116{
1117 struct inode *inode;
1118 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001119 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001120
Trond Myklebustaac00a82007-07-05 19:02:21 -04001121 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001122 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001123 goto out;
1124 }
1125
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001126 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001127 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001128 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001129 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001130 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001131 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001132 goto err;
1133 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001134 state = nfs4_get_open_state(inode, data->owner);
1135 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001136 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001137 if (data->o_res.delegation_type != 0)
1138 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001139 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001140 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001141 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001142out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001143 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001144err_put_inode:
1145 iput(inode);
1146err:
1147 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001148}
1149
Andy Adamsone23008e2012-10-02 21:07:32 -04001150static struct nfs4_state *
1151nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1152{
1153 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1154 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1155 return _nfs4_opendata_to_nfs4_state(data);
1156}
1157
Trond Myklebust864472e2006-01-03 09:55:15 +01001158static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1159{
1160 struct nfs_inode *nfsi = NFS_I(state->inode);
1161 struct nfs_open_context *ctx;
1162
1163 spin_lock(&state->inode->i_lock);
1164 list_for_each_entry(ctx, &nfsi->open_files, list) {
1165 if (ctx->state != state)
1166 continue;
1167 get_nfs_open_context(ctx);
1168 spin_unlock(&state->inode->i_lock);
1169 return ctx;
1170 }
1171 spin_unlock(&state->inode->i_lock);
1172 return ERR_PTR(-ENOENT);
1173}
1174
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001175static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1176{
1177 struct nfs4_opendata *opendata;
1178
Al Viro3d4ff432011-06-22 18:40:12 -04001179 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001180 if (opendata == NULL)
1181 return ERR_PTR(-ENOMEM);
1182 opendata->state = state;
1183 atomic_inc(&state->count);
1184 return opendata;
1185}
1186
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001187static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001188{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001189 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001190 int ret;
1191
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001192 opendata->o_arg.open_flags = 0;
1193 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001194 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1195 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1196 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001197 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001198 if (ret != 0)
1199 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001200 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001201 if (IS_ERR(newstate))
1202 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001203 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001204 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001205 return 0;
1206}
1207
1208static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1209{
Trond Myklebust864472e2006-01-03 09:55:15 +01001210 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001211 int ret;
1212
1213 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001214 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001215 smp_rmb();
1216 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001217 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001218 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001219 if (ret != 0)
1220 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001221 if (newstate != state)
1222 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001223 }
1224 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001225 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001226 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001227 if (ret != 0)
1228 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001229 if (newstate != state)
1230 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001231 }
1232 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001233 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001234 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001235 if (ret != 0)
1236 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001237 if (newstate != state)
1238 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001239 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001240 /*
1241 * We may have performed cached opens for all three recoveries.
1242 * Check if we need to update the current stateid.
1243 */
1244 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001245 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001246 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001247 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001248 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001249 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001250 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001251 return 0;
1252}
1253
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254/*
1255 * OPEN_RECLAIM:
1256 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001258static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001260 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001261 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001262 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 int status;
1264
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001265 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1266 if (IS_ERR(opendata))
1267 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001268 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1269 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001270 rcu_read_lock();
1271 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001272 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001273 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001274 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001275 opendata->o_arg.u.delegation_type = delegation_type;
1276 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001277 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 return status;
1279}
1280
Trond Myklebust539cd032007-06-05 11:46:42 -04001281static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282{
1283 struct nfs_server *server = NFS_SERVER(state->inode);
1284 struct nfs4_exception exception = { };
1285 int err;
1286 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001287 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001288 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001289 break;
1290 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 } while (exception.retry);
1292 return err;
1293}
1294
Trond Myklebust864472e2006-01-03 09:55:15 +01001295static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1296{
1297 struct nfs_open_context *ctx;
1298 int ret;
1299
1300 ctx = nfs4_state_find_open_context(state);
1301 if (IS_ERR(ctx))
1302 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001303 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001304 put_nfs_open_context(ctx);
1305 return ret;
1306}
1307
Trond Myklebust13437e12007-07-06 15:10:43 -04001308static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001310 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001311 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001313 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1314 if (IS_ERR(opendata))
1315 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001316 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebustf597c532012-03-04 18:13:56 -05001317 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust864472e2006-01-03 09:55:15 +01001318 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001319 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001320 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321}
1322
Trond Myklebust13437e12007-07-06 15:10:43 -04001323int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324{
1325 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001326 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 int err;
1328 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001329 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 switch (err) {
1331 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001332 case -ENOENT:
1333 case -ESTALE:
1334 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001335 case -NFS4ERR_BADSESSION:
1336 case -NFS4ERR_BADSLOT:
1337 case -NFS4ERR_BAD_HIGH_SLOT:
1338 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1339 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04001340 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001341 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 case -NFS4ERR_STALE_CLIENTID:
1343 case -NFS4ERR_STALE_STATEID:
1344 case -NFS4ERR_EXPIRED:
1345 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001346 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001347 goto out;
1348 case -ERESTARTSYS:
1349 /*
1350 * The show must go on: exit, but mark the
1351 * stateid as needing recovery.
1352 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001353 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001354 case -NFS4ERR_ADMIN_REVOKED:
1355 case -NFS4ERR_BAD_STATEID:
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001356 nfs_inode_find_state_and_recover(state->inode,
1357 stateid);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001358 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001359 case -ENOMEM:
1360 err = 0;
1361 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 }
1363 err = nfs4_handle_exception(server, err, &exception);
1364 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001365out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 return err;
1367}
1368
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001369static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1370{
1371 struct nfs4_opendata *data = calldata;
1372
1373 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001374 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001375 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001376 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001377 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001378 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001379 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001380}
1381
1382static void nfs4_open_confirm_release(void *calldata)
1383{
1384 struct nfs4_opendata *data = calldata;
1385 struct nfs4_state *state = NULL;
1386
1387 /* If this request hasn't been cancelled, do nothing */
1388 if (data->cancelled == 0)
1389 goto out_free;
1390 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001391 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001392 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001393 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001394 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001395 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001396out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001397 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001398}
1399
1400static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001401 .rpc_call_done = nfs4_open_confirm_done,
1402 .rpc_release = nfs4_open_confirm_release,
1403};
1404
1405/*
1406 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1407 */
1408static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1409{
1410 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1411 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001412 struct rpc_message msg = {
1413 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1414 .rpc_argp = &data->c_arg,
1415 .rpc_resp = &data->c_res,
1416 .rpc_cred = data->owner->so_cred,
1417 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001418 struct rpc_task_setup task_setup_data = {
1419 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001420 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001421 .callback_ops = &nfs4_open_confirm_ops,
1422 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001423 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001424 .flags = RPC_TASK_ASYNC,
1425 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 int status;
1427
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001428 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001429 data->rpc_done = 0;
1430 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001431 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001432 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001433 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001434 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001435 status = nfs4_wait_for_completion_rpc_task(task);
1436 if (status != 0) {
1437 data->cancelled = 1;
1438 smp_wmb();
1439 } else
1440 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001441 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 return status;
1443}
1444
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001445static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001447 struct nfs4_opendata *data = calldata;
1448 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001449
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001450 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1451 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001452 /*
1453 * Check if we still need to send an OPEN call, or if we can use
1454 * a delegation instead.
1455 */
1456 if (data->state != NULL) {
1457 struct nfs_delegation *delegation;
1458
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001459 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001460 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001461 rcu_read_lock();
1462 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001463 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1464 can_open_delegated(delegation, data->o_arg.fmode))
1465 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001466 rcu_read_unlock();
1467 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001468 /* Update client id. */
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001469 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001470 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001471 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Andy Adamsone23008e2012-10-02 21:07:32 -04001472 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001473 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1474 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001475 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001476 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001477 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001478 &data->o_res.seq_res,
1479 task) != 0)
1480 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001481 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001482unlock_no_action:
1483 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001484out_no_action:
1485 task->tk_action = NULL;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05001486 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001487}
1488
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001489static void nfs4_open_done(struct rpc_task *task, void *calldata)
1490{
1491 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001493 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001494
Trond Myklebust14516c32010-07-31 14:29:06 -04001495 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1496 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001497
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001498 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001499 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1500 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001501 case S_IFREG:
1502 break;
1503 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001504 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001505 break;
1506 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001507 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001508 break;
1509 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001510 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001511 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001512 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001513 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001514 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1515 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001516 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001517 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001518}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001519
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001520static void nfs4_open_release(void *calldata)
1521{
1522 struct nfs4_opendata *data = calldata;
1523 struct nfs4_state *state = NULL;
1524
1525 /* If this request hasn't been cancelled, do nothing */
1526 if (data->cancelled == 0)
1527 goto out_free;
1528 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001529 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001530 goto out_free;
1531 /* In case we need an open_confirm, no cleanup! */
1532 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1533 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001534 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001535 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001536 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001537out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001538 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001539}
1540
1541static const struct rpc_call_ops nfs4_open_ops = {
1542 .rpc_call_prepare = nfs4_open_prepare,
1543 .rpc_call_done = nfs4_open_done,
1544 .rpc_release = nfs4_open_release,
1545};
1546
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001547static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001548{
1549 struct inode *dir = data->dir->d_inode;
1550 struct nfs_server *server = NFS_SERVER(dir);
1551 struct nfs_openargs *o_arg = &data->o_arg;
1552 struct nfs_openres *o_res = &data->o_res;
1553 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001554 struct rpc_message msg = {
1555 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1556 .rpc_argp = o_arg,
1557 .rpc_resp = o_res,
1558 .rpc_cred = data->owner->so_cred,
1559 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001560 struct rpc_task_setup task_setup_data = {
1561 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001562 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001563 .callback_ops = &nfs4_open_ops,
1564 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001565 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001566 .flags = RPC_TASK_ASYNC,
1567 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001568 int status;
1569
Trond Myklebust9d12b212012-01-17 22:04:25 -05001570 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001571 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001572 data->rpc_done = 0;
1573 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001574 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001575 if (isrecover)
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001576 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04001577 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001578 if (IS_ERR(task))
1579 return PTR_ERR(task);
1580 status = nfs4_wait_for_completion_rpc_task(task);
1581 if (status != 0) {
1582 data->cancelled = 1;
1583 smp_wmb();
1584 } else
1585 status = data->rpc_status;
1586 rpc_put_task(task);
1587
1588 return status;
1589}
1590
1591static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1592{
1593 struct inode *dir = data->dir->d_inode;
1594 struct nfs_openres *o_res = &data->o_res;
1595 int status;
1596
1597 status = nfs4_run_open_task(data, 1);
1598 if (status != 0 || !data->rpc_done)
1599 return status;
1600
Trond Myklebust6926afd2012-01-07 13:22:46 -05001601 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1602
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001603 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1604 status = _nfs4_proc_open_confirm(data);
1605 if (status != 0)
1606 return status;
1607 }
1608
1609 return status;
1610}
1611
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001612static int nfs4_opendata_access(struct rpc_cred *cred,
1613 struct nfs4_opendata *opendata,
1614 struct nfs4_state *state, fmode_t fmode)
1615{
1616 struct nfs_access_entry cache;
1617 u32 mask;
1618
1619 /* access call failed or for some reason the server doesn't
1620 * support any access modes -- defer access call until later */
1621 if (opendata->o_res.access_supported == 0)
1622 return 0;
1623
1624 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001625 /* don't check MAY_WRITE - a newly created file may not have
1626 * write mode bits, but POSIX allows the creating process to write */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001627 if (fmode & FMODE_READ)
1628 mask |= MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001629 if (fmode & FMODE_EXEC)
1630 mask |= MAY_EXEC;
1631
1632 cache.cred = cred;
1633 cache.jiffies = jiffies;
1634 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1635 nfs_access_add_cache(state->inode, &cache);
1636
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001637 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001638 return 0;
1639
1640 /* even though OPEN succeeded, access is denied. Close the file */
1641 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001642 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001643}
1644
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001645/*
1646 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1647 */
1648static int _nfs4_proc_open(struct nfs4_opendata *data)
1649{
1650 struct inode *dir = data->dir->d_inode;
1651 struct nfs_server *server = NFS_SERVER(dir);
1652 struct nfs_openargs *o_arg = &data->o_arg;
1653 struct nfs_openres *o_res = &data->o_res;
1654 int status;
1655
1656 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001657 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001658 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001659 if (status != 0) {
1660 if (status == -NFS4ERR_BADNAME &&
1661 !(o_arg->open_flags & O_CREAT))
1662 return -ENOENT;
1663 return status;
1664 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001665
Trond Myklebust6926afd2012-01-07 13:22:46 -05001666 nfs_fattr_map_and_free_names(server, &data->f_attr);
1667
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001668 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001669 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001670 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1671 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001673 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001675 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 }
1677 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001678 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001679 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680}
1681
Andy Adamsond83217c2011-03-01 01:34:17 +00001682static int nfs4_recover_expired_lease(struct nfs_server *server)
1683{
1684 return nfs4_client_recover_expired_lease(server->nfs_client);
1685}
1686
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687/*
1688 * OPEN_EXPIRED:
1689 * reclaim state on the server after a network partition.
1690 * Assumes caller holds the appropriate lock
1691 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001692static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001694 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001695 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001697 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1698 if (IS_ERR(opendata))
1699 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001700 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001701 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001702 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001703 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001704 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705}
1706
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001707static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001708{
Trond Myklebust539cd032007-06-05 11:46:42 -04001709 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001710 struct nfs4_exception exception = { };
1711 int err;
1712
1713 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001714 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001715 switch (err) {
1716 default:
1717 goto out;
1718 case -NFS4ERR_GRACE:
1719 case -NFS4ERR_DELAY:
1720 nfs4_handle_exception(server, err, &exception);
1721 err = 0;
1722 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001723 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001724out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001725 return err;
1726}
1727
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1729{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001731 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
Trond Myklebust864472e2006-01-03 09:55:15 +01001733 ctx = nfs4_state_find_open_context(state);
1734 if (IS_ERR(ctx))
1735 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001736 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001737 put_nfs_open_context(ctx);
1738 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739}
1740
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001741#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001742static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001743{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001744 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001745 nfs4_stateid *stateid = &state->stateid;
1746 int status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001747
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001748 /* If a state reset has been done, test_stateid is unneeded */
1749 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1750 return;
1751
1752 status = nfs41_test_stateid(server, stateid);
1753 if (status != NFS_OK) {
1754 /* Free the stateid unless the server explicitly
1755 * informs us the stateid is unrecognized. */
1756 if (status != -NFS4ERR_BAD_STATEID)
1757 nfs41_free_stateid(server, stateid);
Bryan Schumaker69388672012-09-26 15:25:52 -04001758 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001759
Bryan Schumaker69388672012-09-26 15:25:52 -04001760 write_seqlock(&state->seqlock);
1761 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1762 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001763 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1764 }
1765}
1766
1767/**
1768 * nfs41_check_open_stateid - possibly free an open stateid
1769 *
1770 * @state: NFSv4 state for an inode
1771 *
1772 * Returns NFS_OK if recovery for this stateid is now finished.
1773 * Otherwise a negative NFS4ERR value is returned.
1774 */
1775static int nfs41_check_open_stateid(struct nfs4_state *state)
1776{
1777 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04001778 nfs4_stateid *stateid = &state->open_stateid;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001779 int status;
1780
1781 /* If a state reset has been done, test_stateid is unneeded */
1782 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1783 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1784 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1785 return -NFS4ERR_BAD_STATEID;
1786
1787 status = nfs41_test_stateid(server, stateid);
1788 if (status != NFS_OK) {
1789 /* Free the stateid unless the server explicitly
1790 * informs us the stateid is unrecognized. */
1791 if (status != -NFS4ERR_BAD_STATEID)
1792 nfs41_free_stateid(server, stateid);
1793
1794 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1795 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1796 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001797 }
1798 return status;
1799}
1800
1801static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1802{
Chuck Levereb64cf92012-07-11 16:30:05 -04001803 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001804
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001805 nfs41_clear_delegation_stateid(state);
1806 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04001807 if (status != NFS_OK)
1808 status = nfs4_open_expired(sp, state);
1809 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001810}
1811#endif
1812
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001814 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1815 * fields corresponding to attributes that were used to store the verifier.
1816 * Make sure we clobber those fields in the later setattr call
1817 */
1818static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1819{
1820 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1821 !(sattr->ia_valid & ATTR_ATIME_SET))
1822 sattr->ia_valid |= ATTR_ATIME;
1823
1824 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1825 !(sattr->ia_valid & ATTR_MTIME_SET))
1826 sattr->ia_valid |= ATTR_MTIME;
1827}
1828
1829/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001830 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 */
Andy Adamson82be4172012-05-23 05:02:35 -04001832static int _nfs4_do_open(struct inode *dir,
1833 struct dentry *dentry,
1834 fmode_t fmode,
1835 int flags,
1836 struct iattr *sattr,
1837 struct rpc_cred *cred,
1838 struct nfs4_state **res,
1839 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840{
1841 struct nfs4_state_owner *sp;
1842 struct nfs4_state *state = NULL;
1843 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001844 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001845 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
1847 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001849 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1850 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1852 goto out_err;
1853 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001854 status = nfs4_recover_expired_lease(server);
1855 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001856 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001857 if (dentry->d_inode != NULL)
1858 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001859 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001860 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001861 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001862 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863
Andy Adamson82be4172012-05-23 05:02:35 -04001864 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
1865 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
1866 if (!opendata->f_attr.mdsthreshold)
1867 goto err_opendata_put;
Trond Myklebust1549210f2012-06-05 09:16:47 -04001868 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04001869 }
Al Viro82a2c1b2011-06-22 18:30:55 -04001870 if (dentry->d_inode != NULL)
1871 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001872
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001873 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001875 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001877 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001878 status = PTR_ERR(state);
1879 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001880 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001881 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001882 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001883
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001884 status = nfs4_opendata_access(cred, opendata, state, fmode);
1885 if (status != 0)
1886 goto err_opendata_put;
1887
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001888 if (opendata->o_arg.open_flags & O_EXCL) {
1889 nfs4_exclusive_attrset(opendata, sattr);
1890
1891 nfs_fattr_init(opendata->o_res.f_attr);
1892 status = nfs4_do_setattr(state->inode, cred,
1893 opendata->o_res.f_attr, sattr,
1894 state);
1895 if (status == 0)
1896 nfs_setattr_update_inode(state->inode, sattr);
1897 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1898 }
Andy Adamson82be4172012-05-23 05:02:35 -04001899
1900 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
1901 *ctx_th = opendata->f_attr.mdsthreshold;
1902 else
1903 kfree(opendata->f_attr.mdsthreshold);
1904 opendata->f_attr.mdsthreshold = NULL;
1905
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001906 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 *res = state;
1909 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001910err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04001911 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001912 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001913err_put_state_owner:
1914 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 *res = NULL;
1917 return status;
1918}
1919
1920
Andy Adamson82be4172012-05-23 05:02:35 -04001921static struct nfs4_state *nfs4_do_open(struct inode *dir,
1922 struct dentry *dentry,
1923 fmode_t fmode,
1924 int flags,
1925 struct iattr *sattr,
1926 struct rpc_cred *cred,
1927 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928{
1929 struct nfs4_exception exception = { };
1930 struct nfs4_state *res;
1931 int status;
1932
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001933 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 do {
Andy Adamson82be4172012-05-23 05:02:35 -04001935 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
1936 &res, ctx_th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 if (status == 0)
1938 break;
1939 /* NOTE: BAD_SEQID means the server and client disagree about the
1940 * book-keeping w.r.t. state-changing operations
1941 * (OPEN/CLOSE/LOCK/LOCKU...)
1942 * It is actually a sign of a bug on the client or on the server.
1943 *
1944 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001945 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 * have unhashed the old state_owner for us, and that we can
1947 * therefore safely retry using a new one. We should still warn
1948 * the user though...
1949 */
1950 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04001951 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001952 " returned a bad sequence-id error!\n",
1953 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 exception.retry = 1;
1955 continue;
1956 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001957 /*
1958 * BAD_STATEID on OPEN means that the server cancelled our
1959 * state before it received the OPEN_CONFIRM.
1960 * Recover by retrying the request as per the discussion
1961 * on Page 181 of RFC3530.
1962 */
1963 if (status == -NFS4ERR_BAD_STATEID) {
1964 exception.retry = 1;
1965 continue;
1966 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001967 if (status == -EAGAIN) {
1968 /* We must have found a delegation */
1969 exception.retry = 1;
1970 continue;
1971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1973 status, &exception));
1974 } while (exception.retry);
1975 return res;
1976}
1977
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001978static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1979 struct nfs_fattr *fattr, struct iattr *sattr,
1980 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001982 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001984 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 .iap = sattr,
1986 .server = server,
1987 .bitmask = server->attr_bitmask,
1988 };
1989 struct nfs_setattrres res = {
1990 .fattr = fattr,
1991 .server = server,
1992 };
1993 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001994 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1995 .rpc_argp = &arg,
1996 .rpc_resp = &res,
1997 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001999 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04002000 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001
Trond Myklebust0e574af2005-10-27 22:12:38 -04002002 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
Trond Myklebust4fc87962012-03-08 17:42:01 -05002004 if (state != NULL) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002005 struct nfs_lockowner lockowner = {
2006 .l_owner = current->files,
2007 .l_pid = current->tgid,
2008 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002009 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002010 &lockowner);
Trond Myklebust4fc87962012-03-08 17:42:01 -05002011 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2012 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002013 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002014 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002015 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
Bryan Schumaker7c513052011-03-24 17:12:24 +00002017 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002018 if (status == 0 && state != NULL)
2019 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002020 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021}
2022
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002023static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2024 struct nfs_fattr *fattr, struct iattr *sattr,
2025 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002027 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002028 struct nfs4_exception exception = {
2029 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002030 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002031 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 int err;
2033 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04002034 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2035 switch (err) {
2036 case -NFS4ERR_OPENMODE:
2037 if (state && !(state->state & FMODE_WRITE)) {
2038 err = -EBADF;
2039 if (sattr->ia_valid & ATTR_OPEN)
2040 err = -EACCES;
2041 goto out;
2042 }
2043 }
2044 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002046out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 return err;
2048}
2049
2050struct nfs4_closedata {
2051 struct inode *inode;
2052 struct nfs4_state *state;
2053 struct nfs_closeargs arg;
2054 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002055 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002056 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002057 bool roc;
2058 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059};
2060
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002061static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002062{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002063 struct nfs4_closedata *calldata = data;
2064 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002065 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002066
Fred Isamanf7e89172011-01-06 11:36:32 +00002067 if (calldata->roc)
2068 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002069 nfs4_put_open_state(calldata->state);
2070 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002071 nfs4_put_state_owner(sp);
Weston Andros Adamson324d0032012-10-30 17:01:39 -04002072 nfs_sb_deactive_async(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002073 kfree(calldata);
2074}
2075
Trond Myklebust88069f72009-12-08 08:33:16 -05002076static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2077 fmode_t fmode)
2078{
2079 spin_lock(&state->owner->so_lock);
2080 if (!(fmode & FMODE_READ))
2081 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2082 if (!(fmode & FMODE_WRITE))
2083 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2084 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2085 spin_unlock(&state->owner->so_lock);
2086}
2087
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002088static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002090 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 struct nfs_server *server = NFS_SERVER(calldata->inode);
2093
Chuck Levera3ca5652012-03-01 17:00:40 -05002094 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002095 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2096 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 /* hmm. we are done with the inode, and in the process of freeing
2098 * the state_owner. we keep this around to process errors
2099 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 switch (task->tk_status) {
2101 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002102 if (calldata->roc)
2103 pnfs_roc_set_barrier(state->inode,
2104 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002105 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002106 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002107 nfs4_close_clear_stateid_flags(state,
2108 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 break;
2110 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002111 case -NFS4ERR_OLD_STATEID:
2112 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002114 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002115 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002117 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2118 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002120 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002121 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002122 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123}
2124
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002125static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002127 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002128 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002129 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002130 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002131
Chuck Levera3ca5652012-03-01 17:00:40 -05002132 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002133 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07002134 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04002135
Trond Myklebust88069f72009-12-08 08:33:16 -05002136 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2137 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002138 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002139 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002140 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002141 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002142 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2143 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2144 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002145 }
2146 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002147 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2148 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2149 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002150 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002151 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002152 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002153
2154 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002155 /* Note: exit _without_ calling nfs4_close_done */
2156 task->tk_action = NULL;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002157 nfs4_sequence_done(task, &calldata->res.seq_res);
Chuck Levera3ca5652012-03-01 17:00:40 -05002158 goto out;
Trond Myklebust95121352005-10-18 14:20:12 -07002159 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002160
Fred Isamanf7e89172011-01-06 11:36:32 +00002161 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002162 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002163 if (calldata->roc &&
Trond Myklebust7fdab062012-09-20 20:15:57 -04002164 pnfs_roc_drain(inode, &calldata->roc_barrier, task))
Chuck Levera3ca5652012-03-01 17:00:40 -05002165 goto out;
Fred Isamanf7e89172011-01-06 11:36:32 +00002166 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002167
Trond Myklebust516a6af2005-10-27 22:12:41 -04002168 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002169 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002170 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002171 &calldata->arg.seq_args,
2172 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002173 task) != 0)
2174 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002175out:
2176 dprintk("%s: done!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177}
2178
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002179static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002180 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002181 .rpc_call_done = nfs4_close_done,
2182 .rpc_release = nfs4_free_closedata,
2183};
2184
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185/*
2186 * It is possible for data to be read/written from a mem-mapped file
2187 * after the sys_close call (which hits the vfs layer as a flush).
2188 * This means that we can't safely call nfsv4 close on a file until
2189 * the inode is cleared. This in turn means that we are not good
2190 * NFSv4 citizens - we do not indicate to the server to update the file's
2191 * share state even when we are done with one of the three share
2192 * stateid's in the inode.
2193 *
2194 * NOTE: Caller must be holding the sp->so_owner semaphore!
2195 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002196int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002198 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002200 struct nfs4_state_owner *sp = state->owner;
2201 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002202 struct rpc_message msg = {
2203 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2204 .rpc_cred = state->owner->so_cred,
2205 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002206 struct rpc_task_setup task_setup_data = {
2207 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002208 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002209 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002210 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002211 .flags = RPC_TASK_ASYNC,
2212 };
Trond Myklebust95121352005-10-18 14:20:12 -07002213 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002215 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002217 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002218 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002219 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002221 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002222 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002224 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002225 if (calldata->arg.seqid == NULL)
2226 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002227 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002228 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002229 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002230 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002231 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002232 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002233 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002234
Trond Myklebust1174dd12010-12-21 10:52:24 -05002235 msg.rpc_argp = &calldata->arg;
2236 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002237 task_setup_data.callback_data = calldata;
2238 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002239 if (IS_ERR(task))
2240 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002241 status = 0;
2242 if (wait)
2243 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002244 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002245 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002246out_free_calldata:
2247 kfree(calldata);
2248out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002249 nfs4_put_open_state(state);
2250 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002251 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252}
2253
Trond Myklebust2b484292010-09-17 10:56:51 -04002254static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002255nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 struct nfs4_state *state;
2258
Trond Myklebust565277f2007-10-15 18:17:53 -04002259 /* Protect against concurrent sillydeletes */
Andy Adamson82be4172012-05-23 05:02:35 -04002260 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2261 ctx->cred, &ctx->mdsthreshold);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002262 if (IS_ERR(state))
2263 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002264 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002265 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266}
2267
Trond Myklebust1185a552009-12-03 15:54:02 -05002268static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002269{
2270 if (ctx->state == NULL)
2271 return;
2272 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002273 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002274 else
Al Viro643168c2011-06-22 18:20:23 -04002275 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002276}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
2278static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2279{
Benny Halevy43652ad2009-04-01 09:21:54 -04002280 struct nfs4_server_caps_arg args = {
2281 .fhandle = fhandle,
2282 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 struct nfs4_server_caps_res res = {};
2284 struct rpc_message msg = {
2285 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002286 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 .rpc_resp = &res,
2288 };
2289 int status;
2290
Bryan Schumaker7c513052011-03-24 17:12:24 +00002291 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 if (status == 0) {
2293 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002294 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2295 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2296 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2297 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2298 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2300 server->caps |= NFS_CAP_ACLS;
2301 if (res.has_links != 0)
2302 server->caps |= NFS_CAP_HARDLINKS;
2303 if (res.has_symlinks != 0)
2304 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002305 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2306 server->caps |= NFS_CAP_FILEID;
2307 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2308 server->caps |= NFS_CAP_MODE;
2309 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2310 server->caps |= NFS_CAP_NLINK;
2311 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2312 server->caps |= NFS_CAP_OWNER;
2313 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2314 server->caps |= NFS_CAP_OWNER_GROUP;
2315 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2316 server->caps |= NFS_CAP_ATIME;
2317 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2318 server->caps |= NFS_CAP_CTIME;
2319 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2320 server->caps |= NFS_CAP_MTIME;
2321
Trond Myklebusta65318b2009-03-11 14:10:28 -04002322 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2323 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2324 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002326 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002328
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 return status;
2330}
2331
Trond Myklebust55a97592006-06-09 09:34:19 -04002332int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333{
2334 struct nfs4_exception exception = { };
2335 int err;
2336 do {
2337 err = nfs4_handle_exception(server,
2338 _nfs4_server_capabilities(server, fhandle),
2339 &exception);
2340 } while (exception.retry);
2341 return err;
2342}
2343
2344static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2345 struct nfs_fsinfo *info)
2346{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 struct nfs4_lookup_root_arg args = {
2348 .bitmask = nfs4_fattr_bitmap,
2349 };
2350 struct nfs4_lookup_res res = {
2351 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002352 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 .fh = fhandle,
2354 };
2355 struct rpc_message msg = {
2356 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2357 .rpc_argp = &args,
2358 .rpc_resp = &res,
2359 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002360
Trond Myklebust0e574af2005-10-27 22:12:38 -04002361 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002362 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363}
2364
2365static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2366 struct nfs_fsinfo *info)
2367{
2368 struct nfs4_exception exception = { };
2369 int err;
2370 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002371 err = _nfs4_lookup_root(server, fhandle, info);
2372 switch (err) {
2373 case 0:
2374 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002375 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002376 default:
2377 err = nfs4_handle_exception(server, err, &exception);
2378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002380out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 return err;
2382}
2383
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002384static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2385 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2386{
2387 struct rpc_auth *auth;
2388 int ret;
2389
2390 auth = rpcauth_create(flavor, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002391 if (IS_ERR(auth)) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002392 ret = -EIO;
2393 goto out;
2394 }
2395 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002396out:
2397 return ret;
2398}
2399
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002400static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002401 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002403 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002404 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002406 len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04002407 if (len < 0)
2408 return len;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002409
2410 for (i = 0; i < len; i++) {
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002411 /* AUTH_UNIX is the default flavor if none was specified,
2412 * thus has already been tried. */
2413 if (flav_array[i] == RPC_AUTH_UNIX)
2414 continue;
2415
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002416 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002417 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002418 continue;
2419 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002420 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002421 /*
2422 * -EACCESS could mean that the user doesn't have correct permissions
2423 * to access the mount. It could also mean that we tried to mount
2424 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2425 * existing mount programs don't handle -EACCES very well so it should
2426 * be mapped to -EPERM instead.
2427 */
2428 if (status == -EACCES)
2429 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002430 return status;
2431}
2432
2433/*
2434 * get the file handle for the "/" directory on the server
2435 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002436int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2437 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002438{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002439 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002440 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002441 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2442 /*
2443 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2444 * by nfs4_map_errors() as this function exits.
2445 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002446 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 if (status == 0)
2448 status = nfs4_server_capabilities(server, fhandle);
2449 if (status == 0)
2450 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002451 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452}
2453
Bryan Schumakerbae36242012-05-10 15:07:31 -04002454static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2455 struct nfs_fsinfo *info)
2456{
2457 int error;
2458 struct nfs_fattr *fattr = info->fattr;
2459
2460 error = nfs4_server_capabilities(server, mntfh);
2461 if (error < 0) {
2462 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2463 return error;
2464 }
2465
2466 error = nfs4_proc_getattr(server, mntfh, fattr);
2467 if (error < 0) {
2468 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2469 return error;
2470 }
2471
2472 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2473 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2474 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2475
2476 return error;
2477}
2478
Manoj Naik6b97fd32006-06-09 09:34:29 -04002479/*
2480 * Get locations and (maybe) other attributes of a referral.
2481 * Note that we'll actually follow the referral later when
2482 * we detect fsid mismatch in inode revalidation
2483 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002484static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2485 const struct qstr *name, struct nfs_fattr *fattr,
2486 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002487{
2488 int status = -ENOMEM;
2489 struct page *page = NULL;
2490 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002491
2492 page = alloc_page(GFP_KERNEL);
2493 if (page == NULL)
2494 goto out;
2495 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2496 if (locations == NULL)
2497 goto out;
2498
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002499 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002500 if (status != 0)
2501 goto out;
2502 /* Make sure server returned a different fsid for the referral */
2503 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002504 dprintk("%s: server did not return a different fsid for"
2505 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002506 status = -EIO;
2507 goto out;
2508 }
Andy Adamson533eb462011-06-13 18:25:56 -04002509 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2510 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002511
Andy Adamson533eb462011-06-13 18:25:56 -04002512 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002513 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002514 memset(fhandle, 0, sizeof(struct nfs_fh));
2515out:
2516 if (page)
2517 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002518 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002519 return status;
2520}
2521
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2523{
2524 struct nfs4_getattr_arg args = {
2525 .fh = fhandle,
2526 .bitmask = server->attr_bitmask,
2527 };
2528 struct nfs4_getattr_res res = {
2529 .fattr = fattr,
2530 .server = server,
2531 };
2532 struct rpc_message msg = {
2533 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2534 .rpc_argp = &args,
2535 .rpc_resp = &res,
2536 };
2537
Trond Myklebust0e574af2005-10-27 22:12:38 -04002538 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002539 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540}
2541
2542static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2543{
2544 struct nfs4_exception exception = { };
2545 int err;
2546 do {
2547 err = nfs4_handle_exception(server,
2548 _nfs4_proc_getattr(server, fhandle, fattr),
2549 &exception);
2550 } while (exception.retry);
2551 return err;
2552}
2553
2554/*
2555 * The file is not closed if it is opened due to the a request to change
2556 * the size of the file. The open call will not be needed once the
2557 * VFS layer lookup-intents are implemented.
2558 *
2559 * Close is called when the inode is destroyed.
2560 * If we haven't opened the file for O_WRONLY, we
2561 * need to in the size_change case to obtain a stateid.
2562 *
2563 * Got race?
2564 * Because OPEN is always done by name in nfsv4, it is
2565 * possible that we opened a different file by the same
2566 * name. We can recognize this race condition, but we
2567 * can't do anything about it besides returning an error.
2568 *
2569 * This will be fixed with VFS changes (lookup-intent).
2570 */
2571static int
2572nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2573 struct iattr *sattr)
2574{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002575 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002576 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002577 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 int status;
2579
Benny Halevy8a1636c2010-07-14 15:43:57 -04002580 if (pnfs_ld_layoutret_on_setattr(inode))
2581 pnfs_return_layout(inode);
2582
Trond Myklebust0e574af2005-10-27 22:12:38 -04002583 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584
Andy Adamson26699402012-05-30 16:12:24 -04002585 /* Deal with open(O_TRUNC) */
2586 if (sattr->ia_valid & ATTR_OPEN)
2587 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2588
2589 /* Optimization: if the end result is no change, don't RPC */
2590 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2591 return 0;
2592
Trond Myklebustd5308382005-11-04 15:33:38 -05002593 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002594 if (sattr->ia_valid & ATTR_FILE) {
2595 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002596
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002597 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002598 if (ctx) {
2599 cred = ctx->cred;
2600 state = ctx->state;
2601 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002602 }
2603
2604 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002605 if (status == 0)
2606 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 return status;
2608}
2609
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002610static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2611 const struct qstr *name, struct nfs_fh *fhandle,
2612 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002613{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002614 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002615 int status;
2616 struct nfs4_lookup_arg args = {
2617 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002618 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002619 .name = name,
2620 };
2621 struct nfs4_lookup_res res = {
2622 .server = server,
2623 .fattr = fattr,
2624 .fh = fhandle,
2625 };
2626 struct rpc_message msg = {
2627 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2628 .rpc_argp = &args,
2629 .rpc_resp = &res,
2630 };
2631
2632 nfs_fattr_init(fattr);
2633
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002635 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 dprintk("NFS reply lookup: %d\n", status);
2637 return status;
2638}
2639
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002640static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002641{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002642 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002643 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002644 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2645 fattr->nlink = 2;
2646}
2647
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002648static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2649 struct qstr *name, struct nfs_fh *fhandle,
2650 struct nfs_fattr *fattr)
2651{
2652 struct nfs4_exception exception = { };
2653 struct rpc_clnt *client = *clnt;
2654 int err;
2655 do {
2656 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2657 switch (err) {
2658 case -NFS4ERR_BADNAME:
2659 err = -ENOENT;
2660 goto out;
2661 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002662 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002663 goto out;
2664 case -NFS4ERR_WRONGSEC:
2665 err = -EPERM;
2666 if (client != *clnt)
2667 goto out;
2668
2669 client = nfs4_create_sec_client(client, dir, name);
2670 if (IS_ERR(client))
2671 return PTR_ERR(client);
2672
2673 exception.retry = 1;
2674 break;
2675 default:
2676 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2677 }
2678 } while (exception.retry);
2679
2680out:
2681 if (err == 0)
2682 *clnt = client;
2683 else if (client != *clnt)
2684 rpc_shutdown_client(client);
2685
2686 return err;
2687}
2688
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002689static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002690 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002692 int status;
2693 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002694
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002695 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2696 if (client != NFS_CLIENT(dir)) {
2697 rpc_shutdown_client(client);
2698 nfs_fixup_secinfo_attributes(fattr);
2699 }
2700 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701}
2702
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002703struct rpc_clnt *
2704nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2705 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2706{
2707 int status;
2708 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2709
2710 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2711 if (status < 0) {
2712 rpc_shutdown_client(client);
2713 return ERR_PTR(status);
2714 }
2715 return client;
2716}
2717
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2719{
Trond Myklebust76b32992007-08-10 17:45:11 -04002720 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 struct nfs4_accessargs args = {
2722 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002723 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002725 struct nfs4_accessres res = {
2726 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002727 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 struct rpc_message msg = {
2729 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2730 .rpc_argp = &args,
2731 .rpc_resp = &res,
2732 .rpc_cred = entry->cred,
2733 };
2734 int mode = entry->mask;
2735 int status;
2736
2737 /*
2738 * Determine which access bits we want to ask for...
2739 */
2740 if (mode & MAY_READ)
2741 args.access |= NFS4_ACCESS_READ;
2742 if (S_ISDIR(inode->i_mode)) {
2743 if (mode & MAY_WRITE)
2744 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2745 if (mode & MAY_EXEC)
2746 args.access |= NFS4_ACCESS_LOOKUP;
2747 } else {
2748 if (mode & MAY_WRITE)
2749 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2750 if (mode & MAY_EXEC)
2751 args.access |= NFS4_ACCESS_EXECUTE;
2752 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002753
2754 res.fattr = nfs_alloc_fattr();
2755 if (res.fattr == NULL)
2756 return -ENOMEM;
2757
Bryan Schumaker7c513052011-03-24 17:12:24 +00002758 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002760 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04002761 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002763 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 return status;
2765}
2766
2767static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2768{
2769 struct nfs4_exception exception = { };
2770 int err;
2771 do {
2772 err = nfs4_handle_exception(NFS_SERVER(inode),
2773 _nfs4_proc_access(inode, entry),
2774 &exception);
2775 } while (exception.retry);
2776 return err;
2777}
2778
2779/*
2780 * TODO: For the time being, we don't try to get any attributes
2781 * along with any of the zero-copy operations READ, READDIR,
2782 * READLINK, WRITE.
2783 *
2784 * In the case of the first three, we want to put the GETATTR
2785 * after the read-type operation -- this is because it is hard
2786 * to predict the length of a GETATTR response in v4, and thus
2787 * align the READ data correctly. This means that the GETATTR
2788 * may end up partially falling into the page cache, and we should
2789 * shift it into the 'tail' of the xdr_buf before processing.
2790 * To do this efficiently, we need to know the total length
2791 * of data received, which doesn't seem to be available outside
2792 * of the RPC layer.
2793 *
2794 * In the case of WRITE, we also want to put the GETATTR after
2795 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04002796 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 *
2798 * Both of these changes to the XDR layer would in fact be quite
2799 * minor, but I decided to leave them for a subsequent patch.
2800 */
2801static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2802 unsigned int pgbase, unsigned int pglen)
2803{
2804 struct nfs4_readlink args = {
2805 .fh = NFS_FH(inode),
2806 .pgbase = pgbase,
2807 .pglen = pglen,
2808 .pages = &page,
2809 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002810 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 struct rpc_message msg = {
2812 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2813 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002814 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 };
2816
Bryan Schumaker7c513052011-03-24 17:12:24 +00002817 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 -07002818}
2819
2820static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2821 unsigned int pgbase, unsigned int pglen)
2822{
2823 struct nfs4_exception exception = { };
2824 int err;
2825 do {
2826 err = nfs4_handle_exception(NFS_SERVER(inode),
2827 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2828 &exception);
2829 } while (exception.retry);
2830 return err;
2831}
2832
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002834 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836static int
2837nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002838 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839{
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002840 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 int status = 0;
2843
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002844 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
2845 if (IS_ERR(ctx))
2846 return PTR_ERR(ctx);
2847
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002848 sattr->ia_mode &= ~current_umask();
Trond Myklebust8626e4a2012-07-16 12:01:42 -04002849 state = nfs4_do_open(dir, dentry, ctx->mode,
2850 flags, sattr, ctx->cred,
2851 &ctx->mdsthreshold);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002852 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 if (IS_ERR(state)) {
2854 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002855 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002857 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002858 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002859 ctx->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860out:
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002861 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 return status;
2863}
2864
2865static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2866{
Trond Myklebust16e42952005-10-27 22:12:44 -04002867 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002868 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07002870 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002872 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002873 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002874 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002876 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2877 .rpc_argp = &args,
2878 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 };
Trond Myklebust778d2812012-04-27 13:48:19 -04002880 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04002881
Bryan Schumaker7c513052011-03-24 17:12:24 +00002882 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04002883 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04002884 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 return status;
2886}
2887
2888static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2889{
2890 struct nfs4_exception exception = { };
2891 int err;
2892 do {
2893 err = nfs4_handle_exception(NFS_SERVER(dir),
2894 _nfs4_proc_remove(dir, name),
2895 &exception);
2896 } while (exception.retry);
2897 return err;
2898}
2899
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002900static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002902 struct nfs_server *server = NFS_SERVER(dir);
2903 struct nfs_removeargs *args = msg->rpc_argp;
2904 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002906 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05002908 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909}
2910
Bryan Schumaker34e137c2012-03-19 14:54:41 -04002911static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
2912{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04002913 nfs4_setup_sequence(NFS_SERVER(data->dir),
2914 &data->args.seq_args,
2915 &data->res.seq_res,
2916 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917}
2918
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002919static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002921 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2922
Trond Myklebust14516c32010-07-31 14:29:06 -04002923 if (!nfs4_sequence_done(task, &res->seq_res))
2924 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002925 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002926 return 0;
2927 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002928 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929}
2930
Jeff Laytond3d41522010-09-17 17:31:57 -04002931static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2932{
2933 struct nfs_server *server = NFS_SERVER(dir);
2934 struct nfs_renameargs *arg = msg->rpc_argp;
2935 struct nfs_renameres *res = msg->rpc_resp;
2936
2937 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04002938 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002939 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04002940}
2941
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04002942static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
2943{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04002944 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
2945 &data->args.seq_args,
2946 &data->res.seq_res,
2947 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04002948}
2949
2950static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2951 struct inode *new_dir)
2952{
2953 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2954
2955 if (!nfs4_sequence_done(task, &res->seq_res))
2956 return 0;
2957 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2958 return 0;
2959
2960 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04002961 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04002962 return 1;
2963}
2964
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2966 struct inode *new_dir, struct qstr *new_name)
2967{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002968 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002969 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 .old_dir = NFS_FH(old_dir),
2971 .new_dir = NFS_FH(new_dir),
2972 .old_name = old_name,
2973 .new_name = new_name,
2974 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002975 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002976 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002977 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 struct rpc_message msg = {
2979 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2980 .rpc_argp = &arg,
2981 .rpc_resp = &res,
2982 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002983 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984
Bryan Schumaker7c513052011-03-24 17:12:24 +00002985 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 if (!status) {
2987 update_changeattr(old_dir, &res.old_cinfo);
2988 update_changeattr(new_dir, &res.new_cinfo);
2989 }
2990 return status;
2991}
2992
2993static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2994 struct inode *new_dir, struct qstr *new_name)
2995{
2996 struct nfs4_exception exception = { };
2997 int err;
2998 do {
2999 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3000 _nfs4_proc_rename(old_dir, old_name,
3001 new_dir, new_name),
3002 &exception);
3003 } while (exception.retry);
3004 return err;
3005}
3006
3007static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3008{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003009 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 struct nfs4_link_arg arg = {
3011 .fh = NFS_FH(inode),
3012 .dir_fh = NFS_FH(dir),
3013 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003014 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003016 struct nfs4_link_res res = {
3017 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003018 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 struct rpc_message msg = {
3020 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3021 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003022 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003024 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025
Trond Myklebust136f2622010-04-16 16:22:49 -04003026 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003027 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003028 goto out;
3029
Bryan Schumaker7c513052011-03-24 17:12:24 +00003030 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003031 if (!status) {
3032 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04003033 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003034 }
Trond Myklebust136f2622010-04-16 16:22:49 -04003035out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003036 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 return status;
3038}
3039
3040static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3041{
3042 struct nfs4_exception exception = { };
3043 int err;
3044 do {
3045 err = nfs4_handle_exception(NFS_SERVER(inode),
3046 _nfs4_proc_link(inode, dir, name),
3047 &exception);
3048 } while (exception.retry);
3049 return err;
3050}
3051
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003052struct nfs4_createdata {
3053 struct rpc_message msg;
3054 struct nfs4_create_arg arg;
3055 struct nfs4_create_res res;
3056 struct nfs_fh fh;
3057 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003058};
3059
3060static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3061 struct qstr *name, struct iattr *sattr, u32 ftype)
3062{
3063 struct nfs4_createdata *data;
3064
3065 data = kzalloc(sizeof(*data), GFP_KERNEL);
3066 if (data != NULL) {
3067 struct nfs_server *server = NFS_SERVER(dir);
3068
3069 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3070 data->msg.rpc_argp = &data->arg;
3071 data->msg.rpc_resp = &data->res;
3072 data->arg.dir_fh = NFS_FH(dir);
3073 data->arg.server = server;
3074 data->arg.name = name;
3075 data->arg.attrs = sattr;
3076 data->arg.ftype = ftype;
3077 data->arg.bitmask = server->attr_bitmask;
3078 data->res.server = server;
3079 data->res.fh = &data->fh;
3080 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003081 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003082 }
3083 return data;
3084}
3085
3086static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3087{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003088 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003089 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003090 if (status == 0) {
3091 update_changeattr(dir, &data->res.dir_cinfo);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003092 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3093 }
3094 return status;
3095}
3096
3097static void nfs4_free_createdata(struct nfs4_createdata *data)
3098{
3099 kfree(data);
3100}
3101
Chuck Lever4f390c12006-08-22 20:06:22 -04003102static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003103 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003105 struct nfs4_createdata *data;
3106 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107
Chuck Lever94a6d752006-08-22 20:06:23 -04003108 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003109 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003110
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003111 status = -ENOMEM;
3112 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3113 if (data == NULL)
3114 goto out;
3115
3116 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3117 data->arg.u.symlink.pages = &page;
3118 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003120 status = nfs4_do_create(dir, dentry, data);
3121
3122 nfs4_free_createdata(data);
3123out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 return status;
3125}
3126
Chuck Lever4f390c12006-08-22 20:06:22 -04003127static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003128 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129{
3130 struct nfs4_exception exception = { };
3131 int err;
3132 do {
3133 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003134 _nfs4_proc_symlink(dir, dentry, page,
3135 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 &exception);
3137 } while (exception.retry);
3138 return err;
3139}
3140
3141static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3142 struct iattr *sattr)
3143{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003144 struct nfs4_createdata *data;
3145 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003147 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3148 if (data == NULL)
3149 goto out;
3150
3151 status = nfs4_do_create(dir, dentry, data);
3152
3153 nfs4_free_createdata(data);
3154out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 return status;
3156}
3157
3158static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3159 struct iattr *sattr)
3160{
3161 struct nfs4_exception exception = { };
3162 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003163
3164 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 do {
3166 err = nfs4_handle_exception(NFS_SERVER(dir),
3167 _nfs4_proc_mkdir(dir, dentry, sattr),
3168 &exception);
3169 } while (exception.retry);
3170 return err;
3171}
3172
3173static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003174 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175{
3176 struct inode *dir = dentry->d_inode;
3177 struct nfs4_readdir_arg args = {
3178 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003179 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 .pgbase = 0,
3181 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003182 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003183 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 };
3185 struct nfs4_readdir_res res;
3186 struct rpc_message msg = {
3187 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3188 .rpc_argp = &args,
3189 .rpc_resp = &res,
3190 .rpc_cred = cred,
3191 };
3192 int status;
3193
Harvey Harrison3110ff82008-05-02 13:42:44 -07003194 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003195 dentry->d_parent->d_name.name,
3196 dentry->d_name.name,
3197 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003198 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003200 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 -05003201 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003202 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003203 status += args.pgbase;
3204 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003205
3206 nfs_invalidate_atime(dir);
3207
Harvey Harrison3110ff82008-05-02 13:42:44 -07003208 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 return status;
3210}
3211
3212static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003213 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214{
3215 struct nfs4_exception exception = { };
3216 int err;
3217 do {
3218 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3219 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003220 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 &exception);
3222 } while (exception.retry);
3223 return err;
3224}
3225
3226static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3227 struct iattr *sattr, dev_t rdev)
3228{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003229 struct nfs4_createdata *data;
3230 int mode = sattr->ia_mode;
3231 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003233 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3234 if (data == NULL)
3235 goto out;
3236
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003238 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003240 data->arg.ftype = NF4BLK;
3241 data->arg.u.device.specdata1 = MAJOR(rdev);
3242 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 }
3244 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003245 data->arg.ftype = NF4CHR;
3246 data->arg.u.device.specdata1 = MAJOR(rdev);
3247 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003248 } else if (!S_ISSOCK(mode)) {
3249 status = -EINVAL;
3250 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003253 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003254out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003255 nfs4_free_createdata(data);
3256out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 return status;
3258}
3259
3260static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3261 struct iattr *sattr, dev_t rdev)
3262{
3263 struct nfs4_exception exception = { };
3264 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003265
3266 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 do {
3268 err = nfs4_handle_exception(NFS_SERVER(dir),
3269 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3270 &exception);
3271 } while (exception.retry);
3272 return err;
3273}
3274
3275static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3276 struct nfs_fsstat *fsstat)
3277{
3278 struct nfs4_statfs_arg args = {
3279 .fh = fhandle,
3280 .bitmask = server->attr_bitmask,
3281 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003282 struct nfs4_statfs_res res = {
3283 .fsstat = fsstat,
3284 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 struct rpc_message msg = {
3286 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3287 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003288 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 };
3290
Trond Myklebust0e574af2005-10-27 22:12:38 -04003291 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003292 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293}
3294
3295static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3296{
3297 struct nfs4_exception exception = { };
3298 int err;
3299 do {
3300 err = nfs4_handle_exception(server,
3301 _nfs4_proc_statfs(server, fhandle, fsstat),
3302 &exception);
3303 } while (exception.retry);
3304 return err;
3305}
3306
3307static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3308 struct nfs_fsinfo *fsinfo)
3309{
3310 struct nfs4_fsinfo_arg args = {
3311 .fh = fhandle,
3312 .bitmask = server->attr_bitmask,
3313 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003314 struct nfs4_fsinfo_res res = {
3315 .fsinfo = fsinfo,
3316 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 struct rpc_message msg = {
3318 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3319 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003320 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 };
3322
Bryan Schumaker7c513052011-03-24 17:12:24 +00003323 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324}
3325
3326static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3327{
3328 struct nfs4_exception exception = { };
3329 int err;
3330
3331 do {
3332 err = nfs4_handle_exception(server,
3333 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3334 &exception);
3335 } while (exception.retry);
3336 return err;
3337}
3338
3339static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3340{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003341 int error;
3342
Trond Myklebust0e574af2005-10-27 22:12:38 -04003343 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003344 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003345 if (error == 0) {
3346 /* block layout checks this! */
3347 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003348 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003349 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003350
3351 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352}
3353
3354static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3355 struct nfs_pathconf *pathconf)
3356{
3357 struct nfs4_pathconf_arg args = {
3358 .fh = fhandle,
3359 .bitmask = server->attr_bitmask,
3360 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003361 struct nfs4_pathconf_res res = {
3362 .pathconf = pathconf,
3363 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 struct rpc_message msg = {
3365 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3366 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003367 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 };
3369
3370 /* None of the pathconf attributes are mandatory to implement */
3371 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3372 memset(pathconf, 0, sizeof(*pathconf));
3373 return 0;
3374 }
3375
Trond Myklebust0e574af2005-10-27 22:12:38 -04003376 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003377 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378}
3379
3380static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3381 struct nfs_pathconf *pathconf)
3382{
3383 struct nfs4_exception exception = { };
3384 int err;
3385
3386 do {
3387 err = nfs4_handle_exception(server,
3388 _nfs4_proc_pathconf(server, fhandle, pathconf),
3389 &exception);
3390 } while (exception.retry);
3391 return err;
3392}
3393
Benny Halevyd20581a2011-05-22 19:52:03 +03003394void __nfs4_read_done_cb(struct nfs_read_data *data)
3395{
Fred Isamancd841602012-04-20 14:47:44 -04003396 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003397}
3398
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003399static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400{
Fred Isamancd841602012-04-20 14:47:44 -04003401 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003403 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003404 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003405 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003407
Benny Halevyd20581a2011-05-22 19:52:03 +03003408 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003410 renew_lease(server, data->timestamp);
3411 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412}
3413
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003414static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3415{
3416
3417 dprintk("--> %s\n", __func__);
3418
3419 if (!nfs4_sequence_done(task, &data->res.seq_res))
3420 return -EAGAIN;
3421
Benny Halevyd20581a2011-05-22 19:52:03 +03003422 return data->read_done_cb ? data->read_done_cb(task, data) :
3423 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003424}
3425
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003426static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003429 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003430 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003431 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432}
3433
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003434static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3435{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003436 nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3437 &data->args.seq_args,
3438 &data->res.seq_res,
3439 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440}
3441
Fred Isamanb029bc92011-03-03 15:13:42 +00003442static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443{
Fred Isamancd841602012-04-20 14:47:44 -04003444 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003446 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003447 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003448 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003450 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003452 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003453 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003454 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455}
3456
Fred Isamanb029bc92011-03-03 15:13:42 +00003457static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3458{
3459 if (!nfs4_sequence_done(task, &data->res.seq_res))
3460 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003461 return data->write_done_cb ? data->write_done_cb(task, data) :
3462 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003463}
3464
Trond Myklebust5a37f852012-04-28 14:55:16 -04003465static
3466bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003467{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003468 const struct nfs_pgio_header *hdr = data->header;
3469
3470 /* Don't request attributes for pNFS or O_DIRECT writes */
3471 if (data->ds_clp != NULL || hdr->dreq != NULL)
3472 return false;
3473 /* Otherwise, request attributes if and only if we don't hold
3474 * a delegation
3475 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04003476 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003477}
Fred Isamana69aef12011-03-03 15:13:47 +00003478
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003479static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480{
Fred Isamancd841602012-04-20 14:47:44 -04003481 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003482
Trond Myklebust5a37f852012-04-28 14:55:16 -04003483 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003484 data->args.bitmask = NULL;
3485 data->res.fattr = NULL;
3486 } else
3487 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003488
Fred Isamanb029bc92011-03-03 15:13:42 +00003489 if (!data->write_done_cb)
3490 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003491 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 data->timestamp = jiffies;
3493
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003494 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003495 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496}
3497
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003498static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3499{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003500 nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3501 &data->args.seq_args,
3502 &data->res.seq_res,
3503 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504}
3505
Fred Isaman0b7c0152012-04-20 14:47:39 -04003506static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3507{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003508 nfs4_setup_sequence(NFS_SERVER(data->inode),
3509 &data->args.seq_args,
3510 &data->res.seq_res,
3511 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512}
3513
Fred Isaman0b7c0152012-04-20 14:47:39 -04003514static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003517
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003518 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003519 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003520 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003522 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523}
3524
Fred Isaman0b7c0152012-04-20 14:47:39 -04003525static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003526{
3527 if (!nfs4_sequence_done(task, &data->res.seq_res))
3528 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003529 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003530}
3531
Fred Isaman0b7c0152012-04-20 14:47:39 -04003532static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003534 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003535
Fred Isaman0b7c0152012-04-20 14:47:39 -04003536 if (data->commit_done_cb == NULL)
3537 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003538 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003539 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003540 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541}
3542
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003543struct nfs4_renewdata {
3544 struct nfs_client *client;
3545 unsigned long timestamp;
3546};
3547
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548/*
3549 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3550 * standalone procedure for queueing an asynchronous RENEW.
3551 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003552static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003553{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003554 struct nfs4_renewdata *data = calldata;
3555 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003556
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003557 if (atomic_read(&clp->cl_count) > 1)
3558 nfs4_schedule_state_renewal(clp);
3559 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003560 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003561}
3562
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003563static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003565 struct nfs4_renewdata *data = calldata;
3566 struct nfs_client *clp = data->client;
3567 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568
3569 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003570 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003571 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3572 return;
3573 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003574 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003575 return;
3576 }
3577 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003579 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580}
3581
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003582static const struct rpc_call_ops nfs4_renew_ops = {
3583 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003584 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003585};
3586
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003587static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588{
3589 struct rpc_message msg = {
3590 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3591 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003592 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003594 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003596 if (renew_flags == 0)
3597 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003598 if (!atomic_inc_not_zero(&clp->cl_count))
3599 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003600 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003601 if (data == NULL)
3602 return -ENOMEM;
3603 data->client = clp;
3604 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003606 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607}
3608
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003609static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610{
3611 struct rpc_message msg = {
3612 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3613 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003614 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615 };
3616 unsigned long now = jiffies;
3617 int status;
3618
3619 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3620 if (status < 0)
3621 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003622 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 return 0;
3624}
3625
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003626static inline int nfs4_server_supports_acls(struct nfs_server *server)
3627{
3628 return (server->caps & NFS_CAP_ACLS)
3629 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3630 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3631}
3632
Trond Myklebust21f498c2012-08-24 10:59:25 -04003633/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3634 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003635 * the stack.
3636 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04003637#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003638
Neil Hormane9e3d722011-03-04 19:26:03 -05003639static int buf_to_pages_noslab(const void *buf, size_t buflen,
3640 struct page **pages, unsigned int *pgbase)
3641{
3642 struct page *newpage, **spages;
3643 int rc = 0;
3644 size_t len;
3645 spages = pages;
3646
3647 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04003648 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003649 newpage = alloc_page(GFP_KERNEL);
3650
3651 if (newpage == NULL)
3652 goto unwind;
3653 memcpy(page_address(newpage), buf, len);
3654 buf += len;
3655 buflen -= len;
3656 *pages++ = newpage;
3657 rc++;
3658 } while (buflen != 0);
3659
3660 return rc;
3661
3662unwind:
3663 for(; rc > 0; rc--)
3664 __free_page(spages[rc-1]);
3665 return -ENOMEM;
3666}
3667
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003668struct nfs4_cached_acl {
3669 int cached;
3670 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003671 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003672};
3673
3674static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003675{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003676 struct nfs_inode *nfsi = NFS_I(inode);
3677
3678 spin_lock(&inode->i_lock);
3679 kfree(nfsi->nfs4_acl);
3680 nfsi->nfs4_acl = acl;
3681 spin_unlock(&inode->i_lock);
3682}
3683
3684static void nfs4_zap_acl_attr(struct inode *inode)
3685{
3686 nfs4_set_cached_acl(inode, NULL);
3687}
3688
3689static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3690{
3691 struct nfs_inode *nfsi = NFS_I(inode);
3692 struct nfs4_cached_acl *acl;
3693 int ret = -ENOENT;
3694
3695 spin_lock(&inode->i_lock);
3696 acl = nfsi->nfs4_acl;
3697 if (acl == NULL)
3698 goto out;
3699 if (buf == NULL) /* user is just asking for length */
3700 goto out_len;
3701 if (acl->cached == 0)
3702 goto out;
3703 ret = -ERANGE; /* see getxattr(2) man page */
3704 if (acl->len > buflen)
3705 goto out;
3706 memcpy(buf, acl->data, acl->len);
3707out_len:
3708 ret = acl->len;
3709out:
3710 spin_unlock(&inode->i_lock);
3711 return ret;
3712}
3713
Sachin Prabhu5794d212012-04-17 14:36:40 +01003714static 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 +00003715{
3716 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003717 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003718
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003719 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003720 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003721 if (acl == NULL)
3722 goto out;
3723 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003724 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003725 } else {
3726 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3727 if (acl == NULL)
3728 goto out;
3729 acl->cached = 0;
3730 }
3731 acl->len = acl_len;
3732out:
3733 nfs4_set_cached_acl(inode, acl);
3734}
3735
Andy Adamsonbf118a32011-12-07 11:55:27 -05003736/*
3737 * The getxattr API returns the required buffer length when called with a
3738 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3739 * the required buf. On a NULL buf, we send a page of data to the server
3740 * guessing that the ACL request can be serviced by a page. If so, we cache
3741 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3742 * the cache. If not so, we throw away the page, and cache the required
3743 * length. The next getxattr call will then produce another round trip to
3744 * the server, this time with the input buf of the required size.
3745 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003746static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003747{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003748 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003749 struct nfs_getaclargs args = {
3750 .fh = NFS_FH(inode),
3751 .acl_pages = pages,
3752 .acl_len = buflen,
3753 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003754 struct nfs_getaclres res = {
3755 .acl_len = buflen,
3756 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003757 struct rpc_message msg = {
3758 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3759 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003760 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003761 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003762 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3763 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003764
Andy Adamsonbf118a32011-12-07 11:55:27 -05003765 /* As long as we're doing a round trip to the server anyway,
3766 * let's be prepared for a page of acl data. */
3767 if (npages == 0)
3768 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003769 if (npages > ARRAY_SIZE(pages))
3770 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003771
Andy Adamsonbf118a32011-12-07 11:55:27 -05003772 for (i = 0; i < npages; i++) {
3773 pages[i] = alloc_page(GFP_KERNEL);
3774 if (!pages[i])
3775 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003776 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01003777
3778 /* for decoding across pages */
3779 res.acl_scratch = alloc_page(GFP_KERNEL);
3780 if (!res.acl_scratch)
3781 goto out_free;
3782
Andy Adamsonbf118a32011-12-07 11:55:27 -05003783 args.acl_len = npages * PAGE_SIZE;
3784 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003785
Peng Taode040be2012-01-10 22:42:47 +08003786 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003787 __func__, buf, buflen, npages, args.acl_len);
3788 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3789 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003790 if (ret)
3791 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003792
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003793 /* Handle the case where the passed-in buffer is too short */
3794 if (res.acl_flags & NFS4_ACL_TRUNC) {
3795 /* Did the user only issue a request for the acl length? */
3796 if (buf == NULL)
3797 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003798 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003799 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003800 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003801 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01003802 if (buf) {
3803 if (res.acl_len > buflen) {
3804 ret = -ERANGE;
3805 goto out_free;
3806 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003807 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01003808 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003809out_ok:
3810 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003811out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003812 for (i = 0; i < npages; i++)
3813 if (pages[i])
3814 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05003815 if (res.acl_scratch)
3816 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003817 return ret;
3818}
3819
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003820static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3821{
3822 struct nfs4_exception exception = { };
3823 ssize_t ret;
3824 do {
3825 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3826 if (ret >= 0)
3827 break;
3828 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3829 } while (exception.retry);
3830 return ret;
3831}
3832
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003833static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3834{
3835 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003836 int ret;
3837
3838 if (!nfs4_server_supports_acls(server))
3839 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003840 ret = nfs_revalidate_inode(server, inode);
3841 if (ret < 0)
3842 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003843 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3844 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003845 ret = nfs4_read_cached_acl(inode, buf, buflen);
3846 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003847 /* -ENOENT is returned if there is no ACL or if there is an ACL
3848 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003849 return ret;
3850 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003851}
3852
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003853static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003854{
3855 struct nfs_server *server = NFS_SERVER(inode);
3856 struct page *pages[NFS4ACL_MAXPAGES];
3857 struct nfs_setaclargs arg = {
3858 .fh = NFS_FH(inode),
3859 .acl_pages = pages,
3860 .acl_len = buflen,
3861 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003862 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003863 struct rpc_message msg = {
3864 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3865 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003866 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003867 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003868 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05003869 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003870
3871 if (!nfs4_server_supports_acls(server))
3872 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003873 if (npages > ARRAY_SIZE(pages))
3874 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05003875 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3876 if (i < 0)
3877 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04003878 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003879 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003880
3881 /*
3882 * Free each page after tx, so the only ref left is
3883 * held by the network stack
3884 */
3885 for (; i > 0; i--)
3886 put_page(pages[i-1]);
3887
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003888 /*
3889 * Acl update can result in inode attribute update.
3890 * so mark the attribute cache invalid.
3891 */
3892 spin_lock(&inode->i_lock);
3893 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3894 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003895 nfs_access_zap_cache(inode);
3896 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003897 return ret;
3898}
3899
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003900static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3901{
3902 struct nfs4_exception exception = { };
3903 int err;
3904 do {
3905 err = nfs4_handle_exception(NFS_SERVER(inode),
3906 __nfs4_proc_set_acl(inode, buf, buflen),
3907 &exception);
3908 } while (exception.retry);
3909 return err;
3910}
3911
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003913nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003915 struct nfs_client *clp = server->nfs_client;
3916
3917 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918 return 0;
3919 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05003920 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003921 case -NFS4ERR_ADMIN_REVOKED:
3922 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04003923 if (state == NULL)
3924 break;
3925 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003926 case -NFS4ERR_OPENMODE:
3927 if (state == NULL)
3928 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003929 nfs4_schedule_stateid_recovery(server, state);
3930 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003931 case -NFS4ERR_EXPIRED:
3932 if (state != NULL)
3933 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003935 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003936 nfs4_schedule_lease_recovery(clp);
3937 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003938#if defined(CONFIG_NFS_V4_1)
3939 case -NFS4ERR_BADSESSION:
3940 case -NFS4ERR_BADSLOT:
3941 case -NFS4ERR_BAD_HIGH_SLOT:
3942 case -NFS4ERR_DEADSESSION:
3943 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3944 case -NFS4ERR_SEQ_FALSE_RETRY:
3945 case -NFS4ERR_SEQ_MISORDERED:
3946 dprintk("%s ERROR %d, Reset session\n", __func__,
3947 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04003948 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04003949 task->tk_status = 0;
3950 return -EAGAIN;
3951#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003953 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003954 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3956 task->tk_status = 0;
3957 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04003958 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 case -NFS4ERR_OLD_STATEID:
3960 task->tk_status = 0;
3961 return -EAGAIN;
3962 }
3963 task->tk_status = nfs4_map_errors(task->tk_status);
3964 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003965wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003966 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003967 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3968 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3969 task->tk_status = 0;
3970 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971}
3972
Chuck Leverf0920752012-05-21 22:45:41 -04003973static void nfs4_init_boot_verifier(const struct nfs_client *clp,
3974 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05003975{
3976 __be32 verf[2];
3977
Chuck Lever2c820d92012-05-21 22:45:33 -04003978 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
3979 /* An impossible timestamp guarantees this value
3980 * will never match a generated boot time. */
3981 verf[0] = 0;
3982 verf[1] = (__be32)(NSEC_PER_SEC + 1);
3983 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04003984 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
3985 verf[0] = (__be32)nn->boot_time.tv_sec;
3986 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04003987 }
Chuck Levercd937102012-03-02 17:14:31 -05003988 memcpy(bootverf->data, verf, sizeof(bootverf->data));
3989}
3990
Chuck Levere984a552012-09-14 17:24:21 -04003991static unsigned int
3992nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
3993 char *buf, size_t len)
3994{
3995 unsigned int result;
3996
3997 rcu_read_lock();
3998 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
3999 clp->cl_ipaddr,
4000 rpc_peeraddr2str(clp->cl_rpcclient,
4001 RPC_DISPLAY_ADDR),
4002 rpc_peeraddr2str(clp->cl_rpcclient,
4003 RPC_DISPLAY_PROTO));
4004 rcu_read_unlock();
4005 return result;
4006}
4007
4008static unsigned int
4009nfs4_init_uniform_client_string(const struct nfs_client *clp,
4010 char *buf, size_t len)
4011{
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004012 char *nodename = clp->cl_rpcclient->cl_nodename;
4013
4014 if (nfs4_client_id_uniquifier[0] != '\0')
4015 nodename = nfs4_client_id_uniquifier;
Chuck Levere984a552012-09-14 17:24:21 -04004016 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4017 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004018 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004019}
4020
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004021/**
4022 * nfs4_proc_setclientid - Negotiate client ID
4023 * @clp: state data structure
4024 * @program: RPC program for NFSv4 callback service
4025 * @port: IP port number for NFS4 callback service
4026 * @cred: RPC credential to use for this call
4027 * @res: where to place the result
4028 *
4029 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4030 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004031int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4032 unsigned short port, struct rpc_cred *cred,
4033 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034{
4035 nfs4_verifier sc_verifier;
4036 struct nfs4_setclientid setclientid = {
4037 .sc_verifier = &sc_verifier,
4038 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004039 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 };
4041 struct rpc_message msg = {
4042 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4043 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004044 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004045 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004047 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048
Chuck Leverde734832012-07-11 16:30:50 -04004049 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004050 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004051 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4052 setclientid.sc_name_len =
4053 nfs4_init_uniform_client_string(clp,
4054 setclientid.sc_name,
4055 sizeof(setclientid.sc_name));
4056 else
4057 setclientid.sc_name_len =
4058 nfs4_init_nonuniform_client_string(clp,
4059 setclientid.sc_name,
4060 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004061 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004062 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004063 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004064 sizeof(setclientid.sc_netid),
4065 rpc_peeraddr2str(clp->cl_rpcclient,
4066 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004067 rcu_read_unlock();
4068 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004069 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070 clp->cl_ipaddr, port >> 8, port & 255);
4071
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004072 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4073 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4074 setclientid.sc_name_len, setclientid.sc_name);
4075 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4076 dprintk("NFS reply setclientid: %d\n", status);
4077 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078}
4079
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004080/**
4081 * nfs4_proc_setclientid_confirm - Confirm client ID
4082 * @clp: state data structure
4083 * @res: result of a previous SETCLIENTID
4084 * @cred: RPC credential to use for this call
4085 *
4086 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4087 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004088int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004089 struct nfs4_setclientid_res *arg,
4090 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091{
4092 struct nfs_fsinfo fsinfo;
4093 struct rpc_message msg = {
4094 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004095 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004097 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098 };
4099 unsigned long now;
4100 int status;
4101
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004102 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4103 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4104 clp->cl_clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004106 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 if (status == 0) {
4108 spin_lock(&clp->cl_lock);
4109 clp->cl_lease_time = fsinfo.lease_time * HZ;
4110 clp->cl_last_renewal = now;
4111 spin_unlock(&clp->cl_lock);
4112 }
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004113 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 return status;
4115}
4116
Trond Myklebustfe650402006-01-03 09:55:18 +01004117struct nfs4_delegreturndata {
4118 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004119 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004120 struct nfs_fh fh;
4121 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004122 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004123 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004124 int rpc_status;
4125};
4126
Trond Myklebustfe650402006-01-03 09:55:18 +01004127static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4128{
4129 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004130
Trond Myklebust14516c32010-07-31 14:29:06 -04004131 if (!nfs4_sequence_done(task, &data->res.seq_res))
4132 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004133
Ricardo Labiaga79708862009-12-07 09:23:21 -05004134 switch (task->tk_status) {
4135 case -NFS4ERR_STALE_STATEID:
4136 case -NFS4ERR_EXPIRED:
4137 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004138 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004139 break;
4140 default:
4141 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4142 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004143 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004144 return;
4145 }
4146 }
4147 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004148}
4149
4150static void nfs4_delegreturn_release(void *calldata)
4151{
Trond Myklebustfe650402006-01-03 09:55:18 +01004152 kfree(calldata);
4153}
4154
Andy Adamson938e1012009-04-01 09:22:28 -04004155#if defined(CONFIG_NFS_V4_1)
4156static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4157{
4158 struct nfs4_delegreturndata *d_data;
4159
4160 d_data = (struct nfs4_delegreturndata *)data;
4161
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004162 nfs4_setup_sequence(d_data->res.server,
4163 &d_data->args.seq_args,
4164 &d_data->res.seq_res,
4165 task);
Andy Adamson938e1012009-04-01 09:22:28 -04004166}
4167#endif /* CONFIG_NFS_V4_1 */
4168
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004169static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004170#if defined(CONFIG_NFS_V4_1)
4171 .rpc_call_prepare = nfs4_delegreturn_prepare,
4172#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004173 .rpc_call_done = nfs4_delegreturn_done,
4174 .rpc_release = nfs4_delegreturn_release,
4175};
4176
Trond Myklebuste6f81072008-01-24 18:14:34 -05004177static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004178{
4179 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004180 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004181 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004182 struct rpc_message msg = {
4183 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4184 .rpc_cred = cred,
4185 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004186 struct rpc_task_setup task_setup_data = {
4187 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004188 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004189 .callback_ops = &nfs4_delegreturn_ops,
4190 .flags = RPC_TASK_ASYNC,
4191 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004192 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004193
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004194 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004195 if (data == NULL)
4196 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004197 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004198 data->args.fhandle = &data->fh;
4199 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004200 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004201 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004202 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004203 data->res.fattr = &data->fattr;
4204 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004205 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004206 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004207 data->rpc_status = 0;
4208
Trond Myklebustc970aa82007-07-14 15:39:59 -04004209 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004210 msg.rpc_argp = &data->args;
4211 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004212 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004213 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004214 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004215 if (!issync)
4216 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004217 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004218 if (status != 0)
4219 goto out;
4220 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004221 if (status == 0)
4222 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4223 else
4224 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004225out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004226 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004227 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228}
4229
Trond Myklebuste6f81072008-01-24 18:14:34 -05004230int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231{
4232 struct nfs_server *server = NFS_SERVER(inode);
4233 struct nfs4_exception exception = { };
4234 int err;
4235 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004236 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237 switch (err) {
4238 case -NFS4ERR_STALE_STATEID:
4239 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240 case 0:
4241 return 0;
4242 }
4243 err = nfs4_handle_exception(server, err, &exception);
4244 } while (exception.retry);
4245 return err;
4246}
4247
4248#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4249#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4250
4251/*
4252 * sleep, with exponential backoff, and retry the LOCK operation.
4253 */
4254static unsigned long
4255nfs4_set_lock_task_retry(unsigned long timeout)
4256{
Jeff Laytond3103102011-12-01 22:44:39 +01004257 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258 timeout <<= 1;
4259 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4260 return NFS4_LOCK_MAXTIMEOUT;
4261 return timeout;
4262}
4263
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4265{
4266 struct inode *inode = state->inode;
4267 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004268 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004269 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004271 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004273 struct nfs_lockt_res res = {
4274 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 };
4276 struct rpc_message msg = {
4277 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4278 .rpc_argp = &arg,
4279 .rpc_resp = &res,
4280 .rpc_cred = state->owner->so_cred,
4281 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282 struct nfs4_lock_state *lsp;
4283 int status;
4284
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004285 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004286 status = nfs4_set_lock_state(state, request);
4287 if (status != 0)
4288 goto out;
4289 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004290 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004291 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004292 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004293 switch (status) {
4294 case 0:
4295 request->fl_type = F_UNLCK;
4296 break;
4297 case -NFS4ERR_DENIED:
4298 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004300 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004301out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 return status;
4303}
4304
4305static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4306{
4307 struct nfs4_exception exception = { };
4308 int err;
4309
4310 do {
4311 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4312 _nfs4_proc_getlk(state, cmd, request),
4313 &exception);
4314 } while (exception.retry);
4315 return err;
4316}
4317
4318static int do_vfs_lock(struct file *file, struct file_lock *fl)
4319{
4320 int res = 0;
4321 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4322 case FL_POSIX:
4323 res = posix_lock_file_wait(file, fl);
4324 break;
4325 case FL_FLOCK:
4326 res = flock_lock_file_wait(file, fl);
4327 break;
4328 default:
4329 BUG();
4330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 return res;
4332}
4333
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004334struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004335 struct nfs_locku_args arg;
4336 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004337 struct nfs4_lock_state *lsp;
4338 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004339 struct file_lock fl;
4340 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004341 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004342};
4343
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004344static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4345 struct nfs_open_context *ctx,
4346 struct nfs4_lock_state *lsp,
4347 struct nfs_seqid *seqid)
4348{
4349 struct nfs4_unlockdata *p;
4350 struct inode *inode = lsp->ls_state->inode;
4351
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004352 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004353 if (p == NULL)
4354 return NULL;
4355 p->arg.fh = NFS_FH(inode);
4356 p->arg.fl = &p->fl;
4357 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004358 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004359 p->arg.stateid = &lsp->ls_stateid;
4360 p->lsp = lsp;
4361 atomic_inc(&lsp->ls_count);
4362 /* Ensure we don't close file until we're done freeing locks! */
4363 p->ctx = get_nfs_open_context(ctx);
4364 memcpy(&p->fl, fl, sizeof(p->fl));
4365 p->server = NFS_SERVER(inode);
4366 return p;
4367}
4368
Trond Myklebust06f814a2006-01-03 09:55:07 +01004369static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004370{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004371 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004372 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004373 nfs4_put_lock_state(calldata->lsp);
4374 put_nfs_open_context(calldata->ctx);
4375 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004376}
4377
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004378static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004379{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004380 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004381
Trond Myklebust14516c32010-07-31 14:29:06 -04004382 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4383 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004384 switch (task->tk_status) {
4385 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004386 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4387 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004388 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004389 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004390 case -NFS4ERR_BAD_STATEID:
4391 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004392 case -NFS4ERR_STALE_STATEID:
4393 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004394 break;
4395 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004396 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004397 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004398 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04004399 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004400}
4401
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004402static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004403{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004404 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004406 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004407 return;
Trond Myklebust795a88c2012-09-10 13:26:49 -04004408 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004409 /* Note: exit _without_ running nfs4_locku_done */
4410 task->tk_action = NULL;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05004411 nfs4_sequence_done(task, &calldata->res.seq_res);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004412 return;
4413 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004414 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004415 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004416 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004417 &calldata->res.seq_res,
4418 task) != 0)
4419 nfs_release_seqid(calldata->arg.seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420}
4421
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004422static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004423 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004424 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004425 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004426};
4427
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004428static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4429 struct nfs_open_context *ctx,
4430 struct nfs4_lock_state *lsp,
4431 struct nfs_seqid *seqid)
4432{
4433 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004434 struct rpc_message msg = {
4435 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4436 .rpc_cred = ctx->cred,
4437 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004438 struct rpc_task_setup task_setup_data = {
4439 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004440 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004441 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004442 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004443 .flags = RPC_TASK_ASYNC,
4444 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004445
Frank Filz137d6ac2007-07-09 15:32:29 -07004446 /* Ensure this is an unlock - when canceling a lock, the
4447 * canceled lock is passed in, and it won't be an unlock.
4448 */
4449 fl->fl_type = F_UNLCK;
4450
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004451 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4452 if (data == NULL) {
4453 nfs_free_seqid(seqid);
4454 return ERR_PTR(-ENOMEM);
4455 }
4456
Trond Myklebust9d12b212012-01-17 22:04:25 -05004457 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004458 msg.rpc_argp = &data->arg;
4459 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004460 task_setup_data.callback_data = data;
4461 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004462}
4463
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4465{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004466 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004467 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004468 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004469 struct rpc_task *task;
4470 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004471 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472
Trond Myklebust9b073572006-06-29 16:38:34 -04004473 status = nfs4_set_lock_state(state, request);
4474 /* Unlock _before_ we do the RPC call */
4475 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004476 down_read(&nfsi->rwsem);
4477 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4478 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004479 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004480 }
4481 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004482 if (status != 0)
4483 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004484 /* Is this a delegated lock? */
4485 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004486 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004487 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004488 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004489 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004490 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004491 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004492 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004493 status = PTR_ERR(task);
4494 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004495 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004496 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004497 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004498out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004499 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004500 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501}
4502
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004503struct nfs4_lockdata {
4504 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004505 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004506 struct nfs4_lock_state *lsp;
4507 struct nfs_open_context *ctx;
4508 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004509 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004510 int rpc_status;
4511 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004512 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004513};
4514
4515static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004516 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4517 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004518{
4519 struct nfs4_lockdata *p;
4520 struct inode *inode = lsp->ls_state->inode;
4521 struct nfs_server *server = NFS_SERVER(inode);
4522
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004523 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004524 if (p == NULL)
4525 return NULL;
4526
4527 p->arg.fh = NFS_FH(inode);
4528 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004529 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004530 if (p->arg.open_seqid == NULL)
4531 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004532 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004533 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004534 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004535 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004536 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004537 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004538 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004539 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004540 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004541 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004542 atomic_inc(&lsp->ls_count);
4543 p->ctx = get_nfs_open_context(ctx);
4544 memcpy(&p->fl, fl, sizeof(p->fl));
4545 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004546out_free_seqid:
4547 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004548out_free:
4549 kfree(p);
4550 return NULL;
4551}
4552
4553static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4554{
4555 struct nfs4_lockdata *data = calldata;
4556 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557
Harvey Harrison3110ff82008-05-02 13:42:44 -07004558 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004559 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4560 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004561 /* Do we need to do an open_to_lock_owner? */
4562 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004563 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004564 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004565 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004566 data->arg.open_stateid = &state->stateid;
4567 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004568 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004569 } else
4570 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004571 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004572 if (nfs4_setup_sequence(data->server,
4573 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004574 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004575 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04004576 return;
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004577 nfs_release_seqid(data->arg.open_seqid);
4578out_release_lock_seqid:
4579 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004580 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004581}
4582
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004583static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4584{
4585 struct nfs4_lockdata *data = calldata;
4586
Harvey Harrison3110ff82008-05-02 13:42:44 -07004587 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004588
Trond Myklebust14516c32010-07-31 14:29:06 -04004589 if (!nfs4_sequence_done(task, &data->res.seq_res))
4590 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004591
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004592 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004593 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004594 if (data->rpc_status == 0)
4595 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4596 else
4597 goto out;
4598 }
4599 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004600 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004601 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04004602 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004603 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004604out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004605 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004606}
4607
4608static void nfs4_lock_release(void *calldata)
4609{
4610 struct nfs4_lockdata *data = calldata;
4611
Harvey Harrison3110ff82008-05-02 13:42:44 -07004612 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004613 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004614 if (data->cancelled != 0) {
4615 struct rpc_task *task;
4616 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4617 data->arg.lock_seqid);
4618 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004619 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004620 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004621 } else
4622 nfs_free_seqid(data->arg.lock_seqid);
4623 nfs4_put_lock_state(data->lsp);
4624 put_nfs_open_context(data->ctx);
4625 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004626 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004627}
4628
4629static const struct rpc_call_ops nfs4_lock_ops = {
4630 .rpc_call_prepare = nfs4_lock_prepare,
4631 .rpc_call_done = nfs4_lock_done,
4632 .rpc_release = nfs4_lock_release,
4633};
4634
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004635static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4636{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004637 switch (error) {
4638 case -NFS4ERR_ADMIN_REVOKED:
4639 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004640 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004641 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04004642 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004643 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004644 break;
4645 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004646 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004647 case -NFS4ERR_EXPIRED:
4648 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004649 };
4650}
4651
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004652static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004653{
4654 struct nfs4_lockdata *data;
4655 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004656 struct rpc_message msg = {
4657 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4658 .rpc_cred = state->owner->so_cred,
4659 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004660 struct rpc_task_setup task_setup_data = {
4661 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004662 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004663 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004664 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004665 .flags = RPC_TASK_ASYNC,
4666 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004667 int ret;
4668
Harvey Harrison3110ff82008-05-02 13:42:44 -07004669 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004670 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004671 fl->fl_u.nfs4_fl.owner,
4672 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004673 if (data == NULL)
4674 return -ENOMEM;
4675 if (IS_SETLKW(cmd))
4676 data->arg.block = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004677 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004678 msg.rpc_argp = &data->arg;
4679 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004680 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004681 if (recovery_type > NFS_LOCK_NEW) {
4682 if (recovery_type == NFS_LOCK_RECLAIM)
4683 data->arg.reclaim = NFS_LOCK_RECLAIM;
4684 nfs4_set_sequence_privileged(&data->arg.seq_args);
4685 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04004686 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004687 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004688 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004689 ret = nfs4_wait_for_completion_rpc_task(task);
4690 if (ret == 0) {
4691 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004692 if (ret)
4693 nfs4_handle_setlk_error(data->server, data->lsp,
4694 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004695 } else
4696 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004697 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004698 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004699 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700}
4701
4702static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4703{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004704 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004705 struct nfs4_exception exception = {
4706 .inode = state->inode,
4707 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004708 int err;
4709
4710 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004711 /* Cache the lock if possible... */
4712 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4713 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004714 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004715 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004716 break;
4717 nfs4_handle_exception(server, err, &exception);
4718 } while (exception.retry);
4719 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720}
4721
4722static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4723{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004724 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004725 struct nfs4_exception exception = {
4726 .inode = state->inode,
4727 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004728 int err;
4729
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004730 err = nfs4_set_lock_state(state, request);
4731 if (err != 0)
4732 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004733 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004734 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4735 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004736 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004737 switch (err) {
4738 default:
4739 goto out;
4740 case -NFS4ERR_GRACE:
4741 case -NFS4ERR_DELAY:
4742 nfs4_handle_exception(server, err, &exception);
4743 err = 0;
4744 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004745 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004746out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004747 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748}
4749
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004750#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004751/**
4752 * nfs41_check_expired_locks - possibly free a lock stateid
4753 *
4754 * @state: NFSv4 state for an inode
4755 *
4756 * Returns NFS_OK if recovery for this stateid is now finished.
4757 * Otherwise a negative NFS4ERR value is returned.
4758 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004759static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004760{
Chuck Levereb64cf92012-07-11 16:30:05 -04004761 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004762 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004763 struct nfs_server *server = NFS_SERVER(state->inode);
4764
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004765 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04004766 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004767 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4768 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004769 /* Free the stateid unless the server
4770 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04004771 if (status != -NFS4ERR_BAD_STATEID)
4772 nfs41_free_stateid(server,
4773 &lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004774 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004775 ret = status;
4776 }
4777 }
4778 };
4779
4780 return ret;
4781}
4782
4783static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4784{
4785 int status = NFS_OK;
4786
4787 if (test_bit(LK_STATE_IN_USE, &state->flags))
4788 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04004789 if (status != NFS_OK)
4790 status = nfs4_lock_expired(state, request);
4791 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004792}
4793#endif
4794
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4796{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004797 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004798 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004799 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004801 if ((fl_flags & FL_POSIX) &&
4802 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4803 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004804 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004805 status = nfs4_set_lock_state(state, request);
4806 if (status != 0)
4807 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004808 request->fl_flags |= FL_ACCESS;
4809 status = do_vfs_lock(request->fl_file, request);
4810 if (status < 0)
4811 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004812 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004813 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004814 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004815 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004816 request->fl_flags = fl_flags & ~FL_SLEEP;
4817 status = do_vfs_lock(request->fl_file, request);
4818 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004819 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004820 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004821 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004822 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004823 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004824 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004825 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004826 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4827 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004828out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004829 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004830out:
4831 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832 return status;
4833}
4834
4835static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4836{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004837 struct nfs4_exception exception = {
4838 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04004839 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004840 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841 int err;
4842
4843 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004844 err = _nfs4_proc_setlk(state, cmd, request);
4845 if (err == -NFS4ERR_DENIED)
4846 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004848 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849 } while (exception.retry);
4850 return err;
4851}
4852
4853static int
4854nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4855{
4856 struct nfs_open_context *ctx;
4857 struct nfs4_state *state;
4858 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4859 int status;
4860
4861 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004862 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863 state = ctx->state;
4864
4865 if (request->fl_start < 0 || request->fl_end < 0)
4866 return -EINVAL;
4867
Trond Myklebustd9531262009-07-21 19:22:38 -04004868 if (IS_GETLK(cmd)) {
4869 if (state != NULL)
4870 return nfs4_proc_getlk(state, F_GETLK, request);
4871 return 0;
4872 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873
4874 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4875 return -EINVAL;
4876
Trond Myklebustd9531262009-07-21 19:22:38 -04004877 if (request->fl_type == F_UNLCK) {
4878 if (state != NULL)
4879 return nfs4_proc_unlck(state, cmd, request);
4880 return 0;
4881 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882
Trond Myklebustd9531262009-07-21 19:22:38 -04004883 if (state == NULL)
4884 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04004885 /*
4886 * Don't rely on the VFS having checked the file open mode,
4887 * since it won't do this for flock() locks.
4888 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04004889 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04004890 case F_RDLCK:
4891 if (!(filp->f_mode & FMODE_READ))
4892 return -EBADF;
4893 break;
4894 case F_WRLCK:
4895 if (!(filp->f_mode & FMODE_WRITE))
4896 return -EBADF;
4897 }
4898
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899 do {
4900 status = nfs4_proc_setlk(state, cmd, request);
4901 if ((status != -EAGAIN) || IS_SETLK(cmd))
4902 break;
4903 timeout = nfs4_set_lock_task_retry(timeout);
4904 status = -ERESTARTSYS;
4905 if (signalled())
4906 break;
4907 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908 return status;
4909}
4910
Trond Myklebust888e6942005-11-04 15:38:11 -05004911int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4912{
4913 struct nfs_server *server = NFS_SERVER(state->inode);
4914 struct nfs4_exception exception = { };
4915 int err;
4916
4917 err = nfs4_set_lock_state(state, fl);
4918 if (err != 0)
4919 goto out;
4920 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004921 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004922 switch (err) {
4923 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004924 printk(KERN_ERR "NFS: %s: unhandled error "
4925 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004926 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004927 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004928 goto out;
4929 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004930 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07004931 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004932 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004933 nfs4_schedule_lease_recovery(server->nfs_client);
4934 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004935 case -NFS4ERR_BADSESSION:
4936 case -NFS4ERR_BADSLOT:
4937 case -NFS4ERR_BAD_HIGH_SLOT:
4938 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4939 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04004940 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004941 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004942 case -ERESTARTSYS:
4943 /*
4944 * The show must go on: exit, but mark the
4945 * stateid as needing recovery.
4946 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004947 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004948 case -NFS4ERR_ADMIN_REVOKED:
4949 case -NFS4ERR_BAD_STATEID:
4950 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004951 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004952 err = 0;
4953 goto out;
4954 case -ENOMEM:
4955 case -NFS4ERR_DENIED:
4956 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4957 err = 0;
4958 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004959 case -NFS4ERR_DELAY:
4960 break;
4961 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004962 err = nfs4_handle_exception(server, err, &exception);
4963 } while (exception.retry);
4964out:
4965 return err;
4966}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004967
Trond Myklebustcf470c32012-03-07 13:49:12 -05004968struct nfs_release_lockowner_data {
4969 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004970 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004971 struct nfs_release_lockowner_args args;
4972};
4973
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004974static void nfs4_release_lockowner_release(void *calldata)
4975{
Trond Myklebustcf470c32012-03-07 13:49:12 -05004976 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004977 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004978 kfree(calldata);
4979}
4980
Trond Myklebust17280172012-03-11 13:11:00 -04004981static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004982 .rpc_release = nfs4_release_lockowner_release,
4983};
4984
Trond Myklebustcf470c32012-03-07 13:49:12 -05004985int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004986{
4987 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004988 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004989 struct rpc_message msg = {
4990 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4991 };
4992
4993 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05004994 return -EINVAL;
4995 data = kmalloc(sizeof(*data), GFP_NOFS);
4996 if (!data)
4997 return -ENOMEM;
4998 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004999 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005000 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5001 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5002 data->args.lock_owner.s_dev = server->s_dev;
5003 msg.rpc_argp = &data->args;
5004 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5005 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005006}
5007
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005008#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5009
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005010static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5011 const void *buf, size_t buflen,
5012 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005013{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005014 if (strcmp(key, "") != 0)
5015 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005016
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005017 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005018}
5019
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005020static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5021 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005022{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005023 if (strcmp(key, "") != 0)
5024 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005025
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005026 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005027}
5028
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005029static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5030 size_t list_len, const char *name,
5031 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005032{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005033 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005034
J. Bruce Fields096455a2006-03-20 23:23:42 -05005035 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5036 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005037
5038 if (list && len <= list_len)
5039 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005040 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005041}
5042
Andy Adamson533eb462011-06-13 18:25:56 -04005043/*
5044 * nfs_fhget will use either the mounted_on_fileid or the fileid
5045 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005046static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5047{
Andy Adamson533eb462011-06-13 18:25:56 -04005048 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5049 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5050 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005051 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005052 return;
5053
5054 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005055 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005056 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5057 fattr->nlink = 2;
5058}
5059
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005060static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5061 const struct qstr *name,
5062 struct nfs4_fs_locations *fs_locations,
5063 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005064{
5065 struct nfs_server *server = NFS_SERVER(dir);
5066 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005067 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005068 };
5069 struct nfs4_fs_locations_arg args = {
5070 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005071 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005072 .page = page,
5073 .bitmask = bitmask,
5074 };
Benny Halevy22958462009-04-01 09:22:02 -04005075 struct nfs4_fs_locations_res res = {
5076 .fs_locations = fs_locations,
5077 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005078 struct rpc_message msg = {
5079 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5080 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005081 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005082 };
5083 int status;
5084
Harvey Harrison3110ff82008-05-02 13:42:44 -07005085 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005086
5087 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5088 * is not supported */
5089 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5090 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5091 else
5092 bitmask[0] |= FATTR4_WORD0_FILEID;
5093
Trond Myklebustc228fd32007-01-13 02:28:11 -05005094 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005095 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005096 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005097 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005098 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005099 return status;
5100}
5101
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005102int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5103 const struct qstr *name,
5104 struct nfs4_fs_locations *fs_locations,
5105 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005106{
5107 struct nfs4_exception exception = { };
5108 int err;
5109 do {
5110 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005111 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005112 &exception);
5113 } while (exception.retry);
5114 return err;
5115}
5116
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005117static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5118{
5119 int status;
5120 struct nfs4_secinfo_arg args = {
5121 .dir_fh = NFS_FH(dir),
5122 .name = name,
5123 };
5124 struct nfs4_secinfo_res res = {
5125 .flavors = flavors,
5126 };
5127 struct rpc_message msg = {
5128 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5129 .rpc_argp = &args,
5130 .rpc_resp = &res,
5131 };
5132
5133 dprintk("NFS call secinfo %s\n", name->name);
5134 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5135 dprintk("NFS reply secinfo: %d\n", status);
5136 return status;
5137}
5138
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005139int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5140 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005141{
5142 struct nfs4_exception exception = { };
5143 int err;
5144 do {
5145 err = nfs4_handle_exception(NFS_SERVER(dir),
5146 _nfs4_proc_secinfo(dir, name, flavors),
5147 &exception);
5148 } while (exception.retry);
5149 return err;
5150}
5151
Andy Adamson557134a2009-04-01 09:21:53 -04005152#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005153/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005154 * Check the exchange flags returned by the server for invalid flags, having
5155 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5156 * DS flags set.
5157 */
5158static int nfs4_check_cl_exchange_flags(u32 flags)
5159{
5160 if (flags & ~EXCHGID4_FLAG_MASK_R)
5161 goto out_inval;
5162 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5163 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5164 goto out_inval;
5165 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5166 goto out_inval;
5167 return NFS_OK;
5168out_inval:
5169 return -NFS4ERR_INVAL;
5170}
5171
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005172static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005173nfs41_same_server_scope(struct nfs41_server_scope *a,
5174 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005175{
5176 if (a->server_scope_sz == b->server_scope_sz &&
5177 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5178 return true;
5179
5180 return false;
5181}
5182
Andy Adamson357f54d2010-12-14 10:11:57 -05005183/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005184 * nfs4_proc_bind_conn_to_session()
5185 *
5186 * The 4.1 client currently uses the same TCP connection for the
5187 * fore and backchannel.
5188 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005189int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005190{
5191 int status;
5192 struct nfs41_bind_conn_to_session_res res;
5193 struct rpc_message msg = {
5194 .rpc_proc =
5195 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5196 .rpc_argp = clp,
5197 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005198 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005199 };
5200
5201 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005202
5203 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5204 if (unlikely(res.session == NULL)) {
5205 status = -ENOMEM;
5206 goto out;
5207 }
5208
5209 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5210 if (status == 0) {
5211 if (memcmp(res.session->sess_id.data,
5212 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5213 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5214 status = -EIO;
5215 goto out_session;
5216 }
5217 if (res.dir != NFS4_CDFS4_BOTH) {
5218 dprintk("NFS: %s: Unexpected direction from server\n",
5219 __func__);
5220 status = -EIO;
5221 goto out_session;
5222 }
5223 if (res.use_conn_in_rdma_mode) {
5224 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5225 __func__);
5226 status = -EIO;
5227 goto out_session;
5228 }
5229 }
5230out_session:
5231 kfree(res.session);
5232out:
5233 dprintk("<-- %s status= %d\n", __func__, status);
5234 return status;
5235}
5236
5237/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005238 * nfs4_proc_exchange_id()
5239 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005240 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5241 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04005242 * Since the clientid has expired, all compounds using sessions
5243 * associated with the stale clientid will be returning
5244 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5245 * be in some phase of session reset.
5246 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005247int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005248{
5249 nfs4_verifier verifier;
5250 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005251 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005252 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005253 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005254 };
5255 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005256 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005257 };
5258 int status;
5259 struct rpc_message msg = {
5260 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5261 .rpc_argp = &args,
5262 .rpc_resp = &res,
5263 .rpc_cred = cred,
5264 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005265
Chuck Leverf0920752012-05-21 22:45:41 -04005266 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04005267 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5268 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005269 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5270 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5271 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005272
Chuck Leveracdeb692012-05-21 22:46:16 -04005273 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005274 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005275 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005276 status = -ENOMEM;
5277 goto out;
5278 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005279
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005280 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005281 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005282 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005283 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005284 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005285 }
5286
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005287 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005288 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005289 status = -ENOMEM;
5290 goto out_server_scope;
5291 }
5292
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005293 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005294 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005295 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005296
Chuck Lever177313f2012-05-21 22:44:58 -04005297 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005298 clp->cl_clientid = res.clientid;
5299 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5300 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5301 clp->cl_seqid = res.seqid;
5302
Chuck Leveracdeb692012-05-21 22:46:16 -04005303 kfree(clp->cl_serverowner);
5304 clp->cl_serverowner = res.server_owner;
5305 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005306
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005307 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005308 kfree(clp->cl_implid);
5309 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005310
Chuck Lever177313f2012-05-21 22:44:58 -04005311 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005312 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005313 res.server_scope)) {
5314 dprintk("%s: server_scope mismatch detected\n",
5315 __func__);
5316 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005317 kfree(clp->cl_serverscope);
5318 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005319 }
5320
Chuck Lever177313f2012-05-21 22:44:58 -04005321 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005322 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005323 goto out;
5324 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005325 } else
5326 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005327
Chuck Leveracdeb692012-05-21 22:46:16 -04005328out_server_owner:
5329 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005330out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005331 kfree(res.server_scope);
5332out:
Chuck Lever177313f2012-05-21 22:44:58 -04005333 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005334 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005335 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005336 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04005337 clp->cl_implid->date.seconds,
5338 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005339 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005340 return status;
5341}
5342
Trond Myklebust66245532012-05-25 17:18:09 -04005343static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5344 struct rpc_cred *cred)
5345{
5346 struct rpc_message msg = {
5347 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5348 .rpc_argp = clp,
5349 .rpc_cred = cred,
5350 };
5351 int status;
5352
5353 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5354 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04005355 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04005356 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5357 return status;
5358}
5359
5360static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5361 struct rpc_cred *cred)
5362{
5363 unsigned int loop;
5364 int ret;
5365
5366 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5367 ret = _nfs4_proc_destroy_clientid(clp, cred);
5368 switch (ret) {
5369 case -NFS4ERR_DELAY:
5370 case -NFS4ERR_CLIENTID_BUSY:
5371 ssleep(1);
5372 break;
5373 default:
5374 return ret;
5375 }
5376 }
5377 return 0;
5378}
5379
5380int nfs4_destroy_clientid(struct nfs_client *clp)
5381{
5382 struct rpc_cred *cred;
5383 int ret = 0;
5384
5385 if (clp->cl_mvops->minor_version < 1)
5386 goto out;
5387 if (clp->cl_exchange_flags == 0)
5388 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04005389 if (clp->cl_preserve_clid)
5390 goto out;
Trond Myklebust66245532012-05-25 17:18:09 -04005391 cred = nfs4_get_exchange_id_cred(clp);
5392 ret = nfs4_proc_destroy_clientid(clp, cred);
5393 if (cred)
5394 put_rpccred(cred);
5395 switch (ret) {
5396 case 0:
5397 case -NFS4ERR_STALE_CLIENTID:
5398 clp->cl_exchange_flags = 0;
5399 }
5400out:
5401 return ret;
5402}
5403
Andy Adamson2050f0c2009-04-01 09:22:30 -04005404struct nfs4_get_lease_time_data {
5405 struct nfs4_get_lease_time_args *args;
5406 struct nfs4_get_lease_time_res *res;
5407 struct nfs_client *clp;
5408};
5409
5410static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5411 void *calldata)
5412{
Andy Adamson2050f0c2009-04-01 09:22:30 -04005413 struct nfs4_get_lease_time_data *data =
5414 (struct nfs4_get_lease_time_data *)calldata;
5415
5416 dprintk("--> %s\n", __func__);
5417 /* just setup sequence, do not trigger session recovery
5418 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005419 nfs41_setup_sequence(data->clp->cl_session,
5420 &data->args->la_seq_args,
5421 &data->res->lr_seq_res,
5422 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005423 dprintk("<-- %s\n", __func__);
5424}
5425
5426/*
5427 * Called from nfs4_state_manager thread for session setup, so don't recover
5428 * from sequence operation or clientid errors.
5429 */
5430static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5431{
5432 struct nfs4_get_lease_time_data *data =
5433 (struct nfs4_get_lease_time_data *)calldata;
5434
5435 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005436 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5437 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005438 switch (task->tk_status) {
5439 case -NFS4ERR_DELAY:
5440 case -NFS4ERR_GRACE:
5441 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5442 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5443 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005444 /* fall through */
5445 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005446 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005447 return;
5448 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005449 dprintk("<-- %s\n", __func__);
5450}
5451
Trond Myklebust17280172012-03-11 13:11:00 -04005452static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005453 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5454 .rpc_call_done = nfs4_get_lease_time_done,
5455};
5456
5457int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5458{
5459 struct rpc_task *task;
5460 struct nfs4_get_lease_time_args args;
5461 struct nfs4_get_lease_time_res res = {
5462 .lr_fsinfo = fsinfo,
5463 };
5464 struct nfs4_get_lease_time_data data = {
5465 .args = &args,
5466 .res = &res,
5467 .clp = clp,
5468 };
5469 struct rpc_message msg = {
5470 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5471 .rpc_argp = &args,
5472 .rpc_resp = &res,
5473 };
5474 struct rpc_task_setup task_setup = {
5475 .rpc_client = clp->cl_rpcclient,
5476 .rpc_message = &msg,
5477 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005478 .callback_data = &data,
5479 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005480 };
5481 int status;
5482
Trond Myklebust9d12b212012-01-17 22:04:25 -05005483 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005484 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005485 dprintk("--> %s\n", __func__);
5486 task = rpc_run_task(&task_setup);
5487
5488 if (IS_ERR(task))
5489 status = PTR_ERR(task);
5490 else {
5491 status = task->tk_status;
5492 rpc_put_task(task);
5493 }
5494 dprintk("<-- %s return %d\n", __func__, status);
5495
5496 return status;
5497}
5498
Andy Adamsonfc931582009-04-01 09:22:31 -04005499/*
5500 * Initialize the values to be used by the client in CREATE_SESSION
5501 * If nfs4_init_session set the fore channel request and response sizes,
5502 * use them.
5503 *
5504 * Set the back channel max_resp_sz_cached to zero to force the client to
5505 * always set csa_cachethis to FALSE because the current implementation
5506 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5507 */
5508static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5509{
5510 struct nfs4_session *session = args->client->cl_session;
Trond Myklebustae72ae672012-11-20 11:02:55 -05005511 unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
5512 mxresp_sz = session->fc_target_max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005513
5514 if (mxrqst_sz == 0)
5515 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5516 if (mxresp_sz == 0)
5517 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5518 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005519 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5520 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005521 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005522 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005523
5524 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005525 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005526 __func__,
5527 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005528 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005529
5530 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005531 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5532 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5533 args->bc_attrs.max_resp_sz_cached = 0;
5534 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5535 args->bc_attrs.max_reqs = 1;
5536
5537 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5538 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5539 __func__,
5540 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5541 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5542 args->bc_attrs.max_reqs);
5543}
5544
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005545static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005546{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005547 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5548 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5549
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005550 if (rcvd->max_resp_sz > sent->max_resp_sz)
5551 return -EINVAL;
5552 /*
5553 * Our requested max_ops is the minimum we need; we're not
5554 * prepared to break up compounds into smaller pieces than that.
5555 * So, no point even trying to continue if the server won't
5556 * cooperate:
5557 */
5558 if (rcvd->max_ops < sent->max_ops)
5559 return -EINVAL;
5560 if (rcvd->max_reqs == 0)
5561 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005562 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5563 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005564 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005565}
5566
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005567static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5568{
5569 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5570 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005571
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005572 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5573 return -EINVAL;
5574 if (rcvd->max_resp_sz < sent->max_resp_sz)
5575 return -EINVAL;
5576 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5577 return -EINVAL;
5578 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005579 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005580 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005581 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005582 return -EINVAL;
5583 return 0;
5584}
Andy Adamson8d353012009-04-01 09:22:32 -04005585
Andy Adamson8d353012009-04-01 09:22:32 -04005586static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5587 struct nfs4_session *session)
5588{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005589 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005590
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005591 ret = nfs4_verify_fore_channel_attrs(args, session);
5592 if (ret)
5593 return ret;
5594 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005595}
5596
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005597static int _nfs4_proc_create_session(struct nfs_client *clp,
5598 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005599{
5600 struct nfs4_session *session = clp->cl_session;
5601 struct nfs41_create_session_args args = {
5602 .client = clp,
5603 .cb_program = NFS4_CALLBACK,
5604 };
5605 struct nfs41_create_session_res res = {
5606 .client = clp,
5607 };
5608 struct rpc_message msg = {
5609 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5610 .rpc_argp = &args,
5611 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005612 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04005613 };
5614 int status;
5615
5616 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005617 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005618
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005619 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005620
Trond Myklebust43095d32012-11-20 11:13:12 -05005621 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04005622 /* Verify the session's negotiated channel_attrs values */
5623 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005624 /* Increment the clientid slot sequence id */
5625 clp->cl_seqid++;
5626 }
5627
5628 return status;
5629}
5630
5631/*
5632 * Issues a CREATE_SESSION operation to the server.
5633 * It is the responsibility of the caller to verify the session is
5634 * expired before calling this routine.
5635 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005636int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005637{
5638 int status;
5639 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005640 struct nfs4_session *session = clp->cl_session;
5641
5642 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5643
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005644 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04005645 if (status)
5646 goto out;
5647
Andy Adamsonaacd5532011-11-09 13:58:21 -05005648 /* Init or reset the session slot tables */
5649 status = nfs4_setup_session_slot_tables(session);
5650 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005651 if (status)
5652 goto out;
5653
5654 ptr = (unsigned *)&session->sess_id.data[0];
5655 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5656 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005657out:
5658 dprintk("<-- %s\n", __func__);
5659 return status;
5660}
5661
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005662/*
5663 * Issue the over-the-wire RPC DESTROY_SESSION.
5664 * The caller must serialize access to this routine.
5665 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005666int nfs4_proc_destroy_session(struct nfs4_session *session,
5667 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005668{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005669 struct rpc_message msg = {
5670 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5671 .rpc_argp = session,
5672 .rpc_cred = cred,
5673 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005674 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005675
5676 dprintk("--> nfs4_proc_destroy_session\n");
5677
5678 /* session is still being setup */
5679 if (session->clp->cl_cons_state != NFS_CS_READY)
5680 return status;
5681
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005682 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005683
5684 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04005685 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005686 "Session has been destroyed regardless...\n", status);
5687
5688 dprintk("<-- nfs4_proc_destroy_session\n");
5689 return status;
5690}
5691
Trond Myklebust7b38c362012-05-23 13:23:31 -04005692/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005693 * Renew the cl_session lease.
5694 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005695struct nfs4_sequence_data {
5696 struct nfs_client *clp;
5697 struct nfs4_sequence_args args;
5698 struct nfs4_sequence_res res;
5699};
5700
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005701static void nfs41_sequence_release(void *data)
5702{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005703 struct nfs4_sequence_data *calldata = data;
5704 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005705
Alexandros Batsakis71358402010-02-05 03:45:05 -08005706 if (atomic_read(&clp->cl_count) > 1)
5707 nfs4_schedule_state_renewal(clp);
5708 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005709 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005710}
5711
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005712static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5713{
5714 switch(task->tk_status) {
5715 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005716 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5717 return -EAGAIN;
5718 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005719 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005720 }
5721 return 0;
5722}
5723
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005724static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005725{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005726 struct nfs4_sequence_data *calldata = data;
5727 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005728
Trond Myklebust14516c32010-07-31 14:29:06 -04005729 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5730 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005731
5732 if (task->tk_status < 0) {
5733 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005734 if (atomic_read(&clp->cl_count) == 1)
5735 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005736
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005737 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5738 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005739 return;
5740 }
5741 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005742 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005743out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005744 dprintk("<-- %s\n", __func__);
5745}
5746
5747static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5748{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005749 struct nfs4_sequence_data *calldata = data;
5750 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005751 struct nfs4_sequence_args *args;
5752 struct nfs4_sequence_res *res;
5753
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005754 args = task->tk_msg.rpc_argp;
5755 res = task->tk_msg.rpc_resp;
5756
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005757 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005758}
5759
5760static const struct rpc_call_ops nfs41_sequence_ops = {
5761 .rpc_call_done = nfs41_sequence_call_done,
5762 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005763 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005764};
5765
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005766static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
5767 struct rpc_cred *cred,
5768 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005769{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005770 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005771 struct rpc_message msg = {
5772 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5773 .rpc_cred = cred,
5774 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005775 struct rpc_task_setup task_setup_data = {
5776 .rpc_client = clp->cl_rpcclient,
5777 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005778 .callback_ops = &nfs41_sequence_ops,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005779 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5780 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005781
Alexandros Batsakis71358402010-02-05 03:45:05 -08005782 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005783 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005784 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005785 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005786 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005787 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005788 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05005789 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005790 if (is_privileged)
5791 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005792 msg.rpc_argp = &calldata->args;
5793 msg.rpc_resp = &calldata->res;
5794 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005795 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005796
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005797 return rpc_run_task(&task_setup_data);
5798}
5799
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005800static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005801{
5802 struct rpc_task *task;
5803 int ret = 0;
5804
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005805 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5806 return 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005807 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005808 if (IS_ERR(task))
5809 ret = PTR_ERR(task);
5810 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005811 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005812 dprintk("<-- %s status=%d\n", __func__, ret);
5813 return ret;
5814}
5815
5816static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5817{
5818 struct rpc_task *task;
5819 int ret;
5820
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005821 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005822 if (IS_ERR(task)) {
5823 ret = PTR_ERR(task);
5824 goto out;
5825 }
5826 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005827 if (!ret) {
5828 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5829
5830 if (task->tk_status == 0)
5831 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005832 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005833 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005834 rpc_put_task(task);
5835out:
5836 dprintk("<-- %s status=%d\n", __func__, ret);
5837 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005838}
5839
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005840struct nfs4_reclaim_complete_data {
5841 struct nfs_client *clp;
5842 struct nfs41_reclaim_complete_args arg;
5843 struct nfs41_reclaim_complete_res res;
5844};
5845
5846static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5847{
5848 struct nfs4_reclaim_complete_data *calldata = data;
5849
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005850 nfs41_setup_sequence(calldata->clp->cl_session,
5851 &calldata->arg.seq_args,
5852 &calldata->res.seq_res,
5853 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005854}
5855
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005856static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5857{
5858 switch(task->tk_status) {
5859 case 0:
5860 case -NFS4ERR_COMPLETE_ALREADY:
5861 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5862 break;
5863 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005864 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005865 /* fall through */
5866 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005867 return -EAGAIN;
5868 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005869 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005870 }
5871 return 0;
5872}
5873
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005874static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5875{
5876 struct nfs4_reclaim_complete_data *calldata = data;
5877 struct nfs_client *clp = calldata->clp;
5878 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5879
5880 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005881 if (!nfs41_sequence_done(task, res))
5882 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005883
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005884 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5885 rpc_restart_call_prepare(task);
5886 return;
5887 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005888 dprintk("<-- %s\n", __func__);
5889}
5890
5891static void nfs4_free_reclaim_complete_data(void *data)
5892{
5893 struct nfs4_reclaim_complete_data *calldata = data;
5894
5895 kfree(calldata);
5896}
5897
5898static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5899 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5900 .rpc_call_done = nfs4_reclaim_complete_done,
5901 .rpc_release = nfs4_free_reclaim_complete_data,
5902};
5903
5904/*
5905 * Issue a global reclaim complete.
5906 */
5907static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5908{
5909 struct nfs4_reclaim_complete_data *calldata;
5910 struct rpc_task *task;
5911 struct rpc_message msg = {
5912 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5913 };
5914 struct rpc_task_setup task_setup_data = {
5915 .rpc_client = clp->cl_rpcclient,
5916 .rpc_message = &msg,
5917 .callback_ops = &nfs4_reclaim_complete_call_ops,
5918 .flags = RPC_TASK_ASYNC,
5919 };
5920 int status = -ENOMEM;
5921
5922 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005923 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005924 if (calldata == NULL)
5925 goto out;
5926 calldata->clp = clp;
5927 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005928
Trond Myklebust9d12b212012-01-17 22:04:25 -05005929 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005930 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005931 msg.rpc_argp = &calldata->arg;
5932 msg.rpc_resp = &calldata->res;
5933 task_setup_data.callback_data = calldata;
5934 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005935 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005936 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005937 goto out;
5938 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05005939 status = nfs4_wait_for_completion_rpc_task(task);
5940 if (status == 0)
5941 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005942 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005943 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005944out:
5945 dprintk("<-- %s status=%d\n", __func__, status);
5946 return status;
5947}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005948
5949static void
5950nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5951{
5952 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00005953 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04005954 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005955
5956 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00005957 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5958 * right now covering the LAYOUTGET we are about to send.
5959 * However, that is not so catastrophic, and there seems
5960 * to be no way to prevent it completely.
5961 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04005962 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005963 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005964 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00005965 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5966 NFS_I(lgp->args.inode)->layout,
5967 lgp->args.ctx->state)) {
5968 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00005969 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005970}
5971
5972static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5973{
5974 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07005975 struct inode *inode = lgp->args.inode;
5976 struct nfs_server *server = NFS_SERVER(inode);
5977 struct pnfs_layout_hdr *lo;
5978 struct nfs4_state *state = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005979
5980 dprintk("--> %s\n", __func__);
5981
Trond Myklebust6ba7db32012-10-22 20:07:20 -04005982 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07005983 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005984
5985 switch (task->tk_status) {
5986 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07005987 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005988 case -NFS4ERR_LAYOUTTRYLATER:
5989 case -NFS4ERR_RECALLCONFLICT:
5990 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07005991 break;
5992 case -NFS4ERR_EXPIRED:
5993 case -NFS4ERR_BAD_STATEID:
5994 spin_lock(&inode->i_lock);
5995 lo = NFS_I(inode)->layout;
5996 if (!lo || list_empty(&lo->plh_segs)) {
5997 spin_unlock(&inode->i_lock);
5998 /* If the open stateid was bad, then recover it. */
5999 state = lgp->args.ctx->state;
6000 } else {
6001 LIST_HEAD(head);
6002
6003 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6004 spin_unlock(&inode->i_lock);
6005 /* Mark the bad layout state as invalid, then
6006 * retry using the open stateid. */
6007 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006008 }
6009 }
Trond Myklebustee314c22012-10-01 17:25:48 -07006010 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6011 rpc_restart_call_prepare(task);
6012out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006013 dprintk("<-- %s\n", __func__);
6014}
6015
Idan Kedar85541162012-08-02 11:47:10 +03006016static size_t max_response_pages(struct nfs_server *server)
6017{
6018 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6019 return nfs_page_array_len(0, max_resp_sz);
6020}
6021
6022static void nfs4_free_pages(struct page **pages, size_t size)
6023{
6024 int i;
6025
6026 if (!pages)
6027 return;
6028
6029 for (i = 0; i < size; i++) {
6030 if (!pages[i])
6031 break;
6032 __free_page(pages[i]);
6033 }
6034 kfree(pages);
6035}
6036
6037static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6038{
6039 struct page **pages;
6040 int i;
6041
6042 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6043 if (!pages) {
6044 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6045 return NULL;
6046 }
6047
6048 for (i = 0; i < size; i++) {
6049 pages[i] = alloc_page(gfp_flags);
6050 if (!pages[i]) {
6051 dprintk("%s: failed to allocate page\n", __func__);
6052 nfs4_free_pages(pages, size);
6053 return NULL;
6054 }
6055 }
6056
6057 return pages;
6058}
6059
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006060static void nfs4_layoutget_release(void *calldata)
6061{
6062 struct nfs4_layoutget *lgp = calldata;
Idan Kedar85541162012-08-02 11:47:10 +03006063 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6064 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006065
6066 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006067 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006068 put_nfs_open_context(lgp->args.ctx);
6069 kfree(calldata);
6070 dprintk("<-- %s\n", __func__);
6071}
6072
6073static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6074 .rpc_call_prepare = nfs4_layoutget_prepare,
6075 .rpc_call_done = nfs4_layoutget_done,
6076 .rpc_release = nfs4_layoutget_release,
6077};
6078
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006079struct pnfs_layout_segment *
6080nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006081{
6082 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Idan Kedar85541162012-08-02 11:47:10 +03006083 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006084 struct rpc_task *task;
6085 struct rpc_message msg = {
6086 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6087 .rpc_argp = &lgp->args,
6088 .rpc_resp = &lgp->res,
6089 };
6090 struct rpc_task_setup task_setup_data = {
6091 .rpc_client = server->client,
6092 .rpc_message = &msg,
6093 .callback_ops = &nfs4_layoutget_call_ops,
6094 .callback_data = lgp,
6095 .flags = RPC_TASK_ASYNC,
6096 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006097 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006098 int status = 0;
6099
6100 dprintk("--> %s\n", __func__);
6101
Idan Kedar85541162012-08-02 11:47:10 +03006102 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6103 if (!lgp->args.layout.pages) {
6104 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006105 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006106 }
6107 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
6108
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006109 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006110 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006111 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006112 task = rpc_run_task(&task_setup_data);
6113 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006114 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006115 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006116 if (status == 0)
6117 status = task->tk_status;
6118 if (status == 0)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006119 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006120 rpc_put_task(task);
6121 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006122 if (status)
6123 return ERR_PTR(status);
6124 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006125}
6126
Benny Halevycbe82602011-05-22 19:52:37 +03006127static void
6128nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6129{
6130 struct nfs4_layoutreturn *lrp = calldata;
6131
6132 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006133 nfs41_setup_sequence(lrp->clp->cl_session,
6134 &lrp->args.seq_args,
6135 &lrp->res.seq_res,
6136 task);
Benny Halevycbe82602011-05-22 19:52:37 +03006137}
6138
6139static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6140{
6141 struct nfs4_layoutreturn *lrp = calldata;
6142 struct nfs_server *server;
6143
6144 dprintk("--> %s\n", __func__);
6145
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006146 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03006147 return;
6148
6149 server = NFS_SERVER(lrp->args.inode);
6150 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006151 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006152 return;
6153 }
Benny Halevycbe82602011-05-22 19:52:37 +03006154 dprintk("<-- %s\n", __func__);
6155}
6156
6157static void nfs4_layoutreturn_release(void *calldata)
6158{
6159 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04006160 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006161
6162 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04006163 spin_lock(&lo->plh_inode->i_lock);
6164 if (lrp->res.lrs_present)
6165 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6166 lo->plh_block_lgets--;
6167 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006168 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006169 kfree(calldata);
6170 dprintk("<-- %s\n", __func__);
6171}
6172
6173static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6174 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6175 .rpc_call_done = nfs4_layoutreturn_done,
6176 .rpc_release = nfs4_layoutreturn_release,
6177};
6178
6179int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6180{
6181 struct rpc_task *task;
6182 struct rpc_message msg = {
6183 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6184 .rpc_argp = &lrp->args,
6185 .rpc_resp = &lrp->res,
6186 };
6187 struct rpc_task_setup task_setup_data = {
6188 .rpc_client = lrp->clp->cl_rpcclient,
6189 .rpc_message = &msg,
6190 .callback_ops = &nfs4_layoutreturn_call_ops,
6191 .callback_data = lrp,
6192 };
6193 int status;
6194
6195 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006196 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006197 task = rpc_run_task(&task_setup_data);
6198 if (IS_ERR(task))
6199 return PTR_ERR(task);
6200 status = task->tk_status;
6201 dprintk("<-- %s status=%d\n", __func__, status);
6202 rpc_put_task(task);
6203 return status;
6204}
6205
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006206/*
6207 * Retrieve the list of Data Server devices from the MDS.
6208 */
6209static int _nfs4_getdevicelist(struct nfs_server *server,
6210 const struct nfs_fh *fh,
6211 struct pnfs_devicelist *devlist)
6212{
6213 struct nfs4_getdevicelist_args args = {
6214 .fh = fh,
6215 .layoutclass = server->pnfs_curr_ld->id,
6216 };
6217 struct nfs4_getdevicelist_res res = {
6218 .devlist = devlist,
6219 };
6220 struct rpc_message msg = {
6221 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6222 .rpc_argp = &args,
6223 .rpc_resp = &res,
6224 };
6225 int status;
6226
6227 dprintk("--> %s\n", __func__);
6228 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6229 &res.seq_res, 0);
6230 dprintk("<-- %s status=%d\n", __func__, status);
6231 return status;
6232}
6233
6234int nfs4_proc_getdevicelist(struct nfs_server *server,
6235 const struct nfs_fh *fh,
6236 struct pnfs_devicelist *devlist)
6237{
6238 struct nfs4_exception exception = { };
6239 int err;
6240
6241 do {
6242 err = nfs4_handle_exception(server,
6243 _nfs4_getdevicelist(server, fh, devlist),
6244 &exception);
6245 } while (exception.retry);
6246
6247 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6248 err, devlist->num_devs);
6249
6250 return err;
6251}
6252EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6253
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006254static int
6255_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6256{
6257 struct nfs4_getdeviceinfo_args args = {
6258 .pdev = pdev,
6259 };
6260 struct nfs4_getdeviceinfo_res res = {
6261 .pdev = pdev,
6262 };
6263 struct rpc_message msg = {
6264 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6265 .rpc_argp = &args,
6266 .rpc_resp = &res,
6267 };
6268 int status;
6269
6270 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006271 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006272 dprintk("<-- %s status=%d\n", __func__, status);
6273
6274 return status;
6275}
6276
6277int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6278{
6279 struct nfs4_exception exception = { };
6280 int err;
6281
6282 do {
6283 err = nfs4_handle_exception(server,
6284 _nfs4_proc_getdeviceinfo(server, pdev),
6285 &exception);
6286 } while (exception.retry);
6287 return err;
6288}
6289EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6290
Andy Adamson863a3c62011-03-23 13:27:54 +00006291static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6292{
6293 struct nfs4_layoutcommit_data *data = calldata;
6294 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006295 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00006296
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006297 nfs41_setup_sequence(session,
6298 &data->args.seq_args,
6299 &data->res.seq_res,
6300 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00006301}
6302
6303static void
6304nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6305{
6306 struct nfs4_layoutcommit_data *data = calldata;
6307 struct nfs_server *server = NFS_SERVER(data->args.inode);
6308
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006309 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00006310 return;
6311
6312 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006313 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6314 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6315 case -NFS4ERR_BADLAYOUT: /* no layout */
6316 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006317 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006318 break;
6319 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006320 nfs_post_op_update_inode_force_wcc(data->args.inode,
6321 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006322 break;
6323 default:
6324 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6325 rpc_restart_call_prepare(task);
6326 return;
6327 }
6328 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006329}
6330
6331static void nfs4_layoutcommit_release(void *calldata)
6332{
6333 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04006334 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07006335 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00006336
Andy Adamsondb29c082011-07-30 20:52:38 -04006337 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006338 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04006339 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6340 list_del_init(&lseg->pls_lc_list);
6341 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6342 &lseg->pls_flags))
Trond Myklebust9369a432012-09-18 20:57:08 -04006343 pnfs_put_lseg(lseg);
Peng Taoa9bae562011-07-30 20:52:33 -04006344 }
Peng Tao92407e72011-10-23 20:21:17 -07006345
6346 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6347 smp_mb__after_clear_bit();
6348 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6349
Andy Adamson863a3c62011-03-23 13:27:54 +00006350 put_rpccred(data->cred);
6351 kfree(data);
6352}
6353
6354static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6355 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6356 .rpc_call_done = nfs4_layoutcommit_done,
6357 .rpc_release = nfs4_layoutcommit_release,
6358};
6359
6360int
Andy Adamsonef311532011-03-12 02:58:10 -05006361nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006362{
6363 struct rpc_message msg = {
6364 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6365 .rpc_argp = &data->args,
6366 .rpc_resp = &data->res,
6367 .rpc_cred = data->cred,
6368 };
6369 struct rpc_task_setup task_setup_data = {
6370 .task = &data->task,
6371 .rpc_client = NFS_CLIENT(data->args.inode),
6372 .rpc_message = &msg,
6373 .callback_ops = &nfs4_layoutcommit_ops,
6374 .callback_data = data,
6375 .flags = RPC_TASK_ASYNC,
6376 };
6377 struct rpc_task *task;
6378 int status = 0;
6379
6380 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6381 "lbw: %llu inode %lu\n",
6382 data->task.tk_pid, sync,
6383 data->args.lastbytewritten,
6384 data->args.inode->i_ino);
6385
Trond Myklebust9d12b212012-01-17 22:04:25 -05006386 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006387 task = rpc_run_task(&task_setup_data);
6388 if (IS_ERR(task))
6389 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006390 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006391 goto out;
6392 status = nfs4_wait_for_completion_rpc_task(task);
6393 if (status != 0)
6394 goto out;
6395 status = task->tk_status;
6396out:
6397 dprintk("%s: status %d\n", __func__, status);
6398 rpc_put_task(task);
6399 return status;
6400}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006401
6402static int
6403_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6404 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6405{
6406 struct nfs41_secinfo_no_name_args args = {
6407 .style = SECINFO_STYLE_CURRENT_FH,
6408 };
6409 struct nfs4_secinfo_res res = {
6410 .flavors = flavors,
6411 };
6412 struct rpc_message msg = {
6413 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6414 .rpc_argp = &args,
6415 .rpc_resp = &res,
6416 };
6417 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6418}
6419
6420static int
6421nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6422 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6423{
6424 struct nfs4_exception exception = { };
6425 int err;
6426 do {
6427 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6428 switch (err) {
6429 case 0:
6430 case -NFS4ERR_WRONGSEC:
6431 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006432 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006433 default:
6434 err = nfs4_handle_exception(server, err, &exception);
6435 }
6436 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006437out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006438 return err;
6439}
6440
6441static int
6442nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6443 struct nfs_fsinfo *info)
6444{
6445 int err;
6446 struct page *page;
6447 rpc_authflavor_t flavor;
6448 struct nfs4_secinfo_flavors *flavors;
6449
6450 page = alloc_page(GFP_KERNEL);
6451 if (!page) {
6452 err = -ENOMEM;
6453 goto out;
6454 }
6455
6456 flavors = page_address(page);
6457 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6458
6459 /*
6460 * Fall back on "guess and check" method if
6461 * the server doesn't support SECINFO_NO_NAME
6462 */
6463 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6464 err = nfs4_find_root_sec(server, fhandle, info);
6465 goto out_freepage;
6466 }
6467 if (err)
6468 goto out_freepage;
6469
6470 flavor = nfs_find_best_sec(flavors);
6471 if (err == 0)
6472 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6473
6474out_freepage:
6475 put_page(page);
6476 if (err == -EACCES)
6477 return -EPERM;
6478out:
6479 return err;
6480}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006481
6482static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006483{
6484 int status;
6485 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006486 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006487 };
6488 struct nfs41_test_stateid_res res;
6489 struct rpc_message msg = {
6490 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6491 .rpc_argp = &args,
6492 .rpc_resp = &res,
6493 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006494
Chuck Lever38527b12012-07-11 16:30:23 -04006495 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006496 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006497 nfs4_set_sequence_privileged(&args.seq_args);
6498 status = nfs4_call_sync_sequence(server->client, server, &msg,
6499 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04006500 if (status != NFS_OK) {
6501 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04006502 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04006503 }
6504 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04006505 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006506}
6507
Chuck Lever38527b12012-07-11 16:30:23 -04006508/**
6509 * nfs41_test_stateid - perform a TEST_STATEID operation
6510 *
6511 * @server: server / transport on which to perform the operation
6512 * @stateid: state ID to test
6513 *
6514 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6515 * Otherwise a negative NFS4ERR value is returned if the operation
6516 * failed or the state ID is not currently valid.
6517 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006518static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006519{
6520 struct nfs4_exception exception = { };
6521 int err;
6522 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006523 err = _nfs41_test_stateid(server, stateid);
6524 if (err != -NFS4ERR_DELAY)
6525 break;
6526 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04006527 } while (exception.retry);
6528 return err;
6529}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006530
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006531static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006532{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006533 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006534 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006535 };
6536 struct nfs41_free_stateid_res res;
6537 struct rpc_message msg = {
6538 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6539 .rpc_argp = &args,
6540 .rpc_resp = &res,
6541 };
Chuck Lever38527b12012-07-11 16:30:23 -04006542 int status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006543
Chuck Lever38527b12012-07-11 16:30:23 -04006544 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006545 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006546 nfs4_set_sequence_privileged(&args.seq_args);
Chuck Lever38527b12012-07-11 16:30:23 -04006547 status = nfs4_call_sync_sequence(server->client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006548 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04006549 dprintk("NFS reply free_stateid: %d\n", status);
6550 return status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006551}
6552
Chuck Lever38527b12012-07-11 16:30:23 -04006553/**
6554 * nfs41_free_stateid - perform a FREE_STATEID operation
6555 *
6556 * @server: server / transport on which to perform the operation
6557 * @stateid: state ID to release
6558 *
6559 * Returns NFS_OK if the server freed "stateid". Otherwise a
6560 * negative NFS4ERR value is returned.
6561 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006562static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006563{
6564 struct nfs4_exception exception = { };
6565 int err;
6566 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006567 err = _nfs4_free_stateid(server, stateid);
6568 if (err != -NFS4ERR_DELAY)
6569 break;
6570 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006571 } while (exception.retry);
6572 return err;
6573}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006574
6575static bool nfs41_match_stateid(const nfs4_stateid *s1,
6576 const nfs4_stateid *s2)
6577{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006578 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006579 return false;
6580
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006581 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006582 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006583 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006584 return true;
6585
6586 return false;
6587}
6588
Andy Adamson557134a2009-04-01 09:21:53 -04006589#endif /* CONFIG_NFS_V4_1 */
6590
Trond Myklebust36281ca2012-03-04 18:13:56 -05006591static bool nfs4_match_stateid(const nfs4_stateid *s1,
6592 const nfs4_stateid *s2)
6593{
Trond Myklebustf597c532012-03-04 18:13:56 -05006594 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006595}
6596
6597
Trond Myklebust17280172012-03-11 13:11:00 -04006598static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006599 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006600 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006601 .recover_open = nfs4_open_reclaim,
6602 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006603 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006604 .get_clid_cred = nfs4_get_setclientid_cred,
Chuck Lever05f4c352012-09-14 17:24:32 -04006605 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006606};
6607
Andy Adamson591d71c2009-04-01 09:22:47 -04006608#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006609static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006610 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6611 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6612 .recover_open = nfs4_open_reclaim,
6613 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006614 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006615 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006616 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04006617 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04006618};
6619#endif /* CONFIG_NFS_V4_1 */
6620
Trond Myklebust17280172012-03-11 13:11:00 -04006621static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006622 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006623 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006624 .recover_open = nfs4_open_expired,
6625 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006626 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006627 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006628};
6629
Andy Adamson591d71c2009-04-01 09:22:47 -04006630#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006631static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006632 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6633 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006634 .recover_open = nfs41_open_expired,
6635 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006636 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006637 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006638};
6639#endif /* CONFIG_NFS_V4_1 */
6640
Trond Myklebust17280172012-03-11 13:11:00 -04006641static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006642 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006643 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006644 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006645};
6646
6647#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006648static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006649 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006650 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006651 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006652};
6653#endif
6654
Trond Myklebust97dc1352010-06-16 09:52:26 -04006655static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6656 .minor_version = 0,
6657 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006658 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006659 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006660 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6661 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6662 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006663};
6664
6665#if defined(CONFIG_NFS_V4_1)
6666static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6667 .minor_version = 1,
Bryan Schumaker104287c2012-11-12 14:13:13 -05006668 .call_sync = nfs4_call_sync_sequence,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006669 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006670 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006671 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6672 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6673 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006674};
6675#endif
6676
Trond Myklebust97dc1352010-06-16 09:52:26 -04006677const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6678 [0] = &nfs_v4_0_minor_ops,
6679#if defined(CONFIG_NFS_V4_1)
6680 [1] = &nfs_v4_1_minor_ops,
6681#endif
6682};
6683
Bryan Schumaker73a79702012-07-16 16:39:12 -04006684const struct inode_operations nfs4_dir_inode_operations = {
6685 .create = nfs_create,
6686 .lookup = nfs_lookup,
6687 .atomic_open = nfs_atomic_open,
6688 .link = nfs_link,
6689 .unlink = nfs_unlink,
6690 .symlink = nfs_symlink,
6691 .mkdir = nfs_mkdir,
6692 .rmdir = nfs_rmdir,
6693 .mknod = nfs_mknod,
6694 .rename = nfs_rename,
6695 .permission = nfs_permission,
6696 .getattr = nfs_getattr,
6697 .setattr = nfs_setattr,
6698 .getxattr = generic_getxattr,
6699 .setxattr = generic_setxattr,
6700 .listxattr = generic_listxattr,
6701 .removexattr = generic_removexattr,
6702};
6703
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006704static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006705 .permission = nfs_permission,
6706 .getattr = nfs_getattr,
6707 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006708 .getxattr = generic_getxattr,
6709 .setxattr = generic_setxattr,
6710 .listxattr = generic_listxattr,
6711 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006712};
6713
David Howells509de812006-08-22 20:06:11 -04006714const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006715 .version = 4, /* protocol version */
6716 .dentry_ops = &nfs4_dentry_operations,
6717 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006718 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006719 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006720 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04006721 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04006722 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006723 .getattr = nfs4_proc_getattr,
6724 .setattr = nfs4_proc_setattr,
6725 .lookup = nfs4_proc_lookup,
6726 .access = nfs4_proc_access,
6727 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006728 .create = nfs4_proc_create,
6729 .remove = nfs4_proc_remove,
6730 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04006731 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006732 .unlink_done = nfs4_proc_unlink_done,
6733 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006734 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04006735 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04006736 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006737 .link = nfs4_proc_link,
6738 .symlink = nfs4_proc_symlink,
6739 .mkdir = nfs4_proc_mkdir,
6740 .rmdir = nfs4_proc_remove,
6741 .readdir = nfs4_proc_readdir,
6742 .mknod = nfs4_proc_mknod,
6743 .statfs = nfs4_proc_statfs,
6744 .fsinfo = nfs4_proc_fsinfo,
6745 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006746 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006747 .decode_dirent = nfs4_decode_dirent,
6748 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04006749 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04006750 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05006751 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006752 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04006753 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04006754 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006755 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006756 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04006757 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006758 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006759 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006760 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006761 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006762 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04006763 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04006764 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04006765 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00006766 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04006767 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04006768 .create_server = nfs4_create_server,
6769 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006770};
6771
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006772static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6773 .prefix = XATTR_NAME_NFSV4_ACL,
6774 .list = nfs4_xattr_list_nfs4_acl,
6775 .get = nfs4_xattr_get_nfs4_acl,
6776 .set = nfs4_xattr_set_nfs4_acl,
6777};
6778
6779const struct xattr_handler *nfs4_xattr_handlers[] = {
6780 &nfs4_xattr_nfs4_acl_handler,
6781 NULL
6782};
6783
Linus Torvalds1da177e2005-04-16 15:20:36 -07006784/*
6785 * Local variables:
6786 * c-basic-offset: 8
6787 * End:
6788 */