blob: 493f0f41c5547d94eecf12de33bc8acf1545c2f7 [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;
Trond Myklebust14516c32010-07-31 14:29:06 -0400422 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500423 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500424 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400425
Bryan Schumaker468f8612011-04-18 15:57:32 -0400426 /* don't increment the sequence number if the task wasn't sent */
427 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400428 goto out;
429
Trond Myklebust933602e2012-11-16 12:12:38 -0500430 slot = res->sr_slot;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500431 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500432
Trond Myklebustac20d162012-12-15 15:36:07 -0500433 if (slot->interrupted) {
434 slot->interrupted = 0;
435 interrupted = true;
436 }
437
Andy Adamson691daf32009-12-04 15:55:39 -0500438 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400439 switch (res->sr_status) {
440 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400441 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust933602e2012-11-16 12:12:38 -0500442 ++slot->seq_nr;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500443 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500444 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500445 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500446 if (res->sr_status_flags != 0)
447 nfs4_schedule_lease_recovery(clp);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500448 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400449 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500450 case 1:
451 /*
452 * sr_status remains 1 if an RPC level error occurred.
453 * The server may or may not have processed the sequence
454 * operation..
455 * Mark the slot as having hosted an interrupted RPC call.
456 */
457 slot->interrupted = 1;
458 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400459 case -NFS4ERR_DELAY:
460 /* The server detected a resend of the RPC call and
461 * returned NFS4ERR_DELAY as per Section 2.10.6.2
462 * of RFC5661.
463 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500464 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400465 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500466 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500467 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400468 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500469 case -NFS4ERR_BADSLOT:
470 /*
471 * The slot id we used was probably retired. Try again
472 * using a different slot id.
473 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500474 goto retry_nowait;
475 case -NFS4ERR_SEQ_MISORDERED:
476 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500477 * Was the last operation on this sequence interrupted?
478 * If so, retry after bumping the sequence number.
479 */
480 if (interrupted) {
481 ++slot->seq_nr;
482 goto retry_nowait;
483 }
484 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500485 * Could this slot have been previously retired?
486 * If so, then the server may be expecting seq_nr = 1!
487 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500488 if (slot->seq_nr != 1) {
489 slot->seq_nr = 1;
490 goto retry_nowait;
491 }
492 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500493 case -NFS4ERR_SEQ_FALSE_RETRY:
494 ++slot->seq_nr;
495 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400496 default:
497 /* Just update the slot sequence no. */
Trond Myklebust933602e2012-11-16 12:12:38 -0500498 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400499 }
500out:
501 /* The session may be reset by one of the error handlers. */
502 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400503 nfs41_sequence_free_slot(res);
Trond Myklebust85563072012-12-11 10:31:12 -0500504 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500505retry_nowait:
506 if (rpc_restart_call_prepare(task)) {
507 task->tk_status = 0;
508 ret = 0;
509 }
510 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400511out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400512 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400513 goto out;
514 rpc_delay(task, NFS4_POLL_RETRY_MAX);
515 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400516}
517
Trond Myklebust14516c32010-07-31 14:29:06 -0400518static int nfs4_sequence_done(struct rpc_task *task,
519 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400520{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500521 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400522 return 1;
523 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400524}
525
Trond Myklebust9d12b212012-01-17 22:04:25 -0500526static void nfs41_init_sequence(struct nfs4_sequence_args *args,
527 struct nfs4_sequence_res *res, int cache_reply)
528{
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500529 args->sa_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500530 args->sa_cache_this = 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400531 args->sa_privileged = 0;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500532 if (cache_reply)
533 args->sa_cache_this = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500534 res->sr_slot = NULL;
535}
536
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400537static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
538{
539 args->sa_privileged = 1;
540}
541
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000542int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400543 struct nfs4_sequence_args *args,
544 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400545 struct rpc_task *task)
546{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400547 struct nfs4_slot *slot;
548 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400549
550 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400551 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400552 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400553 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400554
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400555 tbl = &session->fc_slot_table;
556
Trond Myklebust69d206b2012-11-22 13:21:02 -0500557 task->tk_timeout = 0;
558
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400559 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400560 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400561 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500562 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500563 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400564 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400565 }
566
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500567 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500568 if (IS_ERR(slot)) {
569 /* If out of memory, try again in 1/4 second */
570 if (slot == ERR_PTR(-ENOMEM))
571 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400572 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400573 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400574 }
575 spin_unlock(&tbl->slot_tbl_lock);
576
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500577 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400578
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500579 dprintk("<-- %s slotid=%d seqid=%d\n", __func__,
580 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400581
Benny Halevydfb4f3092010-09-24 09:17:01 -0400582 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500583 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400584 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400585 /*
586 * sr_status is only set in decode_sequence, and so will remain
587 * set to 1 if an rpc level failure occurs.
588 */
589 res->sr_status = 1;
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400590out_success:
591 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400592 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400593out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400594 /* Privileged tasks are queued with top priority */
595 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400596 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
597 NULL, RPC_PRIORITY_PRIVILEGED);
598 else
599 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400600 spin_unlock(&tbl->slot_tbl_lock);
601 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400602}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000603EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400604
Trond Myklebust035168a2010-06-16 09:52:26 -0400605int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400606 struct nfs4_sequence_args *args,
607 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400608 struct rpc_task *task)
609{
Trond Myklebust035168a2010-06-16 09:52:26 -0400610 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400611 int ret = 0;
612
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400613 if (session == NULL) {
614 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400615 goto out;
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400616 }
Trond Myklebust035168a2010-06-16 09:52:26 -0400617
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500618 dprintk("--> %s clp %p session %p sr_slot %d\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400619 __func__, session->clp, session, res->sr_slot ?
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500620 res->sr_slot->slot_nr : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400621
Trond Myklebust9d12b212012-01-17 22:04:25 -0500622 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400623out:
624 dprintk("<-- %s status=%d\n", __func__, ret);
625 return ret;
626}
627
628struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400629 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400630 struct nfs4_sequence_args *seq_args;
631 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400632};
633
634static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
635{
636 struct nfs41_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400637 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400638
Trond Myklebust035168a2010-06-16 09:52:26 -0400639 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
640
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400641 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400642}
643
Andy Adamson69ab40c2009-04-01 09:22:19 -0400644static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
645{
646 struct nfs41_call_sync_data *data = calldata;
647
Trond Myklebust14516c32010-07-31 14:29:06 -0400648 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400649}
650
Trond Myklebust17280172012-03-11 13:11:00 -0400651static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400652 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400653 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400654};
655
Bryan Schumaker7c513052011-03-24 17:12:24 +0000656static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
657 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400658 struct rpc_message *msg,
659 struct nfs4_sequence_args *args,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400660 struct nfs4_sequence_res *res)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400661{
662 int ret;
663 struct rpc_task *task;
664 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400665 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400666 .seq_args = args,
667 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400668 };
669 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000670 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400671 .rpc_message = msg,
672 .callback_ops = &nfs41_call_sync_ops,
673 .callback_data = &data
674 };
675
Andy Adamsonce5039c2009-04-01 09:22:13 -0400676 task = rpc_run_task(&task_setup);
677 if (IS_ERR(task))
678 ret = PTR_ERR(task);
679 else {
680 ret = task->tk_status;
681 rpc_put_task(task);
682 }
683 return ret;
684}
685
Trond Myklebustdf896452010-06-16 09:52:26 -0400686#else
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400687static
Trond Myklebust9d12b212012-01-17 22:04:25 -0500688void nfs41_init_sequence(struct nfs4_sequence_args *args,
689 struct nfs4_sequence_res *res, int cache_reply)
690{
691}
692
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400693static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
694{
695}
696
697
Trond Myklebust14516c32010-07-31 14:29:06 -0400698static int nfs4_sequence_done(struct rpc_task *task,
699 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400700{
Trond Myklebust14516c32010-07-31 14:29:06 -0400701 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400702}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400703#endif /* CONFIG_NFS_V4_1 */
704
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400705static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000706int _nfs4_call_sync(struct rpc_clnt *clnt,
707 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400708 struct rpc_message *msg,
709 struct nfs4_sequence_args *args,
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400710 struct nfs4_sequence_res *res)
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400711{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000712 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400713}
714
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400715static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000716int nfs4_call_sync(struct rpc_clnt *clnt,
717 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000718 struct rpc_message *msg,
719 struct nfs4_sequence_args *args,
720 struct nfs4_sequence_res *res,
721 int cache_reply)
722{
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400723 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000724 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400725 args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000726}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400727
Trond Myklebust38478b22006-05-25 01:40:57 -0400728static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729{
Trond Myklebust38478b22006-05-25 01:40:57 -0400730 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Trond Myklebust38478b22006-05-25 01:40:57 -0400732 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400733 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700734 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400735 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700736 dir->i_version = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400737 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738}
739
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100740struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400741 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100742 struct nfs_openargs o_arg;
743 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100744 struct nfs_open_confirmargs c_arg;
745 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500746 struct nfs4_string owner_name;
747 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100748 struct nfs_fattr f_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100749 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400750 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100751 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400752 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100753 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100754 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400755 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100756 int rpc_status;
757 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100758};
759
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400760
761static void nfs4_init_opendata_res(struct nfs4_opendata *p)
762{
763 p->o_res.f_attr = &p->f_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400764 p->o_res.seqid = p->o_arg.seqid;
765 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400766 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400767 p->o_res.access_request = p->o_arg.access;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400768 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500769 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400770}
771
Al Viro82a2c1b2011-06-22 18:30:55 -0400772static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500773 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400774 const struct iattr *attrs,
775 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100776{
Al Viro82a2c1b2011-06-22 18:30:55 -0400777 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100778 struct inode *dir = parent->d_inode;
779 struct nfs_server *server = NFS_SERVER(dir);
780 struct nfs4_opendata *p;
781
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400782 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100783 if (p == NULL)
784 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400785 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100786 if (p->o_arg.seqid == NULL)
787 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400788 nfs_sb_active(dentry->d_sb);
789 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100790 p->dir = parent;
791 p->owner = sp;
792 atomic_inc(&sp->so_count);
793 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500794 p->o_arg.open_flags = flags;
795 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700796 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
797 * will return permission denied for all bits until close */
798 if (!(flags & O_EXCL)) {
799 /* ask server to check for all possible rights as results
800 * are cached */
801 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
802 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700803 }
David Howells7539bba2006-08-22 20:06:09 -0400804 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400805 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
806 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400807 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100808 p->o_arg.server = server;
809 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust1549210f2012-06-05 09:16:47 -0400810 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100811 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust536e43d2012-01-17 22:04:26 -0500812 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -0500813 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400814
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100815 p->o_arg.u.attrs = &p->attrs;
816 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500817
818 verf[0] = jiffies;
819 verf[1] = current->pid;
820 memcpy(p->o_arg.u.verifier.data, verf,
821 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100822 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100823 p->c_arg.fh = &p->o_res.fh;
824 p->c_arg.stateid = &p->o_res.stateid;
825 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400826 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400827 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100828 return p;
829err_free:
830 kfree(p);
831err:
832 dput(parent);
833 return NULL;
834}
835
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400836static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100837{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400838 struct nfs4_opendata *p = container_of(kref,
839 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400840 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400841
842 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400843 if (p->state != NULL)
844 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400845 nfs4_put_state_owner(p->owner);
846 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400847 dput(p->dentry);
848 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500849 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400850 kfree(p);
851}
852
853static void nfs4_opendata_put(struct nfs4_opendata *p)
854{
855 if (p != NULL)
856 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100857}
858
Trond Myklebust06f814a2006-01-03 09:55:07 +0100859static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
860{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100861 int ret;
862
Trond Myklebust06f814a2006-01-03 09:55:07 +0100863 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100864 return ret;
865}
866
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500867static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400868{
869 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500870
Trond Myklebust536e43d2012-01-17 22:04:26 -0500871 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500872 goto out;
873 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400874 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500875 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
876 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400877 break;
878 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500879 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
880 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400881 break;
882 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500883 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
884 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400885 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500886out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400887 return ret;
888}
889
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500890static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400891{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500892 if (delegation == NULL)
893 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500894 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400895 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500896 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400897 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500898 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400899 return 1;
900}
901
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500902static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100903{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500904 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100905 case FMODE_WRITE:
906 state->n_wronly++;
907 break;
908 case FMODE_READ:
909 state->n_rdonly++;
910 break;
911 case FMODE_READ|FMODE_WRITE:
912 state->n_rdwr++;
913 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500914 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100915}
916
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500917static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400918{
919 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -0500920 nfs4_stateid_copy(&state->stateid, stateid);
921 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500922 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400923 case FMODE_READ:
924 set_bit(NFS_O_RDONLY_STATE, &state->flags);
925 break;
926 case FMODE_WRITE:
927 set_bit(NFS_O_WRONLY_STATE, &state->flags);
928 break;
929 case FMODE_READ|FMODE_WRITE:
930 set_bit(NFS_O_RDWR_STATE, &state->flags);
931 }
932}
933
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500934static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400935{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400936 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500937 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400938 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400939}
940
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500941static 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 -0700942{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400943 /*
944 * Protect the call to nfs4_state_set_mode_locked and
945 * serialise the stateid update
946 */
947 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400948 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -0500949 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -0400950 set_bit(NFS_DELEGATED_STATE, &state->flags);
951 }
952 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500953 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400954 write_sequnlock(&state->seqlock);
955 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500956 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700957 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958}
959
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500960static 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 -0500961{
962 struct nfs_inode *nfsi = NFS_I(state->inode);
963 struct nfs_delegation *deleg_cur;
964 int ret = 0;
965
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500966 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500967
968 rcu_read_lock();
969 deleg_cur = rcu_dereference(nfsi->delegation);
970 if (deleg_cur == NULL)
971 goto no_delegation;
972
973 spin_lock(&deleg_cur->lock);
974 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500975 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500976 goto no_delegation_unlock;
977
978 if (delegation == NULL)
979 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -0500980 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -0500981 goto no_delegation_unlock;
982
Trond Myklebustb7391f42008-12-23 15:21:52 -0500983 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500984 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500985 ret = 1;
986no_delegation_unlock:
987 spin_unlock(&deleg_cur->lock);
988no_delegation:
989 rcu_read_unlock();
990
991 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500992 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500993 ret = 1;
994 }
995
996 return ret;
997}
998
999
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001000static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001001{
1002 struct nfs_delegation *delegation;
1003
1004 rcu_read_lock();
1005 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001006 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001007 rcu_read_unlock();
1008 return;
1009 }
1010 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001011 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001012}
1013
Trond Myklebust6ee41262007-07-08 14:11:36 -04001014static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001015{
1016 struct nfs4_state *state = opendata->state;
1017 struct nfs_inode *nfsi = NFS_I(state->inode);
1018 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -05001019 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001020 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001021 nfs4_stateid stateid;
1022 int ret = -EAGAIN;
1023
Trond Myklebustaac00a82007-07-05 19:02:21 -04001024 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001025 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001026 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001027 if (can_open_cached(state, fmode, open_mode)) {
1028 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001029 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001030 goto out_return_state;
1031 }
1032 spin_unlock(&state->owner->so_lock);
1033 }
Trond Myklebust34310432008-12-23 15:21:38 -05001034 rcu_read_lock();
1035 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001036 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001037 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001038 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001039 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001040 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001041 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001042 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001043 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001044 if (ret != 0)
1045 goto out;
1046 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001047
1048 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001049 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001050 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001051 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001052out:
1053 return ERR_PTR(ret);
1054out_return_state:
1055 atomic_inc(&state->count);
1056 return state;
1057}
1058
Andy Adamsone23008e2012-10-02 21:07:32 -04001059static void
1060nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1061{
1062 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1063 struct nfs_delegation *delegation;
1064 int delegation_flags = 0;
1065
1066 rcu_read_lock();
1067 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1068 if (delegation)
1069 delegation_flags = delegation->flags;
1070 rcu_read_unlock();
1071 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1072 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1073 "returning a delegation for "
1074 "OPEN(CLAIM_DELEGATE_CUR)\n",
1075 clp->cl_hostname);
1076 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1077 nfs_inode_set_delegation(state->inode,
1078 data->owner->so_cred,
1079 &data->o_res);
1080 else
1081 nfs_inode_reclaim_delegation(state->inode,
1082 data->owner->so_cred,
1083 &data->o_res);
1084}
1085
1086/*
1087 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1088 * and update the nfs4_state.
1089 */
1090static struct nfs4_state *
1091_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1092{
1093 struct inode *inode = data->state->inode;
1094 struct nfs4_state *state = data->state;
1095 int ret;
1096
1097 if (!data->rpc_done) {
1098 ret = data->rpc_status;
1099 goto err;
1100 }
1101
1102 ret = -ESTALE;
1103 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1104 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1105 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1106 goto err;
1107
1108 ret = -ENOMEM;
1109 state = nfs4_get_open_state(inode, data->owner);
1110 if (state == NULL)
1111 goto err;
1112
1113 ret = nfs_refresh_inode(inode, &data->f_attr);
1114 if (ret)
1115 goto err;
1116
1117 if (data->o_res.delegation_type != 0)
1118 nfs4_opendata_check_deleg(data, state);
1119 update_open_stateid(state, &data->o_res.stateid, NULL,
1120 data->o_arg.fmode);
1121
1122 return state;
1123err:
1124 return ERR_PTR(ret);
1125
1126}
1127
1128static struct nfs4_state *
1129_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001130{
1131 struct inode *inode;
1132 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001133 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001134
Trond Myklebustaac00a82007-07-05 19:02:21 -04001135 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001136 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001137 goto out;
1138 }
1139
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001140 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001141 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001142 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001143 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001144 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001145 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001146 goto err;
1147 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001148 state = nfs4_get_open_state(inode, data->owner);
1149 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001150 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001151 if (data->o_res.delegation_type != 0)
1152 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001153 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001154 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001155 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001156out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001157 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001158err_put_inode:
1159 iput(inode);
1160err:
1161 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001162}
1163
Andy Adamsone23008e2012-10-02 21:07:32 -04001164static struct nfs4_state *
1165nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1166{
1167 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1168 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1169 return _nfs4_opendata_to_nfs4_state(data);
1170}
1171
Trond Myklebust864472e2006-01-03 09:55:15 +01001172static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1173{
1174 struct nfs_inode *nfsi = NFS_I(state->inode);
1175 struct nfs_open_context *ctx;
1176
1177 spin_lock(&state->inode->i_lock);
1178 list_for_each_entry(ctx, &nfsi->open_files, list) {
1179 if (ctx->state != state)
1180 continue;
1181 get_nfs_open_context(ctx);
1182 spin_unlock(&state->inode->i_lock);
1183 return ctx;
1184 }
1185 spin_unlock(&state->inode->i_lock);
1186 return ERR_PTR(-ENOENT);
1187}
1188
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001189static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1190{
1191 struct nfs4_opendata *opendata;
1192
Al Viro3d4ff432011-06-22 18:40:12 -04001193 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001194 if (opendata == NULL)
1195 return ERR_PTR(-ENOMEM);
1196 opendata->state = state;
1197 atomic_inc(&state->count);
1198 return opendata;
1199}
1200
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001201static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001202{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001203 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001204 int ret;
1205
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001206 opendata->o_arg.open_flags = 0;
1207 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001208 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1209 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1210 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001211 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001212 if (ret != 0)
1213 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001214 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001215 if (IS_ERR(newstate))
1216 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001217 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001218 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001219 return 0;
1220}
1221
1222static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1223{
Trond Myklebust864472e2006-01-03 09:55:15 +01001224 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001225 int ret;
1226
1227 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001228 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001229 smp_rmb();
1230 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001231 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001232 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001233 if (ret != 0)
1234 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001235 if (newstate != state)
1236 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001237 }
1238 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001239 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001240 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001241 if (ret != 0)
1242 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001243 if (newstate != state)
1244 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001245 }
1246 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001247 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001248 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001249 if (ret != 0)
1250 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001251 if (newstate != state)
1252 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001253 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001254 /*
1255 * We may have performed cached opens for all three recoveries.
1256 * Check if we need to update the current stateid.
1257 */
1258 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001259 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001260 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001261 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001262 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001263 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001264 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001265 return 0;
1266}
1267
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268/*
1269 * OPEN_RECLAIM:
1270 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001272static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001274 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001275 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001276 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 int status;
1278
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001279 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1280 if (IS_ERR(opendata))
1281 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001282 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1283 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001284 rcu_read_lock();
1285 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001286 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001287 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001288 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001289 opendata->o_arg.u.delegation_type = delegation_type;
1290 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001291 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 return status;
1293}
1294
Trond Myklebust539cd032007-06-05 11:46:42 -04001295static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296{
1297 struct nfs_server *server = NFS_SERVER(state->inode);
1298 struct nfs4_exception exception = { };
1299 int err;
1300 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001301 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001302 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001303 break;
1304 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 } while (exception.retry);
1306 return err;
1307}
1308
Trond Myklebust864472e2006-01-03 09:55:15 +01001309static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1310{
1311 struct nfs_open_context *ctx;
1312 int ret;
1313
1314 ctx = nfs4_state_find_open_context(state);
1315 if (IS_ERR(ctx))
1316 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001317 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001318 put_nfs_open_context(ctx);
1319 return ret;
1320}
1321
Trond Myklebust13437e12007-07-06 15:10:43 -04001322static 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 -07001323{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001324 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001325 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001327 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1328 if (IS_ERR(opendata))
1329 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001330 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebustf597c532012-03-04 18:13:56 -05001331 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust864472e2006-01-03 09:55:15 +01001332 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001333 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001334 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335}
1336
Trond Myklebust13437e12007-07-06 15:10:43 -04001337int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338{
1339 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001340 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 int err;
1342 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001343 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 switch (err) {
1345 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001346 case -ENOENT:
1347 case -ESTALE:
1348 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001349 case -NFS4ERR_BADSESSION:
1350 case -NFS4ERR_BADSLOT:
1351 case -NFS4ERR_BAD_HIGH_SLOT:
1352 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1353 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04001354 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001355 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 case -NFS4ERR_STALE_CLIENTID:
1357 case -NFS4ERR_STALE_STATEID:
1358 case -NFS4ERR_EXPIRED:
1359 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001360 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001361 goto out;
1362 case -ERESTARTSYS:
1363 /*
1364 * The show must go on: exit, but mark the
1365 * stateid as needing recovery.
1366 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001367 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001368 case -NFS4ERR_ADMIN_REVOKED:
1369 case -NFS4ERR_BAD_STATEID:
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001370 nfs_inode_find_state_and_recover(state->inode,
1371 stateid);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001372 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001373 case -ENOMEM:
1374 err = 0;
1375 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 }
1377 err = nfs4_handle_exception(server, err, &exception);
1378 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001379out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 return err;
1381}
1382
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001383static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1384{
1385 struct nfs4_opendata *data = calldata;
1386
1387 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001388 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001389 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001390 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001391 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001392 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001393 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001394}
1395
1396static void nfs4_open_confirm_release(void *calldata)
1397{
1398 struct nfs4_opendata *data = calldata;
1399 struct nfs4_state *state = NULL;
1400
1401 /* If this request hasn't been cancelled, do nothing */
1402 if (data->cancelled == 0)
1403 goto out_free;
1404 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001405 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001406 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001407 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001408 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001409 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001410out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001411 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001412}
1413
1414static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001415 .rpc_call_done = nfs4_open_confirm_done,
1416 .rpc_release = nfs4_open_confirm_release,
1417};
1418
1419/*
1420 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1421 */
1422static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1423{
1424 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1425 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001426 struct rpc_message msg = {
1427 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1428 .rpc_argp = &data->c_arg,
1429 .rpc_resp = &data->c_res,
1430 .rpc_cred = data->owner->so_cred,
1431 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001432 struct rpc_task_setup task_setup_data = {
1433 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001434 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001435 .callback_ops = &nfs4_open_confirm_ops,
1436 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001437 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001438 .flags = RPC_TASK_ASYNC,
1439 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 int status;
1441
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001442 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001443 data->rpc_done = 0;
1444 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001445 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001446 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001447 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001448 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001449 status = nfs4_wait_for_completion_rpc_task(task);
1450 if (status != 0) {
1451 data->cancelled = 1;
1452 smp_wmb();
1453 } else
1454 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001455 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 return status;
1457}
1458
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001459static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001461 struct nfs4_opendata *data = calldata;
1462 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001463
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001464 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1465 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001466 /*
1467 * Check if we still need to send an OPEN call, or if we can use
1468 * a delegation instead.
1469 */
1470 if (data->state != NULL) {
1471 struct nfs_delegation *delegation;
1472
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001473 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001474 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001475 rcu_read_lock();
1476 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001477 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1478 can_open_delegated(delegation, data->o_arg.fmode))
1479 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001480 rcu_read_unlock();
1481 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001482 /* Update client id. */
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001483 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001484 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001485 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Andy Adamsone23008e2012-10-02 21:07:32 -04001486 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001487 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1488 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001489 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001490 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001491 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001492 &data->o_res.seq_res,
1493 task) != 0)
1494 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001495 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001496unlock_no_action:
1497 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001498out_no_action:
1499 task->tk_action = NULL;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05001500 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001501}
1502
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001503static void nfs4_open_done(struct rpc_task *task, void *calldata)
1504{
1505 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001507 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001508
Trond Myklebust14516c32010-07-31 14:29:06 -04001509 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1510 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001511
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001512 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001513 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1514 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001515 case S_IFREG:
1516 break;
1517 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001518 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001519 break;
1520 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001521 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001522 break;
1523 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001524 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001525 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001526 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001527 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001528 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1529 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001530 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001531 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001532}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001533
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001534static void nfs4_open_release(void *calldata)
1535{
1536 struct nfs4_opendata *data = calldata;
1537 struct nfs4_state *state = NULL;
1538
1539 /* If this request hasn't been cancelled, do nothing */
1540 if (data->cancelled == 0)
1541 goto out_free;
1542 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001543 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001544 goto out_free;
1545 /* In case we need an open_confirm, no cleanup! */
1546 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1547 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001548 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001549 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001550 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001551out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001552 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001553}
1554
1555static const struct rpc_call_ops nfs4_open_ops = {
1556 .rpc_call_prepare = nfs4_open_prepare,
1557 .rpc_call_done = nfs4_open_done,
1558 .rpc_release = nfs4_open_release,
1559};
1560
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001561static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001562{
1563 struct inode *dir = data->dir->d_inode;
1564 struct nfs_server *server = NFS_SERVER(dir);
1565 struct nfs_openargs *o_arg = &data->o_arg;
1566 struct nfs_openres *o_res = &data->o_res;
1567 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001568 struct rpc_message msg = {
1569 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1570 .rpc_argp = o_arg,
1571 .rpc_resp = o_res,
1572 .rpc_cred = data->owner->so_cred,
1573 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001574 struct rpc_task_setup task_setup_data = {
1575 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001576 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001577 .callback_ops = &nfs4_open_ops,
1578 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001579 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001580 .flags = RPC_TASK_ASYNC,
1581 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001582 int status;
1583
Trond Myklebust9d12b212012-01-17 22:04:25 -05001584 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001585 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001586 data->rpc_done = 0;
1587 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001588 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001589 if (isrecover)
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001590 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04001591 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001592 if (IS_ERR(task))
1593 return PTR_ERR(task);
1594 status = nfs4_wait_for_completion_rpc_task(task);
1595 if (status != 0) {
1596 data->cancelled = 1;
1597 smp_wmb();
1598 } else
1599 status = data->rpc_status;
1600 rpc_put_task(task);
1601
1602 return status;
1603}
1604
1605static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1606{
1607 struct inode *dir = data->dir->d_inode;
1608 struct nfs_openres *o_res = &data->o_res;
1609 int status;
1610
1611 status = nfs4_run_open_task(data, 1);
1612 if (status != 0 || !data->rpc_done)
1613 return status;
1614
Trond Myklebust6926afd2012-01-07 13:22:46 -05001615 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1616
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001617 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1618 status = _nfs4_proc_open_confirm(data);
1619 if (status != 0)
1620 return status;
1621 }
1622
1623 return status;
1624}
1625
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001626static int nfs4_opendata_access(struct rpc_cred *cred,
1627 struct nfs4_opendata *opendata,
1628 struct nfs4_state *state, fmode_t fmode)
1629{
1630 struct nfs_access_entry cache;
1631 u32 mask;
1632
1633 /* access call failed or for some reason the server doesn't
1634 * support any access modes -- defer access call until later */
1635 if (opendata->o_res.access_supported == 0)
1636 return 0;
1637
1638 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001639 /* don't check MAY_WRITE - a newly created file may not have
1640 * write mode bits, but POSIX allows the creating process to write */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001641 if (fmode & FMODE_READ)
1642 mask |= MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001643 if (fmode & FMODE_EXEC)
1644 mask |= MAY_EXEC;
1645
1646 cache.cred = cred;
1647 cache.jiffies = jiffies;
1648 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1649 nfs_access_add_cache(state->inode, &cache);
1650
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001651 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001652 return 0;
1653
1654 /* even though OPEN succeeded, access is denied. Close the file */
1655 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001656 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001657}
1658
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001659/*
1660 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1661 */
1662static int _nfs4_proc_open(struct nfs4_opendata *data)
1663{
1664 struct inode *dir = data->dir->d_inode;
1665 struct nfs_server *server = NFS_SERVER(dir);
1666 struct nfs_openargs *o_arg = &data->o_arg;
1667 struct nfs_openres *o_res = &data->o_res;
1668 int status;
1669
1670 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001671 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001672 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001673 if (status != 0) {
1674 if (status == -NFS4ERR_BADNAME &&
1675 !(o_arg->open_flags & O_CREAT))
1676 return -ENOENT;
1677 return status;
1678 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001679
Trond Myklebust6926afd2012-01-07 13:22:46 -05001680 nfs_fattr_map_and_free_names(server, &data->f_attr);
1681
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001682 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001683 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001684 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1685 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001687 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001689 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 }
1691 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001692 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001693 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694}
1695
Andy Adamsond83217c2011-03-01 01:34:17 +00001696static int nfs4_recover_expired_lease(struct nfs_server *server)
1697{
1698 return nfs4_client_recover_expired_lease(server->nfs_client);
1699}
1700
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701/*
1702 * OPEN_EXPIRED:
1703 * reclaim state on the server after a network partition.
1704 * Assumes caller holds the appropriate lock
1705 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001706static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001708 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001709 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001711 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1712 if (IS_ERR(opendata))
1713 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001714 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001715 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001716 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001717 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001718 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719}
1720
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001721static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001722{
Trond Myklebust539cd032007-06-05 11:46:42 -04001723 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001724 struct nfs4_exception exception = { };
1725 int err;
1726
1727 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001728 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001729 switch (err) {
1730 default:
1731 goto out;
1732 case -NFS4ERR_GRACE:
1733 case -NFS4ERR_DELAY:
1734 nfs4_handle_exception(server, err, &exception);
1735 err = 0;
1736 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001737 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001738out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001739 return err;
1740}
1741
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1743{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001745 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
Trond Myklebust864472e2006-01-03 09:55:15 +01001747 ctx = nfs4_state_find_open_context(state);
1748 if (IS_ERR(ctx))
1749 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001750 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001751 put_nfs_open_context(ctx);
1752 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753}
1754
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001755#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001756static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001757{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001758 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001759 nfs4_stateid *stateid = &state->stateid;
1760 int status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001761
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001762 /* If a state reset has been done, test_stateid is unneeded */
1763 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1764 return;
1765
1766 status = nfs41_test_stateid(server, stateid);
1767 if (status != NFS_OK) {
1768 /* Free the stateid unless the server explicitly
1769 * informs us the stateid is unrecognized. */
1770 if (status != -NFS4ERR_BAD_STATEID)
1771 nfs41_free_stateid(server, stateid);
Bryan Schumaker69388672012-09-26 15:25:52 -04001772 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001773
Bryan Schumaker69388672012-09-26 15:25:52 -04001774 write_seqlock(&state->seqlock);
1775 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1776 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001777 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1778 }
1779}
1780
1781/**
1782 * nfs41_check_open_stateid - possibly free an open stateid
1783 *
1784 * @state: NFSv4 state for an inode
1785 *
1786 * Returns NFS_OK if recovery for this stateid is now finished.
1787 * Otherwise a negative NFS4ERR value is returned.
1788 */
1789static int nfs41_check_open_stateid(struct nfs4_state *state)
1790{
1791 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04001792 nfs4_stateid *stateid = &state->open_stateid;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001793 int status;
1794
1795 /* If a state reset has been done, test_stateid is unneeded */
1796 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1797 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1798 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1799 return -NFS4ERR_BAD_STATEID;
1800
1801 status = nfs41_test_stateid(server, stateid);
1802 if (status != NFS_OK) {
1803 /* Free the stateid unless the server explicitly
1804 * informs us the stateid is unrecognized. */
1805 if (status != -NFS4ERR_BAD_STATEID)
1806 nfs41_free_stateid(server, stateid);
1807
1808 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1809 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1810 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001811 }
1812 return status;
1813}
1814
1815static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1816{
Chuck Levereb64cf92012-07-11 16:30:05 -04001817 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001818
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001819 nfs41_clear_delegation_stateid(state);
1820 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04001821 if (status != NFS_OK)
1822 status = nfs4_open_expired(sp, state);
1823 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001824}
1825#endif
1826
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001828 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1829 * fields corresponding to attributes that were used to store the verifier.
1830 * Make sure we clobber those fields in the later setattr call
1831 */
1832static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1833{
1834 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1835 !(sattr->ia_valid & ATTR_ATIME_SET))
1836 sattr->ia_valid |= ATTR_ATIME;
1837
1838 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1839 !(sattr->ia_valid & ATTR_MTIME_SET))
1840 sattr->ia_valid |= ATTR_MTIME;
1841}
1842
1843/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001844 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 */
Andy Adamson82be4172012-05-23 05:02:35 -04001846static int _nfs4_do_open(struct inode *dir,
1847 struct dentry *dentry,
1848 fmode_t fmode,
1849 int flags,
1850 struct iattr *sattr,
1851 struct rpc_cred *cred,
1852 struct nfs4_state **res,
1853 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854{
1855 struct nfs4_state_owner *sp;
1856 struct nfs4_state *state = NULL;
1857 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001858 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001859 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
1861 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001863 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1864 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1866 goto out_err;
1867 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001868 status = nfs4_recover_expired_lease(server);
1869 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001870 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001871 if (dentry->d_inode != NULL)
1872 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001873 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001874 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001875 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001876 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877
Andy Adamson82be4172012-05-23 05:02:35 -04001878 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
1879 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
1880 if (!opendata->f_attr.mdsthreshold)
1881 goto err_opendata_put;
Trond Myklebust1549210f2012-06-05 09:16:47 -04001882 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04001883 }
Al Viro82a2c1b2011-06-22 18:30:55 -04001884 if (dentry->d_inode != NULL)
1885 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001886
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001887 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001889 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001891 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001892 status = PTR_ERR(state);
1893 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001894 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001895 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001896 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001897
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001898 status = nfs4_opendata_access(cred, opendata, state, fmode);
1899 if (status != 0)
1900 goto err_opendata_put;
1901
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001902 if (opendata->o_arg.open_flags & O_EXCL) {
1903 nfs4_exclusive_attrset(opendata, sattr);
1904
1905 nfs_fattr_init(opendata->o_res.f_attr);
1906 status = nfs4_do_setattr(state->inode, cred,
1907 opendata->o_res.f_attr, sattr,
1908 state);
1909 if (status == 0)
1910 nfs_setattr_update_inode(state->inode, sattr);
1911 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1912 }
Andy Adamson82be4172012-05-23 05:02:35 -04001913
1914 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
1915 *ctx_th = opendata->f_attr.mdsthreshold;
1916 else
1917 kfree(opendata->f_attr.mdsthreshold);
1918 opendata->f_attr.mdsthreshold = NULL;
1919
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001920 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 *res = state;
1923 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001924err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04001925 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001926 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001927err_put_state_owner:
1928 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 *res = NULL;
1931 return status;
1932}
1933
1934
Andy Adamson82be4172012-05-23 05:02:35 -04001935static struct nfs4_state *nfs4_do_open(struct inode *dir,
1936 struct dentry *dentry,
1937 fmode_t fmode,
1938 int flags,
1939 struct iattr *sattr,
1940 struct rpc_cred *cred,
1941 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942{
1943 struct nfs4_exception exception = { };
1944 struct nfs4_state *res;
1945 int status;
1946
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001947 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 do {
Andy Adamson82be4172012-05-23 05:02:35 -04001949 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
1950 &res, ctx_th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 if (status == 0)
1952 break;
1953 /* NOTE: BAD_SEQID means the server and client disagree about the
1954 * book-keeping w.r.t. state-changing operations
1955 * (OPEN/CLOSE/LOCK/LOCKU...)
1956 * It is actually a sign of a bug on the client or on the server.
1957 *
1958 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001959 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 * have unhashed the old state_owner for us, and that we can
1961 * therefore safely retry using a new one. We should still warn
1962 * the user though...
1963 */
1964 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04001965 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001966 " returned a bad sequence-id error!\n",
1967 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 exception.retry = 1;
1969 continue;
1970 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001971 /*
1972 * BAD_STATEID on OPEN means that the server cancelled our
1973 * state before it received the OPEN_CONFIRM.
1974 * Recover by retrying the request as per the discussion
1975 * on Page 181 of RFC3530.
1976 */
1977 if (status == -NFS4ERR_BAD_STATEID) {
1978 exception.retry = 1;
1979 continue;
1980 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001981 if (status == -EAGAIN) {
1982 /* We must have found a delegation */
1983 exception.retry = 1;
1984 continue;
1985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1987 status, &exception));
1988 } while (exception.retry);
1989 return res;
1990}
1991
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001992static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1993 struct nfs_fattr *fattr, struct iattr *sattr,
1994 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001996 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001998 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 .iap = sattr,
2000 .server = server,
2001 .bitmask = server->attr_bitmask,
2002 };
2003 struct nfs_setattrres res = {
2004 .fattr = fattr,
2005 .server = server,
2006 };
2007 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002008 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2009 .rpc_argp = &arg,
2010 .rpc_resp = &res,
2011 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002013 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04002014 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
Trond Myklebust0e574af2005-10-27 22:12:38 -04002016 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
Trond Myklebust4fc87962012-03-08 17:42:01 -05002018 if (state != NULL) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002019 struct nfs_lockowner lockowner = {
2020 .l_owner = current->files,
2021 .l_pid = current->tgid,
2022 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002023 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002024 &lockowner);
Trond Myklebust4fc87962012-03-08 17:42:01 -05002025 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2026 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002027 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002028 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002029 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030
Bryan Schumaker7c513052011-03-24 17:12:24 +00002031 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002032 if (status == 0 && state != NULL)
2033 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002034 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035}
2036
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002037static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2038 struct nfs_fattr *fattr, struct iattr *sattr,
2039 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002041 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002042 struct nfs4_exception exception = {
2043 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002044 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002045 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 int err;
2047 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04002048 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2049 switch (err) {
2050 case -NFS4ERR_OPENMODE:
2051 if (state && !(state->state & FMODE_WRITE)) {
2052 err = -EBADF;
2053 if (sattr->ia_valid & ATTR_OPEN)
2054 err = -EACCES;
2055 goto out;
2056 }
2057 }
2058 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002060out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 return err;
2062}
2063
2064struct nfs4_closedata {
2065 struct inode *inode;
2066 struct nfs4_state *state;
2067 struct nfs_closeargs arg;
2068 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002069 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002070 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002071 bool roc;
2072 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073};
2074
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002075static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002076{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002077 struct nfs4_closedata *calldata = data;
2078 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002079 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002080
Fred Isamanf7e89172011-01-06 11:36:32 +00002081 if (calldata->roc)
2082 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002083 nfs4_put_open_state(calldata->state);
2084 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002085 nfs4_put_state_owner(sp);
Weston Andros Adamson324d0032012-10-30 17:01:39 -04002086 nfs_sb_deactive_async(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002087 kfree(calldata);
2088}
2089
Trond Myklebust88069f72009-12-08 08:33:16 -05002090static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2091 fmode_t fmode)
2092{
2093 spin_lock(&state->owner->so_lock);
2094 if (!(fmode & FMODE_READ))
2095 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2096 if (!(fmode & FMODE_WRITE))
2097 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2098 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2099 spin_unlock(&state->owner->so_lock);
2100}
2101
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002102static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002104 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 struct nfs_server *server = NFS_SERVER(calldata->inode);
2107
Chuck Levera3ca5652012-03-01 17:00:40 -05002108 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002109 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2110 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 /* hmm. we are done with the inode, and in the process of freeing
2112 * the state_owner. we keep this around to process errors
2113 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 switch (task->tk_status) {
2115 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002116 if (calldata->roc)
2117 pnfs_roc_set_barrier(state->inode,
2118 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002119 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002120 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002121 nfs4_close_clear_stateid_flags(state,
2122 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 break;
2124 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002125 case -NFS4ERR_OLD_STATEID:
2126 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002128 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002129 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002131 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2132 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002134 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002135 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002136 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137}
2138
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002139static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002141 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002142 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002143 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002144 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002145
Chuck Levera3ca5652012-03-01 17:00:40 -05002146 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002147 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07002148 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04002149
Trond Myklebust88069f72009-12-08 08:33:16 -05002150 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2151 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002152 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002153 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002154 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002155 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002156 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2157 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2158 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002159 }
2160 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002161 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2162 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2163 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002164 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002165 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002166 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002167
2168 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002169 /* Note: exit _without_ calling nfs4_close_done */
2170 task->tk_action = NULL;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002171 nfs4_sequence_done(task, &calldata->res.seq_res);
Chuck Levera3ca5652012-03-01 17:00:40 -05002172 goto out;
Trond Myklebust95121352005-10-18 14:20:12 -07002173 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002174
Fred Isamanf7e89172011-01-06 11:36:32 +00002175 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002176 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002177 if (calldata->roc &&
Trond Myklebust7fdab062012-09-20 20:15:57 -04002178 pnfs_roc_drain(inode, &calldata->roc_barrier, task))
Chuck Levera3ca5652012-03-01 17:00:40 -05002179 goto out;
Fred Isamanf7e89172011-01-06 11:36:32 +00002180 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002181
Trond Myklebust516a6af2005-10-27 22:12:41 -04002182 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002183 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002184 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002185 &calldata->arg.seq_args,
2186 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002187 task) != 0)
2188 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002189out:
2190 dprintk("%s: done!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191}
2192
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002193static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002194 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002195 .rpc_call_done = nfs4_close_done,
2196 .rpc_release = nfs4_free_closedata,
2197};
2198
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199/*
2200 * It is possible for data to be read/written from a mem-mapped file
2201 * after the sys_close call (which hits the vfs layer as a flush).
2202 * This means that we can't safely call nfsv4 close on a file until
2203 * the inode is cleared. This in turn means that we are not good
2204 * NFSv4 citizens - we do not indicate to the server to update the file's
2205 * share state even when we are done with one of the three share
2206 * stateid's in the inode.
2207 *
2208 * NOTE: Caller must be holding the sp->so_owner semaphore!
2209 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002210int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002212 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002214 struct nfs4_state_owner *sp = state->owner;
2215 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002216 struct rpc_message msg = {
2217 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2218 .rpc_cred = state->owner->so_cred,
2219 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002220 struct rpc_task_setup task_setup_data = {
2221 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002222 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002223 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002224 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002225 .flags = RPC_TASK_ASYNC,
2226 };
Trond Myklebust95121352005-10-18 14:20:12 -07002227 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002229 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002231 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002232 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002233 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002235 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002236 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002238 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002239 if (calldata->arg.seqid == NULL)
2240 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002241 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002242 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002243 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002244 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002245 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002246 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002247 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002248
Trond Myklebust1174dd12010-12-21 10:52:24 -05002249 msg.rpc_argp = &calldata->arg;
2250 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002251 task_setup_data.callback_data = calldata;
2252 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002253 if (IS_ERR(task))
2254 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002255 status = 0;
2256 if (wait)
2257 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002258 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002259 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002260out_free_calldata:
2261 kfree(calldata);
2262out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002263 nfs4_put_open_state(state);
2264 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002265 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266}
2267
Trond Myklebust2b484292010-09-17 10:56:51 -04002268static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002269nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 struct nfs4_state *state;
2272
Trond Myklebust565277f2007-10-15 18:17:53 -04002273 /* Protect against concurrent sillydeletes */
Andy Adamson82be4172012-05-23 05:02:35 -04002274 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2275 ctx->cred, &ctx->mdsthreshold);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002276 if (IS_ERR(state))
2277 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002278 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002279 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280}
2281
Trond Myklebust1185a552009-12-03 15:54:02 -05002282static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002283{
2284 if (ctx->state == NULL)
2285 return;
2286 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002287 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002288 else
Al Viro643168c2011-06-22 18:20:23 -04002289 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002290}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291
2292static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2293{
Benny Halevy43652ad2009-04-01 09:21:54 -04002294 struct nfs4_server_caps_arg args = {
2295 .fhandle = fhandle,
2296 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 struct nfs4_server_caps_res res = {};
2298 struct rpc_message msg = {
2299 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002300 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 .rpc_resp = &res,
2302 };
2303 int status;
2304
Bryan Schumaker7c513052011-03-24 17:12:24 +00002305 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 if (status == 0) {
2307 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002308 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2309 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2310 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2311 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2312 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2314 server->caps |= NFS_CAP_ACLS;
2315 if (res.has_links != 0)
2316 server->caps |= NFS_CAP_HARDLINKS;
2317 if (res.has_symlinks != 0)
2318 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002319 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2320 server->caps |= NFS_CAP_FILEID;
2321 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2322 server->caps |= NFS_CAP_MODE;
2323 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2324 server->caps |= NFS_CAP_NLINK;
2325 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2326 server->caps |= NFS_CAP_OWNER;
2327 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2328 server->caps |= NFS_CAP_OWNER_GROUP;
2329 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2330 server->caps |= NFS_CAP_ATIME;
2331 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2332 server->caps |= NFS_CAP_CTIME;
2333 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2334 server->caps |= NFS_CAP_MTIME;
2335
Trond Myklebusta65318b2009-03-11 14:10:28 -04002336 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2337 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2338 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002340 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002342
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 return status;
2344}
2345
Trond Myklebust55a97592006-06-09 09:34:19 -04002346int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347{
2348 struct nfs4_exception exception = { };
2349 int err;
2350 do {
2351 err = nfs4_handle_exception(server,
2352 _nfs4_server_capabilities(server, fhandle),
2353 &exception);
2354 } while (exception.retry);
2355 return err;
2356}
2357
2358static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2359 struct nfs_fsinfo *info)
2360{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 struct nfs4_lookup_root_arg args = {
2362 .bitmask = nfs4_fattr_bitmap,
2363 };
2364 struct nfs4_lookup_res res = {
2365 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002366 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 .fh = fhandle,
2368 };
2369 struct rpc_message msg = {
2370 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2371 .rpc_argp = &args,
2372 .rpc_resp = &res,
2373 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002374
Trond Myklebust0e574af2005-10-27 22:12:38 -04002375 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002376 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377}
2378
2379static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2380 struct nfs_fsinfo *info)
2381{
2382 struct nfs4_exception exception = { };
2383 int err;
2384 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002385 err = _nfs4_lookup_root(server, fhandle, info);
2386 switch (err) {
2387 case 0:
2388 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002389 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002390 default:
2391 err = nfs4_handle_exception(server, err, &exception);
2392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002394out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 return err;
2396}
2397
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002398static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2399 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2400{
2401 struct rpc_auth *auth;
2402 int ret;
2403
2404 auth = rpcauth_create(flavor, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002405 if (IS_ERR(auth)) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002406 ret = -EIO;
2407 goto out;
2408 }
2409 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002410out:
2411 return ret;
2412}
2413
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002414static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002415 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002417 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002418 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002420 len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04002421 if (len < 0)
2422 return len;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002423
2424 for (i = 0; i < len; i++) {
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002425 /* AUTH_UNIX is the default flavor if none was specified,
2426 * thus has already been tried. */
2427 if (flav_array[i] == RPC_AUTH_UNIX)
2428 continue;
2429
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002430 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002431 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002432 continue;
2433 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002434 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002435 /*
2436 * -EACCESS could mean that the user doesn't have correct permissions
2437 * to access the mount. It could also mean that we tried to mount
2438 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2439 * existing mount programs don't handle -EACCES very well so it should
2440 * be mapped to -EPERM instead.
2441 */
2442 if (status == -EACCES)
2443 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002444 return status;
2445}
2446
2447/*
2448 * get the file handle for the "/" directory on the server
2449 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002450int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2451 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002452{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002453 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002454 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002455 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2456 /*
2457 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2458 * by nfs4_map_errors() as this function exits.
2459 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002460 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 if (status == 0)
2462 status = nfs4_server_capabilities(server, fhandle);
2463 if (status == 0)
2464 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002465 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466}
2467
Bryan Schumakerbae36242012-05-10 15:07:31 -04002468static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2469 struct nfs_fsinfo *info)
2470{
2471 int error;
2472 struct nfs_fattr *fattr = info->fattr;
2473
2474 error = nfs4_server_capabilities(server, mntfh);
2475 if (error < 0) {
2476 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2477 return error;
2478 }
2479
2480 error = nfs4_proc_getattr(server, mntfh, fattr);
2481 if (error < 0) {
2482 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2483 return error;
2484 }
2485
2486 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2487 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2488 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2489
2490 return error;
2491}
2492
Manoj Naik6b97fd32006-06-09 09:34:29 -04002493/*
2494 * Get locations and (maybe) other attributes of a referral.
2495 * Note that we'll actually follow the referral later when
2496 * we detect fsid mismatch in inode revalidation
2497 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002498static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2499 const struct qstr *name, struct nfs_fattr *fattr,
2500 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002501{
2502 int status = -ENOMEM;
2503 struct page *page = NULL;
2504 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002505
2506 page = alloc_page(GFP_KERNEL);
2507 if (page == NULL)
2508 goto out;
2509 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2510 if (locations == NULL)
2511 goto out;
2512
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002513 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002514 if (status != 0)
2515 goto out;
2516 /* Make sure server returned a different fsid for the referral */
2517 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002518 dprintk("%s: server did not return a different fsid for"
2519 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002520 status = -EIO;
2521 goto out;
2522 }
Andy Adamson533eb462011-06-13 18:25:56 -04002523 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2524 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002525
Andy Adamson533eb462011-06-13 18:25:56 -04002526 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002527 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002528 memset(fhandle, 0, sizeof(struct nfs_fh));
2529out:
2530 if (page)
2531 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002532 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002533 return status;
2534}
2535
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2537{
2538 struct nfs4_getattr_arg args = {
2539 .fh = fhandle,
2540 .bitmask = server->attr_bitmask,
2541 };
2542 struct nfs4_getattr_res res = {
2543 .fattr = fattr,
2544 .server = server,
2545 };
2546 struct rpc_message msg = {
2547 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2548 .rpc_argp = &args,
2549 .rpc_resp = &res,
2550 };
2551
Trond Myklebust0e574af2005-10-27 22:12:38 -04002552 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002553 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554}
2555
2556static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2557{
2558 struct nfs4_exception exception = { };
2559 int err;
2560 do {
2561 err = nfs4_handle_exception(server,
2562 _nfs4_proc_getattr(server, fhandle, fattr),
2563 &exception);
2564 } while (exception.retry);
2565 return err;
2566}
2567
2568/*
2569 * The file is not closed if it is opened due to the a request to change
2570 * the size of the file. The open call will not be needed once the
2571 * VFS layer lookup-intents are implemented.
2572 *
2573 * Close is called when the inode is destroyed.
2574 * If we haven't opened the file for O_WRONLY, we
2575 * need to in the size_change case to obtain a stateid.
2576 *
2577 * Got race?
2578 * Because OPEN is always done by name in nfsv4, it is
2579 * possible that we opened a different file by the same
2580 * name. We can recognize this race condition, but we
2581 * can't do anything about it besides returning an error.
2582 *
2583 * This will be fixed with VFS changes (lookup-intent).
2584 */
2585static int
2586nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2587 struct iattr *sattr)
2588{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002589 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002590 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002591 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 int status;
2593
Benny Halevy8a1636c2010-07-14 15:43:57 -04002594 if (pnfs_ld_layoutret_on_setattr(inode))
2595 pnfs_return_layout(inode);
2596
Trond Myklebust0e574af2005-10-27 22:12:38 -04002597 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598
Andy Adamson26699402012-05-30 16:12:24 -04002599 /* Deal with open(O_TRUNC) */
2600 if (sattr->ia_valid & ATTR_OPEN)
2601 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2602
2603 /* Optimization: if the end result is no change, don't RPC */
2604 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2605 return 0;
2606
Trond Myklebustd5308382005-11-04 15:33:38 -05002607 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002608 if (sattr->ia_valid & ATTR_FILE) {
2609 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002610
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002611 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002612 if (ctx) {
2613 cred = ctx->cred;
2614 state = ctx->state;
2615 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002616 }
2617
2618 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002619 if (status == 0)
2620 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 return status;
2622}
2623
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002624static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2625 const struct qstr *name, struct nfs_fh *fhandle,
2626 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002627{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002628 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002629 int status;
2630 struct nfs4_lookup_arg args = {
2631 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002632 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002633 .name = name,
2634 };
2635 struct nfs4_lookup_res res = {
2636 .server = server,
2637 .fattr = fattr,
2638 .fh = fhandle,
2639 };
2640 struct rpc_message msg = {
2641 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2642 .rpc_argp = &args,
2643 .rpc_resp = &res,
2644 };
2645
2646 nfs_fattr_init(fattr);
2647
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002649 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 dprintk("NFS reply lookup: %d\n", status);
2651 return status;
2652}
2653
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002654static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002655{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002656 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002657 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002658 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2659 fattr->nlink = 2;
2660}
2661
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002662static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2663 struct qstr *name, struct nfs_fh *fhandle,
2664 struct nfs_fattr *fattr)
2665{
2666 struct nfs4_exception exception = { };
2667 struct rpc_clnt *client = *clnt;
2668 int err;
2669 do {
2670 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2671 switch (err) {
2672 case -NFS4ERR_BADNAME:
2673 err = -ENOENT;
2674 goto out;
2675 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002676 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002677 goto out;
2678 case -NFS4ERR_WRONGSEC:
2679 err = -EPERM;
2680 if (client != *clnt)
2681 goto out;
2682
2683 client = nfs4_create_sec_client(client, dir, name);
2684 if (IS_ERR(client))
2685 return PTR_ERR(client);
2686
2687 exception.retry = 1;
2688 break;
2689 default:
2690 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2691 }
2692 } while (exception.retry);
2693
2694out:
2695 if (err == 0)
2696 *clnt = client;
2697 else if (client != *clnt)
2698 rpc_shutdown_client(client);
2699
2700 return err;
2701}
2702
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002703static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002704 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002706 int status;
2707 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002708
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002709 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2710 if (client != NFS_CLIENT(dir)) {
2711 rpc_shutdown_client(client);
2712 nfs_fixup_secinfo_attributes(fattr);
2713 }
2714 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715}
2716
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002717struct rpc_clnt *
2718nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2719 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2720{
2721 int status;
2722 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2723
2724 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2725 if (status < 0) {
2726 rpc_shutdown_client(client);
2727 return ERR_PTR(status);
2728 }
2729 return client;
2730}
2731
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2733{
Trond Myklebust76b32992007-08-10 17:45:11 -04002734 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 struct nfs4_accessargs args = {
2736 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002737 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002739 struct nfs4_accessres res = {
2740 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002741 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 struct rpc_message msg = {
2743 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2744 .rpc_argp = &args,
2745 .rpc_resp = &res,
2746 .rpc_cred = entry->cred,
2747 };
2748 int mode = entry->mask;
2749 int status;
2750
2751 /*
2752 * Determine which access bits we want to ask for...
2753 */
2754 if (mode & MAY_READ)
2755 args.access |= NFS4_ACCESS_READ;
2756 if (S_ISDIR(inode->i_mode)) {
2757 if (mode & MAY_WRITE)
2758 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2759 if (mode & MAY_EXEC)
2760 args.access |= NFS4_ACCESS_LOOKUP;
2761 } else {
2762 if (mode & MAY_WRITE)
2763 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2764 if (mode & MAY_EXEC)
2765 args.access |= NFS4_ACCESS_EXECUTE;
2766 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002767
2768 res.fattr = nfs_alloc_fattr();
2769 if (res.fattr == NULL)
2770 return -ENOMEM;
2771
Bryan Schumaker7c513052011-03-24 17:12:24 +00002772 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002774 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04002775 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002777 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 return status;
2779}
2780
2781static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2782{
2783 struct nfs4_exception exception = { };
2784 int err;
2785 do {
2786 err = nfs4_handle_exception(NFS_SERVER(inode),
2787 _nfs4_proc_access(inode, entry),
2788 &exception);
2789 } while (exception.retry);
2790 return err;
2791}
2792
2793/*
2794 * TODO: For the time being, we don't try to get any attributes
2795 * along with any of the zero-copy operations READ, READDIR,
2796 * READLINK, WRITE.
2797 *
2798 * In the case of the first three, we want to put the GETATTR
2799 * after the read-type operation -- this is because it is hard
2800 * to predict the length of a GETATTR response in v4, and thus
2801 * align the READ data correctly. This means that the GETATTR
2802 * may end up partially falling into the page cache, and we should
2803 * shift it into the 'tail' of the xdr_buf before processing.
2804 * To do this efficiently, we need to know the total length
2805 * of data received, which doesn't seem to be available outside
2806 * of the RPC layer.
2807 *
2808 * In the case of WRITE, we also want to put the GETATTR after
2809 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04002810 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 *
2812 * Both of these changes to the XDR layer would in fact be quite
2813 * minor, but I decided to leave them for a subsequent patch.
2814 */
2815static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2816 unsigned int pgbase, unsigned int pglen)
2817{
2818 struct nfs4_readlink args = {
2819 .fh = NFS_FH(inode),
2820 .pgbase = pgbase,
2821 .pglen = pglen,
2822 .pages = &page,
2823 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002824 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 struct rpc_message msg = {
2826 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2827 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002828 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 };
2830
Bryan Schumaker7c513052011-03-24 17:12:24 +00002831 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 -07002832}
2833
2834static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2835 unsigned int pgbase, unsigned int pglen)
2836{
2837 struct nfs4_exception exception = { };
2838 int err;
2839 do {
2840 err = nfs4_handle_exception(NFS_SERVER(inode),
2841 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2842 &exception);
2843 } while (exception.retry);
2844 return err;
2845}
2846
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002848 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850static int
2851nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002852 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853{
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002854 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 int status = 0;
2857
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002858 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
2859 if (IS_ERR(ctx))
2860 return PTR_ERR(ctx);
2861
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002862 sattr->ia_mode &= ~current_umask();
Trond Myklebust8626e4a2012-07-16 12:01:42 -04002863 state = nfs4_do_open(dir, dentry, ctx->mode,
2864 flags, sattr, ctx->cred,
2865 &ctx->mdsthreshold);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002866 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 if (IS_ERR(state)) {
2868 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002869 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002871 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002872 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002873 ctx->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874out:
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002875 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 return status;
2877}
2878
2879static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2880{
Trond Myklebust16e42952005-10-27 22:12:44 -04002881 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002882 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07002884 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002886 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002887 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002888 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002890 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2891 .rpc_argp = &args,
2892 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 };
Trond Myklebust778d2812012-04-27 13:48:19 -04002894 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04002895
Bryan Schumaker7c513052011-03-24 17:12:24 +00002896 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04002897 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04002898 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 return status;
2900}
2901
2902static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2903{
2904 struct nfs4_exception exception = { };
2905 int err;
2906 do {
2907 err = nfs4_handle_exception(NFS_SERVER(dir),
2908 _nfs4_proc_remove(dir, name),
2909 &exception);
2910 } while (exception.retry);
2911 return err;
2912}
2913
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002914static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002916 struct nfs_server *server = NFS_SERVER(dir);
2917 struct nfs_removeargs *args = msg->rpc_argp;
2918 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002920 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05002922 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923}
2924
Bryan Schumaker34e137c2012-03-19 14:54:41 -04002925static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
2926{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04002927 nfs4_setup_sequence(NFS_SERVER(data->dir),
2928 &data->args.seq_args,
2929 &data->res.seq_res,
2930 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931}
2932
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002933static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002935 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2936
Trond Myklebust14516c32010-07-31 14:29:06 -04002937 if (!nfs4_sequence_done(task, &res->seq_res))
2938 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002939 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002940 return 0;
2941 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002942 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943}
2944
Jeff Laytond3d41522010-09-17 17:31:57 -04002945static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2946{
2947 struct nfs_server *server = NFS_SERVER(dir);
2948 struct nfs_renameargs *arg = msg->rpc_argp;
2949 struct nfs_renameres *res = msg->rpc_resp;
2950
2951 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04002952 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002953 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04002954}
2955
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04002956static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
2957{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04002958 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
2959 &data->args.seq_args,
2960 &data->res.seq_res,
2961 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04002962}
2963
2964static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2965 struct inode *new_dir)
2966{
2967 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2968
2969 if (!nfs4_sequence_done(task, &res->seq_res))
2970 return 0;
2971 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2972 return 0;
2973
2974 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04002975 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04002976 return 1;
2977}
2978
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2980 struct inode *new_dir, struct qstr *new_name)
2981{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002982 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002983 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 .old_dir = NFS_FH(old_dir),
2985 .new_dir = NFS_FH(new_dir),
2986 .old_name = old_name,
2987 .new_name = new_name,
2988 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002989 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002990 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002991 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 struct rpc_message msg = {
2993 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2994 .rpc_argp = &arg,
2995 .rpc_resp = &res,
2996 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002997 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998
Bryan Schumaker7c513052011-03-24 17:12:24 +00002999 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 if (!status) {
3001 update_changeattr(old_dir, &res.old_cinfo);
3002 update_changeattr(new_dir, &res.new_cinfo);
3003 }
3004 return status;
3005}
3006
3007static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3008 struct inode *new_dir, struct qstr *new_name)
3009{
3010 struct nfs4_exception exception = { };
3011 int err;
3012 do {
3013 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3014 _nfs4_proc_rename(old_dir, old_name,
3015 new_dir, new_name),
3016 &exception);
3017 } while (exception.retry);
3018 return err;
3019}
3020
3021static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3022{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003023 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 struct nfs4_link_arg arg = {
3025 .fh = NFS_FH(inode),
3026 .dir_fh = NFS_FH(dir),
3027 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003028 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003030 struct nfs4_link_res res = {
3031 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003032 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 struct rpc_message msg = {
3034 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3035 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003036 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003038 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039
Trond Myklebust136f2622010-04-16 16:22:49 -04003040 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003041 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003042 goto out;
3043
Bryan Schumaker7c513052011-03-24 17:12:24 +00003044 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003045 if (!status) {
3046 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04003047 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003048 }
Trond Myklebust136f2622010-04-16 16:22:49 -04003049out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003050 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 return status;
3052}
3053
3054static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3055{
3056 struct nfs4_exception exception = { };
3057 int err;
3058 do {
3059 err = nfs4_handle_exception(NFS_SERVER(inode),
3060 _nfs4_proc_link(inode, dir, name),
3061 &exception);
3062 } while (exception.retry);
3063 return err;
3064}
3065
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003066struct nfs4_createdata {
3067 struct rpc_message msg;
3068 struct nfs4_create_arg arg;
3069 struct nfs4_create_res res;
3070 struct nfs_fh fh;
3071 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003072};
3073
3074static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3075 struct qstr *name, struct iattr *sattr, u32 ftype)
3076{
3077 struct nfs4_createdata *data;
3078
3079 data = kzalloc(sizeof(*data), GFP_KERNEL);
3080 if (data != NULL) {
3081 struct nfs_server *server = NFS_SERVER(dir);
3082
3083 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3084 data->msg.rpc_argp = &data->arg;
3085 data->msg.rpc_resp = &data->res;
3086 data->arg.dir_fh = NFS_FH(dir);
3087 data->arg.server = server;
3088 data->arg.name = name;
3089 data->arg.attrs = sattr;
3090 data->arg.ftype = ftype;
3091 data->arg.bitmask = server->attr_bitmask;
3092 data->res.server = server;
3093 data->res.fh = &data->fh;
3094 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003095 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003096 }
3097 return data;
3098}
3099
3100static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3101{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003102 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003103 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003104 if (status == 0) {
3105 update_changeattr(dir, &data->res.dir_cinfo);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003106 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3107 }
3108 return status;
3109}
3110
3111static void nfs4_free_createdata(struct nfs4_createdata *data)
3112{
3113 kfree(data);
3114}
3115
Chuck Lever4f390c12006-08-22 20:06:22 -04003116static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003117 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003119 struct nfs4_createdata *data;
3120 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121
Chuck Lever94a6d752006-08-22 20:06:23 -04003122 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003123 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003124
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003125 status = -ENOMEM;
3126 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3127 if (data == NULL)
3128 goto out;
3129
3130 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3131 data->arg.u.symlink.pages = &page;
3132 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003134 status = nfs4_do_create(dir, dentry, data);
3135
3136 nfs4_free_createdata(data);
3137out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 return status;
3139}
3140
Chuck Lever4f390c12006-08-22 20:06:22 -04003141static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003142 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143{
3144 struct nfs4_exception exception = { };
3145 int err;
3146 do {
3147 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003148 _nfs4_proc_symlink(dir, dentry, page,
3149 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 &exception);
3151 } while (exception.retry);
3152 return err;
3153}
3154
3155static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3156 struct iattr *sattr)
3157{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003158 struct nfs4_createdata *data;
3159 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003161 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3162 if (data == NULL)
3163 goto out;
3164
3165 status = nfs4_do_create(dir, dentry, data);
3166
3167 nfs4_free_createdata(data);
3168out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 return status;
3170}
3171
3172static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3173 struct iattr *sattr)
3174{
3175 struct nfs4_exception exception = { };
3176 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003177
3178 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 do {
3180 err = nfs4_handle_exception(NFS_SERVER(dir),
3181 _nfs4_proc_mkdir(dir, dentry, sattr),
3182 &exception);
3183 } while (exception.retry);
3184 return err;
3185}
3186
3187static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003188 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189{
3190 struct inode *dir = dentry->d_inode;
3191 struct nfs4_readdir_arg args = {
3192 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003193 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 .pgbase = 0,
3195 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003196 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003197 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 };
3199 struct nfs4_readdir_res res;
3200 struct rpc_message msg = {
3201 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3202 .rpc_argp = &args,
3203 .rpc_resp = &res,
3204 .rpc_cred = cred,
3205 };
3206 int status;
3207
Harvey Harrison3110ff82008-05-02 13:42:44 -07003208 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003209 dentry->d_parent->d_name.name,
3210 dentry->d_name.name,
3211 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003212 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003214 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 -05003215 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003216 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003217 status += args.pgbase;
3218 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003219
3220 nfs_invalidate_atime(dir);
3221
Harvey Harrison3110ff82008-05-02 13:42:44 -07003222 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 return status;
3224}
3225
3226static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003227 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228{
3229 struct nfs4_exception exception = { };
3230 int err;
3231 do {
3232 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3233 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003234 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 &exception);
3236 } while (exception.retry);
3237 return err;
3238}
3239
3240static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3241 struct iattr *sattr, dev_t rdev)
3242{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003243 struct nfs4_createdata *data;
3244 int mode = sattr->ia_mode;
3245 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003247 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3248 if (data == NULL)
3249 goto out;
3250
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003252 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003254 data->arg.ftype = NF4BLK;
3255 data->arg.u.device.specdata1 = MAJOR(rdev);
3256 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 }
3258 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003259 data->arg.ftype = NF4CHR;
3260 data->arg.u.device.specdata1 = MAJOR(rdev);
3261 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003262 } else if (!S_ISSOCK(mode)) {
3263 status = -EINVAL;
3264 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003267 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003268out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003269 nfs4_free_createdata(data);
3270out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 return status;
3272}
3273
3274static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3275 struct iattr *sattr, dev_t rdev)
3276{
3277 struct nfs4_exception exception = { };
3278 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003279
3280 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 do {
3282 err = nfs4_handle_exception(NFS_SERVER(dir),
3283 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3284 &exception);
3285 } while (exception.retry);
3286 return err;
3287}
3288
3289static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3290 struct nfs_fsstat *fsstat)
3291{
3292 struct nfs4_statfs_arg args = {
3293 .fh = fhandle,
3294 .bitmask = server->attr_bitmask,
3295 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003296 struct nfs4_statfs_res res = {
3297 .fsstat = fsstat,
3298 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 struct rpc_message msg = {
3300 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3301 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003302 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 };
3304
Trond Myklebust0e574af2005-10-27 22:12:38 -04003305 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003306 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307}
3308
3309static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3310{
3311 struct nfs4_exception exception = { };
3312 int err;
3313 do {
3314 err = nfs4_handle_exception(server,
3315 _nfs4_proc_statfs(server, fhandle, fsstat),
3316 &exception);
3317 } while (exception.retry);
3318 return err;
3319}
3320
3321static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3322 struct nfs_fsinfo *fsinfo)
3323{
3324 struct nfs4_fsinfo_arg args = {
3325 .fh = fhandle,
3326 .bitmask = server->attr_bitmask,
3327 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003328 struct nfs4_fsinfo_res res = {
3329 .fsinfo = fsinfo,
3330 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 struct rpc_message msg = {
3332 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3333 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003334 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 };
3336
Bryan Schumaker7c513052011-03-24 17:12:24 +00003337 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338}
3339
3340static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3341{
3342 struct nfs4_exception exception = { };
3343 int err;
3344
3345 do {
3346 err = nfs4_handle_exception(server,
3347 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3348 &exception);
3349 } while (exception.retry);
3350 return err;
3351}
3352
3353static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3354{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003355 int error;
3356
Trond Myklebust0e574af2005-10-27 22:12:38 -04003357 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003358 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003359 if (error == 0) {
3360 /* block layout checks this! */
3361 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003362 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003363 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003364
3365 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366}
3367
3368static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3369 struct nfs_pathconf *pathconf)
3370{
3371 struct nfs4_pathconf_arg args = {
3372 .fh = fhandle,
3373 .bitmask = server->attr_bitmask,
3374 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003375 struct nfs4_pathconf_res res = {
3376 .pathconf = pathconf,
3377 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 struct rpc_message msg = {
3379 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3380 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003381 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 };
3383
3384 /* None of the pathconf attributes are mandatory to implement */
3385 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3386 memset(pathconf, 0, sizeof(*pathconf));
3387 return 0;
3388 }
3389
Trond Myklebust0e574af2005-10-27 22:12:38 -04003390 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003391 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392}
3393
3394static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3395 struct nfs_pathconf *pathconf)
3396{
3397 struct nfs4_exception exception = { };
3398 int err;
3399
3400 do {
3401 err = nfs4_handle_exception(server,
3402 _nfs4_proc_pathconf(server, fhandle, pathconf),
3403 &exception);
3404 } while (exception.retry);
3405 return err;
3406}
3407
Benny Halevyd20581a2011-05-22 19:52:03 +03003408void __nfs4_read_done_cb(struct nfs_read_data *data)
3409{
Fred Isamancd841602012-04-20 14:47:44 -04003410 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003411}
3412
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003413static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414{
Fred Isamancd841602012-04-20 14:47:44 -04003415 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003417 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003418 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003419 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003421
Benny Halevyd20581a2011-05-22 19:52:03 +03003422 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003424 renew_lease(server, data->timestamp);
3425 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426}
3427
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003428static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3429{
3430
3431 dprintk("--> %s\n", __func__);
3432
3433 if (!nfs4_sequence_done(task, &data->res.seq_res))
3434 return -EAGAIN;
3435
Benny Halevyd20581a2011-05-22 19:52:03 +03003436 return data->read_done_cb ? data->read_done_cb(task, data) :
3437 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003438}
3439
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003440static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003443 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003444 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003445 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446}
3447
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003448static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3449{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003450 nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3451 &data->args.seq_args,
3452 &data->res.seq_res,
3453 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454}
3455
Fred Isamanb029bc92011-03-03 15:13:42 +00003456static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457{
Fred Isamancd841602012-04-20 14:47:44 -04003458 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003460 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003461 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003462 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003464 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003466 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003467 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003468 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469}
3470
Fred Isamanb029bc92011-03-03 15:13:42 +00003471static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3472{
3473 if (!nfs4_sequence_done(task, &data->res.seq_res))
3474 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003475 return data->write_done_cb ? data->write_done_cb(task, data) :
3476 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003477}
3478
Trond Myklebust5a37f852012-04-28 14:55:16 -04003479static
3480bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003481{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003482 const struct nfs_pgio_header *hdr = data->header;
3483
3484 /* Don't request attributes for pNFS or O_DIRECT writes */
3485 if (data->ds_clp != NULL || hdr->dreq != NULL)
3486 return false;
3487 /* Otherwise, request attributes if and only if we don't hold
3488 * a delegation
3489 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04003490 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003491}
Fred Isamana69aef12011-03-03 15:13:47 +00003492
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003493static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494{
Fred Isamancd841602012-04-20 14:47:44 -04003495 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003496
Trond Myklebust5a37f852012-04-28 14:55:16 -04003497 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003498 data->args.bitmask = NULL;
3499 data->res.fattr = NULL;
3500 } else
3501 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003502
Fred Isamanb029bc92011-03-03 15:13:42 +00003503 if (!data->write_done_cb)
3504 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003505 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 data->timestamp = jiffies;
3507
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003508 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003509 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510}
3511
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003512static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3513{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003514 nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3515 &data->args.seq_args,
3516 &data->res.seq_res,
3517 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518}
3519
Fred Isaman0b7c0152012-04-20 14:47:39 -04003520static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3521{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003522 nfs4_setup_sequence(NFS_SERVER(data->inode),
3523 &data->args.seq_args,
3524 &data->res.seq_res,
3525 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526}
3527
Fred Isaman0b7c0152012-04-20 14:47:39 -04003528static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003531
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003532 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003533 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003534 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003536 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537}
3538
Fred Isaman0b7c0152012-04-20 14:47:39 -04003539static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003540{
3541 if (!nfs4_sequence_done(task, &data->res.seq_res))
3542 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003543 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003544}
3545
Fred Isaman0b7c0152012-04-20 14:47:39 -04003546static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003548 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003549
Fred Isaman0b7c0152012-04-20 14:47:39 -04003550 if (data->commit_done_cb == NULL)
3551 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003552 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003553 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003554 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555}
3556
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003557struct nfs4_renewdata {
3558 struct nfs_client *client;
3559 unsigned long timestamp;
3560};
3561
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562/*
3563 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3564 * standalone procedure for queueing an asynchronous RENEW.
3565 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003566static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003567{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003568 struct nfs4_renewdata *data = calldata;
3569 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003570
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003571 if (atomic_read(&clp->cl_count) > 1)
3572 nfs4_schedule_state_renewal(clp);
3573 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003574 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003575}
3576
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003577static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003579 struct nfs4_renewdata *data = calldata;
3580 struct nfs_client *clp = data->client;
3581 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582
3583 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003584 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003585 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3586 return;
3587 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003588 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003589 return;
3590 }
3591 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003593 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594}
3595
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003596static const struct rpc_call_ops nfs4_renew_ops = {
3597 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003598 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003599};
3600
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003601static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602{
3603 struct rpc_message msg = {
3604 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3605 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003606 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003608 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003610 if (renew_flags == 0)
3611 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003612 if (!atomic_inc_not_zero(&clp->cl_count))
3613 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003614 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003615 if (data == NULL)
3616 return -ENOMEM;
3617 data->client = clp;
3618 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003620 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621}
3622
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003623static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624{
3625 struct rpc_message msg = {
3626 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3627 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003628 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629 };
3630 unsigned long now = jiffies;
3631 int status;
3632
3633 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3634 if (status < 0)
3635 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003636 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637 return 0;
3638}
3639
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003640static inline int nfs4_server_supports_acls(struct nfs_server *server)
3641{
3642 return (server->caps & NFS_CAP_ACLS)
3643 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3644 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3645}
3646
Trond Myklebust21f498c2012-08-24 10:59:25 -04003647/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3648 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003649 * the stack.
3650 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04003651#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003652
Neil Hormane9e3d722011-03-04 19:26:03 -05003653static int buf_to_pages_noslab(const void *buf, size_t buflen,
3654 struct page **pages, unsigned int *pgbase)
3655{
3656 struct page *newpage, **spages;
3657 int rc = 0;
3658 size_t len;
3659 spages = pages;
3660
3661 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04003662 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003663 newpage = alloc_page(GFP_KERNEL);
3664
3665 if (newpage == NULL)
3666 goto unwind;
3667 memcpy(page_address(newpage), buf, len);
3668 buf += len;
3669 buflen -= len;
3670 *pages++ = newpage;
3671 rc++;
3672 } while (buflen != 0);
3673
3674 return rc;
3675
3676unwind:
3677 for(; rc > 0; rc--)
3678 __free_page(spages[rc-1]);
3679 return -ENOMEM;
3680}
3681
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003682struct nfs4_cached_acl {
3683 int cached;
3684 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003685 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003686};
3687
3688static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003689{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003690 struct nfs_inode *nfsi = NFS_I(inode);
3691
3692 spin_lock(&inode->i_lock);
3693 kfree(nfsi->nfs4_acl);
3694 nfsi->nfs4_acl = acl;
3695 spin_unlock(&inode->i_lock);
3696}
3697
3698static void nfs4_zap_acl_attr(struct inode *inode)
3699{
3700 nfs4_set_cached_acl(inode, NULL);
3701}
3702
3703static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3704{
3705 struct nfs_inode *nfsi = NFS_I(inode);
3706 struct nfs4_cached_acl *acl;
3707 int ret = -ENOENT;
3708
3709 spin_lock(&inode->i_lock);
3710 acl = nfsi->nfs4_acl;
3711 if (acl == NULL)
3712 goto out;
3713 if (buf == NULL) /* user is just asking for length */
3714 goto out_len;
3715 if (acl->cached == 0)
3716 goto out;
3717 ret = -ERANGE; /* see getxattr(2) man page */
3718 if (acl->len > buflen)
3719 goto out;
3720 memcpy(buf, acl->data, acl->len);
3721out_len:
3722 ret = acl->len;
3723out:
3724 spin_unlock(&inode->i_lock);
3725 return ret;
3726}
3727
Sachin Prabhu5794d212012-04-17 14:36:40 +01003728static 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 +00003729{
3730 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003731 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003732
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003733 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003734 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003735 if (acl == NULL)
3736 goto out;
3737 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003738 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003739 } else {
3740 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3741 if (acl == NULL)
3742 goto out;
3743 acl->cached = 0;
3744 }
3745 acl->len = acl_len;
3746out:
3747 nfs4_set_cached_acl(inode, acl);
3748}
3749
Andy Adamsonbf118a32011-12-07 11:55:27 -05003750/*
3751 * The getxattr API returns the required buffer length when called with a
3752 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3753 * the required buf. On a NULL buf, we send a page of data to the server
3754 * guessing that the ACL request can be serviced by a page. If so, we cache
3755 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3756 * the cache. If not so, we throw away the page, and cache the required
3757 * length. The next getxattr call will then produce another round trip to
3758 * the server, this time with the input buf of the required size.
3759 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003760static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003761{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003762 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003763 struct nfs_getaclargs args = {
3764 .fh = NFS_FH(inode),
3765 .acl_pages = pages,
3766 .acl_len = buflen,
3767 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003768 struct nfs_getaclres res = {
3769 .acl_len = buflen,
3770 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003771 struct rpc_message msg = {
3772 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3773 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003774 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003775 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003776 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3777 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003778
Andy Adamsonbf118a32011-12-07 11:55:27 -05003779 /* As long as we're doing a round trip to the server anyway,
3780 * let's be prepared for a page of acl data. */
3781 if (npages == 0)
3782 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003783 if (npages > ARRAY_SIZE(pages))
3784 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003785
Andy Adamsonbf118a32011-12-07 11:55:27 -05003786 for (i = 0; i < npages; i++) {
3787 pages[i] = alloc_page(GFP_KERNEL);
3788 if (!pages[i])
3789 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003790 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01003791
3792 /* for decoding across pages */
3793 res.acl_scratch = alloc_page(GFP_KERNEL);
3794 if (!res.acl_scratch)
3795 goto out_free;
3796
Andy Adamsonbf118a32011-12-07 11:55:27 -05003797 args.acl_len = npages * PAGE_SIZE;
3798 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003799
Peng Taode040be2012-01-10 22:42:47 +08003800 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003801 __func__, buf, buflen, npages, args.acl_len);
3802 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3803 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003804 if (ret)
3805 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003806
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003807 /* Handle the case where the passed-in buffer is too short */
3808 if (res.acl_flags & NFS4_ACL_TRUNC) {
3809 /* Did the user only issue a request for the acl length? */
3810 if (buf == NULL)
3811 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003812 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003813 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003814 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003815 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01003816 if (buf) {
3817 if (res.acl_len > buflen) {
3818 ret = -ERANGE;
3819 goto out_free;
3820 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003821 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01003822 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003823out_ok:
3824 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003825out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003826 for (i = 0; i < npages; i++)
3827 if (pages[i])
3828 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05003829 if (res.acl_scratch)
3830 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003831 return ret;
3832}
3833
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003834static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3835{
3836 struct nfs4_exception exception = { };
3837 ssize_t ret;
3838 do {
3839 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3840 if (ret >= 0)
3841 break;
3842 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3843 } while (exception.retry);
3844 return ret;
3845}
3846
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003847static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3848{
3849 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003850 int ret;
3851
3852 if (!nfs4_server_supports_acls(server))
3853 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003854 ret = nfs_revalidate_inode(server, inode);
3855 if (ret < 0)
3856 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003857 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3858 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003859 ret = nfs4_read_cached_acl(inode, buf, buflen);
3860 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003861 /* -ENOENT is returned if there is no ACL or if there is an ACL
3862 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003863 return ret;
3864 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003865}
3866
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003867static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003868{
3869 struct nfs_server *server = NFS_SERVER(inode);
3870 struct page *pages[NFS4ACL_MAXPAGES];
3871 struct nfs_setaclargs arg = {
3872 .fh = NFS_FH(inode),
3873 .acl_pages = pages,
3874 .acl_len = buflen,
3875 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003876 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003877 struct rpc_message msg = {
3878 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3879 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003880 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003881 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003882 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05003883 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003884
3885 if (!nfs4_server_supports_acls(server))
3886 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003887 if (npages > ARRAY_SIZE(pages))
3888 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05003889 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3890 if (i < 0)
3891 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04003892 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003893 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003894
3895 /*
3896 * Free each page after tx, so the only ref left is
3897 * held by the network stack
3898 */
3899 for (; i > 0; i--)
3900 put_page(pages[i-1]);
3901
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003902 /*
3903 * Acl update can result in inode attribute update.
3904 * so mark the attribute cache invalid.
3905 */
3906 spin_lock(&inode->i_lock);
3907 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3908 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003909 nfs_access_zap_cache(inode);
3910 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003911 return ret;
3912}
3913
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003914static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3915{
3916 struct nfs4_exception exception = { };
3917 int err;
3918 do {
3919 err = nfs4_handle_exception(NFS_SERVER(inode),
3920 __nfs4_proc_set_acl(inode, buf, buflen),
3921 &exception);
3922 } while (exception.retry);
3923 return err;
3924}
3925
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003927nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003929 struct nfs_client *clp = server->nfs_client;
3930
3931 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932 return 0;
3933 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05003934 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003935 case -NFS4ERR_ADMIN_REVOKED:
3936 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04003937 if (state == NULL)
3938 break;
3939 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003940 case -NFS4ERR_OPENMODE:
3941 if (state == NULL)
3942 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003943 nfs4_schedule_stateid_recovery(server, state);
3944 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003945 case -NFS4ERR_EXPIRED:
3946 if (state != NULL)
3947 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003949 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003950 nfs4_schedule_lease_recovery(clp);
3951 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003952#if defined(CONFIG_NFS_V4_1)
3953 case -NFS4ERR_BADSESSION:
3954 case -NFS4ERR_BADSLOT:
3955 case -NFS4ERR_BAD_HIGH_SLOT:
3956 case -NFS4ERR_DEADSESSION:
3957 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3958 case -NFS4ERR_SEQ_FALSE_RETRY:
3959 case -NFS4ERR_SEQ_MISORDERED:
3960 dprintk("%s ERROR %d, Reset session\n", __func__,
3961 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04003962 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04003963 task->tk_status = 0;
3964 return -EAGAIN;
3965#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003967 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003968 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3970 task->tk_status = 0;
3971 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04003972 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 case -NFS4ERR_OLD_STATEID:
3974 task->tk_status = 0;
3975 return -EAGAIN;
3976 }
3977 task->tk_status = nfs4_map_errors(task->tk_status);
3978 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003979wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003980 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003981 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3982 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3983 task->tk_status = 0;
3984 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985}
3986
Chuck Leverf0920752012-05-21 22:45:41 -04003987static void nfs4_init_boot_verifier(const struct nfs_client *clp,
3988 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05003989{
3990 __be32 verf[2];
3991
Chuck Lever2c820d92012-05-21 22:45:33 -04003992 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
3993 /* An impossible timestamp guarantees this value
3994 * will never match a generated boot time. */
3995 verf[0] = 0;
3996 verf[1] = (__be32)(NSEC_PER_SEC + 1);
3997 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04003998 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
3999 verf[0] = (__be32)nn->boot_time.tv_sec;
4000 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04004001 }
Chuck Levercd937102012-03-02 17:14:31 -05004002 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4003}
4004
Chuck Levere984a552012-09-14 17:24:21 -04004005static unsigned int
4006nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4007 char *buf, size_t len)
4008{
4009 unsigned int result;
4010
4011 rcu_read_lock();
4012 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4013 clp->cl_ipaddr,
4014 rpc_peeraddr2str(clp->cl_rpcclient,
4015 RPC_DISPLAY_ADDR),
4016 rpc_peeraddr2str(clp->cl_rpcclient,
4017 RPC_DISPLAY_PROTO));
4018 rcu_read_unlock();
4019 return result;
4020}
4021
4022static unsigned int
4023nfs4_init_uniform_client_string(const struct nfs_client *clp,
4024 char *buf, size_t len)
4025{
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004026 char *nodename = clp->cl_rpcclient->cl_nodename;
4027
4028 if (nfs4_client_id_uniquifier[0] != '\0')
4029 nodename = nfs4_client_id_uniquifier;
Chuck Levere984a552012-09-14 17:24:21 -04004030 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4031 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004032 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004033}
4034
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004035/**
4036 * nfs4_proc_setclientid - Negotiate client ID
4037 * @clp: state data structure
4038 * @program: RPC program for NFSv4 callback service
4039 * @port: IP port number for NFS4 callback service
4040 * @cred: RPC credential to use for this call
4041 * @res: where to place the result
4042 *
4043 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4044 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004045int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4046 unsigned short port, struct rpc_cred *cred,
4047 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048{
4049 nfs4_verifier sc_verifier;
4050 struct nfs4_setclientid setclientid = {
4051 .sc_verifier = &sc_verifier,
4052 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004053 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 };
4055 struct rpc_message msg = {
4056 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4057 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004058 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004059 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004061 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062
Chuck Leverde734832012-07-11 16:30:50 -04004063 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004064 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004065 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4066 setclientid.sc_name_len =
4067 nfs4_init_uniform_client_string(clp,
4068 setclientid.sc_name,
4069 sizeof(setclientid.sc_name));
4070 else
4071 setclientid.sc_name_len =
4072 nfs4_init_nonuniform_client_string(clp,
4073 setclientid.sc_name,
4074 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004075 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004076 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004077 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004078 sizeof(setclientid.sc_netid),
4079 rpc_peeraddr2str(clp->cl_rpcclient,
4080 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004081 rcu_read_unlock();
4082 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004083 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084 clp->cl_ipaddr, port >> 8, port & 255);
4085
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004086 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4087 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4088 setclientid.sc_name_len, setclientid.sc_name);
4089 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4090 dprintk("NFS reply setclientid: %d\n", status);
4091 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092}
4093
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004094/**
4095 * nfs4_proc_setclientid_confirm - Confirm client ID
4096 * @clp: state data structure
4097 * @res: result of a previous SETCLIENTID
4098 * @cred: RPC credential to use for this call
4099 *
4100 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4101 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004102int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004103 struct nfs4_setclientid_res *arg,
4104 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105{
4106 struct nfs_fsinfo fsinfo;
4107 struct rpc_message msg = {
4108 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004109 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004111 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112 };
4113 unsigned long now;
4114 int status;
4115
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004116 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4117 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4118 clp->cl_clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004120 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121 if (status == 0) {
4122 spin_lock(&clp->cl_lock);
4123 clp->cl_lease_time = fsinfo.lease_time * HZ;
4124 clp->cl_last_renewal = now;
4125 spin_unlock(&clp->cl_lock);
4126 }
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004127 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128 return status;
4129}
4130
Trond Myklebustfe650402006-01-03 09:55:18 +01004131struct nfs4_delegreturndata {
4132 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004133 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004134 struct nfs_fh fh;
4135 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004136 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004137 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004138 int rpc_status;
4139};
4140
Trond Myklebustfe650402006-01-03 09:55:18 +01004141static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4142{
4143 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004144
Trond Myklebust14516c32010-07-31 14:29:06 -04004145 if (!nfs4_sequence_done(task, &data->res.seq_res))
4146 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004147
Ricardo Labiaga79708862009-12-07 09:23:21 -05004148 switch (task->tk_status) {
4149 case -NFS4ERR_STALE_STATEID:
4150 case -NFS4ERR_EXPIRED:
4151 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004152 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004153 break;
4154 default:
4155 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4156 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004157 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004158 return;
4159 }
4160 }
4161 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004162}
4163
4164static void nfs4_delegreturn_release(void *calldata)
4165{
Trond Myklebustfe650402006-01-03 09:55:18 +01004166 kfree(calldata);
4167}
4168
Andy Adamson938e1012009-04-01 09:22:28 -04004169#if defined(CONFIG_NFS_V4_1)
4170static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4171{
4172 struct nfs4_delegreturndata *d_data;
4173
4174 d_data = (struct nfs4_delegreturndata *)data;
4175
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004176 nfs4_setup_sequence(d_data->res.server,
4177 &d_data->args.seq_args,
4178 &d_data->res.seq_res,
4179 task);
Andy Adamson938e1012009-04-01 09:22:28 -04004180}
4181#endif /* CONFIG_NFS_V4_1 */
4182
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004183static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004184#if defined(CONFIG_NFS_V4_1)
4185 .rpc_call_prepare = nfs4_delegreturn_prepare,
4186#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004187 .rpc_call_done = nfs4_delegreturn_done,
4188 .rpc_release = nfs4_delegreturn_release,
4189};
4190
Trond Myklebuste6f81072008-01-24 18:14:34 -05004191static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004192{
4193 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004194 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004195 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004196 struct rpc_message msg = {
4197 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4198 .rpc_cred = cred,
4199 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004200 struct rpc_task_setup task_setup_data = {
4201 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004202 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004203 .callback_ops = &nfs4_delegreturn_ops,
4204 .flags = RPC_TASK_ASYNC,
4205 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004206 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004207
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004208 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004209 if (data == NULL)
4210 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004211 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004212 data->args.fhandle = &data->fh;
4213 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004214 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004215 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004216 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004217 data->res.fattr = &data->fattr;
4218 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004219 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004220 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004221 data->rpc_status = 0;
4222
Trond Myklebustc970aa82007-07-14 15:39:59 -04004223 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004224 msg.rpc_argp = &data->args;
4225 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004226 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004227 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004228 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004229 if (!issync)
4230 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004231 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004232 if (status != 0)
4233 goto out;
4234 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004235 if (status == 0)
4236 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4237 else
4238 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004239out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004240 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004241 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242}
4243
Trond Myklebuste6f81072008-01-24 18:14:34 -05004244int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245{
4246 struct nfs_server *server = NFS_SERVER(inode);
4247 struct nfs4_exception exception = { };
4248 int err;
4249 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004250 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251 switch (err) {
4252 case -NFS4ERR_STALE_STATEID:
4253 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254 case 0:
4255 return 0;
4256 }
4257 err = nfs4_handle_exception(server, err, &exception);
4258 } while (exception.retry);
4259 return err;
4260}
4261
4262#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4263#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4264
4265/*
4266 * sleep, with exponential backoff, and retry the LOCK operation.
4267 */
4268static unsigned long
4269nfs4_set_lock_task_retry(unsigned long timeout)
4270{
Jeff Laytond3103102011-12-01 22:44:39 +01004271 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272 timeout <<= 1;
4273 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4274 return NFS4_LOCK_MAXTIMEOUT;
4275 return timeout;
4276}
4277
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4279{
4280 struct inode *inode = state->inode;
4281 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004282 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004283 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004285 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004287 struct nfs_lockt_res res = {
4288 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289 };
4290 struct rpc_message msg = {
4291 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4292 .rpc_argp = &arg,
4293 .rpc_resp = &res,
4294 .rpc_cred = state->owner->so_cred,
4295 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 struct nfs4_lock_state *lsp;
4297 int status;
4298
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004299 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004300 status = nfs4_set_lock_state(state, request);
4301 if (status != 0)
4302 goto out;
4303 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004304 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004305 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004306 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004307 switch (status) {
4308 case 0:
4309 request->fl_type = F_UNLCK;
4310 break;
4311 case -NFS4ERR_DENIED:
4312 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004314 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004315out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316 return status;
4317}
4318
4319static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4320{
4321 struct nfs4_exception exception = { };
4322 int err;
4323
4324 do {
4325 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4326 _nfs4_proc_getlk(state, cmd, request),
4327 &exception);
4328 } while (exception.retry);
4329 return err;
4330}
4331
4332static int do_vfs_lock(struct file *file, struct file_lock *fl)
4333{
4334 int res = 0;
4335 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4336 case FL_POSIX:
4337 res = posix_lock_file_wait(file, fl);
4338 break;
4339 case FL_FLOCK:
4340 res = flock_lock_file_wait(file, fl);
4341 break;
4342 default:
4343 BUG();
4344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 return res;
4346}
4347
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004348struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004349 struct nfs_locku_args arg;
4350 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004351 struct nfs4_lock_state *lsp;
4352 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004353 struct file_lock fl;
4354 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004355 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004356};
4357
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004358static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4359 struct nfs_open_context *ctx,
4360 struct nfs4_lock_state *lsp,
4361 struct nfs_seqid *seqid)
4362{
4363 struct nfs4_unlockdata *p;
4364 struct inode *inode = lsp->ls_state->inode;
4365
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004366 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004367 if (p == NULL)
4368 return NULL;
4369 p->arg.fh = NFS_FH(inode);
4370 p->arg.fl = &p->fl;
4371 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004372 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004373 p->arg.stateid = &lsp->ls_stateid;
4374 p->lsp = lsp;
4375 atomic_inc(&lsp->ls_count);
4376 /* Ensure we don't close file until we're done freeing locks! */
4377 p->ctx = get_nfs_open_context(ctx);
4378 memcpy(&p->fl, fl, sizeof(p->fl));
4379 p->server = NFS_SERVER(inode);
4380 return p;
4381}
4382
Trond Myklebust06f814a2006-01-03 09:55:07 +01004383static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004384{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004385 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004386 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004387 nfs4_put_lock_state(calldata->lsp);
4388 put_nfs_open_context(calldata->ctx);
4389 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004390}
4391
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004392static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004393{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004394 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004395
Trond Myklebust14516c32010-07-31 14:29:06 -04004396 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4397 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004398 switch (task->tk_status) {
4399 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004400 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4401 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004402 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004403 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004404 case -NFS4ERR_BAD_STATEID:
4405 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004406 case -NFS4ERR_STALE_STATEID:
4407 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004408 break;
4409 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004410 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004411 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004412 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04004413 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004414}
4415
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004416static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004417{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004418 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004420 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004421 return;
Trond Myklebust795a88c2012-09-10 13:26:49 -04004422 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004423 /* Note: exit _without_ running nfs4_locku_done */
4424 task->tk_action = NULL;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05004425 nfs4_sequence_done(task, &calldata->res.seq_res);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004426 return;
4427 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004428 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004429 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004430 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004431 &calldata->res.seq_res,
4432 task) != 0)
4433 nfs_release_seqid(calldata->arg.seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434}
4435
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004436static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004437 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004438 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004439 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004440};
4441
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004442static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4443 struct nfs_open_context *ctx,
4444 struct nfs4_lock_state *lsp,
4445 struct nfs_seqid *seqid)
4446{
4447 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004448 struct rpc_message msg = {
4449 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4450 .rpc_cred = ctx->cred,
4451 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004452 struct rpc_task_setup task_setup_data = {
4453 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004454 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004455 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004456 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004457 .flags = RPC_TASK_ASYNC,
4458 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004459
Frank Filz137d6ac2007-07-09 15:32:29 -07004460 /* Ensure this is an unlock - when canceling a lock, the
4461 * canceled lock is passed in, and it won't be an unlock.
4462 */
4463 fl->fl_type = F_UNLCK;
4464
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004465 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4466 if (data == NULL) {
4467 nfs_free_seqid(seqid);
4468 return ERR_PTR(-ENOMEM);
4469 }
4470
Trond Myklebust9d12b212012-01-17 22:04:25 -05004471 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004472 msg.rpc_argp = &data->arg;
4473 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004474 task_setup_data.callback_data = data;
4475 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004476}
4477
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4479{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004480 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004481 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004482 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004483 struct rpc_task *task;
4484 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004485 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486
Trond Myklebust9b073572006-06-29 16:38:34 -04004487 status = nfs4_set_lock_state(state, request);
4488 /* Unlock _before_ we do the RPC call */
4489 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004490 down_read(&nfsi->rwsem);
4491 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4492 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004493 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004494 }
4495 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004496 if (status != 0)
4497 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004498 /* Is this a delegated lock? */
4499 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004500 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004501 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004502 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004503 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004504 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004505 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004506 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004507 status = PTR_ERR(task);
4508 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004509 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004510 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004511 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004512out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004513 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004514 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515}
4516
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004517struct nfs4_lockdata {
4518 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004519 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004520 struct nfs4_lock_state *lsp;
4521 struct nfs_open_context *ctx;
4522 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004523 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004524 int rpc_status;
4525 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004526 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004527};
4528
4529static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004530 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4531 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004532{
4533 struct nfs4_lockdata *p;
4534 struct inode *inode = lsp->ls_state->inode;
4535 struct nfs_server *server = NFS_SERVER(inode);
4536
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004537 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004538 if (p == NULL)
4539 return NULL;
4540
4541 p->arg.fh = NFS_FH(inode);
4542 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004543 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004544 if (p->arg.open_seqid == NULL)
4545 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004546 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004547 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004548 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004549 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004550 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004551 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004552 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004553 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004554 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004555 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004556 atomic_inc(&lsp->ls_count);
4557 p->ctx = get_nfs_open_context(ctx);
4558 memcpy(&p->fl, fl, sizeof(p->fl));
4559 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004560out_free_seqid:
4561 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004562out_free:
4563 kfree(p);
4564 return NULL;
4565}
4566
4567static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4568{
4569 struct nfs4_lockdata *data = calldata;
4570 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571
Harvey Harrison3110ff82008-05-02 13:42:44 -07004572 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004573 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4574 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004575 /* Do we need to do an open_to_lock_owner? */
4576 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004577 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004578 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004579 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004580 data->arg.open_stateid = &state->stateid;
4581 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004582 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004583 } else
4584 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004585 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004586 if (nfs4_setup_sequence(data->server,
4587 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004588 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004589 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04004590 return;
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004591 nfs_release_seqid(data->arg.open_seqid);
4592out_release_lock_seqid:
4593 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004594 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004595}
4596
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004597static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4598{
4599 struct nfs4_lockdata *data = calldata;
4600
Harvey Harrison3110ff82008-05-02 13:42:44 -07004601 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004602
Trond Myklebust14516c32010-07-31 14:29:06 -04004603 if (!nfs4_sequence_done(task, &data->res.seq_res))
4604 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004605
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004606 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004607 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004608 if (data->rpc_status == 0)
4609 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4610 else
4611 goto out;
4612 }
4613 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004614 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004615 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04004616 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004617 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004618out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004619 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004620}
4621
4622static void nfs4_lock_release(void *calldata)
4623{
4624 struct nfs4_lockdata *data = calldata;
4625
Harvey Harrison3110ff82008-05-02 13:42:44 -07004626 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004627 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004628 if (data->cancelled != 0) {
4629 struct rpc_task *task;
4630 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4631 data->arg.lock_seqid);
4632 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004633 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004634 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004635 } else
4636 nfs_free_seqid(data->arg.lock_seqid);
4637 nfs4_put_lock_state(data->lsp);
4638 put_nfs_open_context(data->ctx);
4639 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004640 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004641}
4642
4643static const struct rpc_call_ops nfs4_lock_ops = {
4644 .rpc_call_prepare = nfs4_lock_prepare,
4645 .rpc_call_done = nfs4_lock_done,
4646 .rpc_release = nfs4_lock_release,
4647};
4648
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004649static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4650{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004651 switch (error) {
4652 case -NFS4ERR_ADMIN_REVOKED:
4653 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004654 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004655 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04004656 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004657 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004658 break;
4659 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004660 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004661 case -NFS4ERR_EXPIRED:
4662 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004663 };
4664}
4665
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004666static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004667{
4668 struct nfs4_lockdata *data;
4669 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004670 struct rpc_message msg = {
4671 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4672 .rpc_cred = state->owner->so_cred,
4673 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004674 struct rpc_task_setup task_setup_data = {
4675 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004676 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004677 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004678 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004679 .flags = RPC_TASK_ASYNC,
4680 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004681 int ret;
4682
Harvey Harrison3110ff82008-05-02 13:42:44 -07004683 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004684 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004685 fl->fl_u.nfs4_fl.owner,
4686 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004687 if (data == NULL)
4688 return -ENOMEM;
4689 if (IS_SETLKW(cmd))
4690 data->arg.block = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004691 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004692 msg.rpc_argp = &data->arg;
4693 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004694 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004695 if (recovery_type > NFS_LOCK_NEW) {
4696 if (recovery_type == NFS_LOCK_RECLAIM)
4697 data->arg.reclaim = NFS_LOCK_RECLAIM;
4698 nfs4_set_sequence_privileged(&data->arg.seq_args);
4699 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04004700 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004701 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004702 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004703 ret = nfs4_wait_for_completion_rpc_task(task);
4704 if (ret == 0) {
4705 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004706 if (ret)
4707 nfs4_handle_setlk_error(data->server, data->lsp,
4708 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004709 } else
4710 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004711 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004712 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004713 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714}
4715
4716static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4717{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004718 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004719 struct nfs4_exception exception = {
4720 .inode = state->inode,
4721 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004722 int err;
4723
4724 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004725 /* Cache the lock if possible... */
4726 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4727 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004728 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004729 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004730 break;
4731 nfs4_handle_exception(server, err, &exception);
4732 } while (exception.retry);
4733 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734}
4735
4736static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4737{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004738 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004739 struct nfs4_exception exception = {
4740 .inode = state->inode,
4741 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004742 int err;
4743
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004744 err = nfs4_set_lock_state(state, request);
4745 if (err != 0)
4746 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004747 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004748 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4749 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004750 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004751 switch (err) {
4752 default:
4753 goto out;
4754 case -NFS4ERR_GRACE:
4755 case -NFS4ERR_DELAY:
4756 nfs4_handle_exception(server, err, &exception);
4757 err = 0;
4758 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004759 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004760out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004761 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762}
4763
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004764#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004765/**
4766 * nfs41_check_expired_locks - possibly free a lock stateid
4767 *
4768 * @state: NFSv4 state for an inode
4769 *
4770 * Returns NFS_OK if recovery for this stateid is now finished.
4771 * Otherwise a negative NFS4ERR value is returned.
4772 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004773static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004774{
Chuck Levereb64cf92012-07-11 16:30:05 -04004775 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004776 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004777 struct nfs_server *server = NFS_SERVER(state->inode);
4778
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004779 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04004780 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004781 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4782 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004783 /* Free the stateid unless the server
4784 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04004785 if (status != -NFS4ERR_BAD_STATEID)
4786 nfs41_free_stateid(server,
4787 &lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004788 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004789 ret = status;
4790 }
4791 }
4792 };
4793
4794 return ret;
4795}
4796
4797static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4798{
4799 int status = NFS_OK;
4800
4801 if (test_bit(LK_STATE_IN_USE, &state->flags))
4802 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04004803 if (status != NFS_OK)
4804 status = nfs4_lock_expired(state, request);
4805 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004806}
4807#endif
4808
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4810{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004811 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004812 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004813 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004815 if ((fl_flags & FL_POSIX) &&
4816 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4817 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004818 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004819 status = nfs4_set_lock_state(state, request);
4820 if (status != 0)
4821 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004822 request->fl_flags |= FL_ACCESS;
4823 status = do_vfs_lock(request->fl_file, request);
4824 if (status < 0)
4825 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004826 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004827 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004828 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004829 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004830 request->fl_flags = fl_flags & ~FL_SLEEP;
4831 status = do_vfs_lock(request->fl_file, request);
4832 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004833 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004834 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004835 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004836 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004837 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004838 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004839 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004840 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4841 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004842out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004843 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004844out:
4845 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846 return status;
4847}
4848
4849static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4850{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004851 struct nfs4_exception exception = {
4852 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04004853 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004854 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855 int err;
4856
4857 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004858 err = _nfs4_proc_setlk(state, cmd, request);
4859 if (err == -NFS4ERR_DENIED)
4860 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004862 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863 } while (exception.retry);
4864 return err;
4865}
4866
4867static int
4868nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4869{
4870 struct nfs_open_context *ctx;
4871 struct nfs4_state *state;
4872 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4873 int status;
4874
4875 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004876 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877 state = ctx->state;
4878
4879 if (request->fl_start < 0 || request->fl_end < 0)
4880 return -EINVAL;
4881
Trond Myklebustd9531262009-07-21 19:22:38 -04004882 if (IS_GETLK(cmd)) {
4883 if (state != NULL)
4884 return nfs4_proc_getlk(state, F_GETLK, request);
4885 return 0;
4886 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887
4888 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4889 return -EINVAL;
4890
Trond Myklebustd9531262009-07-21 19:22:38 -04004891 if (request->fl_type == F_UNLCK) {
4892 if (state != NULL)
4893 return nfs4_proc_unlck(state, cmd, request);
4894 return 0;
4895 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896
Trond Myklebustd9531262009-07-21 19:22:38 -04004897 if (state == NULL)
4898 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04004899 /*
4900 * Don't rely on the VFS having checked the file open mode,
4901 * since it won't do this for flock() locks.
4902 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04004903 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04004904 case F_RDLCK:
4905 if (!(filp->f_mode & FMODE_READ))
4906 return -EBADF;
4907 break;
4908 case F_WRLCK:
4909 if (!(filp->f_mode & FMODE_WRITE))
4910 return -EBADF;
4911 }
4912
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913 do {
4914 status = nfs4_proc_setlk(state, cmd, request);
4915 if ((status != -EAGAIN) || IS_SETLK(cmd))
4916 break;
4917 timeout = nfs4_set_lock_task_retry(timeout);
4918 status = -ERESTARTSYS;
4919 if (signalled())
4920 break;
4921 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922 return status;
4923}
4924
Trond Myklebust888e6942005-11-04 15:38:11 -05004925int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4926{
4927 struct nfs_server *server = NFS_SERVER(state->inode);
4928 struct nfs4_exception exception = { };
4929 int err;
4930
4931 err = nfs4_set_lock_state(state, fl);
4932 if (err != 0)
4933 goto out;
4934 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004935 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004936 switch (err) {
4937 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004938 printk(KERN_ERR "NFS: %s: unhandled error "
4939 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004940 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004941 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004942 goto out;
4943 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004944 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07004945 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004946 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004947 nfs4_schedule_lease_recovery(server->nfs_client);
4948 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004949 case -NFS4ERR_BADSESSION:
4950 case -NFS4ERR_BADSLOT:
4951 case -NFS4ERR_BAD_HIGH_SLOT:
4952 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4953 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04004954 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004955 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004956 case -ERESTARTSYS:
4957 /*
4958 * The show must go on: exit, but mark the
4959 * stateid as needing recovery.
4960 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004961 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004962 case -NFS4ERR_ADMIN_REVOKED:
4963 case -NFS4ERR_BAD_STATEID:
4964 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004965 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004966 err = 0;
4967 goto out;
4968 case -ENOMEM:
4969 case -NFS4ERR_DENIED:
4970 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4971 err = 0;
4972 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004973 case -NFS4ERR_DELAY:
4974 break;
4975 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004976 err = nfs4_handle_exception(server, err, &exception);
4977 } while (exception.retry);
4978out:
4979 return err;
4980}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004981
Trond Myklebustcf470c32012-03-07 13:49:12 -05004982struct nfs_release_lockowner_data {
4983 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004984 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004985 struct nfs_release_lockowner_args args;
4986};
4987
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004988static void nfs4_release_lockowner_release(void *calldata)
4989{
Trond Myklebustcf470c32012-03-07 13:49:12 -05004990 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004991 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004992 kfree(calldata);
4993}
4994
Trond Myklebust17280172012-03-11 13:11:00 -04004995static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004996 .rpc_release = nfs4_release_lockowner_release,
4997};
4998
Trond Myklebustcf470c32012-03-07 13:49:12 -05004999int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005000{
5001 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005002 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005003 struct rpc_message msg = {
5004 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5005 };
5006
5007 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005008 return -EINVAL;
5009 data = kmalloc(sizeof(*data), GFP_NOFS);
5010 if (!data)
5011 return -ENOMEM;
5012 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005013 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005014 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5015 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5016 data->args.lock_owner.s_dev = server->s_dev;
5017 msg.rpc_argp = &data->args;
5018 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5019 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005020}
5021
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005022#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5023
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005024static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5025 const void *buf, size_t buflen,
5026 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005027{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005028 if (strcmp(key, "") != 0)
5029 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005030
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005031 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005032}
5033
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005034static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5035 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005036{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005037 if (strcmp(key, "") != 0)
5038 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005039
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005040 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005041}
5042
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005043static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5044 size_t list_len, const char *name,
5045 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005046{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005047 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005048
J. Bruce Fields096455a2006-03-20 23:23:42 -05005049 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5050 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005051
5052 if (list && len <= list_len)
5053 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005054 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005055}
5056
Andy Adamson533eb462011-06-13 18:25:56 -04005057/*
5058 * nfs_fhget will use either the mounted_on_fileid or the fileid
5059 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005060static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5061{
Andy Adamson533eb462011-06-13 18:25:56 -04005062 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5063 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5064 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005065 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005066 return;
5067
5068 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005069 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005070 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5071 fattr->nlink = 2;
5072}
5073
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005074static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5075 const struct qstr *name,
5076 struct nfs4_fs_locations *fs_locations,
5077 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005078{
5079 struct nfs_server *server = NFS_SERVER(dir);
5080 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005081 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005082 };
5083 struct nfs4_fs_locations_arg args = {
5084 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005085 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005086 .page = page,
5087 .bitmask = bitmask,
5088 };
Benny Halevy22958462009-04-01 09:22:02 -04005089 struct nfs4_fs_locations_res res = {
5090 .fs_locations = fs_locations,
5091 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005092 struct rpc_message msg = {
5093 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5094 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005095 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005096 };
5097 int status;
5098
Harvey Harrison3110ff82008-05-02 13:42:44 -07005099 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005100
5101 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5102 * is not supported */
5103 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5104 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5105 else
5106 bitmask[0] |= FATTR4_WORD0_FILEID;
5107
Trond Myklebustc228fd32007-01-13 02:28:11 -05005108 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005109 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005110 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005111 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005112 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005113 return status;
5114}
5115
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005116int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5117 const struct qstr *name,
5118 struct nfs4_fs_locations *fs_locations,
5119 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005120{
5121 struct nfs4_exception exception = { };
5122 int err;
5123 do {
5124 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005125 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005126 &exception);
5127 } while (exception.retry);
5128 return err;
5129}
5130
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005131static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5132{
5133 int status;
5134 struct nfs4_secinfo_arg args = {
5135 .dir_fh = NFS_FH(dir),
5136 .name = name,
5137 };
5138 struct nfs4_secinfo_res res = {
5139 .flavors = flavors,
5140 };
5141 struct rpc_message msg = {
5142 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5143 .rpc_argp = &args,
5144 .rpc_resp = &res,
5145 };
5146
5147 dprintk("NFS call secinfo %s\n", name->name);
5148 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5149 dprintk("NFS reply secinfo: %d\n", status);
5150 return status;
5151}
5152
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005153int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5154 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005155{
5156 struct nfs4_exception exception = { };
5157 int err;
5158 do {
5159 err = nfs4_handle_exception(NFS_SERVER(dir),
5160 _nfs4_proc_secinfo(dir, name, flavors),
5161 &exception);
5162 } while (exception.retry);
5163 return err;
5164}
5165
Andy Adamson557134a2009-04-01 09:21:53 -04005166#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005167/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005168 * Check the exchange flags returned by the server for invalid flags, having
5169 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5170 * DS flags set.
5171 */
5172static int nfs4_check_cl_exchange_flags(u32 flags)
5173{
5174 if (flags & ~EXCHGID4_FLAG_MASK_R)
5175 goto out_inval;
5176 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5177 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5178 goto out_inval;
5179 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5180 goto out_inval;
5181 return NFS_OK;
5182out_inval:
5183 return -NFS4ERR_INVAL;
5184}
5185
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005186static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005187nfs41_same_server_scope(struct nfs41_server_scope *a,
5188 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005189{
5190 if (a->server_scope_sz == b->server_scope_sz &&
5191 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5192 return true;
5193
5194 return false;
5195}
5196
Andy Adamson357f54d2010-12-14 10:11:57 -05005197/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005198 * nfs4_proc_bind_conn_to_session()
5199 *
5200 * The 4.1 client currently uses the same TCP connection for the
5201 * fore and backchannel.
5202 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005203int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005204{
5205 int status;
5206 struct nfs41_bind_conn_to_session_res res;
5207 struct rpc_message msg = {
5208 .rpc_proc =
5209 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5210 .rpc_argp = clp,
5211 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005212 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005213 };
5214
5215 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005216
5217 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5218 if (unlikely(res.session == NULL)) {
5219 status = -ENOMEM;
5220 goto out;
5221 }
5222
5223 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5224 if (status == 0) {
5225 if (memcmp(res.session->sess_id.data,
5226 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5227 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5228 status = -EIO;
5229 goto out_session;
5230 }
5231 if (res.dir != NFS4_CDFS4_BOTH) {
5232 dprintk("NFS: %s: Unexpected direction from server\n",
5233 __func__);
5234 status = -EIO;
5235 goto out_session;
5236 }
5237 if (res.use_conn_in_rdma_mode) {
5238 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5239 __func__);
5240 status = -EIO;
5241 goto out_session;
5242 }
5243 }
5244out_session:
5245 kfree(res.session);
5246out:
5247 dprintk("<-- %s status= %d\n", __func__, status);
5248 return status;
5249}
5250
5251/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005252 * nfs4_proc_exchange_id()
5253 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005254 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5255 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04005256 * Since the clientid has expired, all compounds using sessions
5257 * associated with the stale clientid will be returning
5258 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5259 * be in some phase of session reset.
5260 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005261int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005262{
5263 nfs4_verifier verifier;
5264 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005265 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005266 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005267 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005268 };
5269 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005270 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005271 };
5272 int status;
5273 struct rpc_message msg = {
5274 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5275 .rpc_argp = &args,
5276 .rpc_resp = &res,
5277 .rpc_cred = cred,
5278 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005279
Chuck Leverf0920752012-05-21 22:45:41 -04005280 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04005281 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5282 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005283 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5284 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5285 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005286
Chuck Leveracdeb692012-05-21 22:46:16 -04005287 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005288 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005289 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005290 status = -ENOMEM;
5291 goto out;
5292 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005293
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005294 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005295 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005296 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005297 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005298 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005299 }
5300
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005301 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005302 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005303 status = -ENOMEM;
5304 goto out_server_scope;
5305 }
5306
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005307 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005308 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005309 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005310
Chuck Lever177313f2012-05-21 22:44:58 -04005311 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005312 clp->cl_clientid = res.clientid;
5313 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5314 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5315 clp->cl_seqid = res.seqid;
5316
Chuck Leveracdeb692012-05-21 22:46:16 -04005317 kfree(clp->cl_serverowner);
5318 clp->cl_serverowner = res.server_owner;
5319 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005320
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005321 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005322 kfree(clp->cl_implid);
5323 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005324
Chuck Lever177313f2012-05-21 22:44:58 -04005325 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005326 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005327 res.server_scope)) {
5328 dprintk("%s: server_scope mismatch detected\n",
5329 __func__);
5330 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005331 kfree(clp->cl_serverscope);
5332 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005333 }
5334
Chuck Lever177313f2012-05-21 22:44:58 -04005335 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005336 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005337 goto out;
5338 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005339 } else
5340 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005341
Chuck Leveracdeb692012-05-21 22:46:16 -04005342out_server_owner:
5343 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005344out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005345 kfree(res.server_scope);
5346out:
Chuck Lever177313f2012-05-21 22:44:58 -04005347 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005348 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005349 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005350 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04005351 clp->cl_implid->date.seconds,
5352 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005353 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005354 return status;
5355}
5356
Trond Myklebust66245532012-05-25 17:18:09 -04005357static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5358 struct rpc_cred *cred)
5359{
5360 struct rpc_message msg = {
5361 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5362 .rpc_argp = clp,
5363 .rpc_cred = cred,
5364 };
5365 int status;
5366
5367 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5368 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04005369 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04005370 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5371 return status;
5372}
5373
5374static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5375 struct rpc_cred *cred)
5376{
5377 unsigned int loop;
5378 int ret;
5379
5380 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5381 ret = _nfs4_proc_destroy_clientid(clp, cred);
5382 switch (ret) {
5383 case -NFS4ERR_DELAY:
5384 case -NFS4ERR_CLIENTID_BUSY:
5385 ssleep(1);
5386 break;
5387 default:
5388 return ret;
5389 }
5390 }
5391 return 0;
5392}
5393
5394int nfs4_destroy_clientid(struct nfs_client *clp)
5395{
5396 struct rpc_cred *cred;
5397 int ret = 0;
5398
5399 if (clp->cl_mvops->minor_version < 1)
5400 goto out;
5401 if (clp->cl_exchange_flags == 0)
5402 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04005403 if (clp->cl_preserve_clid)
5404 goto out;
Trond Myklebust66245532012-05-25 17:18:09 -04005405 cred = nfs4_get_exchange_id_cred(clp);
5406 ret = nfs4_proc_destroy_clientid(clp, cred);
5407 if (cred)
5408 put_rpccred(cred);
5409 switch (ret) {
5410 case 0:
5411 case -NFS4ERR_STALE_CLIENTID:
5412 clp->cl_exchange_flags = 0;
5413 }
5414out:
5415 return ret;
5416}
5417
Andy Adamson2050f0c2009-04-01 09:22:30 -04005418struct nfs4_get_lease_time_data {
5419 struct nfs4_get_lease_time_args *args;
5420 struct nfs4_get_lease_time_res *res;
5421 struct nfs_client *clp;
5422};
5423
5424static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5425 void *calldata)
5426{
Andy Adamson2050f0c2009-04-01 09:22:30 -04005427 struct nfs4_get_lease_time_data *data =
5428 (struct nfs4_get_lease_time_data *)calldata;
5429
5430 dprintk("--> %s\n", __func__);
5431 /* just setup sequence, do not trigger session recovery
5432 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005433 nfs41_setup_sequence(data->clp->cl_session,
5434 &data->args->la_seq_args,
5435 &data->res->lr_seq_res,
5436 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005437 dprintk("<-- %s\n", __func__);
5438}
5439
5440/*
5441 * Called from nfs4_state_manager thread for session setup, so don't recover
5442 * from sequence operation or clientid errors.
5443 */
5444static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5445{
5446 struct nfs4_get_lease_time_data *data =
5447 (struct nfs4_get_lease_time_data *)calldata;
5448
5449 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005450 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5451 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005452 switch (task->tk_status) {
5453 case -NFS4ERR_DELAY:
5454 case -NFS4ERR_GRACE:
5455 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5456 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5457 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005458 /* fall through */
5459 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005460 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005461 return;
5462 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005463 dprintk("<-- %s\n", __func__);
5464}
5465
Trond Myklebust17280172012-03-11 13:11:00 -04005466static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005467 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5468 .rpc_call_done = nfs4_get_lease_time_done,
5469};
5470
5471int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5472{
5473 struct rpc_task *task;
5474 struct nfs4_get_lease_time_args args;
5475 struct nfs4_get_lease_time_res res = {
5476 .lr_fsinfo = fsinfo,
5477 };
5478 struct nfs4_get_lease_time_data data = {
5479 .args = &args,
5480 .res = &res,
5481 .clp = clp,
5482 };
5483 struct rpc_message msg = {
5484 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5485 .rpc_argp = &args,
5486 .rpc_resp = &res,
5487 };
5488 struct rpc_task_setup task_setup = {
5489 .rpc_client = clp->cl_rpcclient,
5490 .rpc_message = &msg,
5491 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005492 .callback_data = &data,
5493 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005494 };
5495 int status;
5496
Trond Myklebust9d12b212012-01-17 22:04:25 -05005497 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005498 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005499 dprintk("--> %s\n", __func__);
5500 task = rpc_run_task(&task_setup);
5501
5502 if (IS_ERR(task))
5503 status = PTR_ERR(task);
5504 else {
5505 status = task->tk_status;
5506 rpc_put_task(task);
5507 }
5508 dprintk("<-- %s return %d\n", __func__, status);
5509
5510 return status;
5511}
5512
Andy Adamsonfc931582009-04-01 09:22:31 -04005513/*
5514 * Initialize the values to be used by the client in CREATE_SESSION
5515 * If nfs4_init_session set the fore channel request and response sizes,
5516 * use them.
5517 *
5518 * Set the back channel max_resp_sz_cached to zero to force the client to
5519 * always set csa_cachethis to FALSE because the current implementation
5520 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5521 */
5522static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5523{
5524 struct nfs4_session *session = args->client->cl_session;
Trond Myklebustae72ae672012-11-20 11:02:55 -05005525 unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
5526 mxresp_sz = session->fc_target_max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005527
5528 if (mxrqst_sz == 0)
5529 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5530 if (mxresp_sz == 0)
5531 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5532 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005533 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5534 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005535 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005536 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005537
5538 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005539 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005540 __func__,
5541 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005542 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005543
5544 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005545 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5546 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5547 args->bc_attrs.max_resp_sz_cached = 0;
5548 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5549 args->bc_attrs.max_reqs = 1;
5550
5551 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5552 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5553 __func__,
5554 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5555 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5556 args->bc_attrs.max_reqs);
5557}
5558
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005559static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005560{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005561 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5562 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5563
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005564 if (rcvd->max_resp_sz > sent->max_resp_sz)
5565 return -EINVAL;
5566 /*
5567 * Our requested max_ops is the minimum we need; we're not
5568 * prepared to break up compounds into smaller pieces than that.
5569 * So, no point even trying to continue if the server won't
5570 * cooperate:
5571 */
5572 if (rcvd->max_ops < sent->max_ops)
5573 return -EINVAL;
5574 if (rcvd->max_reqs == 0)
5575 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005576 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5577 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005578 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005579}
5580
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005581static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5582{
5583 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5584 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005585
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005586 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5587 return -EINVAL;
5588 if (rcvd->max_resp_sz < sent->max_resp_sz)
5589 return -EINVAL;
5590 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5591 return -EINVAL;
5592 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005593 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005594 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005595 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005596 return -EINVAL;
5597 return 0;
5598}
Andy Adamson8d353012009-04-01 09:22:32 -04005599
Andy Adamson8d353012009-04-01 09:22:32 -04005600static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5601 struct nfs4_session *session)
5602{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005603 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005604
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005605 ret = nfs4_verify_fore_channel_attrs(args, session);
5606 if (ret)
5607 return ret;
5608 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005609}
5610
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005611static int _nfs4_proc_create_session(struct nfs_client *clp,
5612 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005613{
5614 struct nfs4_session *session = clp->cl_session;
5615 struct nfs41_create_session_args args = {
5616 .client = clp,
5617 .cb_program = NFS4_CALLBACK,
5618 };
5619 struct nfs41_create_session_res res = {
5620 .client = clp,
5621 };
5622 struct rpc_message msg = {
5623 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5624 .rpc_argp = &args,
5625 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005626 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04005627 };
5628 int status;
5629
5630 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005631 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005632
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005633 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005634
Trond Myklebust43095d32012-11-20 11:13:12 -05005635 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04005636 /* Verify the session's negotiated channel_attrs values */
5637 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005638 /* Increment the clientid slot sequence id */
5639 clp->cl_seqid++;
5640 }
5641
5642 return status;
5643}
5644
5645/*
5646 * Issues a CREATE_SESSION operation to the server.
5647 * It is the responsibility of the caller to verify the session is
5648 * expired before calling this routine.
5649 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005650int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005651{
5652 int status;
5653 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005654 struct nfs4_session *session = clp->cl_session;
5655
5656 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5657
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005658 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04005659 if (status)
5660 goto out;
5661
Andy Adamsonaacd5532011-11-09 13:58:21 -05005662 /* Init or reset the session slot tables */
5663 status = nfs4_setup_session_slot_tables(session);
5664 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005665 if (status)
5666 goto out;
5667
5668 ptr = (unsigned *)&session->sess_id.data[0];
5669 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5670 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005671out:
5672 dprintk("<-- %s\n", __func__);
5673 return status;
5674}
5675
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005676/*
5677 * Issue the over-the-wire RPC DESTROY_SESSION.
5678 * The caller must serialize access to this routine.
5679 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005680int nfs4_proc_destroy_session(struct nfs4_session *session,
5681 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005682{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005683 struct rpc_message msg = {
5684 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5685 .rpc_argp = session,
5686 .rpc_cred = cred,
5687 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005688 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005689
5690 dprintk("--> nfs4_proc_destroy_session\n");
5691
5692 /* session is still being setup */
5693 if (session->clp->cl_cons_state != NFS_CS_READY)
5694 return status;
5695
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005696 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005697
5698 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04005699 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005700 "Session has been destroyed regardless...\n", status);
5701
5702 dprintk("<-- nfs4_proc_destroy_session\n");
5703 return status;
5704}
5705
Trond Myklebust7b38c362012-05-23 13:23:31 -04005706/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005707 * Renew the cl_session lease.
5708 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005709struct nfs4_sequence_data {
5710 struct nfs_client *clp;
5711 struct nfs4_sequence_args args;
5712 struct nfs4_sequence_res res;
5713};
5714
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005715static void nfs41_sequence_release(void *data)
5716{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005717 struct nfs4_sequence_data *calldata = data;
5718 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005719
Alexandros Batsakis71358402010-02-05 03:45:05 -08005720 if (atomic_read(&clp->cl_count) > 1)
5721 nfs4_schedule_state_renewal(clp);
5722 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005723 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005724}
5725
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005726static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5727{
5728 switch(task->tk_status) {
5729 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005730 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5731 return -EAGAIN;
5732 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005733 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005734 }
5735 return 0;
5736}
5737
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005738static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005739{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005740 struct nfs4_sequence_data *calldata = data;
5741 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005742
Trond Myklebust14516c32010-07-31 14:29:06 -04005743 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5744 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005745
5746 if (task->tk_status < 0) {
5747 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005748 if (atomic_read(&clp->cl_count) == 1)
5749 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005750
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005751 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5752 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005753 return;
5754 }
5755 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005756 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005757out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005758 dprintk("<-- %s\n", __func__);
5759}
5760
5761static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5762{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005763 struct nfs4_sequence_data *calldata = data;
5764 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005765 struct nfs4_sequence_args *args;
5766 struct nfs4_sequence_res *res;
5767
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005768 args = task->tk_msg.rpc_argp;
5769 res = task->tk_msg.rpc_resp;
5770
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005771 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005772}
5773
5774static const struct rpc_call_ops nfs41_sequence_ops = {
5775 .rpc_call_done = nfs41_sequence_call_done,
5776 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005777 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005778};
5779
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005780static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
5781 struct rpc_cred *cred,
5782 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005783{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005784 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005785 struct rpc_message msg = {
5786 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5787 .rpc_cred = cred,
5788 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005789 struct rpc_task_setup task_setup_data = {
5790 .rpc_client = clp->cl_rpcclient,
5791 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005792 .callback_ops = &nfs41_sequence_ops,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005793 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5794 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005795
Alexandros Batsakis71358402010-02-05 03:45:05 -08005796 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005797 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005798 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005799 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005800 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005801 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005802 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05005803 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005804 if (is_privileged)
5805 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005806 msg.rpc_argp = &calldata->args;
5807 msg.rpc_resp = &calldata->res;
5808 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005809 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005810
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005811 return rpc_run_task(&task_setup_data);
5812}
5813
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005814static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005815{
5816 struct rpc_task *task;
5817 int ret = 0;
5818
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005819 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5820 return 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005821 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005822 if (IS_ERR(task))
5823 ret = PTR_ERR(task);
5824 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005825 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005826 dprintk("<-- %s status=%d\n", __func__, ret);
5827 return ret;
5828}
5829
5830static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5831{
5832 struct rpc_task *task;
5833 int ret;
5834
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005835 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005836 if (IS_ERR(task)) {
5837 ret = PTR_ERR(task);
5838 goto out;
5839 }
5840 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005841 if (!ret) {
5842 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5843
5844 if (task->tk_status == 0)
5845 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005846 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005847 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005848 rpc_put_task(task);
5849out:
5850 dprintk("<-- %s status=%d\n", __func__, ret);
5851 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005852}
5853
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005854struct nfs4_reclaim_complete_data {
5855 struct nfs_client *clp;
5856 struct nfs41_reclaim_complete_args arg;
5857 struct nfs41_reclaim_complete_res res;
5858};
5859
5860static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5861{
5862 struct nfs4_reclaim_complete_data *calldata = data;
5863
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005864 nfs41_setup_sequence(calldata->clp->cl_session,
5865 &calldata->arg.seq_args,
5866 &calldata->res.seq_res,
5867 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005868}
5869
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005870static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5871{
5872 switch(task->tk_status) {
5873 case 0:
5874 case -NFS4ERR_COMPLETE_ALREADY:
5875 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5876 break;
5877 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005878 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005879 /* fall through */
5880 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005881 return -EAGAIN;
5882 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005883 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005884 }
5885 return 0;
5886}
5887
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005888static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5889{
5890 struct nfs4_reclaim_complete_data *calldata = data;
5891 struct nfs_client *clp = calldata->clp;
5892 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5893
5894 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005895 if (!nfs41_sequence_done(task, res))
5896 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005897
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005898 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5899 rpc_restart_call_prepare(task);
5900 return;
5901 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005902 dprintk("<-- %s\n", __func__);
5903}
5904
5905static void nfs4_free_reclaim_complete_data(void *data)
5906{
5907 struct nfs4_reclaim_complete_data *calldata = data;
5908
5909 kfree(calldata);
5910}
5911
5912static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5913 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5914 .rpc_call_done = nfs4_reclaim_complete_done,
5915 .rpc_release = nfs4_free_reclaim_complete_data,
5916};
5917
5918/*
5919 * Issue a global reclaim complete.
5920 */
5921static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5922{
5923 struct nfs4_reclaim_complete_data *calldata;
5924 struct rpc_task *task;
5925 struct rpc_message msg = {
5926 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5927 };
5928 struct rpc_task_setup task_setup_data = {
5929 .rpc_client = clp->cl_rpcclient,
5930 .rpc_message = &msg,
5931 .callback_ops = &nfs4_reclaim_complete_call_ops,
5932 .flags = RPC_TASK_ASYNC,
5933 };
5934 int status = -ENOMEM;
5935
5936 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005937 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005938 if (calldata == NULL)
5939 goto out;
5940 calldata->clp = clp;
5941 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005942
Trond Myklebust9d12b212012-01-17 22:04:25 -05005943 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005944 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005945 msg.rpc_argp = &calldata->arg;
5946 msg.rpc_resp = &calldata->res;
5947 task_setup_data.callback_data = calldata;
5948 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005949 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005950 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005951 goto out;
5952 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05005953 status = nfs4_wait_for_completion_rpc_task(task);
5954 if (status == 0)
5955 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005956 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005957 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005958out:
5959 dprintk("<-- %s status=%d\n", __func__, status);
5960 return status;
5961}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005962
5963static void
5964nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5965{
5966 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00005967 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04005968 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005969
5970 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00005971 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5972 * right now covering the LAYOUTGET we are about to send.
5973 * However, that is not so catastrophic, and there seems
5974 * to be no way to prevent it completely.
5975 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04005976 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005977 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005978 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00005979 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5980 NFS_I(lgp->args.inode)->layout,
5981 lgp->args.ctx->state)) {
5982 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00005983 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005984}
5985
5986static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5987{
5988 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07005989 struct inode *inode = lgp->args.inode;
5990 struct nfs_server *server = NFS_SERVER(inode);
5991 struct pnfs_layout_hdr *lo;
5992 struct nfs4_state *state = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005993
5994 dprintk("--> %s\n", __func__);
5995
Trond Myklebust6ba7db32012-10-22 20:07:20 -04005996 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07005997 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005998
5999 switch (task->tk_status) {
6000 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07006001 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006002 case -NFS4ERR_LAYOUTTRYLATER:
6003 case -NFS4ERR_RECALLCONFLICT:
6004 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07006005 break;
6006 case -NFS4ERR_EXPIRED:
6007 case -NFS4ERR_BAD_STATEID:
6008 spin_lock(&inode->i_lock);
6009 lo = NFS_I(inode)->layout;
6010 if (!lo || list_empty(&lo->plh_segs)) {
6011 spin_unlock(&inode->i_lock);
6012 /* If the open stateid was bad, then recover it. */
6013 state = lgp->args.ctx->state;
6014 } else {
6015 LIST_HEAD(head);
6016
6017 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6018 spin_unlock(&inode->i_lock);
6019 /* Mark the bad layout state as invalid, then
6020 * retry using the open stateid. */
6021 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006022 }
6023 }
Trond Myklebustee314c22012-10-01 17:25:48 -07006024 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6025 rpc_restart_call_prepare(task);
6026out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006027 dprintk("<-- %s\n", __func__);
6028}
6029
Idan Kedar85541162012-08-02 11:47:10 +03006030static size_t max_response_pages(struct nfs_server *server)
6031{
6032 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6033 return nfs_page_array_len(0, max_resp_sz);
6034}
6035
6036static void nfs4_free_pages(struct page **pages, size_t size)
6037{
6038 int i;
6039
6040 if (!pages)
6041 return;
6042
6043 for (i = 0; i < size; i++) {
6044 if (!pages[i])
6045 break;
6046 __free_page(pages[i]);
6047 }
6048 kfree(pages);
6049}
6050
6051static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6052{
6053 struct page **pages;
6054 int i;
6055
6056 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6057 if (!pages) {
6058 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6059 return NULL;
6060 }
6061
6062 for (i = 0; i < size; i++) {
6063 pages[i] = alloc_page(gfp_flags);
6064 if (!pages[i]) {
6065 dprintk("%s: failed to allocate page\n", __func__);
6066 nfs4_free_pages(pages, size);
6067 return NULL;
6068 }
6069 }
6070
6071 return pages;
6072}
6073
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006074static void nfs4_layoutget_release(void *calldata)
6075{
6076 struct nfs4_layoutget *lgp = calldata;
Idan Kedar85541162012-08-02 11:47:10 +03006077 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6078 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006079
6080 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006081 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006082 put_nfs_open_context(lgp->args.ctx);
6083 kfree(calldata);
6084 dprintk("<-- %s\n", __func__);
6085}
6086
6087static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6088 .rpc_call_prepare = nfs4_layoutget_prepare,
6089 .rpc_call_done = nfs4_layoutget_done,
6090 .rpc_release = nfs4_layoutget_release,
6091};
6092
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006093struct pnfs_layout_segment *
6094nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006095{
6096 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Idan Kedar85541162012-08-02 11:47:10 +03006097 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006098 struct rpc_task *task;
6099 struct rpc_message msg = {
6100 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6101 .rpc_argp = &lgp->args,
6102 .rpc_resp = &lgp->res,
6103 };
6104 struct rpc_task_setup task_setup_data = {
6105 .rpc_client = server->client,
6106 .rpc_message = &msg,
6107 .callback_ops = &nfs4_layoutget_call_ops,
6108 .callback_data = lgp,
6109 .flags = RPC_TASK_ASYNC,
6110 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006111 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006112 int status = 0;
6113
6114 dprintk("--> %s\n", __func__);
6115
Idan Kedar85541162012-08-02 11:47:10 +03006116 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6117 if (!lgp->args.layout.pages) {
6118 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006119 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006120 }
6121 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
6122
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006123 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006124 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006125 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006126 task = rpc_run_task(&task_setup_data);
6127 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006128 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006129 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006130 if (status == 0)
6131 status = task->tk_status;
6132 if (status == 0)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006133 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006134 rpc_put_task(task);
6135 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006136 if (status)
6137 return ERR_PTR(status);
6138 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006139}
6140
Benny Halevycbe82602011-05-22 19:52:37 +03006141static void
6142nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6143{
6144 struct nfs4_layoutreturn *lrp = calldata;
6145
6146 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006147 nfs41_setup_sequence(lrp->clp->cl_session,
6148 &lrp->args.seq_args,
6149 &lrp->res.seq_res,
6150 task);
Benny Halevycbe82602011-05-22 19:52:37 +03006151}
6152
6153static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6154{
6155 struct nfs4_layoutreturn *lrp = calldata;
6156 struct nfs_server *server;
6157
6158 dprintk("--> %s\n", __func__);
6159
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006160 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03006161 return;
6162
6163 server = NFS_SERVER(lrp->args.inode);
6164 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006165 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006166 return;
6167 }
Benny Halevycbe82602011-05-22 19:52:37 +03006168 dprintk("<-- %s\n", __func__);
6169}
6170
6171static void nfs4_layoutreturn_release(void *calldata)
6172{
6173 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04006174 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006175
6176 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04006177 spin_lock(&lo->plh_inode->i_lock);
6178 if (lrp->res.lrs_present)
6179 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6180 lo->plh_block_lgets--;
6181 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006182 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006183 kfree(calldata);
6184 dprintk("<-- %s\n", __func__);
6185}
6186
6187static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6188 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6189 .rpc_call_done = nfs4_layoutreturn_done,
6190 .rpc_release = nfs4_layoutreturn_release,
6191};
6192
6193int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6194{
6195 struct rpc_task *task;
6196 struct rpc_message msg = {
6197 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6198 .rpc_argp = &lrp->args,
6199 .rpc_resp = &lrp->res,
6200 };
6201 struct rpc_task_setup task_setup_data = {
6202 .rpc_client = lrp->clp->cl_rpcclient,
6203 .rpc_message = &msg,
6204 .callback_ops = &nfs4_layoutreturn_call_ops,
6205 .callback_data = lrp,
6206 };
6207 int status;
6208
6209 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006210 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006211 task = rpc_run_task(&task_setup_data);
6212 if (IS_ERR(task))
6213 return PTR_ERR(task);
6214 status = task->tk_status;
6215 dprintk("<-- %s status=%d\n", __func__, status);
6216 rpc_put_task(task);
6217 return status;
6218}
6219
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006220/*
6221 * Retrieve the list of Data Server devices from the MDS.
6222 */
6223static int _nfs4_getdevicelist(struct nfs_server *server,
6224 const struct nfs_fh *fh,
6225 struct pnfs_devicelist *devlist)
6226{
6227 struct nfs4_getdevicelist_args args = {
6228 .fh = fh,
6229 .layoutclass = server->pnfs_curr_ld->id,
6230 };
6231 struct nfs4_getdevicelist_res res = {
6232 .devlist = devlist,
6233 };
6234 struct rpc_message msg = {
6235 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6236 .rpc_argp = &args,
6237 .rpc_resp = &res,
6238 };
6239 int status;
6240
6241 dprintk("--> %s\n", __func__);
6242 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6243 &res.seq_res, 0);
6244 dprintk("<-- %s status=%d\n", __func__, status);
6245 return status;
6246}
6247
6248int nfs4_proc_getdevicelist(struct nfs_server *server,
6249 const struct nfs_fh *fh,
6250 struct pnfs_devicelist *devlist)
6251{
6252 struct nfs4_exception exception = { };
6253 int err;
6254
6255 do {
6256 err = nfs4_handle_exception(server,
6257 _nfs4_getdevicelist(server, fh, devlist),
6258 &exception);
6259 } while (exception.retry);
6260
6261 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6262 err, devlist->num_devs);
6263
6264 return err;
6265}
6266EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6267
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006268static int
6269_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6270{
6271 struct nfs4_getdeviceinfo_args args = {
6272 .pdev = pdev,
6273 };
6274 struct nfs4_getdeviceinfo_res res = {
6275 .pdev = pdev,
6276 };
6277 struct rpc_message msg = {
6278 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6279 .rpc_argp = &args,
6280 .rpc_resp = &res,
6281 };
6282 int status;
6283
6284 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006285 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006286 dprintk("<-- %s status=%d\n", __func__, status);
6287
6288 return status;
6289}
6290
6291int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6292{
6293 struct nfs4_exception exception = { };
6294 int err;
6295
6296 do {
6297 err = nfs4_handle_exception(server,
6298 _nfs4_proc_getdeviceinfo(server, pdev),
6299 &exception);
6300 } while (exception.retry);
6301 return err;
6302}
6303EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6304
Andy Adamson863a3c62011-03-23 13:27:54 +00006305static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6306{
6307 struct nfs4_layoutcommit_data *data = calldata;
6308 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006309 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00006310
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006311 nfs41_setup_sequence(session,
6312 &data->args.seq_args,
6313 &data->res.seq_res,
6314 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00006315}
6316
6317static void
6318nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6319{
6320 struct nfs4_layoutcommit_data *data = calldata;
6321 struct nfs_server *server = NFS_SERVER(data->args.inode);
6322
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006323 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00006324 return;
6325
6326 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006327 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6328 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6329 case -NFS4ERR_BADLAYOUT: /* no layout */
6330 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006331 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006332 break;
6333 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006334 nfs_post_op_update_inode_force_wcc(data->args.inode,
6335 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006336 break;
6337 default:
6338 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6339 rpc_restart_call_prepare(task);
6340 return;
6341 }
6342 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006343}
6344
6345static void nfs4_layoutcommit_release(void *calldata)
6346{
6347 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04006348 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07006349 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00006350
Andy Adamsondb29c082011-07-30 20:52:38 -04006351 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006352 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04006353 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6354 list_del_init(&lseg->pls_lc_list);
6355 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6356 &lseg->pls_flags))
Trond Myklebust9369a432012-09-18 20:57:08 -04006357 pnfs_put_lseg(lseg);
Peng Taoa9bae562011-07-30 20:52:33 -04006358 }
Peng Tao92407e72011-10-23 20:21:17 -07006359
6360 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6361 smp_mb__after_clear_bit();
6362 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6363
Andy Adamson863a3c62011-03-23 13:27:54 +00006364 put_rpccred(data->cred);
6365 kfree(data);
6366}
6367
6368static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6369 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6370 .rpc_call_done = nfs4_layoutcommit_done,
6371 .rpc_release = nfs4_layoutcommit_release,
6372};
6373
6374int
Andy Adamsonef311532011-03-12 02:58:10 -05006375nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006376{
6377 struct rpc_message msg = {
6378 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6379 .rpc_argp = &data->args,
6380 .rpc_resp = &data->res,
6381 .rpc_cred = data->cred,
6382 };
6383 struct rpc_task_setup task_setup_data = {
6384 .task = &data->task,
6385 .rpc_client = NFS_CLIENT(data->args.inode),
6386 .rpc_message = &msg,
6387 .callback_ops = &nfs4_layoutcommit_ops,
6388 .callback_data = data,
6389 .flags = RPC_TASK_ASYNC,
6390 };
6391 struct rpc_task *task;
6392 int status = 0;
6393
6394 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6395 "lbw: %llu inode %lu\n",
6396 data->task.tk_pid, sync,
6397 data->args.lastbytewritten,
6398 data->args.inode->i_ino);
6399
Trond Myklebust9d12b212012-01-17 22:04:25 -05006400 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006401 task = rpc_run_task(&task_setup_data);
6402 if (IS_ERR(task))
6403 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006404 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006405 goto out;
6406 status = nfs4_wait_for_completion_rpc_task(task);
6407 if (status != 0)
6408 goto out;
6409 status = task->tk_status;
6410out:
6411 dprintk("%s: status %d\n", __func__, status);
6412 rpc_put_task(task);
6413 return status;
6414}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006415
6416static int
6417_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6418 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6419{
6420 struct nfs41_secinfo_no_name_args args = {
6421 .style = SECINFO_STYLE_CURRENT_FH,
6422 };
6423 struct nfs4_secinfo_res res = {
6424 .flavors = flavors,
6425 };
6426 struct rpc_message msg = {
6427 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6428 .rpc_argp = &args,
6429 .rpc_resp = &res,
6430 };
6431 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6432}
6433
6434static int
6435nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6436 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6437{
6438 struct nfs4_exception exception = { };
6439 int err;
6440 do {
6441 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6442 switch (err) {
6443 case 0:
6444 case -NFS4ERR_WRONGSEC:
6445 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006446 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006447 default:
6448 err = nfs4_handle_exception(server, err, &exception);
6449 }
6450 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006451out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006452 return err;
6453}
6454
6455static int
6456nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6457 struct nfs_fsinfo *info)
6458{
6459 int err;
6460 struct page *page;
6461 rpc_authflavor_t flavor;
6462 struct nfs4_secinfo_flavors *flavors;
6463
6464 page = alloc_page(GFP_KERNEL);
6465 if (!page) {
6466 err = -ENOMEM;
6467 goto out;
6468 }
6469
6470 flavors = page_address(page);
6471 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6472
6473 /*
6474 * Fall back on "guess and check" method if
6475 * the server doesn't support SECINFO_NO_NAME
6476 */
6477 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6478 err = nfs4_find_root_sec(server, fhandle, info);
6479 goto out_freepage;
6480 }
6481 if (err)
6482 goto out_freepage;
6483
6484 flavor = nfs_find_best_sec(flavors);
6485 if (err == 0)
6486 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6487
6488out_freepage:
6489 put_page(page);
6490 if (err == -EACCES)
6491 return -EPERM;
6492out:
6493 return err;
6494}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006495
6496static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006497{
6498 int status;
6499 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006500 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006501 };
6502 struct nfs41_test_stateid_res res;
6503 struct rpc_message msg = {
6504 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6505 .rpc_argp = &args,
6506 .rpc_resp = &res,
6507 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006508
Chuck Lever38527b12012-07-11 16:30:23 -04006509 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006510 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006511 nfs4_set_sequence_privileged(&args.seq_args);
6512 status = nfs4_call_sync_sequence(server->client, server, &msg,
6513 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04006514 if (status != NFS_OK) {
6515 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04006516 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04006517 }
6518 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04006519 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006520}
6521
Chuck Lever38527b12012-07-11 16:30:23 -04006522/**
6523 * nfs41_test_stateid - perform a TEST_STATEID operation
6524 *
6525 * @server: server / transport on which to perform the operation
6526 * @stateid: state ID to test
6527 *
6528 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6529 * Otherwise a negative NFS4ERR value is returned if the operation
6530 * failed or the state ID is not currently valid.
6531 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006532static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006533{
6534 struct nfs4_exception exception = { };
6535 int err;
6536 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006537 err = _nfs41_test_stateid(server, stateid);
6538 if (err != -NFS4ERR_DELAY)
6539 break;
6540 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04006541 } while (exception.retry);
6542 return err;
6543}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006544
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006545static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006546{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006547 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006548 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006549 };
6550 struct nfs41_free_stateid_res res;
6551 struct rpc_message msg = {
6552 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6553 .rpc_argp = &args,
6554 .rpc_resp = &res,
6555 };
Chuck Lever38527b12012-07-11 16:30:23 -04006556 int status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006557
Chuck Lever38527b12012-07-11 16:30:23 -04006558 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006559 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006560 nfs4_set_sequence_privileged(&args.seq_args);
Chuck Lever38527b12012-07-11 16:30:23 -04006561 status = nfs4_call_sync_sequence(server->client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006562 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04006563 dprintk("NFS reply free_stateid: %d\n", status);
6564 return status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006565}
6566
Chuck Lever38527b12012-07-11 16:30:23 -04006567/**
6568 * nfs41_free_stateid - perform a FREE_STATEID operation
6569 *
6570 * @server: server / transport on which to perform the operation
6571 * @stateid: state ID to release
6572 *
6573 * Returns NFS_OK if the server freed "stateid". Otherwise a
6574 * negative NFS4ERR value is returned.
6575 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006576static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006577{
6578 struct nfs4_exception exception = { };
6579 int err;
6580 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006581 err = _nfs4_free_stateid(server, stateid);
6582 if (err != -NFS4ERR_DELAY)
6583 break;
6584 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006585 } while (exception.retry);
6586 return err;
6587}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006588
6589static bool nfs41_match_stateid(const nfs4_stateid *s1,
6590 const nfs4_stateid *s2)
6591{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006592 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006593 return false;
6594
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006595 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006596 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006597 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006598 return true;
6599
6600 return false;
6601}
6602
Andy Adamson557134a2009-04-01 09:21:53 -04006603#endif /* CONFIG_NFS_V4_1 */
6604
Trond Myklebust36281ca2012-03-04 18:13:56 -05006605static bool nfs4_match_stateid(const nfs4_stateid *s1,
6606 const nfs4_stateid *s2)
6607{
Trond Myklebustf597c532012-03-04 18:13:56 -05006608 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006609}
6610
6611
Trond Myklebust17280172012-03-11 13:11:00 -04006612static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006613 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006614 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006615 .recover_open = nfs4_open_reclaim,
6616 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006617 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006618 .get_clid_cred = nfs4_get_setclientid_cred,
Chuck Lever05f4c352012-09-14 17:24:32 -04006619 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006620};
6621
Andy Adamson591d71c2009-04-01 09:22:47 -04006622#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006623static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006624 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6625 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6626 .recover_open = nfs4_open_reclaim,
6627 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006628 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006629 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006630 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04006631 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04006632};
6633#endif /* CONFIG_NFS_V4_1 */
6634
Trond Myklebust17280172012-03-11 13:11:00 -04006635static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006636 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006637 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006638 .recover_open = nfs4_open_expired,
6639 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006640 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006641 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006642};
6643
Andy Adamson591d71c2009-04-01 09:22:47 -04006644#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006645static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006646 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6647 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006648 .recover_open = nfs41_open_expired,
6649 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006650 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006651 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006652};
6653#endif /* CONFIG_NFS_V4_1 */
6654
Trond Myklebust17280172012-03-11 13:11:00 -04006655static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006656 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006657 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006658 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006659};
6660
6661#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006662static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006663 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006664 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006665 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006666};
6667#endif
6668
Trond Myklebust97dc1352010-06-16 09:52:26 -04006669static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6670 .minor_version = 0,
6671 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006672 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006673 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006674 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6675 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6676 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006677};
6678
6679#if defined(CONFIG_NFS_V4_1)
6680static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6681 .minor_version = 1,
Bryan Schumaker104287c2012-11-12 14:13:13 -05006682 .call_sync = nfs4_call_sync_sequence,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006683 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006684 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006685 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6686 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6687 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006688};
6689#endif
6690
Trond Myklebust97dc1352010-06-16 09:52:26 -04006691const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6692 [0] = &nfs_v4_0_minor_ops,
6693#if defined(CONFIG_NFS_V4_1)
6694 [1] = &nfs_v4_1_minor_ops,
6695#endif
6696};
6697
Bryan Schumaker73a79702012-07-16 16:39:12 -04006698const struct inode_operations nfs4_dir_inode_operations = {
6699 .create = nfs_create,
6700 .lookup = nfs_lookup,
6701 .atomic_open = nfs_atomic_open,
6702 .link = nfs_link,
6703 .unlink = nfs_unlink,
6704 .symlink = nfs_symlink,
6705 .mkdir = nfs_mkdir,
6706 .rmdir = nfs_rmdir,
6707 .mknod = nfs_mknod,
6708 .rename = nfs_rename,
6709 .permission = nfs_permission,
6710 .getattr = nfs_getattr,
6711 .setattr = nfs_setattr,
6712 .getxattr = generic_getxattr,
6713 .setxattr = generic_setxattr,
6714 .listxattr = generic_listxattr,
6715 .removexattr = generic_removexattr,
6716};
6717
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006718static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006719 .permission = nfs_permission,
6720 .getattr = nfs_getattr,
6721 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006722 .getxattr = generic_getxattr,
6723 .setxattr = generic_setxattr,
6724 .listxattr = generic_listxattr,
6725 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006726};
6727
David Howells509de812006-08-22 20:06:11 -04006728const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006729 .version = 4, /* protocol version */
6730 .dentry_ops = &nfs4_dentry_operations,
6731 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006732 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006733 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006734 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04006735 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04006736 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006737 .getattr = nfs4_proc_getattr,
6738 .setattr = nfs4_proc_setattr,
6739 .lookup = nfs4_proc_lookup,
6740 .access = nfs4_proc_access,
6741 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006742 .create = nfs4_proc_create,
6743 .remove = nfs4_proc_remove,
6744 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04006745 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006746 .unlink_done = nfs4_proc_unlink_done,
6747 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006748 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04006749 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04006750 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006751 .link = nfs4_proc_link,
6752 .symlink = nfs4_proc_symlink,
6753 .mkdir = nfs4_proc_mkdir,
6754 .rmdir = nfs4_proc_remove,
6755 .readdir = nfs4_proc_readdir,
6756 .mknod = nfs4_proc_mknod,
6757 .statfs = nfs4_proc_statfs,
6758 .fsinfo = nfs4_proc_fsinfo,
6759 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006760 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006761 .decode_dirent = nfs4_decode_dirent,
6762 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04006763 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04006764 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05006765 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006766 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04006767 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04006768 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006769 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006770 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04006771 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006772 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006773 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006774 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006775 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006776 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04006777 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04006778 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04006779 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00006780 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04006781 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04006782 .create_server = nfs4_create_server,
6783 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006784};
6785
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006786static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6787 .prefix = XATTR_NAME_NFSV4_ACL,
6788 .list = nfs4_xattr_list_nfs4_acl,
6789 .get = nfs4_xattr_get_nfs4_acl,
6790 .set = nfs4_xattr_set_nfs4_acl,
6791};
6792
6793const struct xattr_handler *nfs4_xattr_handlers[] = {
6794 &nfs4_xattr_nfs4_acl_handler,
6795 NULL
6796};
6797
Linus Torvalds1da177e2005-04-16 15:20:36 -07006798/*
6799 * Local variables:
6800 * c-basic-offset: 8
6801 * End:
6802 */