blob: d51227371c6768f2ca903276c33e21edb64f9b70 [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)
1469 return;
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 Myklebustb75ad4c2012-11-29 17:27:47 -05001504 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001505}
1506
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001507static void nfs4_open_done(struct rpc_task *task, void *calldata)
1508{
1509 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001511 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001512
Trond Myklebust14516c32010-07-31 14:29:06 -04001513 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1514 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001515
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001516 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001517 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1518 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001519 case S_IFREG:
1520 break;
1521 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001522 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001523 break;
1524 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001525 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001526 break;
1527 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001528 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001529 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001530 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001531 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001532 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1533 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001534 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001535 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001536}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001537
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001538static void nfs4_open_release(void *calldata)
1539{
1540 struct nfs4_opendata *data = calldata;
1541 struct nfs4_state *state = NULL;
1542
1543 /* If this request hasn't been cancelled, do nothing */
1544 if (data->cancelled == 0)
1545 goto out_free;
1546 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001547 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001548 goto out_free;
1549 /* In case we need an open_confirm, no cleanup! */
1550 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1551 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001552 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001553 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001554 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001555out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001556 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001557}
1558
1559static const struct rpc_call_ops nfs4_open_ops = {
1560 .rpc_call_prepare = nfs4_open_prepare,
1561 .rpc_call_done = nfs4_open_done,
1562 .rpc_release = nfs4_open_release,
1563};
1564
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001565static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001566{
1567 struct inode *dir = data->dir->d_inode;
1568 struct nfs_server *server = NFS_SERVER(dir);
1569 struct nfs_openargs *o_arg = &data->o_arg;
1570 struct nfs_openres *o_res = &data->o_res;
1571 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001572 struct rpc_message msg = {
1573 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1574 .rpc_argp = o_arg,
1575 .rpc_resp = o_res,
1576 .rpc_cred = data->owner->so_cred,
1577 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001578 struct rpc_task_setup task_setup_data = {
1579 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001580 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001581 .callback_ops = &nfs4_open_ops,
1582 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001583 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001584 .flags = RPC_TASK_ASYNC,
1585 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001586 int status;
1587
Trond Myklebust9d12b212012-01-17 22:04:25 -05001588 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001589 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001590 data->rpc_done = 0;
1591 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001592 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001593 if (isrecover)
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001594 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04001595 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001596 if (IS_ERR(task))
1597 return PTR_ERR(task);
1598 status = nfs4_wait_for_completion_rpc_task(task);
1599 if (status != 0) {
1600 data->cancelled = 1;
1601 smp_wmb();
1602 } else
1603 status = data->rpc_status;
1604 rpc_put_task(task);
1605
1606 return status;
1607}
1608
1609static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1610{
1611 struct inode *dir = data->dir->d_inode;
1612 struct nfs_openres *o_res = &data->o_res;
1613 int status;
1614
1615 status = nfs4_run_open_task(data, 1);
1616 if (status != 0 || !data->rpc_done)
1617 return status;
1618
Trond Myklebust6926afd2012-01-07 13:22:46 -05001619 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1620
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001621 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1622 status = _nfs4_proc_open_confirm(data);
1623 if (status != 0)
1624 return status;
1625 }
1626
1627 return status;
1628}
1629
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001630static int nfs4_opendata_access(struct rpc_cred *cred,
1631 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001632 struct nfs4_state *state, fmode_t fmode,
1633 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001634{
1635 struct nfs_access_entry cache;
1636 u32 mask;
1637
1638 /* access call failed or for some reason the server doesn't
1639 * support any access modes -- defer access call until later */
1640 if (opendata->o_res.access_supported == 0)
1641 return 0;
1642
1643 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001644 /* don't check MAY_WRITE - a newly created file may not have
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001645 * write mode bits, but POSIX allows the creating process to write.
1646 * use openflags to check for exec, because fmode won't
1647 * always have FMODE_EXEC set when file open for exec. */
1648 if (openflags & __FMODE_EXEC) {
1649 /* ONLY check for exec rights */
1650 mask = MAY_EXEC;
1651 } else if (fmode & FMODE_READ)
1652 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001653
1654 cache.cred = cred;
1655 cache.jiffies = jiffies;
1656 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1657 nfs_access_add_cache(state->inode, &cache);
1658
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001659 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001660 return 0;
1661
1662 /* even though OPEN succeeded, access is denied. Close the file */
1663 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001664 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001665}
1666
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001667/*
1668 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1669 */
1670static int _nfs4_proc_open(struct nfs4_opendata *data)
1671{
1672 struct inode *dir = data->dir->d_inode;
1673 struct nfs_server *server = NFS_SERVER(dir);
1674 struct nfs_openargs *o_arg = &data->o_arg;
1675 struct nfs_openres *o_res = &data->o_res;
1676 int status;
1677
1678 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001679 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001680 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001681 if (status != 0) {
1682 if (status == -NFS4ERR_BADNAME &&
1683 !(o_arg->open_flags & O_CREAT))
1684 return -ENOENT;
1685 return status;
1686 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001687
Trond Myklebust6926afd2012-01-07 13:22:46 -05001688 nfs_fattr_map_and_free_names(server, &data->f_attr);
1689
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001690 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001691 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001692 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1693 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001695 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001697 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 }
1699 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001700 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001701 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702}
1703
Andy Adamsond83217c2011-03-01 01:34:17 +00001704static int nfs4_recover_expired_lease(struct nfs_server *server)
1705{
1706 return nfs4_client_recover_expired_lease(server->nfs_client);
1707}
1708
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709/*
1710 * OPEN_EXPIRED:
1711 * reclaim state on the server after a network partition.
1712 * Assumes caller holds the appropriate lock
1713 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001714static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001716 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001717 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001719 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1720 if (IS_ERR(opendata))
1721 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001722 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001723 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001724 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001725 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001726 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727}
1728
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001729static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001730{
Trond Myklebust539cd032007-06-05 11:46:42 -04001731 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001732 struct nfs4_exception exception = { };
1733 int err;
1734
1735 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001736 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001737 switch (err) {
1738 default:
1739 goto out;
1740 case -NFS4ERR_GRACE:
1741 case -NFS4ERR_DELAY:
1742 nfs4_handle_exception(server, err, &exception);
1743 err = 0;
1744 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001745 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001746out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001747 return err;
1748}
1749
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1751{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001753 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
Trond Myklebust864472e2006-01-03 09:55:15 +01001755 ctx = nfs4_state_find_open_context(state);
1756 if (IS_ERR(ctx))
1757 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001758 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001759 put_nfs_open_context(ctx);
1760 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761}
1762
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001763#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001764static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001765{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001766 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001767 nfs4_stateid *stateid = &state->stateid;
1768 int status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001769
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001770 /* If a state reset has been done, test_stateid is unneeded */
1771 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1772 return;
1773
1774 status = nfs41_test_stateid(server, stateid);
1775 if (status != NFS_OK) {
1776 /* Free the stateid unless the server explicitly
1777 * informs us the stateid is unrecognized. */
1778 if (status != -NFS4ERR_BAD_STATEID)
1779 nfs41_free_stateid(server, stateid);
Bryan Schumaker69388672012-09-26 15:25:52 -04001780 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001781
Bryan Schumaker69388672012-09-26 15:25:52 -04001782 write_seqlock(&state->seqlock);
1783 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1784 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001785 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1786 }
1787}
1788
1789/**
1790 * nfs41_check_open_stateid - possibly free an open stateid
1791 *
1792 * @state: NFSv4 state for an inode
1793 *
1794 * Returns NFS_OK if recovery for this stateid is now finished.
1795 * Otherwise a negative NFS4ERR value is returned.
1796 */
1797static int nfs41_check_open_stateid(struct nfs4_state *state)
1798{
1799 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04001800 nfs4_stateid *stateid = &state->open_stateid;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001801 int status;
1802
1803 /* If a state reset has been done, test_stateid is unneeded */
1804 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1805 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1806 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1807 return -NFS4ERR_BAD_STATEID;
1808
1809 status = nfs41_test_stateid(server, stateid);
1810 if (status != NFS_OK) {
1811 /* Free the stateid unless the server explicitly
1812 * informs us the stateid is unrecognized. */
1813 if (status != -NFS4ERR_BAD_STATEID)
1814 nfs41_free_stateid(server, stateid);
1815
1816 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1817 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1818 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001819 }
1820 return status;
1821}
1822
1823static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1824{
Chuck Levereb64cf92012-07-11 16:30:05 -04001825 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001826
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001827 nfs41_clear_delegation_stateid(state);
1828 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04001829 if (status != NFS_OK)
1830 status = nfs4_open_expired(sp, state);
1831 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001832}
1833#endif
1834
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001836 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1837 * fields corresponding to attributes that were used to store the verifier.
1838 * Make sure we clobber those fields in the later setattr call
1839 */
1840static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1841{
1842 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1843 !(sattr->ia_valid & ATTR_ATIME_SET))
1844 sattr->ia_valid |= ATTR_ATIME;
1845
1846 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1847 !(sattr->ia_valid & ATTR_MTIME_SET))
1848 sattr->ia_valid |= ATTR_MTIME;
1849}
1850
1851/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001852 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 */
Andy Adamson82be4172012-05-23 05:02:35 -04001854static int _nfs4_do_open(struct inode *dir,
1855 struct dentry *dentry,
1856 fmode_t fmode,
1857 int flags,
1858 struct iattr *sattr,
1859 struct rpc_cred *cred,
1860 struct nfs4_state **res,
1861 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862{
1863 struct nfs4_state_owner *sp;
1864 struct nfs4_state *state = NULL;
1865 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001866 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001867 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
1869 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001871 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1872 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1874 goto out_err;
1875 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001876 status = nfs4_recover_expired_lease(server);
1877 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001878 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001879 if (dentry->d_inode != NULL)
1880 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001881 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001882 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001883 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001884 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
Andy Adamson82be4172012-05-23 05:02:35 -04001886 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
1887 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
1888 if (!opendata->f_attr.mdsthreshold)
1889 goto err_opendata_put;
Trond Myklebust1549210f2012-06-05 09:16:47 -04001890 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04001891 }
Al Viro82a2c1b2011-06-22 18:30:55 -04001892 if (dentry->d_inode != NULL)
1893 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001894
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001895 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001897 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001899 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001900 status = PTR_ERR(state);
1901 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001902 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001903 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001904 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001905
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001906 status = nfs4_opendata_access(cred, opendata, state, fmode, flags);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001907 if (status != 0)
1908 goto err_opendata_put;
1909
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001910 if (opendata->o_arg.open_flags & O_EXCL) {
1911 nfs4_exclusive_attrset(opendata, sattr);
1912
1913 nfs_fattr_init(opendata->o_res.f_attr);
1914 status = nfs4_do_setattr(state->inode, cred,
1915 opendata->o_res.f_attr, sattr,
1916 state);
1917 if (status == 0)
1918 nfs_setattr_update_inode(state->inode, sattr);
1919 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1920 }
Andy Adamson82be4172012-05-23 05:02:35 -04001921
1922 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
1923 *ctx_th = opendata->f_attr.mdsthreshold;
1924 else
1925 kfree(opendata->f_attr.mdsthreshold);
1926 opendata->f_attr.mdsthreshold = NULL;
1927
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001928 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 *res = state;
1931 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001932err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04001933 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001934 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001935err_put_state_owner:
1936 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 *res = NULL;
1939 return status;
1940}
1941
1942
Andy Adamson82be4172012-05-23 05:02:35 -04001943static struct nfs4_state *nfs4_do_open(struct inode *dir,
1944 struct dentry *dentry,
1945 fmode_t fmode,
1946 int flags,
1947 struct iattr *sattr,
1948 struct rpc_cred *cred,
1949 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950{
1951 struct nfs4_exception exception = { };
1952 struct nfs4_state *res;
1953 int status;
1954
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001955 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 do {
Andy Adamson82be4172012-05-23 05:02:35 -04001957 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
1958 &res, ctx_th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 if (status == 0)
1960 break;
1961 /* NOTE: BAD_SEQID means the server and client disagree about the
1962 * book-keeping w.r.t. state-changing operations
1963 * (OPEN/CLOSE/LOCK/LOCKU...)
1964 * It is actually a sign of a bug on the client or on the server.
1965 *
1966 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001967 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 * have unhashed the old state_owner for us, and that we can
1969 * therefore safely retry using a new one. We should still warn
1970 * the user though...
1971 */
1972 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04001973 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001974 " returned a bad sequence-id error!\n",
1975 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 exception.retry = 1;
1977 continue;
1978 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001979 /*
1980 * BAD_STATEID on OPEN means that the server cancelled our
1981 * state before it received the OPEN_CONFIRM.
1982 * Recover by retrying the request as per the discussion
1983 * on Page 181 of RFC3530.
1984 */
1985 if (status == -NFS4ERR_BAD_STATEID) {
1986 exception.retry = 1;
1987 continue;
1988 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001989 if (status == -EAGAIN) {
1990 /* We must have found a delegation */
1991 exception.retry = 1;
1992 continue;
1993 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1995 status, &exception));
1996 } while (exception.retry);
1997 return res;
1998}
1999
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002000static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2001 struct nfs_fattr *fattr, struct iattr *sattr,
2002 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002004 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002006 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 .iap = sattr,
2008 .server = server,
2009 .bitmask = server->attr_bitmask,
2010 };
2011 struct nfs_setattrres res = {
2012 .fattr = fattr,
2013 .server = server,
2014 };
2015 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002016 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2017 .rpc_argp = &arg,
2018 .rpc_resp = &res,
2019 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002021 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04002022 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023
Trond Myklebust0e574af2005-10-27 22:12:38 -04002024 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
Trond Myklebust4fc87962012-03-08 17:42:01 -05002026 if (state != NULL) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002027 struct nfs_lockowner lockowner = {
2028 .l_owner = current->files,
2029 .l_pid = current->tgid,
2030 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002031 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002032 &lockowner);
Trond Myklebust4fc87962012-03-08 17:42:01 -05002033 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2034 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002035 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002036 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002037 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038
Bryan Schumaker7c513052011-03-24 17:12:24 +00002039 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002040 if (status == 0 && state != NULL)
2041 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002042 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043}
2044
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002045static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2046 struct nfs_fattr *fattr, struct iattr *sattr,
2047 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002049 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002050 struct nfs4_exception exception = {
2051 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002052 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002053 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 int err;
2055 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04002056 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2057 switch (err) {
2058 case -NFS4ERR_OPENMODE:
2059 if (state && !(state->state & FMODE_WRITE)) {
2060 err = -EBADF;
2061 if (sattr->ia_valid & ATTR_OPEN)
2062 err = -EACCES;
2063 goto out;
2064 }
2065 }
2066 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002068out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 return err;
2070}
2071
2072struct nfs4_closedata {
2073 struct inode *inode;
2074 struct nfs4_state *state;
2075 struct nfs_closeargs arg;
2076 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002077 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002078 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002079 bool roc;
2080 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081};
2082
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002083static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002084{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002085 struct nfs4_closedata *calldata = data;
2086 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002087 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002088
Fred Isamanf7e89172011-01-06 11:36:32 +00002089 if (calldata->roc)
2090 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002091 nfs4_put_open_state(calldata->state);
2092 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002093 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002094 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002095 kfree(calldata);
2096}
2097
Trond Myklebust88069f72009-12-08 08:33:16 -05002098static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2099 fmode_t fmode)
2100{
2101 spin_lock(&state->owner->so_lock);
2102 if (!(fmode & FMODE_READ))
2103 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2104 if (!(fmode & FMODE_WRITE))
2105 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2106 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2107 spin_unlock(&state->owner->so_lock);
2108}
2109
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002110static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002112 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 struct nfs_server *server = NFS_SERVER(calldata->inode);
2115
Chuck Levera3ca5652012-03-01 17:00:40 -05002116 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002117 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2118 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 /* hmm. we are done with the inode, and in the process of freeing
2120 * the state_owner. we keep this around to process errors
2121 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 switch (task->tk_status) {
2123 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002124 if (calldata->roc)
2125 pnfs_roc_set_barrier(state->inode,
2126 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002127 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002128 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002129 nfs4_close_clear_stateid_flags(state,
2130 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 break;
2132 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002133 case -NFS4ERR_OLD_STATEID:
2134 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002136 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002137 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002139 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2140 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002142 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002143 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002144 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145}
2146
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002147static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002149 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002150 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002151 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002152 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002153
Chuck Levera3ca5652012-03-01 17:00:40 -05002154 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002155 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07002156 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04002157
Trond Myklebust88069f72009-12-08 08:33:16 -05002158 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2159 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002160 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002161 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002162 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002163 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002164 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2165 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2166 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002167 }
2168 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002169 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2170 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2171 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002172 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002173 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002174 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002175
2176 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002177 /* Note: exit _without_ calling nfs4_close_done */
2178 task->tk_action = NULL;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002179 nfs4_sequence_done(task, &calldata->res.seq_res);
Chuck Levera3ca5652012-03-01 17:00:40 -05002180 goto out;
Trond Myklebust95121352005-10-18 14:20:12 -07002181 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002182
Fred Isamanf7e89172011-01-06 11:36:32 +00002183 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002184 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002185 if (calldata->roc &&
Trond Myklebust7fdab062012-09-20 20:15:57 -04002186 pnfs_roc_drain(inode, &calldata->roc_barrier, task))
Chuck Levera3ca5652012-03-01 17:00:40 -05002187 goto out;
Fred Isamanf7e89172011-01-06 11:36:32 +00002188 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002189
Trond Myklebust516a6af2005-10-27 22:12:41 -04002190 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002191 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002192 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002193 &calldata->arg.seq_args,
2194 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002195 task) != 0)
2196 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002197out:
2198 dprintk("%s: done!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199}
2200
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002201static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002202 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002203 .rpc_call_done = nfs4_close_done,
2204 .rpc_release = nfs4_free_closedata,
2205};
2206
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207/*
2208 * It is possible for data to be read/written from a mem-mapped file
2209 * after the sys_close call (which hits the vfs layer as a flush).
2210 * This means that we can't safely call nfsv4 close on a file until
2211 * the inode is cleared. This in turn means that we are not good
2212 * NFSv4 citizens - we do not indicate to the server to update the file's
2213 * share state even when we are done with one of the three share
2214 * stateid's in the inode.
2215 *
2216 * NOTE: Caller must be holding the sp->so_owner semaphore!
2217 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002218int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002220 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002222 struct nfs4_state_owner *sp = state->owner;
2223 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002224 struct rpc_message msg = {
2225 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2226 .rpc_cred = state->owner->so_cred,
2227 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002228 struct rpc_task_setup task_setup_data = {
2229 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002230 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002231 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002232 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002233 .flags = RPC_TASK_ASYNC,
2234 };
Trond Myklebust95121352005-10-18 14:20:12 -07002235 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002237 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002239 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002240 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002241 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002243 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002244 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002246 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002247 if (calldata->arg.seqid == NULL)
2248 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002249 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002250 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002251 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002252 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002253 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002254 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002255 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002256
Trond Myklebust1174dd12010-12-21 10:52:24 -05002257 msg.rpc_argp = &calldata->arg;
2258 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002259 task_setup_data.callback_data = calldata;
2260 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002261 if (IS_ERR(task))
2262 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002263 status = 0;
2264 if (wait)
2265 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002266 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002267 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002268out_free_calldata:
2269 kfree(calldata);
2270out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002271 nfs4_put_open_state(state);
2272 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002273 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274}
2275
Trond Myklebust2b484292010-09-17 10:56:51 -04002276static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002277nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 struct nfs4_state *state;
2280
Trond Myklebust565277f2007-10-15 18:17:53 -04002281 /* Protect against concurrent sillydeletes */
Andy Adamson82be4172012-05-23 05:02:35 -04002282 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2283 ctx->cred, &ctx->mdsthreshold);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002284 if (IS_ERR(state))
2285 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002286 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002287 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288}
2289
Trond Myklebust1185a552009-12-03 15:54:02 -05002290static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002291{
2292 if (ctx->state == NULL)
2293 return;
2294 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002295 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002296 else
Al Viro643168c2011-06-22 18:20:23 -04002297 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002298}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299
2300static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2301{
Benny Halevy43652ad2009-04-01 09:21:54 -04002302 struct nfs4_server_caps_arg args = {
2303 .fhandle = fhandle,
2304 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 struct nfs4_server_caps_res res = {};
2306 struct rpc_message msg = {
2307 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002308 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 .rpc_resp = &res,
2310 };
2311 int status;
2312
Bryan Schumaker7c513052011-03-24 17:12:24 +00002313 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 if (status == 0) {
2315 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002316 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2317 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2318 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2319 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2320 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2322 server->caps |= NFS_CAP_ACLS;
2323 if (res.has_links != 0)
2324 server->caps |= NFS_CAP_HARDLINKS;
2325 if (res.has_symlinks != 0)
2326 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002327 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2328 server->caps |= NFS_CAP_FILEID;
2329 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2330 server->caps |= NFS_CAP_MODE;
2331 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2332 server->caps |= NFS_CAP_NLINK;
2333 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2334 server->caps |= NFS_CAP_OWNER;
2335 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2336 server->caps |= NFS_CAP_OWNER_GROUP;
2337 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2338 server->caps |= NFS_CAP_ATIME;
2339 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2340 server->caps |= NFS_CAP_CTIME;
2341 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2342 server->caps |= NFS_CAP_MTIME;
2343
Trond Myklebusta65318b2009-03-11 14:10:28 -04002344 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2345 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2346 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002348 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002350
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 return status;
2352}
2353
Trond Myklebust55a97592006-06-09 09:34:19 -04002354int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355{
2356 struct nfs4_exception exception = { };
2357 int err;
2358 do {
2359 err = nfs4_handle_exception(server,
2360 _nfs4_server_capabilities(server, fhandle),
2361 &exception);
2362 } while (exception.retry);
2363 return err;
2364}
2365
2366static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2367 struct nfs_fsinfo *info)
2368{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 struct nfs4_lookup_root_arg args = {
2370 .bitmask = nfs4_fattr_bitmap,
2371 };
2372 struct nfs4_lookup_res res = {
2373 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002374 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 .fh = fhandle,
2376 };
2377 struct rpc_message msg = {
2378 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2379 .rpc_argp = &args,
2380 .rpc_resp = &res,
2381 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002382
Trond Myklebust0e574af2005-10-27 22:12:38 -04002383 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002384 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385}
2386
2387static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2388 struct nfs_fsinfo *info)
2389{
2390 struct nfs4_exception exception = { };
2391 int err;
2392 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002393 err = _nfs4_lookup_root(server, fhandle, info);
2394 switch (err) {
2395 case 0:
2396 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002397 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002398 default:
2399 err = nfs4_handle_exception(server, err, &exception);
2400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002402out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 return err;
2404}
2405
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002406static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2407 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2408{
2409 struct rpc_auth *auth;
2410 int ret;
2411
2412 auth = rpcauth_create(flavor, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002413 if (IS_ERR(auth)) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002414 ret = -EIO;
2415 goto out;
2416 }
2417 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002418out:
2419 return ret;
2420}
2421
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002422static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002423 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002425 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002426 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002428 len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04002429 if (len < 0)
2430 return len;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002431
2432 for (i = 0; i < len; i++) {
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002433 /* AUTH_UNIX is the default flavor if none was specified,
2434 * thus has already been tried. */
2435 if (flav_array[i] == RPC_AUTH_UNIX)
2436 continue;
2437
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002438 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002439 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002440 continue;
2441 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002442 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002443 /*
2444 * -EACCESS could mean that the user doesn't have correct permissions
2445 * to access the mount. It could also mean that we tried to mount
2446 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2447 * existing mount programs don't handle -EACCES very well so it should
2448 * be mapped to -EPERM instead.
2449 */
2450 if (status == -EACCES)
2451 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002452 return status;
2453}
2454
2455/*
2456 * get the file handle for the "/" directory on the server
2457 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002458int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2459 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002460{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002461 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002462 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002463 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2464 /*
2465 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2466 * by nfs4_map_errors() as this function exits.
2467 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002468 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 if (status == 0)
2470 status = nfs4_server_capabilities(server, fhandle);
2471 if (status == 0)
2472 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002473 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474}
2475
Bryan Schumakerbae36242012-05-10 15:07:31 -04002476static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2477 struct nfs_fsinfo *info)
2478{
2479 int error;
2480 struct nfs_fattr *fattr = info->fattr;
2481
2482 error = nfs4_server_capabilities(server, mntfh);
2483 if (error < 0) {
2484 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2485 return error;
2486 }
2487
2488 error = nfs4_proc_getattr(server, mntfh, fattr);
2489 if (error < 0) {
2490 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2491 return error;
2492 }
2493
2494 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2495 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2496 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2497
2498 return error;
2499}
2500
Manoj Naik6b97fd32006-06-09 09:34:29 -04002501/*
2502 * Get locations and (maybe) other attributes of a referral.
2503 * Note that we'll actually follow the referral later when
2504 * we detect fsid mismatch in inode revalidation
2505 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002506static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2507 const struct qstr *name, struct nfs_fattr *fattr,
2508 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002509{
2510 int status = -ENOMEM;
2511 struct page *page = NULL;
2512 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002513
2514 page = alloc_page(GFP_KERNEL);
2515 if (page == NULL)
2516 goto out;
2517 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2518 if (locations == NULL)
2519 goto out;
2520
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002521 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002522 if (status != 0)
2523 goto out;
2524 /* Make sure server returned a different fsid for the referral */
2525 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002526 dprintk("%s: server did not return a different fsid for"
2527 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002528 status = -EIO;
2529 goto out;
2530 }
Andy Adamson533eb462011-06-13 18:25:56 -04002531 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2532 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002533
Andy Adamson533eb462011-06-13 18:25:56 -04002534 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002535 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002536 memset(fhandle, 0, sizeof(struct nfs_fh));
2537out:
2538 if (page)
2539 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002540 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002541 return status;
2542}
2543
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2545{
2546 struct nfs4_getattr_arg args = {
2547 .fh = fhandle,
2548 .bitmask = server->attr_bitmask,
2549 };
2550 struct nfs4_getattr_res res = {
2551 .fattr = fattr,
2552 .server = server,
2553 };
2554 struct rpc_message msg = {
2555 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2556 .rpc_argp = &args,
2557 .rpc_resp = &res,
2558 };
2559
Trond Myklebust0e574af2005-10-27 22:12:38 -04002560 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002561 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562}
2563
2564static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2565{
2566 struct nfs4_exception exception = { };
2567 int err;
2568 do {
2569 err = nfs4_handle_exception(server,
2570 _nfs4_proc_getattr(server, fhandle, fattr),
2571 &exception);
2572 } while (exception.retry);
2573 return err;
2574}
2575
2576/*
2577 * The file is not closed if it is opened due to the a request to change
2578 * the size of the file. The open call will not be needed once the
2579 * VFS layer lookup-intents are implemented.
2580 *
2581 * Close is called when the inode is destroyed.
2582 * If we haven't opened the file for O_WRONLY, we
2583 * need to in the size_change case to obtain a stateid.
2584 *
2585 * Got race?
2586 * Because OPEN is always done by name in nfsv4, it is
2587 * possible that we opened a different file by the same
2588 * name. We can recognize this race condition, but we
2589 * can't do anything about it besides returning an error.
2590 *
2591 * This will be fixed with VFS changes (lookup-intent).
2592 */
2593static int
2594nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2595 struct iattr *sattr)
2596{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002597 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002598 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002599 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 int status;
2601
Benny Halevy8a1636c2010-07-14 15:43:57 -04002602 if (pnfs_ld_layoutret_on_setattr(inode))
2603 pnfs_return_layout(inode);
2604
Trond Myklebust0e574af2005-10-27 22:12:38 -04002605 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606
Andy Adamson26699402012-05-30 16:12:24 -04002607 /* Deal with open(O_TRUNC) */
2608 if (sattr->ia_valid & ATTR_OPEN)
2609 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2610
2611 /* Optimization: if the end result is no change, don't RPC */
2612 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2613 return 0;
2614
Trond Myklebustd5308382005-11-04 15:33:38 -05002615 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002616 if (sattr->ia_valid & ATTR_FILE) {
2617 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002618
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002619 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002620 if (ctx) {
2621 cred = ctx->cred;
2622 state = ctx->state;
2623 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002624 }
2625
2626 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002627 if (status == 0)
2628 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 return status;
2630}
2631
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002632static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2633 const struct qstr *name, struct nfs_fh *fhandle,
2634 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002635{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002636 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002637 int status;
2638 struct nfs4_lookup_arg args = {
2639 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002640 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002641 .name = name,
2642 };
2643 struct nfs4_lookup_res res = {
2644 .server = server,
2645 .fattr = fattr,
2646 .fh = fhandle,
2647 };
2648 struct rpc_message msg = {
2649 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2650 .rpc_argp = &args,
2651 .rpc_resp = &res,
2652 };
2653
2654 nfs_fattr_init(fattr);
2655
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002657 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 dprintk("NFS reply lookup: %d\n", status);
2659 return status;
2660}
2661
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002662static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002663{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002664 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002665 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002666 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2667 fattr->nlink = 2;
2668}
2669
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002670static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2671 struct qstr *name, struct nfs_fh *fhandle,
2672 struct nfs_fattr *fattr)
2673{
2674 struct nfs4_exception exception = { };
2675 struct rpc_clnt *client = *clnt;
2676 int err;
2677 do {
2678 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2679 switch (err) {
2680 case -NFS4ERR_BADNAME:
2681 err = -ENOENT;
2682 goto out;
2683 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002684 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002685 goto out;
2686 case -NFS4ERR_WRONGSEC:
2687 err = -EPERM;
2688 if (client != *clnt)
2689 goto out;
2690
2691 client = nfs4_create_sec_client(client, dir, name);
2692 if (IS_ERR(client))
2693 return PTR_ERR(client);
2694
2695 exception.retry = 1;
2696 break;
2697 default:
2698 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2699 }
2700 } while (exception.retry);
2701
2702out:
2703 if (err == 0)
2704 *clnt = client;
2705 else if (client != *clnt)
2706 rpc_shutdown_client(client);
2707
2708 return err;
2709}
2710
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002711static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002712 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002714 int status;
2715 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002716
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002717 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2718 if (client != NFS_CLIENT(dir)) {
2719 rpc_shutdown_client(client);
2720 nfs_fixup_secinfo_attributes(fattr);
2721 }
2722 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723}
2724
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002725struct rpc_clnt *
2726nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2727 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2728{
2729 int status;
2730 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2731
2732 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2733 if (status < 0) {
2734 rpc_shutdown_client(client);
2735 return ERR_PTR(status);
2736 }
2737 return client;
2738}
2739
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2741{
Trond Myklebust76b32992007-08-10 17:45:11 -04002742 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 struct nfs4_accessargs args = {
2744 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002745 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002747 struct nfs4_accessres res = {
2748 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002749 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 struct rpc_message msg = {
2751 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2752 .rpc_argp = &args,
2753 .rpc_resp = &res,
2754 .rpc_cred = entry->cred,
2755 };
2756 int mode = entry->mask;
2757 int status;
2758
2759 /*
2760 * Determine which access bits we want to ask for...
2761 */
2762 if (mode & MAY_READ)
2763 args.access |= NFS4_ACCESS_READ;
2764 if (S_ISDIR(inode->i_mode)) {
2765 if (mode & MAY_WRITE)
2766 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2767 if (mode & MAY_EXEC)
2768 args.access |= NFS4_ACCESS_LOOKUP;
2769 } else {
2770 if (mode & MAY_WRITE)
2771 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2772 if (mode & MAY_EXEC)
2773 args.access |= NFS4_ACCESS_EXECUTE;
2774 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002775
2776 res.fattr = nfs_alloc_fattr();
2777 if (res.fattr == NULL)
2778 return -ENOMEM;
2779
Bryan Schumaker7c513052011-03-24 17:12:24 +00002780 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002782 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04002783 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002785 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 return status;
2787}
2788
2789static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2790{
2791 struct nfs4_exception exception = { };
2792 int err;
2793 do {
2794 err = nfs4_handle_exception(NFS_SERVER(inode),
2795 _nfs4_proc_access(inode, entry),
2796 &exception);
2797 } while (exception.retry);
2798 return err;
2799}
2800
2801/*
2802 * TODO: For the time being, we don't try to get any attributes
2803 * along with any of the zero-copy operations READ, READDIR,
2804 * READLINK, WRITE.
2805 *
2806 * In the case of the first three, we want to put the GETATTR
2807 * after the read-type operation -- this is because it is hard
2808 * to predict the length of a GETATTR response in v4, and thus
2809 * align the READ data correctly. This means that the GETATTR
2810 * may end up partially falling into the page cache, and we should
2811 * shift it into the 'tail' of the xdr_buf before processing.
2812 * To do this efficiently, we need to know the total length
2813 * of data received, which doesn't seem to be available outside
2814 * of the RPC layer.
2815 *
2816 * In the case of WRITE, we also want to put the GETATTR after
2817 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04002818 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 *
2820 * Both of these changes to the XDR layer would in fact be quite
2821 * minor, but I decided to leave them for a subsequent patch.
2822 */
2823static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2824 unsigned int pgbase, unsigned int pglen)
2825{
2826 struct nfs4_readlink args = {
2827 .fh = NFS_FH(inode),
2828 .pgbase = pgbase,
2829 .pglen = pglen,
2830 .pages = &page,
2831 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002832 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 struct rpc_message msg = {
2834 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2835 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002836 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 };
2838
Bryan Schumaker7c513052011-03-24 17:12:24 +00002839 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 -07002840}
2841
2842static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2843 unsigned int pgbase, unsigned int pglen)
2844{
2845 struct nfs4_exception exception = { };
2846 int err;
2847 do {
2848 err = nfs4_handle_exception(NFS_SERVER(inode),
2849 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2850 &exception);
2851 } while (exception.retry);
2852 return err;
2853}
2854
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002856 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858static int
2859nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002860 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861{
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002862 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 int status = 0;
2865
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002866 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
2867 if (IS_ERR(ctx))
2868 return PTR_ERR(ctx);
2869
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002870 sattr->ia_mode &= ~current_umask();
Trond Myklebust8626e4a2012-07-16 12:01:42 -04002871 state = nfs4_do_open(dir, dentry, ctx->mode,
2872 flags, sattr, ctx->cred,
2873 &ctx->mdsthreshold);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002874 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 if (IS_ERR(state)) {
2876 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002877 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002879 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002880 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002881 ctx->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882out:
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002883 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 return status;
2885}
2886
2887static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2888{
Trond Myklebust16e42952005-10-27 22:12:44 -04002889 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002890 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07002892 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002894 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002895 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002896 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002898 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2899 .rpc_argp = &args,
2900 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 };
Trond Myklebust778d2812012-04-27 13:48:19 -04002902 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04002903
Bryan Schumaker7c513052011-03-24 17:12:24 +00002904 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04002905 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04002906 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 return status;
2908}
2909
2910static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2911{
2912 struct nfs4_exception exception = { };
2913 int err;
2914 do {
2915 err = nfs4_handle_exception(NFS_SERVER(dir),
2916 _nfs4_proc_remove(dir, name),
2917 &exception);
2918 } while (exception.retry);
2919 return err;
2920}
2921
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002922static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002924 struct nfs_server *server = NFS_SERVER(dir);
2925 struct nfs_removeargs *args = msg->rpc_argp;
2926 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002928 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05002930 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931}
2932
Bryan Schumaker34e137c2012-03-19 14:54:41 -04002933static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
2934{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04002935 nfs4_setup_sequence(NFS_SERVER(data->dir),
2936 &data->args.seq_args,
2937 &data->res.seq_res,
2938 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939}
2940
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002941static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002943 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2944
Trond Myklebust14516c32010-07-31 14:29:06 -04002945 if (!nfs4_sequence_done(task, &res->seq_res))
2946 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002947 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002948 return 0;
2949 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002950 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951}
2952
Jeff Laytond3d41522010-09-17 17:31:57 -04002953static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2954{
2955 struct nfs_server *server = NFS_SERVER(dir);
2956 struct nfs_renameargs *arg = msg->rpc_argp;
2957 struct nfs_renameres *res = msg->rpc_resp;
2958
2959 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04002960 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002961 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04002962}
2963
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04002964static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
2965{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04002966 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
2967 &data->args.seq_args,
2968 &data->res.seq_res,
2969 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04002970}
2971
2972static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2973 struct inode *new_dir)
2974{
2975 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2976
2977 if (!nfs4_sequence_done(task, &res->seq_res))
2978 return 0;
2979 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2980 return 0;
2981
2982 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04002983 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04002984 return 1;
2985}
2986
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2988 struct inode *new_dir, struct qstr *new_name)
2989{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002990 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002991 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 .old_dir = NFS_FH(old_dir),
2993 .new_dir = NFS_FH(new_dir),
2994 .old_name = old_name,
2995 .new_name = new_name,
2996 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002997 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002998 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002999 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 struct rpc_message msg = {
3001 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3002 .rpc_argp = &arg,
3003 .rpc_resp = &res,
3004 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003005 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006
Bryan Schumaker7c513052011-03-24 17:12:24 +00003007 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 if (!status) {
3009 update_changeattr(old_dir, &res.old_cinfo);
3010 update_changeattr(new_dir, &res.new_cinfo);
3011 }
3012 return status;
3013}
3014
3015static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3016 struct inode *new_dir, struct qstr *new_name)
3017{
3018 struct nfs4_exception exception = { };
3019 int err;
3020 do {
3021 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3022 _nfs4_proc_rename(old_dir, old_name,
3023 new_dir, new_name),
3024 &exception);
3025 } while (exception.retry);
3026 return err;
3027}
3028
3029static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3030{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003031 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 struct nfs4_link_arg arg = {
3033 .fh = NFS_FH(inode),
3034 .dir_fh = NFS_FH(dir),
3035 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003036 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003038 struct nfs4_link_res res = {
3039 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003040 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 struct rpc_message msg = {
3042 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3043 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003044 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003046 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047
Trond Myklebust136f2622010-04-16 16:22:49 -04003048 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003049 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003050 goto out;
3051
Bryan Schumaker7c513052011-03-24 17:12:24 +00003052 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003053 if (!status) {
3054 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04003055 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003056 }
Trond Myklebust136f2622010-04-16 16:22:49 -04003057out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003058 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 return status;
3060}
3061
3062static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3063{
3064 struct nfs4_exception exception = { };
3065 int err;
3066 do {
3067 err = nfs4_handle_exception(NFS_SERVER(inode),
3068 _nfs4_proc_link(inode, dir, name),
3069 &exception);
3070 } while (exception.retry);
3071 return err;
3072}
3073
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003074struct nfs4_createdata {
3075 struct rpc_message msg;
3076 struct nfs4_create_arg arg;
3077 struct nfs4_create_res res;
3078 struct nfs_fh fh;
3079 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003080};
3081
3082static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3083 struct qstr *name, struct iattr *sattr, u32 ftype)
3084{
3085 struct nfs4_createdata *data;
3086
3087 data = kzalloc(sizeof(*data), GFP_KERNEL);
3088 if (data != NULL) {
3089 struct nfs_server *server = NFS_SERVER(dir);
3090
3091 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3092 data->msg.rpc_argp = &data->arg;
3093 data->msg.rpc_resp = &data->res;
3094 data->arg.dir_fh = NFS_FH(dir);
3095 data->arg.server = server;
3096 data->arg.name = name;
3097 data->arg.attrs = sattr;
3098 data->arg.ftype = ftype;
3099 data->arg.bitmask = server->attr_bitmask;
3100 data->res.server = server;
3101 data->res.fh = &data->fh;
3102 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003103 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003104 }
3105 return data;
3106}
3107
3108static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3109{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003110 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003111 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003112 if (status == 0) {
3113 update_changeattr(dir, &data->res.dir_cinfo);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003114 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3115 }
3116 return status;
3117}
3118
3119static void nfs4_free_createdata(struct nfs4_createdata *data)
3120{
3121 kfree(data);
3122}
3123
Chuck Lever4f390c12006-08-22 20:06:22 -04003124static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003125 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003127 struct nfs4_createdata *data;
3128 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129
Chuck Lever94a6d752006-08-22 20:06:23 -04003130 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003131 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003132
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003133 status = -ENOMEM;
3134 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3135 if (data == NULL)
3136 goto out;
3137
3138 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3139 data->arg.u.symlink.pages = &page;
3140 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003142 status = nfs4_do_create(dir, dentry, data);
3143
3144 nfs4_free_createdata(data);
3145out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 return status;
3147}
3148
Chuck Lever4f390c12006-08-22 20:06:22 -04003149static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003150 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151{
3152 struct nfs4_exception exception = { };
3153 int err;
3154 do {
3155 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003156 _nfs4_proc_symlink(dir, dentry, page,
3157 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 &exception);
3159 } while (exception.retry);
3160 return err;
3161}
3162
3163static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3164 struct iattr *sattr)
3165{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003166 struct nfs4_createdata *data;
3167 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003169 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3170 if (data == NULL)
3171 goto out;
3172
3173 status = nfs4_do_create(dir, dentry, data);
3174
3175 nfs4_free_createdata(data);
3176out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 return status;
3178}
3179
3180static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3181 struct iattr *sattr)
3182{
3183 struct nfs4_exception exception = { };
3184 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003185
3186 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 do {
3188 err = nfs4_handle_exception(NFS_SERVER(dir),
3189 _nfs4_proc_mkdir(dir, dentry, sattr),
3190 &exception);
3191 } while (exception.retry);
3192 return err;
3193}
3194
3195static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003196 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197{
3198 struct inode *dir = dentry->d_inode;
3199 struct nfs4_readdir_arg args = {
3200 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003201 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 .pgbase = 0,
3203 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003204 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003205 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 };
3207 struct nfs4_readdir_res res;
3208 struct rpc_message msg = {
3209 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3210 .rpc_argp = &args,
3211 .rpc_resp = &res,
3212 .rpc_cred = cred,
3213 };
3214 int status;
3215
Harvey Harrison3110ff82008-05-02 13:42:44 -07003216 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003217 dentry->d_parent->d_name.name,
3218 dentry->d_name.name,
3219 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003220 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003222 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 -05003223 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003224 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003225 status += args.pgbase;
3226 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003227
3228 nfs_invalidate_atime(dir);
3229
Harvey Harrison3110ff82008-05-02 13:42:44 -07003230 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 return status;
3232}
3233
3234static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003235 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236{
3237 struct nfs4_exception exception = { };
3238 int err;
3239 do {
3240 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3241 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003242 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 &exception);
3244 } while (exception.retry);
3245 return err;
3246}
3247
3248static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3249 struct iattr *sattr, dev_t rdev)
3250{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003251 struct nfs4_createdata *data;
3252 int mode = sattr->ia_mode;
3253 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003255 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3256 if (data == NULL)
3257 goto out;
3258
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003260 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003262 data->arg.ftype = NF4BLK;
3263 data->arg.u.device.specdata1 = MAJOR(rdev);
3264 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 }
3266 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003267 data->arg.ftype = NF4CHR;
3268 data->arg.u.device.specdata1 = MAJOR(rdev);
3269 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003270 } else if (!S_ISSOCK(mode)) {
3271 status = -EINVAL;
3272 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003275 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003276out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003277 nfs4_free_createdata(data);
3278out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 return status;
3280}
3281
3282static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3283 struct iattr *sattr, dev_t rdev)
3284{
3285 struct nfs4_exception exception = { };
3286 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003287
3288 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 do {
3290 err = nfs4_handle_exception(NFS_SERVER(dir),
3291 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3292 &exception);
3293 } while (exception.retry);
3294 return err;
3295}
3296
3297static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3298 struct nfs_fsstat *fsstat)
3299{
3300 struct nfs4_statfs_arg args = {
3301 .fh = fhandle,
3302 .bitmask = server->attr_bitmask,
3303 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003304 struct nfs4_statfs_res res = {
3305 .fsstat = fsstat,
3306 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 struct rpc_message msg = {
3308 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3309 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003310 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 };
3312
Trond Myklebust0e574af2005-10-27 22:12:38 -04003313 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003314 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315}
3316
3317static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3318{
3319 struct nfs4_exception exception = { };
3320 int err;
3321 do {
3322 err = nfs4_handle_exception(server,
3323 _nfs4_proc_statfs(server, fhandle, fsstat),
3324 &exception);
3325 } while (exception.retry);
3326 return err;
3327}
3328
3329static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3330 struct nfs_fsinfo *fsinfo)
3331{
3332 struct nfs4_fsinfo_arg args = {
3333 .fh = fhandle,
3334 .bitmask = server->attr_bitmask,
3335 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003336 struct nfs4_fsinfo_res res = {
3337 .fsinfo = fsinfo,
3338 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 struct rpc_message msg = {
3340 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3341 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003342 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 };
3344
Bryan Schumaker7c513052011-03-24 17:12:24 +00003345 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346}
3347
3348static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3349{
3350 struct nfs4_exception exception = { };
3351 int err;
3352
3353 do {
3354 err = nfs4_handle_exception(server,
3355 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3356 &exception);
3357 } while (exception.retry);
3358 return err;
3359}
3360
3361static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3362{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003363 int error;
3364
Trond Myklebust0e574af2005-10-27 22:12:38 -04003365 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003366 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003367 if (error == 0) {
3368 /* block layout checks this! */
3369 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003370 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003371 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003372
3373 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374}
3375
3376static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3377 struct nfs_pathconf *pathconf)
3378{
3379 struct nfs4_pathconf_arg args = {
3380 .fh = fhandle,
3381 .bitmask = server->attr_bitmask,
3382 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003383 struct nfs4_pathconf_res res = {
3384 .pathconf = pathconf,
3385 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 struct rpc_message msg = {
3387 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3388 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003389 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 };
3391
3392 /* None of the pathconf attributes are mandatory to implement */
3393 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3394 memset(pathconf, 0, sizeof(*pathconf));
3395 return 0;
3396 }
3397
Trond Myklebust0e574af2005-10-27 22:12:38 -04003398 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003399 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400}
3401
3402static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3403 struct nfs_pathconf *pathconf)
3404{
3405 struct nfs4_exception exception = { };
3406 int err;
3407
3408 do {
3409 err = nfs4_handle_exception(server,
3410 _nfs4_proc_pathconf(server, fhandle, pathconf),
3411 &exception);
3412 } while (exception.retry);
3413 return err;
3414}
3415
Benny Halevyd20581a2011-05-22 19:52:03 +03003416void __nfs4_read_done_cb(struct nfs_read_data *data)
3417{
Fred Isamancd841602012-04-20 14:47:44 -04003418 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003419}
3420
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003421static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422{
Fred Isamancd841602012-04-20 14:47:44 -04003423 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003425 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003426 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003427 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003429
Benny Halevyd20581a2011-05-22 19:52:03 +03003430 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003432 renew_lease(server, data->timestamp);
3433 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434}
3435
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003436static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3437{
3438
3439 dprintk("--> %s\n", __func__);
3440
3441 if (!nfs4_sequence_done(task, &data->res.seq_res))
3442 return -EAGAIN;
3443
Benny Halevyd20581a2011-05-22 19:52:03 +03003444 return data->read_done_cb ? data->read_done_cb(task, data) :
3445 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003446}
3447
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003448static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003451 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003452 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003453 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454}
3455
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003456static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3457{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003458 nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3459 &data->args.seq_args,
3460 &data->res.seq_res,
3461 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462}
3463
Fred Isamanb029bc92011-03-03 15:13:42 +00003464static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465{
Fred Isamancd841602012-04-20 14:47:44 -04003466 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003468 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003469 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003470 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003472 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003474 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003475 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003476 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477}
3478
Fred Isamanb029bc92011-03-03 15:13:42 +00003479static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3480{
3481 if (!nfs4_sequence_done(task, &data->res.seq_res))
3482 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003483 return data->write_done_cb ? data->write_done_cb(task, data) :
3484 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003485}
3486
Trond Myklebust5a37f852012-04-28 14:55:16 -04003487static
3488bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003489{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003490 const struct nfs_pgio_header *hdr = data->header;
3491
3492 /* Don't request attributes for pNFS or O_DIRECT writes */
3493 if (data->ds_clp != NULL || hdr->dreq != NULL)
3494 return false;
3495 /* Otherwise, request attributes if and only if we don't hold
3496 * a delegation
3497 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04003498 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003499}
Fred Isamana69aef12011-03-03 15:13:47 +00003500
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003501static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502{
Fred Isamancd841602012-04-20 14:47:44 -04003503 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003504
Trond Myklebust5a37f852012-04-28 14:55:16 -04003505 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003506 data->args.bitmask = NULL;
3507 data->res.fattr = NULL;
3508 } else
3509 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003510
Fred Isamanb029bc92011-03-03 15:13:42 +00003511 if (!data->write_done_cb)
3512 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003513 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 data->timestamp = jiffies;
3515
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003516 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003517 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518}
3519
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003520static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3521{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003522 nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3523 &data->args.seq_args,
3524 &data->res.seq_res,
3525 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526}
3527
Fred Isaman0b7c0152012-04-20 14:47:39 -04003528static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3529{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003530 nfs4_setup_sequence(NFS_SERVER(data->inode),
3531 &data->args.seq_args,
3532 &data->res.seq_res,
3533 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534}
3535
Fred Isaman0b7c0152012-04-20 14:47:39 -04003536static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003539
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003540 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003541 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003542 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003544 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545}
3546
Fred Isaman0b7c0152012-04-20 14:47:39 -04003547static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003548{
3549 if (!nfs4_sequence_done(task, &data->res.seq_res))
3550 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003551 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003552}
3553
Fred Isaman0b7c0152012-04-20 14:47:39 -04003554static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003556 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003557
Fred Isaman0b7c0152012-04-20 14:47:39 -04003558 if (data->commit_done_cb == NULL)
3559 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003560 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003561 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003562 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563}
3564
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003565struct nfs4_renewdata {
3566 struct nfs_client *client;
3567 unsigned long timestamp;
3568};
3569
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570/*
3571 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3572 * standalone procedure for queueing an asynchronous RENEW.
3573 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003574static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003575{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003576 struct nfs4_renewdata *data = calldata;
3577 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003578
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003579 if (atomic_read(&clp->cl_count) > 1)
3580 nfs4_schedule_state_renewal(clp);
3581 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003582 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003583}
3584
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003585static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003587 struct nfs4_renewdata *data = calldata;
3588 struct nfs_client *clp = data->client;
3589 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590
3591 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003592 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003593 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3594 return;
3595 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003596 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003597 return;
3598 }
3599 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003601 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602}
3603
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003604static const struct rpc_call_ops nfs4_renew_ops = {
3605 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003606 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003607};
3608
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003609static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610{
3611 struct rpc_message msg = {
3612 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3613 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003614 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003616 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003618 if (renew_flags == 0)
3619 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003620 if (!atomic_inc_not_zero(&clp->cl_count))
3621 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003622 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003623 if (data == NULL)
3624 return -ENOMEM;
3625 data->client = clp;
3626 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003628 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629}
3630
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003631static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632{
3633 struct rpc_message msg = {
3634 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3635 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003636 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637 };
3638 unsigned long now = jiffies;
3639 int status;
3640
3641 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3642 if (status < 0)
3643 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003644 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 return 0;
3646}
3647
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003648static inline int nfs4_server_supports_acls(struct nfs_server *server)
3649{
3650 return (server->caps & NFS_CAP_ACLS)
3651 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3652 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3653}
3654
Trond Myklebust21f498c2012-08-24 10:59:25 -04003655/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3656 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003657 * the stack.
3658 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04003659#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003660
Neil Hormane9e3d722011-03-04 19:26:03 -05003661static int buf_to_pages_noslab(const void *buf, size_t buflen,
3662 struct page **pages, unsigned int *pgbase)
3663{
3664 struct page *newpage, **spages;
3665 int rc = 0;
3666 size_t len;
3667 spages = pages;
3668
3669 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04003670 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003671 newpage = alloc_page(GFP_KERNEL);
3672
3673 if (newpage == NULL)
3674 goto unwind;
3675 memcpy(page_address(newpage), buf, len);
3676 buf += len;
3677 buflen -= len;
3678 *pages++ = newpage;
3679 rc++;
3680 } while (buflen != 0);
3681
3682 return rc;
3683
3684unwind:
3685 for(; rc > 0; rc--)
3686 __free_page(spages[rc-1]);
3687 return -ENOMEM;
3688}
3689
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003690struct nfs4_cached_acl {
3691 int cached;
3692 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003693 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003694};
3695
3696static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003697{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003698 struct nfs_inode *nfsi = NFS_I(inode);
3699
3700 spin_lock(&inode->i_lock);
3701 kfree(nfsi->nfs4_acl);
3702 nfsi->nfs4_acl = acl;
3703 spin_unlock(&inode->i_lock);
3704}
3705
3706static void nfs4_zap_acl_attr(struct inode *inode)
3707{
3708 nfs4_set_cached_acl(inode, NULL);
3709}
3710
3711static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3712{
3713 struct nfs_inode *nfsi = NFS_I(inode);
3714 struct nfs4_cached_acl *acl;
3715 int ret = -ENOENT;
3716
3717 spin_lock(&inode->i_lock);
3718 acl = nfsi->nfs4_acl;
3719 if (acl == NULL)
3720 goto out;
3721 if (buf == NULL) /* user is just asking for length */
3722 goto out_len;
3723 if (acl->cached == 0)
3724 goto out;
3725 ret = -ERANGE; /* see getxattr(2) man page */
3726 if (acl->len > buflen)
3727 goto out;
3728 memcpy(buf, acl->data, acl->len);
3729out_len:
3730 ret = acl->len;
3731out:
3732 spin_unlock(&inode->i_lock);
3733 return ret;
3734}
3735
Sachin Prabhu5794d212012-04-17 14:36:40 +01003736static 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 +00003737{
3738 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003739 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003740
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003741 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003742 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003743 if (acl == NULL)
3744 goto out;
3745 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003746 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003747 } else {
3748 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3749 if (acl == NULL)
3750 goto out;
3751 acl->cached = 0;
3752 }
3753 acl->len = acl_len;
3754out:
3755 nfs4_set_cached_acl(inode, acl);
3756}
3757
Andy Adamsonbf118a32011-12-07 11:55:27 -05003758/*
3759 * The getxattr API returns the required buffer length when called with a
3760 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3761 * the required buf. On a NULL buf, we send a page of data to the server
3762 * guessing that the ACL request can be serviced by a page. If so, we cache
3763 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3764 * the cache. If not so, we throw away the page, and cache the required
3765 * length. The next getxattr call will then produce another round trip to
3766 * the server, this time with the input buf of the required size.
3767 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003768static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003769{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003770 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003771 struct nfs_getaclargs args = {
3772 .fh = NFS_FH(inode),
3773 .acl_pages = pages,
3774 .acl_len = buflen,
3775 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003776 struct nfs_getaclres res = {
3777 .acl_len = buflen,
3778 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003779 struct rpc_message msg = {
3780 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3781 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003782 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003783 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003784 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3785 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003786
Andy Adamsonbf118a32011-12-07 11:55:27 -05003787 /* As long as we're doing a round trip to the server anyway,
3788 * let's be prepared for a page of acl data. */
3789 if (npages == 0)
3790 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003791 if (npages > ARRAY_SIZE(pages))
3792 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003793
Andy Adamsonbf118a32011-12-07 11:55:27 -05003794 for (i = 0; i < npages; i++) {
3795 pages[i] = alloc_page(GFP_KERNEL);
3796 if (!pages[i])
3797 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003798 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01003799
3800 /* for decoding across pages */
3801 res.acl_scratch = alloc_page(GFP_KERNEL);
3802 if (!res.acl_scratch)
3803 goto out_free;
3804
Andy Adamsonbf118a32011-12-07 11:55:27 -05003805 args.acl_len = npages * PAGE_SIZE;
3806 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003807
Peng Taode040be2012-01-10 22:42:47 +08003808 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003809 __func__, buf, buflen, npages, args.acl_len);
3810 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3811 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003812 if (ret)
3813 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003814
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003815 /* Handle the case where the passed-in buffer is too short */
3816 if (res.acl_flags & NFS4_ACL_TRUNC) {
3817 /* Did the user only issue a request for the acl length? */
3818 if (buf == NULL)
3819 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003820 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003821 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003822 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003823 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01003824 if (buf) {
3825 if (res.acl_len > buflen) {
3826 ret = -ERANGE;
3827 goto out_free;
3828 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003829 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01003830 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003831out_ok:
3832 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003833out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003834 for (i = 0; i < npages; i++)
3835 if (pages[i])
3836 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05003837 if (res.acl_scratch)
3838 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003839 return ret;
3840}
3841
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003842static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3843{
3844 struct nfs4_exception exception = { };
3845 ssize_t ret;
3846 do {
3847 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3848 if (ret >= 0)
3849 break;
3850 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3851 } while (exception.retry);
3852 return ret;
3853}
3854
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003855static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3856{
3857 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003858 int ret;
3859
3860 if (!nfs4_server_supports_acls(server))
3861 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003862 ret = nfs_revalidate_inode(server, inode);
3863 if (ret < 0)
3864 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003865 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3866 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003867 ret = nfs4_read_cached_acl(inode, buf, buflen);
3868 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003869 /* -ENOENT is returned if there is no ACL or if there is an ACL
3870 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003871 return ret;
3872 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003873}
3874
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003875static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003876{
3877 struct nfs_server *server = NFS_SERVER(inode);
3878 struct page *pages[NFS4ACL_MAXPAGES];
3879 struct nfs_setaclargs arg = {
3880 .fh = NFS_FH(inode),
3881 .acl_pages = pages,
3882 .acl_len = buflen,
3883 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003884 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003885 struct rpc_message msg = {
3886 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3887 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003888 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003889 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003890 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05003891 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003892
3893 if (!nfs4_server_supports_acls(server))
3894 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003895 if (npages > ARRAY_SIZE(pages))
3896 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05003897 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3898 if (i < 0)
3899 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04003900 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003901 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003902
3903 /*
3904 * Free each page after tx, so the only ref left is
3905 * held by the network stack
3906 */
3907 for (; i > 0; i--)
3908 put_page(pages[i-1]);
3909
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003910 /*
3911 * Acl update can result in inode attribute update.
3912 * so mark the attribute cache invalid.
3913 */
3914 spin_lock(&inode->i_lock);
3915 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3916 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003917 nfs_access_zap_cache(inode);
3918 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003919 return ret;
3920}
3921
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003922static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3923{
3924 struct nfs4_exception exception = { };
3925 int err;
3926 do {
3927 err = nfs4_handle_exception(NFS_SERVER(inode),
3928 __nfs4_proc_set_acl(inode, buf, buflen),
3929 &exception);
3930 } while (exception.retry);
3931 return err;
3932}
3933
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003935nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003937 struct nfs_client *clp = server->nfs_client;
3938
3939 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 return 0;
3941 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05003942 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003943 case -NFS4ERR_ADMIN_REVOKED:
3944 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04003945 if (state == NULL)
3946 break;
3947 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003948 case -NFS4ERR_OPENMODE:
3949 if (state == NULL)
3950 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003951 nfs4_schedule_stateid_recovery(server, state);
3952 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003953 case -NFS4ERR_EXPIRED:
3954 if (state != NULL)
3955 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003957 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003958 nfs4_schedule_lease_recovery(clp);
3959 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003960#if defined(CONFIG_NFS_V4_1)
3961 case -NFS4ERR_BADSESSION:
3962 case -NFS4ERR_BADSLOT:
3963 case -NFS4ERR_BAD_HIGH_SLOT:
3964 case -NFS4ERR_DEADSESSION:
3965 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3966 case -NFS4ERR_SEQ_FALSE_RETRY:
3967 case -NFS4ERR_SEQ_MISORDERED:
3968 dprintk("%s ERROR %d, Reset session\n", __func__,
3969 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04003970 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04003971 task->tk_status = 0;
3972 return -EAGAIN;
3973#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003975 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003976 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3978 task->tk_status = 0;
3979 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04003980 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981 case -NFS4ERR_OLD_STATEID:
3982 task->tk_status = 0;
3983 return -EAGAIN;
3984 }
3985 task->tk_status = nfs4_map_errors(task->tk_status);
3986 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003987wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003988 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003989 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3990 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3991 task->tk_status = 0;
3992 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993}
3994
Chuck Leverf0920752012-05-21 22:45:41 -04003995static void nfs4_init_boot_verifier(const struct nfs_client *clp,
3996 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05003997{
3998 __be32 verf[2];
3999
Chuck Lever2c820d92012-05-21 22:45:33 -04004000 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4001 /* An impossible timestamp guarantees this value
4002 * will never match a generated boot time. */
4003 verf[0] = 0;
4004 verf[1] = (__be32)(NSEC_PER_SEC + 1);
4005 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004006 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4007 verf[0] = (__be32)nn->boot_time.tv_sec;
4008 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04004009 }
Chuck Levercd937102012-03-02 17:14:31 -05004010 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4011}
4012
Chuck Levere984a552012-09-14 17:24:21 -04004013static unsigned int
4014nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4015 char *buf, size_t len)
4016{
4017 unsigned int result;
4018
4019 rcu_read_lock();
4020 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4021 clp->cl_ipaddr,
4022 rpc_peeraddr2str(clp->cl_rpcclient,
4023 RPC_DISPLAY_ADDR),
4024 rpc_peeraddr2str(clp->cl_rpcclient,
4025 RPC_DISPLAY_PROTO));
4026 rcu_read_unlock();
4027 return result;
4028}
4029
4030static unsigned int
4031nfs4_init_uniform_client_string(const struct nfs_client *clp,
4032 char *buf, size_t len)
4033{
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004034 char *nodename = clp->cl_rpcclient->cl_nodename;
4035
4036 if (nfs4_client_id_uniquifier[0] != '\0')
4037 nodename = nfs4_client_id_uniquifier;
Chuck Levere984a552012-09-14 17:24:21 -04004038 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4039 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004040 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004041}
4042
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004043/**
4044 * nfs4_proc_setclientid - Negotiate client ID
4045 * @clp: state data structure
4046 * @program: RPC program for NFSv4 callback service
4047 * @port: IP port number for NFS4 callback service
4048 * @cred: RPC credential to use for this call
4049 * @res: where to place the result
4050 *
4051 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4052 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004053int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4054 unsigned short port, struct rpc_cred *cred,
4055 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056{
4057 nfs4_verifier sc_verifier;
4058 struct nfs4_setclientid setclientid = {
4059 .sc_verifier = &sc_verifier,
4060 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004061 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062 };
4063 struct rpc_message msg = {
4064 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4065 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004066 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004067 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004069 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070
Chuck Leverde734832012-07-11 16:30:50 -04004071 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004072 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004073 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4074 setclientid.sc_name_len =
4075 nfs4_init_uniform_client_string(clp,
4076 setclientid.sc_name,
4077 sizeof(setclientid.sc_name));
4078 else
4079 setclientid.sc_name_len =
4080 nfs4_init_nonuniform_client_string(clp,
4081 setclientid.sc_name,
4082 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004083 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004084 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004085 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004086 sizeof(setclientid.sc_netid),
4087 rpc_peeraddr2str(clp->cl_rpcclient,
4088 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004089 rcu_read_unlock();
4090 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004091 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092 clp->cl_ipaddr, port >> 8, port & 255);
4093
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004094 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4095 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4096 setclientid.sc_name_len, setclientid.sc_name);
4097 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4098 dprintk("NFS reply setclientid: %d\n", status);
4099 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100}
4101
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004102/**
4103 * nfs4_proc_setclientid_confirm - Confirm client ID
4104 * @clp: state data structure
4105 * @res: result of a previous SETCLIENTID
4106 * @cred: RPC credential to use for this call
4107 *
4108 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4109 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004110int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004111 struct nfs4_setclientid_res *arg,
4112 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113{
4114 struct nfs_fsinfo fsinfo;
4115 struct rpc_message msg = {
4116 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004117 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004119 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120 };
4121 unsigned long now;
4122 int status;
4123
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004124 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4125 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4126 clp->cl_clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004128 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 if (status == 0) {
4130 spin_lock(&clp->cl_lock);
4131 clp->cl_lease_time = fsinfo.lease_time * HZ;
4132 clp->cl_last_renewal = now;
4133 spin_unlock(&clp->cl_lock);
4134 }
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004135 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136 return status;
4137}
4138
Trond Myklebustfe650402006-01-03 09:55:18 +01004139struct nfs4_delegreturndata {
4140 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004141 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004142 struct nfs_fh fh;
4143 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004144 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004145 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004146 int rpc_status;
4147};
4148
Trond Myklebustfe650402006-01-03 09:55:18 +01004149static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4150{
4151 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004152
Trond Myklebust14516c32010-07-31 14:29:06 -04004153 if (!nfs4_sequence_done(task, &data->res.seq_res))
4154 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004155
Ricardo Labiaga79708862009-12-07 09:23:21 -05004156 switch (task->tk_status) {
4157 case -NFS4ERR_STALE_STATEID:
4158 case -NFS4ERR_EXPIRED:
4159 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004160 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004161 break;
4162 default:
4163 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4164 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004165 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004166 return;
4167 }
4168 }
4169 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004170}
4171
4172static void nfs4_delegreturn_release(void *calldata)
4173{
Trond Myklebustfe650402006-01-03 09:55:18 +01004174 kfree(calldata);
4175}
4176
Andy Adamson938e1012009-04-01 09:22:28 -04004177#if defined(CONFIG_NFS_V4_1)
4178static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4179{
4180 struct nfs4_delegreturndata *d_data;
4181
4182 d_data = (struct nfs4_delegreturndata *)data;
4183
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004184 nfs4_setup_sequence(d_data->res.server,
4185 &d_data->args.seq_args,
4186 &d_data->res.seq_res,
4187 task);
Andy Adamson938e1012009-04-01 09:22:28 -04004188}
4189#endif /* CONFIG_NFS_V4_1 */
4190
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004191static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004192#if defined(CONFIG_NFS_V4_1)
4193 .rpc_call_prepare = nfs4_delegreturn_prepare,
4194#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004195 .rpc_call_done = nfs4_delegreturn_done,
4196 .rpc_release = nfs4_delegreturn_release,
4197};
4198
Trond Myklebuste6f81072008-01-24 18:14:34 -05004199static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004200{
4201 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004202 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004203 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004204 struct rpc_message msg = {
4205 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4206 .rpc_cred = cred,
4207 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004208 struct rpc_task_setup task_setup_data = {
4209 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004210 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004211 .callback_ops = &nfs4_delegreturn_ops,
4212 .flags = RPC_TASK_ASYNC,
4213 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004214 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004215
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004216 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004217 if (data == NULL)
4218 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004219 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004220 data->args.fhandle = &data->fh;
4221 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004222 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004223 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004224 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004225 data->res.fattr = &data->fattr;
4226 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004227 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004228 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004229 data->rpc_status = 0;
4230
Trond Myklebustc970aa82007-07-14 15:39:59 -04004231 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004232 msg.rpc_argp = &data->args;
4233 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004234 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004235 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004236 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004237 if (!issync)
4238 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004239 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004240 if (status != 0)
4241 goto out;
4242 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004243 if (status == 0)
4244 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4245 else
4246 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004247out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004248 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004249 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250}
4251
Trond Myklebuste6f81072008-01-24 18:14:34 -05004252int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253{
4254 struct nfs_server *server = NFS_SERVER(inode);
4255 struct nfs4_exception exception = { };
4256 int err;
4257 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004258 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259 switch (err) {
4260 case -NFS4ERR_STALE_STATEID:
4261 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 case 0:
4263 return 0;
4264 }
4265 err = nfs4_handle_exception(server, err, &exception);
4266 } while (exception.retry);
4267 return err;
4268}
4269
4270#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4271#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4272
4273/*
4274 * sleep, with exponential backoff, and retry the LOCK operation.
4275 */
4276static unsigned long
4277nfs4_set_lock_task_retry(unsigned long timeout)
4278{
Jeff Laytond3103102011-12-01 22:44:39 +01004279 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280 timeout <<= 1;
4281 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4282 return NFS4_LOCK_MAXTIMEOUT;
4283 return timeout;
4284}
4285
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4287{
4288 struct inode *inode = state->inode;
4289 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004290 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004291 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004293 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004295 struct nfs_lockt_res res = {
4296 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 };
4298 struct rpc_message msg = {
4299 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4300 .rpc_argp = &arg,
4301 .rpc_resp = &res,
4302 .rpc_cred = state->owner->so_cred,
4303 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304 struct nfs4_lock_state *lsp;
4305 int status;
4306
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004307 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004308 status = nfs4_set_lock_state(state, request);
4309 if (status != 0)
4310 goto out;
4311 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004312 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004313 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004314 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004315 switch (status) {
4316 case 0:
4317 request->fl_type = F_UNLCK;
4318 break;
4319 case -NFS4ERR_DENIED:
4320 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004322 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004323out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324 return status;
4325}
4326
4327static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4328{
4329 struct nfs4_exception exception = { };
4330 int err;
4331
4332 do {
4333 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4334 _nfs4_proc_getlk(state, cmd, request),
4335 &exception);
4336 } while (exception.retry);
4337 return err;
4338}
4339
4340static int do_vfs_lock(struct file *file, struct file_lock *fl)
4341{
4342 int res = 0;
4343 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4344 case FL_POSIX:
4345 res = posix_lock_file_wait(file, fl);
4346 break;
4347 case FL_FLOCK:
4348 res = flock_lock_file_wait(file, fl);
4349 break;
4350 default:
4351 BUG();
4352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353 return res;
4354}
4355
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004356struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004357 struct nfs_locku_args arg;
4358 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004359 struct nfs4_lock_state *lsp;
4360 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004361 struct file_lock fl;
4362 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004363 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004364};
4365
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004366static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4367 struct nfs_open_context *ctx,
4368 struct nfs4_lock_state *lsp,
4369 struct nfs_seqid *seqid)
4370{
4371 struct nfs4_unlockdata *p;
4372 struct inode *inode = lsp->ls_state->inode;
4373
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004374 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004375 if (p == NULL)
4376 return NULL;
4377 p->arg.fh = NFS_FH(inode);
4378 p->arg.fl = &p->fl;
4379 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004380 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004381 p->arg.stateid = &lsp->ls_stateid;
4382 p->lsp = lsp;
4383 atomic_inc(&lsp->ls_count);
4384 /* Ensure we don't close file until we're done freeing locks! */
4385 p->ctx = get_nfs_open_context(ctx);
4386 memcpy(&p->fl, fl, sizeof(p->fl));
4387 p->server = NFS_SERVER(inode);
4388 return p;
4389}
4390
Trond Myklebust06f814a2006-01-03 09:55:07 +01004391static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004392{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004393 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004394 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004395 nfs4_put_lock_state(calldata->lsp);
4396 put_nfs_open_context(calldata->ctx);
4397 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004398}
4399
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004400static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004401{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004402 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004403
Trond Myklebust14516c32010-07-31 14:29:06 -04004404 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4405 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004406 switch (task->tk_status) {
4407 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004408 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4409 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004410 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004411 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004412 case -NFS4ERR_BAD_STATEID:
4413 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004414 case -NFS4ERR_STALE_STATEID:
4415 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004416 break;
4417 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004418 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004419 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004420 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04004421 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004422}
4423
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004424static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004425{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004426 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004428 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004429 return;
Trond Myklebust795a88c2012-09-10 13:26:49 -04004430 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004431 /* Note: exit _without_ running nfs4_locku_done */
4432 task->tk_action = NULL;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05004433 nfs4_sequence_done(task, &calldata->res.seq_res);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004434 return;
4435 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004436 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004437 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004438 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004439 &calldata->res.seq_res,
4440 task) != 0)
4441 nfs_release_seqid(calldata->arg.seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442}
4443
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004444static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004445 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004446 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004447 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004448};
4449
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004450static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4451 struct nfs_open_context *ctx,
4452 struct nfs4_lock_state *lsp,
4453 struct nfs_seqid *seqid)
4454{
4455 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004456 struct rpc_message msg = {
4457 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4458 .rpc_cred = ctx->cred,
4459 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004460 struct rpc_task_setup task_setup_data = {
4461 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004462 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004463 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004464 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004465 .flags = RPC_TASK_ASYNC,
4466 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004467
Frank Filz137d6ac2007-07-09 15:32:29 -07004468 /* Ensure this is an unlock - when canceling a lock, the
4469 * canceled lock is passed in, and it won't be an unlock.
4470 */
4471 fl->fl_type = F_UNLCK;
4472
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004473 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4474 if (data == NULL) {
4475 nfs_free_seqid(seqid);
4476 return ERR_PTR(-ENOMEM);
4477 }
4478
Trond Myklebust9d12b212012-01-17 22:04:25 -05004479 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004480 msg.rpc_argp = &data->arg;
4481 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004482 task_setup_data.callback_data = data;
4483 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004484}
4485
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4487{
Trond Myklebust65b62a22013-02-07 10:54:07 -05004488 struct inode *inode = state->inode;
4489 struct nfs4_state_owner *sp = state->owner;
4490 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004491 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004492 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004493 struct rpc_task *task;
4494 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004495 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496
Trond Myklebust9b073572006-06-29 16:38:34 -04004497 status = nfs4_set_lock_state(state, request);
4498 /* Unlock _before_ we do the RPC call */
4499 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05004500 /* Exclude nfs_delegation_claim_locks() */
4501 mutex_lock(&sp->so_delegreturn_mutex);
4502 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004503 down_read(&nfsi->rwsem);
4504 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4505 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05004506 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04004507 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004508 }
4509 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05004510 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04004511 if (status != 0)
4512 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004513 /* Is this a delegated lock? */
4514 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004515 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004516 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004517 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004518 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004519 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004520 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004521 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004522 status = PTR_ERR(task);
4523 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004524 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004525 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004526 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004527out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004528 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004529 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530}
4531
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004532struct nfs4_lockdata {
4533 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004534 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004535 struct nfs4_lock_state *lsp;
4536 struct nfs_open_context *ctx;
4537 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004538 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004539 int rpc_status;
4540 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004541 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004542};
4543
4544static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004545 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4546 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004547{
4548 struct nfs4_lockdata *p;
4549 struct inode *inode = lsp->ls_state->inode;
4550 struct nfs_server *server = NFS_SERVER(inode);
4551
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004552 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004553 if (p == NULL)
4554 return NULL;
4555
4556 p->arg.fh = NFS_FH(inode);
4557 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004558 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004559 if (p->arg.open_seqid == NULL)
4560 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004561 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004562 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004563 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004564 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004565 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004566 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004567 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004568 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004569 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004570 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004571 atomic_inc(&lsp->ls_count);
4572 p->ctx = get_nfs_open_context(ctx);
4573 memcpy(&p->fl, fl, sizeof(p->fl));
4574 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004575out_free_seqid:
4576 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004577out_free:
4578 kfree(p);
4579 return NULL;
4580}
4581
4582static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4583{
4584 struct nfs4_lockdata *data = calldata;
4585 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586
Harvey Harrison3110ff82008-05-02 13:42:44 -07004587 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004588 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4589 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004590 /* Do we need to do an open_to_lock_owner? */
4591 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004592 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004593 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004594 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004595 data->arg.open_stateid = &state->stateid;
4596 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004597 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004598 } else
4599 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004600 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004601 if (nfs4_setup_sequence(data->server,
4602 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004603 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004604 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04004605 return;
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004606 nfs_release_seqid(data->arg.open_seqid);
4607out_release_lock_seqid:
4608 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004609 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004610}
4611
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004612static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4613{
4614 struct nfs4_lockdata *data = calldata;
4615
Harvey Harrison3110ff82008-05-02 13:42:44 -07004616 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004617
Trond Myklebust14516c32010-07-31 14:29:06 -04004618 if (!nfs4_sequence_done(task, &data->res.seq_res))
4619 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004620
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004621 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004622 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004623 if (data->rpc_status == 0)
4624 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4625 else
4626 goto out;
4627 }
4628 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004629 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004630 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04004631 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004632 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004633out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004634 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004635}
4636
4637static void nfs4_lock_release(void *calldata)
4638{
4639 struct nfs4_lockdata *data = calldata;
4640
Harvey Harrison3110ff82008-05-02 13:42:44 -07004641 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004642 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004643 if (data->cancelled != 0) {
4644 struct rpc_task *task;
4645 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4646 data->arg.lock_seqid);
4647 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004648 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004649 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004650 } else
4651 nfs_free_seqid(data->arg.lock_seqid);
4652 nfs4_put_lock_state(data->lsp);
4653 put_nfs_open_context(data->ctx);
4654 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004655 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004656}
4657
4658static const struct rpc_call_ops nfs4_lock_ops = {
4659 .rpc_call_prepare = nfs4_lock_prepare,
4660 .rpc_call_done = nfs4_lock_done,
4661 .rpc_release = nfs4_lock_release,
4662};
4663
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004664static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4665{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004666 switch (error) {
4667 case -NFS4ERR_ADMIN_REVOKED:
4668 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004669 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004670 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04004671 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004672 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004673 break;
4674 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004675 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004676 case -NFS4ERR_EXPIRED:
4677 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004678 };
4679}
4680
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004681static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004682{
4683 struct nfs4_lockdata *data;
4684 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004685 struct rpc_message msg = {
4686 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4687 .rpc_cred = state->owner->so_cred,
4688 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004689 struct rpc_task_setup task_setup_data = {
4690 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004691 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004692 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004693 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004694 .flags = RPC_TASK_ASYNC,
4695 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004696 int ret;
4697
Harvey Harrison3110ff82008-05-02 13:42:44 -07004698 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004699 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004700 fl->fl_u.nfs4_fl.owner,
4701 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004702 if (data == NULL)
4703 return -ENOMEM;
4704 if (IS_SETLKW(cmd))
4705 data->arg.block = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004706 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004707 msg.rpc_argp = &data->arg;
4708 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004709 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004710 if (recovery_type > NFS_LOCK_NEW) {
4711 if (recovery_type == NFS_LOCK_RECLAIM)
4712 data->arg.reclaim = NFS_LOCK_RECLAIM;
4713 nfs4_set_sequence_privileged(&data->arg.seq_args);
4714 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04004715 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004716 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004717 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004718 ret = nfs4_wait_for_completion_rpc_task(task);
4719 if (ret == 0) {
4720 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004721 if (ret)
4722 nfs4_handle_setlk_error(data->server, data->lsp,
4723 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004724 } else
4725 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004726 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004727 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004728 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729}
4730
4731static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4732{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004733 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004734 struct nfs4_exception exception = {
4735 .inode = state->inode,
4736 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004737 int err;
4738
4739 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004740 /* Cache the lock if possible... */
4741 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4742 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004743 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004744 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004745 break;
4746 nfs4_handle_exception(server, err, &exception);
4747 } while (exception.retry);
4748 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749}
4750
4751static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4752{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004753 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004754 struct nfs4_exception exception = {
4755 .inode = state->inode,
4756 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004757 int err;
4758
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004759 err = nfs4_set_lock_state(state, request);
4760 if (err != 0)
4761 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004762 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004763 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4764 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004765 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004766 switch (err) {
4767 default:
4768 goto out;
4769 case -NFS4ERR_GRACE:
4770 case -NFS4ERR_DELAY:
4771 nfs4_handle_exception(server, err, &exception);
4772 err = 0;
4773 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004774 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004775out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004776 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777}
4778
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004779#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004780/**
4781 * nfs41_check_expired_locks - possibly free a lock stateid
4782 *
4783 * @state: NFSv4 state for an inode
4784 *
4785 * Returns NFS_OK if recovery for this stateid is now finished.
4786 * Otherwise a negative NFS4ERR value is returned.
4787 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004788static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004789{
Chuck Levereb64cf92012-07-11 16:30:05 -04004790 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004791 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004792 struct nfs_server *server = NFS_SERVER(state->inode);
4793
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004794 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04004795 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004796 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4797 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004798 /* Free the stateid unless the server
4799 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04004800 if (status != -NFS4ERR_BAD_STATEID)
4801 nfs41_free_stateid(server,
4802 &lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004803 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004804 ret = status;
4805 }
4806 }
4807 };
4808
4809 return ret;
4810}
4811
4812static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4813{
4814 int status = NFS_OK;
4815
4816 if (test_bit(LK_STATE_IN_USE, &state->flags))
4817 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04004818 if (status != NFS_OK)
4819 status = nfs4_lock_expired(state, request);
4820 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004821}
4822#endif
4823
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4825{
Trond Myklebust9a99af492013-02-04 20:17:49 -05004826 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004827 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004828 unsigned char fl_flags = request->fl_flags;
Trond Myklebust9a99af492013-02-04 20:17:49 -05004829 unsigned int seq;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004830 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004832 if ((fl_flags & FL_POSIX) &&
4833 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4834 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004835 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004836 status = nfs4_set_lock_state(state, request);
4837 if (status != 0)
4838 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004839 request->fl_flags |= FL_ACCESS;
4840 status = do_vfs_lock(request->fl_file, request);
4841 if (status < 0)
4842 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004843 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004844 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004845 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004846 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004847 request->fl_flags = fl_flags & ~FL_SLEEP;
4848 status = do_vfs_lock(request->fl_file, request);
4849 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004850 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05004851 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
4852 up_read(&nfsi->rwsem);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004853 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004854 if (status != 0)
Trond Myklebust9a99af492013-02-04 20:17:49 -05004855 goto out;
4856 down_read(&nfsi->rwsem);
4857 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
4858 status = -NFS4ERR_DELAY;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004859 goto out_unlock;
Trond Myklebust9a99af492013-02-04 20:17:49 -05004860 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004861 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004862 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004863 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004864 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4865 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004866out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004867 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004868out:
4869 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870 return status;
4871}
4872
4873static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4874{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004875 struct nfs4_exception exception = {
4876 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04004877 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004878 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879 int err;
4880
4881 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004882 err = _nfs4_proc_setlk(state, cmd, request);
4883 if (err == -NFS4ERR_DENIED)
4884 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004886 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887 } while (exception.retry);
4888 return err;
4889}
4890
4891static int
4892nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4893{
4894 struct nfs_open_context *ctx;
4895 struct nfs4_state *state;
4896 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4897 int status;
4898
4899 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004900 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901 state = ctx->state;
4902
4903 if (request->fl_start < 0 || request->fl_end < 0)
4904 return -EINVAL;
4905
Trond Myklebustd9531262009-07-21 19:22:38 -04004906 if (IS_GETLK(cmd)) {
4907 if (state != NULL)
4908 return nfs4_proc_getlk(state, F_GETLK, request);
4909 return 0;
4910 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911
4912 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4913 return -EINVAL;
4914
Trond Myklebustd9531262009-07-21 19:22:38 -04004915 if (request->fl_type == F_UNLCK) {
4916 if (state != NULL)
4917 return nfs4_proc_unlck(state, cmd, request);
4918 return 0;
4919 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004920
Trond Myklebustd9531262009-07-21 19:22:38 -04004921 if (state == NULL)
4922 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04004923 /*
4924 * Don't rely on the VFS having checked the file open mode,
4925 * since it won't do this for flock() locks.
4926 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04004927 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04004928 case F_RDLCK:
4929 if (!(filp->f_mode & FMODE_READ))
4930 return -EBADF;
4931 break;
4932 case F_WRLCK:
4933 if (!(filp->f_mode & FMODE_WRITE))
4934 return -EBADF;
4935 }
4936
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937 do {
4938 status = nfs4_proc_setlk(state, cmd, request);
4939 if ((status != -EAGAIN) || IS_SETLK(cmd))
4940 break;
4941 timeout = nfs4_set_lock_task_retry(timeout);
4942 status = -ERESTARTSYS;
4943 if (signalled())
4944 break;
4945 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946 return status;
4947}
4948
Trond Myklebust888e6942005-11-04 15:38:11 -05004949int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4950{
4951 struct nfs_server *server = NFS_SERVER(state->inode);
4952 struct nfs4_exception exception = { };
4953 int err;
4954
4955 err = nfs4_set_lock_state(state, fl);
4956 if (err != 0)
4957 goto out;
4958 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004959 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004960 switch (err) {
4961 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004962 printk(KERN_ERR "NFS: %s: unhandled error "
4963 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004964 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004965 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004966 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004967 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004968 case -NFS4ERR_STALE_STATEID:
Trond Myklebust37380e42013-02-05 12:35:16 -05004969 set_bit(NFS_DELEGATED_STATE, &state->flags);
4970 case -NFS4ERR_EXPIRED:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004971 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust37380e42013-02-05 12:35:16 -05004972 err = -EAGAIN;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004973 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004974 case -NFS4ERR_BADSESSION:
4975 case -NFS4ERR_BADSLOT:
4976 case -NFS4ERR_BAD_HIGH_SLOT:
4977 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4978 case -NFS4ERR_DEADSESSION:
Trond Myklebust37380e42013-02-05 12:35:16 -05004979 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust9f594792012-05-27 13:02:53 -04004980 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Trond Myklebust37380e42013-02-05 12:35:16 -05004981 err = -EAGAIN;
Trond Myklebustd5122202009-06-17 13:22:58 -07004982 goto out;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004983 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004984 case -NFS4ERR_ADMIN_REVOKED:
4985 case -NFS4ERR_BAD_STATEID:
4986 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004987 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004988 err = 0;
4989 goto out;
4990 case -ENOMEM:
4991 case -NFS4ERR_DENIED:
4992 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4993 err = 0;
4994 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004995 }
Trond Myklebust37380e42013-02-05 12:35:16 -05004996 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust888e6942005-11-04 15:38:11 -05004997 err = nfs4_handle_exception(server, err, &exception);
4998 } while (exception.retry);
4999out:
5000 return err;
5001}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005002
Trond Myklebustcf470c32012-03-07 13:49:12 -05005003struct nfs_release_lockowner_data {
5004 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005005 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005006 struct nfs_release_lockowner_args args;
5007};
5008
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005009static void nfs4_release_lockowner_release(void *calldata)
5010{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005011 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005012 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005013 kfree(calldata);
5014}
5015
Trond Myklebust17280172012-03-11 13:11:00 -04005016static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005017 .rpc_release = nfs4_release_lockowner_release,
5018};
5019
Trond Myklebustcf470c32012-03-07 13:49:12 -05005020int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005021{
5022 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005023 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005024 struct rpc_message msg = {
5025 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5026 };
5027
5028 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005029 return -EINVAL;
5030 data = kmalloc(sizeof(*data), GFP_NOFS);
5031 if (!data)
5032 return -ENOMEM;
5033 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005034 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005035 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5036 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5037 data->args.lock_owner.s_dev = server->s_dev;
5038 msg.rpc_argp = &data->args;
5039 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5040 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005041}
5042
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005043#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5044
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005045static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5046 const void *buf, size_t buflen,
5047 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005048{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005049 if (strcmp(key, "") != 0)
5050 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005051
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005052 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005053}
5054
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005055static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5056 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005057{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005058 if (strcmp(key, "") != 0)
5059 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005060
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005061 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005062}
5063
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005064static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5065 size_t list_len, const char *name,
5066 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005067{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005068 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005069
J. Bruce Fields096455a2006-03-20 23:23:42 -05005070 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5071 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005072
5073 if (list && len <= list_len)
5074 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005075 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005076}
5077
Andy Adamson533eb462011-06-13 18:25:56 -04005078/*
5079 * nfs_fhget will use either the mounted_on_fileid or the fileid
5080 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005081static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5082{
Andy Adamson533eb462011-06-13 18:25:56 -04005083 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5084 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5085 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005086 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005087 return;
5088
5089 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005090 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005091 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5092 fattr->nlink = 2;
5093}
5094
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005095static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5096 const struct qstr *name,
5097 struct nfs4_fs_locations *fs_locations,
5098 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005099{
5100 struct nfs_server *server = NFS_SERVER(dir);
5101 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005102 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005103 };
5104 struct nfs4_fs_locations_arg args = {
5105 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005106 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005107 .page = page,
5108 .bitmask = bitmask,
5109 };
Benny Halevy22958462009-04-01 09:22:02 -04005110 struct nfs4_fs_locations_res res = {
5111 .fs_locations = fs_locations,
5112 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005113 struct rpc_message msg = {
5114 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5115 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005116 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005117 };
5118 int status;
5119
Harvey Harrison3110ff82008-05-02 13:42:44 -07005120 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005121
5122 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5123 * is not supported */
5124 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5125 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5126 else
5127 bitmask[0] |= FATTR4_WORD0_FILEID;
5128
Trond Myklebustc228fd32007-01-13 02:28:11 -05005129 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005130 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005131 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005132 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005133 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005134 return status;
5135}
5136
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005137int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5138 const struct qstr *name,
5139 struct nfs4_fs_locations *fs_locations,
5140 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005141{
5142 struct nfs4_exception exception = { };
5143 int err;
5144 do {
5145 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005146 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005147 &exception);
5148 } while (exception.retry);
5149 return err;
5150}
5151
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005152static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5153{
5154 int status;
5155 struct nfs4_secinfo_arg args = {
5156 .dir_fh = NFS_FH(dir),
5157 .name = name,
5158 };
5159 struct nfs4_secinfo_res res = {
5160 .flavors = flavors,
5161 };
5162 struct rpc_message msg = {
5163 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5164 .rpc_argp = &args,
5165 .rpc_resp = &res,
5166 };
5167
5168 dprintk("NFS call secinfo %s\n", name->name);
5169 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5170 dprintk("NFS reply secinfo: %d\n", status);
5171 return status;
5172}
5173
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005174int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5175 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005176{
5177 struct nfs4_exception exception = { };
5178 int err;
5179 do {
5180 err = nfs4_handle_exception(NFS_SERVER(dir),
5181 _nfs4_proc_secinfo(dir, name, flavors),
5182 &exception);
5183 } while (exception.retry);
5184 return err;
5185}
5186
Andy Adamson557134a2009-04-01 09:21:53 -04005187#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005188/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005189 * Check the exchange flags returned by the server for invalid flags, having
5190 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5191 * DS flags set.
5192 */
5193static int nfs4_check_cl_exchange_flags(u32 flags)
5194{
5195 if (flags & ~EXCHGID4_FLAG_MASK_R)
5196 goto out_inval;
5197 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5198 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5199 goto out_inval;
5200 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5201 goto out_inval;
5202 return NFS_OK;
5203out_inval:
5204 return -NFS4ERR_INVAL;
5205}
5206
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005207static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005208nfs41_same_server_scope(struct nfs41_server_scope *a,
5209 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005210{
5211 if (a->server_scope_sz == b->server_scope_sz &&
5212 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5213 return true;
5214
5215 return false;
5216}
5217
Andy Adamson357f54d2010-12-14 10:11:57 -05005218/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005219 * nfs4_proc_bind_conn_to_session()
5220 *
5221 * The 4.1 client currently uses the same TCP connection for the
5222 * fore and backchannel.
5223 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005224int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005225{
5226 int status;
5227 struct nfs41_bind_conn_to_session_res res;
5228 struct rpc_message msg = {
5229 .rpc_proc =
5230 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5231 .rpc_argp = clp,
5232 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005233 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005234 };
5235
5236 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005237
5238 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5239 if (unlikely(res.session == NULL)) {
5240 status = -ENOMEM;
5241 goto out;
5242 }
5243
5244 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5245 if (status == 0) {
5246 if (memcmp(res.session->sess_id.data,
5247 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5248 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5249 status = -EIO;
5250 goto out_session;
5251 }
5252 if (res.dir != NFS4_CDFS4_BOTH) {
5253 dprintk("NFS: %s: Unexpected direction from server\n",
5254 __func__);
5255 status = -EIO;
5256 goto out_session;
5257 }
5258 if (res.use_conn_in_rdma_mode) {
5259 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5260 __func__);
5261 status = -EIO;
5262 goto out_session;
5263 }
5264 }
5265out_session:
5266 kfree(res.session);
5267out:
5268 dprintk("<-- %s status= %d\n", __func__, status);
5269 return status;
5270}
5271
5272/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005273 * nfs4_proc_exchange_id()
5274 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005275 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5276 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04005277 * Since the clientid has expired, all compounds using sessions
5278 * associated with the stale clientid will be returning
5279 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5280 * be in some phase of session reset.
5281 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005282int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005283{
5284 nfs4_verifier verifier;
5285 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005286 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005287 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005288 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005289 };
5290 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005291 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005292 };
5293 int status;
5294 struct rpc_message msg = {
5295 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5296 .rpc_argp = &args,
5297 .rpc_resp = &res,
5298 .rpc_cred = cred,
5299 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005300
Chuck Leverf0920752012-05-21 22:45:41 -04005301 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04005302 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5303 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005304 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5305 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5306 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005307
Chuck Leveracdeb692012-05-21 22:46:16 -04005308 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005309 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005310 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005311 status = -ENOMEM;
5312 goto out;
5313 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005314
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005315 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005316 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005317 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005318 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005319 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005320 }
5321
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005322 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005323 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005324 status = -ENOMEM;
5325 goto out_server_scope;
5326 }
5327
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005328 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005329 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005330 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005331
Chuck Lever177313f2012-05-21 22:44:58 -04005332 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005333 clp->cl_clientid = res.clientid;
5334 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5335 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5336 clp->cl_seqid = res.seqid;
5337
Chuck Leveracdeb692012-05-21 22:46:16 -04005338 kfree(clp->cl_serverowner);
5339 clp->cl_serverowner = res.server_owner;
5340 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005341
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005342 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005343 kfree(clp->cl_implid);
5344 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005345
Chuck Lever177313f2012-05-21 22:44:58 -04005346 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005347 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005348 res.server_scope)) {
5349 dprintk("%s: server_scope mismatch detected\n",
5350 __func__);
5351 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005352 kfree(clp->cl_serverscope);
5353 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005354 }
5355
Chuck Lever177313f2012-05-21 22:44:58 -04005356 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005357 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005358 goto out;
5359 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005360 } else
5361 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005362
Chuck Leveracdeb692012-05-21 22:46:16 -04005363out_server_owner:
5364 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005365out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005366 kfree(res.server_scope);
5367out:
Chuck Lever177313f2012-05-21 22:44:58 -04005368 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005369 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005370 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005371 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04005372 clp->cl_implid->date.seconds,
5373 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005374 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005375 return status;
5376}
5377
Trond Myklebust66245532012-05-25 17:18:09 -04005378static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5379 struct rpc_cred *cred)
5380{
5381 struct rpc_message msg = {
5382 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5383 .rpc_argp = clp,
5384 .rpc_cred = cred,
5385 };
5386 int status;
5387
5388 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5389 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04005390 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04005391 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5392 return status;
5393}
5394
5395static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5396 struct rpc_cred *cred)
5397{
5398 unsigned int loop;
5399 int ret;
5400
5401 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5402 ret = _nfs4_proc_destroy_clientid(clp, cred);
5403 switch (ret) {
5404 case -NFS4ERR_DELAY:
5405 case -NFS4ERR_CLIENTID_BUSY:
5406 ssleep(1);
5407 break;
5408 default:
5409 return ret;
5410 }
5411 }
5412 return 0;
5413}
5414
5415int nfs4_destroy_clientid(struct nfs_client *clp)
5416{
5417 struct rpc_cred *cred;
5418 int ret = 0;
5419
5420 if (clp->cl_mvops->minor_version < 1)
5421 goto out;
5422 if (clp->cl_exchange_flags == 0)
5423 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04005424 if (clp->cl_preserve_clid)
5425 goto out;
Trond Myklebust66245532012-05-25 17:18:09 -04005426 cred = nfs4_get_exchange_id_cred(clp);
5427 ret = nfs4_proc_destroy_clientid(clp, cred);
5428 if (cred)
5429 put_rpccred(cred);
5430 switch (ret) {
5431 case 0:
5432 case -NFS4ERR_STALE_CLIENTID:
5433 clp->cl_exchange_flags = 0;
5434 }
5435out:
5436 return ret;
5437}
5438
Andy Adamson2050f0c2009-04-01 09:22:30 -04005439struct nfs4_get_lease_time_data {
5440 struct nfs4_get_lease_time_args *args;
5441 struct nfs4_get_lease_time_res *res;
5442 struct nfs_client *clp;
5443};
5444
5445static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5446 void *calldata)
5447{
Andy Adamson2050f0c2009-04-01 09:22:30 -04005448 struct nfs4_get_lease_time_data *data =
5449 (struct nfs4_get_lease_time_data *)calldata;
5450
5451 dprintk("--> %s\n", __func__);
5452 /* just setup sequence, do not trigger session recovery
5453 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005454 nfs41_setup_sequence(data->clp->cl_session,
5455 &data->args->la_seq_args,
5456 &data->res->lr_seq_res,
5457 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005458 dprintk("<-- %s\n", __func__);
5459}
5460
5461/*
5462 * Called from nfs4_state_manager thread for session setup, so don't recover
5463 * from sequence operation or clientid errors.
5464 */
5465static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5466{
5467 struct nfs4_get_lease_time_data *data =
5468 (struct nfs4_get_lease_time_data *)calldata;
5469
5470 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005471 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5472 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005473 switch (task->tk_status) {
5474 case -NFS4ERR_DELAY:
5475 case -NFS4ERR_GRACE:
5476 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5477 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5478 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005479 /* fall through */
5480 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005481 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005482 return;
5483 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005484 dprintk("<-- %s\n", __func__);
5485}
5486
Trond Myklebust17280172012-03-11 13:11:00 -04005487static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005488 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5489 .rpc_call_done = nfs4_get_lease_time_done,
5490};
5491
5492int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5493{
5494 struct rpc_task *task;
5495 struct nfs4_get_lease_time_args args;
5496 struct nfs4_get_lease_time_res res = {
5497 .lr_fsinfo = fsinfo,
5498 };
5499 struct nfs4_get_lease_time_data data = {
5500 .args = &args,
5501 .res = &res,
5502 .clp = clp,
5503 };
5504 struct rpc_message msg = {
5505 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5506 .rpc_argp = &args,
5507 .rpc_resp = &res,
5508 };
5509 struct rpc_task_setup task_setup = {
5510 .rpc_client = clp->cl_rpcclient,
5511 .rpc_message = &msg,
5512 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005513 .callback_data = &data,
5514 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005515 };
5516 int status;
5517
Trond Myklebust9d12b212012-01-17 22:04:25 -05005518 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005519 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005520 dprintk("--> %s\n", __func__);
5521 task = rpc_run_task(&task_setup);
5522
5523 if (IS_ERR(task))
5524 status = PTR_ERR(task);
5525 else {
5526 status = task->tk_status;
5527 rpc_put_task(task);
5528 }
5529 dprintk("<-- %s return %d\n", __func__, status);
5530
5531 return status;
5532}
5533
Andy Adamsonfc931582009-04-01 09:22:31 -04005534/*
5535 * Initialize the values to be used by the client in CREATE_SESSION
5536 * If nfs4_init_session set the fore channel request and response sizes,
5537 * use them.
5538 *
5539 * Set the back channel max_resp_sz_cached to zero to force the client to
5540 * always set csa_cachethis to FALSE because the current implementation
5541 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5542 */
5543static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5544{
5545 struct nfs4_session *session = args->client->cl_session;
Trond Myklebustae72ae672012-11-20 11:02:55 -05005546 unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
5547 mxresp_sz = session->fc_target_max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005548
5549 if (mxrqst_sz == 0)
5550 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5551 if (mxresp_sz == 0)
5552 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5553 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005554 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5555 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005556 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005557 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005558
5559 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005560 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005561 __func__,
5562 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005563 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005564
5565 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005566 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5567 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5568 args->bc_attrs.max_resp_sz_cached = 0;
5569 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5570 args->bc_attrs.max_reqs = 1;
5571
5572 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5573 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5574 __func__,
5575 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5576 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5577 args->bc_attrs.max_reqs);
5578}
5579
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005580static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005581{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005582 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5583 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5584
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005585 if (rcvd->max_resp_sz > sent->max_resp_sz)
5586 return -EINVAL;
5587 /*
5588 * Our requested max_ops is the minimum we need; we're not
5589 * prepared to break up compounds into smaller pieces than that.
5590 * So, no point even trying to continue if the server won't
5591 * cooperate:
5592 */
5593 if (rcvd->max_ops < sent->max_ops)
5594 return -EINVAL;
5595 if (rcvd->max_reqs == 0)
5596 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005597 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5598 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005599 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005600}
5601
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005602static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5603{
5604 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5605 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005606
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005607 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5608 return -EINVAL;
5609 if (rcvd->max_resp_sz < sent->max_resp_sz)
5610 return -EINVAL;
5611 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5612 return -EINVAL;
5613 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005614 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005615 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005616 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005617 return -EINVAL;
5618 return 0;
5619}
Andy Adamson8d353012009-04-01 09:22:32 -04005620
Andy Adamson8d353012009-04-01 09:22:32 -04005621static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5622 struct nfs4_session *session)
5623{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005624 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005625
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005626 ret = nfs4_verify_fore_channel_attrs(args, session);
5627 if (ret)
5628 return ret;
5629 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005630}
5631
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005632static int _nfs4_proc_create_session(struct nfs_client *clp,
5633 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005634{
5635 struct nfs4_session *session = clp->cl_session;
5636 struct nfs41_create_session_args args = {
5637 .client = clp,
5638 .cb_program = NFS4_CALLBACK,
5639 };
5640 struct nfs41_create_session_res res = {
5641 .client = clp,
5642 };
5643 struct rpc_message msg = {
5644 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5645 .rpc_argp = &args,
5646 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005647 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04005648 };
5649 int status;
5650
5651 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005652 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005653
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005654 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005655
Trond Myklebust43095d32012-11-20 11:13:12 -05005656 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04005657 /* Verify the session's negotiated channel_attrs values */
5658 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005659 /* Increment the clientid slot sequence id */
5660 clp->cl_seqid++;
5661 }
5662
5663 return status;
5664}
5665
5666/*
5667 * Issues a CREATE_SESSION operation to the server.
5668 * It is the responsibility of the caller to verify the session is
5669 * expired before calling this routine.
5670 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005671int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005672{
5673 int status;
5674 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005675 struct nfs4_session *session = clp->cl_session;
5676
5677 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5678
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005679 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04005680 if (status)
5681 goto out;
5682
Andy Adamsonaacd5532011-11-09 13:58:21 -05005683 /* Init or reset the session slot tables */
5684 status = nfs4_setup_session_slot_tables(session);
5685 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005686 if (status)
5687 goto out;
5688
5689 ptr = (unsigned *)&session->sess_id.data[0];
5690 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5691 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005692out:
5693 dprintk("<-- %s\n", __func__);
5694 return status;
5695}
5696
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005697/*
5698 * Issue the over-the-wire RPC DESTROY_SESSION.
5699 * The caller must serialize access to this routine.
5700 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005701int nfs4_proc_destroy_session(struct nfs4_session *session,
5702 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005703{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005704 struct rpc_message msg = {
5705 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5706 .rpc_argp = session,
5707 .rpc_cred = cred,
5708 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005709 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005710
5711 dprintk("--> nfs4_proc_destroy_session\n");
5712
5713 /* session is still being setup */
5714 if (session->clp->cl_cons_state != NFS_CS_READY)
5715 return status;
5716
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005717 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005718
5719 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04005720 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005721 "Session has been destroyed regardless...\n", status);
5722
5723 dprintk("<-- nfs4_proc_destroy_session\n");
5724 return status;
5725}
5726
Trond Myklebust7b38c362012-05-23 13:23:31 -04005727/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005728 * Renew the cl_session lease.
5729 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005730struct nfs4_sequence_data {
5731 struct nfs_client *clp;
5732 struct nfs4_sequence_args args;
5733 struct nfs4_sequence_res res;
5734};
5735
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005736static void nfs41_sequence_release(void *data)
5737{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005738 struct nfs4_sequence_data *calldata = data;
5739 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005740
Alexandros Batsakis71358402010-02-05 03:45:05 -08005741 if (atomic_read(&clp->cl_count) > 1)
5742 nfs4_schedule_state_renewal(clp);
5743 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005744 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005745}
5746
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005747static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5748{
5749 switch(task->tk_status) {
5750 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005751 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5752 return -EAGAIN;
5753 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005754 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005755 }
5756 return 0;
5757}
5758
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005759static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005760{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005761 struct nfs4_sequence_data *calldata = data;
5762 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005763
Trond Myklebust14516c32010-07-31 14:29:06 -04005764 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5765 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005766
5767 if (task->tk_status < 0) {
5768 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005769 if (atomic_read(&clp->cl_count) == 1)
5770 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005771
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005772 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5773 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005774 return;
5775 }
5776 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005777 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005778out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005779 dprintk("<-- %s\n", __func__);
5780}
5781
5782static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5783{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005784 struct nfs4_sequence_data *calldata = data;
5785 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005786 struct nfs4_sequence_args *args;
5787 struct nfs4_sequence_res *res;
5788
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005789 args = task->tk_msg.rpc_argp;
5790 res = task->tk_msg.rpc_resp;
5791
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005792 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005793}
5794
5795static const struct rpc_call_ops nfs41_sequence_ops = {
5796 .rpc_call_done = nfs41_sequence_call_done,
5797 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005798 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005799};
5800
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005801static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
5802 struct rpc_cred *cred,
5803 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005804{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005805 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005806 struct rpc_message msg = {
5807 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5808 .rpc_cred = cred,
5809 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005810 struct rpc_task_setup task_setup_data = {
5811 .rpc_client = clp->cl_rpcclient,
5812 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005813 .callback_ops = &nfs41_sequence_ops,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005814 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5815 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005816
Alexandros Batsakis71358402010-02-05 03:45:05 -08005817 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005818 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005819 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005820 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005821 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005822 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005823 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05005824 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005825 if (is_privileged)
5826 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005827 msg.rpc_argp = &calldata->args;
5828 msg.rpc_resp = &calldata->res;
5829 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005830 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005831
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005832 return rpc_run_task(&task_setup_data);
5833}
5834
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005835static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005836{
5837 struct rpc_task *task;
5838 int ret = 0;
5839
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005840 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5841 return 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005842 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005843 if (IS_ERR(task))
5844 ret = PTR_ERR(task);
5845 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005846 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005847 dprintk("<-- %s status=%d\n", __func__, ret);
5848 return ret;
5849}
5850
5851static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5852{
5853 struct rpc_task *task;
5854 int ret;
5855
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005856 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005857 if (IS_ERR(task)) {
5858 ret = PTR_ERR(task);
5859 goto out;
5860 }
5861 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005862 if (!ret) {
5863 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5864
5865 if (task->tk_status == 0)
5866 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005867 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005868 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005869 rpc_put_task(task);
5870out:
5871 dprintk("<-- %s status=%d\n", __func__, ret);
5872 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005873}
5874
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005875struct nfs4_reclaim_complete_data {
5876 struct nfs_client *clp;
5877 struct nfs41_reclaim_complete_args arg;
5878 struct nfs41_reclaim_complete_res res;
5879};
5880
5881static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5882{
5883 struct nfs4_reclaim_complete_data *calldata = data;
5884
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005885 nfs41_setup_sequence(calldata->clp->cl_session,
5886 &calldata->arg.seq_args,
5887 &calldata->res.seq_res,
5888 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005889}
5890
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005891static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5892{
5893 switch(task->tk_status) {
5894 case 0:
5895 case -NFS4ERR_COMPLETE_ALREADY:
5896 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5897 break;
5898 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005899 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005900 /* fall through */
5901 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005902 return -EAGAIN;
5903 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005904 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005905 }
5906 return 0;
5907}
5908
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005909static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5910{
5911 struct nfs4_reclaim_complete_data *calldata = data;
5912 struct nfs_client *clp = calldata->clp;
5913 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5914
5915 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005916 if (!nfs41_sequence_done(task, res))
5917 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005918
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005919 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5920 rpc_restart_call_prepare(task);
5921 return;
5922 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005923 dprintk("<-- %s\n", __func__);
5924}
5925
5926static void nfs4_free_reclaim_complete_data(void *data)
5927{
5928 struct nfs4_reclaim_complete_data *calldata = data;
5929
5930 kfree(calldata);
5931}
5932
5933static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5934 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5935 .rpc_call_done = nfs4_reclaim_complete_done,
5936 .rpc_release = nfs4_free_reclaim_complete_data,
5937};
5938
5939/*
5940 * Issue a global reclaim complete.
5941 */
5942static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5943{
5944 struct nfs4_reclaim_complete_data *calldata;
5945 struct rpc_task *task;
5946 struct rpc_message msg = {
5947 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5948 };
5949 struct rpc_task_setup task_setup_data = {
5950 .rpc_client = clp->cl_rpcclient,
5951 .rpc_message = &msg,
5952 .callback_ops = &nfs4_reclaim_complete_call_ops,
5953 .flags = RPC_TASK_ASYNC,
5954 };
5955 int status = -ENOMEM;
5956
5957 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005958 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005959 if (calldata == NULL)
5960 goto out;
5961 calldata->clp = clp;
5962 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005963
Trond Myklebust9d12b212012-01-17 22:04:25 -05005964 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005965 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005966 msg.rpc_argp = &calldata->arg;
5967 msg.rpc_resp = &calldata->res;
5968 task_setup_data.callback_data = calldata;
5969 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005970 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005971 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005972 goto out;
5973 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05005974 status = nfs4_wait_for_completion_rpc_task(task);
5975 if (status == 0)
5976 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005977 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005978 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005979out:
5980 dprintk("<-- %s status=%d\n", __func__, status);
5981 return status;
5982}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005983
5984static void
5985nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5986{
5987 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00005988 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04005989 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005990
5991 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00005992 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5993 * right now covering the LAYOUTGET we are about to send.
5994 * However, that is not so catastrophic, and there seems
5995 * to be no way to prevent it completely.
5996 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04005997 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005998 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005999 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006000 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6001 NFS_I(lgp->args.inode)->layout,
6002 lgp->args.ctx->state)) {
6003 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00006004 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006005}
6006
6007static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6008{
6009 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07006010 struct inode *inode = lgp->args.inode;
6011 struct nfs_server *server = NFS_SERVER(inode);
6012 struct pnfs_layout_hdr *lo;
6013 struct nfs4_state *state = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006014
6015 dprintk("--> %s\n", __func__);
6016
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006017 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07006018 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006019
6020 switch (task->tk_status) {
6021 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07006022 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006023 case -NFS4ERR_LAYOUTTRYLATER:
6024 case -NFS4ERR_RECALLCONFLICT:
6025 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07006026 break;
6027 case -NFS4ERR_EXPIRED:
6028 case -NFS4ERR_BAD_STATEID:
6029 spin_lock(&inode->i_lock);
6030 lo = NFS_I(inode)->layout;
6031 if (!lo || list_empty(&lo->plh_segs)) {
6032 spin_unlock(&inode->i_lock);
6033 /* If the open stateid was bad, then recover it. */
6034 state = lgp->args.ctx->state;
6035 } else {
6036 LIST_HEAD(head);
6037
6038 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6039 spin_unlock(&inode->i_lock);
6040 /* Mark the bad layout state as invalid, then
6041 * retry using the open stateid. */
6042 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006043 }
6044 }
Trond Myklebustee314c22012-10-01 17:25:48 -07006045 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6046 rpc_restart_call_prepare(task);
6047out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006048 dprintk("<-- %s\n", __func__);
6049}
6050
Idan Kedar85541162012-08-02 11:47:10 +03006051static size_t max_response_pages(struct nfs_server *server)
6052{
6053 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6054 return nfs_page_array_len(0, max_resp_sz);
6055}
6056
6057static void nfs4_free_pages(struct page **pages, size_t size)
6058{
6059 int i;
6060
6061 if (!pages)
6062 return;
6063
6064 for (i = 0; i < size; i++) {
6065 if (!pages[i])
6066 break;
6067 __free_page(pages[i]);
6068 }
6069 kfree(pages);
6070}
6071
6072static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6073{
6074 struct page **pages;
6075 int i;
6076
6077 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6078 if (!pages) {
6079 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6080 return NULL;
6081 }
6082
6083 for (i = 0; i < size; i++) {
6084 pages[i] = alloc_page(gfp_flags);
6085 if (!pages[i]) {
6086 dprintk("%s: failed to allocate page\n", __func__);
6087 nfs4_free_pages(pages, size);
6088 return NULL;
6089 }
6090 }
6091
6092 return pages;
6093}
6094
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006095static void nfs4_layoutget_release(void *calldata)
6096{
6097 struct nfs4_layoutget *lgp = calldata;
Idan Kedar85541162012-08-02 11:47:10 +03006098 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6099 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006100
6101 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006102 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006103 put_nfs_open_context(lgp->args.ctx);
6104 kfree(calldata);
6105 dprintk("<-- %s\n", __func__);
6106}
6107
6108static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6109 .rpc_call_prepare = nfs4_layoutget_prepare,
6110 .rpc_call_done = nfs4_layoutget_done,
6111 .rpc_release = nfs4_layoutget_release,
6112};
6113
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006114struct pnfs_layout_segment *
6115nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006116{
6117 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Idan Kedar85541162012-08-02 11:47:10 +03006118 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006119 struct rpc_task *task;
6120 struct rpc_message msg = {
6121 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6122 .rpc_argp = &lgp->args,
6123 .rpc_resp = &lgp->res,
6124 };
6125 struct rpc_task_setup task_setup_data = {
6126 .rpc_client = server->client,
6127 .rpc_message = &msg,
6128 .callback_ops = &nfs4_layoutget_call_ops,
6129 .callback_data = lgp,
6130 .flags = RPC_TASK_ASYNC,
6131 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006132 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006133 int status = 0;
6134
6135 dprintk("--> %s\n", __func__);
6136
Idan Kedar85541162012-08-02 11:47:10 +03006137 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6138 if (!lgp->args.layout.pages) {
6139 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006140 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006141 }
6142 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
6143
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006144 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006145 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006146 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006147 task = rpc_run_task(&task_setup_data);
6148 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006149 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006150 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006151 if (status == 0)
6152 status = task->tk_status;
6153 if (status == 0)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006154 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006155 rpc_put_task(task);
6156 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006157 if (status)
6158 return ERR_PTR(status);
6159 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006160}
6161
Benny Halevycbe82602011-05-22 19:52:37 +03006162static void
6163nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6164{
6165 struct nfs4_layoutreturn *lrp = calldata;
6166
6167 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006168 nfs41_setup_sequence(lrp->clp->cl_session,
6169 &lrp->args.seq_args,
6170 &lrp->res.seq_res,
6171 task);
Benny Halevycbe82602011-05-22 19:52:37 +03006172}
6173
6174static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6175{
6176 struct nfs4_layoutreturn *lrp = calldata;
6177 struct nfs_server *server;
6178
6179 dprintk("--> %s\n", __func__);
6180
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006181 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03006182 return;
6183
6184 server = NFS_SERVER(lrp->args.inode);
6185 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006186 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006187 return;
6188 }
Benny Halevycbe82602011-05-22 19:52:37 +03006189 dprintk("<-- %s\n", __func__);
6190}
6191
6192static void nfs4_layoutreturn_release(void *calldata)
6193{
6194 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04006195 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006196
6197 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04006198 spin_lock(&lo->plh_inode->i_lock);
6199 if (lrp->res.lrs_present)
6200 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6201 lo->plh_block_lgets--;
6202 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006203 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006204 kfree(calldata);
6205 dprintk("<-- %s\n", __func__);
6206}
6207
6208static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6209 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6210 .rpc_call_done = nfs4_layoutreturn_done,
6211 .rpc_release = nfs4_layoutreturn_release,
6212};
6213
6214int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6215{
6216 struct rpc_task *task;
6217 struct rpc_message msg = {
6218 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6219 .rpc_argp = &lrp->args,
6220 .rpc_resp = &lrp->res,
6221 };
6222 struct rpc_task_setup task_setup_data = {
6223 .rpc_client = lrp->clp->cl_rpcclient,
6224 .rpc_message = &msg,
6225 .callback_ops = &nfs4_layoutreturn_call_ops,
6226 .callback_data = lrp,
6227 };
6228 int status;
6229
6230 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006231 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006232 task = rpc_run_task(&task_setup_data);
6233 if (IS_ERR(task))
6234 return PTR_ERR(task);
6235 status = task->tk_status;
6236 dprintk("<-- %s status=%d\n", __func__, status);
6237 rpc_put_task(task);
6238 return status;
6239}
6240
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006241/*
6242 * Retrieve the list of Data Server devices from the MDS.
6243 */
6244static int _nfs4_getdevicelist(struct nfs_server *server,
6245 const struct nfs_fh *fh,
6246 struct pnfs_devicelist *devlist)
6247{
6248 struct nfs4_getdevicelist_args args = {
6249 .fh = fh,
6250 .layoutclass = server->pnfs_curr_ld->id,
6251 };
6252 struct nfs4_getdevicelist_res res = {
6253 .devlist = devlist,
6254 };
6255 struct rpc_message msg = {
6256 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6257 .rpc_argp = &args,
6258 .rpc_resp = &res,
6259 };
6260 int status;
6261
6262 dprintk("--> %s\n", __func__);
6263 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6264 &res.seq_res, 0);
6265 dprintk("<-- %s status=%d\n", __func__, status);
6266 return status;
6267}
6268
6269int nfs4_proc_getdevicelist(struct nfs_server *server,
6270 const struct nfs_fh *fh,
6271 struct pnfs_devicelist *devlist)
6272{
6273 struct nfs4_exception exception = { };
6274 int err;
6275
6276 do {
6277 err = nfs4_handle_exception(server,
6278 _nfs4_getdevicelist(server, fh, devlist),
6279 &exception);
6280 } while (exception.retry);
6281
6282 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6283 err, devlist->num_devs);
6284
6285 return err;
6286}
6287EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6288
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006289static int
6290_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6291{
6292 struct nfs4_getdeviceinfo_args args = {
6293 .pdev = pdev,
6294 };
6295 struct nfs4_getdeviceinfo_res res = {
6296 .pdev = pdev,
6297 };
6298 struct rpc_message msg = {
6299 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6300 .rpc_argp = &args,
6301 .rpc_resp = &res,
6302 };
6303 int status;
6304
6305 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006306 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006307 dprintk("<-- %s status=%d\n", __func__, status);
6308
6309 return status;
6310}
6311
6312int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6313{
6314 struct nfs4_exception exception = { };
6315 int err;
6316
6317 do {
6318 err = nfs4_handle_exception(server,
6319 _nfs4_proc_getdeviceinfo(server, pdev),
6320 &exception);
6321 } while (exception.retry);
6322 return err;
6323}
6324EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6325
Andy Adamson863a3c62011-03-23 13:27:54 +00006326static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6327{
6328 struct nfs4_layoutcommit_data *data = calldata;
6329 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006330 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00006331
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006332 nfs41_setup_sequence(session,
6333 &data->args.seq_args,
6334 &data->res.seq_res,
6335 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00006336}
6337
6338static void
6339nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6340{
6341 struct nfs4_layoutcommit_data *data = calldata;
6342 struct nfs_server *server = NFS_SERVER(data->args.inode);
6343
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006344 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00006345 return;
6346
6347 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006348 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6349 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6350 case -NFS4ERR_BADLAYOUT: /* no layout */
6351 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006352 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006353 break;
6354 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006355 nfs_post_op_update_inode_force_wcc(data->args.inode,
6356 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006357 break;
6358 default:
6359 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6360 rpc_restart_call_prepare(task);
6361 return;
6362 }
6363 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006364}
6365
6366static void nfs4_layoutcommit_release(void *calldata)
6367{
6368 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04006369 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07006370 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00006371
Andy Adamsondb29c082011-07-30 20:52:38 -04006372 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006373 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04006374 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6375 list_del_init(&lseg->pls_lc_list);
6376 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6377 &lseg->pls_flags))
Trond Myklebust9369a432012-09-18 20:57:08 -04006378 pnfs_put_lseg(lseg);
Peng Taoa9bae562011-07-30 20:52:33 -04006379 }
Peng Tao92407e72011-10-23 20:21:17 -07006380
6381 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6382 smp_mb__after_clear_bit();
6383 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6384
Andy Adamson863a3c62011-03-23 13:27:54 +00006385 put_rpccred(data->cred);
6386 kfree(data);
6387}
6388
6389static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6390 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6391 .rpc_call_done = nfs4_layoutcommit_done,
6392 .rpc_release = nfs4_layoutcommit_release,
6393};
6394
6395int
Andy Adamsonef311532011-03-12 02:58:10 -05006396nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006397{
6398 struct rpc_message msg = {
6399 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6400 .rpc_argp = &data->args,
6401 .rpc_resp = &data->res,
6402 .rpc_cred = data->cred,
6403 };
6404 struct rpc_task_setup task_setup_data = {
6405 .task = &data->task,
6406 .rpc_client = NFS_CLIENT(data->args.inode),
6407 .rpc_message = &msg,
6408 .callback_ops = &nfs4_layoutcommit_ops,
6409 .callback_data = data,
6410 .flags = RPC_TASK_ASYNC,
6411 };
6412 struct rpc_task *task;
6413 int status = 0;
6414
6415 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6416 "lbw: %llu inode %lu\n",
6417 data->task.tk_pid, sync,
6418 data->args.lastbytewritten,
6419 data->args.inode->i_ino);
6420
Trond Myklebust9d12b212012-01-17 22:04:25 -05006421 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006422 task = rpc_run_task(&task_setup_data);
6423 if (IS_ERR(task))
6424 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006425 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006426 goto out;
6427 status = nfs4_wait_for_completion_rpc_task(task);
6428 if (status != 0)
6429 goto out;
6430 status = task->tk_status;
6431out:
6432 dprintk("%s: status %d\n", __func__, status);
6433 rpc_put_task(task);
6434 return status;
6435}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006436
6437static int
6438_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6439 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6440{
6441 struct nfs41_secinfo_no_name_args args = {
6442 .style = SECINFO_STYLE_CURRENT_FH,
6443 };
6444 struct nfs4_secinfo_res res = {
6445 .flavors = flavors,
6446 };
6447 struct rpc_message msg = {
6448 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6449 .rpc_argp = &args,
6450 .rpc_resp = &res,
6451 };
6452 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6453}
6454
6455static int
6456nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6457 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6458{
6459 struct nfs4_exception exception = { };
6460 int err;
6461 do {
6462 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6463 switch (err) {
6464 case 0:
6465 case -NFS4ERR_WRONGSEC:
6466 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006467 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006468 default:
6469 err = nfs4_handle_exception(server, err, &exception);
6470 }
6471 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006472out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006473 return err;
6474}
6475
6476static int
6477nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6478 struct nfs_fsinfo *info)
6479{
6480 int err;
6481 struct page *page;
6482 rpc_authflavor_t flavor;
6483 struct nfs4_secinfo_flavors *flavors;
6484
6485 page = alloc_page(GFP_KERNEL);
6486 if (!page) {
6487 err = -ENOMEM;
6488 goto out;
6489 }
6490
6491 flavors = page_address(page);
6492 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6493
6494 /*
6495 * Fall back on "guess and check" method if
6496 * the server doesn't support SECINFO_NO_NAME
6497 */
6498 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6499 err = nfs4_find_root_sec(server, fhandle, info);
6500 goto out_freepage;
6501 }
6502 if (err)
6503 goto out_freepage;
6504
6505 flavor = nfs_find_best_sec(flavors);
6506 if (err == 0)
6507 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6508
6509out_freepage:
6510 put_page(page);
6511 if (err == -EACCES)
6512 return -EPERM;
6513out:
6514 return err;
6515}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006516
6517static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006518{
6519 int status;
6520 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006521 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006522 };
6523 struct nfs41_test_stateid_res res;
6524 struct rpc_message msg = {
6525 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6526 .rpc_argp = &args,
6527 .rpc_resp = &res,
6528 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006529
Chuck Lever38527b12012-07-11 16:30:23 -04006530 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006531 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006532 nfs4_set_sequence_privileged(&args.seq_args);
6533 status = nfs4_call_sync_sequence(server->client, server, &msg,
6534 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04006535 if (status != NFS_OK) {
6536 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04006537 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04006538 }
6539 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04006540 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006541}
6542
Chuck Lever38527b12012-07-11 16:30:23 -04006543/**
6544 * nfs41_test_stateid - perform a TEST_STATEID operation
6545 *
6546 * @server: server / transport on which to perform the operation
6547 * @stateid: state ID to test
6548 *
6549 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6550 * Otherwise a negative NFS4ERR value is returned if the operation
6551 * failed or the state ID is not currently valid.
6552 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006553static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006554{
6555 struct nfs4_exception exception = { };
6556 int err;
6557 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006558 err = _nfs41_test_stateid(server, stateid);
6559 if (err != -NFS4ERR_DELAY)
6560 break;
6561 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04006562 } while (exception.retry);
6563 return err;
6564}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006565
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006566static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006567{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006568 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006569 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006570 };
6571 struct nfs41_free_stateid_res res;
6572 struct rpc_message msg = {
6573 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6574 .rpc_argp = &args,
6575 .rpc_resp = &res,
6576 };
Chuck Lever38527b12012-07-11 16:30:23 -04006577 int status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006578
Chuck Lever38527b12012-07-11 16:30:23 -04006579 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006580 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006581 nfs4_set_sequence_privileged(&args.seq_args);
Chuck Lever38527b12012-07-11 16:30:23 -04006582 status = nfs4_call_sync_sequence(server->client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006583 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04006584 dprintk("NFS reply free_stateid: %d\n", status);
6585 return status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006586}
6587
Chuck Lever38527b12012-07-11 16:30:23 -04006588/**
6589 * nfs41_free_stateid - perform a FREE_STATEID operation
6590 *
6591 * @server: server / transport on which to perform the operation
6592 * @stateid: state ID to release
6593 *
6594 * Returns NFS_OK if the server freed "stateid". Otherwise a
6595 * negative NFS4ERR value is returned.
6596 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006597static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006598{
6599 struct nfs4_exception exception = { };
6600 int err;
6601 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006602 err = _nfs4_free_stateid(server, stateid);
6603 if (err != -NFS4ERR_DELAY)
6604 break;
6605 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006606 } while (exception.retry);
6607 return err;
6608}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006609
6610static bool nfs41_match_stateid(const nfs4_stateid *s1,
6611 const nfs4_stateid *s2)
6612{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006613 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006614 return false;
6615
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006616 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006617 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006618 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006619 return true;
6620
6621 return false;
6622}
6623
Andy Adamson557134a2009-04-01 09:21:53 -04006624#endif /* CONFIG_NFS_V4_1 */
6625
Trond Myklebust36281ca2012-03-04 18:13:56 -05006626static bool nfs4_match_stateid(const nfs4_stateid *s1,
6627 const nfs4_stateid *s2)
6628{
Trond Myklebustf597c532012-03-04 18:13:56 -05006629 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006630}
6631
6632
Trond Myklebust17280172012-03-11 13:11:00 -04006633static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006634 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006635 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006636 .recover_open = nfs4_open_reclaim,
6637 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006638 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006639 .get_clid_cred = nfs4_get_setclientid_cred,
Chuck Lever05f4c352012-09-14 17:24:32 -04006640 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006641};
6642
Andy Adamson591d71c2009-04-01 09:22:47 -04006643#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006644static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006645 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6646 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6647 .recover_open = nfs4_open_reclaim,
6648 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006649 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006650 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006651 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04006652 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04006653};
6654#endif /* CONFIG_NFS_V4_1 */
6655
Trond Myklebust17280172012-03-11 13:11:00 -04006656static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006657 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006658 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006659 .recover_open = nfs4_open_expired,
6660 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006661 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006662 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006663};
6664
Andy Adamson591d71c2009-04-01 09:22:47 -04006665#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006666static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006667 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6668 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006669 .recover_open = nfs41_open_expired,
6670 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006671 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006672 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006673};
6674#endif /* CONFIG_NFS_V4_1 */
6675
Trond Myklebust17280172012-03-11 13:11:00 -04006676static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006677 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006678 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006679 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006680};
6681
6682#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006683static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006684 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006685 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006686 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006687};
6688#endif
6689
Trond Myklebust97dc1352010-06-16 09:52:26 -04006690static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6691 .minor_version = 0,
6692 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006693 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006694 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006695 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6696 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6697 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006698};
6699
6700#if defined(CONFIG_NFS_V4_1)
6701static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6702 .minor_version = 1,
Bryan Schumaker104287c2012-11-12 14:13:13 -05006703 .call_sync = nfs4_call_sync_sequence,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006704 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006705 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006706 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6707 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6708 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006709};
6710#endif
6711
Trond Myklebust97dc1352010-06-16 09:52:26 -04006712const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6713 [0] = &nfs_v4_0_minor_ops,
6714#if defined(CONFIG_NFS_V4_1)
6715 [1] = &nfs_v4_1_minor_ops,
6716#endif
6717};
6718
Bryan Schumaker73a79702012-07-16 16:39:12 -04006719const struct inode_operations nfs4_dir_inode_operations = {
6720 .create = nfs_create,
6721 .lookup = nfs_lookup,
6722 .atomic_open = nfs_atomic_open,
6723 .link = nfs_link,
6724 .unlink = nfs_unlink,
6725 .symlink = nfs_symlink,
6726 .mkdir = nfs_mkdir,
6727 .rmdir = nfs_rmdir,
6728 .mknod = nfs_mknod,
6729 .rename = nfs_rename,
6730 .permission = nfs_permission,
6731 .getattr = nfs_getattr,
6732 .setattr = nfs_setattr,
6733 .getxattr = generic_getxattr,
6734 .setxattr = generic_setxattr,
6735 .listxattr = generic_listxattr,
6736 .removexattr = generic_removexattr,
6737};
6738
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006739static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006740 .permission = nfs_permission,
6741 .getattr = nfs_getattr,
6742 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006743 .getxattr = generic_getxattr,
6744 .setxattr = generic_setxattr,
6745 .listxattr = generic_listxattr,
6746 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006747};
6748
David Howells509de812006-08-22 20:06:11 -04006749const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006750 .version = 4, /* protocol version */
6751 .dentry_ops = &nfs4_dentry_operations,
6752 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006753 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006754 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006755 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04006756 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04006757 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006758 .getattr = nfs4_proc_getattr,
6759 .setattr = nfs4_proc_setattr,
6760 .lookup = nfs4_proc_lookup,
6761 .access = nfs4_proc_access,
6762 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006763 .create = nfs4_proc_create,
6764 .remove = nfs4_proc_remove,
6765 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04006766 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006767 .unlink_done = nfs4_proc_unlink_done,
6768 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006769 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04006770 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04006771 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006772 .link = nfs4_proc_link,
6773 .symlink = nfs4_proc_symlink,
6774 .mkdir = nfs4_proc_mkdir,
6775 .rmdir = nfs4_proc_remove,
6776 .readdir = nfs4_proc_readdir,
6777 .mknod = nfs4_proc_mknod,
6778 .statfs = nfs4_proc_statfs,
6779 .fsinfo = nfs4_proc_fsinfo,
6780 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006781 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006782 .decode_dirent = nfs4_decode_dirent,
6783 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04006784 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04006785 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05006786 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006787 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04006788 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04006789 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006790 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006791 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04006792 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006793 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006794 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006795 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006796 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006797 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04006798 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04006799 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04006800 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00006801 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04006802 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04006803 .create_server = nfs4_create_server,
6804 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006805};
6806
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006807static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6808 .prefix = XATTR_NAME_NFSV4_ACL,
6809 .list = nfs4_xattr_list_nfs4_acl,
6810 .get = nfs4_xattr_get_nfs4_acl,
6811 .set = nfs4_xattr_set_nfs4_acl,
6812};
6813
6814const struct xattr_handler *nfs4_xattr_handlers[] = {
6815 &nfs4_xattr_nfs4_acl_handler,
6816 NULL
6817};
6818
Linus Torvalds1da177e2005-04-16 15:20:36 -07006819/*
6820 * Local variables:
6821 * c-basic-offset: 8
6822 * End:
6823 */