blob: eae83bf96c6dd3255376e7d1f37c7de1e951a1f3 [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"
David Howellsde242c02012-12-20 21:52:38 +000067#include "fscache.h"
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;
David Howellsde242c02012-12-20 21:52:38 +0000737 nfs_fscache_invalidate(dir);
Trond Myklebust38478b22006-05-25 01:40:57 -0400738 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739}
740
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100741struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400742 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100743 struct nfs_openargs o_arg;
744 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100745 struct nfs_open_confirmargs c_arg;
746 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500747 struct nfs4_string owner_name;
748 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100749 struct nfs_fattr f_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100750 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400751 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100752 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400753 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100754 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100755 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400756 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100757 int rpc_status;
758 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100759};
760
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400761
762static void nfs4_init_opendata_res(struct nfs4_opendata *p)
763{
764 p->o_res.f_attr = &p->f_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400765 p->o_res.seqid = p->o_arg.seqid;
766 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400767 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400768 p->o_res.access_request = p->o_arg.access;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400769 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500770 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400771}
772
Al Viro82a2c1b2011-06-22 18:30:55 -0400773static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500774 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400775 const struct iattr *attrs,
776 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100777{
Al Viro82a2c1b2011-06-22 18:30:55 -0400778 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100779 struct inode *dir = parent->d_inode;
780 struct nfs_server *server = NFS_SERVER(dir);
781 struct nfs4_opendata *p;
782
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400783 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100784 if (p == NULL)
785 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400786 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100787 if (p->o_arg.seqid == NULL)
788 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400789 nfs_sb_active(dentry->d_sb);
790 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100791 p->dir = parent;
792 p->owner = sp;
793 atomic_inc(&sp->so_count);
794 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500795 p->o_arg.open_flags = flags;
796 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700797 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
798 * will return permission denied for all bits until close */
799 if (!(flags & O_EXCL)) {
800 /* ask server to check for all possible rights as results
801 * are cached */
802 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
803 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700804 }
David Howells7539bba2006-08-22 20:06:09 -0400805 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400806 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
807 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400808 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100809 p->o_arg.server = server;
810 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust1549210f2012-06-05 09:16:47 -0400811 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100812 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust536e43d2012-01-17 22:04:26 -0500813 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -0500814 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400815
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100816 p->o_arg.u.attrs = &p->attrs;
817 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500818
819 verf[0] = jiffies;
820 verf[1] = current->pid;
821 memcpy(p->o_arg.u.verifier.data, verf,
822 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100823 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100824 p->c_arg.fh = &p->o_res.fh;
825 p->c_arg.stateid = &p->o_res.stateid;
826 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400827 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400828 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100829 return p;
830err_free:
831 kfree(p);
832err:
833 dput(parent);
834 return NULL;
835}
836
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400837static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100838{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400839 struct nfs4_opendata *p = container_of(kref,
840 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400841 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400842
843 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400844 if (p->state != NULL)
845 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400846 nfs4_put_state_owner(p->owner);
847 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400848 dput(p->dentry);
849 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500850 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400851 kfree(p);
852}
853
854static void nfs4_opendata_put(struct nfs4_opendata *p)
855{
856 if (p != NULL)
857 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100858}
859
Trond Myklebust06f814a2006-01-03 09:55:07 +0100860static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
861{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100862 int ret;
863
Trond Myklebust06f814a2006-01-03 09:55:07 +0100864 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100865 return ret;
866}
867
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500868static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400869{
870 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500871
Trond Myklebust536e43d2012-01-17 22:04:26 -0500872 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500873 goto out;
874 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400875 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500876 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
877 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400878 break;
879 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500880 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
881 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400882 break;
883 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500884 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
885 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400886 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500887out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400888 return ret;
889}
890
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500891static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400892{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500893 if (delegation == NULL)
894 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500895 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400896 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500897 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400898 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -0500899 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
900 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500901 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400902 return 1;
903}
904
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500905static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100906{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500907 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100908 case FMODE_WRITE:
909 state->n_wronly++;
910 break;
911 case FMODE_READ:
912 state->n_rdonly++;
913 break;
914 case FMODE_READ|FMODE_WRITE:
915 state->n_rdwr++;
916 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500917 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100918}
919
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500920static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400921{
922 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -0500923 nfs4_stateid_copy(&state->stateid, stateid);
924 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500925 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400926 case FMODE_READ:
927 set_bit(NFS_O_RDONLY_STATE, &state->flags);
928 break;
929 case FMODE_WRITE:
930 set_bit(NFS_O_WRONLY_STATE, &state->flags);
931 break;
932 case FMODE_READ|FMODE_WRITE:
933 set_bit(NFS_O_RDWR_STATE, &state->flags);
934 }
935}
936
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500937static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400938{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400939 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500940 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400941 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400942}
943
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500944static 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 -0700945{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400946 /*
947 * Protect the call to nfs4_state_set_mode_locked and
948 * serialise the stateid update
949 */
950 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400951 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -0500952 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -0400953 set_bit(NFS_DELEGATED_STATE, &state->flags);
954 }
955 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500956 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400957 write_sequnlock(&state->seqlock);
958 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500959 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700960 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961}
962
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500963static 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 -0500964{
965 struct nfs_inode *nfsi = NFS_I(state->inode);
966 struct nfs_delegation *deleg_cur;
967 int ret = 0;
968
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500969 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500970
971 rcu_read_lock();
972 deleg_cur = rcu_dereference(nfsi->delegation);
973 if (deleg_cur == NULL)
974 goto no_delegation;
975
976 spin_lock(&deleg_cur->lock);
977 if (nfsi->delegation != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -0500978 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500979 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500980 goto no_delegation_unlock;
981
982 if (delegation == NULL)
983 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -0500984 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -0500985 goto no_delegation_unlock;
986
Trond Myklebustb7391f42008-12-23 15:21:52 -0500987 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500988 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500989 ret = 1;
990no_delegation_unlock:
991 spin_unlock(&deleg_cur->lock);
992no_delegation:
993 rcu_read_unlock();
994
995 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500996 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500997 ret = 1;
998 }
999
1000 return ret;
1001}
1002
1003
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001004static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001005{
1006 struct nfs_delegation *delegation;
1007
1008 rcu_read_lock();
1009 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001010 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001011 rcu_read_unlock();
1012 return;
1013 }
1014 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001015 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001016}
1017
Trond Myklebust6ee41262007-07-08 14:11:36 -04001018static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001019{
1020 struct nfs4_state *state = opendata->state;
1021 struct nfs_inode *nfsi = NFS_I(state->inode);
1022 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -05001023 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001024 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001025 nfs4_stateid stateid;
1026 int ret = -EAGAIN;
1027
Trond Myklebustaac00a82007-07-05 19:02:21 -04001028 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001029 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001030 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001031 if (can_open_cached(state, fmode, open_mode)) {
1032 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001033 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001034 goto out_return_state;
1035 }
1036 spin_unlock(&state->owner->so_lock);
1037 }
Trond Myklebust34310432008-12-23 15:21:38 -05001038 rcu_read_lock();
1039 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001040 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001041 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001042 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001043 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001044 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001045 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001046 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001047 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001048 if (ret != 0)
1049 goto out;
1050 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001051
1052 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001053 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001054 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001055 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001056out:
1057 return ERR_PTR(ret);
1058out_return_state:
1059 atomic_inc(&state->count);
1060 return state;
1061}
1062
Andy Adamsone23008e2012-10-02 21:07:32 -04001063static void
1064nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1065{
1066 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1067 struct nfs_delegation *delegation;
1068 int delegation_flags = 0;
1069
1070 rcu_read_lock();
1071 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1072 if (delegation)
1073 delegation_flags = delegation->flags;
1074 rcu_read_unlock();
1075 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1076 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1077 "returning a delegation for "
1078 "OPEN(CLAIM_DELEGATE_CUR)\n",
1079 clp->cl_hostname);
1080 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1081 nfs_inode_set_delegation(state->inode,
1082 data->owner->so_cred,
1083 &data->o_res);
1084 else
1085 nfs_inode_reclaim_delegation(state->inode,
1086 data->owner->so_cred,
1087 &data->o_res);
1088}
1089
1090/*
1091 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1092 * and update the nfs4_state.
1093 */
1094static struct nfs4_state *
1095_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1096{
1097 struct inode *inode = data->state->inode;
1098 struct nfs4_state *state = data->state;
1099 int ret;
1100
1101 if (!data->rpc_done) {
1102 ret = data->rpc_status;
1103 goto err;
1104 }
1105
1106 ret = -ESTALE;
1107 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1108 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1109 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1110 goto err;
1111
1112 ret = -ENOMEM;
1113 state = nfs4_get_open_state(inode, data->owner);
1114 if (state == NULL)
1115 goto err;
1116
1117 ret = nfs_refresh_inode(inode, &data->f_attr);
1118 if (ret)
1119 goto err;
1120
1121 if (data->o_res.delegation_type != 0)
1122 nfs4_opendata_check_deleg(data, state);
1123 update_open_stateid(state, &data->o_res.stateid, NULL,
1124 data->o_arg.fmode);
1125
1126 return state;
1127err:
1128 return ERR_PTR(ret);
1129
1130}
1131
1132static struct nfs4_state *
1133_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001134{
1135 struct inode *inode;
1136 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001137 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001138
Trond Myklebustaac00a82007-07-05 19:02:21 -04001139 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001140 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001141 goto out;
1142 }
1143
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001144 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001145 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001146 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001147 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001148 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001149 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001150 goto err;
1151 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001152 state = nfs4_get_open_state(inode, data->owner);
1153 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001154 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001155 if (data->o_res.delegation_type != 0)
1156 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001157 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001158 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001159 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001160out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001161 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001162err_put_inode:
1163 iput(inode);
1164err:
1165 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001166}
1167
Andy Adamsone23008e2012-10-02 21:07:32 -04001168static struct nfs4_state *
1169nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1170{
1171 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1172 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1173 return _nfs4_opendata_to_nfs4_state(data);
1174}
1175
Trond Myklebust864472e2006-01-03 09:55:15 +01001176static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1177{
1178 struct nfs_inode *nfsi = NFS_I(state->inode);
1179 struct nfs_open_context *ctx;
1180
1181 spin_lock(&state->inode->i_lock);
1182 list_for_each_entry(ctx, &nfsi->open_files, list) {
1183 if (ctx->state != state)
1184 continue;
1185 get_nfs_open_context(ctx);
1186 spin_unlock(&state->inode->i_lock);
1187 return ctx;
1188 }
1189 spin_unlock(&state->inode->i_lock);
1190 return ERR_PTR(-ENOENT);
1191}
1192
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001193static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1194{
1195 struct nfs4_opendata *opendata;
1196
Al Viro3d4ff432011-06-22 18:40:12 -04001197 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001198 if (opendata == NULL)
1199 return ERR_PTR(-ENOMEM);
1200 opendata->state = state;
1201 atomic_inc(&state->count);
1202 return opendata;
1203}
1204
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001205static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001206{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001207 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001208 int ret;
1209
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001210 opendata->o_arg.open_flags = 0;
1211 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001212 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1213 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1214 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001215 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001216 if (ret != 0)
1217 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001218 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001219 if (IS_ERR(newstate))
1220 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001221 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001222 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001223 return 0;
1224}
1225
1226static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1227{
Trond Myklebust864472e2006-01-03 09:55:15 +01001228 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001229 int ret;
1230
1231 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001232 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001233 smp_rmb();
1234 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001235 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001236 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001237 if (ret != 0)
1238 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001239 if (newstate != state)
1240 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001241 }
1242 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001243 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001244 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001245 if (ret != 0)
1246 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001247 if (newstate != state)
1248 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001249 }
1250 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001251 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001252 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001253 if (ret != 0)
1254 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001255 if (newstate != state)
1256 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001257 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001258 /*
1259 * We may have performed cached opens for all three recoveries.
1260 * Check if we need to update the current stateid.
1261 */
1262 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001263 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001264 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001265 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001266 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001267 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001268 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001269 return 0;
1270}
1271
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272/*
1273 * OPEN_RECLAIM:
1274 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001276static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001278 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001279 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001280 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 int status;
1282
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001283 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1284 if (IS_ERR(opendata))
1285 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001286 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1287 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001288 rcu_read_lock();
1289 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001290 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001291 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001292 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001293 opendata->o_arg.u.delegation_type = delegation_type;
1294 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001295 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 return status;
1297}
1298
Trond Myklebust539cd032007-06-05 11:46:42 -04001299static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300{
1301 struct nfs_server *server = NFS_SERVER(state->inode);
1302 struct nfs4_exception exception = { };
1303 int err;
1304 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001305 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001306 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001307 break;
1308 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 } while (exception.retry);
1310 return err;
1311}
1312
Trond Myklebust864472e2006-01-03 09:55:15 +01001313static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1314{
1315 struct nfs_open_context *ctx;
1316 int ret;
1317
1318 ctx = nfs4_state_find_open_context(state);
1319 if (IS_ERR(ctx))
1320 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001321 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001322 put_nfs_open_context(ctx);
1323 return ret;
1324}
1325
Trond Myklebust13437e12007-07-06 15:10:43 -04001326static 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 -07001327{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001328 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001329 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001331 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1332 if (IS_ERR(opendata))
1333 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001334 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebustf597c532012-03-04 18:13:56 -05001335 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust864472e2006-01-03 09:55:15 +01001336 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001337 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001338 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339}
1340
Trond Myklebust13437e12007-07-06 15:10:43 -04001341int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342{
1343 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001344 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 int err;
1346 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001347 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 switch (err) {
1349 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001350 case -ENOENT:
1351 case -ESTALE:
1352 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001353 case -NFS4ERR_BADSESSION:
1354 case -NFS4ERR_BADSLOT:
1355 case -NFS4ERR_BAD_HIGH_SLOT:
1356 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1357 case -NFS4ERR_DEADSESSION:
Trond Myklebust37380e42013-02-05 12:35:16 -05001358 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust9f594792012-05-27 13:02:53 -04001359 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Trond Myklebust37380e42013-02-05 12:35:16 -05001360 err = -EAGAIN;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001361 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 case -NFS4ERR_STALE_CLIENTID:
1363 case -NFS4ERR_STALE_STATEID:
Trond Myklebust37380e42013-02-05 12:35:16 -05001364 set_bit(NFS_DELEGATED_STATE, &state->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 case -NFS4ERR_EXPIRED:
1366 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001367 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust37380e42013-02-05 12:35:16 -05001368 err = -EAGAIN;
Trond Myklebust965b5d62009-06-17 13:22:59 -07001369 goto out;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001370 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001371 case -NFS4ERR_ADMIN_REVOKED:
1372 case -NFS4ERR_BAD_STATEID:
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001373 nfs_inode_find_state_and_recover(state->inode,
1374 stateid);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001375 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001376 case -ENOMEM:
1377 err = 0;
1378 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 }
Trond Myklebust37380e42013-02-05 12:35:16 -05001380 set_bit(NFS_DELEGATED_STATE, &state->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 err = nfs4_handle_exception(server, err, &exception);
1382 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001383out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 return err;
1385}
1386
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001387static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1388{
1389 struct nfs4_opendata *data = calldata;
1390
1391 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001392 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001393 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001394 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001395 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001396 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001397 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001398}
1399
1400static void nfs4_open_confirm_release(void *calldata)
1401{
1402 struct nfs4_opendata *data = calldata;
1403 struct nfs4_state *state = NULL;
1404
1405 /* If this request hasn't been cancelled, do nothing */
1406 if (data->cancelled == 0)
1407 goto out_free;
1408 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001409 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001410 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001411 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001412 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001413 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001414out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001415 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001416}
1417
1418static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001419 .rpc_call_done = nfs4_open_confirm_done,
1420 .rpc_release = nfs4_open_confirm_release,
1421};
1422
1423/*
1424 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1425 */
1426static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1427{
1428 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1429 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001430 struct rpc_message msg = {
1431 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1432 .rpc_argp = &data->c_arg,
1433 .rpc_resp = &data->c_res,
1434 .rpc_cred = data->owner->so_cred,
1435 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001436 struct rpc_task_setup task_setup_data = {
1437 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001438 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001439 .callback_ops = &nfs4_open_confirm_ops,
1440 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001441 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001442 .flags = RPC_TASK_ASYNC,
1443 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 int status;
1445
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001446 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001447 data->rpc_done = 0;
1448 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001449 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001450 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001451 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001452 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001453 status = nfs4_wait_for_completion_rpc_task(task);
1454 if (status != 0) {
1455 data->cancelled = 1;
1456 smp_wmb();
1457 } else
1458 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001459 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 return status;
1461}
1462
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001463static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001465 struct nfs4_opendata *data = calldata;
1466 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001467
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001468 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001469 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001470 /*
1471 * Check if we still need to send an OPEN call, or if we can use
1472 * a delegation instead.
1473 */
1474 if (data->state != NULL) {
1475 struct nfs_delegation *delegation;
1476
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001477 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001478 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001479 rcu_read_lock();
1480 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001481 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1482 can_open_delegated(delegation, data->o_arg.fmode))
1483 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001484 rcu_read_unlock();
1485 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001486 /* Update client id. */
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001487 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001488 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001489 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Andy Adamsone23008e2012-10-02 21:07:32 -04001490 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001491 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1492 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001493 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001494 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001495 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001496 &data->o_res.seq_res,
1497 task) != 0)
1498 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001499 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001500unlock_no_action:
1501 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001502out_no_action:
1503 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001504out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05001505 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001506}
1507
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001508static void nfs4_open_done(struct rpc_task *task, void *calldata)
1509{
1510 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001512 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001513
Trond Myklebust14516c32010-07-31 14:29:06 -04001514 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1515 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001516
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001517 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001518 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1519 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001520 case S_IFREG:
1521 break;
1522 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001523 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001524 break;
1525 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001526 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001527 break;
1528 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001529 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001530 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001531 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001532 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001533 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1534 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001535 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001536 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001537}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001538
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001539static void nfs4_open_release(void *calldata)
1540{
1541 struct nfs4_opendata *data = calldata;
1542 struct nfs4_state *state = NULL;
1543
1544 /* If this request hasn't been cancelled, do nothing */
1545 if (data->cancelled == 0)
1546 goto out_free;
1547 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001548 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001549 goto out_free;
1550 /* In case we need an open_confirm, no cleanup! */
1551 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1552 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001553 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001554 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001555 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001556out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001557 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001558}
1559
1560static const struct rpc_call_ops nfs4_open_ops = {
1561 .rpc_call_prepare = nfs4_open_prepare,
1562 .rpc_call_done = nfs4_open_done,
1563 .rpc_release = nfs4_open_release,
1564};
1565
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001566static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001567{
1568 struct inode *dir = data->dir->d_inode;
1569 struct nfs_server *server = NFS_SERVER(dir);
1570 struct nfs_openargs *o_arg = &data->o_arg;
1571 struct nfs_openres *o_res = &data->o_res;
1572 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001573 struct rpc_message msg = {
1574 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1575 .rpc_argp = o_arg,
1576 .rpc_resp = o_res,
1577 .rpc_cred = data->owner->so_cred,
1578 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001579 struct rpc_task_setup task_setup_data = {
1580 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001581 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001582 .callback_ops = &nfs4_open_ops,
1583 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001584 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001585 .flags = RPC_TASK_ASYNC,
1586 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001587 int status;
1588
Trond Myklebust9d12b212012-01-17 22:04:25 -05001589 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001590 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001591 data->rpc_done = 0;
1592 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001593 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001594 if (isrecover)
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001595 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04001596 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001597 if (IS_ERR(task))
1598 return PTR_ERR(task);
1599 status = nfs4_wait_for_completion_rpc_task(task);
1600 if (status != 0) {
1601 data->cancelled = 1;
1602 smp_wmb();
1603 } else
1604 status = data->rpc_status;
1605 rpc_put_task(task);
1606
1607 return status;
1608}
1609
1610static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1611{
1612 struct inode *dir = data->dir->d_inode;
1613 struct nfs_openres *o_res = &data->o_res;
1614 int status;
1615
1616 status = nfs4_run_open_task(data, 1);
1617 if (status != 0 || !data->rpc_done)
1618 return status;
1619
Trond Myklebust6926afd2012-01-07 13:22:46 -05001620 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1621
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001622 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1623 status = _nfs4_proc_open_confirm(data);
1624 if (status != 0)
1625 return status;
1626 }
1627
1628 return status;
1629}
1630
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001631static int nfs4_opendata_access(struct rpc_cred *cred,
1632 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001633 struct nfs4_state *state, fmode_t fmode,
1634 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001635{
1636 struct nfs_access_entry cache;
1637 u32 mask;
1638
1639 /* access call failed or for some reason the server doesn't
1640 * support any access modes -- defer access call until later */
1641 if (opendata->o_res.access_supported == 0)
1642 return 0;
1643
1644 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001645 /* don't check MAY_WRITE - a newly created file may not have
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001646 * write mode bits, but POSIX allows the creating process to write.
1647 * use openflags to check for exec, because fmode won't
1648 * always have FMODE_EXEC set when file open for exec. */
1649 if (openflags & __FMODE_EXEC) {
1650 /* ONLY check for exec rights */
1651 mask = MAY_EXEC;
1652 } else if (fmode & FMODE_READ)
1653 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001654
1655 cache.cred = cred;
1656 cache.jiffies = jiffies;
1657 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1658 nfs_access_add_cache(state->inode, &cache);
1659
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001660 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001661 return 0;
1662
1663 /* even though OPEN succeeded, access is denied. Close the file */
1664 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001665 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001666}
1667
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001668/*
1669 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1670 */
1671static int _nfs4_proc_open(struct nfs4_opendata *data)
1672{
1673 struct inode *dir = data->dir->d_inode;
1674 struct nfs_server *server = NFS_SERVER(dir);
1675 struct nfs_openargs *o_arg = &data->o_arg;
1676 struct nfs_openres *o_res = &data->o_res;
1677 int status;
1678
1679 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001680 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001681 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001682 if (status != 0) {
1683 if (status == -NFS4ERR_BADNAME &&
1684 !(o_arg->open_flags & O_CREAT))
1685 return -ENOENT;
1686 return status;
1687 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001688
Trond Myklebust6926afd2012-01-07 13:22:46 -05001689 nfs_fattr_map_and_free_names(server, &data->f_attr);
1690
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001691 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001692 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001693 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1694 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001696 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001698 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 }
1700 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001701 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001702 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703}
1704
Andy Adamsond83217c2011-03-01 01:34:17 +00001705static int nfs4_recover_expired_lease(struct nfs_server *server)
1706{
1707 return nfs4_client_recover_expired_lease(server->nfs_client);
1708}
1709
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710/*
1711 * OPEN_EXPIRED:
1712 * reclaim state on the server after a network partition.
1713 * Assumes caller holds the appropriate lock
1714 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001715static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001717 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001718 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001720 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1721 if (IS_ERR(opendata))
1722 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001723 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001724 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001725 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001726 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001727 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728}
1729
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001730static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001731{
Trond Myklebust539cd032007-06-05 11:46:42 -04001732 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001733 struct nfs4_exception exception = { };
1734 int err;
1735
1736 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001737 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001738 switch (err) {
1739 default:
1740 goto out;
1741 case -NFS4ERR_GRACE:
1742 case -NFS4ERR_DELAY:
1743 nfs4_handle_exception(server, err, &exception);
1744 err = 0;
1745 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001746 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001747out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001748 return err;
1749}
1750
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1752{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001754 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
Trond Myklebust864472e2006-01-03 09:55:15 +01001756 ctx = nfs4_state_find_open_context(state);
1757 if (IS_ERR(ctx))
1758 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001759 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001760 put_nfs_open_context(ctx);
1761 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762}
1763
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001764#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001765static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001766{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001767 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001768 nfs4_stateid *stateid = &state->stateid;
1769 int status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001770
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001771 /* If a state reset has been done, test_stateid is unneeded */
1772 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1773 return;
1774
1775 status = nfs41_test_stateid(server, stateid);
1776 if (status != NFS_OK) {
1777 /* Free the stateid unless the server explicitly
1778 * informs us the stateid is unrecognized. */
1779 if (status != -NFS4ERR_BAD_STATEID)
1780 nfs41_free_stateid(server, stateid);
Bryan Schumaker69388672012-09-26 15:25:52 -04001781 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001782
Bryan Schumaker69388672012-09-26 15:25:52 -04001783 write_seqlock(&state->seqlock);
1784 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1785 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001786 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1787 }
1788}
1789
1790/**
1791 * nfs41_check_open_stateid - possibly free an open stateid
1792 *
1793 * @state: NFSv4 state for an inode
1794 *
1795 * Returns NFS_OK if recovery for this stateid is now finished.
1796 * Otherwise a negative NFS4ERR value is returned.
1797 */
1798static int nfs41_check_open_stateid(struct nfs4_state *state)
1799{
1800 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04001801 nfs4_stateid *stateid = &state->open_stateid;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001802 int status;
1803
1804 /* If a state reset has been done, test_stateid is unneeded */
1805 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1806 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1807 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1808 return -NFS4ERR_BAD_STATEID;
1809
1810 status = nfs41_test_stateid(server, stateid);
1811 if (status != NFS_OK) {
1812 /* Free the stateid unless the server explicitly
1813 * informs us the stateid is unrecognized. */
1814 if (status != -NFS4ERR_BAD_STATEID)
1815 nfs41_free_stateid(server, stateid);
1816
1817 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1818 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1819 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001820 }
1821 return status;
1822}
1823
1824static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1825{
Chuck Levereb64cf92012-07-11 16:30:05 -04001826 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001827
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001828 nfs41_clear_delegation_stateid(state);
1829 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04001830 if (status != NFS_OK)
1831 status = nfs4_open_expired(sp, state);
1832 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001833}
1834#endif
1835
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001837 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1838 * fields corresponding to attributes that were used to store the verifier.
1839 * Make sure we clobber those fields in the later setattr call
1840 */
1841static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1842{
1843 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1844 !(sattr->ia_valid & ATTR_ATIME_SET))
1845 sattr->ia_valid |= ATTR_ATIME;
1846
1847 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1848 !(sattr->ia_valid & ATTR_MTIME_SET))
1849 sattr->ia_valid |= ATTR_MTIME;
1850}
1851
Trond Myklebustc21443c2013-02-07 14:26:21 -05001852static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
1853 fmode_t fmode,
1854 int flags,
1855 struct nfs4_state **res)
1856{
1857 struct nfs4_state_owner *sp = opendata->owner;
1858 struct nfs_server *server = sp->so_server;
1859 struct nfs4_state *state;
1860 unsigned int seq;
1861 int ret;
1862
1863 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
1864
1865 ret = _nfs4_proc_open(opendata);
1866 if (ret != 0)
1867 goto out;
1868
1869 state = nfs4_opendata_to_nfs4_state(opendata);
1870 ret = PTR_ERR(state);
1871 if (IS_ERR(state))
1872 goto out;
1873 if (server->caps & NFS_CAP_POSIX_LOCK)
1874 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
1875
1876 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
1877 if (ret != 0)
1878 goto out;
1879
1880 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
1881 nfs4_schedule_stateid_recovery(server, state);
1882 nfs4_wait_clnt_recover(server->nfs_client);
1883 }
1884 *res = state;
1885out:
1886 return ret;
1887}
1888
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001889/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001890 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 */
Andy Adamson82be4172012-05-23 05:02:35 -04001892static int _nfs4_do_open(struct inode *dir,
1893 struct dentry *dentry,
1894 fmode_t fmode,
1895 int flags,
1896 struct iattr *sattr,
1897 struct rpc_cred *cred,
1898 struct nfs4_state **res,
1899 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900{
1901 struct nfs4_state_owner *sp;
1902 struct nfs4_state *state = NULL;
1903 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001904 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001905 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906
1907 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001909 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1910 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1912 goto out_err;
1913 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001914 status = nfs4_recover_expired_lease(server);
1915 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001916 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001917 if (dentry->d_inode != NULL)
1918 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001919 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001920 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001921 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001922 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
Andy Adamson82be4172012-05-23 05:02:35 -04001924 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
1925 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
1926 if (!opendata->f_attr.mdsthreshold)
1927 goto err_opendata_put;
Trond Myklebust1549210f2012-06-05 09:16:47 -04001928 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04001929 }
Al Viro82a2c1b2011-06-22 18:30:55 -04001930 if (dentry->d_inode != NULL)
1931 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001932
Trond Myklebustc21443c2013-02-07 14:26:21 -05001933 status = _nfs4_open_and_get_state(opendata, fmode, flags, &state);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001934 if (status != 0)
1935 goto err_opendata_put;
1936
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001937 if (opendata->o_arg.open_flags & O_EXCL) {
1938 nfs4_exclusive_attrset(opendata, sattr);
1939
1940 nfs_fattr_init(opendata->o_res.f_attr);
1941 status = nfs4_do_setattr(state->inode, cred,
1942 opendata->o_res.f_attr, sattr,
1943 state);
1944 if (status == 0)
1945 nfs_setattr_update_inode(state->inode, sattr);
1946 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1947 }
Andy Adamson82be4172012-05-23 05:02:35 -04001948
1949 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
1950 *ctx_th = opendata->f_attr.mdsthreshold;
1951 else
1952 kfree(opendata->f_attr.mdsthreshold);
1953 opendata->f_attr.mdsthreshold = NULL;
1954
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001955 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 *res = state;
1958 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001959err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04001960 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001961 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001962err_put_state_owner:
1963 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 *res = NULL;
1966 return status;
1967}
1968
1969
Andy Adamson82be4172012-05-23 05:02:35 -04001970static struct nfs4_state *nfs4_do_open(struct inode *dir,
1971 struct dentry *dentry,
1972 fmode_t fmode,
1973 int flags,
1974 struct iattr *sattr,
1975 struct rpc_cred *cred,
1976 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977{
1978 struct nfs4_exception exception = { };
1979 struct nfs4_state *res;
1980 int status;
1981
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001982 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 do {
Andy Adamson82be4172012-05-23 05:02:35 -04001984 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
1985 &res, ctx_th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 if (status == 0)
1987 break;
1988 /* NOTE: BAD_SEQID means the server and client disagree about the
1989 * book-keeping w.r.t. state-changing operations
1990 * (OPEN/CLOSE/LOCK/LOCKU...)
1991 * It is actually a sign of a bug on the client or on the server.
1992 *
1993 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001994 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 * have unhashed the old state_owner for us, and that we can
1996 * therefore safely retry using a new one. We should still warn
1997 * the user though...
1998 */
1999 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002000 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002001 " returned a bad sequence-id error!\n",
2002 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 exception.retry = 1;
2004 continue;
2005 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002006 /*
2007 * BAD_STATEID on OPEN means that the server cancelled our
2008 * state before it received the OPEN_CONFIRM.
2009 * Recover by retrying the request as per the discussion
2010 * on Page 181 of RFC3530.
2011 */
2012 if (status == -NFS4ERR_BAD_STATEID) {
2013 exception.retry = 1;
2014 continue;
2015 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002016 if (status == -EAGAIN) {
2017 /* We must have found a delegation */
2018 exception.retry = 1;
2019 continue;
2020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
2022 status, &exception));
2023 } while (exception.retry);
2024 return res;
2025}
2026
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002027static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2028 struct nfs_fattr *fattr, struct iattr *sattr,
2029 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002031 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002033 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 .iap = sattr,
2035 .server = server,
2036 .bitmask = server->attr_bitmask,
2037 };
2038 struct nfs_setattrres res = {
2039 .fattr = fattr,
2040 .server = server,
2041 };
2042 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002043 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2044 .rpc_argp = &arg,
2045 .rpc_resp = &res,
2046 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002048 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04002049 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
Trond Myklebust0e574af2005-10-27 22:12:38 -04002051 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052
Trond Myklebust4fc87962012-03-08 17:42:01 -05002053 if (state != NULL) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002054 struct nfs_lockowner lockowner = {
2055 .l_owner = current->files,
2056 .l_pid = current->tgid,
2057 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002058 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002059 &lockowner);
Trond Myklebust4fc87962012-03-08 17:42:01 -05002060 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2061 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002062 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002063 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002064 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065
Bryan Schumaker7c513052011-03-24 17:12:24 +00002066 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002067 if (status == 0 && state != NULL)
2068 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002069 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070}
2071
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002072static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2073 struct nfs_fattr *fattr, struct iattr *sattr,
2074 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002076 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002077 struct nfs4_exception exception = {
2078 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002079 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002080 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 int err;
2082 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04002083 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2084 switch (err) {
2085 case -NFS4ERR_OPENMODE:
2086 if (state && !(state->state & FMODE_WRITE)) {
2087 err = -EBADF;
2088 if (sattr->ia_valid & ATTR_OPEN)
2089 err = -EACCES;
2090 goto out;
2091 }
2092 }
2093 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002095out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 return err;
2097}
2098
2099struct nfs4_closedata {
2100 struct inode *inode;
2101 struct nfs4_state *state;
2102 struct nfs_closeargs arg;
2103 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002104 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002105 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002106 bool roc;
2107 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108};
2109
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002110static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002111{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002112 struct nfs4_closedata *calldata = data;
2113 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002114 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002115
Fred Isamanf7e89172011-01-06 11:36:32 +00002116 if (calldata->roc)
2117 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002118 nfs4_put_open_state(calldata->state);
2119 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002120 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002121 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002122 kfree(calldata);
2123}
2124
Trond Myklebust88069f72009-12-08 08:33:16 -05002125static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2126 fmode_t fmode)
2127{
2128 spin_lock(&state->owner->so_lock);
2129 if (!(fmode & FMODE_READ))
2130 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2131 if (!(fmode & FMODE_WRITE))
2132 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2133 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2134 spin_unlock(&state->owner->so_lock);
2135}
2136
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002137static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002139 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 struct nfs_server *server = NFS_SERVER(calldata->inode);
2142
Chuck Levera3ca5652012-03-01 17:00:40 -05002143 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002144 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2145 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 /* hmm. we are done with the inode, and in the process of freeing
2147 * the state_owner. we keep this around to process errors
2148 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 switch (task->tk_status) {
2150 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002151 if (calldata->roc)
2152 pnfs_roc_set_barrier(state->inode,
2153 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002154 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002155 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002156 nfs4_close_clear_stateid_flags(state,
2157 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 break;
2159 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002160 case -NFS4ERR_OLD_STATEID:
2161 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002163 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002164 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002166 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2167 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002169 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002170 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002171 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172}
2173
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002174static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002176 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002177 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002178 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002179 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002180
Chuck Levera3ca5652012-03-01 17:00:40 -05002181 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002182 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002183 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04002184
Trond Myklebust88069f72009-12-08 08:33:16 -05002185 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2186 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002187 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002188 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002189 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002190 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002191 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2192 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2193 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002194 }
2195 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002196 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2197 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2198 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002199 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002200 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002201 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002202
2203 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002204 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002205 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07002206 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002207
Fred Isamanf7e89172011-01-06 11:36:32 +00002208 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002209 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002210 if (calldata->roc &&
Trond Myklebust7fdab062012-09-20 20:15:57 -04002211 pnfs_roc_drain(inode, &calldata->roc_barrier, task))
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002212 goto out_wait;
Fred Isamanf7e89172011-01-06 11:36:32 +00002213 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002214
Trond Myklebust516a6af2005-10-27 22:12:41 -04002215 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002216 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002217 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002218 &calldata->arg.seq_args,
2219 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002220 task) != 0)
2221 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002222 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002223 return;
2224out_no_action:
2225 task->tk_action = NULL;
2226out_wait:
2227 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228}
2229
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002230static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002231 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002232 .rpc_call_done = nfs4_close_done,
2233 .rpc_release = nfs4_free_closedata,
2234};
2235
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236/*
2237 * It is possible for data to be read/written from a mem-mapped file
2238 * after the sys_close call (which hits the vfs layer as a flush).
2239 * This means that we can't safely call nfsv4 close on a file until
2240 * the inode is cleared. This in turn means that we are not good
2241 * NFSv4 citizens - we do not indicate to the server to update the file's
2242 * share state even when we are done with one of the three share
2243 * stateid's in the inode.
2244 *
2245 * NOTE: Caller must be holding the sp->so_owner semaphore!
2246 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002247int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002249 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002251 struct nfs4_state_owner *sp = state->owner;
2252 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002253 struct rpc_message msg = {
2254 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2255 .rpc_cred = state->owner->so_cred,
2256 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002257 struct rpc_task_setup task_setup_data = {
2258 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002259 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002260 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002261 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002262 .flags = RPC_TASK_ASYNC,
2263 };
Trond Myklebust95121352005-10-18 14:20:12 -07002264 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002266 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002268 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002269 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002270 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002272 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002273 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002275 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002276 if (calldata->arg.seqid == NULL)
2277 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002278 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002279 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002280 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002281 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002282 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002283 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002284 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002285
Trond Myklebust1174dd12010-12-21 10:52:24 -05002286 msg.rpc_argp = &calldata->arg;
2287 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002288 task_setup_data.callback_data = calldata;
2289 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002290 if (IS_ERR(task))
2291 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002292 status = 0;
2293 if (wait)
2294 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002295 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002296 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002297out_free_calldata:
2298 kfree(calldata);
2299out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002300 nfs4_put_open_state(state);
2301 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002302 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303}
2304
Trond Myklebust2b484292010-09-17 10:56:51 -04002305static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002306nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 struct nfs4_state *state;
2309
Trond Myklebust565277f2007-10-15 18:17:53 -04002310 /* Protect against concurrent sillydeletes */
Andy Adamson82be4172012-05-23 05:02:35 -04002311 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2312 ctx->cred, &ctx->mdsthreshold);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002313 if (IS_ERR(state))
2314 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002315 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002316 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317}
2318
Trond Myklebust1185a552009-12-03 15:54:02 -05002319static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002320{
2321 if (ctx->state == NULL)
2322 return;
2323 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002324 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002325 else
Al Viro643168c2011-06-22 18:20:23 -04002326 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002327}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328
2329static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2330{
Benny Halevy43652ad2009-04-01 09:21:54 -04002331 struct nfs4_server_caps_arg args = {
2332 .fhandle = fhandle,
2333 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 struct nfs4_server_caps_res res = {};
2335 struct rpc_message msg = {
2336 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002337 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 .rpc_resp = &res,
2339 };
2340 int status;
2341
Bryan Schumaker7c513052011-03-24 17:12:24 +00002342 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 if (status == 0) {
2344 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04002345 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2346 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2347 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2348 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2349 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2351 server->caps |= NFS_CAP_ACLS;
2352 if (res.has_links != 0)
2353 server->caps |= NFS_CAP_HARDLINKS;
2354 if (res.has_symlinks != 0)
2355 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04002356 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2357 server->caps |= NFS_CAP_FILEID;
2358 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2359 server->caps |= NFS_CAP_MODE;
2360 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2361 server->caps |= NFS_CAP_NLINK;
2362 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2363 server->caps |= NFS_CAP_OWNER;
2364 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2365 server->caps |= NFS_CAP_OWNER_GROUP;
2366 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2367 server->caps |= NFS_CAP_ATIME;
2368 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2369 server->caps |= NFS_CAP_CTIME;
2370 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2371 server->caps |= NFS_CAP_MTIME;
2372
Trond Myklebusta65318b2009-03-11 14:10:28 -04002373 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2374 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2375 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002377 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002379
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 return status;
2381}
2382
Trond Myklebust55a97592006-06-09 09:34:19 -04002383int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384{
2385 struct nfs4_exception exception = { };
2386 int err;
2387 do {
2388 err = nfs4_handle_exception(server,
2389 _nfs4_server_capabilities(server, fhandle),
2390 &exception);
2391 } while (exception.retry);
2392 return err;
2393}
2394
2395static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2396 struct nfs_fsinfo *info)
2397{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 struct nfs4_lookup_root_arg args = {
2399 .bitmask = nfs4_fattr_bitmap,
2400 };
2401 struct nfs4_lookup_res res = {
2402 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002403 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 .fh = fhandle,
2405 };
2406 struct rpc_message msg = {
2407 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2408 .rpc_argp = &args,
2409 .rpc_resp = &res,
2410 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002411
Trond Myklebust0e574af2005-10-27 22:12:38 -04002412 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002413 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414}
2415
2416static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2417 struct nfs_fsinfo *info)
2418{
2419 struct nfs4_exception exception = { };
2420 int err;
2421 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002422 err = _nfs4_lookup_root(server, fhandle, info);
2423 switch (err) {
2424 case 0:
2425 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002426 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002427 default:
2428 err = nfs4_handle_exception(server, err, &exception);
2429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002431out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 return err;
2433}
2434
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002435static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2436 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2437{
2438 struct rpc_auth *auth;
2439 int ret;
2440
2441 auth = rpcauth_create(flavor, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002442 if (IS_ERR(auth)) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002443 ret = -EIO;
2444 goto out;
2445 }
2446 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002447out:
2448 return ret;
2449}
2450
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002451static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002452 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002454 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002455 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002457 len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04002458 if (len < 0)
2459 return len;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002460
2461 for (i = 0; i < len; i++) {
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002462 /* AUTH_UNIX is the default flavor if none was specified,
2463 * thus has already been tried. */
2464 if (flav_array[i] == RPC_AUTH_UNIX)
2465 continue;
2466
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002467 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002468 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002469 continue;
2470 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002471 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002472 /*
2473 * -EACCESS could mean that the user doesn't have correct permissions
2474 * to access the mount. It could also mean that we tried to mount
2475 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2476 * existing mount programs don't handle -EACCES very well so it should
2477 * be mapped to -EPERM instead.
2478 */
2479 if (status == -EACCES)
2480 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002481 return status;
2482}
2483
2484/*
2485 * get the file handle for the "/" directory on the server
2486 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002487int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2488 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002489{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002490 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002491 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002492 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2493 /*
2494 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2495 * by nfs4_map_errors() as this function exits.
2496 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002497 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 if (status == 0)
2499 status = nfs4_server_capabilities(server, fhandle);
2500 if (status == 0)
2501 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002502 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503}
2504
Bryan Schumakerbae36242012-05-10 15:07:31 -04002505static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2506 struct nfs_fsinfo *info)
2507{
2508 int error;
2509 struct nfs_fattr *fattr = info->fattr;
2510
2511 error = nfs4_server_capabilities(server, mntfh);
2512 if (error < 0) {
2513 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2514 return error;
2515 }
2516
2517 error = nfs4_proc_getattr(server, mntfh, fattr);
2518 if (error < 0) {
2519 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2520 return error;
2521 }
2522
2523 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2524 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2525 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2526
2527 return error;
2528}
2529
Manoj Naik6b97fd32006-06-09 09:34:29 -04002530/*
2531 * Get locations and (maybe) other attributes of a referral.
2532 * Note that we'll actually follow the referral later when
2533 * we detect fsid mismatch in inode revalidation
2534 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002535static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2536 const struct qstr *name, struct nfs_fattr *fattr,
2537 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002538{
2539 int status = -ENOMEM;
2540 struct page *page = NULL;
2541 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002542
2543 page = alloc_page(GFP_KERNEL);
2544 if (page == NULL)
2545 goto out;
2546 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2547 if (locations == NULL)
2548 goto out;
2549
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002550 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002551 if (status != 0)
2552 goto out;
2553 /* Make sure server returned a different fsid for the referral */
2554 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002555 dprintk("%s: server did not return a different fsid for"
2556 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002557 status = -EIO;
2558 goto out;
2559 }
Andy Adamson533eb462011-06-13 18:25:56 -04002560 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2561 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002562
Andy Adamson533eb462011-06-13 18:25:56 -04002563 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002564 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002565 memset(fhandle, 0, sizeof(struct nfs_fh));
2566out:
2567 if (page)
2568 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002569 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002570 return status;
2571}
2572
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2574{
2575 struct nfs4_getattr_arg args = {
2576 .fh = fhandle,
2577 .bitmask = server->attr_bitmask,
2578 };
2579 struct nfs4_getattr_res res = {
2580 .fattr = fattr,
2581 .server = server,
2582 };
2583 struct rpc_message msg = {
2584 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2585 .rpc_argp = &args,
2586 .rpc_resp = &res,
2587 };
2588
Trond Myklebust0e574af2005-10-27 22:12:38 -04002589 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002590 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591}
2592
2593static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2594{
2595 struct nfs4_exception exception = { };
2596 int err;
2597 do {
2598 err = nfs4_handle_exception(server,
2599 _nfs4_proc_getattr(server, fhandle, fattr),
2600 &exception);
2601 } while (exception.retry);
2602 return err;
2603}
2604
2605/*
2606 * The file is not closed if it is opened due to the a request to change
2607 * the size of the file. The open call will not be needed once the
2608 * VFS layer lookup-intents are implemented.
2609 *
2610 * Close is called when the inode is destroyed.
2611 * If we haven't opened the file for O_WRONLY, we
2612 * need to in the size_change case to obtain a stateid.
2613 *
2614 * Got race?
2615 * Because OPEN is always done by name in nfsv4, it is
2616 * possible that we opened a different file by the same
2617 * name. We can recognize this race condition, but we
2618 * can't do anything about it besides returning an error.
2619 *
2620 * This will be fixed with VFS changes (lookup-intent).
2621 */
2622static int
2623nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2624 struct iattr *sattr)
2625{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002626 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002627 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002628 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 int status;
2630
Benny Halevy8a1636c2010-07-14 15:43:57 -04002631 if (pnfs_ld_layoutret_on_setattr(inode))
2632 pnfs_return_layout(inode);
2633
Trond Myklebust0e574af2005-10-27 22:12:38 -04002634 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635
Andy Adamson26699402012-05-30 16:12:24 -04002636 /* Deal with open(O_TRUNC) */
2637 if (sattr->ia_valid & ATTR_OPEN)
2638 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2639
2640 /* Optimization: if the end result is no change, don't RPC */
2641 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2642 return 0;
2643
Trond Myklebustd5308382005-11-04 15:33:38 -05002644 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002645 if (sattr->ia_valid & ATTR_FILE) {
2646 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002647
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002648 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002649 if (ctx) {
2650 cred = ctx->cred;
2651 state = ctx->state;
2652 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002653 }
2654
2655 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002656 if (status == 0)
2657 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 return status;
2659}
2660
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002661static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2662 const struct qstr *name, struct nfs_fh *fhandle,
2663 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002664{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002665 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002666 int status;
2667 struct nfs4_lookup_arg args = {
2668 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002669 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002670 .name = name,
2671 };
2672 struct nfs4_lookup_res res = {
2673 .server = server,
2674 .fattr = fattr,
2675 .fh = fhandle,
2676 };
2677 struct rpc_message msg = {
2678 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2679 .rpc_argp = &args,
2680 .rpc_resp = &res,
2681 };
2682
2683 nfs_fattr_init(fattr);
2684
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002686 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 dprintk("NFS reply lookup: %d\n", status);
2688 return status;
2689}
2690
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002691static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002692{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002693 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002694 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002695 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2696 fattr->nlink = 2;
2697}
2698
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002699static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2700 struct qstr *name, struct nfs_fh *fhandle,
2701 struct nfs_fattr *fattr)
2702{
2703 struct nfs4_exception exception = { };
2704 struct rpc_clnt *client = *clnt;
2705 int err;
2706 do {
2707 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2708 switch (err) {
2709 case -NFS4ERR_BADNAME:
2710 err = -ENOENT;
2711 goto out;
2712 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002713 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002714 goto out;
2715 case -NFS4ERR_WRONGSEC:
2716 err = -EPERM;
2717 if (client != *clnt)
2718 goto out;
2719
2720 client = nfs4_create_sec_client(client, dir, name);
2721 if (IS_ERR(client))
2722 return PTR_ERR(client);
2723
2724 exception.retry = 1;
2725 break;
2726 default:
2727 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2728 }
2729 } while (exception.retry);
2730
2731out:
2732 if (err == 0)
2733 *clnt = client;
2734 else if (client != *clnt)
2735 rpc_shutdown_client(client);
2736
2737 return err;
2738}
2739
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002740static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002741 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002743 int status;
2744 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002745
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002746 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2747 if (client != NFS_CLIENT(dir)) {
2748 rpc_shutdown_client(client);
2749 nfs_fixup_secinfo_attributes(fattr);
2750 }
2751 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752}
2753
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002754struct rpc_clnt *
2755nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2756 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2757{
2758 int status;
2759 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2760
2761 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2762 if (status < 0) {
2763 rpc_shutdown_client(client);
2764 return ERR_PTR(status);
2765 }
2766 return client;
2767}
2768
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2770{
Trond Myklebust76b32992007-08-10 17:45:11 -04002771 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 struct nfs4_accessargs args = {
2773 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002774 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002776 struct nfs4_accessres res = {
2777 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002778 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 struct rpc_message msg = {
2780 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2781 .rpc_argp = &args,
2782 .rpc_resp = &res,
2783 .rpc_cred = entry->cred,
2784 };
2785 int mode = entry->mask;
2786 int status;
2787
2788 /*
2789 * Determine which access bits we want to ask for...
2790 */
2791 if (mode & MAY_READ)
2792 args.access |= NFS4_ACCESS_READ;
2793 if (S_ISDIR(inode->i_mode)) {
2794 if (mode & MAY_WRITE)
2795 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2796 if (mode & MAY_EXEC)
2797 args.access |= NFS4_ACCESS_LOOKUP;
2798 } else {
2799 if (mode & MAY_WRITE)
2800 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2801 if (mode & MAY_EXEC)
2802 args.access |= NFS4_ACCESS_EXECUTE;
2803 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002804
2805 res.fattr = nfs_alloc_fattr();
2806 if (res.fattr == NULL)
2807 return -ENOMEM;
2808
Bryan Schumaker7c513052011-03-24 17:12:24 +00002809 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002811 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04002812 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002814 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 return status;
2816}
2817
2818static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2819{
2820 struct nfs4_exception exception = { };
2821 int err;
2822 do {
2823 err = nfs4_handle_exception(NFS_SERVER(inode),
2824 _nfs4_proc_access(inode, entry),
2825 &exception);
2826 } while (exception.retry);
2827 return err;
2828}
2829
2830/*
2831 * TODO: For the time being, we don't try to get any attributes
2832 * along with any of the zero-copy operations READ, READDIR,
2833 * READLINK, WRITE.
2834 *
2835 * In the case of the first three, we want to put the GETATTR
2836 * after the read-type operation -- this is because it is hard
2837 * to predict the length of a GETATTR response in v4, and thus
2838 * align the READ data correctly. This means that the GETATTR
2839 * may end up partially falling into the page cache, and we should
2840 * shift it into the 'tail' of the xdr_buf before processing.
2841 * To do this efficiently, we need to know the total length
2842 * of data received, which doesn't seem to be available outside
2843 * of the RPC layer.
2844 *
2845 * In the case of WRITE, we also want to put the GETATTR after
2846 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04002847 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 *
2849 * Both of these changes to the XDR layer would in fact be quite
2850 * minor, but I decided to leave them for a subsequent patch.
2851 */
2852static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2853 unsigned int pgbase, unsigned int pglen)
2854{
2855 struct nfs4_readlink args = {
2856 .fh = NFS_FH(inode),
2857 .pgbase = pgbase,
2858 .pglen = pglen,
2859 .pages = &page,
2860 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002861 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 struct rpc_message msg = {
2863 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2864 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002865 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 };
2867
Bryan Schumaker7c513052011-03-24 17:12:24 +00002868 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 -07002869}
2870
2871static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2872 unsigned int pgbase, unsigned int pglen)
2873{
2874 struct nfs4_exception exception = { };
2875 int err;
2876 do {
2877 err = nfs4_handle_exception(NFS_SERVER(inode),
2878 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2879 &exception);
2880 } while (exception.retry);
2881 return err;
2882}
2883
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002885 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887static int
2888nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002889 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890{
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002891 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 int status = 0;
2894
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002895 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
2896 if (IS_ERR(ctx))
2897 return PTR_ERR(ctx);
2898
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002899 sattr->ia_mode &= ~current_umask();
Trond Myklebust8626e4a2012-07-16 12:01:42 -04002900 state = nfs4_do_open(dir, dentry, ctx->mode,
2901 flags, sattr, ctx->cred,
2902 &ctx->mdsthreshold);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002903 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 if (IS_ERR(state)) {
2905 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002906 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002908 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002909 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002910 ctx->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911out:
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002912 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 return status;
2914}
2915
2916static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2917{
Trond Myklebust16e42952005-10-27 22:12:44 -04002918 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002919 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07002921 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002923 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002924 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002925 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002927 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2928 .rpc_argp = &args,
2929 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 };
Trond Myklebust778d2812012-04-27 13:48:19 -04002931 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04002932
Bryan Schumaker7c513052011-03-24 17:12:24 +00002933 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04002934 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04002935 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 return status;
2937}
2938
2939static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2940{
2941 struct nfs4_exception exception = { };
2942 int err;
2943 do {
2944 err = nfs4_handle_exception(NFS_SERVER(dir),
2945 _nfs4_proc_remove(dir, name),
2946 &exception);
2947 } while (exception.retry);
2948 return err;
2949}
2950
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002951static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002953 struct nfs_server *server = NFS_SERVER(dir);
2954 struct nfs_removeargs *args = msg->rpc_argp;
2955 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002957 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05002959 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960}
2961
Bryan Schumaker34e137c2012-03-19 14:54:41 -04002962static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
2963{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04002964 nfs4_setup_sequence(NFS_SERVER(data->dir),
2965 &data->args.seq_args,
2966 &data->res.seq_res,
2967 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968}
2969
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002970static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002972 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2973
Trond Myklebust14516c32010-07-31 14:29:06 -04002974 if (!nfs4_sequence_done(task, &res->seq_res))
2975 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002976 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002977 return 0;
2978 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002979 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980}
2981
Jeff Laytond3d41522010-09-17 17:31:57 -04002982static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2983{
2984 struct nfs_server *server = NFS_SERVER(dir);
2985 struct nfs_renameargs *arg = msg->rpc_argp;
2986 struct nfs_renameres *res = msg->rpc_resp;
2987
2988 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04002989 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002990 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04002991}
2992
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04002993static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
2994{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04002995 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
2996 &data->args.seq_args,
2997 &data->res.seq_res,
2998 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04002999}
3000
3001static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3002 struct inode *new_dir)
3003{
3004 struct nfs_renameres *res = task->tk_msg.rpc_resp;
3005
3006 if (!nfs4_sequence_done(task, &res->seq_res))
3007 return 0;
3008 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3009 return 0;
3010
3011 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003012 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003013 return 1;
3014}
3015
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3017 struct inode *new_dir, struct qstr *new_name)
3018{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003019 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04003020 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 .old_dir = NFS_FH(old_dir),
3022 .new_dir = NFS_FH(new_dir),
3023 .old_name = old_name,
3024 .new_name = new_name,
3025 };
Jeff Laytone8582a82010-09-17 17:31:06 -04003026 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003027 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003028 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 struct rpc_message msg = {
3030 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3031 .rpc_argp = &arg,
3032 .rpc_resp = &res,
3033 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003034 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035
Bryan Schumaker7c513052011-03-24 17:12:24 +00003036 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 if (!status) {
3038 update_changeattr(old_dir, &res.old_cinfo);
3039 update_changeattr(new_dir, &res.new_cinfo);
3040 }
3041 return status;
3042}
3043
3044static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3045 struct inode *new_dir, struct qstr *new_name)
3046{
3047 struct nfs4_exception exception = { };
3048 int err;
3049 do {
3050 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3051 _nfs4_proc_rename(old_dir, old_name,
3052 new_dir, new_name),
3053 &exception);
3054 } while (exception.retry);
3055 return err;
3056}
3057
3058static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3059{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003060 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 struct nfs4_link_arg arg = {
3062 .fh = NFS_FH(inode),
3063 .dir_fh = NFS_FH(dir),
3064 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003065 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003067 struct nfs4_link_res res = {
3068 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003069 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 struct rpc_message msg = {
3071 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3072 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003073 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003075 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076
Trond Myklebust136f2622010-04-16 16:22:49 -04003077 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003078 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003079 goto out;
3080
Bryan Schumaker7c513052011-03-24 17:12:24 +00003081 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003082 if (!status) {
3083 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04003084 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003085 }
Trond Myklebust136f2622010-04-16 16:22:49 -04003086out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003087 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 return status;
3089}
3090
3091static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3092{
3093 struct nfs4_exception exception = { };
3094 int err;
3095 do {
3096 err = nfs4_handle_exception(NFS_SERVER(inode),
3097 _nfs4_proc_link(inode, dir, name),
3098 &exception);
3099 } while (exception.retry);
3100 return err;
3101}
3102
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003103struct nfs4_createdata {
3104 struct rpc_message msg;
3105 struct nfs4_create_arg arg;
3106 struct nfs4_create_res res;
3107 struct nfs_fh fh;
3108 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003109};
3110
3111static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3112 struct qstr *name, struct iattr *sattr, u32 ftype)
3113{
3114 struct nfs4_createdata *data;
3115
3116 data = kzalloc(sizeof(*data), GFP_KERNEL);
3117 if (data != NULL) {
3118 struct nfs_server *server = NFS_SERVER(dir);
3119
3120 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3121 data->msg.rpc_argp = &data->arg;
3122 data->msg.rpc_resp = &data->res;
3123 data->arg.dir_fh = NFS_FH(dir);
3124 data->arg.server = server;
3125 data->arg.name = name;
3126 data->arg.attrs = sattr;
3127 data->arg.ftype = ftype;
3128 data->arg.bitmask = server->attr_bitmask;
3129 data->res.server = server;
3130 data->res.fh = &data->fh;
3131 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003132 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003133 }
3134 return data;
3135}
3136
3137static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3138{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003139 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003140 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003141 if (status == 0) {
3142 update_changeattr(dir, &data->res.dir_cinfo);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003143 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3144 }
3145 return status;
3146}
3147
3148static void nfs4_free_createdata(struct nfs4_createdata *data)
3149{
3150 kfree(data);
3151}
3152
Chuck Lever4f390c12006-08-22 20:06:22 -04003153static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003154 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003156 struct nfs4_createdata *data;
3157 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158
Chuck Lever94a6d752006-08-22 20:06:23 -04003159 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003160 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003161
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003162 status = -ENOMEM;
3163 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3164 if (data == NULL)
3165 goto out;
3166
3167 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3168 data->arg.u.symlink.pages = &page;
3169 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003171 status = nfs4_do_create(dir, dentry, data);
3172
3173 nfs4_free_createdata(data);
3174out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 return status;
3176}
3177
Chuck Lever4f390c12006-08-22 20:06:22 -04003178static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003179 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180{
3181 struct nfs4_exception exception = { };
3182 int err;
3183 do {
3184 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003185 _nfs4_proc_symlink(dir, dentry, page,
3186 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 &exception);
3188 } while (exception.retry);
3189 return err;
3190}
3191
3192static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3193 struct iattr *sattr)
3194{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003195 struct nfs4_createdata *data;
3196 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003198 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3199 if (data == NULL)
3200 goto out;
3201
3202 status = nfs4_do_create(dir, dentry, data);
3203
3204 nfs4_free_createdata(data);
3205out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 return status;
3207}
3208
3209static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3210 struct iattr *sattr)
3211{
3212 struct nfs4_exception exception = { };
3213 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003214
3215 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 do {
3217 err = nfs4_handle_exception(NFS_SERVER(dir),
3218 _nfs4_proc_mkdir(dir, dentry, sattr),
3219 &exception);
3220 } while (exception.retry);
3221 return err;
3222}
3223
3224static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003225 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226{
3227 struct inode *dir = dentry->d_inode;
3228 struct nfs4_readdir_arg args = {
3229 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003230 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 .pgbase = 0,
3232 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003233 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003234 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 };
3236 struct nfs4_readdir_res res;
3237 struct rpc_message msg = {
3238 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3239 .rpc_argp = &args,
3240 .rpc_resp = &res,
3241 .rpc_cred = cred,
3242 };
3243 int status;
3244
Harvey Harrison3110ff82008-05-02 13:42:44 -07003245 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003246 dentry->d_parent->d_name.name,
3247 dentry->d_name.name,
3248 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003249 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003251 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 -05003252 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003253 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003254 status += args.pgbase;
3255 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003256
3257 nfs_invalidate_atime(dir);
3258
Harvey Harrison3110ff82008-05-02 13:42:44 -07003259 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 return status;
3261}
3262
3263static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003264 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265{
3266 struct nfs4_exception exception = { };
3267 int err;
3268 do {
3269 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3270 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003271 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 &exception);
3273 } while (exception.retry);
3274 return err;
3275}
3276
3277static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3278 struct iattr *sattr, dev_t rdev)
3279{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003280 struct nfs4_createdata *data;
3281 int mode = sattr->ia_mode;
3282 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003284 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3285 if (data == NULL)
3286 goto out;
3287
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003289 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003291 data->arg.ftype = NF4BLK;
3292 data->arg.u.device.specdata1 = MAJOR(rdev);
3293 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 }
3295 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003296 data->arg.ftype = NF4CHR;
3297 data->arg.u.device.specdata1 = MAJOR(rdev);
3298 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003299 } else if (!S_ISSOCK(mode)) {
3300 status = -EINVAL;
3301 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003304 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003305out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003306 nfs4_free_createdata(data);
3307out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 return status;
3309}
3310
3311static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3312 struct iattr *sattr, dev_t rdev)
3313{
3314 struct nfs4_exception exception = { };
3315 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003316
3317 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 do {
3319 err = nfs4_handle_exception(NFS_SERVER(dir),
3320 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3321 &exception);
3322 } while (exception.retry);
3323 return err;
3324}
3325
3326static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3327 struct nfs_fsstat *fsstat)
3328{
3329 struct nfs4_statfs_arg args = {
3330 .fh = fhandle,
3331 .bitmask = server->attr_bitmask,
3332 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003333 struct nfs4_statfs_res res = {
3334 .fsstat = fsstat,
3335 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 struct rpc_message msg = {
3337 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3338 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003339 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 };
3341
Trond Myklebust0e574af2005-10-27 22:12:38 -04003342 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003343 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344}
3345
3346static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3347{
3348 struct nfs4_exception exception = { };
3349 int err;
3350 do {
3351 err = nfs4_handle_exception(server,
3352 _nfs4_proc_statfs(server, fhandle, fsstat),
3353 &exception);
3354 } while (exception.retry);
3355 return err;
3356}
3357
3358static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3359 struct nfs_fsinfo *fsinfo)
3360{
3361 struct nfs4_fsinfo_arg args = {
3362 .fh = fhandle,
3363 .bitmask = server->attr_bitmask,
3364 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003365 struct nfs4_fsinfo_res res = {
3366 .fsinfo = fsinfo,
3367 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 struct rpc_message msg = {
3369 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3370 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003371 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 };
3373
Bryan Schumaker7c513052011-03-24 17:12:24 +00003374 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375}
3376
3377static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3378{
3379 struct nfs4_exception exception = { };
3380 int err;
3381
3382 do {
3383 err = nfs4_handle_exception(server,
3384 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3385 &exception);
3386 } while (exception.retry);
3387 return err;
3388}
3389
3390static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3391{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003392 int error;
3393
Trond Myklebust0e574af2005-10-27 22:12:38 -04003394 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003395 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003396 if (error == 0) {
3397 /* block layout checks this! */
3398 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003399 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003400 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003401
3402 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403}
3404
3405static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3406 struct nfs_pathconf *pathconf)
3407{
3408 struct nfs4_pathconf_arg args = {
3409 .fh = fhandle,
3410 .bitmask = server->attr_bitmask,
3411 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003412 struct nfs4_pathconf_res res = {
3413 .pathconf = pathconf,
3414 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 struct rpc_message msg = {
3416 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3417 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003418 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 };
3420
3421 /* None of the pathconf attributes are mandatory to implement */
3422 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3423 memset(pathconf, 0, sizeof(*pathconf));
3424 return 0;
3425 }
3426
Trond Myklebust0e574af2005-10-27 22:12:38 -04003427 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003428 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429}
3430
3431static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3432 struct nfs_pathconf *pathconf)
3433{
3434 struct nfs4_exception exception = { };
3435 int err;
3436
3437 do {
3438 err = nfs4_handle_exception(server,
3439 _nfs4_proc_pathconf(server, fhandle, pathconf),
3440 &exception);
3441 } while (exception.retry);
3442 return err;
3443}
3444
Benny Halevyd20581a2011-05-22 19:52:03 +03003445void __nfs4_read_done_cb(struct nfs_read_data *data)
3446{
Fred Isamancd841602012-04-20 14:47:44 -04003447 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003448}
3449
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003450static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451{
Fred Isamancd841602012-04-20 14:47:44 -04003452 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003454 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003455 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003456 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003458
Benny Halevyd20581a2011-05-22 19:52:03 +03003459 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003461 renew_lease(server, data->timestamp);
3462 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463}
3464
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003465static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3466{
3467
3468 dprintk("--> %s\n", __func__);
3469
3470 if (!nfs4_sequence_done(task, &data->res.seq_res))
3471 return -EAGAIN;
3472
Benny Halevyd20581a2011-05-22 19:52:03 +03003473 return data->read_done_cb ? data->read_done_cb(task, data) :
3474 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003475}
3476
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003477static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 data->timestamp = jiffies;
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003480 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003481 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003482 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483}
3484
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003485static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3486{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003487 nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3488 &data->args.seq_args,
3489 &data->res.seq_res,
3490 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491}
3492
Fred Isamanb029bc92011-03-03 15:13:42 +00003493static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494{
Fred Isamancd841602012-04-20 14:47:44 -04003495 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003497 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003498 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003499 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003501 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003503 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003504 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003505 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506}
3507
Fred Isamanb029bc92011-03-03 15:13:42 +00003508static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3509{
3510 if (!nfs4_sequence_done(task, &data->res.seq_res))
3511 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003512 return data->write_done_cb ? data->write_done_cb(task, data) :
3513 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003514}
3515
Trond Myklebust5a37f852012-04-28 14:55:16 -04003516static
3517bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003518{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003519 const struct nfs_pgio_header *hdr = data->header;
3520
3521 /* Don't request attributes for pNFS or O_DIRECT writes */
3522 if (data->ds_clp != NULL || hdr->dreq != NULL)
3523 return false;
3524 /* Otherwise, request attributes if and only if we don't hold
3525 * a delegation
3526 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04003527 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003528}
Fred Isamana69aef12011-03-03 15:13:47 +00003529
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003530static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531{
Fred Isamancd841602012-04-20 14:47:44 -04003532 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003533
Trond Myklebust5a37f852012-04-28 14:55:16 -04003534 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003535 data->args.bitmask = NULL;
3536 data->res.fattr = NULL;
3537 } else
3538 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003539
Fred Isamanb029bc92011-03-03 15:13:42 +00003540 if (!data->write_done_cb)
3541 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003542 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 data->timestamp = jiffies;
3544
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003545 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003546 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547}
3548
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003549static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3550{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003551 nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3552 &data->args.seq_args,
3553 &data->res.seq_res,
3554 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555}
3556
Fred Isaman0b7c0152012-04-20 14:47:39 -04003557static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3558{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003559 nfs4_setup_sequence(NFS_SERVER(data->inode),
3560 &data->args.seq_args,
3561 &data->res.seq_res,
3562 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563}
3564
Fred Isaman0b7c0152012-04-20 14:47:39 -04003565static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003568
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003569 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003570 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003571 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003573 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574}
3575
Fred Isaman0b7c0152012-04-20 14:47:39 -04003576static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003577{
3578 if (!nfs4_sequence_done(task, &data->res.seq_res))
3579 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003580 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003581}
3582
Fred Isaman0b7c0152012-04-20 14:47:39 -04003583static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003585 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003586
Fred Isaman0b7c0152012-04-20 14:47:39 -04003587 if (data->commit_done_cb == NULL)
3588 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003589 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003590 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003591 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592}
3593
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003594struct nfs4_renewdata {
3595 struct nfs_client *client;
3596 unsigned long timestamp;
3597};
3598
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599/*
3600 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3601 * standalone procedure for queueing an asynchronous RENEW.
3602 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003603static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003604{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003605 struct nfs4_renewdata *data = calldata;
3606 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003607
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003608 if (atomic_read(&clp->cl_count) > 1)
3609 nfs4_schedule_state_renewal(clp);
3610 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003611 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003612}
3613
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003614static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003616 struct nfs4_renewdata *data = calldata;
3617 struct nfs_client *clp = data->client;
3618 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619
3620 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003621 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003622 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3623 return;
3624 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003625 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003626 return;
3627 }
3628 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003630 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631}
3632
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003633static const struct rpc_call_ops nfs4_renew_ops = {
3634 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003635 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003636};
3637
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003638static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639{
3640 struct rpc_message msg = {
3641 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3642 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003643 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003645 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003647 if (renew_flags == 0)
3648 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003649 if (!atomic_inc_not_zero(&clp->cl_count))
3650 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003651 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003652 if (data == NULL)
3653 return -ENOMEM;
3654 data->client = clp;
3655 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003657 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658}
3659
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003660static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661{
3662 struct rpc_message msg = {
3663 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3664 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003665 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 };
3667 unsigned long now = jiffies;
3668 int status;
3669
3670 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3671 if (status < 0)
3672 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003673 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 return 0;
3675}
3676
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003677static inline int nfs4_server_supports_acls(struct nfs_server *server)
3678{
3679 return (server->caps & NFS_CAP_ACLS)
3680 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3681 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3682}
3683
Trond Myklebust21f498c2012-08-24 10:59:25 -04003684/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3685 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003686 * the stack.
3687 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04003688#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003689
Neil Hormane9e3d722011-03-04 19:26:03 -05003690static int buf_to_pages_noslab(const void *buf, size_t buflen,
3691 struct page **pages, unsigned int *pgbase)
3692{
3693 struct page *newpage, **spages;
3694 int rc = 0;
3695 size_t len;
3696 spages = pages;
3697
3698 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04003699 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003700 newpage = alloc_page(GFP_KERNEL);
3701
3702 if (newpage == NULL)
3703 goto unwind;
3704 memcpy(page_address(newpage), buf, len);
3705 buf += len;
3706 buflen -= len;
3707 *pages++ = newpage;
3708 rc++;
3709 } while (buflen != 0);
3710
3711 return rc;
3712
3713unwind:
3714 for(; rc > 0; rc--)
3715 __free_page(spages[rc-1]);
3716 return -ENOMEM;
3717}
3718
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003719struct nfs4_cached_acl {
3720 int cached;
3721 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003722 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003723};
3724
3725static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003726{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003727 struct nfs_inode *nfsi = NFS_I(inode);
3728
3729 spin_lock(&inode->i_lock);
3730 kfree(nfsi->nfs4_acl);
3731 nfsi->nfs4_acl = acl;
3732 spin_unlock(&inode->i_lock);
3733}
3734
3735static void nfs4_zap_acl_attr(struct inode *inode)
3736{
3737 nfs4_set_cached_acl(inode, NULL);
3738}
3739
3740static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3741{
3742 struct nfs_inode *nfsi = NFS_I(inode);
3743 struct nfs4_cached_acl *acl;
3744 int ret = -ENOENT;
3745
3746 spin_lock(&inode->i_lock);
3747 acl = nfsi->nfs4_acl;
3748 if (acl == NULL)
3749 goto out;
3750 if (buf == NULL) /* user is just asking for length */
3751 goto out_len;
3752 if (acl->cached == 0)
3753 goto out;
3754 ret = -ERANGE; /* see getxattr(2) man page */
3755 if (acl->len > buflen)
3756 goto out;
3757 memcpy(buf, acl->data, acl->len);
3758out_len:
3759 ret = acl->len;
3760out:
3761 spin_unlock(&inode->i_lock);
3762 return ret;
3763}
3764
Sachin Prabhu5794d212012-04-17 14:36:40 +01003765static 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 +00003766{
3767 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003768 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003769
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003770 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003771 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003772 if (acl == NULL)
3773 goto out;
3774 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003775 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003776 } else {
3777 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3778 if (acl == NULL)
3779 goto out;
3780 acl->cached = 0;
3781 }
3782 acl->len = acl_len;
3783out:
3784 nfs4_set_cached_acl(inode, acl);
3785}
3786
Andy Adamsonbf118a32011-12-07 11:55:27 -05003787/*
3788 * The getxattr API returns the required buffer length when called with a
3789 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3790 * the required buf. On a NULL buf, we send a page of data to the server
3791 * guessing that the ACL request can be serviced by a page. If so, we cache
3792 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3793 * the cache. If not so, we throw away the page, and cache the required
3794 * length. The next getxattr call will then produce another round trip to
3795 * the server, this time with the input buf of the required size.
3796 */
Trond Myklebust16b42892006-08-24 12:27:15 -04003797static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003798{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003799 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003800 struct nfs_getaclargs args = {
3801 .fh = NFS_FH(inode),
3802 .acl_pages = pages,
3803 .acl_len = buflen,
3804 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003805 struct nfs_getaclres res = {
3806 .acl_len = buflen,
3807 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003808 struct rpc_message msg = {
3809 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3810 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003811 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003812 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003813 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3814 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003815
Andy Adamsonbf118a32011-12-07 11:55:27 -05003816 /* As long as we're doing a round trip to the server anyway,
3817 * let's be prepared for a page of acl data. */
3818 if (npages == 0)
3819 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003820 if (npages > ARRAY_SIZE(pages))
3821 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003822
Andy Adamsonbf118a32011-12-07 11:55:27 -05003823 for (i = 0; i < npages; i++) {
3824 pages[i] = alloc_page(GFP_KERNEL);
3825 if (!pages[i])
3826 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003827 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01003828
3829 /* for decoding across pages */
3830 res.acl_scratch = alloc_page(GFP_KERNEL);
3831 if (!res.acl_scratch)
3832 goto out_free;
3833
Andy Adamsonbf118a32011-12-07 11:55:27 -05003834 args.acl_len = npages * PAGE_SIZE;
3835 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003836
Peng Taode040be2012-01-10 22:42:47 +08003837 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003838 __func__, buf, buflen, npages, args.acl_len);
3839 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3840 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003841 if (ret)
3842 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003843
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003844 /* Handle the case where the passed-in buffer is too short */
3845 if (res.acl_flags & NFS4_ACL_TRUNC) {
3846 /* Did the user only issue a request for the acl length? */
3847 if (buf == NULL)
3848 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003849 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003850 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003851 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003852 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01003853 if (buf) {
3854 if (res.acl_len > buflen) {
3855 ret = -ERANGE;
3856 goto out_free;
3857 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003858 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01003859 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003860out_ok:
3861 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003862out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003863 for (i = 0; i < npages; i++)
3864 if (pages[i])
3865 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05003866 if (res.acl_scratch)
3867 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003868 return ret;
3869}
3870
Trond Myklebust16b42892006-08-24 12:27:15 -04003871static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3872{
3873 struct nfs4_exception exception = { };
3874 ssize_t ret;
3875 do {
3876 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3877 if (ret >= 0)
3878 break;
3879 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3880 } while (exception.retry);
3881 return ret;
3882}
3883
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003884static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3885{
3886 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003887 int ret;
3888
3889 if (!nfs4_server_supports_acls(server))
3890 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003891 ret = nfs_revalidate_inode(server, inode);
3892 if (ret < 0)
3893 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003894 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3895 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003896 ret = nfs4_read_cached_acl(inode, buf, buflen);
3897 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003898 /* -ENOENT is returned if there is no ACL or if there is an ACL
3899 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003900 return ret;
3901 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003902}
3903
Trond Myklebust16b42892006-08-24 12:27:15 -04003904static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003905{
3906 struct nfs_server *server = NFS_SERVER(inode);
3907 struct page *pages[NFS4ACL_MAXPAGES];
3908 struct nfs_setaclargs arg = {
3909 .fh = NFS_FH(inode),
3910 .acl_pages = pages,
3911 .acl_len = buflen,
3912 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003913 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003914 struct rpc_message msg = {
3915 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3916 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003917 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003918 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003919 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05003920 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003921
3922 if (!nfs4_server_supports_acls(server))
3923 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003924 if (npages > ARRAY_SIZE(pages))
3925 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05003926 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3927 if (i < 0)
3928 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04003929 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003930 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003931
3932 /*
3933 * Free each page after tx, so the only ref left is
3934 * held by the network stack
3935 */
3936 for (; i > 0; i--)
3937 put_page(pages[i-1]);
3938
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003939 /*
3940 * Acl update can result in inode attribute update.
3941 * so mark the attribute cache invalid.
3942 */
3943 spin_lock(&inode->i_lock);
3944 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3945 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003946 nfs_access_zap_cache(inode);
3947 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003948 return ret;
3949}
3950
Trond Myklebust16b42892006-08-24 12:27:15 -04003951static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3952{
3953 struct nfs4_exception exception = { };
3954 int err;
3955 do {
3956 err = nfs4_handle_exception(NFS_SERVER(inode),
3957 __nfs4_proc_set_acl(inode, buf, buflen),
3958 &exception);
3959 } while (exception.retry);
3960 return err;
3961}
3962
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003964nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003966 struct nfs_client *clp = server->nfs_client;
3967
3968 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 return 0;
3970 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05003971 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003972 case -NFS4ERR_ADMIN_REVOKED:
3973 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04003974 if (state == NULL)
3975 break;
3976 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003977 case -NFS4ERR_OPENMODE:
3978 if (state == NULL)
3979 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003980 nfs4_schedule_stateid_recovery(server, state);
3981 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003982 case -NFS4ERR_EXPIRED:
3983 if (state != NULL)
3984 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003986 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003987 nfs4_schedule_lease_recovery(clp);
3988 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003989#if defined(CONFIG_NFS_V4_1)
3990 case -NFS4ERR_BADSESSION:
3991 case -NFS4ERR_BADSLOT:
3992 case -NFS4ERR_BAD_HIGH_SLOT:
3993 case -NFS4ERR_DEADSESSION:
3994 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3995 case -NFS4ERR_SEQ_FALSE_RETRY:
3996 case -NFS4ERR_SEQ_MISORDERED:
3997 dprintk("%s ERROR %d, Reset session\n", __func__,
3998 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04003999 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04004000 task->tk_status = 0;
4001 return -EAGAIN;
4002#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004004 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05004005 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4007 task->tk_status = 0;
4008 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004009 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 case -NFS4ERR_OLD_STATEID:
4011 task->tk_status = 0;
4012 return -EAGAIN;
4013 }
4014 task->tk_status = nfs4_map_errors(task->tk_status);
4015 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004016wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004017 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004018 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4019 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4020 task->tk_status = 0;
4021 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022}
4023
Chuck Leverf0920752012-05-21 22:45:41 -04004024static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4025 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004026{
4027 __be32 verf[2];
4028
Chuck Lever2c820d92012-05-21 22:45:33 -04004029 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4030 /* An impossible timestamp guarantees this value
4031 * will never match a generated boot time. */
4032 verf[0] = 0;
4033 verf[1] = (__be32)(NSEC_PER_SEC + 1);
4034 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004035 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4036 verf[0] = (__be32)nn->boot_time.tv_sec;
4037 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04004038 }
Chuck Levercd937102012-03-02 17:14:31 -05004039 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4040}
4041
Chuck Levere984a552012-09-14 17:24:21 -04004042static unsigned int
4043nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4044 char *buf, size_t len)
4045{
4046 unsigned int result;
4047
4048 rcu_read_lock();
4049 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4050 clp->cl_ipaddr,
4051 rpc_peeraddr2str(clp->cl_rpcclient,
4052 RPC_DISPLAY_ADDR),
4053 rpc_peeraddr2str(clp->cl_rpcclient,
4054 RPC_DISPLAY_PROTO));
4055 rcu_read_unlock();
4056 return result;
4057}
4058
4059static unsigned int
4060nfs4_init_uniform_client_string(const struct nfs_client *clp,
4061 char *buf, size_t len)
4062{
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004063 char *nodename = clp->cl_rpcclient->cl_nodename;
4064
4065 if (nfs4_client_id_uniquifier[0] != '\0')
4066 nodename = nfs4_client_id_uniquifier;
Chuck Levere984a552012-09-14 17:24:21 -04004067 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4068 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004069 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004070}
4071
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004072/**
4073 * nfs4_proc_setclientid - Negotiate client ID
4074 * @clp: state data structure
4075 * @program: RPC program for NFSv4 callback service
4076 * @port: IP port number for NFS4 callback service
4077 * @cred: RPC credential to use for this call
4078 * @res: where to place the result
4079 *
4080 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4081 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004082int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4083 unsigned short port, struct rpc_cred *cred,
4084 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085{
4086 nfs4_verifier sc_verifier;
4087 struct nfs4_setclientid setclientid = {
4088 .sc_verifier = &sc_verifier,
4089 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004090 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091 };
4092 struct rpc_message msg = {
4093 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4094 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004095 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004096 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004098 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099
Chuck Leverde734832012-07-11 16:30:50 -04004100 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004101 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004102 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4103 setclientid.sc_name_len =
4104 nfs4_init_uniform_client_string(clp,
4105 setclientid.sc_name,
4106 sizeof(setclientid.sc_name));
4107 else
4108 setclientid.sc_name_len =
4109 nfs4_init_nonuniform_client_string(clp,
4110 setclientid.sc_name,
4111 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004112 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004113 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004114 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004115 sizeof(setclientid.sc_netid),
4116 rpc_peeraddr2str(clp->cl_rpcclient,
4117 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004118 rcu_read_unlock();
4119 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004120 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121 clp->cl_ipaddr, port >> 8, port & 255);
4122
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004123 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4124 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4125 setclientid.sc_name_len, setclientid.sc_name);
4126 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4127 dprintk("NFS reply setclientid: %d\n", status);
4128 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129}
4130
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004131/**
4132 * nfs4_proc_setclientid_confirm - Confirm client ID
4133 * @clp: state data structure
4134 * @res: result of a previous SETCLIENTID
4135 * @cred: RPC credential to use for this call
4136 *
4137 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4138 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004139int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004140 struct nfs4_setclientid_res *arg,
4141 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142{
4143 struct nfs_fsinfo fsinfo;
4144 struct rpc_message msg = {
4145 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004146 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004148 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149 };
4150 unsigned long now;
4151 int status;
4152
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004153 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4154 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4155 clp->cl_clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004157 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 if (status == 0) {
4159 spin_lock(&clp->cl_lock);
4160 clp->cl_lease_time = fsinfo.lease_time * HZ;
4161 clp->cl_last_renewal = now;
4162 spin_unlock(&clp->cl_lock);
4163 }
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004164 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165 return status;
4166}
4167
Trond Myklebustfe650402006-01-03 09:55:18 +01004168struct nfs4_delegreturndata {
4169 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004170 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004171 struct nfs_fh fh;
4172 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004173 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004174 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004175 int rpc_status;
4176};
4177
Trond Myklebustfe650402006-01-03 09:55:18 +01004178static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4179{
4180 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004181
Trond Myklebust14516c32010-07-31 14:29:06 -04004182 if (!nfs4_sequence_done(task, &data->res.seq_res))
4183 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004184
Ricardo Labiaga79708862009-12-07 09:23:21 -05004185 switch (task->tk_status) {
4186 case -NFS4ERR_STALE_STATEID:
4187 case -NFS4ERR_EXPIRED:
4188 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004189 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004190 break;
4191 default:
4192 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4193 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004194 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004195 return;
4196 }
4197 }
4198 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004199}
4200
4201static void nfs4_delegreturn_release(void *calldata)
4202{
Trond Myklebustfe650402006-01-03 09:55:18 +01004203 kfree(calldata);
4204}
4205
Andy Adamson938e1012009-04-01 09:22:28 -04004206#if defined(CONFIG_NFS_V4_1)
4207static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4208{
4209 struct nfs4_delegreturndata *d_data;
4210
4211 d_data = (struct nfs4_delegreturndata *)data;
4212
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004213 nfs4_setup_sequence(d_data->res.server,
4214 &d_data->args.seq_args,
4215 &d_data->res.seq_res,
4216 task);
Andy Adamson938e1012009-04-01 09:22:28 -04004217}
4218#endif /* CONFIG_NFS_V4_1 */
4219
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004220static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004221#if defined(CONFIG_NFS_V4_1)
4222 .rpc_call_prepare = nfs4_delegreturn_prepare,
4223#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004224 .rpc_call_done = nfs4_delegreturn_done,
4225 .rpc_release = nfs4_delegreturn_release,
4226};
4227
Trond Myklebuste6f81072008-01-24 18:14:34 -05004228static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004229{
4230 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004231 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004232 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004233 struct rpc_message msg = {
4234 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4235 .rpc_cred = cred,
4236 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004237 struct rpc_task_setup task_setup_data = {
4238 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004239 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004240 .callback_ops = &nfs4_delegreturn_ops,
4241 .flags = RPC_TASK_ASYNC,
4242 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004243 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004244
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004245 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004246 if (data == NULL)
4247 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004248 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004249 data->args.fhandle = &data->fh;
4250 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004251 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004252 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004253 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004254 data->res.fattr = &data->fattr;
4255 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004256 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004257 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004258 data->rpc_status = 0;
4259
Trond Myklebustc970aa82007-07-14 15:39:59 -04004260 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004261 msg.rpc_argp = &data->args;
4262 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004263 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004264 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004265 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004266 if (!issync)
4267 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004268 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004269 if (status != 0)
4270 goto out;
4271 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004272 if (status == 0)
4273 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4274 else
4275 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004276out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004277 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004278 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279}
4280
Trond Myklebuste6f81072008-01-24 18:14:34 -05004281int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282{
4283 struct nfs_server *server = NFS_SERVER(inode);
4284 struct nfs4_exception exception = { };
4285 int err;
4286 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004287 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288 switch (err) {
4289 case -NFS4ERR_STALE_STATEID:
4290 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 case 0:
4292 return 0;
4293 }
4294 err = nfs4_handle_exception(server, err, &exception);
4295 } while (exception.retry);
4296 return err;
4297}
4298
4299#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4300#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4301
4302/*
4303 * sleep, with exponential backoff, and retry the LOCK operation.
4304 */
4305static unsigned long
4306nfs4_set_lock_task_retry(unsigned long timeout)
4307{
Jeff Laytond3103102011-12-01 22:44:39 +01004308 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309 timeout <<= 1;
4310 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4311 return NFS4_LOCK_MAXTIMEOUT;
4312 return timeout;
4313}
4314
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4316{
4317 struct inode *inode = state->inode;
4318 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004319 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004320 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004322 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004324 struct nfs_lockt_res res = {
4325 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326 };
4327 struct rpc_message msg = {
4328 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4329 .rpc_argp = &arg,
4330 .rpc_resp = &res,
4331 .rpc_cred = state->owner->so_cred,
4332 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333 struct nfs4_lock_state *lsp;
4334 int status;
4335
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004336 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004337 status = nfs4_set_lock_state(state, request);
4338 if (status != 0)
4339 goto out;
4340 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004341 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004342 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004343 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004344 switch (status) {
4345 case 0:
4346 request->fl_type = F_UNLCK;
4347 break;
4348 case -NFS4ERR_DENIED:
4349 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004351 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004352out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353 return status;
4354}
4355
4356static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4357{
4358 struct nfs4_exception exception = { };
4359 int err;
4360
4361 do {
4362 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4363 _nfs4_proc_getlk(state, cmd, request),
4364 &exception);
4365 } while (exception.retry);
4366 return err;
4367}
4368
4369static int do_vfs_lock(struct file *file, struct file_lock *fl)
4370{
4371 int res = 0;
4372 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4373 case FL_POSIX:
4374 res = posix_lock_file_wait(file, fl);
4375 break;
4376 case FL_FLOCK:
4377 res = flock_lock_file_wait(file, fl);
4378 break;
4379 default:
4380 BUG();
4381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382 return res;
4383}
4384
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004385struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004386 struct nfs_locku_args arg;
4387 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004388 struct nfs4_lock_state *lsp;
4389 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004390 struct file_lock fl;
4391 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004392 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004393};
4394
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004395static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4396 struct nfs_open_context *ctx,
4397 struct nfs4_lock_state *lsp,
4398 struct nfs_seqid *seqid)
4399{
4400 struct nfs4_unlockdata *p;
4401 struct inode *inode = lsp->ls_state->inode;
4402
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004403 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004404 if (p == NULL)
4405 return NULL;
4406 p->arg.fh = NFS_FH(inode);
4407 p->arg.fl = &p->fl;
4408 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004409 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004410 p->arg.stateid = &lsp->ls_stateid;
4411 p->lsp = lsp;
4412 atomic_inc(&lsp->ls_count);
4413 /* Ensure we don't close file until we're done freeing locks! */
4414 p->ctx = get_nfs_open_context(ctx);
4415 memcpy(&p->fl, fl, sizeof(p->fl));
4416 p->server = NFS_SERVER(inode);
4417 return p;
4418}
4419
Trond Myklebust06f814a2006-01-03 09:55:07 +01004420static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004421{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004422 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004423 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004424 nfs4_put_lock_state(calldata->lsp);
4425 put_nfs_open_context(calldata->ctx);
4426 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004427}
4428
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004429static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004430{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004431 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004432
Trond Myklebust14516c32010-07-31 14:29:06 -04004433 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4434 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004435 switch (task->tk_status) {
4436 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004437 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4438 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004439 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004440 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004441 case -NFS4ERR_BAD_STATEID:
4442 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004443 case -NFS4ERR_STALE_STATEID:
4444 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004445 break;
4446 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004447 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004448 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004449 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04004450 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004451}
4452
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004453static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004454{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004455 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004457 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004458 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04004459 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004460 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004461 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004462 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004463 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004464 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004465 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004466 &calldata->res.seq_res,
4467 task) != 0)
4468 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004469 return;
4470out_no_action:
4471 task->tk_action = NULL;
4472out_wait:
4473 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474}
4475
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004476static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004477 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004478 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004479 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004480};
4481
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004482static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4483 struct nfs_open_context *ctx,
4484 struct nfs4_lock_state *lsp,
4485 struct nfs_seqid *seqid)
4486{
4487 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004488 struct rpc_message msg = {
4489 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4490 .rpc_cred = ctx->cred,
4491 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004492 struct rpc_task_setup task_setup_data = {
4493 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004494 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004495 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004496 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004497 .flags = RPC_TASK_ASYNC,
4498 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004499
Frank Filz137d6ac2007-07-09 15:32:29 -07004500 /* Ensure this is an unlock - when canceling a lock, the
4501 * canceled lock is passed in, and it won't be an unlock.
4502 */
4503 fl->fl_type = F_UNLCK;
4504
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004505 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4506 if (data == NULL) {
4507 nfs_free_seqid(seqid);
4508 return ERR_PTR(-ENOMEM);
4509 }
4510
Trond Myklebust9d12b212012-01-17 22:04:25 -05004511 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004512 msg.rpc_argp = &data->arg;
4513 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004514 task_setup_data.callback_data = data;
4515 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004516}
4517
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4519{
Trond Myklebust65b62a22013-02-07 10:54:07 -05004520 struct inode *inode = state->inode;
4521 struct nfs4_state_owner *sp = state->owner;
4522 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004523 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004524 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004525 struct rpc_task *task;
4526 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004527 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528
Trond Myklebust9b073572006-06-29 16:38:34 -04004529 status = nfs4_set_lock_state(state, request);
4530 /* Unlock _before_ we do the RPC call */
4531 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05004532 /* Exclude nfs_delegation_claim_locks() */
4533 mutex_lock(&sp->so_delegreturn_mutex);
4534 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004535 down_read(&nfsi->rwsem);
4536 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4537 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05004538 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04004539 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004540 }
4541 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05004542 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04004543 if (status != 0)
4544 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004545 /* Is this a delegated lock? */
4546 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004547 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004548 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004549 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004550 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004551 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004552 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004553 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004554 status = PTR_ERR(task);
4555 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004556 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004557 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004558 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004559out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004560 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004561 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562}
4563
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004564struct nfs4_lockdata {
4565 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004566 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004567 struct nfs4_lock_state *lsp;
4568 struct nfs_open_context *ctx;
4569 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004570 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004571 int rpc_status;
4572 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004573 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004574};
4575
4576static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004577 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4578 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004579{
4580 struct nfs4_lockdata *p;
4581 struct inode *inode = lsp->ls_state->inode;
4582 struct nfs_server *server = NFS_SERVER(inode);
4583
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004584 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004585 if (p == NULL)
4586 return NULL;
4587
4588 p->arg.fh = NFS_FH(inode);
4589 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004590 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004591 if (p->arg.open_seqid == NULL)
4592 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004593 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004594 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004595 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004596 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004597 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004598 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004599 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004600 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004601 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004602 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004603 atomic_inc(&lsp->ls_count);
4604 p->ctx = get_nfs_open_context(ctx);
4605 memcpy(&p->fl, fl, sizeof(p->fl));
4606 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004607out_free_seqid:
4608 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004609out_free:
4610 kfree(p);
4611 return NULL;
4612}
4613
4614static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4615{
4616 struct nfs4_lockdata *data = calldata;
4617 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618
Harvey Harrison3110ff82008-05-02 13:42:44 -07004619 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004620 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004621 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004622 /* Do we need to do an open_to_lock_owner? */
4623 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004624 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004625 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004626 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004627 data->arg.open_stateid = &state->stateid;
4628 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004629 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004630 } else
4631 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004632 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004633 if (nfs4_setup_sequence(data->server,
4634 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004635 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004636 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04004637 return;
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004638 nfs_release_seqid(data->arg.open_seqid);
4639out_release_lock_seqid:
4640 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004641out_wait:
4642 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004643 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004644}
4645
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004646static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4647{
4648 struct nfs4_lockdata *data = calldata;
4649
Harvey Harrison3110ff82008-05-02 13:42:44 -07004650 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004651
Trond Myklebust14516c32010-07-31 14:29:06 -04004652 if (!nfs4_sequence_done(task, &data->res.seq_res))
4653 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004654
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004655 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004656 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004657 if (data->rpc_status == 0)
4658 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4659 else
4660 goto out;
4661 }
4662 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004663 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004664 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04004665 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004666 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004667out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004668 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004669}
4670
4671static void nfs4_lock_release(void *calldata)
4672{
4673 struct nfs4_lockdata *data = calldata;
4674
Harvey Harrison3110ff82008-05-02 13:42:44 -07004675 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004676 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004677 if (data->cancelled != 0) {
4678 struct rpc_task *task;
4679 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4680 data->arg.lock_seqid);
4681 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004682 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004683 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004684 } else
4685 nfs_free_seqid(data->arg.lock_seqid);
4686 nfs4_put_lock_state(data->lsp);
4687 put_nfs_open_context(data->ctx);
4688 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004689 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004690}
4691
4692static const struct rpc_call_ops nfs4_lock_ops = {
4693 .rpc_call_prepare = nfs4_lock_prepare,
4694 .rpc_call_done = nfs4_lock_done,
4695 .rpc_release = nfs4_lock_release,
4696};
4697
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004698static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4699{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004700 switch (error) {
4701 case -NFS4ERR_ADMIN_REVOKED:
4702 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004703 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004704 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04004705 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004706 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004707 break;
4708 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004709 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004710 case -NFS4ERR_EXPIRED:
4711 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004712 };
4713}
4714
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004715static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004716{
4717 struct nfs4_lockdata *data;
4718 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004719 struct rpc_message msg = {
4720 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4721 .rpc_cred = state->owner->so_cred,
4722 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004723 struct rpc_task_setup task_setup_data = {
4724 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004725 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004726 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004727 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004728 .flags = RPC_TASK_ASYNC,
4729 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004730 int ret;
4731
Harvey Harrison3110ff82008-05-02 13:42:44 -07004732 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004733 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004734 fl->fl_u.nfs4_fl.owner,
4735 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004736 if (data == NULL)
4737 return -ENOMEM;
4738 if (IS_SETLKW(cmd))
4739 data->arg.block = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004740 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004741 msg.rpc_argp = &data->arg;
4742 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004743 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004744 if (recovery_type > NFS_LOCK_NEW) {
4745 if (recovery_type == NFS_LOCK_RECLAIM)
4746 data->arg.reclaim = NFS_LOCK_RECLAIM;
4747 nfs4_set_sequence_privileged(&data->arg.seq_args);
4748 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04004749 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004750 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004751 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004752 ret = nfs4_wait_for_completion_rpc_task(task);
4753 if (ret == 0) {
4754 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004755 if (ret)
4756 nfs4_handle_setlk_error(data->server, data->lsp,
4757 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004758 } else
4759 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004760 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004761 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004762 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763}
4764
4765static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4766{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004767 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004768 struct nfs4_exception exception = {
4769 .inode = state->inode,
4770 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004771 int err;
4772
4773 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004774 /* Cache the lock if possible... */
4775 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4776 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004777 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004778 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004779 break;
4780 nfs4_handle_exception(server, err, &exception);
4781 } while (exception.retry);
4782 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783}
4784
4785static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4786{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004787 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004788 struct nfs4_exception exception = {
4789 .inode = state->inode,
4790 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004791 int err;
4792
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004793 err = nfs4_set_lock_state(state, request);
4794 if (err != 0)
4795 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004796 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004797 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4798 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004799 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004800 switch (err) {
4801 default:
4802 goto out;
4803 case -NFS4ERR_GRACE:
4804 case -NFS4ERR_DELAY:
4805 nfs4_handle_exception(server, err, &exception);
4806 err = 0;
4807 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004808 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004809out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004810 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811}
4812
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004813#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004814/**
4815 * nfs41_check_expired_locks - possibly free a lock stateid
4816 *
4817 * @state: NFSv4 state for an inode
4818 *
4819 * Returns NFS_OK if recovery for this stateid is now finished.
4820 * Otherwise a negative NFS4ERR value is returned.
4821 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004822static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004823{
Chuck Levereb64cf92012-07-11 16:30:05 -04004824 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004825 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004826 struct nfs_server *server = NFS_SERVER(state->inode);
4827
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004828 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04004829 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004830 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4831 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004832 /* Free the stateid unless the server
4833 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04004834 if (status != -NFS4ERR_BAD_STATEID)
4835 nfs41_free_stateid(server,
4836 &lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004837 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004838 ret = status;
4839 }
4840 }
4841 };
4842
4843 return ret;
4844}
4845
4846static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4847{
4848 int status = NFS_OK;
4849
4850 if (test_bit(LK_STATE_IN_USE, &state->flags))
4851 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04004852 if (status != NFS_OK)
4853 status = nfs4_lock_expired(state, request);
4854 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004855}
4856#endif
4857
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4859{
Trond Myklebust9a99af42013-02-04 20:17:49 -05004860 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004861 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004862 unsigned char fl_flags = request->fl_flags;
Trond Myklebust9a99af42013-02-04 20:17:49 -05004863 unsigned int seq;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004864 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004866 if ((fl_flags & FL_POSIX) &&
4867 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4868 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004869 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004870 status = nfs4_set_lock_state(state, request);
4871 if (status != 0)
4872 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004873 request->fl_flags |= FL_ACCESS;
4874 status = do_vfs_lock(request->fl_file, request);
4875 if (status < 0)
4876 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004877 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004878 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004879 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004880 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004881 request->fl_flags = fl_flags & ~FL_SLEEP;
4882 status = do_vfs_lock(request->fl_file, request);
4883 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004884 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05004885 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
4886 up_read(&nfsi->rwsem);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004887 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004888 if (status != 0)
Trond Myklebust9a99af42013-02-04 20:17:49 -05004889 goto out;
4890 down_read(&nfsi->rwsem);
4891 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
4892 status = -NFS4ERR_DELAY;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004893 goto out_unlock;
Trond Myklebust9a99af42013-02-04 20:17:49 -05004894 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004895 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004896 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004897 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004898 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4899 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004900out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004901 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004902out:
4903 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904 return status;
4905}
4906
4907static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4908{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004909 struct nfs4_exception exception = {
4910 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04004911 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004912 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913 int err;
4914
4915 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004916 err = _nfs4_proc_setlk(state, cmd, request);
4917 if (err == -NFS4ERR_DENIED)
4918 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004920 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921 } while (exception.retry);
4922 return err;
4923}
4924
4925static int
4926nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4927{
4928 struct nfs_open_context *ctx;
4929 struct nfs4_state *state;
4930 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4931 int status;
4932
4933 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004934 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935 state = ctx->state;
4936
4937 if (request->fl_start < 0 || request->fl_end < 0)
4938 return -EINVAL;
4939
Trond Myklebustd9531262009-07-21 19:22:38 -04004940 if (IS_GETLK(cmd)) {
4941 if (state != NULL)
4942 return nfs4_proc_getlk(state, F_GETLK, request);
4943 return 0;
4944 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945
4946 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4947 return -EINVAL;
4948
Trond Myklebustd9531262009-07-21 19:22:38 -04004949 if (request->fl_type == F_UNLCK) {
4950 if (state != NULL)
4951 return nfs4_proc_unlck(state, cmd, request);
4952 return 0;
4953 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954
Trond Myklebustd9531262009-07-21 19:22:38 -04004955 if (state == NULL)
4956 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04004957 /*
4958 * Don't rely on the VFS having checked the file open mode,
4959 * since it won't do this for flock() locks.
4960 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04004961 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04004962 case F_RDLCK:
4963 if (!(filp->f_mode & FMODE_READ))
4964 return -EBADF;
4965 break;
4966 case F_WRLCK:
4967 if (!(filp->f_mode & FMODE_WRITE))
4968 return -EBADF;
4969 }
4970
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971 do {
4972 status = nfs4_proc_setlk(state, cmd, request);
4973 if ((status != -EAGAIN) || IS_SETLK(cmd))
4974 break;
4975 timeout = nfs4_set_lock_task_retry(timeout);
4976 status = -ERESTARTSYS;
4977 if (signalled())
4978 break;
4979 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980 return status;
4981}
4982
Trond Myklebust888e6942005-11-04 15:38:11 -05004983int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4984{
4985 struct nfs_server *server = NFS_SERVER(state->inode);
4986 struct nfs4_exception exception = { };
4987 int err;
4988
4989 err = nfs4_set_lock_state(state, fl);
4990 if (err != 0)
4991 goto out;
4992 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004993 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004994 switch (err) {
4995 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004996 printk(KERN_ERR "NFS: %s: unhandled error "
4997 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004998 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004999 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07005000 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07005001 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005002 case -NFS4ERR_STALE_STATEID:
Trond Myklebust37380e42013-02-05 12:35:16 -05005003 set_bit(NFS_DELEGATED_STATE, &state->flags);
5004 case -NFS4ERR_EXPIRED:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005005 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust37380e42013-02-05 12:35:16 -05005006 err = -EAGAIN;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005007 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05005008 case -NFS4ERR_BADSESSION:
5009 case -NFS4ERR_BADSLOT:
5010 case -NFS4ERR_BAD_HIGH_SLOT:
5011 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
5012 case -NFS4ERR_DEADSESSION:
Trond Myklebust37380e42013-02-05 12:35:16 -05005013 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust9f594792012-05-27 13:02:53 -04005014 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Trond Myklebust37380e42013-02-05 12:35:16 -05005015 err = -EAGAIN;
Trond Myklebustd5122202009-06-17 13:22:58 -07005016 goto out;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005017 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005018 case -NFS4ERR_ADMIN_REVOKED:
5019 case -NFS4ERR_BAD_STATEID:
5020 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005021 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07005022 err = 0;
5023 goto out;
5024 case -ENOMEM:
5025 case -NFS4ERR_DENIED:
5026 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
5027 err = 0;
5028 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07005029 }
Trond Myklebust37380e42013-02-05 12:35:16 -05005030 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust888e6942005-11-04 15:38:11 -05005031 err = nfs4_handle_exception(server, err, &exception);
5032 } while (exception.retry);
5033out:
5034 return err;
5035}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005036
Trond Myklebustcf470c32012-03-07 13:49:12 -05005037struct nfs_release_lockowner_data {
5038 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005039 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005040 struct nfs_release_lockowner_args args;
5041};
5042
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005043static void nfs4_release_lockowner_release(void *calldata)
5044{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005045 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005046 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005047 kfree(calldata);
5048}
5049
Trond Myklebust17280172012-03-11 13:11:00 -04005050static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005051 .rpc_release = nfs4_release_lockowner_release,
5052};
5053
Trond Myklebustcf470c32012-03-07 13:49:12 -05005054int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005055{
5056 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005057 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005058 struct rpc_message msg = {
5059 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5060 };
5061
5062 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005063 return -EINVAL;
5064 data = kmalloc(sizeof(*data), GFP_NOFS);
5065 if (!data)
5066 return -ENOMEM;
5067 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005068 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005069 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5070 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5071 data->args.lock_owner.s_dev = server->s_dev;
5072 msg.rpc_argp = &data->args;
5073 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5074 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005075}
5076
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005077#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5078
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005079static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5080 const void *buf, size_t buflen,
5081 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005082{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005083 if (strcmp(key, "") != 0)
5084 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005085
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005086 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005087}
5088
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005089static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5090 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005091{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005092 if (strcmp(key, "") != 0)
5093 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005094
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005095 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005096}
5097
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005098static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5099 size_t list_len, const char *name,
5100 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005101{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005102 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005103
J. Bruce Fields096455a2006-03-20 23:23:42 -05005104 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5105 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005106
5107 if (list && len <= list_len)
5108 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005109 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005110}
5111
Andy Adamson533eb462011-06-13 18:25:56 -04005112/*
5113 * nfs_fhget will use either the mounted_on_fileid or the fileid
5114 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005115static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5116{
Andy Adamson533eb462011-06-13 18:25:56 -04005117 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5118 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5119 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005120 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005121 return;
5122
5123 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005124 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005125 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5126 fattr->nlink = 2;
5127}
5128
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005129static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5130 const struct qstr *name,
5131 struct nfs4_fs_locations *fs_locations,
5132 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005133{
5134 struct nfs_server *server = NFS_SERVER(dir);
5135 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005136 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005137 };
5138 struct nfs4_fs_locations_arg args = {
5139 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005140 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005141 .page = page,
5142 .bitmask = bitmask,
5143 };
Benny Halevy22958462009-04-01 09:22:02 -04005144 struct nfs4_fs_locations_res res = {
5145 .fs_locations = fs_locations,
5146 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005147 struct rpc_message msg = {
5148 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5149 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005150 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005151 };
5152 int status;
5153
Harvey Harrison3110ff82008-05-02 13:42:44 -07005154 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005155
5156 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5157 * is not supported */
5158 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5159 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5160 else
5161 bitmask[0] |= FATTR4_WORD0_FILEID;
5162
Trond Myklebustc228fd32007-01-13 02:28:11 -05005163 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005164 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005165 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005166 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005167 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005168 return status;
5169}
5170
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005171int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5172 const struct qstr *name,
5173 struct nfs4_fs_locations *fs_locations,
5174 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005175{
5176 struct nfs4_exception exception = { };
5177 int err;
5178 do {
5179 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005180 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005181 &exception);
5182 } while (exception.retry);
5183 return err;
5184}
5185
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005186static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5187{
5188 int status;
5189 struct nfs4_secinfo_arg args = {
5190 .dir_fh = NFS_FH(dir),
5191 .name = name,
5192 };
5193 struct nfs4_secinfo_res res = {
5194 .flavors = flavors,
5195 };
5196 struct rpc_message msg = {
5197 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5198 .rpc_argp = &args,
5199 .rpc_resp = &res,
5200 };
5201
5202 dprintk("NFS call secinfo %s\n", name->name);
5203 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5204 dprintk("NFS reply secinfo: %d\n", status);
5205 return status;
5206}
5207
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005208int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5209 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005210{
5211 struct nfs4_exception exception = { };
5212 int err;
5213 do {
5214 err = nfs4_handle_exception(NFS_SERVER(dir),
5215 _nfs4_proc_secinfo(dir, name, flavors),
5216 &exception);
5217 } while (exception.retry);
5218 return err;
5219}
5220
Andy Adamson557134a2009-04-01 09:21:53 -04005221#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005222/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005223 * Check the exchange flags returned by the server for invalid flags, having
5224 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5225 * DS flags set.
5226 */
5227static int nfs4_check_cl_exchange_flags(u32 flags)
5228{
5229 if (flags & ~EXCHGID4_FLAG_MASK_R)
5230 goto out_inval;
5231 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5232 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5233 goto out_inval;
5234 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5235 goto out_inval;
5236 return NFS_OK;
5237out_inval:
5238 return -NFS4ERR_INVAL;
5239}
5240
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005241static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005242nfs41_same_server_scope(struct nfs41_server_scope *a,
5243 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005244{
5245 if (a->server_scope_sz == b->server_scope_sz &&
5246 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5247 return true;
5248
5249 return false;
5250}
5251
Andy Adamson357f54d2010-12-14 10:11:57 -05005252/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005253 * nfs4_proc_bind_conn_to_session()
5254 *
5255 * The 4.1 client currently uses the same TCP connection for the
5256 * fore and backchannel.
5257 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005258int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005259{
5260 int status;
5261 struct nfs41_bind_conn_to_session_res res;
5262 struct rpc_message msg = {
5263 .rpc_proc =
5264 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5265 .rpc_argp = clp,
5266 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005267 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005268 };
5269
5270 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005271
5272 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5273 if (unlikely(res.session == NULL)) {
5274 status = -ENOMEM;
5275 goto out;
5276 }
5277
5278 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5279 if (status == 0) {
5280 if (memcmp(res.session->sess_id.data,
5281 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5282 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5283 status = -EIO;
5284 goto out_session;
5285 }
5286 if (res.dir != NFS4_CDFS4_BOTH) {
5287 dprintk("NFS: %s: Unexpected direction from server\n",
5288 __func__);
5289 status = -EIO;
5290 goto out_session;
5291 }
5292 if (res.use_conn_in_rdma_mode) {
5293 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5294 __func__);
5295 status = -EIO;
5296 goto out_session;
5297 }
5298 }
5299out_session:
5300 kfree(res.session);
5301out:
5302 dprintk("<-- %s status= %d\n", __func__, status);
5303 return status;
5304}
5305
5306/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005307 * nfs4_proc_exchange_id()
5308 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005309 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5310 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04005311 * Since the clientid has expired, all compounds using sessions
5312 * associated with the stale clientid will be returning
5313 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5314 * be in some phase of session reset.
5315 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005316int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005317{
5318 nfs4_verifier verifier;
5319 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005320 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005321 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005322 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005323 };
5324 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005325 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005326 };
5327 int status;
5328 struct rpc_message msg = {
5329 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5330 .rpc_argp = &args,
5331 .rpc_resp = &res,
5332 .rpc_cred = cred,
5333 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005334
Chuck Leverf0920752012-05-21 22:45:41 -04005335 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04005336 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5337 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005338 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5339 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5340 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005341
Chuck Leveracdeb692012-05-21 22:46:16 -04005342 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005343 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005344 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005345 status = -ENOMEM;
5346 goto out;
5347 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005348
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005349 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005350 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005351 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005352 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005353 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005354 }
5355
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005356 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005357 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005358 status = -ENOMEM;
5359 goto out_server_scope;
5360 }
5361
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005362 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005363 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005364 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005365
Chuck Lever177313f2012-05-21 22:44:58 -04005366 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005367 clp->cl_clientid = res.clientid;
5368 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5369 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5370 clp->cl_seqid = res.seqid;
5371
Chuck Leveracdeb692012-05-21 22:46:16 -04005372 kfree(clp->cl_serverowner);
5373 clp->cl_serverowner = res.server_owner;
5374 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005375
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005376 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005377 kfree(clp->cl_implid);
5378 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005379
Chuck Lever177313f2012-05-21 22:44:58 -04005380 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005381 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005382 res.server_scope)) {
5383 dprintk("%s: server_scope mismatch detected\n",
5384 __func__);
5385 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005386 kfree(clp->cl_serverscope);
5387 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005388 }
5389
Chuck Lever177313f2012-05-21 22:44:58 -04005390 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005391 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005392 goto out;
5393 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005394 } else
5395 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005396
Chuck Leveracdeb692012-05-21 22:46:16 -04005397out_server_owner:
5398 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005399out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005400 kfree(res.server_scope);
5401out:
Chuck Lever177313f2012-05-21 22:44:58 -04005402 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005403 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005404 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005405 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04005406 clp->cl_implid->date.seconds,
5407 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005408 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005409 return status;
5410}
5411
Trond Myklebust66245532012-05-25 17:18:09 -04005412static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5413 struct rpc_cred *cred)
5414{
5415 struct rpc_message msg = {
5416 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5417 .rpc_argp = clp,
5418 .rpc_cred = cred,
5419 };
5420 int status;
5421
5422 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5423 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04005424 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04005425 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5426 return status;
5427}
5428
5429static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5430 struct rpc_cred *cred)
5431{
5432 unsigned int loop;
5433 int ret;
5434
5435 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5436 ret = _nfs4_proc_destroy_clientid(clp, cred);
5437 switch (ret) {
5438 case -NFS4ERR_DELAY:
5439 case -NFS4ERR_CLIENTID_BUSY:
5440 ssleep(1);
5441 break;
5442 default:
5443 return ret;
5444 }
5445 }
5446 return 0;
5447}
5448
5449int nfs4_destroy_clientid(struct nfs_client *clp)
5450{
5451 struct rpc_cred *cred;
5452 int ret = 0;
5453
5454 if (clp->cl_mvops->minor_version < 1)
5455 goto out;
5456 if (clp->cl_exchange_flags == 0)
5457 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04005458 if (clp->cl_preserve_clid)
5459 goto out;
Trond Myklebust66245532012-05-25 17:18:09 -04005460 cred = nfs4_get_exchange_id_cred(clp);
5461 ret = nfs4_proc_destroy_clientid(clp, cred);
5462 if (cred)
5463 put_rpccred(cred);
5464 switch (ret) {
5465 case 0:
5466 case -NFS4ERR_STALE_CLIENTID:
5467 clp->cl_exchange_flags = 0;
5468 }
5469out:
5470 return ret;
5471}
5472
Andy Adamson2050f0c2009-04-01 09:22:30 -04005473struct nfs4_get_lease_time_data {
5474 struct nfs4_get_lease_time_args *args;
5475 struct nfs4_get_lease_time_res *res;
5476 struct nfs_client *clp;
5477};
5478
5479static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5480 void *calldata)
5481{
Andy Adamson2050f0c2009-04-01 09:22:30 -04005482 struct nfs4_get_lease_time_data *data =
5483 (struct nfs4_get_lease_time_data *)calldata;
5484
5485 dprintk("--> %s\n", __func__);
5486 /* just setup sequence, do not trigger session recovery
5487 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005488 nfs41_setup_sequence(data->clp->cl_session,
5489 &data->args->la_seq_args,
5490 &data->res->lr_seq_res,
5491 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005492 dprintk("<-- %s\n", __func__);
5493}
5494
5495/*
5496 * Called from nfs4_state_manager thread for session setup, so don't recover
5497 * from sequence operation or clientid errors.
5498 */
5499static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5500{
5501 struct nfs4_get_lease_time_data *data =
5502 (struct nfs4_get_lease_time_data *)calldata;
5503
5504 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005505 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5506 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005507 switch (task->tk_status) {
5508 case -NFS4ERR_DELAY:
5509 case -NFS4ERR_GRACE:
5510 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5511 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5512 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005513 /* fall through */
5514 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005515 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005516 return;
5517 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005518 dprintk("<-- %s\n", __func__);
5519}
5520
Trond Myklebust17280172012-03-11 13:11:00 -04005521static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005522 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5523 .rpc_call_done = nfs4_get_lease_time_done,
5524};
5525
5526int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5527{
5528 struct rpc_task *task;
5529 struct nfs4_get_lease_time_args args;
5530 struct nfs4_get_lease_time_res res = {
5531 .lr_fsinfo = fsinfo,
5532 };
5533 struct nfs4_get_lease_time_data data = {
5534 .args = &args,
5535 .res = &res,
5536 .clp = clp,
5537 };
5538 struct rpc_message msg = {
5539 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5540 .rpc_argp = &args,
5541 .rpc_resp = &res,
5542 };
5543 struct rpc_task_setup task_setup = {
5544 .rpc_client = clp->cl_rpcclient,
5545 .rpc_message = &msg,
5546 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005547 .callback_data = &data,
5548 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005549 };
5550 int status;
5551
Trond Myklebust9d12b212012-01-17 22:04:25 -05005552 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005553 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005554 dprintk("--> %s\n", __func__);
5555 task = rpc_run_task(&task_setup);
5556
5557 if (IS_ERR(task))
5558 status = PTR_ERR(task);
5559 else {
5560 status = task->tk_status;
5561 rpc_put_task(task);
5562 }
5563 dprintk("<-- %s return %d\n", __func__, status);
5564
5565 return status;
5566}
5567
Andy Adamsonfc931582009-04-01 09:22:31 -04005568/*
5569 * Initialize the values to be used by the client in CREATE_SESSION
5570 * If nfs4_init_session set the fore channel request and response sizes,
5571 * use them.
5572 *
5573 * Set the back channel max_resp_sz_cached to zero to force the client to
5574 * always set csa_cachethis to FALSE because the current implementation
5575 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5576 */
5577static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5578{
5579 struct nfs4_session *session = args->client->cl_session;
Trond Myklebustae72ae672012-11-20 11:02:55 -05005580 unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
5581 mxresp_sz = session->fc_target_max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005582
5583 if (mxrqst_sz == 0)
5584 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5585 if (mxresp_sz == 0)
5586 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5587 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005588 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5589 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005590 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005591 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005592
5593 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005594 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005595 __func__,
5596 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005597 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005598
5599 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005600 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5601 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5602 args->bc_attrs.max_resp_sz_cached = 0;
5603 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5604 args->bc_attrs.max_reqs = 1;
5605
5606 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5607 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5608 __func__,
5609 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5610 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5611 args->bc_attrs.max_reqs);
5612}
5613
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005614static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005615{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005616 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5617 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5618
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005619 if (rcvd->max_resp_sz > sent->max_resp_sz)
5620 return -EINVAL;
5621 /*
5622 * Our requested max_ops is the minimum we need; we're not
5623 * prepared to break up compounds into smaller pieces than that.
5624 * So, no point even trying to continue if the server won't
5625 * cooperate:
5626 */
5627 if (rcvd->max_ops < sent->max_ops)
5628 return -EINVAL;
5629 if (rcvd->max_reqs == 0)
5630 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005631 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5632 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005633 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005634}
5635
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005636static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5637{
5638 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5639 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005640
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005641 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5642 return -EINVAL;
5643 if (rcvd->max_resp_sz < sent->max_resp_sz)
5644 return -EINVAL;
5645 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5646 return -EINVAL;
5647 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005648 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005649 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005650 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005651 return -EINVAL;
5652 return 0;
5653}
Andy Adamson8d353012009-04-01 09:22:32 -04005654
Andy Adamson8d353012009-04-01 09:22:32 -04005655static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5656 struct nfs4_session *session)
5657{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005658 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005659
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005660 ret = nfs4_verify_fore_channel_attrs(args, session);
5661 if (ret)
5662 return ret;
5663 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005664}
5665
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005666static int _nfs4_proc_create_session(struct nfs_client *clp,
5667 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005668{
5669 struct nfs4_session *session = clp->cl_session;
5670 struct nfs41_create_session_args args = {
5671 .client = clp,
5672 .cb_program = NFS4_CALLBACK,
5673 };
5674 struct nfs41_create_session_res res = {
5675 .client = clp,
5676 };
5677 struct rpc_message msg = {
5678 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5679 .rpc_argp = &args,
5680 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005681 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04005682 };
5683 int status;
5684
5685 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005686 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005687
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005688 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005689
Trond Myklebust43095d32012-11-20 11:13:12 -05005690 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04005691 /* Verify the session's negotiated channel_attrs values */
5692 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005693 /* Increment the clientid slot sequence id */
5694 clp->cl_seqid++;
5695 }
5696
5697 return status;
5698}
5699
5700/*
5701 * Issues a CREATE_SESSION operation to the server.
5702 * It is the responsibility of the caller to verify the session is
5703 * expired before calling this routine.
5704 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005705int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005706{
5707 int status;
5708 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005709 struct nfs4_session *session = clp->cl_session;
5710
5711 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5712
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005713 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04005714 if (status)
5715 goto out;
5716
Andy Adamsonaacd5532011-11-09 13:58:21 -05005717 /* Init or reset the session slot tables */
5718 status = nfs4_setup_session_slot_tables(session);
5719 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005720 if (status)
5721 goto out;
5722
5723 ptr = (unsigned *)&session->sess_id.data[0];
5724 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5725 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005726out:
5727 dprintk("<-- %s\n", __func__);
5728 return status;
5729}
5730
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005731/*
5732 * Issue the over-the-wire RPC DESTROY_SESSION.
5733 * The caller must serialize access to this routine.
5734 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005735int nfs4_proc_destroy_session(struct nfs4_session *session,
5736 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005737{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005738 struct rpc_message msg = {
5739 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5740 .rpc_argp = session,
5741 .rpc_cred = cred,
5742 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005743 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005744
5745 dprintk("--> nfs4_proc_destroy_session\n");
5746
5747 /* session is still being setup */
5748 if (session->clp->cl_cons_state != NFS_CS_READY)
5749 return status;
5750
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005751 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005752
5753 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04005754 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005755 "Session has been destroyed regardless...\n", status);
5756
5757 dprintk("<-- nfs4_proc_destroy_session\n");
5758 return status;
5759}
5760
Trond Myklebust7b38c362012-05-23 13:23:31 -04005761/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005762 * Renew the cl_session lease.
5763 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005764struct nfs4_sequence_data {
5765 struct nfs_client *clp;
5766 struct nfs4_sequence_args args;
5767 struct nfs4_sequence_res res;
5768};
5769
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005770static void nfs41_sequence_release(void *data)
5771{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005772 struct nfs4_sequence_data *calldata = data;
5773 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005774
Alexandros Batsakis71358402010-02-05 03:45:05 -08005775 if (atomic_read(&clp->cl_count) > 1)
5776 nfs4_schedule_state_renewal(clp);
5777 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005778 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005779}
5780
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005781static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5782{
5783 switch(task->tk_status) {
5784 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005785 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5786 return -EAGAIN;
5787 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005788 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005789 }
5790 return 0;
5791}
5792
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005793static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005794{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005795 struct nfs4_sequence_data *calldata = data;
5796 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005797
Trond Myklebust14516c32010-07-31 14:29:06 -04005798 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5799 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005800
5801 if (task->tk_status < 0) {
5802 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005803 if (atomic_read(&clp->cl_count) == 1)
5804 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005805
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005806 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5807 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005808 return;
5809 }
5810 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005811 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005812out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005813 dprintk("<-- %s\n", __func__);
5814}
5815
5816static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5817{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005818 struct nfs4_sequence_data *calldata = data;
5819 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005820 struct nfs4_sequence_args *args;
5821 struct nfs4_sequence_res *res;
5822
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005823 args = task->tk_msg.rpc_argp;
5824 res = task->tk_msg.rpc_resp;
5825
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005826 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005827}
5828
5829static const struct rpc_call_ops nfs41_sequence_ops = {
5830 .rpc_call_done = nfs41_sequence_call_done,
5831 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005832 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005833};
5834
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005835static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
5836 struct rpc_cred *cred,
5837 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005838{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005839 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005840 struct rpc_message msg = {
5841 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5842 .rpc_cred = cred,
5843 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005844 struct rpc_task_setup task_setup_data = {
5845 .rpc_client = clp->cl_rpcclient,
5846 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005847 .callback_ops = &nfs41_sequence_ops,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005848 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5849 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005850
Alexandros Batsakis71358402010-02-05 03:45:05 -08005851 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005852 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005853 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005854 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005855 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005856 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005857 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05005858 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005859 if (is_privileged)
5860 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005861 msg.rpc_argp = &calldata->args;
5862 msg.rpc_resp = &calldata->res;
5863 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005864 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005865
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005866 return rpc_run_task(&task_setup_data);
5867}
5868
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005869static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005870{
5871 struct rpc_task *task;
5872 int ret = 0;
5873
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005874 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5875 return 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005876 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005877 if (IS_ERR(task))
5878 ret = PTR_ERR(task);
5879 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005880 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005881 dprintk("<-- %s status=%d\n", __func__, ret);
5882 return ret;
5883}
5884
5885static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5886{
5887 struct rpc_task *task;
5888 int ret;
5889
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005890 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005891 if (IS_ERR(task)) {
5892 ret = PTR_ERR(task);
5893 goto out;
5894 }
5895 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005896 if (!ret) {
5897 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5898
5899 if (task->tk_status == 0)
5900 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005901 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005902 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005903 rpc_put_task(task);
5904out:
5905 dprintk("<-- %s status=%d\n", __func__, ret);
5906 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005907}
5908
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005909struct nfs4_reclaim_complete_data {
5910 struct nfs_client *clp;
5911 struct nfs41_reclaim_complete_args arg;
5912 struct nfs41_reclaim_complete_res res;
5913};
5914
5915static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5916{
5917 struct nfs4_reclaim_complete_data *calldata = data;
5918
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005919 nfs41_setup_sequence(calldata->clp->cl_session,
5920 &calldata->arg.seq_args,
5921 &calldata->res.seq_res,
5922 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005923}
5924
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005925static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5926{
5927 switch(task->tk_status) {
5928 case 0:
5929 case -NFS4ERR_COMPLETE_ALREADY:
5930 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5931 break;
5932 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005933 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005934 /* fall through */
5935 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005936 return -EAGAIN;
5937 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005938 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005939 }
5940 return 0;
5941}
5942
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005943static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5944{
5945 struct nfs4_reclaim_complete_data *calldata = data;
5946 struct nfs_client *clp = calldata->clp;
5947 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5948
5949 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005950 if (!nfs41_sequence_done(task, res))
5951 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005952
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005953 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5954 rpc_restart_call_prepare(task);
5955 return;
5956 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005957 dprintk("<-- %s\n", __func__);
5958}
5959
5960static void nfs4_free_reclaim_complete_data(void *data)
5961{
5962 struct nfs4_reclaim_complete_data *calldata = data;
5963
5964 kfree(calldata);
5965}
5966
5967static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5968 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5969 .rpc_call_done = nfs4_reclaim_complete_done,
5970 .rpc_release = nfs4_free_reclaim_complete_data,
5971};
5972
5973/*
5974 * Issue a global reclaim complete.
5975 */
5976static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5977{
5978 struct nfs4_reclaim_complete_data *calldata;
5979 struct rpc_task *task;
5980 struct rpc_message msg = {
5981 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5982 };
5983 struct rpc_task_setup task_setup_data = {
5984 .rpc_client = clp->cl_rpcclient,
5985 .rpc_message = &msg,
5986 .callback_ops = &nfs4_reclaim_complete_call_ops,
5987 .flags = RPC_TASK_ASYNC,
5988 };
5989 int status = -ENOMEM;
5990
5991 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005992 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005993 if (calldata == NULL)
5994 goto out;
5995 calldata->clp = clp;
5996 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005997
Trond Myklebust9d12b212012-01-17 22:04:25 -05005998 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005999 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006000 msg.rpc_argp = &calldata->arg;
6001 msg.rpc_resp = &calldata->res;
6002 task_setup_data.callback_data = calldata;
6003 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006004 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006005 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006006 goto out;
6007 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05006008 status = nfs4_wait_for_completion_rpc_task(task);
6009 if (status == 0)
6010 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006011 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006012 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006013out:
6014 dprintk("<-- %s status=%d\n", __func__, status);
6015 return status;
6016}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006017
6018static void
6019nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6020{
6021 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00006022 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006023 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006024
6025 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00006026 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6027 * right now covering the LAYOUTGET we are about to send.
6028 * However, that is not so catastrophic, and there seems
6029 * to be no way to prevent it completely.
6030 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006031 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006032 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006033 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006034 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6035 NFS_I(lgp->args.inode)->layout,
6036 lgp->args.ctx->state)) {
6037 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00006038 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006039}
6040
6041static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6042{
6043 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07006044 struct inode *inode = lgp->args.inode;
6045 struct nfs_server *server = NFS_SERVER(inode);
6046 struct pnfs_layout_hdr *lo;
6047 struct nfs4_state *state = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006048
6049 dprintk("--> %s\n", __func__);
6050
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006051 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07006052 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006053
6054 switch (task->tk_status) {
6055 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07006056 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006057 case -NFS4ERR_LAYOUTTRYLATER:
6058 case -NFS4ERR_RECALLCONFLICT:
6059 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07006060 break;
6061 case -NFS4ERR_EXPIRED:
6062 case -NFS4ERR_BAD_STATEID:
6063 spin_lock(&inode->i_lock);
6064 lo = NFS_I(inode)->layout;
6065 if (!lo || list_empty(&lo->plh_segs)) {
6066 spin_unlock(&inode->i_lock);
6067 /* If the open stateid was bad, then recover it. */
6068 state = lgp->args.ctx->state;
6069 } else {
6070 LIST_HEAD(head);
6071
6072 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6073 spin_unlock(&inode->i_lock);
6074 /* Mark the bad layout state as invalid, then
6075 * retry using the open stateid. */
6076 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006077 }
6078 }
Trond Myklebustee314c22012-10-01 17:25:48 -07006079 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6080 rpc_restart_call_prepare(task);
6081out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006082 dprintk("<-- %s\n", __func__);
6083}
6084
Idan Kedar85541162012-08-02 11:47:10 +03006085static size_t max_response_pages(struct nfs_server *server)
6086{
6087 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6088 return nfs_page_array_len(0, max_resp_sz);
6089}
6090
6091static void nfs4_free_pages(struct page **pages, size_t size)
6092{
6093 int i;
6094
6095 if (!pages)
6096 return;
6097
6098 for (i = 0; i < size; i++) {
6099 if (!pages[i])
6100 break;
6101 __free_page(pages[i]);
6102 }
6103 kfree(pages);
6104}
6105
6106static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6107{
6108 struct page **pages;
6109 int i;
6110
6111 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6112 if (!pages) {
6113 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6114 return NULL;
6115 }
6116
6117 for (i = 0; i < size; i++) {
6118 pages[i] = alloc_page(gfp_flags);
6119 if (!pages[i]) {
6120 dprintk("%s: failed to allocate page\n", __func__);
6121 nfs4_free_pages(pages, size);
6122 return NULL;
6123 }
6124 }
6125
6126 return pages;
6127}
6128
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006129static void nfs4_layoutget_release(void *calldata)
6130{
6131 struct nfs4_layoutget *lgp = calldata;
Idan Kedar85541162012-08-02 11:47:10 +03006132 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6133 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006134
6135 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006136 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006137 put_nfs_open_context(lgp->args.ctx);
6138 kfree(calldata);
6139 dprintk("<-- %s\n", __func__);
6140}
6141
6142static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6143 .rpc_call_prepare = nfs4_layoutget_prepare,
6144 .rpc_call_done = nfs4_layoutget_done,
6145 .rpc_release = nfs4_layoutget_release,
6146};
6147
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006148struct pnfs_layout_segment *
6149nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006150{
6151 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Idan Kedar85541162012-08-02 11:47:10 +03006152 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006153 struct rpc_task *task;
6154 struct rpc_message msg = {
6155 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6156 .rpc_argp = &lgp->args,
6157 .rpc_resp = &lgp->res,
6158 };
6159 struct rpc_task_setup task_setup_data = {
6160 .rpc_client = server->client,
6161 .rpc_message = &msg,
6162 .callback_ops = &nfs4_layoutget_call_ops,
6163 .callback_data = lgp,
6164 .flags = RPC_TASK_ASYNC,
6165 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006166 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006167 int status = 0;
6168
6169 dprintk("--> %s\n", __func__);
6170
Idan Kedar85541162012-08-02 11:47:10 +03006171 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6172 if (!lgp->args.layout.pages) {
6173 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006174 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006175 }
6176 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
6177
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006178 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006179 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006180 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006181 task = rpc_run_task(&task_setup_data);
6182 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006183 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006184 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006185 if (status == 0)
6186 status = task->tk_status;
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05006187 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
6188 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006189 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006190 rpc_put_task(task);
6191 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006192 if (status)
6193 return ERR_PTR(status);
6194 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006195}
6196
Benny Halevycbe82602011-05-22 19:52:37 +03006197static void
6198nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6199{
6200 struct nfs4_layoutreturn *lrp = calldata;
6201
6202 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006203 nfs41_setup_sequence(lrp->clp->cl_session,
6204 &lrp->args.seq_args,
6205 &lrp->res.seq_res,
6206 task);
Benny Halevycbe82602011-05-22 19:52:37 +03006207}
6208
6209static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6210{
6211 struct nfs4_layoutreturn *lrp = calldata;
6212 struct nfs_server *server;
6213
6214 dprintk("--> %s\n", __func__);
6215
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006216 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03006217 return;
6218
6219 server = NFS_SERVER(lrp->args.inode);
6220 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006221 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006222 return;
6223 }
Benny Halevycbe82602011-05-22 19:52:37 +03006224 dprintk("<-- %s\n", __func__);
6225}
6226
6227static void nfs4_layoutreturn_release(void *calldata)
6228{
6229 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04006230 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006231
6232 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04006233 spin_lock(&lo->plh_inode->i_lock);
6234 if (lrp->res.lrs_present)
6235 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6236 lo->plh_block_lgets--;
6237 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006238 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006239 kfree(calldata);
6240 dprintk("<-- %s\n", __func__);
6241}
6242
6243static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6244 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6245 .rpc_call_done = nfs4_layoutreturn_done,
6246 .rpc_release = nfs4_layoutreturn_release,
6247};
6248
6249int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6250{
6251 struct rpc_task *task;
6252 struct rpc_message msg = {
6253 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6254 .rpc_argp = &lrp->args,
6255 .rpc_resp = &lrp->res,
6256 };
6257 struct rpc_task_setup task_setup_data = {
6258 .rpc_client = lrp->clp->cl_rpcclient,
6259 .rpc_message = &msg,
6260 .callback_ops = &nfs4_layoutreturn_call_ops,
6261 .callback_data = lrp,
6262 };
6263 int status;
6264
6265 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006266 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006267 task = rpc_run_task(&task_setup_data);
6268 if (IS_ERR(task))
6269 return PTR_ERR(task);
6270 status = task->tk_status;
6271 dprintk("<-- %s status=%d\n", __func__, status);
6272 rpc_put_task(task);
6273 return status;
6274}
6275
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006276/*
6277 * Retrieve the list of Data Server devices from the MDS.
6278 */
6279static int _nfs4_getdevicelist(struct nfs_server *server,
6280 const struct nfs_fh *fh,
6281 struct pnfs_devicelist *devlist)
6282{
6283 struct nfs4_getdevicelist_args args = {
6284 .fh = fh,
6285 .layoutclass = server->pnfs_curr_ld->id,
6286 };
6287 struct nfs4_getdevicelist_res res = {
6288 .devlist = devlist,
6289 };
6290 struct rpc_message msg = {
6291 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6292 .rpc_argp = &args,
6293 .rpc_resp = &res,
6294 };
6295 int status;
6296
6297 dprintk("--> %s\n", __func__);
6298 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6299 &res.seq_res, 0);
6300 dprintk("<-- %s status=%d\n", __func__, status);
6301 return status;
6302}
6303
6304int nfs4_proc_getdevicelist(struct nfs_server *server,
6305 const struct nfs_fh *fh,
6306 struct pnfs_devicelist *devlist)
6307{
6308 struct nfs4_exception exception = { };
6309 int err;
6310
6311 do {
6312 err = nfs4_handle_exception(server,
6313 _nfs4_getdevicelist(server, fh, devlist),
6314 &exception);
6315 } while (exception.retry);
6316
6317 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6318 err, devlist->num_devs);
6319
6320 return err;
6321}
6322EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6323
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006324static int
6325_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6326{
6327 struct nfs4_getdeviceinfo_args args = {
6328 .pdev = pdev,
6329 };
6330 struct nfs4_getdeviceinfo_res res = {
6331 .pdev = pdev,
6332 };
6333 struct rpc_message msg = {
6334 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6335 .rpc_argp = &args,
6336 .rpc_resp = &res,
6337 };
6338 int status;
6339
6340 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006341 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006342 dprintk("<-- %s status=%d\n", __func__, status);
6343
6344 return status;
6345}
6346
6347int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6348{
6349 struct nfs4_exception exception = { };
6350 int err;
6351
6352 do {
6353 err = nfs4_handle_exception(server,
6354 _nfs4_proc_getdeviceinfo(server, pdev),
6355 &exception);
6356 } while (exception.retry);
6357 return err;
6358}
6359EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6360
Andy Adamson863a3c62011-03-23 13:27:54 +00006361static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6362{
6363 struct nfs4_layoutcommit_data *data = calldata;
6364 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006365 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00006366
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006367 nfs41_setup_sequence(session,
6368 &data->args.seq_args,
6369 &data->res.seq_res,
6370 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00006371}
6372
6373static void
6374nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6375{
6376 struct nfs4_layoutcommit_data *data = calldata;
6377 struct nfs_server *server = NFS_SERVER(data->args.inode);
6378
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006379 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00006380 return;
6381
6382 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006383 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6384 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6385 case -NFS4ERR_BADLAYOUT: /* no layout */
6386 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006387 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006388 break;
6389 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006390 nfs_post_op_update_inode_force_wcc(data->args.inode,
6391 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006392 break;
6393 default:
6394 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6395 rpc_restart_call_prepare(task);
6396 return;
6397 }
6398 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006399}
6400
6401static void nfs4_layoutcommit_release(void *calldata)
6402{
6403 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04006404 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07006405 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00006406
Andy Adamsondb29c082011-07-30 20:52:38 -04006407 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006408 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04006409 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6410 list_del_init(&lseg->pls_lc_list);
6411 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6412 &lseg->pls_flags))
Trond Myklebust9369a432012-09-18 20:57:08 -04006413 pnfs_put_lseg(lseg);
Peng Taoa9bae562011-07-30 20:52:33 -04006414 }
Peng Tao92407e72011-10-23 20:21:17 -07006415
6416 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6417 smp_mb__after_clear_bit();
6418 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6419
Andy Adamson863a3c62011-03-23 13:27:54 +00006420 put_rpccred(data->cred);
6421 kfree(data);
6422}
6423
6424static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6425 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6426 .rpc_call_done = nfs4_layoutcommit_done,
6427 .rpc_release = nfs4_layoutcommit_release,
6428};
6429
6430int
Andy Adamsonef311532011-03-12 02:58:10 -05006431nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006432{
6433 struct rpc_message msg = {
6434 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6435 .rpc_argp = &data->args,
6436 .rpc_resp = &data->res,
6437 .rpc_cred = data->cred,
6438 };
6439 struct rpc_task_setup task_setup_data = {
6440 .task = &data->task,
6441 .rpc_client = NFS_CLIENT(data->args.inode),
6442 .rpc_message = &msg,
6443 .callback_ops = &nfs4_layoutcommit_ops,
6444 .callback_data = data,
6445 .flags = RPC_TASK_ASYNC,
6446 };
6447 struct rpc_task *task;
6448 int status = 0;
6449
6450 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6451 "lbw: %llu inode %lu\n",
6452 data->task.tk_pid, sync,
6453 data->args.lastbytewritten,
6454 data->args.inode->i_ino);
6455
Trond Myklebust9d12b212012-01-17 22:04:25 -05006456 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006457 task = rpc_run_task(&task_setup_data);
6458 if (IS_ERR(task))
6459 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006460 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006461 goto out;
6462 status = nfs4_wait_for_completion_rpc_task(task);
6463 if (status != 0)
6464 goto out;
6465 status = task->tk_status;
6466out:
6467 dprintk("%s: status %d\n", __func__, status);
6468 rpc_put_task(task);
6469 return status;
6470}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006471
6472static int
6473_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6474 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6475{
6476 struct nfs41_secinfo_no_name_args args = {
6477 .style = SECINFO_STYLE_CURRENT_FH,
6478 };
6479 struct nfs4_secinfo_res res = {
6480 .flavors = flavors,
6481 };
6482 struct rpc_message msg = {
6483 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6484 .rpc_argp = &args,
6485 .rpc_resp = &res,
6486 };
6487 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6488}
6489
6490static int
6491nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6492 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6493{
6494 struct nfs4_exception exception = { };
6495 int err;
6496 do {
6497 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6498 switch (err) {
6499 case 0:
6500 case -NFS4ERR_WRONGSEC:
6501 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006502 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006503 default:
6504 err = nfs4_handle_exception(server, err, &exception);
6505 }
6506 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006507out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006508 return err;
6509}
6510
6511static int
6512nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6513 struct nfs_fsinfo *info)
6514{
6515 int err;
6516 struct page *page;
6517 rpc_authflavor_t flavor;
6518 struct nfs4_secinfo_flavors *flavors;
6519
6520 page = alloc_page(GFP_KERNEL);
6521 if (!page) {
6522 err = -ENOMEM;
6523 goto out;
6524 }
6525
6526 flavors = page_address(page);
6527 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6528
6529 /*
6530 * Fall back on "guess and check" method if
6531 * the server doesn't support SECINFO_NO_NAME
6532 */
6533 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6534 err = nfs4_find_root_sec(server, fhandle, info);
6535 goto out_freepage;
6536 }
6537 if (err)
6538 goto out_freepage;
6539
6540 flavor = nfs_find_best_sec(flavors);
6541 if (err == 0)
6542 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6543
6544out_freepage:
6545 put_page(page);
6546 if (err == -EACCES)
6547 return -EPERM;
6548out:
6549 return err;
6550}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006551
6552static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006553{
6554 int status;
6555 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006556 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006557 };
6558 struct nfs41_test_stateid_res res;
6559 struct rpc_message msg = {
6560 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6561 .rpc_argp = &args,
6562 .rpc_resp = &res,
6563 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006564
Chuck Lever38527b12012-07-11 16:30:23 -04006565 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006566 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006567 nfs4_set_sequence_privileged(&args.seq_args);
6568 status = nfs4_call_sync_sequence(server->client, server, &msg,
6569 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04006570 if (status != NFS_OK) {
6571 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04006572 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04006573 }
6574 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04006575 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006576}
6577
Chuck Lever38527b12012-07-11 16:30:23 -04006578/**
6579 * nfs41_test_stateid - perform a TEST_STATEID operation
6580 *
6581 * @server: server / transport on which to perform the operation
6582 * @stateid: state ID to test
6583 *
6584 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6585 * Otherwise a negative NFS4ERR value is returned if the operation
6586 * failed or the state ID is not currently valid.
6587 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006588static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006589{
6590 struct nfs4_exception exception = { };
6591 int err;
6592 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006593 err = _nfs41_test_stateid(server, stateid);
6594 if (err != -NFS4ERR_DELAY)
6595 break;
6596 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04006597 } while (exception.retry);
6598 return err;
6599}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006600
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006601static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006602{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006603 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006604 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006605 };
6606 struct nfs41_free_stateid_res res;
6607 struct rpc_message msg = {
6608 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6609 .rpc_argp = &args,
6610 .rpc_resp = &res,
6611 };
Chuck Lever38527b12012-07-11 16:30:23 -04006612 int status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006613
Chuck Lever38527b12012-07-11 16:30:23 -04006614 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006615 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006616 nfs4_set_sequence_privileged(&args.seq_args);
Chuck Lever38527b12012-07-11 16:30:23 -04006617 status = nfs4_call_sync_sequence(server->client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006618 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04006619 dprintk("NFS reply free_stateid: %d\n", status);
6620 return status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006621}
6622
Chuck Lever38527b12012-07-11 16:30:23 -04006623/**
6624 * nfs41_free_stateid - perform a FREE_STATEID operation
6625 *
6626 * @server: server / transport on which to perform the operation
6627 * @stateid: state ID to release
6628 *
6629 * Returns NFS_OK if the server freed "stateid". Otherwise a
6630 * negative NFS4ERR value is returned.
6631 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006632static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006633{
6634 struct nfs4_exception exception = { };
6635 int err;
6636 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006637 err = _nfs4_free_stateid(server, stateid);
6638 if (err != -NFS4ERR_DELAY)
6639 break;
6640 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006641 } while (exception.retry);
6642 return err;
6643}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006644
6645static bool nfs41_match_stateid(const nfs4_stateid *s1,
6646 const nfs4_stateid *s2)
6647{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006648 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006649 return false;
6650
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006651 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006652 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006653 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006654 return true;
6655
6656 return false;
6657}
6658
Andy Adamson557134a2009-04-01 09:21:53 -04006659#endif /* CONFIG_NFS_V4_1 */
6660
Trond Myklebust36281ca2012-03-04 18:13:56 -05006661static bool nfs4_match_stateid(const nfs4_stateid *s1,
6662 const nfs4_stateid *s2)
6663{
Trond Myklebustf597c532012-03-04 18:13:56 -05006664 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006665}
6666
6667
Trond Myklebust17280172012-03-11 13:11:00 -04006668static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006669 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006670 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006671 .recover_open = nfs4_open_reclaim,
6672 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006673 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006674 .get_clid_cred = nfs4_get_setclientid_cred,
Chuck Lever05f4c352012-09-14 17:24:32 -04006675 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006676};
6677
Andy Adamson591d71c2009-04-01 09:22:47 -04006678#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006679static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006680 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6681 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6682 .recover_open = nfs4_open_reclaim,
6683 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006684 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006685 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006686 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04006687 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04006688};
6689#endif /* CONFIG_NFS_V4_1 */
6690
Trond Myklebust17280172012-03-11 13:11:00 -04006691static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006692 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006693 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006694 .recover_open = nfs4_open_expired,
6695 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006696 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006697 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006698};
6699
Andy Adamson591d71c2009-04-01 09:22:47 -04006700#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006701static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006702 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6703 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006704 .recover_open = nfs41_open_expired,
6705 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006706 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006707 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006708};
6709#endif /* CONFIG_NFS_V4_1 */
6710
Trond Myklebust17280172012-03-11 13:11:00 -04006711static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006712 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006713 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006714 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006715};
6716
6717#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006718static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006719 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006720 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006721 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006722};
6723#endif
6724
Trond Myklebust97dc1352010-06-16 09:52:26 -04006725static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6726 .minor_version = 0,
6727 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006728 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006729 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006730 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6731 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6732 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006733};
6734
6735#if defined(CONFIG_NFS_V4_1)
6736static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6737 .minor_version = 1,
Bryan Schumaker104287c2012-11-12 14:13:13 -05006738 .call_sync = nfs4_call_sync_sequence,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006739 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006740 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006741 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6742 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6743 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006744};
6745#endif
6746
Trond Myklebust97dc1352010-06-16 09:52:26 -04006747const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6748 [0] = &nfs_v4_0_minor_ops,
6749#if defined(CONFIG_NFS_V4_1)
6750 [1] = &nfs_v4_1_minor_ops,
6751#endif
6752};
6753
Bryan Schumaker73a79702012-07-16 16:39:12 -04006754const struct inode_operations nfs4_dir_inode_operations = {
6755 .create = nfs_create,
6756 .lookup = nfs_lookup,
6757 .atomic_open = nfs_atomic_open,
6758 .link = nfs_link,
6759 .unlink = nfs_unlink,
6760 .symlink = nfs_symlink,
6761 .mkdir = nfs_mkdir,
6762 .rmdir = nfs_rmdir,
6763 .mknod = nfs_mknod,
6764 .rename = nfs_rename,
6765 .permission = nfs_permission,
6766 .getattr = nfs_getattr,
6767 .setattr = nfs_setattr,
6768 .getxattr = generic_getxattr,
6769 .setxattr = generic_setxattr,
6770 .listxattr = generic_listxattr,
6771 .removexattr = generic_removexattr,
6772};
6773
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006774static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006775 .permission = nfs_permission,
6776 .getattr = nfs_getattr,
6777 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006778 .getxattr = generic_getxattr,
6779 .setxattr = generic_setxattr,
6780 .listxattr = generic_listxattr,
6781 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006782};
6783
David Howells509de812006-08-22 20:06:11 -04006784const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006785 .version = 4, /* protocol version */
6786 .dentry_ops = &nfs4_dentry_operations,
6787 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006788 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006789 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006790 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04006791 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04006792 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006793 .getattr = nfs4_proc_getattr,
6794 .setattr = nfs4_proc_setattr,
6795 .lookup = nfs4_proc_lookup,
6796 .access = nfs4_proc_access,
6797 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006798 .create = nfs4_proc_create,
6799 .remove = nfs4_proc_remove,
6800 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04006801 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006802 .unlink_done = nfs4_proc_unlink_done,
6803 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006804 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04006805 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04006806 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006807 .link = nfs4_proc_link,
6808 .symlink = nfs4_proc_symlink,
6809 .mkdir = nfs4_proc_mkdir,
6810 .rmdir = nfs4_proc_remove,
6811 .readdir = nfs4_proc_readdir,
6812 .mknod = nfs4_proc_mknod,
6813 .statfs = nfs4_proc_statfs,
6814 .fsinfo = nfs4_proc_fsinfo,
6815 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006816 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006817 .decode_dirent = nfs4_decode_dirent,
6818 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04006819 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04006820 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05006821 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006822 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04006823 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04006824 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006825 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006826 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04006827 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006828 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006829 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006830 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006831 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006832 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04006833 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04006834 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04006835 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00006836 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04006837 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04006838 .create_server = nfs4_create_server,
6839 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006840};
6841
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006842static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6843 .prefix = XATTR_NAME_NFSV4_ACL,
6844 .list = nfs4_xattr_list_nfs4_acl,
6845 .get = nfs4_xattr_get_nfs4_acl,
6846 .set = nfs4_xattr_set_nfs4_acl,
6847};
6848
6849const struct xattr_handler *nfs4_xattr_handlers[] = {
6850 &nfs4_xattr_nfs4_acl_handler,
6851 NULL
6852};
6853
Linus Torvalds1da177e2005-04-16 15:20:36 -07006854/*
6855 * Local variables:
6856 * c-basic-offset: 8
6857 * End:
6858 */