blob: 7cbf7aa6c634a3ce9075acc750010aba7641908d [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 Myklebust035168ab2010-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 Myklebust035168ab2010-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 Myklebust035168ab2010-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 Myklebust035168ab2010-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 Myklebust035168ab2010-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 Myklebust035168ab2010-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 Myklebust035168ab2010-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 Myklebust035168ab2010-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
Trond Myklebustc21443c2013-02-07 14:26:21 -05001851static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
1852 fmode_t fmode,
1853 int flags,
1854 struct nfs4_state **res)
1855{
1856 struct nfs4_state_owner *sp = opendata->owner;
1857 struct nfs_server *server = sp->so_server;
1858 struct nfs4_state *state;
1859 unsigned int seq;
1860 int ret;
1861
1862 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
1863
1864 ret = _nfs4_proc_open(opendata);
1865 if (ret != 0)
1866 goto out;
1867
1868 state = nfs4_opendata_to_nfs4_state(opendata);
1869 ret = PTR_ERR(state);
1870 if (IS_ERR(state))
1871 goto out;
1872 if (server->caps & NFS_CAP_POSIX_LOCK)
1873 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
1874
1875 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
1876 if (ret != 0)
1877 goto out;
1878
1879 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
1880 nfs4_schedule_stateid_recovery(server, state);
1881 nfs4_wait_clnt_recover(server->nfs_client);
1882 }
1883 *res = state;
1884out:
1885 return ret;
1886}
1887
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001888/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001889 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 */
Andy Adamson82be4172012-05-23 05:02:35 -04001891static int _nfs4_do_open(struct inode *dir,
1892 struct dentry *dentry,
1893 fmode_t fmode,
1894 int flags,
1895 struct iattr *sattr,
1896 struct rpc_cred *cred,
1897 struct nfs4_state **res,
1898 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899{
1900 struct nfs4_state_owner *sp;
1901 struct nfs4_state *state = NULL;
1902 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001903 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001904 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
1906 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001908 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1909 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1911 goto out_err;
1912 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001913 status = nfs4_recover_expired_lease(server);
1914 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001915 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001916 if (dentry->d_inode != NULL)
1917 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001918 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001919 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001920 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001921 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
Andy Adamson82be4172012-05-23 05:02:35 -04001923 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
1924 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
1925 if (!opendata->f_attr.mdsthreshold)
1926 goto err_opendata_put;
Trond Myklebust1549210f2012-06-05 09:16:47 -04001927 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04001928 }
Al Viro82a2c1b2011-06-22 18:30:55 -04001929 if (dentry->d_inode != NULL)
1930 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001931
Trond Myklebustc21443c2013-02-07 14:26:21 -05001932 status = _nfs4_open_and_get_state(opendata, fmode, flags, &state);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001933 if (status != 0)
1934 goto err_opendata_put;
1935
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001936 if (opendata->o_arg.open_flags & O_EXCL) {
1937 nfs4_exclusive_attrset(opendata, sattr);
1938
1939 nfs_fattr_init(opendata->o_res.f_attr);
1940 status = nfs4_do_setattr(state->inode, cred,
1941 opendata->o_res.f_attr, sattr,
1942 state);
1943 if (status == 0)
1944 nfs_setattr_update_inode(state->inode, sattr);
1945 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1946 }
Andy Adamson82be4172012-05-23 05:02:35 -04001947
1948 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
1949 *ctx_th = opendata->f_attr.mdsthreshold;
1950 else
1951 kfree(opendata->f_attr.mdsthreshold);
1952 opendata->f_attr.mdsthreshold = NULL;
1953
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001954 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 *res = state;
1957 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001958err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04001959 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001960 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001961err_put_state_owner:
1962 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 *res = NULL;
1965 return status;
1966}
1967
1968
Andy Adamson82be4172012-05-23 05:02:35 -04001969static struct nfs4_state *nfs4_do_open(struct inode *dir,
1970 struct dentry *dentry,
1971 fmode_t fmode,
1972 int flags,
1973 struct iattr *sattr,
1974 struct rpc_cred *cred,
1975 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976{
1977 struct nfs4_exception exception = { };
1978 struct nfs4_state *res;
1979 int status;
1980
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001981 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 do {
Andy Adamson82be4172012-05-23 05:02:35 -04001983 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
1984 &res, ctx_th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 if (status == 0)
1986 break;
1987 /* NOTE: BAD_SEQID means the server and client disagree about the
1988 * book-keeping w.r.t. state-changing operations
1989 * (OPEN/CLOSE/LOCK/LOCKU...)
1990 * It is actually a sign of a bug on the client or on the server.
1991 *
1992 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001993 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 * have unhashed the old state_owner for us, and that we can
1995 * therefore safely retry using a new one. We should still warn
1996 * the user though...
1997 */
1998 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04001999 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002000 " returned a bad sequence-id error!\n",
2001 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 exception.retry = 1;
2003 continue;
2004 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002005 /*
2006 * BAD_STATEID on OPEN means that the server cancelled our
2007 * state before it received the OPEN_CONFIRM.
2008 * Recover by retrying the request as per the discussion
2009 * on Page 181 of RFC3530.
2010 */
2011 if (status == -NFS4ERR_BAD_STATEID) {
2012 exception.retry = 1;
2013 continue;
2014 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002015 if (status == -EAGAIN) {
2016 /* We must have found a delegation */
2017 exception.retry = 1;
2018 continue;
2019 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
2021 status, &exception));
2022 } while (exception.retry);
2023 return res;
2024}
2025
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002026static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2027 struct nfs_fattr *fattr, struct iattr *sattr,
2028 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002030 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002032 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 .iap = sattr,
2034 .server = server,
2035 .bitmask = server->attr_bitmask,
2036 };
2037 struct nfs_setattrres res = {
2038 .fattr = fattr,
2039 .server = server,
2040 };
2041 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002042 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2043 .rpc_argp = &arg,
2044 .rpc_resp = &res,
2045 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002047 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04002048 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049
Trond Myklebust0e574af2005-10-27 22:12:38 -04002050 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
Trond Myklebust4fc87962012-03-08 17:42:01 -05002052 if (state != NULL) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002053 struct nfs_lockowner lockowner = {
2054 .l_owner = current->files,
2055 .l_pid = current->tgid,
2056 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002057 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002058 &lockowner);
Trond Myklebust4fc87962012-03-08 17:42:01 -05002059 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2060 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002061 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002062 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002063 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
Bryan Schumaker7c513052011-03-24 17:12:24 +00002065 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002066 if (status == 0 && state != NULL)
2067 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002068 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069}
2070
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002071static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2072 struct nfs_fattr *fattr, struct iattr *sattr,
2073 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002075 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002076 struct nfs4_exception exception = {
2077 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002078 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002079 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 int err;
2081 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04002082 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2083 switch (err) {
2084 case -NFS4ERR_OPENMODE:
2085 if (state && !(state->state & FMODE_WRITE)) {
2086 err = -EBADF;
2087 if (sattr->ia_valid & ATTR_OPEN)
2088 err = -EACCES;
2089 goto out;
2090 }
2091 }
2092 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002094out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 return err;
2096}
2097
2098struct nfs4_closedata {
2099 struct inode *inode;
2100 struct nfs4_state *state;
2101 struct nfs_closeargs arg;
2102 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002103 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002104 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002105 bool roc;
2106 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107};
2108
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002109static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002110{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002111 struct nfs4_closedata *calldata = data;
2112 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002113 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002114
Fred Isamanf7e89172011-01-06 11:36:32 +00002115 if (calldata->roc)
2116 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002117 nfs4_put_open_state(calldata->state);
2118 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002119 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002120 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002121 kfree(calldata);
2122}
2123
Trond Myklebust88069f72009-12-08 08:33:16 -05002124static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2125 fmode_t fmode)
2126{
2127 spin_lock(&state->owner->so_lock);
2128 if (!(fmode & FMODE_READ))
2129 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2130 if (!(fmode & FMODE_WRITE))
2131 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2132 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2133 spin_unlock(&state->owner->so_lock);
2134}
2135
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002136static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002138 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 struct nfs_server *server = NFS_SERVER(calldata->inode);
2141
Chuck Levera3ca5652012-03-01 17:00:40 -05002142 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002143 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2144 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 /* hmm. we are done with the inode, and in the process of freeing
2146 * the state_owner. we keep this around to process errors
2147 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 switch (task->tk_status) {
2149 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002150 if (calldata->roc)
2151 pnfs_roc_set_barrier(state->inode,
2152 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002153 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002154 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002155 nfs4_close_clear_stateid_flags(state,
2156 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 break;
2158 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002159 case -NFS4ERR_OLD_STATEID:
2160 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002162 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002163 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002165 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2166 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002168 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002169 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002170 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171}
2172
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002173static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002175 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002176 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002177 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002178 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002179
Chuck Levera3ca5652012-03-01 17:00:40 -05002180 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002181 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07002182 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04002183
Trond Myklebust88069f72009-12-08 08:33:16 -05002184 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2185 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002186 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002187 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002188 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002189 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002190 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2191 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2192 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002193 }
2194 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002195 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2196 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2197 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002198 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002199 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002200 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002201
2202 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002203 /* Note: exit _without_ calling nfs4_close_done */
2204 task->tk_action = NULL;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002205 nfs4_sequence_done(task, &calldata->res.seq_res);
Chuck Levera3ca5652012-03-01 17:00:40 -05002206 goto out;
Trond Myklebust95121352005-10-18 14:20:12 -07002207 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002208
Fred Isamanf7e89172011-01-06 11:36:32 +00002209 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002210 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002211 if (calldata->roc &&
Trond Myklebust7fdab062012-09-20 20:15:57 -04002212 pnfs_roc_drain(inode, &calldata->roc_barrier, task))
Chuck Levera3ca5652012-03-01 17:00:40 -05002213 goto out;
Fred Isamanf7e89172011-01-06 11:36:32 +00002214 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002215
Trond Myklebust516a6af2005-10-27 22:12:41 -04002216 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002217 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002218 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002219 &calldata->arg.seq_args,
2220 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002221 task) != 0)
2222 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002223out:
2224 dprintk("%s: done!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225}
2226
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002227static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002228 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002229 .rpc_call_done = nfs4_close_done,
2230 .rpc_release = nfs4_free_closedata,
2231};
2232
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233/*
2234 * It is possible for data to be read/written from a mem-mapped file
2235 * after the sys_close call (which hits the vfs layer as a flush).
2236 * This means that we can't safely call nfsv4 close on a file until
2237 * the inode is cleared. This in turn means that we are not good
2238 * NFSv4 citizens - we do not indicate to the server to update the file's
2239 * share state even when we are done with one of the three share
2240 * stateid's in the inode.
2241 *
2242 * NOTE: Caller must be holding the sp->so_owner semaphore!
2243 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002244int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002246 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002248 struct nfs4_state_owner *sp = state->owner;
2249 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002250 struct rpc_message msg = {
2251 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2252 .rpc_cred = state->owner->so_cred,
2253 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002254 struct rpc_task_setup task_setup_data = {
2255 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002256 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002257 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002258 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002259 .flags = RPC_TASK_ASYNC,
2260 };
Trond Myklebust95121352005-10-18 14:20:12 -07002261 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002263 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002265 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002266 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002267 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002269 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002270 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002272 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002273 if (calldata->arg.seqid == NULL)
2274 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002275 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002276 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002277 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002278 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002279 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002280 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002281 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002282
Trond Myklebust1174dd12010-12-21 10:52:24 -05002283 msg.rpc_argp = &calldata->arg;
2284 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002285 task_setup_data.callback_data = calldata;
2286 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002287 if (IS_ERR(task))
2288 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002289 status = 0;
2290 if (wait)
2291 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002292 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002293 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002294out_free_calldata:
2295 kfree(calldata);
2296out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002297 nfs4_put_open_state(state);
2298 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002299 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300}
2301
Trond Myklebust2b484292010-09-17 10:56:51 -04002302static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002303nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 struct nfs4_state *state;
2306
Trond Myklebust565277f2007-10-15 18:17:53 -04002307 /* Protect against concurrent sillydeletes */
Andy Adamson82be4172012-05-23 05:02:35 -04002308 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2309 ctx->cred, &ctx->mdsthreshold);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002310 if (IS_ERR(state))
2311 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002312 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002313 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314}
2315
Trond Myklebust1185a552009-12-03 15:54:02 -05002316static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002317{
2318 if (ctx->state == NULL)
2319 return;
2320 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002321 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002322 else
Al Viro643168c2011-06-22 18:20:23 -04002323 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002324}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325
2326static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2327{
Benny Halevy43652ad2009-04-01 09:21:54 -04002328 struct nfs4_server_caps_arg args = {
2329 .fhandle = fhandle,
2330 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 struct nfs4_server_caps_res res = {};
2332 struct rpc_message msg = {
2333 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002334 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 .rpc_resp = &res,
2336 };
2337 int status;
2338
Bryan Schumaker7c513052011-03-24 17:12:24 +00002339 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 if (status == 0) {
2341 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04002342 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2343 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2344 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2345 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2346 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2348 server->caps |= NFS_CAP_ACLS;
2349 if (res.has_links != 0)
2350 server->caps |= NFS_CAP_HARDLINKS;
2351 if (res.has_symlinks != 0)
2352 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04002353 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2354 server->caps |= NFS_CAP_FILEID;
2355 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2356 server->caps |= NFS_CAP_MODE;
2357 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2358 server->caps |= NFS_CAP_NLINK;
2359 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2360 server->caps |= NFS_CAP_OWNER;
2361 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2362 server->caps |= NFS_CAP_OWNER_GROUP;
2363 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2364 server->caps |= NFS_CAP_ATIME;
2365 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2366 server->caps |= NFS_CAP_CTIME;
2367 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2368 server->caps |= NFS_CAP_MTIME;
2369
Trond Myklebusta65318b2009-03-11 14:10:28 -04002370 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2371 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2372 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002374 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002376
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 return status;
2378}
2379
Trond Myklebust55a97592006-06-09 09:34:19 -04002380int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381{
2382 struct nfs4_exception exception = { };
2383 int err;
2384 do {
2385 err = nfs4_handle_exception(server,
2386 _nfs4_server_capabilities(server, fhandle),
2387 &exception);
2388 } while (exception.retry);
2389 return err;
2390}
2391
2392static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2393 struct nfs_fsinfo *info)
2394{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 struct nfs4_lookup_root_arg args = {
2396 .bitmask = nfs4_fattr_bitmap,
2397 };
2398 struct nfs4_lookup_res res = {
2399 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002400 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 .fh = fhandle,
2402 };
2403 struct rpc_message msg = {
2404 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2405 .rpc_argp = &args,
2406 .rpc_resp = &res,
2407 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002408
Trond Myklebust0e574af2005-10-27 22:12:38 -04002409 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002410 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411}
2412
2413static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2414 struct nfs_fsinfo *info)
2415{
2416 struct nfs4_exception exception = { };
2417 int err;
2418 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002419 err = _nfs4_lookup_root(server, fhandle, info);
2420 switch (err) {
2421 case 0:
2422 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002423 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002424 default:
2425 err = nfs4_handle_exception(server, err, &exception);
2426 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002428out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 return err;
2430}
2431
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002432static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2433 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2434{
2435 struct rpc_auth *auth;
2436 int ret;
2437
2438 auth = rpcauth_create(flavor, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002439 if (IS_ERR(auth)) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002440 ret = -EIO;
2441 goto out;
2442 }
2443 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002444out:
2445 return ret;
2446}
2447
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002448static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002449 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002451 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002452 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002454 len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04002455 if (len < 0)
2456 return len;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002457
2458 for (i = 0; i < len; i++) {
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002459 /* AUTH_UNIX is the default flavor if none was specified,
2460 * thus has already been tried. */
2461 if (flav_array[i] == RPC_AUTH_UNIX)
2462 continue;
2463
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002464 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002465 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002466 continue;
2467 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002468 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002469 /*
2470 * -EACCESS could mean that the user doesn't have correct permissions
2471 * to access the mount. It could also mean that we tried to mount
2472 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2473 * existing mount programs don't handle -EACCES very well so it should
2474 * be mapped to -EPERM instead.
2475 */
2476 if (status == -EACCES)
2477 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002478 return status;
2479}
2480
2481/*
2482 * get the file handle for the "/" directory on the server
2483 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002484int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2485 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002486{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002487 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002488 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002489 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2490 /*
2491 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2492 * by nfs4_map_errors() as this function exits.
2493 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002494 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 if (status == 0)
2496 status = nfs4_server_capabilities(server, fhandle);
2497 if (status == 0)
2498 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002499 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500}
2501
Bryan Schumakerbae36242012-05-10 15:07:31 -04002502static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2503 struct nfs_fsinfo *info)
2504{
2505 int error;
2506 struct nfs_fattr *fattr = info->fattr;
2507
2508 error = nfs4_server_capabilities(server, mntfh);
2509 if (error < 0) {
2510 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2511 return error;
2512 }
2513
2514 error = nfs4_proc_getattr(server, mntfh, fattr);
2515 if (error < 0) {
2516 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2517 return error;
2518 }
2519
2520 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2521 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2522 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2523
2524 return error;
2525}
2526
Manoj Naik6b97fd32006-06-09 09:34:29 -04002527/*
2528 * Get locations and (maybe) other attributes of a referral.
2529 * Note that we'll actually follow the referral later when
2530 * we detect fsid mismatch in inode revalidation
2531 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002532static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2533 const struct qstr *name, struct nfs_fattr *fattr,
2534 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002535{
2536 int status = -ENOMEM;
2537 struct page *page = NULL;
2538 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002539
2540 page = alloc_page(GFP_KERNEL);
2541 if (page == NULL)
2542 goto out;
2543 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2544 if (locations == NULL)
2545 goto out;
2546
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002547 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002548 if (status != 0)
2549 goto out;
2550 /* Make sure server returned a different fsid for the referral */
2551 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002552 dprintk("%s: server did not return a different fsid for"
2553 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002554 status = -EIO;
2555 goto out;
2556 }
Andy Adamson533eb462011-06-13 18:25:56 -04002557 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2558 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002559
Andy Adamson533eb462011-06-13 18:25:56 -04002560 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002561 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002562 memset(fhandle, 0, sizeof(struct nfs_fh));
2563out:
2564 if (page)
2565 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002566 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002567 return status;
2568}
2569
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2571{
2572 struct nfs4_getattr_arg args = {
2573 .fh = fhandle,
2574 .bitmask = server->attr_bitmask,
2575 };
2576 struct nfs4_getattr_res res = {
2577 .fattr = fattr,
2578 .server = server,
2579 };
2580 struct rpc_message msg = {
2581 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2582 .rpc_argp = &args,
2583 .rpc_resp = &res,
2584 };
2585
Trond Myklebust0e574af2005-10-27 22:12:38 -04002586 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002587 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588}
2589
2590static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2591{
2592 struct nfs4_exception exception = { };
2593 int err;
2594 do {
2595 err = nfs4_handle_exception(server,
2596 _nfs4_proc_getattr(server, fhandle, fattr),
2597 &exception);
2598 } while (exception.retry);
2599 return err;
2600}
2601
2602/*
2603 * The file is not closed if it is opened due to the a request to change
2604 * the size of the file. The open call will not be needed once the
2605 * VFS layer lookup-intents are implemented.
2606 *
2607 * Close is called when the inode is destroyed.
2608 * If we haven't opened the file for O_WRONLY, we
2609 * need to in the size_change case to obtain a stateid.
2610 *
2611 * Got race?
2612 * Because OPEN is always done by name in nfsv4, it is
2613 * possible that we opened a different file by the same
2614 * name. We can recognize this race condition, but we
2615 * can't do anything about it besides returning an error.
2616 *
2617 * This will be fixed with VFS changes (lookup-intent).
2618 */
2619static int
2620nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2621 struct iattr *sattr)
2622{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002623 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002624 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002625 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 int status;
2627
Benny Halevy8a1636c2010-07-14 15:43:57 -04002628 if (pnfs_ld_layoutret_on_setattr(inode))
2629 pnfs_return_layout(inode);
2630
Trond Myklebust0e574af2005-10-27 22:12:38 -04002631 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632
Andy Adamson26699402012-05-30 16:12:24 -04002633 /* Deal with open(O_TRUNC) */
2634 if (sattr->ia_valid & ATTR_OPEN)
2635 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2636
2637 /* Optimization: if the end result is no change, don't RPC */
2638 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2639 return 0;
2640
Trond Myklebustd5308382005-11-04 15:33:38 -05002641 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002642 if (sattr->ia_valid & ATTR_FILE) {
2643 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002644
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002645 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002646 if (ctx) {
2647 cred = ctx->cred;
2648 state = ctx->state;
2649 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002650 }
2651
2652 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002653 if (status == 0)
2654 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 return status;
2656}
2657
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002658static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2659 const struct qstr *name, struct nfs_fh *fhandle,
2660 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002661{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002662 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002663 int status;
2664 struct nfs4_lookup_arg args = {
2665 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002666 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002667 .name = name,
2668 };
2669 struct nfs4_lookup_res res = {
2670 .server = server,
2671 .fattr = fattr,
2672 .fh = fhandle,
2673 };
2674 struct rpc_message msg = {
2675 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2676 .rpc_argp = &args,
2677 .rpc_resp = &res,
2678 };
2679
2680 nfs_fattr_init(fattr);
2681
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002683 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 dprintk("NFS reply lookup: %d\n", status);
2685 return status;
2686}
2687
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002688static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002689{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002690 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002691 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002692 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2693 fattr->nlink = 2;
2694}
2695
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002696static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2697 struct qstr *name, struct nfs_fh *fhandle,
2698 struct nfs_fattr *fattr)
2699{
2700 struct nfs4_exception exception = { };
2701 struct rpc_clnt *client = *clnt;
2702 int err;
2703 do {
2704 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2705 switch (err) {
2706 case -NFS4ERR_BADNAME:
2707 err = -ENOENT;
2708 goto out;
2709 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002710 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002711 goto out;
2712 case -NFS4ERR_WRONGSEC:
2713 err = -EPERM;
2714 if (client != *clnt)
2715 goto out;
2716
2717 client = nfs4_create_sec_client(client, dir, name);
2718 if (IS_ERR(client))
2719 return PTR_ERR(client);
2720
2721 exception.retry = 1;
2722 break;
2723 default:
2724 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2725 }
2726 } while (exception.retry);
2727
2728out:
2729 if (err == 0)
2730 *clnt = client;
2731 else if (client != *clnt)
2732 rpc_shutdown_client(client);
2733
2734 return err;
2735}
2736
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002737static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002738 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002740 int status;
2741 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002742
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002743 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2744 if (client != NFS_CLIENT(dir)) {
2745 rpc_shutdown_client(client);
2746 nfs_fixup_secinfo_attributes(fattr);
2747 }
2748 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749}
2750
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002751struct rpc_clnt *
2752nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2753 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2754{
2755 int status;
2756 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2757
2758 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2759 if (status < 0) {
2760 rpc_shutdown_client(client);
2761 return ERR_PTR(status);
2762 }
2763 return client;
2764}
2765
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2767{
Trond Myklebust76b32992007-08-10 17:45:11 -04002768 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 struct nfs4_accessargs args = {
2770 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002771 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002773 struct nfs4_accessres res = {
2774 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002775 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 struct rpc_message msg = {
2777 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2778 .rpc_argp = &args,
2779 .rpc_resp = &res,
2780 .rpc_cred = entry->cred,
2781 };
2782 int mode = entry->mask;
2783 int status;
2784
2785 /*
2786 * Determine which access bits we want to ask for...
2787 */
2788 if (mode & MAY_READ)
2789 args.access |= NFS4_ACCESS_READ;
2790 if (S_ISDIR(inode->i_mode)) {
2791 if (mode & MAY_WRITE)
2792 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2793 if (mode & MAY_EXEC)
2794 args.access |= NFS4_ACCESS_LOOKUP;
2795 } else {
2796 if (mode & MAY_WRITE)
2797 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2798 if (mode & MAY_EXEC)
2799 args.access |= NFS4_ACCESS_EXECUTE;
2800 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002801
2802 res.fattr = nfs_alloc_fattr();
2803 if (res.fattr == NULL)
2804 return -ENOMEM;
2805
Bryan Schumaker7c513052011-03-24 17:12:24 +00002806 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002808 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04002809 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002811 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 return status;
2813}
2814
2815static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2816{
2817 struct nfs4_exception exception = { };
2818 int err;
2819 do {
2820 err = nfs4_handle_exception(NFS_SERVER(inode),
2821 _nfs4_proc_access(inode, entry),
2822 &exception);
2823 } while (exception.retry);
2824 return err;
2825}
2826
2827/*
2828 * TODO: For the time being, we don't try to get any attributes
2829 * along with any of the zero-copy operations READ, READDIR,
2830 * READLINK, WRITE.
2831 *
2832 * In the case of the first three, we want to put the GETATTR
2833 * after the read-type operation -- this is because it is hard
2834 * to predict the length of a GETATTR response in v4, and thus
2835 * align the READ data correctly. This means that the GETATTR
2836 * may end up partially falling into the page cache, and we should
2837 * shift it into the 'tail' of the xdr_buf before processing.
2838 * To do this efficiently, we need to know the total length
2839 * of data received, which doesn't seem to be available outside
2840 * of the RPC layer.
2841 *
2842 * In the case of WRITE, we also want to put the GETATTR after
2843 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04002844 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 *
2846 * Both of these changes to the XDR layer would in fact be quite
2847 * minor, but I decided to leave them for a subsequent patch.
2848 */
2849static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2850 unsigned int pgbase, unsigned int pglen)
2851{
2852 struct nfs4_readlink args = {
2853 .fh = NFS_FH(inode),
2854 .pgbase = pgbase,
2855 .pglen = pglen,
2856 .pages = &page,
2857 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002858 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 struct rpc_message msg = {
2860 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2861 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002862 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 };
2864
Bryan Schumaker7c513052011-03-24 17:12:24 +00002865 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 -07002866}
2867
2868static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2869 unsigned int pgbase, unsigned int pglen)
2870{
2871 struct nfs4_exception exception = { };
2872 int err;
2873 do {
2874 err = nfs4_handle_exception(NFS_SERVER(inode),
2875 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2876 &exception);
2877 } while (exception.retry);
2878 return err;
2879}
2880
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002882 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884static int
2885nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002886 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887{
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002888 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 int status = 0;
2891
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002892 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
2893 if (IS_ERR(ctx))
2894 return PTR_ERR(ctx);
2895
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002896 sattr->ia_mode &= ~current_umask();
Trond Myklebust8626e4a2012-07-16 12:01:42 -04002897 state = nfs4_do_open(dir, dentry, ctx->mode,
2898 flags, sattr, ctx->cred,
2899 &ctx->mdsthreshold);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002900 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 if (IS_ERR(state)) {
2902 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002903 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002905 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002906 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002907 ctx->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908out:
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002909 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 return status;
2911}
2912
2913static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2914{
Trond Myklebust16e42952005-10-27 22:12:44 -04002915 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002916 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07002918 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002920 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002921 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002922 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002924 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2925 .rpc_argp = &args,
2926 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 };
Trond Myklebust778d2812012-04-27 13:48:19 -04002928 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04002929
Bryan Schumaker7c513052011-03-24 17:12:24 +00002930 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04002931 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04002932 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 return status;
2934}
2935
2936static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2937{
2938 struct nfs4_exception exception = { };
2939 int err;
2940 do {
2941 err = nfs4_handle_exception(NFS_SERVER(dir),
2942 _nfs4_proc_remove(dir, name),
2943 &exception);
2944 } while (exception.retry);
2945 return err;
2946}
2947
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002948static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002950 struct nfs_server *server = NFS_SERVER(dir);
2951 struct nfs_removeargs *args = msg->rpc_argp;
2952 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002954 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05002956 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957}
2958
Bryan Schumaker34e137c2012-03-19 14:54:41 -04002959static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
2960{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04002961 nfs4_setup_sequence(NFS_SERVER(data->dir),
2962 &data->args.seq_args,
2963 &data->res.seq_res,
2964 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965}
2966
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002967static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002969 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2970
Trond Myklebust14516c32010-07-31 14:29:06 -04002971 if (!nfs4_sequence_done(task, &res->seq_res))
2972 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002973 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002974 return 0;
2975 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002976 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977}
2978
Jeff Laytond3d41522010-09-17 17:31:57 -04002979static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2980{
2981 struct nfs_server *server = NFS_SERVER(dir);
2982 struct nfs_renameargs *arg = msg->rpc_argp;
2983 struct nfs_renameres *res = msg->rpc_resp;
2984
2985 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04002986 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002987 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04002988}
2989
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04002990static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
2991{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04002992 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
2993 &data->args.seq_args,
2994 &data->res.seq_res,
2995 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04002996}
2997
2998static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2999 struct inode *new_dir)
3000{
3001 struct nfs_renameres *res = task->tk_msg.rpc_resp;
3002
3003 if (!nfs4_sequence_done(task, &res->seq_res))
3004 return 0;
3005 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3006 return 0;
3007
3008 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003009 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003010 return 1;
3011}
3012
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3014 struct inode *new_dir, struct qstr *new_name)
3015{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003016 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04003017 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 .old_dir = NFS_FH(old_dir),
3019 .new_dir = NFS_FH(new_dir),
3020 .old_name = old_name,
3021 .new_name = new_name,
3022 };
Jeff Laytone8582a82010-09-17 17:31:06 -04003023 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003024 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003025 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 struct rpc_message msg = {
3027 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3028 .rpc_argp = &arg,
3029 .rpc_resp = &res,
3030 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003031 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032
Bryan Schumaker7c513052011-03-24 17:12:24 +00003033 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 if (!status) {
3035 update_changeattr(old_dir, &res.old_cinfo);
3036 update_changeattr(new_dir, &res.new_cinfo);
3037 }
3038 return status;
3039}
3040
3041static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3042 struct inode *new_dir, struct qstr *new_name)
3043{
3044 struct nfs4_exception exception = { };
3045 int err;
3046 do {
3047 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3048 _nfs4_proc_rename(old_dir, old_name,
3049 new_dir, new_name),
3050 &exception);
3051 } while (exception.retry);
3052 return err;
3053}
3054
3055static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3056{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003057 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 struct nfs4_link_arg arg = {
3059 .fh = NFS_FH(inode),
3060 .dir_fh = NFS_FH(dir),
3061 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003062 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003064 struct nfs4_link_res res = {
3065 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003066 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 struct rpc_message msg = {
3068 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3069 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003070 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003072 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073
Trond Myklebust136f2622010-04-16 16:22:49 -04003074 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003075 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003076 goto out;
3077
Bryan Schumaker7c513052011-03-24 17:12:24 +00003078 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003079 if (!status) {
3080 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04003081 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003082 }
Trond Myklebust136f2622010-04-16 16:22:49 -04003083out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003084 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 return status;
3086}
3087
3088static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3089{
3090 struct nfs4_exception exception = { };
3091 int err;
3092 do {
3093 err = nfs4_handle_exception(NFS_SERVER(inode),
3094 _nfs4_proc_link(inode, dir, name),
3095 &exception);
3096 } while (exception.retry);
3097 return err;
3098}
3099
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003100struct nfs4_createdata {
3101 struct rpc_message msg;
3102 struct nfs4_create_arg arg;
3103 struct nfs4_create_res res;
3104 struct nfs_fh fh;
3105 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003106};
3107
3108static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3109 struct qstr *name, struct iattr *sattr, u32 ftype)
3110{
3111 struct nfs4_createdata *data;
3112
3113 data = kzalloc(sizeof(*data), GFP_KERNEL);
3114 if (data != NULL) {
3115 struct nfs_server *server = NFS_SERVER(dir);
3116
3117 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3118 data->msg.rpc_argp = &data->arg;
3119 data->msg.rpc_resp = &data->res;
3120 data->arg.dir_fh = NFS_FH(dir);
3121 data->arg.server = server;
3122 data->arg.name = name;
3123 data->arg.attrs = sattr;
3124 data->arg.ftype = ftype;
3125 data->arg.bitmask = server->attr_bitmask;
3126 data->res.server = server;
3127 data->res.fh = &data->fh;
3128 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003129 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003130 }
3131 return data;
3132}
3133
3134static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3135{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003136 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003137 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003138 if (status == 0) {
3139 update_changeattr(dir, &data->res.dir_cinfo);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003140 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3141 }
3142 return status;
3143}
3144
3145static void nfs4_free_createdata(struct nfs4_createdata *data)
3146{
3147 kfree(data);
3148}
3149
Chuck Lever4f390c12006-08-22 20:06:22 -04003150static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003151 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003153 struct nfs4_createdata *data;
3154 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155
Chuck Lever94a6d752006-08-22 20:06:23 -04003156 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003157 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003158
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003159 status = -ENOMEM;
3160 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3161 if (data == NULL)
3162 goto out;
3163
3164 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3165 data->arg.u.symlink.pages = &page;
3166 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003168 status = nfs4_do_create(dir, dentry, data);
3169
3170 nfs4_free_createdata(data);
3171out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 return status;
3173}
3174
Chuck Lever4f390c12006-08-22 20:06:22 -04003175static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003176 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177{
3178 struct nfs4_exception exception = { };
3179 int err;
3180 do {
3181 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003182 _nfs4_proc_symlink(dir, dentry, page,
3183 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 &exception);
3185 } while (exception.retry);
3186 return err;
3187}
3188
3189static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3190 struct iattr *sattr)
3191{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003192 struct nfs4_createdata *data;
3193 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003195 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3196 if (data == NULL)
3197 goto out;
3198
3199 status = nfs4_do_create(dir, dentry, data);
3200
3201 nfs4_free_createdata(data);
3202out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 return status;
3204}
3205
3206static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3207 struct iattr *sattr)
3208{
3209 struct nfs4_exception exception = { };
3210 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003211
3212 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 do {
3214 err = nfs4_handle_exception(NFS_SERVER(dir),
3215 _nfs4_proc_mkdir(dir, dentry, sattr),
3216 &exception);
3217 } while (exception.retry);
3218 return err;
3219}
3220
3221static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003222 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223{
3224 struct inode *dir = dentry->d_inode;
3225 struct nfs4_readdir_arg args = {
3226 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003227 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 .pgbase = 0,
3229 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003230 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003231 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 };
3233 struct nfs4_readdir_res res;
3234 struct rpc_message msg = {
3235 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3236 .rpc_argp = &args,
3237 .rpc_resp = &res,
3238 .rpc_cred = cred,
3239 };
3240 int status;
3241
Harvey Harrison3110ff82008-05-02 13:42:44 -07003242 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003243 dentry->d_parent->d_name.name,
3244 dentry->d_name.name,
3245 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003246 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003248 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 -05003249 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003250 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003251 status += args.pgbase;
3252 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003253
3254 nfs_invalidate_atime(dir);
3255
Harvey Harrison3110ff82008-05-02 13:42:44 -07003256 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 return status;
3258}
3259
3260static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003261 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262{
3263 struct nfs4_exception exception = { };
3264 int err;
3265 do {
3266 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3267 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003268 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 &exception);
3270 } while (exception.retry);
3271 return err;
3272}
3273
3274static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3275 struct iattr *sattr, dev_t rdev)
3276{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003277 struct nfs4_createdata *data;
3278 int mode = sattr->ia_mode;
3279 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003281 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3282 if (data == NULL)
3283 goto out;
3284
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003286 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003288 data->arg.ftype = NF4BLK;
3289 data->arg.u.device.specdata1 = MAJOR(rdev);
3290 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 }
3292 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003293 data->arg.ftype = NF4CHR;
3294 data->arg.u.device.specdata1 = MAJOR(rdev);
3295 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003296 } else if (!S_ISSOCK(mode)) {
3297 status = -EINVAL;
3298 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003301 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003302out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003303 nfs4_free_createdata(data);
3304out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 return status;
3306}
3307
3308static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3309 struct iattr *sattr, dev_t rdev)
3310{
3311 struct nfs4_exception exception = { };
3312 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003313
3314 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 do {
3316 err = nfs4_handle_exception(NFS_SERVER(dir),
3317 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3318 &exception);
3319 } while (exception.retry);
3320 return err;
3321}
3322
3323static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3324 struct nfs_fsstat *fsstat)
3325{
3326 struct nfs4_statfs_arg args = {
3327 .fh = fhandle,
3328 .bitmask = server->attr_bitmask,
3329 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003330 struct nfs4_statfs_res res = {
3331 .fsstat = fsstat,
3332 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 struct rpc_message msg = {
3334 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3335 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003336 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 };
3338
Trond Myklebust0e574af2005-10-27 22:12:38 -04003339 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003340 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341}
3342
3343static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3344{
3345 struct nfs4_exception exception = { };
3346 int err;
3347 do {
3348 err = nfs4_handle_exception(server,
3349 _nfs4_proc_statfs(server, fhandle, fsstat),
3350 &exception);
3351 } while (exception.retry);
3352 return err;
3353}
3354
3355static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3356 struct nfs_fsinfo *fsinfo)
3357{
3358 struct nfs4_fsinfo_arg args = {
3359 .fh = fhandle,
3360 .bitmask = server->attr_bitmask,
3361 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003362 struct nfs4_fsinfo_res res = {
3363 .fsinfo = fsinfo,
3364 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 struct rpc_message msg = {
3366 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3367 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003368 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 };
3370
Bryan Schumaker7c513052011-03-24 17:12:24 +00003371 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372}
3373
3374static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3375{
3376 struct nfs4_exception exception = { };
3377 int err;
3378
3379 do {
3380 err = nfs4_handle_exception(server,
3381 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3382 &exception);
3383 } while (exception.retry);
3384 return err;
3385}
3386
3387static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3388{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003389 int error;
3390
Trond Myklebust0e574af2005-10-27 22:12:38 -04003391 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003392 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003393 if (error == 0) {
3394 /* block layout checks this! */
3395 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003396 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003397 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003398
3399 return error;
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_pathconf_arg args = {
3406 .fh = fhandle,
3407 .bitmask = server->attr_bitmask,
3408 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003409 struct nfs4_pathconf_res res = {
3410 .pathconf = pathconf,
3411 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 struct rpc_message msg = {
3413 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3414 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003415 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 };
3417
3418 /* None of the pathconf attributes are mandatory to implement */
3419 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3420 memset(pathconf, 0, sizeof(*pathconf));
3421 return 0;
3422 }
3423
Trond Myklebust0e574af2005-10-27 22:12:38 -04003424 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003425 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426}
3427
3428static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3429 struct nfs_pathconf *pathconf)
3430{
3431 struct nfs4_exception exception = { };
3432 int err;
3433
3434 do {
3435 err = nfs4_handle_exception(server,
3436 _nfs4_proc_pathconf(server, fhandle, pathconf),
3437 &exception);
3438 } while (exception.retry);
3439 return err;
3440}
3441
Benny Halevyd20581a2011-05-22 19:52:03 +03003442void __nfs4_read_done_cb(struct nfs_read_data *data)
3443{
Fred Isamancd841602012-04-20 14:47:44 -04003444 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003445}
3446
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003447static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448{
Fred Isamancd841602012-04-20 14:47:44 -04003449 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003451 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003452 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003453 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003455
Benny Halevyd20581a2011-05-22 19:52:03 +03003456 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003458 renew_lease(server, data->timestamp);
3459 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460}
3461
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003462static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3463{
3464
3465 dprintk("--> %s\n", __func__);
3466
3467 if (!nfs4_sequence_done(task, &data->res.seq_res))
3468 return -EAGAIN;
3469
Benny Halevyd20581a2011-05-22 19:52:03 +03003470 return data->read_done_cb ? data->read_done_cb(task, data) :
3471 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003472}
3473
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003474static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476 data->timestamp = jiffies;
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003477 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003478 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003479 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480}
3481
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003482static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3483{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003484 nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3485 &data->args.seq_args,
3486 &data->res.seq_res,
3487 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488}
3489
Fred Isamanb029bc92011-03-03 15:13:42 +00003490static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491{
Fred Isamancd841602012-04-20 14:47:44 -04003492 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003494 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003495 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003496 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003498 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003500 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003501 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003502 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503}
3504
Fred Isamanb029bc92011-03-03 15:13:42 +00003505static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3506{
3507 if (!nfs4_sequence_done(task, &data->res.seq_res))
3508 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003509 return data->write_done_cb ? data->write_done_cb(task, data) :
3510 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003511}
3512
Trond Myklebust5a37f852012-04-28 14:55:16 -04003513static
3514bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003515{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003516 const struct nfs_pgio_header *hdr = data->header;
3517
3518 /* Don't request attributes for pNFS or O_DIRECT writes */
3519 if (data->ds_clp != NULL || hdr->dreq != NULL)
3520 return false;
3521 /* Otherwise, request attributes if and only if we don't hold
3522 * a delegation
3523 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04003524 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003525}
Fred Isamana69aef12011-03-03 15:13:47 +00003526
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003527static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528{
Fred Isamancd841602012-04-20 14:47:44 -04003529 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003530
Trond Myklebust5a37f852012-04-28 14:55:16 -04003531 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003532 data->args.bitmask = NULL;
3533 data->res.fattr = NULL;
3534 } else
3535 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003536
Fred Isamanb029bc92011-03-03 15:13:42 +00003537 if (!data->write_done_cb)
3538 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003539 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 data->timestamp = jiffies;
3541
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003542 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003543 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544}
3545
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003546static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3547{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003548 nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3549 &data->args.seq_args,
3550 &data->res.seq_res,
3551 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552}
3553
Fred Isaman0b7c0152012-04-20 14:47:39 -04003554static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3555{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003556 nfs4_setup_sequence(NFS_SERVER(data->inode),
3557 &data->args.seq_args,
3558 &data->res.seq_res,
3559 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560}
3561
Fred Isaman0b7c0152012-04-20 14:47:39 -04003562static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003565
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003566 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003567 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003568 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003570 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571}
3572
Fred Isaman0b7c0152012-04-20 14:47:39 -04003573static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003574{
3575 if (!nfs4_sequence_done(task, &data->res.seq_res))
3576 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003577 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003578}
3579
Fred Isaman0b7c0152012-04-20 14:47:39 -04003580static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003582 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003583
Fred Isaman0b7c0152012-04-20 14:47:39 -04003584 if (data->commit_done_cb == NULL)
3585 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003586 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003587 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003588 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589}
3590
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003591struct nfs4_renewdata {
3592 struct nfs_client *client;
3593 unsigned long timestamp;
3594};
3595
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596/*
3597 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3598 * standalone procedure for queueing an asynchronous RENEW.
3599 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003600static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003601{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003602 struct nfs4_renewdata *data = calldata;
3603 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003604
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003605 if (atomic_read(&clp->cl_count) > 1)
3606 nfs4_schedule_state_renewal(clp);
3607 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003608 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003609}
3610
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003611static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003613 struct nfs4_renewdata *data = calldata;
3614 struct nfs_client *clp = data->client;
3615 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616
3617 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003618 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003619 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3620 return;
3621 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003622 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003623 return;
3624 }
3625 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003627 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628}
3629
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003630static const struct rpc_call_ops nfs4_renew_ops = {
3631 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003632 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003633};
3634
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003635static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636{
3637 struct rpc_message msg = {
3638 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3639 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003640 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003642 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003644 if (renew_flags == 0)
3645 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003646 if (!atomic_inc_not_zero(&clp->cl_count))
3647 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003648 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003649 if (data == NULL)
3650 return -ENOMEM;
3651 data->client = clp;
3652 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003654 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655}
3656
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003657static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658{
3659 struct rpc_message msg = {
3660 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3661 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003662 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 };
3664 unsigned long now = jiffies;
3665 int status;
3666
3667 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3668 if (status < 0)
3669 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003670 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 return 0;
3672}
3673
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003674static inline int nfs4_server_supports_acls(struct nfs_server *server)
3675{
3676 return (server->caps & NFS_CAP_ACLS)
3677 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3678 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3679}
3680
Trond Myklebust21f498c2012-08-24 10:59:25 -04003681/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3682 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003683 * the stack.
3684 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04003685#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003686
Neil Hormane9e3d722011-03-04 19:26:03 -05003687static int buf_to_pages_noslab(const void *buf, size_t buflen,
3688 struct page **pages, unsigned int *pgbase)
3689{
3690 struct page *newpage, **spages;
3691 int rc = 0;
3692 size_t len;
3693 spages = pages;
3694
3695 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04003696 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003697 newpage = alloc_page(GFP_KERNEL);
3698
3699 if (newpage == NULL)
3700 goto unwind;
3701 memcpy(page_address(newpage), buf, len);
3702 buf += len;
3703 buflen -= len;
3704 *pages++ = newpage;
3705 rc++;
3706 } while (buflen != 0);
3707
3708 return rc;
3709
3710unwind:
3711 for(; rc > 0; rc--)
3712 __free_page(spages[rc-1]);
3713 return -ENOMEM;
3714}
3715
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003716struct nfs4_cached_acl {
3717 int cached;
3718 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003719 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003720};
3721
3722static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003723{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003724 struct nfs_inode *nfsi = NFS_I(inode);
3725
3726 spin_lock(&inode->i_lock);
3727 kfree(nfsi->nfs4_acl);
3728 nfsi->nfs4_acl = acl;
3729 spin_unlock(&inode->i_lock);
3730}
3731
3732static void nfs4_zap_acl_attr(struct inode *inode)
3733{
3734 nfs4_set_cached_acl(inode, NULL);
3735}
3736
3737static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3738{
3739 struct nfs_inode *nfsi = NFS_I(inode);
3740 struct nfs4_cached_acl *acl;
3741 int ret = -ENOENT;
3742
3743 spin_lock(&inode->i_lock);
3744 acl = nfsi->nfs4_acl;
3745 if (acl == NULL)
3746 goto out;
3747 if (buf == NULL) /* user is just asking for length */
3748 goto out_len;
3749 if (acl->cached == 0)
3750 goto out;
3751 ret = -ERANGE; /* see getxattr(2) man page */
3752 if (acl->len > buflen)
3753 goto out;
3754 memcpy(buf, acl->data, acl->len);
3755out_len:
3756 ret = acl->len;
3757out:
3758 spin_unlock(&inode->i_lock);
3759 return ret;
3760}
3761
Sachin Prabhu5794d212012-04-17 14:36:40 +01003762static 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 +00003763{
3764 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003765 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003766
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003767 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003768 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003769 if (acl == NULL)
3770 goto out;
3771 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003772 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003773 } else {
3774 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3775 if (acl == NULL)
3776 goto out;
3777 acl->cached = 0;
3778 }
3779 acl->len = acl_len;
3780out:
3781 nfs4_set_cached_acl(inode, acl);
3782}
3783
Andy Adamsonbf118a32011-12-07 11:55:27 -05003784/*
3785 * The getxattr API returns the required buffer length when called with a
3786 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3787 * the required buf. On a NULL buf, we send a page of data to the server
3788 * guessing that the ACL request can be serviced by a page. If so, we cache
3789 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3790 * the cache. If not so, we throw away the page, and cache the required
3791 * length. The next getxattr call will then produce another round trip to
3792 * the server, this time with the input buf of the required size.
3793 */
Trond Myklebust16b42892006-08-24 12:27:15 -04003794static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003795{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003796 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003797 struct nfs_getaclargs args = {
3798 .fh = NFS_FH(inode),
3799 .acl_pages = pages,
3800 .acl_len = buflen,
3801 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003802 struct nfs_getaclres res = {
3803 .acl_len = buflen,
3804 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003805 struct rpc_message msg = {
3806 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3807 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003808 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003809 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003810 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3811 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003812
Andy Adamsonbf118a32011-12-07 11:55:27 -05003813 /* As long as we're doing a round trip to the server anyway,
3814 * let's be prepared for a page of acl data. */
3815 if (npages == 0)
3816 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003817 if (npages > ARRAY_SIZE(pages))
3818 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003819
Andy Adamsonbf118a32011-12-07 11:55:27 -05003820 for (i = 0; i < npages; i++) {
3821 pages[i] = alloc_page(GFP_KERNEL);
3822 if (!pages[i])
3823 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003824 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01003825
3826 /* for decoding across pages */
3827 res.acl_scratch = alloc_page(GFP_KERNEL);
3828 if (!res.acl_scratch)
3829 goto out_free;
3830
Andy Adamsonbf118a32011-12-07 11:55:27 -05003831 args.acl_len = npages * PAGE_SIZE;
3832 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003833
Peng Taode040be2012-01-10 22:42:47 +08003834 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003835 __func__, buf, buflen, npages, args.acl_len);
3836 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3837 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003838 if (ret)
3839 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003840
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003841 /* Handle the case where the passed-in buffer is too short */
3842 if (res.acl_flags & NFS4_ACL_TRUNC) {
3843 /* Did the user only issue a request for the acl length? */
3844 if (buf == NULL)
3845 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003846 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003847 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003848 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003849 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01003850 if (buf) {
3851 if (res.acl_len > buflen) {
3852 ret = -ERANGE;
3853 goto out_free;
3854 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003855 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01003856 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003857out_ok:
3858 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003859out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003860 for (i = 0; i < npages; i++)
3861 if (pages[i])
3862 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05003863 if (res.acl_scratch)
3864 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003865 return ret;
3866}
3867
Trond Myklebust16b42892006-08-24 12:27:15 -04003868static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3869{
3870 struct nfs4_exception exception = { };
3871 ssize_t ret;
3872 do {
3873 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3874 if (ret >= 0)
3875 break;
3876 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3877 } while (exception.retry);
3878 return ret;
3879}
3880
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003881static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3882{
3883 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003884 int ret;
3885
3886 if (!nfs4_server_supports_acls(server))
3887 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003888 ret = nfs_revalidate_inode(server, inode);
3889 if (ret < 0)
3890 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003891 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3892 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003893 ret = nfs4_read_cached_acl(inode, buf, buflen);
3894 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003895 /* -ENOENT is returned if there is no ACL or if there is an ACL
3896 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003897 return ret;
3898 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003899}
3900
Trond Myklebust16b42892006-08-24 12:27:15 -04003901static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003902{
3903 struct nfs_server *server = NFS_SERVER(inode);
3904 struct page *pages[NFS4ACL_MAXPAGES];
3905 struct nfs_setaclargs arg = {
3906 .fh = NFS_FH(inode),
3907 .acl_pages = pages,
3908 .acl_len = buflen,
3909 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003910 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003911 struct rpc_message msg = {
3912 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3913 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003914 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003915 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003916 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05003917 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003918
3919 if (!nfs4_server_supports_acls(server))
3920 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003921 if (npages > ARRAY_SIZE(pages))
3922 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05003923 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3924 if (i < 0)
3925 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04003926 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003927 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003928
3929 /*
3930 * Free each page after tx, so the only ref left is
3931 * held by the network stack
3932 */
3933 for (; i > 0; i--)
3934 put_page(pages[i-1]);
3935
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003936 /*
3937 * Acl update can result in inode attribute update.
3938 * so mark the attribute cache invalid.
3939 */
3940 spin_lock(&inode->i_lock);
3941 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3942 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003943 nfs_access_zap_cache(inode);
3944 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003945 return ret;
3946}
3947
Trond Myklebust16b42892006-08-24 12:27:15 -04003948static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3949{
3950 struct nfs4_exception exception = { };
3951 int err;
3952 do {
3953 err = nfs4_handle_exception(NFS_SERVER(inode),
3954 __nfs4_proc_set_acl(inode, buf, buflen),
3955 &exception);
3956 } while (exception.retry);
3957 return err;
3958}
3959
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003961nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003963 struct nfs_client *clp = server->nfs_client;
3964
3965 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 return 0;
3967 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05003968 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003969 case -NFS4ERR_ADMIN_REVOKED:
3970 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04003971 if (state == NULL)
3972 break;
3973 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003974 case -NFS4ERR_OPENMODE:
3975 if (state == NULL)
3976 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003977 nfs4_schedule_stateid_recovery(server, state);
3978 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003979 case -NFS4ERR_EXPIRED:
3980 if (state != NULL)
3981 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003983 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003984 nfs4_schedule_lease_recovery(clp);
3985 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003986#if defined(CONFIG_NFS_V4_1)
3987 case -NFS4ERR_BADSESSION:
3988 case -NFS4ERR_BADSLOT:
3989 case -NFS4ERR_BAD_HIGH_SLOT:
3990 case -NFS4ERR_DEADSESSION:
3991 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3992 case -NFS4ERR_SEQ_FALSE_RETRY:
3993 case -NFS4ERR_SEQ_MISORDERED:
3994 dprintk("%s ERROR %d, Reset session\n", __func__,
3995 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04003996 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04003997 task->tk_status = 0;
3998 return -EAGAIN;
3999#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004001 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05004002 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4004 task->tk_status = 0;
4005 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004006 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007 case -NFS4ERR_OLD_STATEID:
4008 task->tk_status = 0;
4009 return -EAGAIN;
4010 }
4011 task->tk_status = nfs4_map_errors(task->tk_status);
4012 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004013wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004014 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004015 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4016 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4017 task->tk_status = 0;
4018 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019}
4020
Chuck Leverf0920752012-05-21 22:45:41 -04004021static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4022 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004023{
4024 __be32 verf[2];
4025
Chuck Lever2c820d92012-05-21 22:45:33 -04004026 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4027 /* An impossible timestamp guarantees this value
4028 * will never match a generated boot time. */
4029 verf[0] = 0;
4030 verf[1] = (__be32)(NSEC_PER_SEC + 1);
4031 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004032 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4033 verf[0] = (__be32)nn->boot_time.tv_sec;
4034 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04004035 }
Chuck Levercd937102012-03-02 17:14:31 -05004036 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4037}
4038
Chuck Levere984a552012-09-14 17:24:21 -04004039static unsigned int
4040nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4041 char *buf, size_t len)
4042{
4043 unsigned int result;
4044
4045 rcu_read_lock();
4046 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4047 clp->cl_ipaddr,
4048 rpc_peeraddr2str(clp->cl_rpcclient,
4049 RPC_DISPLAY_ADDR),
4050 rpc_peeraddr2str(clp->cl_rpcclient,
4051 RPC_DISPLAY_PROTO));
4052 rcu_read_unlock();
4053 return result;
4054}
4055
4056static unsigned int
4057nfs4_init_uniform_client_string(const struct nfs_client *clp,
4058 char *buf, size_t len)
4059{
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004060 char *nodename = clp->cl_rpcclient->cl_nodename;
4061
4062 if (nfs4_client_id_uniquifier[0] != '\0')
4063 nodename = nfs4_client_id_uniquifier;
Chuck Levere984a552012-09-14 17:24:21 -04004064 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4065 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004066 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004067}
4068
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004069/**
4070 * nfs4_proc_setclientid - Negotiate client ID
4071 * @clp: state data structure
4072 * @program: RPC program for NFSv4 callback service
4073 * @port: IP port number for NFS4 callback service
4074 * @cred: RPC credential to use for this call
4075 * @res: where to place the result
4076 *
4077 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4078 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004079int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4080 unsigned short port, struct rpc_cred *cred,
4081 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082{
4083 nfs4_verifier sc_verifier;
4084 struct nfs4_setclientid setclientid = {
4085 .sc_verifier = &sc_verifier,
4086 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004087 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088 };
4089 struct rpc_message msg = {
4090 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4091 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004092 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004093 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004095 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096
Chuck Leverde734832012-07-11 16:30:50 -04004097 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004098 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004099 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4100 setclientid.sc_name_len =
4101 nfs4_init_uniform_client_string(clp,
4102 setclientid.sc_name,
4103 sizeof(setclientid.sc_name));
4104 else
4105 setclientid.sc_name_len =
4106 nfs4_init_nonuniform_client_string(clp,
4107 setclientid.sc_name,
4108 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004109 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004110 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004111 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004112 sizeof(setclientid.sc_netid),
4113 rpc_peeraddr2str(clp->cl_rpcclient,
4114 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004115 rcu_read_unlock();
4116 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004117 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 clp->cl_ipaddr, port >> 8, port & 255);
4119
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004120 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4121 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4122 setclientid.sc_name_len, setclientid.sc_name);
4123 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4124 dprintk("NFS reply setclientid: %d\n", status);
4125 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126}
4127
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004128/**
4129 * nfs4_proc_setclientid_confirm - Confirm client ID
4130 * @clp: state data structure
4131 * @res: result of a previous SETCLIENTID
4132 * @cred: RPC credential to use for this call
4133 *
4134 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4135 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004136int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004137 struct nfs4_setclientid_res *arg,
4138 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139{
4140 struct nfs_fsinfo fsinfo;
4141 struct rpc_message msg = {
4142 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004143 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004145 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146 };
4147 unsigned long now;
4148 int status;
4149
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004150 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4151 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4152 clp->cl_clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004154 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 if (status == 0) {
4156 spin_lock(&clp->cl_lock);
4157 clp->cl_lease_time = fsinfo.lease_time * HZ;
4158 clp->cl_last_renewal = now;
4159 spin_unlock(&clp->cl_lock);
4160 }
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004161 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 return status;
4163}
4164
Trond Myklebustfe650402006-01-03 09:55:18 +01004165struct nfs4_delegreturndata {
4166 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004167 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004168 struct nfs_fh fh;
4169 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004170 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004171 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004172 int rpc_status;
4173};
4174
Trond Myklebustfe650402006-01-03 09:55:18 +01004175static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4176{
4177 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004178
Trond Myklebust14516c32010-07-31 14:29:06 -04004179 if (!nfs4_sequence_done(task, &data->res.seq_res))
4180 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004181
Ricardo Labiaga79708862009-12-07 09:23:21 -05004182 switch (task->tk_status) {
4183 case -NFS4ERR_STALE_STATEID:
4184 case -NFS4ERR_EXPIRED:
4185 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004186 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004187 break;
4188 default:
4189 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4190 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004191 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004192 return;
4193 }
4194 }
4195 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004196}
4197
4198static void nfs4_delegreturn_release(void *calldata)
4199{
Trond Myklebustfe650402006-01-03 09:55:18 +01004200 kfree(calldata);
4201}
4202
Andy Adamson938e1012009-04-01 09:22:28 -04004203#if defined(CONFIG_NFS_V4_1)
4204static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4205{
4206 struct nfs4_delegreturndata *d_data;
4207
4208 d_data = (struct nfs4_delegreturndata *)data;
4209
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004210 nfs4_setup_sequence(d_data->res.server,
4211 &d_data->args.seq_args,
4212 &d_data->res.seq_res,
4213 task);
Andy Adamson938e1012009-04-01 09:22:28 -04004214}
4215#endif /* CONFIG_NFS_V4_1 */
4216
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004217static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004218#if defined(CONFIG_NFS_V4_1)
4219 .rpc_call_prepare = nfs4_delegreturn_prepare,
4220#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004221 .rpc_call_done = nfs4_delegreturn_done,
4222 .rpc_release = nfs4_delegreturn_release,
4223};
4224
Trond Myklebuste6f81072008-01-24 18:14:34 -05004225static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004226{
4227 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004228 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004229 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004230 struct rpc_message msg = {
4231 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4232 .rpc_cred = cred,
4233 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004234 struct rpc_task_setup task_setup_data = {
4235 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004236 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004237 .callback_ops = &nfs4_delegreturn_ops,
4238 .flags = RPC_TASK_ASYNC,
4239 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004240 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004241
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004242 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004243 if (data == NULL)
4244 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004245 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004246 data->args.fhandle = &data->fh;
4247 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004248 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004249 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004250 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004251 data->res.fattr = &data->fattr;
4252 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004253 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004254 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004255 data->rpc_status = 0;
4256
Trond Myklebustc970aa82007-07-14 15:39:59 -04004257 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004258 msg.rpc_argp = &data->args;
4259 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004260 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004261 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004262 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004263 if (!issync)
4264 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004265 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004266 if (status != 0)
4267 goto out;
4268 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004269 if (status == 0)
4270 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4271 else
4272 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004273out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004274 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004275 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276}
4277
Trond Myklebuste6f81072008-01-24 18:14:34 -05004278int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279{
4280 struct nfs_server *server = NFS_SERVER(inode);
4281 struct nfs4_exception exception = { };
4282 int err;
4283 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004284 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285 switch (err) {
4286 case -NFS4ERR_STALE_STATEID:
4287 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288 case 0:
4289 return 0;
4290 }
4291 err = nfs4_handle_exception(server, err, &exception);
4292 } while (exception.retry);
4293 return err;
4294}
4295
4296#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4297#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4298
4299/*
4300 * sleep, with exponential backoff, and retry the LOCK operation.
4301 */
4302static unsigned long
4303nfs4_set_lock_task_retry(unsigned long timeout)
4304{
Jeff Laytond3103102011-12-01 22:44:39 +01004305 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306 timeout <<= 1;
4307 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4308 return NFS4_LOCK_MAXTIMEOUT;
4309 return timeout;
4310}
4311
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4313{
4314 struct inode *inode = state->inode;
4315 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004316 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004317 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004319 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004321 struct nfs_lockt_res res = {
4322 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323 };
4324 struct rpc_message msg = {
4325 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4326 .rpc_argp = &arg,
4327 .rpc_resp = &res,
4328 .rpc_cred = state->owner->so_cred,
4329 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 struct nfs4_lock_state *lsp;
4331 int status;
4332
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004333 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004334 status = nfs4_set_lock_state(state, request);
4335 if (status != 0)
4336 goto out;
4337 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004338 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004339 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004340 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004341 switch (status) {
4342 case 0:
4343 request->fl_type = F_UNLCK;
4344 break;
4345 case -NFS4ERR_DENIED:
4346 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004348 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004349out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350 return status;
4351}
4352
4353static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4354{
4355 struct nfs4_exception exception = { };
4356 int err;
4357
4358 do {
4359 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4360 _nfs4_proc_getlk(state, cmd, request),
4361 &exception);
4362 } while (exception.retry);
4363 return err;
4364}
4365
4366static int do_vfs_lock(struct file *file, struct file_lock *fl)
4367{
4368 int res = 0;
4369 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4370 case FL_POSIX:
4371 res = posix_lock_file_wait(file, fl);
4372 break;
4373 case FL_FLOCK:
4374 res = flock_lock_file_wait(file, fl);
4375 break;
4376 default:
4377 BUG();
4378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379 return res;
4380}
4381
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004382struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004383 struct nfs_locku_args arg;
4384 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004385 struct nfs4_lock_state *lsp;
4386 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004387 struct file_lock fl;
4388 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004389 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004390};
4391
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004392static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4393 struct nfs_open_context *ctx,
4394 struct nfs4_lock_state *lsp,
4395 struct nfs_seqid *seqid)
4396{
4397 struct nfs4_unlockdata *p;
4398 struct inode *inode = lsp->ls_state->inode;
4399
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004400 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004401 if (p == NULL)
4402 return NULL;
4403 p->arg.fh = NFS_FH(inode);
4404 p->arg.fl = &p->fl;
4405 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004406 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004407 p->arg.stateid = &lsp->ls_stateid;
4408 p->lsp = lsp;
4409 atomic_inc(&lsp->ls_count);
4410 /* Ensure we don't close file until we're done freeing locks! */
4411 p->ctx = get_nfs_open_context(ctx);
4412 memcpy(&p->fl, fl, sizeof(p->fl));
4413 p->server = NFS_SERVER(inode);
4414 return p;
4415}
4416
Trond Myklebust06f814a2006-01-03 09:55:07 +01004417static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004418{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004419 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004420 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004421 nfs4_put_lock_state(calldata->lsp);
4422 put_nfs_open_context(calldata->ctx);
4423 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004424}
4425
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004426static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004427{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004428 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004429
Trond Myklebust14516c32010-07-31 14:29:06 -04004430 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4431 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004432 switch (task->tk_status) {
4433 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004434 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4435 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004436 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004437 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004438 case -NFS4ERR_BAD_STATEID:
4439 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004440 case -NFS4ERR_STALE_STATEID:
4441 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004442 break;
4443 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004444 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004445 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004446 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04004447 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004448}
4449
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004450static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004451{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004452 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004454 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004455 return;
Trond Myklebust795a88c2012-09-10 13:26:49 -04004456 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004457 /* Note: exit _without_ running nfs4_locku_done */
4458 task->tk_action = NULL;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05004459 nfs4_sequence_done(task, &calldata->res.seq_res);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004460 return;
4461 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004462 calldata->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04004463 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004464 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004465 &calldata->res.seq_res,
4466 task) != 0)
4467 nfs_release_seqid(calldata->arg.seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468}
4469
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004470static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004471 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004472 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004473 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004474};
4475
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004476static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4477 struct nfs_open_context *ctx,
4478 struct nfs4_lock_state *lsp,
4479 struct nfs_seqid *seqid)
4480{
4481 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004482 struct rpc_message msg = {
4483 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4484 .rpc_cred = ctx->cred,
4485 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004486 struct rpc_task_setup task_setup_data = {
4487 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004488 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004489 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004490 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004491 .flags = RPC_TASK_ASYNC,
4492 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004493
Frank Filz137d6ac2007-07-09 15:32:29 -07004494 /* Ensure this is an unlock - when canceling a lock, the
4495 * canceled lock is passed in, and it won't be an unlock.
4496 */
4497 fl->fl_type = F_UNLCK;
4498
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004499 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4500 if (data == NULL) {
4501 nfs_free_seqid(seqid);
4502 return ERR_PTR(-ENOMEM);
4503 }
4504
Trond Myklebust9d12b212012-01-17 22:04:25 -05004505 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004506 msg.rpc_argp = &data->arg;
4507 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004508 task_setup_data.callback_data = data;
4509 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004510}
4511
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4513{
Trond Myklebust65b62a22013-02-07 10:54:07 -05004514 struct inode *inode = state->inode;
4515 struct nfs4_state_owner *sp = state->owner;
4516 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004517 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004518 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004519 struct rpc_task *task;
4520 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004521 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522
Trond Myklebust9b073572006-06-29 16:38:34 -04004523 status = nfs4_set_lock_state(state, request);
4524 /* Unlock _before_ we do the RPC call */
4525 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05004526 /* Exclude nfs_delegation_claim_locks() */
4527 mutex_lock(&sp->so_delegreturn_mutex);
4528 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004529 down_read(&nfsi->rwsem);
4530 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4531 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05004532 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04004533 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004534 }
4535 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05004536 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04004537 if (status != 0)
4538 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004539 /* Is this a delegated lock? */
4540 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004541 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004542 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004543 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004544 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004545 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004546 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004547 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004548 status = PTR_ERR(task);
4549 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004550 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004551 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004552 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004553out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004554 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004555 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556}
4557
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004558struct nfs4_lockdata {
4559 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004560 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004561 struct nfs4_lock_state *lsp;
4562 struct nfs_open_context *ctx;
4563 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004564 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004565 int rpc_status;
4566 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004567 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004568};
4569
4570static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004571 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4572 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004573{
4574 struct nfs4_lockdata *p;
4575 struct inode *inode = lsp->ls_state->inode;
4576 struct nfs_server *server = NFS_SERVER(inode);
4577
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004578 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004579 if (p == NULL)
4580 return NULL;
4581
4582 p->arg.fh = NFS_FH(inode);
4583 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004584 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004585 if (p->arg.open_seqid == NULL)
4586 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004587 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004588 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004589 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004590 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004591 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004592 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004593 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004594 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004595 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004596 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004597 atomic_inc(&lsp->ls_count);
4598 p->ctx = get_nfs_open_context(ctx);
4599 memcpy(&p->fl, fl, sizeof(p->fl));
4600 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004601out_free_seqid:
4602 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004603out_free:
4604 kfree(p);
4605 return NULL;
4606}
4607
4608static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4609{
4610 struct nfs4_lockdata *data = calldata;
4611 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612
Harvey Harrison3110ff82008-05-02 13:42:44 -07004613 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004614 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4615 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004616 /* Do we need to do an open_to_lock_owner? */
4617 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004618 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004619 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004620 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004621 data->arg.open_stateid = &state->stateid;
4622 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004623 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004624 } else
4625 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004626 data->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04004627 if (nfs4_setup_sequence(data->server,
4628 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004629 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004630 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04004631 return;
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004632 nfs_release_seqid(data->arg.open_seqid);
4633out_release_lock_seqid:
4634 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004635 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004636}
4637
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004638static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4639{
4640 struct nfs4_lockdata *data = calldata;
4641
Harvey Harrison3110ff82008-05-02 13:42:44 -07004642 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004643
Trond Myklebust14516c32010-07-31 14:29:06 -04004644 if (!nfs4_sequence_done(task, &data->res.seq_res))
4645 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004646
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004647 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004648 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004649 if (data->rpc_status == 0)
4650 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4651 else
4652 goto out;
4653 }
4654 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004655 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004656 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04004657 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004658 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004659out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004660 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004661}
4662
4663static void nfs4_lock_release(void *calldata)
4664{
4665 struct nfs4_lockdata *data = calldata;
4666
Harvey Harrison3110ff82008-05-02 13:42:44 -07004667 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004668 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004669 if (data->cancelled != 0) {
4670 struct rpc_task *task;
4671 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4672 data->arg.lock_seqid);
4673 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004674 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004675 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004676 } else
4677 nfs_free_seqid(data->arg.lock_seqid);
4678 nfs4_put_lock_state(data->lsp);
4679 put_nfs_open_context(data->ctx);
4680 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004681 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004682}
4683
4684static const struct rpc_call_ops nfs4_lock_ops = {
4685 .rpc_call_prepare = nfs4_lock_prepare,
4686 .rpc_call_done = nfs4_lock_done,
4687 .rpc_release = nfs4_lock_release,
4688};
4689
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004690static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4691{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004692 switch (error) {
4693 case -NFS4ERR_ADMIN_REVOKED:
4694 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004695 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004696 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04004697 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004698 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004699 break;
4700 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004701 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004702 case -NFS4ERR_EXPIRED:
4703 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004704 };
4705}
4706
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004707static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004708{
4709 struct nfs4_lockdata *data;
4710 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004711 struct rpc_message msg = {
4712 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4713 .rpc_cred = state->owner->so_cred,
4714 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004715 struct rpc_task_setup task_setup_data = {
4716 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004717 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004718 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004719 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004720 .flags = RPC_TASK_ASYNC,
4721 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004722 int ret;
4723
Harvey Harrison3110ff82008-05-02 13:42:44 -07004724 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004725 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004726 fl->fl_u.nfs4_fl.owner,
4727 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004728 if (data == NULL)
4729 return -ENOMEM;
4730 if (IS_SETLKW(cmd))
4731 data->arg.block = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004732 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004733 msg.rpc_argp = &data->arg;
4734 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004735 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004736 if (recovery_type > NFS_LOCK_NEW) {
4737 if (recovery_type == NFS_LOCK_RECLAIM)
4738 data->arg.reclaim = NFS_LOCK_RECLAIM;
4739 nfs4_set_sequence_privileged(&data->arg.seq_args);
4740 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04004741 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004742 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004743 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004744 ret = nfs4_wait_for_completion_rpc_task(task);
4745 if (ret == 0) {
4746 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004747 if (ret)
4748 nfs4_handle_setlk_error(data->server, data->lsp,
4749 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004750 } else
4751 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004752 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004753 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004754 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755}
4756
4757static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4758{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004759 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004760 struct nfs4_exception exception = {
4761 .inode = state->inode,
4762 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004763 int err;
4764
4765 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004766 /* Cache the lock if possible... */
4767 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4768 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004769 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004770 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004771 break;
4772 nfs4_handle_exception(server, err, &exception);
4773 } while (exception.retry);
4774 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775}
4776
4777static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4778{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004779 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004780 struct nfs4_exception exception = {
4781 .inode = state->inode,
4782 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004783 int err;
4784
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004785 err = nfs4_set_lock_state(state, request);
4786 if (err != 0)
4787 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004788 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004789 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4790 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004791 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004792 switch (err) {
4793 default:
4794 goto out;
4795 case -NFS4ERR_GRACE:
4796 case -NFS4ERR_DELAY:
4797 nfs4_handle_exception(server, err, &exception);
4798 err = 0;
4799 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004800 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004801out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004802 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803}
4804
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004805#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004806/**
4807 * nfs41_check_expired_locks - possibly free a lock stateid
4808 *
4809 * @state: NFSv4 state for an inode
4810 *
4811 * Returns NFS_OK if recovery for this stateid is now finished.
4812 * Otherwise a negative NFS4ERR value is returned.
4813 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004814static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004815{
Chuck Levereb64cf92012-07-11 16:30:05 -04004816 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004817 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004818 struct nfs_server *server = NFS_SERVER(state->inode);
4819
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004820 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04004821 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004822 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4823 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004824 /* Free the stateid unless the server
4825 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04004826 if (status != -NFS4ERR_BAD_STATEID)
4827 nfs41_free_stateid(server,
4828 &lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004829 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004830 ret = status;
4831 }
4832 }
4833 };
4834
4835 return ret;
4836}
4837
4838static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4839{
4840 int status = NFS_OK;
4841
4842 if (test_bit(LK_STATE_IN_USE, &state->flags))
4843 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04004844 if (status != NFS_OK)
4845 status = nfs4_lock_expired(state, request);
4846 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004847}
4848#endif
4849
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4851{
Trond Myklebust9a99af42013-02-04 20:17:49 -05004852 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004853 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004854 unsigned char fl_flags = request->fl_flags;
Trond Myklebust9a99af42013-02-04 20:17:49 -05004855 unsigned int seq;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004856 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004858 if ((fl_flags & FL_POSIX) &&
4859 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4860 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004861 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004862 status = nfs4_set_lock_state(state, request);
4863 if (status != 0)
4864 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004865 request->fl_flags |= FL_ACCESS;
4866 status = do_vfs_lock(request->fl_file, request);
4867 if (status < 0)
4868 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004869 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004870 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004871 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004872 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004873 request->fl_flags = fl_flags & ~FL_SLEEP;
4874 status = do_vfs_lock(request->fl_file, request);
4875 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004876 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05004877 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
4878 up_read(&nfsi->rwsem);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004879 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004880 if (status != 0)
Trond Myklebust9a99af42013-02-04 20:17:49 -05004881 goto out;
4882 down_read(&nfsi->rwsem);
4883 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
4884 status = -NFS4ERR_DELAY;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004885 goto out_unlock;
Trond Myklebust9a99af42013-02-04 20:17:49 -05004886 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004887 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004888 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004889 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004890 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4891 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004892out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004893 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004894out:
4895 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896 return status;
4897}
4898
4899static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4900{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004901 struct nfs4_exception exception = {
4902 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04004903 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004904 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905 int err;
4906
4907 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004908 err = _nfs4_proc_setlk(state, cmd, request);
4909 if (err == -NFS4ERR_DENIED)
4910 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004912 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913 } while (exception.retry);
4914 return err;
4915}
4916
4917static int
4918nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4919{
4920 struct nfs_open_context *ctx;
4921 struct nfs4_state *state;
4922 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4923 int status;
4924
4925 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004926 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927 state = ctx->state;
4928
4929 if (request->fl_start < 0 || request->fl_end < 0)
4930 return -EINVAL;
4931
Trond Myklebustd9531262009-07-21 19:22:38 -04004932 if (IS_GETLK(cmd)) {
4933 if (state != NULL)
4934 return nfs4_proc_getlk(state, F_GETLK, request);
4935 return 0;
4936 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937
4938 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4939 return -EINVAL;
4940
Trond Myklebustd9531262009-07-21 19:22:38 -04004941 if (request->fl_type == F_UNLCK) {
4942 if (state != NULL)
4943 return nfs4_proc_unlck(state, cmd, request);
4944 return 0;
4945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946
Trond Myklebustd9531262009-07-21 19:22:38 -04004947 if (state == NULL)
4948 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04004949 /*
4950 * Don't rely on the VFS having checked the file open mode,
4951 * since it won't do this for flock() locks.
4952 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04004953 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04004954 case F_RDLCK:
4955 if (!(filp->f_mode & FMODE_READ))
4956 return -EBADF;
4957 break;
4958 case F_WRLCK:
4959 if (!(filp->f_mode & FMODE_WRITE))
4960 return -EBADF;
4961 }
4962
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963 do {
4964 status = nfs4_proc_setlk(state, cmd, request);
4965 if ((status != -EAGAIN) || IS_SETLK(cmd))
4966 break;
4967 timeout = nfs4_set_lock_task_retry(timeout);
4968 status = -ERESTARTSYS;
4969 if (signalled())
4970 break;
4971 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972 return status;
4973}
4974
Trond Myklebust888e6942005-11-04 15:38:11 -05004975int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4976{
4977 struct nfs_server *server = NFS_SERVER(state->inode);
4978 struct nfs4_exception exception = { };
4979 int err;
4980
4981 err = nfs4_set_lock_state(state, fl);
4982 if (err != 0)
4983 goto out;
4984 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004985 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004986 switch (err) {
4987 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004988 printk(KERN_ERR "NFS: %s: unhandled error "
4989 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004990 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004991 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004992 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004993 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004994 case -NFS4ERR_STALE_STATEID:
Trond Myklebust37380e42013-02-05 12:35:16 -05004995 set_bit(NFS_DELEGATED_STATE, &state->flags);
4996 case -NFS4ERR_EXPIRED:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004997 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust37380e42013-02-05 12:35:16 -05004998 err = -EAGAIN;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004999 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05005000 case -NFS4ERR_BADSESSION:
5001 case -NFS4ERR_BADSLOT:
5002 case -NFS4ERR_BAD_HIGH_SLOT:
5003 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
5004 case -NFS4ERR_DEADSESSION:
Trond Myklebust37380e42013-02-05 12:35:16 -05005005 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust9f594792012-05-27 13:02:53 -04005006 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Trond Myklebust37380e42013-02-05 12:35:16 -05005007 err = -EAGAIN;
Trond Myklebustd5122202009-06-17 13:22:58 -07005008 goto out;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005009 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005010 case -NFS4ERR_ADMIN_REVOKED:
5011 case -NFS4ERR_BAD_STATEID:
5012 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005013 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07005014 err = 0;
5015 goto out;
5016 case -ENOMEM:
5017 case -NFS4ERR_DENIED:
5018 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
5019 err = 0;
5020 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07005021 }
Trond Myklebust37380e42013-02-05 12:35:16 -05005022 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust888e6942005-11-04 15:38:11 -05005023 err = nfs4_handle_exception(server, err, &exception);
5024 } while (exception.retry);
5025out:
5026 return err;
5027}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005028
Trond Myklebustcf470c32012-03-07 13:49:12 -05005029struct nfs_release_lockowner_data {
5030 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005031 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005032 struct nfs_release_lockowner_args args;
5033};
5034
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005035static void nfs4_release_lockowner_release(void *calldata)
5036{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005037 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005038 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005039 kfree(calldata);
5040}
5041
Trond Myklebust17280172012-03-11 13:11:00 -04005042static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005043 .rpc_release = nfs4_release_lockowner_release,
5044};
5045
Trond Myklebustcf470c32012-03-07 13:49:12 -05005046int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005047{
5048 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005049 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005050 struct rpc_message msg = {
5051 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5052 };
5053
5054 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005055 return -EINVAL;
5056 data = kmalloc(sizeof(*data), GFP_NOFS);
5057 if (!data)
5058 return -ENOMEM;
5059 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005060 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005061 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5062 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5063 data->args.lock_owner.s_dev = server->s_dev;
5064 msg.rpc_argp = &data->args;
5065 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5066 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005067}
5068
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005069#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5070
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005071static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5072 const void *buf, size_t buflen,
5073 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005074{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005075 if (strcmp(key, "") != 0)
5076 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005077
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005078 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005079}
5080
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005081static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5082 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005083{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005084 if (strcmp(key, "") != 0)
5085 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005086
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005087 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005088}
5089
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005090static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5091 size_t list_len, const char *name,
5092 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005093{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005094 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005095
J. Bruce Fields096455a2006-03-20 23:23:42 -05005096 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5097 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005098
5099 if (list && len <= list_len)
5100 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005101 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005102}
5103
Andy Adamson533eb462011-06-13 18:25:56 -04005104/*
5105 * nfs_fhget will use either the mounted_on_fileid or the fileid
5106 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005107static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5108{
Andy Adamson533eb462011-06-13 18:25:56 -04005109 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5110 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5111 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005112 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005113 return;
5114
5115 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005116 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005117 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5118 fattr->nlink = 2;
5119}
5120
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005121static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5122 const struct qstr *name,
5123 struct nfs4_fs_locations *fs_locations,
5124 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005125{
5126 struct nfs_server *server = NFS_SERVER(dir);
5127 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005128 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005129 };
5130 struct nfs4_fs_locations_arg args = {
5131 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005132 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005133 .page = page,
5134 .bitmask = bitmask,
5135 };
Benny Halevy22958462009-04-01 09:22:02 -04005136 struct nfs4_fs_locations_res res = {
5137 .fs_locations = fs_locations,
5138 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005139 struct rpc_message msg = {
5140 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5141 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005142 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005143 };
5144 int status;
5145
Harvey Harrison3110ff82008-05-02 13:42:44 -07005146 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005147
5148 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5149 * is not supported */
5150 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5151 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5152 else
5153 bitmask[0] |= FATTR4_WORD0_FILEID;
5154
Trond Myklebustc228fd32007-01-13 02:28:11 -05005155 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005156 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005157 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005158 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005159 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005160 return status;
5161}
5162
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005163int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5164 const struct qstr *name,
5165 struct nfs4_fs_locations *fs_locations,
5166 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005167{
5168 struct nfs4_exception exception = { };
5169 int err;
5170 do {
5171 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005172 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005173 &exception);
5174 } while (exception.retry);
5175 return err;
5176}
5177
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005178static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5179{
5180 int status;
5181 struct nfs4_secinfo_arg args = {
5182 .dir_fh = NFS_FH(dir),
5183 .name = name,
5184 };
5185 struct nfs4_secinfo_res res = {
5186 .flavors = flavors,
5187 };
5188 struct rpc_message msg = {
5189 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5190 .rpc_argp = &args,
5191 .rpc_resp = &res,
5192 };
5193
5194 dprintk("NFS call secinfo %s\n", name->name);
5195 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5196 dprintk("NFS reply secinfo: %d\n", status);
5197 return status;
5198}
5199
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005200int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5201 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005202{
5203 struct nfs4_exception exception = { };
5204 int err;
5205 do {
5206 err = nfs4_handle_exception(NFS_SERVER(dir),
5207 _nfs4_proc_secinfo(dir, name, flavors),
5208 &exception);
5209 } while (exception.retry);
5210 return err;
5211}
5212
Andy Adamson557134a2009-04-01 09:21:53 -04005213#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005214/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005215 * Check the exchange flags returned by the server for invalid flags, having
5216 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5217 * DS flags set.
5218 */
5219static int nfs4_check_cl_exchange_flags(u32 flags)
5220{
5221 if (flags & ~EXCHGID4_FLAG_MASK_R)
5222 goto out_inval;
5223 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5224 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5225 goto out_inval;
5226 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5227 goto out_inval;
5228 return NFS_OK;
5229out_inval:
5230 return -NFS4ERR_INVAL;
5231}
5232
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005233static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005234nfs41_same_server_scope(struct nfs41_server_scope *a,
5235 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005236{
5237 if (a->server_scope_sz == b->server_scope_sz &&
5238 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5239 return true;
5240
5241 return false;
5242}
5243
Andy Adamson357f54d2010-12-14 10:11:57 -05005244/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005245 * nfs4_proc_bind_conn_to_session()
5246 *
5247 * The 4.1 client currently uses the same TCP connection for the
5248 * fore and backchannel.
5249 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005250int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005251{
5252 int status;
5253 struct nfs41_bind_conn_to_session_res res;
5254 struct rpc_message msg = {
5255 .rpc_proc =
5256 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5257 .rpc_argp = clp,
5258 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005259 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005260 };
5261
5262 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005263
5264 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5265 if (unlikely(res.session == NULL)) {
5266 status = -ENOMEM;
5267 goto out;
5268 }
5269
5270 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5271 if (status == 0) {
5272 if (memcmp(res.session->sess_id.data,
5273 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5274 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5275 status = -EIO;
5276 goto out_session;
5277 }
5278 if (res.dir != NFS4_CDFS4_BOTH) {
5279 dprintk("NFS: %s: Unexpected direction from server\n",
5280 __func__);
5281 status = -EIO;
5282 goto out_session;
5283 }
5284 if (res.use_conn_in_rdma_mode) {
5285 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5286 __func__);
5287 status = -EIO;
5288 goto out_session;
5289 }
5290 }
5291out_session:
5292 kfree(res.session);
5293out:
5294 dprintk("<-- %s status= %d\n", __func__, status);
5295 return status;
5296}
5297
5298/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005299 * nfs4_proc_exchange_id()
5300 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005301 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5302 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04005303 * Since the clientid has expired, all compounds using sessions
5304 * associated with the stale clientid will be returning
5305 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5306 * be in some phase of session reset.
5307 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005308int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005309{
5310 nfs4_verifier verifier;
5311 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005312 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005313 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005314 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005315 };
5316 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005317 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005318 };
5319 int status;
5320 struct rpc_message msg = {
5321 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5322 .rpc_argp = &args,
5323 .rpc_resp = &res,
5324 .rpc_cred = cred,
5325 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005326
Chuck Leverf0920752012-05-21 22:45:41 -04005327 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04005328 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5329 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005330 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5331 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5332 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005333
Chuck Leveracdeb692012-05-21 22:46:16 -04005334 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005335 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005336 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005337 status = -ENOMEM;
5338 goto out;
5339 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005340
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005341 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005342 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005343 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005344 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005345 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005346 }
5347
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005348 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005349 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005350 status = -ENOMEM;
5351 goto out_server_scope;
5352 }
5353
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005354 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005355 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005356 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005357
Chuck Lever177313f2012-05-21 22:44:58 -04005358 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005359 clp->cl_clientid = res.clientid;
5360 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5361 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5362 clp->cl_seqid = res.seqid;
5363
Chuck Leveracdeb692012-05-21 22:46:16 -04005364 kfree(clp->cl_serverowner);
5365 clp->cl_serverowner = res.server_owner;
5366 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005367
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005368 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005369 kfree(clp->cl_implid);
5370 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005371
Chuck Lever177313f2012-05-21 22:44:58 -04005372 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005373 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005374 res.server_scope)) {
5375 dprintk("%s: server_scope mismatch detected\n",
5376 __func__);
5377 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005378 kfree(clp->cl_serverscope);
5379 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005380 }
5381
Chuck Lever177313f2012-05-21 22:44:58 -04005382 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005383 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005384 goto out;
5385 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005386 } else
5387 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005388
Chuck Leveracdeb692012-05-21 22:46:16 -04005389out_server_owner:
5390 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005391out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005392 kfree(res.server_scope);
5393out:
Chuck Lever177313f2012-05-21 22:44:58 -04005394 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005395 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005396 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005397 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04005398 clp->cl_implid->date.seconds,
5399 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005400 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005401 return status;
5402}
5403
Trond Myklebust66245532012-05-25 17:18:09 -04005404static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5405 struct rpc_cred *cred)
5406{
5407 struct rpc_message msg = {
5408 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5409 .rpc_argp = clp,
5410 .rpc_cred = cred,
5411 };
5412 int status;
5413
5414 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5415 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04005416 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04005417 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5418 return status;
5419}
5420
5421static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5422 struct rpc_cred *cred)
5423{
5424 unsigned int loop;
5425 int ret;
5426
5427 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5428 ret = _nfs4_proc_destroy_clientid(clp, cred);
5429 switch (ret) {
5430 case -NFS4ERR_DELAY:
5431 case -NFS4ERR_CLIENTID_BUSY:
5432 ssleep(1);
5433 break;
5434 default:
5435 return ret;
5436 }
5437 }
5438 return 0;
5439}
5440
5441int nfs4_destroy_clientid(struct nfs_client *clp)
5442{
5443 struct rpc_cred *cred;
5444 int ret = 0;
5445
5446 if (clp->cl_mvops->minor_version < 1)
5447 goto out;
5448 if (clp->cl_exchange_flags == 0)
5449 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04005450 if (clp->cl_preserve_clid)
5451 goto out;
Trond Myklebust66245532012-05-25 17:18:09 -04005452 cred = nfs4_get_exchange_id_cred(clp);
5453 ret = nfs4_proc_destroy_clientid(clp, cred);
5454 if (cred)
5455 put_rpccred(cred);
5456 switch (ret) {
5457 case 0:
5458 case -NFS4ERR_STALE_CLIENTID:
5459 clp->cl_exchange_flags = 0;
5460 }
5461out:
5462 return ret;
5463}
5464
Andy Adamson2050f0c2009-04-01 09:22:30 -04005465struct nfs4_get_lease_time_data {
5466 struct nfs4_get_lease_time_args *args;
5467 struct nfs4_get_lease_time_res *res;
5468 struct nfs_client *clp;
5469};
5470
5471static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5472 void *calldata)
5473{
Andy Adamson2050f0c2009-04-01 09:22:30 -04005474 struct nfs4_get_lease_time_data *data =
5475 (struct nfs4_get_lease_time_data *)calldata;
5476
5477 dprintk("--> %s\n", __func__);
5478 /* just setup sequence, do not trigger session recovery
5479 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005480 nfs41_setup_sequence(data->clp->cl_session,
5481 &data->args->la_seq_args,
5482 &data->res->lr_seq_res,
5483 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005484 dprintk("<-- %s\n", __func__);
5485}
5486
5487/*
5488 * Called from nfs4_state_manager thread for session setup, so don't recover
5489 * from sequence operation or clientid errors.
5490 */
5491static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5492{
5493 struct nfs4_get_lease_time_data *data =
5494 (struct nfs4_get_lease_time_data *)calldata;
5495
5496 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005497 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5498 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005499 switch (task->tk_status) {
5500 case -NFS4ERR_DELAY:
5501 case -NFS4ERR_GRACE:
5502 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5503 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5504 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005505 /* fall through */
5506 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005507 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005508 return;
5509 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005510 dprintk("<-- %s\n", __func__);
5511}
5512
Trond Myklebust17280172012-03-11 13:11:00 -04005513static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005514 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5515 .rpc_call_done = nfs4_get_lease_time_done,
5516};
5517
5518int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5519{
5520 struct rpc_task *task;
5521 struct nfs4_get_lease_time_args args;
5522 struct nfs4_get_lease_time_res res = {
5523 .lr_fsinfo = fsinfo,
5524 };
5525 struct nfs4_get_lease_time_data data = {
5526 .args = &args,
5527 .res = &res,
5528 .clp = clp,
5529 };
5530 struct rpc_message msg = {
5531 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5532 .rpc_argp = &args,
5533 .rpc_resp = &res,
5534 };
5535 struct rpc_task_setup task_setup = {
5536 .rpc_client = clp->cl_rpcclient,
5537 .rpc_message = &msg,
5538 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005539 .callback_data = &data,
5540 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005541 };
5542 int status;
5543
Trond Myklebust9d12b212012-01-17 22:04:25 -05005544 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005545 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005546 dprintk("--> %s\n", __func__);
5547 task = rpc_run_task(&task_setup);
5548
5549 if (IS_ERR(task))
5550 status = PTR_ERR(task);
5551 else {
5552 status = task->tk_status;
5553 rpc_put_task(task);
5554 }
5555 dprintk("<-- %s return %d\n", __func__, status);
5556
5557 return status;
5558}
5559
Andy Adamsonfc931582009-04-01 09:22:31 -04005560/*
5561 * Initialize the values to be used by the client in CREATE_SESSION
5562 * If nfs4_init_session set the fore channel request and response sizes,
5563 * use them.
5564 *
5565 * Set the back channel max_resp_sz_cached to zero to force the client to
5566 * always set csa_cachethis to FALSE because the current implementation
5567 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5568 */
5569static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5570{
5571 struct nfs4_session *session = args->client->cl_session;
Trond Myklebustae72ae672012-11-20 11:02:55 -05005572 unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
5573 mxresp_sz = session->fc_target_max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005574
5575 if (mxrqst_sz == 0)
5576 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5577 if (mxresp_sz == 0)
5578 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5579 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005580 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5581 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005582 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005583 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005584
5585 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005586 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005587 __func__,
5588 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005589 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005590
5591 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005592 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5593 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5594 args->bc_attrs.max_resp_sz_cached = 0;
5595 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5596 args->bc_attrs.max_reqs = 1;
5597
5598 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5599 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5600 __func__,
5601 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5602 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5603 args->bc_attrs.max_reqs);
5604}
5605
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005606static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005607{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005608 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5609 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5610
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005611 if (rcvd->max_resp_sz > sent->max_resp_sz)
5612 return -EINVAL;
5613 /*
5614 * Our requested max_ops is the minimum we need; we're not
5615 * prepared to break up compounds into smaller pieces than that.
5616 * So, no point even trying to continue if the server won't
5617 * cooperate:
5618 */
5619 if (rcvd->max_ops < sent->max_ops)
5620 return -EINVAL;
5621 if (rcvd->max_reqs == 0)
5622 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005623 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5624 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005625 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005626}
5627
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005628static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5629{
5630 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5631 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005632
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005633 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5634 return -EINVAL;
5635 if (rcvd->max_resp_sz < sent->max_resp_sz)
5636 return -EINVAL;
5637 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5638 return -EINVAL;
5639 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005640 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005641 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005642 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005643 return -EINVAL;
5644 return 0;
5645}
Andy Adamson8d353012009-04-01 09:22:32 -04005646
Andy Adamson8d353012009-04-01 09:22:32 -04005647static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5648 struct nfs4_session *session)
5649{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005650 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005651
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005652 ret = nfs4_verify_fore_channel_attrs(args, session);
5653 if (ret)
5654 return ret;
5655 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005656}
5657
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005658static int _nfs4_proc_create_session(struct nfs_client *clp,
5659 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005660{
5661 struct nfs4_session *session = clp->cl_session;
5662 struct nfs41_create_session_args args = {
5663 .client = clp,
5664 .cb_program = NFS4_CALLBACK,
5665 };
5666 struct nfs41_create_session_res res = {
5667 .client = clp,
5668 };
5669 struct rpc_message msg = {
5670 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5671 .rpc_argp = &args,
5672 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005673 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04005674 };
5675 int status;
5676
5677 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005678 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005679
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005680 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005681
Trond Myklebust43095d32012-11-20 11:13:12 -05005682 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04005683 /* Verify the session's negotiated channel_attrs values */
5684 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005685 /* Increment the clientid slot sequence id */
5686 clp->cl_seqid++;
5687 }
5688
5689 return status;
5690}
5691
5692/*
5693 * Issues a CREATE_SESSION operation to the server.
5694 * It is the responsibility of the caller to verify the session is
5695 * expired before calling this routine.
5696 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005697int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005698{
5699 int status;
5700 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005701 struct nfs4_session *session = clp->cl_session;
5702
5703 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5704
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005705 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04005706 if (status)
5707 goto out;
5708
Andy Adamsonaacd5532011-11-09 13:58:21 -05005709 /* Init or reset the session slot tables */
5710 status = nfs4_setup_session_slot_tables(session);
5711 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005712 if (status)
5713 goto out;
5714
5715 ptr = (unsigned *)&session->sess_id.data[0];
5716 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5717 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005718out:
5719 dprintk("<-- %s\n", __func__);
5720 return status;
5721}
5722
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005723/*
5724 * Issue the over-the-wire RPC DESTROY_SESSION.
5725 * The caller must serialize access to this routine.
5726 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005727int nfs4_proc_destroy_session(struct nfs4_session *session,
5728 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005729{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005730 struct rpc_message msg = {
5731 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5732 .rpc_argp = session,
5733 .rpc_cred = cred,
5734 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005735 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005736
5737 dprintk("--> nfs4_proc_destroy_session\n");
5738
5739 /* session is still being setup */
5740 if (session->clp->cl_cons_state != NFS_CS_READY)
5741 return status;
5742
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005743 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005744
5745 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04005746 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005747 "Session has been destroyed regardless...\n", status);
5748
5749 dprintk("<-- nfs4_proc_destroy_session\n");
5750 return status;
5751}
5752
Trond Myklebust7b38c362012-05-23 13:23:31 -04005753/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005754 * Renew the cl_session lease.
5755 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005756struct nfs4_sequence_data {
5757 struct nfs_client *clp;
5758 struct nfs4_sequence_args args;
5759 struct nfs4_sequence_res res;
5760};
5761
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005762static void nfs41_sequence_release(void *data)
5763{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005764 struct nfs4_sequence_data *calldata = data;
5765 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005766
Alexandros Batsakis71358402010-02-05 03:45:05 -08005767 if (atomic_read(&clp->cl_count) > 1)
5768 nfs4_schedule_state_renewal(clp);
5769 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005770 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005771}
5772
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005773static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5774{
5775 switch(task->tk_status) {
5776 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005777 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5778 return -EAGAIN;
5779 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005780 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005781 }
5782 return 0;
5783}
5784
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005785static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005786{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005787 struct nfs4_sequence_data *calldata = data;
5788 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005789
Trond Myklebust14516c32010-07-31 14:29:06 -04005790 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5791 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005792
5793 if (task->tk_status < 0) {
5794 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005795 if (atomic_read(&clp->cl_count) == 1)
5796 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005797
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005798 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5799 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005800 return;
5801 }
5802 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005803 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005804out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005805 dprintk("<-- %s\n", __func__);
5806}
5807
5808static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5809{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005810 struct nfs4_sequence_data *calldata = data;
5811 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005812 struct nfs4_sequence_args *args;
5813 struct nfs4_sequence_res *res;
5814
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005815 args = task->tk_msg.rpc_argp;
5816 res = task->tk_msg.rpc_resp;
5817
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005818 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005819}
5820
5821static const struct rpc_call_ops nfs41_sequence_ops = {
5822 .rpc_call_done = nfs41_sequence_call_done,
5823 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005824 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005825};
5826
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005827static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
5828 struct rpc_cred *cred,
5829 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005830{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005831 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005832 struct rpc_message msg = {
5833 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5834 .rpc_cred = cred,
5835 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005836 struct rpc_task_setup task_setup_data = {
5837 .rpc_client = clp->cl_rpcclient,
5838 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005839 .callback_ops = &nfs41_sequence_ops,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005840 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5841 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005842
Alexandros Batsakis71358402010-02-05 03:45:05 -08005843 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005844 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005845 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005846 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005847 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005848 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005849 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05005850 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005851 if (is_privileged)
5852 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005853 msg.rpc_argp = &calldata->args;
5854 msg.rpc_resp = &calldata->res;
5855 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005856 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005857
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005858 return rpc_run_task(&task_setup_data);
5859}
5860
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005861static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005862{
5863 struct rpc_task *task;
5864 int ret = 0;
5865
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005866 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5867 return 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005868 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005869 if (IS_ERR(task))
5870 ret = PTR_ERR(task);
5871 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005872 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005873 dprintk("<-- %s status=%d\n", __func__, ret);
5874 return ret;
5875}
5876
5877static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5878{
5879 struct rpc_task *task;
5880 int ret;
5881
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005882 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005883 if (IS_ERR(task)) {
5884 ret = PTR_ERR(task);
5885 goto out;
5886 }
5887 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005888 if (!ret) {
5889 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5890
5891 if (task->tk_status == 0)
5892 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005893 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005894 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005895 rpc_put_task(task);
5896out:
5897 dprintk("<-- %s status=%d\n", __func__, ret);
5898 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005899}
5900
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005901struct nfs4_reclaim_complete_data {
5902 struct nfs_client *clp;
5903 struct nfs41_reclaim_complete_args arg;
5904 struct nfs41_reclaim_complete_res res;
5905};
5906
5907static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5908{
5909 struct nfs4_reclaim_complete_data *calldata = data;
5910
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005911 nfs41_setup_sequence(calldata->clp->cl_session,
5912 &calldata->arg.seq_args,
5913 &calldata->res.seq_res,
5914 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005915}
5916
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005917static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5918{
5919 switch(task->tk_status) {
5920 case 0:
5921 case -NFS4ERR_COMPLETE_ALREADY:
5922 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5923 break;
5924 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005925 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005926 /* fall through */
5927 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005928 return -EAGAIN;
5929 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005930 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005931 }
5932 return 0;
5933}
5934
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005935static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5936{
5937 struct nfs4_reclaim_complete_data *calldata = data;
5938 struct nfs_client *clp = calldata->clp;
5939 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5940
5941 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005942 if (!nfs41_sequence_done(task, res))
5943 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005944
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005945 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5946 rpc_restart_call_prepare(task);
5947 return;
5948 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005949 dprintk("<-- %s\n", __func__);
5950}
5951
5952static void nfs4_free_reclaim_complete_data(void *data)
5953{
5954 struct nfs4_reclaim_complete_data *calldata = data;
5955
5956 kfree(calldata);
5957}
5958
5959static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5960 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5961 .rpc_call_done = nfs4_reclaim_complete_done,
5962 .rpc_release = nfs4_free_reclaim_complete_data,
5963};
5964
5965/*
5966 * Issue a global reclaim complete.
5967 */
5968static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5969{
5970 struct nfs4_reclaim_complete_data *calldata;
5971 struct rpc_task *task;
5972 struct rpc_message msg = {
5973 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5974 };
5975 struct rpc_task_setup task_setup_data = {
5976 .rpc_client = clp->cl_rpcclient,
5977 .rpc_message = &msg,
5978 .callback_ops = &nfs4_reclaim_complete_call_ops,
5979 .flags = RPC_TASK_ASYNC,
5980 };
5981 int status = -ENOMEM;
5982
5983 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005984 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005985 if (calldata == NULL)
5986 goto out;
5987 calldata->clp = clp;
5988 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005989
Trond Myklebust9d12b212012-01-17 22:04:25 -05005990 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005991 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005992 msg.rpc_argp = &calldata->arg;
5993 msg.rpc_resp = &calldata->res;
5994 task_setup_data.callback_data = calldata;
5995 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005996 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005997 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005998 goto out;
5999 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05006000 status = nfs4_wait_for_completion_rpc_task(task);
6001 if (status == 0)
6002 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006003 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006004 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006005out:
6006 dprintk("<-- %s status=%d\n", __func__, status);
6007 return status;
6008}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006009
6010static void
6011nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6012{
6013 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00006014 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006015 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006016
6017 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00006018 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6019 * right now covering the LAYOUTGET we are about to send.
6020 * However, that is not so catastrophic, and there seems
6021 * to be no way to prevent it completely.
6022 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006023 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006024 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006025 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006026 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6027 NFS_I(lgp->args.inode)->layout,
6028 lgp->args.ctx->state)) {
6029 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00006030 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006031}
6032
6033static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6034{
6035 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07006036 struct inode *inode = lgp->args.inode;
6037 struct nfs_server *server = NFS_SERVER(inode);
6038 struct pnfs_layout_hdr *lo;
6039 struct nfs4_state *state = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006040
6041 dprintk("--> %s\n", __func__);
6042
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006043 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07006044 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006045
6046 switch (task->tk_status) {
6047 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07006048 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006049 case -NFS4ERR_LAYOUTTRYLATER:
6050 case -NFS4ERR_RECALLCONFLICT:
6051 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07006052 break;
6053 case -NFS4ERR_EXPIRED:
6054 case -NFS4ERR_BAD_STATEID:
6055 spin_lock(&inode->i_lock);
6056 lo = NFS_I(inode)->layout;
6057 if (!lo || list_empty(&lo->plh_segs)) {
6058 spin_unlock(&inode->i_lock);
6059 /* If the open stateid was bad, then recover it. */
6060 state = lgp->args.ctx->state;
6061 } else {
6062 LIST_HEAD(head);
6063
6064 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6065 spin_unlock(&inode->i_lock);
6066 /* Mark the bad layout state as invalid, then
6067 * retry using the open stateid. */
6068 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006069 }
6070 }
Trond Myklebustee314c22012-10-01 17:25:48 -07006071 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6072 rpc_restart_call_prepare(task);
6073out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006074 dprintk("<-- %s\n", __func__);
6075}
6076
Idan Kedar85541162012-08-02 11:47:10 +03006077static size_t max_response_pages(struct nfs_server *server)
6078{
6079 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6080 return nfs_page_array_len(0, max_resp_sz);
6081}
6082
6083static void nfs4_free_pages(struct page **pages, size_t size)
6084{
6085 int i;
6086
6087 if (!pages)
6088 return;
6089
6090 for (i = 0; i < size; i++) {
6091 if (!pages[i])
6092 break;
6093 __free_page(pages[i]);
6094 }
6095 kfree(pages);
6096}
6097
6098static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6099{
6100 struct page **pages;
6101 int i;
6102
6103 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6104 if (!pages) {
6105 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6106 return NULL;
6107 }
6108
6109 for (i = 0; i < size; i++) {
6110 pages[i] = alloc_page(gfp_flags);
6111 if (!pages[i]) {
6112 dprintk("%s: failed to allocate page\n", __func__);
6113 nfs4_free_pages(pages, size);
6114 return NULL;
6115 }
6116 }
6117
6118 return pages;
6119}
6120
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006121static void nfs4_layoutget_release(void *calldata)
6122{
6123 struct nfs4_layoutget *lgp = calldata;
Idan Kedar85541162012-08-02 11:47:10 +03006124 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6125 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006126
6127 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006128 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006129 put_nfs_open_context(lgp->args.ctx);
6130 kfree(calldata);
6131 dprintk("<-- %s\n", __func__);
6132}
6133
6134static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6135 .rpc_call_prepare = nfs4_layoutget_prepare,
6136 .rpc_call_done = nfs4_layoutget_done,
6137 .rpc_release = nfs4_layoutget_release,
6138};
6139
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006140struct pnfs_layout_segment *
6141nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006142{
6143 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Idan Kedar85541162012-08-02 11:47:10 +03006144 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006145 struct rpc_task *task;
6146 struct rpc_message msg = {
6147 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6148 .rpc_argp = &lgp->args,
6149 .rpc_resp = &lgp->res,
6150 };
6151 struct rpc_task_setup task_setup_data = {
6152 .rpc_client = server->client,
6153 .rpc_message = &msg,
6154 .callback_ops = &nfs4_layoutget_call_ops,
6155 .callback_data = lgp,
6156 .flags = RPC_TASK_ASYNC,
6157 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006158 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006159 int status = 0;
6160
6161 dprintk("--> %s\n", __func__);
6162
Idan Kedar85541162012-08-02 11:47:10 +03006163 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6164 if (!lgp->args.layout.pages) {
6165 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006166 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006167 }
6168 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
6169
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006170 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006171 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006172 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006173 task = rpc_run_task(&task_setup_data);
6174 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006175 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006176 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006177 if (status == 0)
6178 status = task->tk_status;
6179 if (status == 0)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006180 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006181 rpc_put_task(task);
6182 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006183 if (status)
6184 return ERR_PTR(status);
6185 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006186}
6187
Benny Halevycbe82602011-05-22 19:52:37 +03006188static void
6189nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6190{
6191 struct nfs4_layoutreturn *lrp = calldata;
6192
6193 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006194 nfs41_setup_sequence(lrp->clp->cl_session,
6195 &lrp->args.seq_args,
6196 &lrp->res.seq_res,
6197 task);
Benny Halevycbe82602011-05-22 19:52:37 +03006198}
6199
6200static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6201{
6202 struct nfs4_layoutreturn *lrp = calldata;
6203 struct nfs_server *server;
6204
6205 dprintk("--> %s\n", __func__);
6206
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006207 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03006208 return;
6209
6210 server = NFS_SERVER(lrp->args.inode);
6211 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006212 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006213 return;
6214 }
Benny Halevycbe82602011-05-22 19:52:37 +03006215 dprintk("<-- %s\n", __func__);
6216}
6217
6218static void nfs4_layoutreturn_release(void *calldata)
6219{
6220 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04006221 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006222
6223 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04006224 spin_lock(&lo->plh_inode->i_lock);
6225 if (lrp->res.lrs_present)
6226 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6227 lo->plh_block_lgets--;
6228 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006229 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006230 kfree(calldata);
6231 dprintk("<-- %s\n", __func__);
6232}
6233
6234static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6235 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6236 .rpc_call_done = nfs4_layoutreturn_done,
6237 .rpc_release = nfs4_layoutreturn_release,
6238};
6239
6240int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6241{
6242 struct rpc_task *task;
6243 struct rpc_message msg = {
6244 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6245 .rpc_argp = &lrp->args,
6246 .rpc_resp = &lrp->res,
6247 };
6248 struct rpc_task_setup task_setup_data = {
6249 .rpc_client = lrp->clp->cl_rpcclient,
6250 .rpc_message = &msg,
6251 .callback_ops = &nfs4_layoutreturn_call_ops,
6252 .callback_data = lrp,
6253 };
6254 int status;
6255
6256 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006257 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006258 task = rpc_run_task(&task_setup_data);
6259 if (IS_ERR(task))
6260 return PTR_ERR(task);
6261 status = task->tk_status;
6262 dprintk("<-- %s status=%d\n", __func__, status);
6263 rpc_put_task(task);
6264 return status;
6265}
6266
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006267/*
6268 * Retrieve the list of Data Server devices from the MDS.
6269 */
6270static int _nfs4_getdevicelist(struct nfs_server *server,
6271 const struct nfs_fh *fh,
6272 struct pnfs_devicelist *devlist)
6273{
6274 struct nfs4_getdevicelist_args args = {
6275 .fh = fh,
6276 .layoutclass = server->pnfs_curr_ld->id,
6277 };
6278 struct nfs4_getdevicelist_res res = {
6279 .devlist = devlist,
6280 };
6281 struct rpc_message msg = {
6282 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6283 .rpc_argp = &args,
6284 .rpc_resp = &res,
6285 };
6286 int status;
6287
6288 dprintk("--> %s\n", __func__);
6289 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6290 &res.seq_res, 0);
6291 dprintk("<-- %s status=%d\n", __func__, status);
6292 return status;
6293}
6294
6295int nfs4_proc_getdevicelist(struct nfs_server *server,
6296 const struct nfs_fh *fh,
6297 struct pnfs_devicelist *devlist)
6298{
6299 struct nfs4_exception exception = { };
6300 int err;
6301
6302 do {
6303 err = nfs4_handle_exception(server,
6304 _nfs4_getdevicelist(server, fh, devlist),
6305 &exception);
6306 } while (exception.retry);
6307
6308 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6309 err, devlist->num_devs);
6310
6311 return err;
6312}
6313EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6314
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006315static int
6316_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6317{
6318 struct nfs4_getdeviceinfo_args args = {
6319 .pdev = pdev,
6320 };
6321 struct nfs4_getdeviceinfo_res res = {
6322 .pdev = pdev,
6323 };
6324 struct rpc_message msg = {
6325 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6326 .rpc_argp = &args,
6327 .rpc_resp = &res,
6328 };
6329 int status;
6330
6331 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006332 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006333 dprintk("<-- %s status=%d\n", __func__, status);
6334
6335 return status;
6336}
6337
6338int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6339{
6340 struct nfs4_exception exception = { };
6341 int err;
6342
6343 do {
6344 err = nfs4_handle_exception(server,
6345 _nfs4_proc_getdeviceinfo(server, pdev),
6346 &exception);
6347 } while (exception.retry);
6348 return err;
6349}
6350EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6351
Andy Adamson863a3c62011-03-23 13:27:54 +00006352static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6353{
6354 struct nfs4_layoutcommit_data *data = calldata;
6355 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006356 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00006357
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006358 nfs41_setup_sequence(session,
6359 &data->args.seq_args,
6360 &data->res.seq_res,
6361 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00006362}
6363
6364static void
6365nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6366{
6367 struct nfs4_layoutcommit_data *data = calldata;
6368 struct nfs_server *server = NFS_SERVER(data->args.inode);
6369
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006370 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00006371 return;
6372
6373 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006374 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6375 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6376 case -NFS4ERR_BADLAYOUT: /* no layout */
6377 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006378 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006379 break;
6380 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006381 nfs_post_op_update_inode_force_wcc(data->args.inode,
6382 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006383 break;
6384 default:
6385 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6386 rpc_restart_call_prepare(task);
6387 return;
6388 }
6389 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006390}
6391
6392static void nfs4_layoutcommit_release(void *calldata)
6393{
6394 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04006395 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07006396 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00006397
Andy Adamsondb29c082011-07-30 20:52:38 -04006398 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006399 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04006400 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6401 list_del_init(&lseg->pls_lc_list);
6402 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6403 &lseg->pls_flags))
Trond Myklebust9369a432012-09-18 20:57:08 -04006404 pnfs_put_lseg(lseg);
Peng Taoa9bae562011-07-30 20:52:33 -04006405 }
Peng Tao92407e72011-10-23 20:21:17 -07006406
6407 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6408 smp_mb__after_clear_bit();
6409 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6410
Andy Adamson863a3c62011-03-23 13:27:54 +00006411 put_rpccred(data->cred);
6412 kfree(data);
6413}
6414
6415static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6416 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6417 .rpc_call_done = nfs4_layoutcommit_done,
6418 .rpc_release = nfs4_layoutcommit_release,
6419};
6420
6421int
Andy Adamsonef311532011-03-12 02:58:10 -05006422nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006423{
6424 struct rpc_message msg = {
6425 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6426 .rpc_argp = &data->args,
6427 .rpc_resp = &data->res,
6428 .rpc_cred = data->cred,
6429 };
6430 struct rpc_task_setup task_setup_data = {
6431 .task = &data->task,
6432 .rpc_client = NFS_CLIENT(data->args.inode),
6433 .rpc_message = &msg,
6434 .callback_ops = &nfs4_layoutcommit_ops,
6435 .callback_data = data,
6436 .flags = RPC_TASK_ASYNC,
6437 };
6438 struct rpc_task *task;
6439 int status = 0;
6440
6441 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6442 "lbw: %llu inode %lu\n",
6443 data->task.tk_pid, sync,
6444 data->args.lastbytewritten,
6445 data->args.inode->i_ino);
6446
Trond Myklebust9d12b212012-01-17 22:04:25 -05006447 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006448 task = rpc_run_task(&task_setup_data);
6449 if (IS_ERR(task))
6450 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006451 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006452 goto out;
6453 status = nfs4_wait_for_completion_rpc_task(task);
6454 if (status != 0)
6455 goto out;
6456 status = task->tk_status;
6457out:
6458 dprintk("%s: status %d\n", __func__, status);
6459 rpc_put_task(task);
6460 return status;
6461}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006462
6463static int
6464_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6465 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6466{
6467 struct nfs41_secinfo_no_name_args args = {
6468 .style = SECINFO_STYLE_CURRENT_FH,
6469 };
6470 struct nfs4_secinfo_res res = {
6471 .flavors = flavors,
6472 };
6473 struct rpc_message msg = {
6474 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6475 .rpc_argp = &args,
6476 .rpc_resp = &res,
6477 };
6478 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6479}
6480
6481static int
6482nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6483 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6484{
6485 struct nfs4_exception exception = { };
6486 int err;
6487 do {
6488 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6489 switch (err) {
6490 case 0:
6491 case -NFS4ERR_WRONGSEC:
6492 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006493 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006494 default:
6495 err = nfs4_handle_exception(server, err, &exception);
6496 }
6497 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006498out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006499 return err;
6500}
6501
6502static int
6503nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6504 struct nfs_fsinfo *info)
6505{
6506 int err;
6507 struct page *page;
6508 rpc_authflavor_t flavor;
6509 struct nfs4_secinfo_flavors *flavors;
6510
6511 page = alloc_page(GFP_KERNEL);
6512 if (!page) {
6513 err = -ENOMEM;
6514 goto out;
6515 }
6516
6517 flavors = page_address(page);
6518 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6519
6520 /*
6521 * Fall back on "guess and check" method if
6522 * the server doesn't support SECINFO_NO_NAME
6523 */
6524 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6525 err = nfs4_find_root_sec(server, fhandle, info);
6526 goto out_freepage;
6527 }
6528 if (err)
6529 goto out_freepage;
6530
6531 flavor = nfs_find_best_sec(flavors);
6532 if (err == 0)
6533 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6534
6535out_freepage:
6536 put_page(page);
6537 if (err == -EACCES)
6538 return -EPERM;
6539out:
6540 return err;
6541}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006542
6543static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006544{
6545 int status;
6546 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006547 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006548 };
6549 struct nfs41_test_stateid_res res;
6550 struct rpc_message msg = {
6551 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6552 .rpc_argp = &args,
6553 .rpc_resp = &res,
6554 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006555
Chuck Lever38527b12012-07-11 16:30:23 -04006556 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006557 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006558 nfs4_set_sequence_privileged(&args.seq_args);
6559 status = nfs4_call_sync_sequence(server->client, server, &msg,
6560 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04006561 if (status != NFS_OK) {
6562 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04006563 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04006564 }
6565 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04006566 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006567}
6568
Chuck Lever38527b12012-07-11 16:30:23 -04006569/**
6570 * nfs41_test_stateid - perform a TEST_STATEID operation
6571 *
6572 * @server: server / transport on which to perform the operation
6573 * @stateid: state ID to test
6574 *
6575 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6576 * Otherwise a negative NFS4ERR value is returned if the operation
6577 * failed or the state ID is not currently valid.
6578 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006579static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006580{
6581 struct nfs4_exception exception = { };
6582 int err;
6583 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006584 err = _nfs41_test_stateid(server, stateid);
6585 if (err != -NFS4ERR_DELAY)
6586 break;
6587 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04006588 } while (exception.retry);
6589 return err;
6590}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006591
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006592static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006593{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006594 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006595 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006596 };
6597 struct nfs41_free_stateid_res res;
6598 struct rpc_message msg = {
6599 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6600 .rpc_argp = &args,
6601 .rpc_resp = &res,
6602 };
Chuck Lever38527b12012-07-11 16:30:23 -04006603 int status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006604
Chuck Lever38527b12012-07-11 16:30:23 -04006605 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006606 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006607 nfs4_set_sequence_privileged(&args.seq_args);
Chuck Lever38527b12012-07-11 16:30:23 -04006608 status = nfs4_call_sync_sequence(server->client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006609 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04006610 dprintk("NFS reply free_stateid: %d\n", status);
6611 return status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006612}
6613
Chuck Lever38527b12012-07-11 16:30:23 -04006614/**
6615 * nfs41_free_stateid - perform a FREE_STATEID operation
6616 *
6617 * @server: server / transport on which to perform the operation
6618 * @stateid: state ID to release
6619 *
6620 * Returns NFS_OK if the server freed "stateid". Otherwise a
6621 * negative NFS4ERR value is returned.
6622 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006623static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006624{
6625 struct nfs4_exception exception = { };
6626 int err;
6627 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006628 err = _nfs4_free_stateid(server, stateid);
6629 if (err != -NFS4ERR_DELAY)
6630 break;
6631 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006632 } while (exception.retry);
6633 return err;
6634}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006635
6636static bool nfs41_match_stateid(const nfs4_stateid *s1,
6637 const nfs4_stateid *s2)
6638{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006639 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006640 return false;
6641
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006642 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006643 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006644 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006645 return true;
6646
6647 return false;
6648}
6649
Andy Adamson557134a2009-04-01 09:21:53 -04006650#endif /* CONFIG_NFS_V4_1 */
6651
Trond Myklebust36281ca2012-03-04 18:13:56 -05006652static bool nfs4_match_stateid(const nfs4_stateid *s1,
6653 const nfs4_stateid *s2)
6654{
Trond Myklebustf597c532012-03-04 18:13:56 -05006655 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006656}
6657
6658
Trond Myklebust17280172012-03-11 13:11:00 -04006659static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006660 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006661 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006662 .recover_open = nfs4_open_reclaim,
6663 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006664 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006665 .get_clid_cred = nfs4_get_setclientid_cred,
Chuck Lever05f4c352012-09-14 17:24:32 -04006666 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006667};
6668
Andy Adamson591d71c2009-04-01 09:22:47 -04006669#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006670static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006671 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6672 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6673 .recover_open = nfs4_open_reclaim,
6674 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006675 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006676 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006677 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04006678 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04006679};
6680#endif /* CONFIG_NFS_V4_1 */
6681
Trond Myklebust17280172012-03-11 13:11:00 -04006682static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006683 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006684 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006685 .recover_open = nfs4_open_expired,
6686 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006687 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006688 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006689};
6690
Andy Adamson591d71c2009-04-01 09:22:47 -04006691#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006692static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006693 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6694 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006695 .recover_open = nfs41_open_expired,
6696 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006697 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006698 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006699};
6700#endif /* CONFIG_NFS_V4_1 */
6701
Trond Myklebust17280172012-03-11 13:11:00 -04006702static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006703 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006704 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006705 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006706};
6707
6708#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006709static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006710 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006711 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006712 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006713};
6714#endif
6715
Trond Myklebust97dc1352010-06-16 09:52:26 -04006716static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6717 .minor_version = 0,
6718 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006719 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006720 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006721 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6722 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6723 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006724};
6725
6726#if defined(CONFIG_NFS_V4_1)
6727static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6728 .minor_version = 1,
Bryan Schumaker104287c2012-11-12 14:13:13 -05006729 .call_sync = nfs4_call_sync_sequence,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006730 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006731 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006732 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6733 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6734 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006735};
6736#endif
6737
Trond Myklebust97dc1352010-06-16 09:52:26 -04006738const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6739 [0] = &nfs_v4_0_minor_ops,
6740#if defined(CONFIG_NFS_V4_1)
6741 [1] = &nfs_v4_1_minor_ops,
6742#endif
6743};
6744
Bryan Schumaker73a79702012-07-16 16:39:12 -04006745const struct inode_operations nfs4_dir_inode_operations = {
6746 .create = nfs_create,
6747 .lookup = nfs_lookup,
6748 .atomic_open = nfs_atomic_open,
6749 .link = nfs_link,
6750 .unlink = nfs_unlink,
6751 .symlink = nfs_symlink,
6752 .mkdir = nfs_mkdir,
6753 .rmdir = nfs_rmdir,
6754 .mknod = nfs_mknod,
6755 .rename = nfs_rename,
6756 .permission = nfs_permission,
6757 .getattr = nfs_getattr,
6758 .setattr = nfs_setattr,
6759 .getxattr = generic_getxattr,
6760 .setxattr = generic_setxattr,
6761 .listxattr = generic_listxattr,
6762 .removexattr = generic_removexattr,
6763};
6764
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006765static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006766 .permission = nfs_permission,
6767 .getattr = nfs_getattr,
6768 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006769 .getxattr = generic_getxattr,
6770 .setxattr = generic_setxattr,
6771 .listxattr = generic_listxattr,
6772 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006773};
6774
David Howells509de812006-08-22 20:06:11 -04006775const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006776 .version = 4, /* protocol version */
6777 .dentry_ops = &nfs4_dentry_operations,
6778 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006779 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006780 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006781 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04006782 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04006783 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006784 .getattr = nfs4_proc_getattr,
6785 .setattr = nfs4_proc_setattr,
6786 .lookup = nfs4_proc_lookup,
6787 .access = nfs4_proc_access,
6788 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006789 .create = nfs4_proc_create,
6790 .remove = nfs4_proc_remove,
6791 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04006792 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006793 .unlink_done = nfs4_proc_unlink_done,
6794 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006795 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04006796 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04006797 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006798 .link = nfs4_proc_link,
6799 .symlink = nfs4_proc_symlink,
6800 .mkdir = nfs4_proc_mkdir,
6801 .rmdir = nfs4_proc_remove,
6802 .readdir = nfs4_proc_readdir,
6803 .mknod = nfs4_proc_mknod,
6804 .statfs = nfs4_proc_statfs,
6805 .fsinfo = nfs4_proc_fsinfo,
6806 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006807 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006808 .decode_dirent = nfs4_decode_dirent,
6809 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04006810 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04006811 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05006812 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006813 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04006814 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04006815 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006816 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006817 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04006818 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006819 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006820 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006821 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006822 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006823 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04006824 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04006825 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04006826 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00006827 .init_client = nfs4_init_client,
Bryan Schumakercdb7eced2012-06-20 15:53:45 -04006828 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04006829 .create_server = nfs4_create_server,
6830 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006831};
6832
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006833static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6834 .prefix = XATTR_NAME_NFSV4_ACL,
6835 .list = nfs4_xattr_list_nfs4_acl,
6836 .get = nfs4_xattr_get_nfs4_acl,
6837 .set = nfs4_xattr_set_nfs4_acl,
6838};
6839
6840const struct xattr_handler *nfs4_xattr_handlers[] = {
6841 &nfs4_xattr_nfs4_acl_handler,
6842 NULL
6843};
6844
Linus Torvalds1da177e2005-04-16 15:20:36 -07006845/*
6846 * Local variables:
6847 * c-basic-offset: 8
6848 * End:
6849 */