blob: d75e2a2576eb681393b520f5a904c62f9086cf80 [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>
Andy Adamson5a0ffe52009-04-01 09:23:18 -040055#include <linux/sunrpc/bc_xprt.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000056#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050057#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010058#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Trond Myklebust4ce79712005-06-22 17:16:21 +000060#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050062#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050063#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040064#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040065#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040066#include "netns.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68#define NFSDBG_FACILITY NFSDBG_PROC
69
Trond Myklebust2066fe82006-09-15 08:30:46 -040070#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define NFS4_POLL_RETRY_MAX (15*HZ)
72
Trond Myklebustcdd4e682006-01-03 09:55:12 +010073struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010074static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080075static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050077static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Chuck Lever81934dd2012-03-01 17:01:57 -050078static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Bryan Schumakerbae36242012-05-10 15:07:31 -040079static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *);
Trond Myklebust99367812007-07-17 21:52:41 -040080static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040081static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
82 struct nfs_fattr *fattr, struct iattr *sattr,
83 struct nfs4_state *state);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040084#ifdef CONFIG_NFS_V4_1
Bryan Schumaker1cab0652012-01-31 10:39:29 -050085static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *);
86static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040087#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070088/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050089static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
Trond Myklebust52567b02009-10-23 14:46:42 -040091 if (err >= -1000)
92 return err;
93 switch (err) {
94 case -NFS4ERR_RESOURCE:
95 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +000096 case -NFS4ERR_WRONGSEC:
97 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -080098 case -NFS4ERR_BADOWNER:
99 case -NFS4ERR_BADNAME:
100 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400101 case -NFS4ERR_SHARE_DENIED:
102 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400103 case -NFS4ERR_MINOR_VERS_MISMATCH:
104 return -EPROTONOSUPPORT;
Weston Andros Adamson6168f622012-09-10 14:00:46 -0400105 case -NFS4ERR_ACCESS:
106 return -EACCES;
Trond Myklebust52567b02009-10-23 14:46:42 -0400107 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700109 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400110 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400112 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113}
114
115/*
116 * This is our standard bitmap for GETATTR requests.
117 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400118const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 FATTR4_WORD0_TYPE
120 | FATTR4_WORD0_CHANGE
121 | FATTR4_WORD0_SIZE
122 | FATTR4_WORD0_FSID
123 | FATTR4_WORD0_FILEID,
124 FATTR4_WORD1_MODE
125 | FATTR4_WORD1_NUMLINKS
126 | FATTR4_WORD1_OWNER
127 | FATTR4_WORD1_OWNER_GROUP
128 | FATTR4_WORD1_RAWDEV
129 | FATTR4_WORD1_SPACE_USED
130 | FATTR4_WORD1_TIME_ACCESS
131 | FATTR4_WORD1_TIME_METADATA
132 | FATTR4_WORD1_TIME_MODIFY
133};
134
Trond Myklebust1549210f2012-06-05 09:16:47 -0400135static const u32 nfs4_pnfs_open_bitmap[3] = {
136 FATTR4_WORD0_TYPE
137 | FATTR4_WORD0_CHANGE
138 | FATTR4_WORD0_SIZE
139 | FATTR4_WORD0_FSID
140 | FATTR4_WORD0_FILEID,
141 FATTR4_WORD1_MODE
142 | FATTR4_WORD1_NUMLINKS
143 | FATTR4_WORD1_OWNER
144 | FATTR4_WORD1_OWNER_GROUP
145 | FATTR4_WORD1_RAWDEV
146 | FATTR4_WORD1_SPACE_USED
147 | FATTR4_WORD1_TIME_ACCESS
148 | FATTR4_WORD1_TIME_METADATA
149 | FATTR4_WORD1_TIME_MODIFY,
150 FATTR4_WORD2_MDSTHRESHOLD
151};
152
Andy Adamsone23008e2012-10-02 21:07:32 -0400153static const u32 nfs4_open_noattr_bitmap[3] = {
154 FATTR4_WORD0_TYPE
155 | FATTR4_WORD0_CHANGE
156 | FATTR4_WORD0_FILEID,
157};
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159const u32 nfs4_statfs_bitmap[2] = {
160 FATTR4_WORD0_FILES_AVAIL
161 | FATTR4_WORD0_FILES_FREE
162 | FATTR4_WORD0_FILES_TOTAL,
163 FATTR4_WORD1_SPACE_AVAIL
164 | FATTR4_WORD1_SPACE_FREE
165 | FATTR4_WORD1_SPACE_TOTAL
166};
167
Trond Myklebust4ce79712005-06-22 17:16:21 +0000168const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 FATTR4_WORD0_MAXLINK
170 | FATTR4_WORD0_MAXNAME,
171 0
172};
173
Fred Isamandae100c2011-07-30 20:52:37 -0400174const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 | FATTR4_WORD0_MAXREAD
176 | FATTR4_WORD0_MAXWRITE
177 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700178 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400179 | FATTR4_WORD1_FS_LAYOUT_TYPES,
180 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181};
182
Manoj Naik830b8e32006-06-09 09:34:25 -0400183const u32 nfs4_fs_locations_bitmap[2] = {
184 FATTR4_WORD0_TYPE
185 | FATTR4_WORD0_CHANGE
186 | FATTR4_WORD0_SIZE
187 | FATTR4_WORD0_FSID
188 | FATTR4_WORD0_FILEID
189 | FATTR4_WORD0_FS_LOCATIONS,
190 FATTR4_WORD1_MODE
191 | FATTR4_WORD1_NUMLINKS
192 | FATTR4_WORD1_OWNER
193 | FATTR4_WORD1_OWNER_GROUP
194 | FATTR4_WORD1_RAWDEV
195 | FATTR4_WORD1_SPACE_USED
196 | FATTR4_WORD1_TIME_ACCESS
197 | FATTR4_WORD1_TIME_METADATA
198 | FATTR4_WORD1_TIME_MODIFY
199 | FATTR4_WORD1_MOUNTED_ON_FILEID
200};
201
Al Virobc4785c2006-10-19 23:28:51 -0700202static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 struct nfs4_readdir_arg *readdir)
204{
Al Viro0dbb4c62006-10-19 23:28:49 -0700205 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000208 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
210 return;
211 }
212
213 readdir->cookie = 0;
214 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
215 if (cookie == 2)
216 return;
217
218 /*
219 * NFSv4 servers do not return entries for '.' and '..'
220 * Therefore, we fake these entries here. We let '.'
221 * have cookie 0 and '..' have cookie 1. Note that
222 * when talking to the server, we always send cookie 0
223 * instead of 1 or 2.
224 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800225 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 if (cookie == 0) {
228 *p++ = xdr_one; /* next */
229 *p++ = xdr_zero; /* cookie, first word */
230 *p++ = xdr_one; /* cookie, second word */
231 *p++ = xdr_one; /* entry len */
232 memcpy(p, ".\0\0\0", 4); /* entry */
233 p++;
234 *p++ = xdr_one; /* bitmap length */
235 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
236 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400237 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 }
239
240 *p++ = xdr_one; /* next */
241 *p++ = xdr_zero; /* cookie, first word */
242 *p++ = xdr_two; /* cookie, second word */
243 *p++ = xdr_two; /* entry len */
244 memcpy(p, "..\0\0", 4); /* entry */
245 p++;
246 *p++ = xdr_one; /* bitmap length */
247 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
248 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400249 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 readdir->pgbase = (char *)p - (char *)start;
252 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800253 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254}
255
Trond Myklebust65de8722008-12-23 15:21:44 -0500256static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
257{
258 int res = 0;
259
260 might_sleep();
261
262 if (*timeout <= 0)
263 *timeout = NFS4_POLL_RETRY_MIN;
264 if (*timeout > NFS4_POLL_RETRY_MAX)
265 *timeout = NFS4_POLL_RETRY_MAX;
Jeff Laytond3103102011-12-01 22:44:39 +0100266 freezable_schedule_timeout_killable(*timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500267 if (fatal_signal_pending(current))
268 res = -ERESTARTSYS;
269 *timeout <<= 1;
270 return res;
271}
272
273/* This is the error handling routine for processes that are allowed
274 * to sleep.
275 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800276static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500277{
278 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500279 struct nfs4_state *state = exception->state;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500280 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500281 int ret = errorcode;
282
283 exception->retry = 0;
284 switch(errorcode) {
285 case 0:
286 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500287 case -NFS4ERR_OPENMODE:
Bryan Schumaker011e2a72012-06-20 15:53:43 -0400288 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400289 nfs4_inode_return_delegation(inode);
Trond Myklebust3114ea72012-03-07 16:39:06 -0500290 exception->retry = 1;
291 return 0;
292 }
293 if (state == NULL)
294 break;
295 nfs4_schedule_stateid_recovery(server, state);
296 goto wait_on_recovery;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500297 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500298 case -NFS4ERR_ADMIN_REVOKED:
299 case -NFS4ERR_BAD_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500300 if (state == NULL)
301 break;
Trond Myklebust14977482012-03-27 18:31:25 -0400302 nfs_remove_bad_delegation(state->inode);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500303 nfs4_schedule_stateid_recovery(server, state);
304 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400305 case -NFS4ERR_EXPIRED:
306 if (state != NULL)
307 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust65de8722008-12-23 15:21:44 -0500308 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500309 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500310 nfs4_schedule_lease_recovery(clp);
311 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500312#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400313 case -NFS4ERR_BADSESSION:
314 case -NFS4ERR_BADSLOT:
315 case -NFS4ERR_BAD_HIGH_SLOT:
316 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
317 case -NFS4ERR_DEADSESSION:
318 case -NFS4ERR_SEQ_FALSE_RETRY:
319 case -NFS4ERR_SEQ_MISORDERED:
320 dprintk("%s ERROR: %d Reset session\n", __func__,
321 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400322 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400323 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500324#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500325 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500326 if (exception->timeout > HZ) {
327 /* We have retried a decent amount, time to
328 * fail
329 */
330 ret = -EBUSY;
331 break;
332 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500333 case -NFS4ERR_GRACE:
334 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -0500335 case -EKEYEXPIRED:
Trond Myklebust65de8722008-12-23 15:21:44 -0500336 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
Benny Halevy510b8172009-04-01 09:22:14 -0400381/*
Trond Myklebust87dda672012-11-20 19:49:20 -0500382 * nfs4_shrink_slot_table - free retired slots from the slot table
383 */
384static void nfs4_shrink_slot_table(struct nfs4_slot_table *tbl, u32 newsize)
385{
386 struct nfs4_slot **p;
387 if (newsize >= tbl->max_slots)
388 return;
389
390 p = &tbl->slots;
391 while (newsize--)
392 p = &(*p)->next;
393 while (*p) {
394 struct nfs4_slot *slot = *p;
395
396 *p = slot->next;
397 kfree(slot);
398 tbl->max_slots--;
399 }
400}
401
402/*
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400403 * nfs4_free_slot - free a slot and efficiently update slot table.
404 *
405 * freeing a slot is trivially done by clearing its respective bit
406 * in the bitmap.
407 * If the freed slotid equals highest_used_slotid we want to update it
408 * so that the server would be able to size down the slot table if needed,
409 * otherwise we know that the highest_used_slotid is still in use.
410 * When updating highest_used_slotid there may be "holes" in the bitmap
411 * so we need to scan down from highest_used_slotid to 0 looking for the now
412 * highest slotid in use.
Trond Myklebust45d43c22012-02-06 19:38:51 -0500413 * If none found, highest_used_slotid is set to NFS4_NO_SLOT.
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500414 *
415 * Must be called while holding tbl->slot_tbl_lock
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400416 */
417static void
Trond Myklebustf4af6e2a2012-11-20 14:17:32 -0500418nfs4_free_slot(struct nfs4_slot_table *tbl, struct nfs4_slot *slot)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400419{
Trond Myklebustf4af6e2a2012-11-20 14:17:32 -0500420 u32 slotid = slot->slot_nr;
421
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400422 /* clear used bit in bitmap */
423 __clear_bit(slotid, tbl->used_slots);
424
425 /* update highest_used_slotid when it is freed */
426 if (slotid == tbl->highest_used_slotid) {
Trond Myklebustf4af6e2a2012-11-20 14:17:32 -0500427 u32 new_max = find_last_bit(tbl->used_slots, slotid);
428 if (new_max < slotid)
429 tbl->highest_used_slotid = new_max;
Trond Myklebust5d633602012-11-23 13:09:38 -0500430 else {
Trond Myklebust45d43c22012-02-06 19:38:51 -0500431 tbl->highest_used_slotid = NFS4_NO_SLOT;
Trond Myklebust5d633602012-11-23 13:09:38 -0500432 nfs4_session_drain_complete(tbl->session, tbl);
433 }
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400434 }
Trond Myklebust45d43c22012-02-06 19:38:51 -0500435 dprintk("%s: slotid %u highest_used_slotid %d\n", __func__,
436 slotid, tbl->highest_used_slotid);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400437}
438
Trond Myklebust961a8282012-01-17 22:57:37 -0500439bool nfs4_set_task_privileged(struct rpc_task *task, void *dummy)
440{
441 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
442 return true;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400443}
444
Trond Myklebustd185a332010-06-16 09:52:25 -0400445static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400446{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500447 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400448 struct nfs4_slot_table *tbl;
449
Benny Halevydfb4f3092010-09-24 09:17:01 -0400450 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400451 /* just wake up the next guy waiting since
452 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500453 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500454 return;
Andy Adamson13615872009-04-01 09:22:17 -0400455 }
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500456 tbl = res->sr_slot->table;
457 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500458
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500459 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustf4af6e2a2012-11-20 14:17:32 -0500460 nfs4_free_slot(tbl, res->sr_slot);
Trond Myklebust5d633602012-11-23 13:09:38 -0500461 if (!nfs4_session_draining(session))
462 rpc_wake_up_first(&tbl->slot_tbl_waitq,
463 nfs4_set_task_privileged, NULL);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500464 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400465 res->sr_slot = NULL;
Andy Adamson13615872009-04-01 09:22:17 -0400466}
467
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500468/* Update the client's idea of target_highest_slotid */
469static void nfs41_set_target_slotid_locked(struct nfs4_slot_table *tbl,
470 u32 target_highest_slotid)
471{
Trond Myklebust97e548a2012-11-20 14:45:48 -0500472 unsigned int max_slotid, i;
473
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500474 if (tbl->target_highest_slotid == target_highest_slotid)
475 return;
476 tbl->target_highest_slotid = target_highest_slotid;
477 tbl->generation++;
Trond Myklebust97e548a2012-11-20 14:45:48 -0500478
Trond Myklebust87dda672012-11-20 19:49:20 -0500479 max_slotid = min(NFS4_MAX_SLOT_TABLE - 1, tbl->target_highest_slotid);
Trond Myklebust97e548a2012-11-20 14:45:48 -0500480 for (i = tbl->max_slotid + 1; i <= max_slotid; i++)
481 rpc_wake_up_next(&tbl->slot_tbl_waitq);
482 tbl->max_slotid = max_slotid;
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500483}
484
Trond Myklebustce008c42012-11-20 15:16:30 -0500485void nfs41_set_target_slotid(struct nfs4_slot_table *tbl,
486 u32 target_highest_slotid)
487{
488 spin_lock(&tbl->slot_tbl_lock);
489 nfs41_set_target_slotid_locked(tbl, target_highest_slotid);
490 spin_unlock(&tbl->slot_tbl_lock);
491}
492
Trond Myklebustda0507b2012-11-20 18:10:30 -0500493static void nfs41_set_server_slotid_locked(struct nfs4_slot_table *tbl,
494 u32 highest_slotid)
495{
Trond Myklebustda0507b2012-11-20 18:10:30 -0500496 if (tbl->server_highest_slotid == highest_slotid)
497 return;
498 if (tbl->highest_used_slotid > highest_slotid)
499 return;
Trond Myklebust87dda672012-11-20 19:49:20 -0500500 /* Deallocate slots */
501 nfs4_shrink_slot_table(tbl, highest_slotid + 1);
Trond Myklebustda0507b2012-11-20 18:10:30 -0500502 tbl->server_highest_slotid = highest_slotid;
503}
504
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500505static void nfs41_update_target_slotid(struct nfs4_slot_table *tbl,
506 struct nfs4_slot *slot,
507 struct nfs4_sequence_res *res)
508{
509 spin_lock(&tbl->slot_tbl_lock);
510 if (tbl->generation != slot->generation)
511 goto out;
Trond Myklebustda0507b2012-11-20 18:10:30 -0500512 nfs41_set_server_slotid_locked(tbl, res->sr_highest_slotid);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500513 nfs41_set_target_slotid_locked(tbl, res->sr_target_highest_slotid);
514out:
515 spin_unlock(&tbl->slot_tbl_lock);
516}
517
Trond Myklebust14516c32010-07-31 14:29:06 -0400518static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400519{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500520 struct nfs4_session *session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500521 struct nfs4_slot *slot;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400522 unsigned long timestamp;
Trond Myklebust14516c32010-07-31 14:29:06 -0400523 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400524
525 /*
526 * sr_status remains 1 if an RPC level error occurred. The server
527 * may or may not have processed the sequence operation..
528 * Proceed as if the server received and processed the sequence
529 * operation.
530 */
531 if (res->sr_status == 1)
532 res->sr_status = NFS_OK;
533
Bryan Schumaker468f8612011-04-18 15:57:32 -0400534 /* don't increment the sequence number if the task wasn't sent */
535 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400536 goto out;
537
Trond Myklebust933602e2012-11-16 12:12:38 -0500538 slot = res->sr_slot;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500539 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500540
Andy Adamson691daf32009-12-04 15:55:39 -0500541 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400542 switch (res->sr_status) {
543 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400544 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust933602e2012-11-16 12:12:38 -0500545 ++slot->seq_nr;
546 timestamp = slot->renewal_time;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500547 clp = session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400548 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500549 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500550 if (res->sr_status_flags != 0)
551 nfs4_schedule_lease_recovery(clp);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500552 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400553 break;
554 case -NFS4ERR_DELAY:
555 /* The server detected a resend of the RPC call and
556 * returned NFS4ERR_DELAY as per Section 2.10.6.2
557 * of RFC5661.
558 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500559 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400560 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500561 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500562 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400563 goto out_retry;
564 default:
565 /* Just update the slot sequence no. */
Trond Myklebust933602e2012-11-16 12:12:38 -0500566 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400567 }
568out:
569 /* The session may be reset by one of the error handlers. */
570 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400571 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400572 return 1;
573out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400574 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400575 goto out;
576 rpc_delay(task, NFS4_POLL_RETRY_MAX);
577 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400578}
579
Trond Myklebust14516c32010-07-31 14:29:06 -0400580static int nfs4_sequence_done(struct rpc_task *task,
581 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400582{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500583 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400584 return 1;
585 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400586}
587
Trond Myklebust87dda672012-11-20 19:49:20 -0500588static struct nfs4_slot *nfs4_new_slot(struct nfs4_slot_table *tbl,
589 u32 slotid, u32 seq_init, gfp_t gfp_mask)
590{
591 struct nfs4_slot *slot;
592
593 slot = kzalloc(sizeof(*slot), gfp_mask);
594 if (slot) {
595 slot->table = tbl;
596 slot->slot_nr = slotid;
597 slot->seq_nr = seq_init;
598 }
599 return slot;
600}
601
602static struct nfs4_slot *nfs4_find_or_create_slot(struct nfs4_slot_table *tbl,
603 u32 slotid, u32 seq_init, gfp_t gfp_mask)
604{
605 struct nfs4_slot **p, *slot;
606
607 p = &tbl->slots;
608 for (;;) {
609 if (*p == NULL) {
610 *p = nfs4_new_slot(tbl, tbl->max_slots,
611 seq_init, gfp_mask);
612 if (*p == NULL)
613 break;
614 tbl->max_slots++;
615 }
616 slot = *p;
617 if (slot->slot_nr == slotid)
618 return slot;
619 p = &slot->next;
620 }
Trond Myklebust69d206b2012-11-22 13:21:02 -0500621 return ERR_PTR(-ENOMEM);
Trond Myklebust87dda672012-11-20 19:49:20 -0500622}
623
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400624/*
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500625 * nfs4_alloc_slot - efficiently look for a free slot
Benny Halevy510b8172009-04-01 09:22:14 -0400626 *
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500627 * nfs4_alloc_slot looks for an unset bit in the used_slots bitmap.
Benny Halevy510b8172009-04-01 09:22:14 -0400628 * If found, we mark the slot as used, update the highest_used_slotid,
629 * and respectively set up the sequence operation args.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400630 *
631 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400632 */
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500633static struct nfs4_slot *nfs4_alloc_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400634{
Trond Myklebust69d206b2012-11-22 13:21:02 -0500635 struct nfs4_slot *ret = ERR_PTR(-EBUSY);
Trond Myklebust45d43c22012-02-06 19:38:51 -0500636 u32 slotid;
Benny Halevy510b8172009-04-01 09:22:14 -0400637
Trond Myklebust45d43c22012-02-06 19:38:51 -0500638 dprintk("--> %s used_slots=%04lx highest_used=%u max_slots=%u\n",
Benny Halevy510b8172009-04-01 09:22:14 -0400639 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
Trond Myklebust87dda672012-11-20 19:49:20 -0500640 tbl->max_slotid + 1);
Trond Myklebust97e548a2012-11-20 14:45:48 -0500641 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slotid + 1);
642 if (slotid > tbl->max_slotid)
Benny Halevy510b8172009-04-01 09:22:14 -0400643 goto out;
Trond Myklebust87dda672012-11-20 19:49:20 -0500644 ret = nfs4_find_or_create_slot(tbl, slotid, 1, GFP_NOWAIT);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500645 if (IS_ERR(ret))
Trond Myklebust87dda672012-11-20 19:49:20 -0500646 goto out;
Benny Halevy510b8172009-04-01 09:22:14 -0400647 __set_bit(slotid, tbl->used_slots);
Trond Myklebust45d43c22012-02-06 19:38:51 -0500648 if (slotid > tbl->highest_used_slotid ||
649 tbl->highest_used_slotid == NFS4_NO_SLOT)
Benny Halevy510b8172009-04-01 09:22:14 -0400650 tbl->highest_used_slotid = slotid;
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500651 ret->renewal_time = jiffies;
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500652 ret->generation = tbl->generation;
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500653
Benny Halevy510b8172009-04-01 09:22:14 -0400654out:
655 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500656 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
Trond Myklebust69d206b2012-11-22 13:21:02 -0500657 !IS_ERR(ret) ? ret->slot_nr : -1);
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500658 return ret;
Benny Halevy510b8172009-04-01 09:22:14 -0400659}
660
Trond Myklebust9d12b212012-01-17 22:04:25 -0500661static void nfs41_init_sequence(struct nfs4_sequence_args *args,
662 struct nfs4_sequence_res *res, int cache_reply)
663{
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500664 args->sa_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500665 args->sa_cache_this = 0;
666 if (cache_reply)
667 args->sa_cache_this = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500668 res->sr_slot = NULL;
669}
670
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000671int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400672 struct nfs4_sequence_args *args,
673 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400674 struct rpc_task *task)
675{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400676 struct nfs4_slot *slot;
677 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400678
679 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400680 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400681 if (res->sr_slot != NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400682 return 0;
683
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400684 tbl = &session->fc_slot_table;
685
Trond Myklebust69d206b2012-11-22 13:21:02 -0500686 task->tk_timeout = 0;
687
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400688 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400689 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800690 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500691 /* The state manager will wait until the slot table is empty */
Andy Adamson05f0d232009-12-04 15:55:37 -0500692 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400693 spin_unlock(&tbl->slot_tbl_lock);
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500694 dprintk("%s session is draining\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500695 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400696 }
697
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800698 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
699 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
700 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
701 spin_unlock(&tbl->slot_tbl_lock);
702 dprintk("%s enforce FIFO order\n", __func__);
703 return -EAGAIN;
704 }
705
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500706 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500707 if (IS_ERR(slot)) {
708 /* If out of memory, try again in 1/4 second */
709 if (slot == ERR_PTR(-ENOMEM))
710 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400711 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
712 spin_unlock(&tbl->slot_tbl_lock);
713 dprintk("<-- %s: no free slots\n", __func__);
714 return -EAGAIN;
715 }
716 spin_unlock(&tbl->slot_tbl_lock);
717
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800718 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500719
720 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400721
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500722 dprintk("<-- %s slotid=%d seqid=%d\n", __func__,
723 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400724
Benny Halevydfb4f3092010-09-24 09:17:01 -0400725 res->sr_slot = slot;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400726 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400727 /*
728 * sr_status is only set in decode_sequence, and so will remain
729 * set to 1 if an rpc level failure occurs.
730 */
731 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400732 return 0;
733}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000734EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400735
Trond Myklebust035168a2010-06-16 09:52:26 -0400736int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400737 struct nfs4_sequence_args *args,
738 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400739 struct rpc_task *task)
740{
Trond Myklebust035168a2010-06-16 09:52:26 -0400741 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400742 int ret = 0;
743
Trond Myklebust9d12b212012-01-17 22:04:25 -0500744 if (session == NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400745 goto out;
Trond Myklebust035168a2010-06-16 09:52:26 -0400746
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500747 dprintk("--> %s clp %p session %p sr_slot %d\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400748 __func__, session->clp, session, res->sr_slot ?
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500749 res->sr_slot->slot_nr : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400750
Trond Myklebust9d12b212012-01-17 22:04:25 -0500751 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400752out:
753 dprintk("<-- %s status=%d\n", __func__, ret);
754 return ret;
755}
756
757struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400758 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400759 struct nfs4_sequence_args *seq_args;
760 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400761};
762
763static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
764{
765 struct nfs41_call_sync_data *data = calldata;
766
Trond Myklebust035168a2010-06-16 09:52:26 -0400767 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
768
769 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -0500770 data->seq_res, task))
Andy Adamsonce5039c2009-04-01 09:22:13 -0400771 return;
772 rpc_call_start(task);
773}
774
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800775static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
776{
777 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
778 nfs41_call_sync_prepare(task, calldata);
779}
780
Andy Adamson69ab40c2009-04-01 09:22:19 -0400781static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
782{
783 struct nfs41_call_sync_data *data = calldata;
784
Trond Myklebust14516c32010-07-31 14:29:06 -0400785 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400786}
787
Trond Myklebust17280172012-03-11 13:11:00 -0400788static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400789 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400790 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400791};
792
Trond Myklebust17280172012-03-11 13:11:00 -0400793static const struct rpc_call_ops nfs41_call_priv_sync_ops = {
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800794 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
795 .rpc_call_done = nfs41_call_sync_done,
796};
797
Bryan Schumaker7c513052011-03-24 17:12:24 +0000798static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
799 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400800 struct rpc_message *msg,
801 struct nfs4_sequence_args *args,
802 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800803 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400804{
805 int ret;
806 struct rpc_task *task;
807 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400808 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400809 .seq_args = args,
810 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400811 };
812 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000813 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400814 .rpc_message = msg,
815 .callback_ops = &nfs41_call_sync_ops,
816 .callback_data = &data
817 };
818
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800819 if (privileged)
820 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400821 task = rpc_run_task(&task_setup);
822 if (IS_ERR(task))
823 ret = PTR_ERR(task);
824 else {
825 ret = task->tk_status;
826 rpc_put_task(task);
827 }
828 return ret;
829}
830
Bryan Schumaker7c513052011-03-24 17:12:24 +0000831int _nfs4_call_sync_session(struct rpc_clnt *clnt,
832 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400833 struct rpc_message *msg,
834 struct nfs4_sequence_args *args,
835 struct nfs4_sequence_res *res,
836 int cache_reply)
837{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500838 nfs41_init_sequence(args, res, cache_reply);
839 return nfs4_call_sync_sequence(clnt, server, msg, args, res, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400840}
841
Trond Myklebustdf896452010-06-16 09:52:26 -0400842#else
Trond Myklebust9d12b212012-01-17 22:04:25 -0500843static inline
844void nfs41_init_sequence(struct nfs4_sequence_args *args,
845 struct nfs4_sequence_res *res, int cache_reply)
846{
847}
848
Trond Myklebust14516c32010-07-31 14:29:06 -0400849static int nfs4_sequence_done(struct rpc_task *task,
850 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400851{
Trond Myklebust14516c32010-07-31 14:29:06 -0400852 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400853}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400854#endif /* CONFIG_NFS_V4_1 */
855
Bryan Schumaker7c513052011-03-24 17:12:24 +0000856int _nfs4_call_sync(struct rpc_clnt *clnt,
857 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400858 struct rpc_message *msg,
859 struct nfs4_sequence_args *args,
860 struct nfs4_sequence_res *res,
861 int cache_reply)
862{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500863 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000864 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400865}
866
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000867static inline
Bryan Schumaker7c513052011-03-24 17:12:24 +0000868int nfs4_call_sync(struct rpc_clnt *clnt,
869 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000870 struct rpc_message *msg,
871 struct nfs4_sequence_args *args,
872 struct nfs4_sequence_res *res,
873 int cache_reply)
874{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000875 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
876 args, res, cache_reply);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000877}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400878
Trond Myklebust38478b22006-05-25 01:40:57 -0400879static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880{
Trond Myklebust38478b22006-05-25 01:40:57 -0400881 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Trond Myklebust38478b22006-05-25 01:40:57 -0400883 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400884 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700885 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400886 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700887 dir->i_version = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400888 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889}
890
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100891struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400892 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100893 struct nfs_openargs o_arg;
894 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100895 struct nfs_open_confirmargs c_arg;
896 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500897 struct nfs4_string owner_name;
898 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100899 struct nfs_fattr f_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100900 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400901 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100902 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400903 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100904 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100905 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400906 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100907 int rpc_status;
908 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100909};
910
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400911
912static void nfs4_init_opendata_res(struct nfs4_opendata *p)
913{
914 p->o_res.f_attr = &p->f_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400915 p->o_res.seqid = p->o_arg.seqid;
916 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400917 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400918 p->o_res.access_request = p->o_arg.access;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400919 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500920 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400921}
922
Al Viro82a2c1b2011-06-22 18:30:55 -0400923static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500924 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400925 const struct iattr *attrs,
926 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100927{
Al Viro82a2c1b2011-06-22 18:30:55 -0400928 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100929 struct inode *dir = parent->d_inode;
930 struct nfs_server *server = NFS_SERVER(dir);
931 struct nfs4_opendata *p;
932
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400933 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100934 if (p == NULL)
935 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400936 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100937 if (p->o_arg.seqid == NULL)
938 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400939 nfs_sb_active(dentry->d_sb);
940 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100941 p->dir = parent;
942 p->owner = sp;
943 atomic_inc(&sp->so_count);
944 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500945 p->o_arg.open_flags = flags;
946 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700947 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
948 * will return permission denied for all bits until close */
949 if (!(flags & O_EXCL)) {
950 /* ask server to check for all possible rights as results
951 * are cached */
952 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
953 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700954 }
David Howells7539bba2006-08-22 20:06:09 -0400955 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400956 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
957 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400958 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100959 p->o_arg.server = server;
960 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust1549210f2012-06-05 09:16:47 -0400961 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100962 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust536e43d2012-01-17 22:04:26 -0500963 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -0500964 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400965
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100966 p->o_arg.u.attrs = &p->attrs;
967 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500968
969 verf[0] = jiffies;
970 verf[1] = current->pid;
971 memcpy(p->o_arg.u.verifier.data, verf,
972 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100973 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100974 p->c_arg.fh = &p->o_res.fh;
975 p->c_arg.stateid = &p->o_res.stateid;
976 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400977 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400978 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100979 return p;
980err_free:
981 kfree(p);
982err:
983 dput(parent);
984 return NULL;
985}
986
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400987static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100988{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400989 struct nfs4_opendata *p = container_of(kref,
990 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400991 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400992
993 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400994 if (p->state != NULL)
995 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400996 nfs4_put_state_owner(p->owner);
997 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400998 dput(p->dentry);
999 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001000 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001001 kfree(p);
1002}
1003
1004static void nfs4_opendata_put(struct nfs4_opendata *p)
1005{
1006 if (p != NULL)
1007 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001008}
1009
Trond Myklebust06f814a2006-01-03 09:55:07 +01001010static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1011{
Trond Myklebust06f814a2006-01-03 09:55:07 +01001012 int ret;
1013
Trond Myklebust06f814a2006-01-03 09:55:07 +01001014 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +01001015 return ret;
1016}
1017
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001018static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001019{
1020 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001021
Trond Myklebust536e43d2012-01-17 22:04:26 -05001022 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001023 goto out;
1024 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001025 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001026 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1027 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001028 break;
1029 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001030 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1031 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001032 break;
1033 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001034 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1035 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001036 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001037out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001038 return ret;
1039}
1040
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001041static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001042{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001043 if (delegation == NULL)
1044 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001045 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001046 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -05001047 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -04001048 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -05001049 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001050 return 1;
1051}
1052
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001053static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001054{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001055 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001056 case FMODE_WRITE:
1057 state->n_wronly++;
1058 break;
1059 case FMODE_READ:
1060 state->n_rdonly++;
1061 break;
1062 case FMODE_READ|FMODE_WRITE:
1063 state->n_rdwr++;
1064 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001065 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001066}
1067
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001068static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -04001069{
1070 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001071 nfs4_stateid_copy(&state->stateid, stateid);
1072 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001073 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001074 case FMODE_READ:
1075 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1076 break;
1077 case FMODE_WRITE:
1078 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1079 break;
1080 case FMODE_READ|FMODE_WRITE:
1081 set_bit(NFS_O_RDWR_STATE, &state->flags);
1082 }
1083}
1084
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001085static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -04001086{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001087 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001088 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001089 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001090}
1091
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001092static 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 -07001093{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001094 /*
1095 * Protect the call to nfs4_state_set_mode_locked and
1096 * serialise the stateid update
1097 */
1098 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001099 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001100 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001101 set_bit(NFS_DELEGATED_STATE, &state->flags);
1102 }
1103 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001104 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001105 write_sequnlock(&state->seqlock);
1106 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001107 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001108 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109}
1110
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001111static 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 -05001112{
1113 struct nfs_inode *nfsi = NFS_I(state->inode);
1114 struct nfs_delegation *deleg_cur;
1115 int ret = 0;
1116
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001117 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001118
1119 rcu_read_lock();
1120 deleg_cur = rcu_dereference(nfsi->delegation);
1121 if (deleg_cur == NULL)
1122 goto no_delegation;
1123
1124 spin_lock(&deleg_cur->lock);
1125 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001126 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001127 goto no_delegation_unlock;
1128
1129 if (delegation == NULL)
1130 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001131 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001132 goto no_delegation_unlock;
1133
Trond Myklebustb7391f42008-12-23 15:21:52 -05001134 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001135 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001136 ret = 1;
1137no_delegation_unlock:
1138 spin_unlock(&deleg_cur->lock);
1139no_delegation:
1140 rcu_read_unlock();
1141
1142 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001143 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001144 ret = 1;
1145 }
1146
1147 return ret;
1148}
1149
1150
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001151static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001152{
1153 struct nfs_delegation *delegation;
1154
1155 rcu_read_lock();
1156 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001157 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001158 rcu_read_unlock();
1159 return;
1160 }
1161 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001162 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001163}
1164
Trond Myklebust6ee41262007-07-08 14:11:36 -04001165static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001166{
1167 struct nfs4_state *state = opendata->state;
1168 struct nfs_inode *nfsi = NFS_I(state->inode);
1169 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -05001170 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001171 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001172 nfs4_stateid stateid;
1173 int ret = -EAGAIN;
1174
Trond Myklebustaac00a82007-07-05 19:02:21 -04001175 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001176 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001177 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001178 if (can_open_cached(state, fmode, open_mode)) {
1179 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001180 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001181 goto out_return_state;
1182 }
1183 spin_unlock(&state->owner->so_lock);
1184 }
Trond Myklebust34310432008-12-23 15:21:38 -05001185 rcu_read_lock();
1186 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001187 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001188 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001189 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001190 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001191 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001192 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001193 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001194 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001195 if (ret != 0)
1196 goto out;
1197 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001198
1199 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001200 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001201 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001202 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001203out:
1204 return ERR_PTR(ret);
1205out_return_state:
1206 atomic_inc(&state->count);
1207 return state;
1208}
1209
Andy Adamsone23008e2012-10-02 21:07:32 -04001210static void
1211nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1212{
1213 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1214 struct nfs_delegation *delegation;
1215 int delegation_flags = 0;
1216
1217 rcu_read_lock();
1218 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1219 if (delegation)
1220 delegation_flags = delegation->flags;
1221 rcu_read_unlock();
1222 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1223 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1224 "returning a delegation for "
1225 "OPEN(CLAIM_DELEGATE_CUR)\n",
1226 clp->cl_hostname);
1227 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1228 nfs_inode_set_delegation(state->inode,
1229 data->owner->so_cred,
1230 &data->o_res);
1231 else
1232 nfs_inode_reclaim_delegation(state->inode,
1233 data->owner->so_cred,
1234 &data->o_res);
1235}
1236
1237/*
1238 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1239 * and update the nfs4_state.
1240 */
1241static struct nfs4_state *
1242_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1243{
1244 struct inode *inode = data->state->inode;
1245 struct nfs4_state *state = data->state;
1246 int ret;
1247
1248 if (!data->rpc_done) {
1249 ret = data->rpc_status;
1250 goto err;
1251 }
1252
1253 ret = -ESTALE;
1254 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1255 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1256 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1257 goto err;
1258
1259 ret = -ENOMEM;
1260 state = nfs4_get_open_state(inode, data->owner);
1261 if (state == NULL)
1262 goto err;
1263
1264 ret = nfs_refresh_inode(inode, &data->f_attr);
1265 if (ret)
1266 goto err;
1267
1268 if (data->o_res.delegation_type != 0)
1269 nfs4_opendata_check_deleg(data, state);
1270 update_open_stateid(state, &data->o_res.stateid, NULL,
1271 data->o_arg.fmode);
1272
1273 return state;
1274err:
1275 return ERR_PTR(ret);
1276
1277}
1278
1279static struct nfs4_state *
1280_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001281{
1282 struct inode *inode;
1283 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001284 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001285
Trond Myklebustaac00a82007-07-05 19:02:21 -04001286 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001287 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001288 goto out;
1289 }
1290
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001291 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001292 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001293 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001294 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001295 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001296 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001297 goto err;
1298 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001299 state = nfs4_get_open_state(inode, data->owner);
1300 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001301 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001302 if (data->o_res.delegation_type != 0)
1303 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001304 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001305 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001306 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001307out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001308 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001309err_put_inode:
1310 iput(inode);
1311err:
1312 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001313}
1314
Andy Adamsone23008e2012-10-02 21:07:32 -04001315static struct nfs4_state *
1316nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1317{
1318 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1319 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1320 return _nfs4_opendata_to_nfs4_state(data);
1321}
1322
Trond Myklebust864472e2006-01-03 09:55:15 +01001323static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1324{
1325 struct nfs_inode *nfsi = NFS_I(state->inode);
1326 struct nfs_open_context *ctx;
1327
1328 spin_lock(&state->inode->i_lock);
1329 list_for_each_entry(ctx, &nfsi->open_files, list) {
1330 if (ctx->state != state)
1331 continue;
1332 get_nfs_open_context(ctx);
1333 spin_unlock(&state->inode->i_lock);
1334 return ctx;
1335 }
1336 spin_unlock(&state->inode->i_lock);
1337 return ERR_PTR(-ENOENT);
1338}
1339
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001340static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1341{
1342 struct nfs4_opendata *opendata;
1343
Al Viro3d4ff432011-06-22 18:40:12 -04001344 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001345 if (opendata == NULL)
1346 return ERR_PTR(-ENOMEM);
1347 opendata->state = state;
1348 atomic_inc(&state->count);
1349 return opendata;
1350}
1351
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001352static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001353{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001354 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001355 int ret;
1356
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001357 opendata->o_arg.open_flags = 0;
1358 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001359 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1360 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1361 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001362 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001363 if (ret != 0)
1364 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001365 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001366 if (IS_ERR(newstate))
1367 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001368 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001369 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001370 return 0;
1371}
1372
1373static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1374{
Trond Myklebust864472e2006-01-03 09:55:15 +01001375 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001376 int ret;
1377
1378 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001379 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001380 smp_rmb();
1381 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001382 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001383 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001384 if (ret != 0)
1385 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001386 if (newstate != state)
1387 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001388 }
1389 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001390 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001391 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001392 if (ret != 0)
1393 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001394 if (newstate != state)
1395 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001396 }
1397 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001398 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001399 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001400 if (ret != 0)
1401 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001402 if (newstate != state)
1403 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001404 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001405 /*
1406 * We may have performed cached opens for all three recoveries.
1407 * Check if we need to update the current stateid.
1408 */
1409 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001410 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001411 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001412 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001413 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001414 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001415 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001416 return 0;
1417}
1418
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419/*
1420 * OPEN_RECLAIM:
1421 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001423static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001425 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001426 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001427 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 int status;
1429
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001430 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1431 if (IS_ERR(opendata))
1432 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001433 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1434 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001435 rcu_read_lock();
1436 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001437 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001438 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001439 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001440 opendata->o_arg.u.delegation_type = delegation_type;
1441 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001442 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 return status;
1444}
1445
Trond Myklebust539cd032007-06-05 11:46:42 -04001446static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447{
1448 struct nfs_server *server = NFS_SERVER(state->inode);
1449 struct nfs4_exception exception = { };
1450 int err;
1451 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001452 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001453 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001454 break;
1455 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 } while (exception.retry);
1457 return err;
1458}
1459
Trond Myklebust864472e2006-01-03 09:55:15 +01001460static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1461{
1462 struct nfs_open_context *ctx;
1463 int ret;
1464
1465 ctx = nfs4_state_find_open_context(state);
1466 if (IS_ERR(ctx))
1467 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001468 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001469 put_nfs_open_context(ctx);
1470 return ret;
1471}
1472
Trond Myklebust13437e12007-07-06 15:10:43 -04001473static 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 -07001474{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001475 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001476 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001478 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1479 if (IS_ERR(opendata))
1480 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001481 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebustf597c532012-03-04 18:13:56 -05001482 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust864472e2006-01-03 09:55:15 +01001483 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001484 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001485 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486}
1487
Trond Myklebust13437e12007-07-06 15:10:43 -04001488int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489{
1490 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001491 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 int err;
1493 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001494 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 switch (err) {
1496 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001497 case -ENOENT:
1498 case -ESTALE:
1499 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001500 case -NFS4ERR_BADSESSION:
1501 case -NFS4ERR_BADSLOT:
1502 case -NFS4ERR_BAD_HIGH_SLOT:
1503 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1504 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04001505 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001506 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 case -NFS4ERR_STALE_CLIENTID:
1508 case -NFS4ERR_STALE_STATEID:
1509 case -NFS4ERR_EXPIRED:
1510 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001511 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001512 goto out;
1513 case -ERESTARTSYS:
1514 /*
1515 * The show must go on: exit, but mark the
1516 * stateid as needing recovery.
1517 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001518 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001519 case -NFS4ERR_ADMIN_REVOKED:
1520 case -NFS4ERR_BAD_STATEID:
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001521 nfs_inode_find_state_and_recover(state->inode,
1522 stateid);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001523 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001524 case -EKEYEXPIRED:
1525 /*
1526 * User RPCSEC_GSS context has expired.
1527 * We cannot recover this stateid now, so
1528 * skip it and allow recovery thread to
1529 * proceed.
1530 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001531 case -ENOMEM:
1532 err = 0;
1533 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 }
1535 err = nfs4_handle_exception(server, err, &exception);
1536 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001537out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 return err;
1539}
1540
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001541static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1542{
1543 struct nfs4_opendata *data = calldata;
1544
1545 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001546 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001547 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001548 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001549 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001550 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001551 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001552}
1553
1554static void nfs4_open_confirm_release(void *calldata)
1555{
1556 struct nfs4_opendata *data = calldata;
1557 struct nfs4_state *state = NULL;
1558
1559 /* If this request hasn't been cancelled, do nothing */
1560 if (data->cancelled == 0)
1561 goto out_free;
1562 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001563 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001564 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001565 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001566 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001567 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001568out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001569 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001570}
1571
1572static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001573 .rpc_call_done = nfs4_open_confirm_done,
1574 .rpc_release = nfs4_open_confirm_release,
1575};
1576
1577/*
1578 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1579 */
1580static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1581{
1582 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1583 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001584 struct rpc_message msg = {
1585 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1586 .rpc_argp = &data->c_arg,
1587 .rpc_resp = &data->c_res,
1588 .rpc_cred = data->owner->so_cred,
1589 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001590 struct rpc_task_setup task_setup_data = {
1591 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001592 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001593 .callback_ops = &nfs4_open_confirm_ops,
1594 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001595 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001596 .flags = RPC_TASK_ASYNC,
1597 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 int status;
1599
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001600 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001601 data->rpc_done = 0;
1602 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001603 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001604 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001605 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001606 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001607 status = nfs4_wait_for_completion_rpc_task(task);
1608 if (status != 0) {
1609 data->cancelled = 1;
1610 smp_wmb();
1611 } else
1612 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001613 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 return status;
1615}
1616
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001617static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001619 struct nfs4_opendata *data = calldata;
1620 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001621
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001622 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1623 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001624 /*
1625 * Check if we still need to send an OPEN call, or if we can use
1626 * a delegation instead.
1627 */
1628 if (data->state != NULL) {
1629 struct nfs_delegation *delegation;
1630
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001631 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001632 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001633 rcu_read_lock();
1634 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001635 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1636 can_open_delegated(delegation, data->o_arg.fmode))
1637 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001638 rcu_read_unlock();
1639 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001640 /* Update client id. */
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001641 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001642 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001643 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Andy Adamsone23008e2012-10-02 21:07:32 -04001644 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001645 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1646 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001647 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001648 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001649 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001650 &data->o_res.seq_res,
1651 task) != 0)
1652 nfs_release_seqid(data->o_arg.seqid);
1653 else
1654 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001655 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001656unlock_no_action:
1657 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001658out_no_action:
1659 task->tk_action = NULL;
1660
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001661}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001663static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1664{
1665 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1666 nfs4_open_prepare(task, calldata);
1667}
1668
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001669static void nfs4_open_done(struct rpc_task *task, void *calldata)
1670{
1671 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001673 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001674
Trond Myklebust14516c32010-07-31 14:29:06 -04001675 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1676 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001677
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001678 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001679 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1680 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001681 case S_IFREG:
1682 break;
1683 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001684 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001685 break;
1686 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001687 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001688 break;
1689 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001690 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001691 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001692 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001693 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001694 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1695 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001696 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001697 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001698}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001699
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001700static void nfs4_open_release(void *calldata)
1701{
1702 struct nfs4_opendata *data = calldata;
1703 struct nfs4_state *state = NULL;
1704
1705 /* If this request hasn't been cancelled, do nothing */
1706 if (data->cancelled == 0)
1707 goto out_free;
1708 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001709 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001710 goto out_free;
1711 /* In case we need an open_confirm, no cleanup! */
1712 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1713 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001714 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001715 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001716 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001717out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001718 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001719}
1720
1721static const struct rpc_call_ops nfs4_open_ops = {
1722 .rpc_call_prepare = nfs4_open_prepare,
1723 .rpc_call_done = nfs4_open_done,
1724 .rpc_release = nfs4_open_release,
1725};
1726
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001727static const struct rpc_call_ops nfs4_recover_open_ops = {
1728 .rpc_call_prepare = nfs4_recover_open_prepare,
1729 .rpc_call_done = nfs4_open_done,
1730 .rpc_release = nfs4_open_release,
1731};
1732
1733static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001734{
1735 struct inode *dir = data->dir->d_inode;
1736 struct nfs_server *server = NFS_SERVER(dir);
1737 struct nfs_openargs *o_arg = &data->o_arg;
1738 struct nfs_openres *o_res = &data->o_res;
1739 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001740 struct rpc_message msg = {
1741 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1742 .rpc_argp = o_arg,
1743 .rpc_resp = o_res,
1744 .rpc_cred = data->owner->so_cred,
1745 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001746 struct rpc_task_setup task_setup_data = {
1747 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001748 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001749 .callback_ops = &nfs4_open_ops,
1750 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001751 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001752 .flags = RPC_TASK_ASYNC,
1753 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001754 int status;
1755
Trond Myklebust9d12b212012-01-17 22:04:25 -05001756 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001757 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001758 data->rpc_done = 0;
1759 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001760 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001761 if (isrecover)
1762 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001763 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001764 if (IS_ERR(task))
1765 return PTR_ERR(task);
1766 status = nfs4_wait_for_completion_rpc_task(task);
1767 if (status != 0) {
1768 data->cancelled = 1;
1769 smp_wmb();
1770 } else
1771 status = data->rpc_status;
1772 rpc_put_task(task);
1773
1774 return status;
1775}
1776
1777static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1778{
1779 struct inode *dir = data->dir->d_inode;
1780 struct nfs_openres *o_res = &data->o_res;
1781 int status;
1782
1783 status = nfs4_run_open_task(data, 1);
1784 if (status != 0 || !data->rpc_done)
1785 return status;
1786
Trond Myklebust6926afd2012-01-07 13:22:46 -05001787 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1788
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001789 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1790 status = _nfs4_proc_open_confirm(data);
1791 if (status != 0)
1792 return status;
1793 }
1794
1795 return status;
1796}
1797
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001798static int nfs4_opendata_access(struct rpc_cred *cred,
1799 struct nfs4_opendata *opendata,
1800 struct nfs4_state *state, fmode_t fmode)
1801{
1802 struct nfs_access_entry cache;
1803 u32 mask;
1804
1805 /* access call failed or for some reason the server doesn't
1806 * support any access modes -- defer access call until later */
1807 if (opendata->o_res.access_supported == 0)
1808 return 0;
1809
1810 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001811 /* don't check MAY_WRITE - a newly created file may not have
1812 * write mode bits, but POSIX allows the creating process to write */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001813 if (fmode & FMODE_READ)
1814 mask |= MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001815 if (fmode & FMODE_EXEC)
1816 mask |= MAY_EXEC;
1817
1818 cache.cred = cred;
1819 cache.jiffies = jiffies;
1820 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1821 nfs_access_add_cache(state->inode, &cache);
1822
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001823 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001824 return 0;
1825
1826 /* even though OPEN succeeded, access is denied. Close the file */
1827 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001828 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001829}
1830
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001831/*
1832 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1833 */
1834static int _nfs4_proc_open(struct nfs4_opendata *data)
1835{
1836 struct inode *dir = data->dir->d_inode;
1837 struct nfs_server *server = NFS_SERVER(dir);
1838 struct nfs_openargs *o_arg = &data->o_arg;
1839 struct nfs_openres *o_res = &data->o_res;
1840 int status;
1841
1842 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001843 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001844 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001845 if (status != 0) {
1846 if (status == -NFS4ERR_BADNAME &&
1847 !(o_arg->open_flags & O_CREAT))
1848 return -ENOENT;
1849 return status;
1850 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001851
Trond Myklebust6926afd2012-01-07 13:22:46 -05001852 nfs_fattr_map_and_free_names(server, &data->f_attr);
1853
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001854 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001855 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001856 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1857 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001859 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001861 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 }
1863 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001864 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001865 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866}
1867
Andy Adamsond83217c2011-03-01 01:34:17 +00001868static int nfs4_recover_expired_lease(struct nfs_server *server)
1869{
1870 return nfs4_client_recover_expired_lease(server->nfs_client);
1871}
1872
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873/*
1874 * OPEN_EXPIRED:
1875 * reclaim state on the server after a network partition.
1876 * Assumes caller holds the appropriate lock
1877 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001878static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001880 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001881 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001883 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1884 if (IS_ERR(opendata))
1885 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001886 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001887 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001888 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001889 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001890 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891}
1892
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001893static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001894{
Trond Myklebust539cd032007-06-05 11:46:42 -04001895 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001896 struct nfs4_exception exception = { };
1897 int err;
1898
1899 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001900 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001901 switch (err) {
1902 default:
1903 goto out;
1904 case -NFS4ERR_GRACE:
1905 case -NFS4ERR_DELAY:
1906 nfs4_handle_exception(server, err, &exception);
1907 err = 0;
1908 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001909 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001910out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001911 return err;
1912}
1913
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1915{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001917 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
Trond Myklebust864472e2006-01-03 09:55:15 +01001919 ctx = nfs4_state_find_open_context(state);
1920 if (IS_ERR(ctx))
1921 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001922 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001923 put_nfs_open_context(ctx);
1924 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925}
1926
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001927#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001928static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001929{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001930 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001931 nfs4_stateid *stateid = &state->stateid;
1932 int status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001933
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001934 /* If a state reset has been done, test_stateid is unneeded */
1935 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1936 return;
1937
1938 status = nfs41_test_stateid(server, stateid);
1939 if (status != NFS_OK) {
1940 /* Free the stateid unless the server explicitly
1941 * informs us the stateid is unrecognized. */
1942 if (status != -NFS4ERR_BAD_STATEID)
1943 nfs41_free_stateid(server, stateid);
Bryan Schumaker69388672012-09-26 15:25:52 -04001944 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001945
Bryan Schumaker69388672012-09-26 15:25:52 -04001946 write_seqlock(&state->seqlock);
1947 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1948 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001949 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1950 }
1951}
1952
1953/**
1954 * nfs41_check_open_stateid - possibly free an open stateid
1955 *
1956 * @state: NFSv4 state for an inode
1957 *
1958 * Returns NFS_OK if recovery for this stateid is now finished.
1959 * Otherwise a negative NFS4ERR value is returned.
1960 */
1961static int nfs41_check_open_stateid(struct nfs4_state *state)
1962{
1963 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04001964 nfs4_stateid *stateid = &state->open_stateid;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001965 int status;
1966
1967 /* If a state reset has been done, test_stateid is unneeded */
1968 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1969 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1970 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1971 return -NFS4ERR_BAD_STATEID;
1972
1973 status = nfs41_test_stateid(server, stateid);
1974 if (status != NFS_OK) {
1975 /* Free the stateid unless the server explicitly
1976 * informs us the stateid is unrecognized. */
1977 if (status != -NFS4ERR_BAD_STATEID)
1978 nfs41_free_stateid(server, stateid);
1979
1980 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1981 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1982 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001983 }
1984 return status;
1985}
1986
1987static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1988{
Chuck Levereb64cf92012-07-11 16:30:05 -04001989 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001990
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001991 nfs41_clear_delegation_stateid(state);
1992 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04001993 if (status != NFS_OK)
1994 status = nfs4_open_expired(sp, state);
1995 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001996}
1997#endif
1998
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002000 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2001 * fields corresponding to attributes that were used to store the verifier.
2002 * Make sure we clobber those fields in the later setattr call
2003 */
2004static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
2005{
2006 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2007 !(sattr->ia_valid & ATTR_ATIME_SET))
2008 sattr->ia_valid |= ATTR_ATIME;
2009
2010 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2011 !(sattr->ia_valid & ATTR_MTIME_SET))
2012 sattr->ia_valid |= ATTR_MTIME;
2013}
2014
2015/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002016 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 */
Andy Adamson82be4172012-05-23 05:02:35 -04002018static int _nfs4_do_open(struct inode *dir,
2019 struct dentry *dentry,
2020 fmode_t fmode,
2021 int flags,
2022 struct iattr *sattr,
2023 struct rpc_cred *cred,
2024 struct nfs4_state **res,
2025 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026{
2027 struct nfs4_state_owner *sp;
2028 struct nfs4_state *state = NULL;
2029 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002030 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002031 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032
2033 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002035 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2036 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2038 goto out_err;
2039 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002040 status = nfs4_recover_expired_lease(server);
2041 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002042 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04002043 if (dentry->d_inode != NULL)
2044 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002045 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04002046 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002047 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002048 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049
Andy Adamson82be4172012-05-23 05:02:35 -04002050 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2051 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2052 if (!opendata->f_attr.mdsthreshold)
2053 goto err_opendata_put;
Trond Myklebust1549210f2012-06-05 09:16:47 -04002054 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002055 }
Al Viro82a2c1b2011-06-22 18:30:55 -04002056 if (dentry->d_inode != NULL)
2057 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002058
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002059 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002061 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002063 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002064 status = PTR_ERR(state);
2065 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002066 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002067 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05002068 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002069
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002070 status = nfs4_opendata_access(cred, opendata, state, fmode);
2071 if (status != 0)
2072 goto err_opendata_put;
2073
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002074 if (opendata->o_arg.open_flags & O_EXCL) {
2075 nfs4_exclusive_attrset(opendata, sattr);
2076
2077 nfs_fattr_init(opendata->o_res.f_attr);
2078 status = nfs4_do_setattr(state->inode, cred,
2079 opendata->o_res.f_attr, sattr,
2080 state);
2081 if (status == 0)
2082 nfs_setattr_update_inode(state->inode, sattr);
2083 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
2084 }
Andy Adamson82be4172012-05-23 05:02:35 -04002085
2086 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2087 *ctx_th = opendata->f_attr.mdsthreshold;
2088 else
2089 kfree(opendata->f_attr.mdsthreshold);
2090 opendata->f_attr.mdsthreshold = NULL;
2091
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002092 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 *res = state;
2095 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002096err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04002097 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002098 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002099err_put_state_owner:
2100 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 *res = NULL;
2103 return status;
2104}
2105
2106
Andy Adamson82be4172012-05-23 05:02:35 -04002107static struct nfs4_state *nfs4_do_open(struct inode *dir,
2108 struct dentry *dentry,
2109 fmode_t fmode,
2110 int flags,
2111 struct iattr *sattr,
2112 struct rpc_cred *cred,
2113 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114{
2115 struct nfs4_exception exception = { };
2116 struct nfs4_state *res;
2117 int status;
2118
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002119 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 do {
Andy Adamson82be4172012-05-23 05:02:35 -04002121 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
2122 &res, ctx_th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 if (status == 0)
2124 break;
2125 /* NOTE: BAD_SEQID means the server and client disagree about the
2126 * book-keeping w.r.t. state-changing operations
2127 * (OPEN/CLOSE/LOCK/LOCKU...)
2128 * It is actually a sign of a bug on the client or on the server.
2129 *
2130 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002131 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 * have unhashed the old state_owner for us, and that we can
2133 * therefore safely retry using a new one. We should still warn
2134 * the user though...
2135 */
2136 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002137 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002138 " returned a bad sequence-id error!\n",
2139 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 exception.retry = 1;
2141 continue;
2142 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002143 /*
2144 * BAD_STATEID on OPEN means that the server cancelled our
2145 * state before it received the OPEN_CONFIRM.
2146 * Recover by retrying the request as per the discussion
2147 * on Page 181 of RFC3530.
2148 */
2149 if (status == -NFS4ERR_BAD_STATEID) {
2150 exception.retry = 1;
2151 continue;
2152 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002153 if (status == -EAGAIN) {
2154 /* We must have found a delegation */
2155 exception.retry = 1;
2156 continue;
2157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
2159 status, &exception));
2160 } while (exception.retry);
2161 return res;
2162}
2163
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002164static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2165 struct nfs_fattr *fattr, struct iattr *sattr,
2166 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002168 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002170 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 .iap = sattr,
2172 .server = server,
2173 .bitmask = server->attr_bitmask,
2174 };
2175 struct nfs_setattrres res = {
2176 .fattr = fattr,
2177 .server = server,
2178 };
2179 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002180 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2181 .rpc_argp = &arg,
2182 .rpc_resp = &res,
2183 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002185 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04002186 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
Trond Myklebust0e574af2005-10-27 22:12:38 -04002188 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
Trond Myklebust4fc87962012-03-08 17:42:01 -05002190 if (state != NULL) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002191 struct nfs_lockowner lockowner = {
2192 .l_owner = current->files,
2193 .l_pid = current->tgid,
2194 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002195 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002196 &lockowner);
Trond Myklebust4fc87962012-03-08 17:42:01 -05002197 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2198 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002199 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002200 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002201 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
Bryan Schumaker7c513052011-03-24 17:12:24 +00002203 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002204 if (status == 0 && state != NULL)
2205 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002206 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207}
2208
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002209static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2210 struct nfs_fattr *fattr, struct iattr *sattr,
2211 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002213 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002214 struct nfs4_exception exception = {
2215 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002216 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002217 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 int err;
2219 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04002220 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2221 switch (err) {
2222 case -NFS4ERR_OPENMODE:
2223 if (state && !(state->state & FMODE_WRITE)) {
2224 err = -EBADF;
2225 if (sattr->ia_valid & ATTR_OPEN)
2226 err = -EACCES;
2227 goto out;
2228 }
2229 }
2230 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002232out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 return err;
2234}
2235
2236struct nfs4_closedata {
2237 struct inode *inode;
2238 struct nfs4_state *state;
2239 struct nfs_closeargs arg;
2240 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002241 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002242 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002243 bool roc;
2244 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245};
2246
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002247static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002248{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002249 struct nfs4_closedata *calldata = data;
2250 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002251 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002252
Fred Isamanf7e89172011-01-06 11:36:32 +00002253 if (calldata->roc)
2254 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002255 nfs4_put_open_state(calldata->state);
2256 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002257 nfs4_put_state_owner(sp);
Weston Andros Adamson324d0032012-10-30 17:01:39 -04002258 nfs_sb_deactive_async(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002259 kfree(calldata);
2260}
2261
Trond Myklebust88069f72009-12-08 08:33:16 -05002262static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2263 fmode_t fmode)
2264{
2265 spin_lock(&state->owner->so_lock);
2266 if (!(fmode & FMODE_READ))
2267 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2268 if (!(fmode & FMODE_WRITE))
2269 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2270 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2271 spin_unlock(&state->owner->so_lock);
2272}
2273
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002274static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002276 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 struct nfs_server *server = NFS_SERVER(calldata->inode);
2279
Chuck Levera3ca5652012-03-01 17:00:40 -05002280 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002281 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2282 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 /* hmm. we are done with the inode, and in the process of freeing
2284 * the state_owner. we keep this around to process errors
2285 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 switch (task->tk_status) {
2287 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002288 if (calldata->roc)
2289 pnfs_roc_set_barrier(state->inode,
2290 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002291 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002292 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002293 nfs4_close_clear_stateid_flags(state,
2294 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 break;
2296 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002297 case -NFS4ERR_OLD_STATEID:
2298 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002300 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002301 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002303 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2304 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002306 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002307 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002308 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309}
2310
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002311static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002313 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002314 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002315 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002316 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002317
Chuck Levera3ca5652012-03-01 17:00:40 -05002318 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002319 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07002320 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04002321
Trond Myklebust88069f72009-12-08 08:33:16 -05002322 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2323 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002324 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002325 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002326 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002327 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002328 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2329 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2330 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002331 }
2332 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002333 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2334 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2335 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002336 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002337 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002338 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002339
2340 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002341 /* Note: exit _without_ calling nfs4_close_done */
2342 task->tk_action = NULL;
Chuck Levera3ca5652012-03-01 17:00:40 -05002343 goto out;
Trond Myklebust95121352005-10-18 14:20:12 -07002344 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002345
Fred Isamanf7e89172011-01-06 11:36:32 +00002346 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002347 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002348 if (calldata->roc &&
Trond Myklebust7fdab062012-09-20 20:15:57 -04002349 pnfs_roc_drain(inode, &calldata->roc_barrier, task))
Chuck Levera3ca5652012-03-01 17:00:40 -05002350 goto out;
Fred Isamanf7e89172011-01-06 11:36:32 +00002351 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002352
Trond Myklebust516a6af2005-10-27 22:12:41 -04002353 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002354 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002355 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002356 &calldata->arg.seq_args,
2357 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002358 task) != 0)
2359 nfs_release_seqid(calldata->arg.seqid);
2360 else
2361 rpc_call_start(task);
Chuck Levera3ca5652012-03-01 17:00:40 -05002362out:
2363 dprintk("%s: done!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364}
2365
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002366static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002367 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002368 .rpc_call_done = nfs4_close_done,
2369 .rpc_release = nfs4_free_closedata,
2370};
2371
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372/*
2373 * It is possible for data to be read/written from a mem-mapped file
2374 * after the sys_close call (which hits the vfs layer as a flush).
2375 * This means that we can't safely call nfsv4 close on a file until
2376 * the inode is cleared. This in turn means that we are not good
2377 * NFSv4 citizens - we do not indicate to the server to update the file's
2378 * share state even when we are done with one of the three share
2379 * stateid's in the inode.
2380 *
2381 * NOTE: Caller must be holding the sp->so_owner semaphore!
2382 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002383int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002385 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002387 struct nfs4_state_owner *sp = state->owner;
2388 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002389 struct rpc_message msg = {
2390 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2391 .rpc_cred = state->owner->so_cred,
2392 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002393 struct rpc_task_setup task_setup_data = {
2394 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002395 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002396 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002397 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002398 .flags = RPC_TASK_ASYNC,
2399 };
Trond Myklebust95121352005-10-18 14:20:12 -07002400 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002402 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002404 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002405 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002406 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002408 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002409 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002411 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002412 if (calldata->arg.seqid == NULL)
2413 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002414 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002415 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002416 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002417 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002418 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002419 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002420 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002421
Trond Myklebust1174dd12010-12-21 10:52:24 -05002422 msg.rpc_argp = &calldata->arg;
2423 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002424 task_setup_data.callback_data = calldata;
2425 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002426 if (IS_ERR(task))
2427 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002428 status = 0;
2429 if (wait)
2430 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002431 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002432 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002433out_free_calldata:
2434 kfree(calldata);
2435out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002436 nfs4_put_open_state(state);
2437 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002438 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439}
2440
Trond Myklebust2b484292010-09-17 10:56:51 -04002441static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002442nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 struct nfs4_state *state;
2445
Trond Myklebust565277f2007-10-15 18:17:53 -04002446 /* Protect against concurrent sillydeletes */
Andy Adamson82be4172012-05-23 05:02:35 -04002447 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2448 ctx->cred, &ctx->mdsthreshold);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002449 if (IS_ERR(state))
2450 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002451 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002452 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453}
2454
Trond Myklebust1185a552009-12-03 15:54:02 -05002455static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002456{
2457 if (ctx->state == NULL)
2458 return;
2459 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002460 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002461 else
Al Viro643168c2011-06-22 18:20:23 -04002462 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002463}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464
2465static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2466{
Benny Halevy43652ad2009-04-01 09:21:54 -04002467 struct nfs4_server_caps_arg args = {
2468 .fhandle = fhandle,
2469 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 struct nfs4_server_caps_res res = {};
2471 struct rpc_message msg = {
2472 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002473 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 .rpc_resp = &res,
2475 };
2476 int status;
2477
Bryan Schumaker7c513052011-03-24 17:12:24 +00002478 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 if (status == 0) {
2480 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002481 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2482 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2483 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2484 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2485 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2487 server->caps |= NFS_CAP_ACLS;
2488 if (res.has_links != 0)
2489 server->caps |= NFS_CAP_HARDLINKS;
2490 if (res.has_symlinks != 0)
2491 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002492 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2493 server->caps |= NFS_CAP_FILEID;
2494 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2495 server->caps |= NFS_CAP_MODE;
2496 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2497 server->caps |= NFS_CAP_NLINK;
2498 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2499 server->caps |= NFS_CAP_OWNER;
2500 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2501 server->caps |= NFS_CAP_OWNER_GROUP;
2502 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2503 server->caps |= NFS_CAP_ATIME;
2504 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2505 server->caps |= NFS_CAP_CTIME;
2506 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2507 server->caps |= NFS_CAP_MTIME;
2508
Trond Myklebusta65318b2009-03-11 14:10:28 -04002509 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2510 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2511 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002513 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002515
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 return status;
2517}
2518
Trond Myklebust55a97592006-06-09 09:34:19 -04002519int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520{
2521 struct nfs4_exception exception = { };
2522 int err;
2523 do {
2524 err = nfs4_handle_exception(server,
2525 _nfs4_server_capabilities(server, fhandle),
2526 &exception);
2527 } while (exception.retry);
2528 return err;
2529}
2530
2531static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2532 struct nfs_fsinfo *info)
2533{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 struct nfs4_lookup_root_arg args = {
2535 .bitmask = nfs4_fattr_bitmap,
2536 };
2537 struct nfs4_lookup_res res = {
2538 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002539 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 .fh = fhandle,
2541 };
2542 struct rpc_message msg = {
2543 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2544 .rpc_argp = &args,
2545 .rpc_resp = &res,
2546 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002547
Trond Myklebust0e574af2005-10-27 22:12:38 -04002548 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002549 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550}
2551
2552static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2553 struct nfs_fsinfo *info)
2554{
2555 struct nfs4_exception exception = { };
2556 int err;
2557 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002558 err = _nfs4_lookup_root(server, fhandle, info);
2559 switch (err) {
2560 case 0:
2561 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002562 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002563 default:
2564 err = nfs4_handle_exception(server, err, &exception);
2565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002567out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 return err;
2569}
2570
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002571static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2572 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2573{
2574 struct rpc_auth *auth;
2575 int ret;
2576
2577 auth = rpcauth_create(flavor, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002578 if (IS_ERR(auth)) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002579 ret = -EIO;
2580 goto out;
2581 }
2582 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002583out:
2584 return ret;
2585}
2586
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002587static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002588 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002590 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002591 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002593 len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04002594 if (len < 0)
2595 return len;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002596
2597 for (i = 0; i < len; i++) {
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002598 /* AUTH_UNIX is the default flavor if none was specified,
2599 * thus has already been tried. */
2600 if (flav_array[i] == RPC_AUTH_UNIX)
2601 continue;
2602
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002603 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002604 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002605 continue;
2606 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002607 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002608 /*
2609 * -EACCESS could mean that the user doesn't have correct permissions
2610 * to access the mount. It could also mean that we tried to mount
2611 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2612 * existing mount programs don't handle -EACCES very well so it should
2613 * be mapped to -EPERM instead.
2614 */
2615 if (status == -EACCES)
2616 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002617 return status;
2618}
2619
2620/*
2621 * get the file handle for the "/" directory on the server
2622 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002623int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2624 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002625{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002626 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002627 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002628 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2629 /*
2630 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2631 * by nfs4_map_errors() as this function exits.
2632 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002633 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 if (status == 0)
2635 status = nfs4_server_capabilities(server, fhandle);
2636 if (status == 0)
2637 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002638 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639}
2640
Bryan Schumakerbae36242012-05-10 15:07:31 -04002641static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2642 struct nfs_fsinfo *info)
2643{
2644 int error;
2645 struct nfs_fattr *fattr = info->fattr;
2646
2647 error = nfs4_server_capabilities(server, mntfh);
2648 if (error < 0) {
2649 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2650 return error;
2651 }
2652
2653 error = nfs4_proc_getattr(server, mntfh, fattr);
2654 if (error < 0) {
2655 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2656 return error;
2657 }
2658
2659 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2660 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2661 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2662
2663 return error;
2664}
2665
Manoj Naik6b97fd32006-06-09 09:34:29 -04002666/*
2667 * Get locations and (maybe) other attributes of a referral.
2668 * Note that we'll actually follow the referral later when
2669 * we detect fsid mismatch in inode revalidation
2670 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002671static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2672 const struct qstr *name, struct nfs_fattr *fattr,
2673 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002674{
2675 int status = -ENOMEM;
2676 struct page *page = NULL;
2677 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002678
2679 page = alloc_page(GFP_KERNEL);
2680 if (page == NULL)
2681 goto out;
2682 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2683 if (locations == NULL)
2684 goto out;
2685
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002686 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002687 if (status != 0)
2688 goto out;
2689 /* Make sure server returned a different fsid for the referral */
2690 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002691 dprintk("%s: server did not return a different fsid for"
2692 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002693 status = -EIO;
2694 goto out;
2695 }
Andy Adamson533eb462011-06-13 18:25:56 -04002696 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2697 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002698
Andy Adamson533eb462011-06-13 18:25:56 -04002699 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002700 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002701 memset(fhandle, 0, sizeof(struct nfs_fh));
2702out:
2703 if (page)
2704 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002705 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002706 return status;
2707}
2708
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2710{
2711 struct nfs4_getattr_arg args = {
2712 .fh = fhandle,
2713 .bitmask = server->attr_bitmask,
2714 };
2715 struct nfs4_getattr_res res = {
2716 .fattr = fattr,
2717 .server = server,
2718 };
2719 struct rpc_message msg = {
2720 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2721 .rpc_argp = &args,
2722 .rpc_resp = &res,
2723 };
2724
Trond Myklebust0e574af2005-10-27 22:12:38 -04002725 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002726 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727}
2728
2729static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2730{
2731 struct nfs4_exception exception = { };
2732 int err;
2733 do {
2734 err = nfs4_handle_exception(server,
2735 _nfs4_proc_getattr(server, fhandle, fattr),
2736 &exception);
2737 } while (exception.retry);
2738 return err;
2739}
2740
2741/*
2742 * The file is not closed if it is opened due to the a request to change
2743 * the size of the file. The open call will not be needed once the
2744 * VFS layer lookup-intents are implemented.
2745 *
2746 * Close is called when the inode is destroyed.
2747 * If we haven't opened the file for O_WRONLY, we
2748 * need to in the size_change case to obtain a stateid.
2749 *
2750 * Got race?
2751 * Because OPEN is always done by name in nfsv4, it is
2752 * possible that we opened a different file by the same
2753 * name. We can recognize this race condition, but we
2754 * can't do anything about it besides returning an error.
2755 *
2756 * This will be fixed with VFS changes (lookup-intent).
2757 */
2758static int
2759nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2760 struct iattr *sattr)
2761{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002762 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002763 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002764 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 int status;
2766
Benny Halevy8a1636c2010-07-14 15:43:57 -04002767 if (pnfs_ld_layoutret_on_setattr(inode))
2768 pnfs_return_layout(inode);
2769
Trond Myklebust0e574af2005-10-27 22:12:38 -04002770 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771
Andy Adamson26699402012-05-30 16:12:24 -04002772 /* Deal with open(O_TRUNC) */
2773 if (sattr->ia_valid & ATTR_OPEN)
2774 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2775
2776 /* Optimization: if the end result is no change, don't RPC */
2777 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2778 return 0;
2779
Trond Myklebustd5308382005-11-04 15:33:38 -05002780 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002781 if (sattr->ia_valid & ATTR_FILE) {
2782 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002783
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002784 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002785 if (ctx) {
2786 cred = ctx->cred;
2787 state = ctx->state;
2788 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002789 }
2790
2791 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002792 if (status == 0)
2793 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 return status;
2795}
2796
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002797static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2798 const struct qstr *name, struct nfs_fh *fhandle,
2799 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002800{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002801 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002802 int status;
2803 struct nfs4_lookup_arg args = {
2804 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002805 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002806 .name = name,
2807 };
2808 struct nfs4_lookup_res res = {
2809 .server = server,
2810 .fattr = fattr,
2811 .fh = fhandle,
2812 };
2813 struct rpc_message msg = {
2814 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2815 .rpc_argp = &args,
2816 .rpc_resp = &res,
2817 };
2818
2819 nfs_fattr_init(fattr);
2820
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002822 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 dprintk("NFS reply lookup: %d\n", status);
2824 return status;
2825}
2826
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002827static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002828{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002829 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002830 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002831 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2832 fattr->nlink = 2;
2833}
2834
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002835static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2836 struct qstr *name, struct nfs_fh *fhandle,
2837 struct nfs_fattr *fattr)
2838{
2839 struct nfs4_exception exception = { };
2840 struct rpc_clnt *client = *clnt;
2841 int err;
2842 do {
2843 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2844 switch (err) {
2845 case -NFS4ERR_BADNAME:
2846 err = -ENOENT;
2847 goto out;
2848 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002849 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002850 goto out;
2851 case -NFS4ERR_WRONGSEC:
2852 err = -EPERM;
2853 if (client != *clnt)
2854 goto out;
2855
2856 client = nfs4_create_sec_client(client, dir, name);
2857 if (IS_ERR(client))
2858 return PTR_ERR(client);
2859
2860 exception.retry = 1;
2861 break;
2862 default:
2863 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2864 }
2865 } while (exception.retry);
2866
2867out:
2868 if (err == 0)
2869 *clnt = client;
2870 else if (client != *clnt)
2871 rpc_shutdown_client(client);
2872
2873 return err;
2874}
2875
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002876static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002877 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002879 int status;
2880 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002881
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002882 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2883 if (client != NFS_CLIENT(dir)) {
2884 rpc_shutdown_client(client);
2885 nfs_fixup_secinfo_attributes(fattr);
2886 }
2887 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888}
2889
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002890struct rpc_clnt *
2891nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2892 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2893{
2894 int status;
2895 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2896
2897 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2898 if (status < 0) {
2899 rpc_shutdown_client(client);
2900 return ERR_PTR(status);
2901 }
2902 return client;
2903}
2904
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2906{
Trond Myklebust76b32992007-08-10 17:45:11 -04002907 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 struct nfs4_accessargs args = {
2909 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002910 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002912 struct nfs4_accessres res = {
2913 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002914 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 struct rpc_message msg = {
2916 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2917 .rpc_argp = &args,
2918 .rpc_resp = &res,
2919 .rpc_cred = entry->cred,
2920 };
2921 int mode = entry->mask;
2922 int status;
2923
2924 /*
2925 * Determine which access bits we want to ask for...
2926 */
2927 if (mode & MAY_READ)
2928 args.access |= NFS4_ACCESS_READ;
2929 if (S_ISDIR(inode->i_mode)) {
2930 if (mode & MAY_WRITE)
2931 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2932 if (mode & MAY_EXEC)
2933 args.access |= NFS4_ACCESS_LOOKUP;
2934 } else {
2935 if (mode & MAY_WRITE)
2936 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2937 if (mode & MAY_EXEC)
2938 args.access |= NFS4_ACCESS_EXECUTE;
2939 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002940
2941 res.fattr = nfs_alloc_fattr();
2942 if (res.fattr == NULL)
2943 return -ENOMEM;
2944
Bryan Schumaker7c513052011-03-24 17:12:24 +00002945 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002947 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04002948 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002950 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 return status;
2952}
2953
2954static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2955{
2956 struct nfs4_exception exception = { };
2957 int err;
2958 do {
2959 err = nfs4_handle_exception(NFS_SERVER(inode),
2960 _nfs4_proc_access(inode, entry),
2961 &exception);
2962 } while (exception.retry);
2963 return err;
2964}
2965
2966/*
2967 * TODO: For the time being, we don't try to get any attributes
2968 * along with any of the zero-copy operations READ, READDIR,
2969 * READLINK, WRITE.
2970 *
2971 * In the case of the first three, we want to put the GETATTR
2972 * after the read-type operation -- this is because it is hard
2973 * to predict the length of a GETATTR response in v4, and thus
2974 * align the READ data correctly. This means that the GETATTR
2975 * may end up partially falling into the page cache, and we should
2976 * shift it into the 'tail' of the xdr_buf before processing.
2977 * To do this efficiently, we need to know the total length
2978 * of data received, which doesn't seem to be available outside
2979 * of the RPC layer.
2980 *
2981 * In the case of WRITE, we also want to put the GETATTR after
2982 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04002983 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 *
2985 * Both of these changes to the XDR layer would in fact be quite
2986 * minor, but I decided to leave them for a subsequent patch.
2987 */
2988static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2989 unsigned int pgbase, unsigned int pglen)
2990{
2991 struct nfs4_readlink args = {
2992 .fh = NFS_FH(inode),
2993 .pgbase = pgbase,
2994 .pglen = pglen,
2995 .pages = &page,
2996 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002997 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 struct rpc_message msg = {
2999 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3000 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003001 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 };
3003
Bryan Schumaker7c513052011-03-24 17:12:24 +00003004 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 -07003005}
3006
3007static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3008 unsigned int pgbase, unsigned int pglen)
3009{
3010 struct nfs4_exception exception = { };
3011 int err;
3012 do {
3013 err = nfs4_handle_exception(NFS_SERVER(inode),
3014 _nfs4_proc_readlink(inode, page, pgbase, pglen),
3015 &exception);
3016 } while (exception.retry);
3017 return err;
3018}
3019
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003021 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023static int
3024nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003025 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026{
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003027 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 int status = 0;
3030
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003031 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3032 if (IS_ERR(ctx))
3033 return PTR_ERR(ctx);
3034
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003035 sattr->ia_mode &= ~current_umask();
Trond Myklebust8626e4a2012-07-16 12:01:42 -04003036 state = nfs4_do_open(dir, dentry, ctx->mode,
3037 flags, sattr, ctx->cred,
3038 &ctx->mdsthreshold);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04003039 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 if (IS_ERR(state)) {
3041 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003042 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04003044 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04003045 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003046 ctx->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047out:
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003048 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 return status;
3050}
3051
3052static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3053{
Trond Myklebust16e42952005-10-27 22:12:44 -04003054 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003055 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003057 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003059 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003060 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003061 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003063 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3064 .rpc_argp = &args,
3065 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003067 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003068
Bryan Schumaker7c513052011-03-24 17:12:24 +00003069 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003070 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003071 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 return status;
3073}
3074
3075static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3076{
3077 struct nfs4_exception exception = { };
3078 int err;
3079 do {
3080 err = nfs4_handle_exception(NFS_SERVER(dir),
3081 _nfs4_proc_remove(dir, name),
3082 &exception);
3083 } while (exception.retry);
3084 return err;
3085}
3086
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003087static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003089 struct nfs_server *server = NFS_SERVER(dir);
3090 struct nfs_removeargs *args = msg->rpc_argp;
3091 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003093 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003095 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096}
3097
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003098static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3099{
3100 if (nfs4_setup_sequence(NFS_SERVER(data->dir),
3101 &data->args.seq_args,
3102 &data->res.seq_res,
3103 task))
3104 return;
3105 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106}
3107
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003108static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003110 struct nfs_removeres *res = task->tk_msg.rpc_resp;
3111
Trond Myklebust14516c32010-07-31 14:29:06 -04003112 if (!nfs4_sequence_done(task, &res->seq_res))
3113 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003114 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003115 return 0;
3116 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003117 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118}
3119
Jeff Laytond3d41522010-09-17 17:31:57 -04003120static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3121{
3122 struct nfs_server *server = NFS_SERVER(dir);
3123 struct nfs_renameargs *arg = msg->rpc_argp;
3124 struct nfs_renameres *res = msg->rpc_resp;
3125
3126 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003127 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05003128 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003129}
3130
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003131static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3132{
3133 if (nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3134 &data->args.seq_args,
3135 &data->res.seq_res,
3136 task))
3137 return;
3138 rpc_call_start(task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003139}
3140
3141static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3142 struct inode *new_dir)
3143{
3144 struct nfs_renameres *res = task->tk_msg.rpc_resp;
3145
3146 if (!nfs4_sequence_done(task, &res->seq_res))
3147 return 0;
3148 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3149 return 0;
3150
3151 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003152 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003153 return 1;
3154}
3155
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3157 struct inode *new_dir, struct qstr *new_name)
3158{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003159 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04003160 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 .old_dir = NFS_FH(old_dir),
3162 .new_dir = NFS_FH(new_dir),
3163 .old_name = old_name,
3164 .new_name = new_name,
3165 };
Jeff Laytone8582a82010-09-17 17:31:06 -04003166 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003167 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003168 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 struct rpc_message msg = {
3170 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3171 .rpc_argp = &arg,
3172 .rpc_resp = &res,
3173 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003174 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175
Bryan Schumaker7c513052011-03-24 17:12:24 +00003176 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 if (!status) {
3178 update_changeattr(old_dir, &res.old_cinfo);
3179 update_changeattr(new_dir, &res.new_cinfo);
3180 }
3181 return status;
3182}
3183
3184static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3185 struct inode *new_dir, struct qstr *new_name)
3186{
3187 struct nfs4_exception exception = { };
3188 int err;
3189 do {
3190 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3191 _nfs4_proc_rename(old_dir, old_name,
3192 new_dir, new_name),
3193 &exception);
3194 } while (exception.retry);
3195 return err;
3196}
3197
3198static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3199{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003200 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 struct nfs4_link_arg arg = {
3202 .fh = NFS_FH(inode),
3203 .dir_fh = NFS_FH(dir),
3204 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003205 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003207 struct nfs4_link_res res = {
3208 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003209 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 struct rpc_message msg = {
3211 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3212 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003213 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003215 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216
Trond Myklebust136f2622010-04-16 16:22:49 -04003217 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003218 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003219 goto out;
3220
Bryan Schumaker7c513052011-03-24 17:12:24 +00003221 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003222 if (!status) {
3223 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04003224 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003225 }
Trond Myklebust136f2622010-04-16 16:22:49 -04003226out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003227 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 return status;
3229}
3230
3231static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3232{
3233 struct nfs4_exception exception = { };
3234 int err;
3235 do {
3236 err = nfs4_handle_exception(NFS_SERVER(inode),
3237 _nfs4_proc_link(inode, dir, name),
3238 &exception);
3239 } while (exception.retry);
3240 return err;
3241}
3242
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003243struct nfs4_createdata {
3244 struct rpc_message msg;
3245 struct nfs4_create_arg arg;
3246 struct nfs4_create_res res;
3247 struct nfs_fh fh;
3248 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003249};
3250
3251static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3252 struct qstr *name, struct iattr *sattr, u32 ftype)
3253{
3254 struct nfs4_createdata *data;
3255
3256 data = kzalloc(sizeof(*data), GFP_KERNEL);
3257 if (data != NULL) {
3258 struct nfs_server *server = NFS_SERVER(dir);
3259
3260 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3261 data->msg.rpc_argp = &data->arg;
3262 data->msg.rpc_resp = &data->res;
3263 data->arg.dir_fh = NFS_FH(dir);
3264 data->arg.server = server;
3265 data->arg.name = name;
3266 data->arg.attrs = sattr;
3267 data->arg.ftype = ftype;
3268 data->arg.bitmask = server->attr_bitmask;
3269 data->res.server = server;
3270 data->res.fh = &data->fh;
3271 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003272 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003273 }
3274 return data;
3275}
3276
3277static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3278{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003279 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003280 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003281 if (status == 0) {
3282 update_changeattr(dir, &data->res.dir_cinfo);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003283 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3284 }
3285 return status;
3286}
3287
3288static void nfs4_free_createdata(struct nfs4_createdata *data)
3289{
3290 kfree(data);
3291}
3292
Chuck Lever4f390c12006-08-22 20:06:22 -04003293static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003294 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003296 struct nfs4_createdata *data;
3297 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298
Chuck Lever94a6d752006-08-22 20:06:23 -04003299 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003300 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003301
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003302 status = -ENOMEM;
3303 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3304 if (data == NULL)
3305 goto out;
3306
3307 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3308 data->arg.u.symlink.pages = &page;
3309 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003311 status = nfs4_do_create(dir, dentry, data);
3312
3313 nfs4_free_createdata(data);
3314out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 return status;
3316}
3317
Chuck Lever4f390c12006-08-22 20:06:22 -04003318static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003319 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320{
3321 struct nfs4_exception exception = { };
3322 int err;
3323 do {
3324 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003325 _nfs4_proc_symlink(dir, dentry, page,
3326 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 &exception);
3328 } while (exception.retry);
3329 return err;
3330}
3331
3332static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3333 struct iattr *sattr)
3334{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003335 struct nfs4_createdata *data;
3336 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003338 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3339 if (data == NULL)
3340 goto out;
3341
3342 status = nfs4_do_create(dir, dentry, data);
3343
3344 nfs4_free_createdata(data);
3345out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 return status;
3347}
3348
3349static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3350 struct iattr *sattr)
3351{
3352 struct nfs4_exception exception = { };
3353 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003354
3355 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 do {
3357 err = nfs4_handle_exception(NFS_SERVER(dir),
3358 _nfs4_proc_mkdir(dir, dentry, sattr),
3359 &exception);
3360 } while (exception.retry);
3361 return err;
3362}
3363
3364static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003365 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366{
3367 struct inode *dir = dentry->d_inode;
3368 struct nfs4_readdir_arg args = {
3369 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003370 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 .pgbase = 0,
3372 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003373 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003374 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 };
3376 struct nfs4_readdir_res res;
3377 struct rpc_message msg = {
3378 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3379 .rpc_argp = &args,
3380 .rpc_resp = &res,
3381 .rpc_cred = cred,
3382 };
3383 int status;
3384
Harvey Harrison3110ff82008-05-02 13:42:44 -07003385 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003386 dentry->d_parent->d_name.name,
3387 dentry->d_name.name,
3388 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003389 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003391 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 -05003392 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003393 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003394 status += args.pgbase;
3395 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003396
3397 nfs_invalidate_atime(dir);
3398
Harvey Harrison3110ff82008-05-02 13:42:44 -07003399 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 return status;
3401}
3402
3403static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003404 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405{
3406 struct nfs4_exception exception = { };
3407 int err;
3408 do {
3409 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3410 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003411 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 &exception);
3413 } while (exception.retry);
3414 return err;
3415}
3416
3417static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3418 struct iattr *sattr, dev_t rdev)
3419{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003420 struct nfs4_createdata *data;
3421 int mode = sattr->ia_mode;
3422 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003424 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3425 if (data == NULL)
3426 goto out;
3427
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003429 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003431 data->arg.ftype = NF4BLK;
3432 data->arg.u.device.specdata1 = MAJOR(rdev);
3433 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 }
3435 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003436 data->arg.ftype = NF4CHR;
3437 data->arg.u.device.specdata1 = MAJOR(rdev);
3438 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003439 } else if (!S_ISSOCK(mode)) {
3440 status = -EINVAL;
3441 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003444 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003445out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003446 nfs4_free_createdata(data);
3447out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 return status;
3449}
3450
3451static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3452 struct iattr *sattr, dev_t rdev)
3453{
3454 struct nfs4_exception exception = { };
3455 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003456
3457 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 do {
3459 err = nfs4_handle_exception(NFS_SERVER(dir),
3460 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3461 &exception);
3462 } while (exception.retry);
3463 return err;
3464}
3465
3466static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3467 struct nfs_fsstat *fsstat)
3468{
3469 struct nfs4_statfs_arg args = {
3470 .fh = fhandle,
3471 .bitmask = server->attr_bitmask,
3472 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003473 struct nfs4_statfs_res res = {
3474 .fsstat = fsstat,
3475 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476 struct rpc_message msg = {
3477 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3478 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003479 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 };
3481
Trond Myklebust0e574af2005-10-27 22:12:38 -04003482 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003483 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484}
3485
3486static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3487{
3488 struct nfs4_exception exception = { };
3489 int err;
3490 do {
3491 err = nfs4_handle_exception(server,
3492 _nfs4_proc_statfs(server, fhandle, fsstat),
3493 &exception);
3494 } while (exception.retry);
3495 return err;
3496}
3497
3498static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3499 struct nfs_fsinfo *fsinfo)
3500{
3501 struct nfs4_fsinfo_arg args = {
3502 .fh = fhandle,
3503 .bitmask = server->attr_bitmask,
3504 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003505 struct nfs4_fsinfo_res res = {
3506 .fsinfo = fsinfo,
3507 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 struct rpc_message msg = {
3509 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3510 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003511 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 };
3513
Bryan Schumaker7c513052011-03-24 17:12:24 +00003514 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515}
3516
3517static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3518{
3519 struct nfs4_exception exception = { };
3520 int err;
3521
3522 do {
3523 err = nfs4_handle_exception(server,
3524 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3525 &exception);
3526 } while (exception.retry);
3527 return err;
3528}
3529
3530static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3531{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003532 int error;
3533
Trond Myklebust0e574af2005-10-27 22:12:38 -04003534 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003535 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003536 if (error == 0) {
3537 /* block layout checks this! */
3538 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003539 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003540 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003541
3542 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543}
3544
3545static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3546 struct nfs_pathconf *pathconf)
3547{
3548 struct nfs4_pathconf_arg args = {
3549 .fh = fhandle,
3550 .bitmask = server->attr_bitmask,
3551 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003552 struct nfs4_pathconf_res res = {
3553 .pathconf = pathconf,
3554 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 struct rpc_message msg = {
3556 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3557 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003558 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 };
3560
3561 /* None of the pathconf attributes are mandatory to implement */
3562 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3563 memset(pathconf, 0, sizeof(*pathconf));
3564 return 0;
3565 }
3566
Trond Myklebust0e574af2005-10-27 22:12:38 -04003567 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003568 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569}
3570
3571static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3572 struct nfs_pathconf *pathconf)
3573{
3574 struct nfs4_exception exception = { };
3575 int err;
3576
3577 do {
3578 err = nfs4_handle_exception(server,
3579 _nfs4_proc_pathconf(server, fhandle, pathconf),
3580 &exception);
3581 } while (exception.retry);
3582 return err;
3583}
3584
Benny Halevyd20581a2011-05-22 19:52:03 +03003585void __nfs4_read_done_cb(struct nfs_read_data *data)
3586{
Fred Isamancd841602012-04-20 14:47:44 -04003587 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003588}
3589
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003590static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591{
Fred Isamancd841602012-04-20 14:47:44 -04003592 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003594 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003595 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003596 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003598
Benny Halevyd20581a2011-05-22 19:52:03 +03003599 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003601 renew_lease(server, data->timestamp);
3602 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603}
3604
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003605static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3606{
3607
3608 dprintk("--> %s\n", __func__);
3609
3610 if (!nfs4_sequence_done(task, &data->res.seq_res))
3611 return -EAGAIN;
3612
Benny Halevyd20581a2011-05-22 19:52:03 +03003613 return data->read_done_cb ? data->read_done_cb(task, data) :
3614 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003615}
3616
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003617static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003620 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003621 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003622 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623}
3624
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003625static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3626{
Fred Isamancd841602012-04-20 14:47:44 -04003627 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003628 &data->args.seq_args,
3629 &data->res.seq_res,
3630 task))
3631 return;
3632 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633}
3634
Fred Isamanb029bc92011-03-03 15:13:42 +00003635static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636{
Fred Isamancd841602012-04-20 14:47:44 -04003637 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003639 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003640 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003641 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003643 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003645 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003646 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003647 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648}
3649
Fred Isamanb029bc92011-03-03 15:13:42 +00003650static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3651{
3652 if (!nfs4_sequence_done(task, &data->res.seq_res))
3653 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003654 return data->write_done_cb ? data->write_done_cb(task, data) :
3655 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003656}
3657
Trond Myklebust5a37f852012-04-28 14:55:16 -04003658static
3659bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003660{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003661 const struct nfs_pgio_header *hdr = data->header;
3662
3663 /* Don't request attributes for pNFS or O_DIRECT writes */
3664 if (data->ds_clp != NULL || hdr->dreq != NULL)
3665 return false;
3666 /* Otherwise, request attributes if and only if we don't hold
3667 * a delegation
3668 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04003669 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003670}
Fred Isamana69aef12011-03-03 15:13:47 +00003671
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003672static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673{
Fred Isamancd841602012-04-20 14:47:44 -04003674 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003675
Trond Myklebust5a37f852012-04-28 14:55:16 -04003676 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003677 data->args.bitmask = NULL;
3678 data->res.fattr = NULL;
3679 } else
3680 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003681
Fred Isamanb029bc92011-03-03 15:13:42 +00003682 if (!data->write_done_cb)
3683 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003684 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 data->timestamp = jiffies;
3686
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003687 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003688 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689}
3690
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003691static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3692{
Fred Isamancd841602012-04-20 14:47:44 -04003693 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003694 &data->args.seq_args,
3695 &data->res.seq_res,
3696 task))
3697 return;
3698 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699}
3700
Fred Isaman0b7c0152012-04-20 14:47:39 -04003701static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3702{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3704 &data->args.seq_args,
Trond Myklebust788e7a82006-03-20 13:44:27 -05003705 &data->res.seq_res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 return;
3708 rpc_call_start(task);
Trond Myklebust14516c32010-07-31 14:29:06 -04003709}
3710
Fred Isaman0b7c0152012-04-20 14:47:39 -04003711static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712{
3713 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003714
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003715 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003716 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003717 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003719 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720}
3721
Fred Isaman0b7c0152012-04-20 14:47:39 -04003722static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003723{
3724 if (!nfs4_sequence_done(task, &data->res.seq_res))
3725 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003726 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003727}
3728
Fred Isaman0b7c0152012-04-20 14:47:39 -04003729static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003731 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003732
Fred Isaman0b7c0152012-04-20 14:47:39 -04003733 if (data->commit_done_cb == NULL)
3734 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003735 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003736 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003737 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738}
3739
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003740struct nfs4_renewdata {
3741 struct nfs_client *client;
3742 unsigned long timestamp;
3743};
3744
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745/*
3746 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3747 * standalone procedure for queueing an asynchronous RENEW.
3748 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003749static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003750{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003751 struct nfs4_renewdata *data = calldata;
3752 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003753
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003754 if (atomic_read(&clp->cl_count) > 1)
3755 nfs4_schedule_state_renewal(clp);
3756 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003757 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003758}
3759
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003760static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003762 struct nfs4_renewdata *data = calldata;
3763 struct nfs_client *clp = data->client;
3764 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765
3766 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003767 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003768 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3769 return;
3770 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003771 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003772 return;
3773 }
3774 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003776 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777}
3778
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003779static const struct rpc_call_ops nfs4_renew_ops = {
3780 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003781 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003782};
3783
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003784static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785{
3786 struct rpc_message msg = {
3787 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3788 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003789 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003791 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003793 if (renew_flags == 0)
3794 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003795 if (!atomic_inc_not_zero(&clp->cl_count))
3796 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003797 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003798 if (data == NULL)
3799 return -ENOMEM;
3800 data->client = clp;
3801 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003803 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804}
3805
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003806static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807{
3808 struct rpc_message msg = {
3809 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3810 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003811 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812 };
3813 unsigned long now = jiffies;
3814 int status;
3815
3816 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3817 if (status < 0)
3818 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003819 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820 return 0;
3821}
3822
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003823static inline int nfs4_server_supports_acls(struct nfs_server *server)
3824{
3825 return (server->caps & NFS_CAP_ACLS)
3826 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3827 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3828}
3829
Trond Myklebust21f498c2012-08-24 10:59:25 -04003830/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3831 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003832 * the stack.
3833 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04003834#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003835
Neil Hormane9e3d722011-03-04 19:26:03 -05003836static int buf_to_pages_noslab(const void *buf, size_t buflen,
3837 struct page **pages, unsigned int *pgbase)
3838{
3839 struct page *newpage, **spages;
3840 int rc = 0;
3841 size_t len;
3842 spages = pages;
3843
3844 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04003845 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003846 newpage = alloc_page(GFP_KERNEL);
3847
3848 if (newpage == NULL)
3849 goto unwind;
3850 memcpy(page_address(newpage), buf, len);
3851 buf += len;
3852 buflen -= len;
3853 *pages++ = newpage;
3854 rc++;
3855 } while (buflen != 0);
3856
3857 return rc;
3858
3859unwind:
3860 for(; rc > 0; rc--)
3861 __free_page(spages[rc-1]);
3862 return -ENOMEM;
3863}
3864
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003865struct nfs4_cached_acl {
3866 int cached;
3867 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003868 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003869};
3870
3871static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003872{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003873 struct nfs_inode *nfsi = NFS_I(inode);
3874
3875 spin_lock(&inode->i_lock);
3876 kfree(nfsi->nfs4_acl);
3877 nfsi->nfs4_acl = acl;
3878 spin_unlock(&inode->i_lock);
3879}
3880
3881static void nfs4_zap_acl_attr(struct inode *inode)
3882{
3883 nfs4_set_cached_acl(inode, NULL);
3884}
3885
3886static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3887{
3888 struct nfs_inode *nfsi = NFS_I(inode);
3889 struct nfs4_cached_acl *acl;
3890 int ret = -ENOENT;
3891
3892 spin_lock(&inode->i_lock);
3893 acl = nfsi->nfs4_acl;
3894 if (acl == NULL)
3895 goto out;
3896 if (buf == NULL) /* user is just asking for length */
3897 goto out_len;
3898 if (acl->cached == 0)
3899 goto out;
3900 ret = -ERANGE; /* see getxattr(2) man page */
3901 if (acl->len > buflen)
3902 goto out;
3903 memcpy(buf, acl->data, acl->len);
3904out_len:
3905 ret = acl->len;
3906out:
3907 spin_unlock(&inode->i_lock);
3908 return ret;
3909}
3910
Sachin Prabhu5794d212012-04-17 14:36:40 +01003911static 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 +00003912{
3913 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003914 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003915
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003916 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003917 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003918 if (acl == NULL)
3919 goto out;
3920 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003921 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003922 } else {
3923 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3924 if (acl == NULL)
3925 goto out;
3926 acl->cached = 0;
3927 }
3928 acl->len = acl_len;
3929out:
3930 nfs4_set_cached_acl(inode, acl);
3931}
3932
Andy Adamsonbf118a32011-12-07 11:55:27 -05003933/*
3934 * The getxattr API returns the required buffer length when called with a
3935 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3936 * the required buf. On a NULL buf, we send a page of data to the server
3937 * guessing that the ACL request can be serviced by a page. If so, we cache
3938 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3939 * the cache. If not so, we throw away the page, and cache the required
3940 * length. The next getxattr call will then produce another round trip to
3941 * the server, this time with the input buf of the required size.
3942 */
Trond Myklebust16b42892006-08-24 12:27:15 -04003943static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003944{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003945 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003946 struct nfs_getaclargs args = {
3947 .fh = NFS_FH(inode),
3948 .acl_pages = pages,
3949 .acl_len = buflen,
3950 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003951 struct nfs_getaclres res = {
3952 .acl_len = buflen,
3953 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003954 struct rpc_message msg = {
3955 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3956 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003957 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003958 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003959 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3960 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003961
Andy Adamsonbf118a32011-12-07 11:55:27 -05003962 /* As long as we're doing a round trip to the server anyway,
3963 * let's be prepared for a page of acl data. */
3964 if (npages == 0)
3965 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003966 if (npages > ARRAY_SIZE(pages))
3967 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003968
Andy Adamsonbf118a32011-12-07 11:55:27 -05003969 for (i = 0; i < npages; i++) {
3970 pages[i] = alloc_page(GFP_KERNEL);
3971 if (!pages[i])
3972 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003973 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01003974
3975 /* for decoding across pages */
3976 res.acl_scratch = alloc_page(GFP_KERNEL);
3977 if (!res.acl_scratch)
3978 goto out_free;
3979
Andy Adamsonbf118a32011-12-07 11:55:27 -05003980 args.acl_len = npages * PAGE_SIZE;
3981 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003982
Peng Taode040be2012-01-10 22:42:47 +08003983 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003984 __func__, buf, buflen, npages, args.acl_len);
3985 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3986 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003987 if (ret)
3988 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003989
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003990 /* Handle the case where the passed-in buffer is too short */
3991 if (res.acl_flags & NFS4_ACL_TRUNC) {
3992 /* Did the user only issue a request for the acl length? */
3993 if (buf == NULL)
3994 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003995 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003996 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003997 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003998 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
3999 if (buf)
4000 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
4001out_ok:
4002 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004003out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05004004 for (i = 0; i < npages; i++)
4005 if (pages[i])
4006 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05004007 if (res.acl_scratch)
4008 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004009 return ret;
4010}
4011
Trond Myklebust16b42892006-08-24 12:27:15 -04004012static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4013{
4014 struct nfs4_exception exception = { };
4015 ssize_t ret;
4016 do {
4017 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
4018 if (ret >= 0)
4019 break;
4020 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4021 } while (exception.retry);
4022 return ret;
4023}
4024
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004025static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4026{
4027 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004028 int ret;
4029
4030 if (!nfs4_server_supports_acls(server))
4031 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004032 ret = nfs_revalidate_inode(server, inode);
4033 if (ret < 0)
4034 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004035 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4036 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004037 ret = nfs4_read_cached_acl(inode, buf, buflen);
4038 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004039 /* -ENOENT is returned if there is no ACL or if there is an ACL
4040 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004041 return ret;
4042 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004043}
4044
Trond Myklebust16b42892006-08-24 12:27:15 -04004045static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004046{
4047 struct nfs_server *server = NFS_SERVER(inode);
4048 struct page *pages[NFS4ACL_MAXPAGES];
4049 struct nfs_setaclargs arg = {
4050 .fh = NFS_FH(inode),
4051 .acl_pages = pages,
4052 .acl_len = buflen,
4053 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004054 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004055 struct rpc_message msg = {
4056 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4057 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004058 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004059 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004060 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004061 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004062
4063 if (!nfs4_server_supports_acls(server))
4064 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004065 if (npages > ARRAY_SIZE(pages))
4066 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05004067 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4068 if (i < 0)
4069 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004070 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004071 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004072
4073 /*
4074 * Free each page after tx, so the only ref left is
4075 * held by the network stack
4076 */
4077 for (; i > 0; i--)
4078 put_page(pages[i-1]);
4079
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004080 /*
4081 * Acl update can result in inode attribute update.
4082 * so mark the attribute cache invalid.
4083 */
4084 spin_lock(&inode->i_lock);
4085 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4086 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004087 nfs_access_zap_cache(inode);
4088 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004089 return ret;
4090}
4091
Trond Myklebust16b42892006-08-24 12:27:15 -04004092static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4093{
4094 struct nfs4_exception exception = { };
4095 int err;
4096 do {
4097 err = nfs4_handle_exception(NFS_SERVER(inode),
4098 __nfs4_proc_set_acl(inode, buf, buflen),
4099 &exception);
4100 } while (exception.retry);
4101 return err;
4102}
4103
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004105nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004107 struct nfs_client *clp = server->nfs_client;
4108
4109 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110 return 0;
4111 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004112 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004113 case -NFS4ERR_ADMIN_REVOKED:
4114 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04004115 if (state == NULL)
4116 break;
4117 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004118 case -NFS4ERR_OPENMODE:
4119 if (state == NULL)
4120 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004121 nfs4_schedule_stateid_recovery(server, state);
4122 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004123 case -NFS4ERR_EXPIRED:
4124 if (state != NULL)
4125 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004127 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004128 nfs4_schedule_lease_recovery(clp);
4129 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004130#if defined(CONFIG_NFS_V4_1)
4131 case -NFS4ERR_BADSESSION:
4132 case -NFS4ERR_BADSLOT:
4133 case -NFS4ERR_BAD_HIGH_SLOT:
4134 case -NFS4ERR_DEADSESSION:
4135 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4136 case -NFS4ERR_SEQ_FALSE_RETRY:
4137 case -NFS4ERR_SEQ_MISORDERED:
4138 dprintk("%s ERROR %d, Reset session\n", __func__,
4139 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004140 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04004141 task->tk_status = 0;
4142 return -EAGAIN;
4143#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004145 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05004146 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05004147 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4149 task->tk_status = 0;
4150 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004151 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152 case -NFS4ERR_OLD_STATEID:
4153 task->tk_status = 0;
4154 return -EAGAIN;
4155 }
4156 task->tk_status = nfs4_map_errors(task->tk_status);
4157 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004158wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004159 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004160 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4161 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4162 task->tk_status = 0;
4163 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164}
4165
Chuck Leverf0920752012-05-21 22:45:41 -04004166static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4167 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004168{
4169 __be32 verf[2];
4170
Chuck Lever2c820d92012-05-21 22:45:33 -04004171 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4172 /* An impossible timestamp guarantees this value
4173 * will never match a generated boot time. */
4174 verf[0] = 0;
4175 verf[1] = (__be32)(NSEC_PER_SEC + 1);
4176 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004177 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4178 verf[0] = (__be32)nn->boot_time.tv_sec;
4179 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04004180 }
Chuck Levercd937102012-03-02 17:14:31 -05004181 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4182}
4183
Chuck Levere984a552012-09-14 17:24:21 -04004184static unsigned int
4185nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4186 char *buf, size_t len)
4187{
4188 unsigned int result;
4189
4190 rcu_read_lock();
4191 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4192 clp->cl_ipaddr,
4193 rpc_peeraddr2str(clp->cl_rpcclient,
4194 RPC_DISPLAY_ADDR),
4195 rpc_peeraddr2str(clp->cl_rpcclient,
4196 RPC_DISPLAY_PROTO));
4197 rcu_read_unlock();
4198 return result;
4199}
4200
4201static unsigned int
4202nfs4_init_uniform_client_string(const struct nfs_client *clp,
4203 char *buf, size_t len)
4204{
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004205 char *nodename = clp->cl_rpcclient->cl_nodename;
4206
4207 if (nfs4_client_id_uniquifier[0] != '\0')
4208 nodename = nfs4_client_id_uniquifier;
Chuck Levere984a552012-09-14 17:24:21 -04004209 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4210 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004211 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004212}
4213
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004214/**
4215 * nfs4_proc_setclientid - Negotiate client ID
4216 * @clp: state data structure
4217 * @program: RPC program for NFSv4 callback service
4218 * @port: IP port number for NFS4 callback service
4219 * @cred: RPC credential to use for this call
4220 * @res: where to place the result
4221 *
4222 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4223 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004224int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4225 unsigned short port, struct rpc_cred *cred,
4226 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227{
4228 nfs4_verifier sc_verifier;
4229 struct nfs4_setclientid setclientid = {
4230 .sc_verifier = &sc_verifier,
4231 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004232 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 };
4234 struct rpc_message msg = {
4235 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4236 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004237 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004238 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004240 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241
Chuck Leverde734832012-07-11 16:30:50 -04004242 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004243 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004244 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4245 setclientid.sc_name_len =
4246 nfs4_init_uniform_client_string(clp,
4247 setclientid.sc_name,
4248 sizeof(setclientid.sc_name));
4249 else
4250 setclientid.sc_name_len =
4251 nfs4_init_nonuniform_client_string(clp,
4252 setclientid.sc_name,
4253 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004254 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004255 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004256 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004257 sizeof(setclientid.sc_netid),
4258 rpc_peeraddr2str(clp->cl_rpcclient,
4259 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004260 rcu_read_unlock();
4261 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004262 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 clp->cl_ipaddr, port >> 8, port & 255);
4264
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004265 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4266 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4267 setclientid.sc_name_len, setclientid.sc_name);
4268 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4269 dprintk("NFS reply setclientid: %d\n", status);
4270 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271}
4272
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004273/**
4274 * nfs4_proc_setclientid_confirm - Confirm client ID
4275 * @clp: state data structure
4276 * @res: result of a previous SETCLIENTID
4277 * @cred: RPC credential to use for this call
4278 *
4279 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4280 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004281int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004282 struct nfs4_setclientid_res *arg,
4283 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284{
4285 struct nfs_fsinfo fsinfo;
4286 struct rpc_message msg = {
4287 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004288 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004290 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 };
4292 unsigned long now;
4293 int status;
4294
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004295 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4296 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4297 clp->cl_clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004299 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 if (status == 0) {
4301 spin_lock(&clp->cl_lock);
4302 clp->cl_lease_time = fsinfo.lease_time * HZ;
4303 clp->cl_last_renewal = now;
4304 spin_unlock(&clp->cl_lock);
4305 }
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004306 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307 return status;
4308}
4309
Trond Myklebustfe650402006-01-03 09:55:18 +01004310struct nfs4_delegreturndata {
4311 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004312 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004313 struct nfs_fh fh;
4314 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004315 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004316 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004317 int rpc_status;
4318};
4319
Trond Myklebustfe650402006-01-03 09:55:18 +01004320static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4321{
4322 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004323
Trond Myklebust14516c32010-07-31 14:29:06 -04004324 if (!nfs4_sequence_done(task, &data->res.seq_res))
4325 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004326
Ricardo Labiaga79708862009-12-07 09:23:21 -05004327 switch (task->tk_status) {
4328 case -NFS4ERR_STALE_STATEID:
4329 case -NFS4ERR_EXPIRED:
4330 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004331 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004332 break;
4333 default:
4334 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4335 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004336 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004337 return;
4338 }
4339 }
4340 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004341}
4342
4343static void nfs4_delegreturn_release(void *calldata)
4344{
Trond Myklebustfe650402006-01-03 09:55:18 +01004345 kfree(calldata);
4346}
4347
Andy Adamson938e1012009-04-01 09:22:28 -04004348#if defined(CONFIG_NFS_V4_1)
4349static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4350{
4351 struct nfs4_delegreturndata *d_data;
4352
4353 d_data = (struct nfs4_delegreturndata *)data;
4354
Trond Myklebust035168a2010-06-16 09:52:26 -04004355 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04004356 &d_data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004357 &d_data->res.seq_res, task))
Andy Adamson938e1012009-04-01 09:22:28 -04004358 return;
4359 rpc_call_start(task);
4360}
4361#endif /* CONFIG_NFS_V4_1 */
4362
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004363static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004364#if defined(CONFIG_NFS_V4_1)
4365 .rpc_call_prepare = nfs4_delegreturn_prepare,
4366#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004367 .rpc_call_done = nfs4_delegreturn_done,
4368 .rpc_release = nfs4_delegreturn_release,
4369};
4370
Trond Myklebuste6f81072008-01-24 18:14:34 -05004371static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004372{
4373 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004374 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004375 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004376 struct rpc_message msg = {
4377 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4378 .rpc_cred = cred,
4379 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004380 struct rpc_task_setup task_setup_data = {
4381 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004382 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004383 .callback_ops = &nfs4_delegreturn_ops,
4384 .flags = RPC_TASK_ASYNC,
4385 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004386 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004387
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004388 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004389 if (data == NULL)
4390 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004391 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004392 data->args.fhandle = &data->fh;
4393 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004394 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004395 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004396 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004397 data->res.fattr = &data->fattr;
4398 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004399 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004400 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004401 data->rpc_status = 0;
4402
Trond Myklebustc970aa82007-07-14 15:39:59 -04004403 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004404 msg.rpc_argp = &data->args;
4405 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004406 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004407 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004408 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004409 if (!issync)
4410 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004411 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004412 if (status != 0)
4413 goto out;
4414 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004415 if (status == 0)
4416 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4417 else
4418 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004419out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004420 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004421 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422}
4423
Trond Myklebuste6f81072008-01-24 18:14:34 -05004424int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425{
4426 struct nfs_server *server = NFS_SERVER(inode);
4427 struct nfs4_exception exception = { };
4428 int err;
4429 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004430 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431 switch (err) {
4432 case -NFS4ERR_STALE_STATEID:
4433 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434 case 0:
4435 return 0;
4436 }
4437 err = nfs4_handle_exception(server, err, &exception);
4438 } while (exception.retry);
4439 return err;
4440}
4441
4442#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4443#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4444
4445/*
4446 * sleep, with exponential backoff, and retry the LOCK operation.
4447 */
4448static unsigned long
4449nfs4_set_lock_task_retry(unsigned long timeout)
4450{
Jeff Laytond3103102011-12-01 22:44:39 +01004451 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452 timeout <<= 1;
4453 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4454 return NFS4_LOCK_MAXTIMEOUT;
4455 return timeout;
4456}
4457
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4459{
4460 struct inode *inode = state->inode;
4461 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004462 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004463 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004465 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004467 struct nfs_lockt_res res = {
4468 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469 };
4470 struct rpc_message msg = {
4471 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4472 .rpc_argp = &arg,
4473 .rpc_resp = &res,
4474 .rpc_cred = state->owner->so_cred,
4475 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476 struct nfs4_lock_state *lsp;
4477 int status;
4478
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004479 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004480 status = nfs4_set_lock_state(state, request);
4481 if (status != 0)
4482 goto out;
4483 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004484 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004485 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004486 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004487 switch (status) {
4488 case 0:
4489 request->fl_type = F_UNLCK;
4490 break;
4491 case -NFS4ERR_DENIED:
4492 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004494 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004495out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 return status;
4497}
4498
4499static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4500{
4501 struct nfs4_exception exception = { };
4502 int err;
4503
4504 do {
4505 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4506 _nfs4_proc_getlk(state, cmd, request),
4507 &exception);
4508 } while (exception.retry);
4509 return err;
4510}
4511
4512static int do_vfs_lock(struct file *file, struct file_lock *fl)
4513{
4514 int res = 0;
4515 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4516 case FL_POSIX:
4517 res = posix_lock_file_wait(file, fl);
4518 break;
4519 case FL_FLOCK:
4520 res = flock_lock_file_wait(file, fl);
4521 break;
4522 default:
4523 BUG();
4524 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525 return res;
4526}
4527
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004528struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004529 struct nfs_locku_args arg;
4530 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004531 struct nfs4_lock_state *lsp;
4532 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004533 struct file_lock fl;
4534 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004535 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004536};
4537
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004538static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4539 struct nfs_open_context *ctx,
4540 struct nfs4_lock_state *lsp,
4541 struct nfs_seqid *seqid)
4542{
4543 struct nfs4_unlockdata *p;
4544 struct inode *inode = lsp->ls_state->inode;
4545
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004546 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004547 if (p == NULL)
4548 return NULL;
4549 p->arg.fh = NFS_FH(inode);
4550 p->arg.fl = &p->fl;
4551 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004552 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004553 p->arg.stateid = &lsp->ls_stateid;
4554 p->lsp = lsp;
4555 atomic_inc(&lsp->ls_count);
4556 /* Ensure we don't close file until we're done freeing locks! */
4557 p->ctx = get_nfs_open_context(ctx);
4558 memcpy(&p->fl, fl, sizeof(p->fl));
4559 p->server = NFS_SERVER(inode);
4560 return p;
4561}
4562
Trond Myklebust06f814a2006-01-03 09:55:07 +01004563static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004564{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004565 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004566 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004567 nfs4_put_lock_state(calldata->lsp);
4568 put_nfs_open_context(calldata->ctx);
4569 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004570}
4571
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004572static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004573{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004574 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004575
Trond Myklebust14516c32010-07-31 14:29:06 -04004576 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4577 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004578 switch (task->tk_status) {
4579 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004580 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4581 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004582 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004583 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004584 case -NFS4ERR_BAD_STATEID:
4585 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004586 case -NFS4ERR_STALE_STATEID:
4587 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004588 break;
4589 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004590 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004591 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004592 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04004593 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004594}
4595
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004596static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004597{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004598 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004600 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004601 return;
Trond Myklebust795a88c2012-09-10 13:26:49 -04004602 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004603 /* Note: exit _without_ running nfs4_locku_done */
4604 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004605 return;
4606 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004607 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004608 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004609 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004610 &calldata->res.seq_res,
4611 task) != 0)
4612 nfs_release_seqid(calldata->arg.seqid);
4613 else
4614 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615}
4616
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004617static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004618 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004619 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004620 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004621};
4622
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004623static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4624 struct nfs_open_context *ctx,
4625 struct nfs4_lock_state *lsp,
4626 struct nfs_seqid *seqid)
4627{
4628 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004629 struct rpc_message msg = {
4630 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4631 .rpc_cred = ctx->cred,
4632 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004633 struct rpc_task_setup task_setup_data = {
4634 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004635 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004636 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004637 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004638 .flags = RPC_TASK_ASYNC,
4639 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004640
Frank Filz137d6ac2007-07-09 15:32:29 -07004641 /* Ensure this is an unlock - when canceling a lock, the
4642 * canceled lock is passed in, and it won't be an unlock.
4643 */
4644 fl->fl_type = F_UNLCK;
4645
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004646 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4647 if (data == NULL) {
4648 nfs_free_seqid(seqid);
4649 return ERR_PTR(-ENOMEM);
4650 }
4651
Trond Myklebust9d12b212012-01-17 22:04:25 -05004652 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004653 msg.rpc_argp = &data->arg;
4654 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004655 task_setup_data.callback_data = data;
4656 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004657}
4658
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4660{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004661 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004662 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004663 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004664 struct rpc_task *task;
4665 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004666 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667
Trond Myklebust9b073572006-06-29 16:38:34 -04004668 status = nfs4_set_lock_state(state, request);
4669 /* Unlock _before_ we do the RPC call */
4670 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004671 down_read(&nfsi->rwsem);
4672 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4673 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004674 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004675 }
4676 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004677 if (status != 0)
4678 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004679 /* Is this a delegated lock? */
4680 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004681 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004682 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004683 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004684 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004685 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004686 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004687 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004688 status = PTR_ERR(task);
4689 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004690 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004691 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004692 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004693out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004694 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004695 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696}
4697
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004698struct nfs4_lockdata {
4699 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004700 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004701 struct nfs4_lock_state *lsp;
4702 struct nfs_open_context *ctx;
4703 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004704 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004705 int rpc_status;
4706 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004707 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004708};
4709
4710static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004711 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4712 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004713{
4714 struct nfs4_lockdata *p;
4715 struct inode *inode = lsp->ls_state->inode;
4716 struct nfs_server *server = NFS_SERVER(inode);
4717
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004718 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004719 if (p == NULL)
4720 return NULL;
4721
4722 p->arg.fh = NFS_FH(inode);
4723 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004724 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004725 if (p->arg.open_seqid == NULL)
4726 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004727 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004728 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004729 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004730 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004731 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004732 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004733 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004734 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004735 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004736 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004737 atomic_inc(&lsp->ls_count);
4738 p->ctx = get_nfs_open_context(ctx);
4739 memcpy(&p->fl, fl, sizeof(p->fl));
4740 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004741out_free_seqid:
4742 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004743out_free:
4744 kfree(p);
4745 return NULL;
4746}
4747
4748static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4749{
4750 struct nfs4_lockdata *data = calldata;
4751 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752
Harvey Harrison3110ff82008-05-02 13:42:44 -07004753 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004754 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4755 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004756 /* Do we need to do an open_to_lock_owner? */
4757 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004758 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004759 goto out_release_lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004760 data->arg.open_stateid = &state->stateid;
4761 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004762 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004763 } else
4764 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004765 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004766 if (nfs4_setup_sequence(data->server,
4767 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004768 &data->res.seq_res,
4769 task) == 0) {
4770 rpc_call_start(task);
Andy Adamson66179ef2009-04-01 09:22:22 -04004771 return;
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004772 }
4773 nfs_release_seqid(data->arg.open_seqid);
4774out_release_lock_seqid:
4775 nfs_release_seqid(data->arg.lock_seqid);
4776 dprintk("%s: done!, ret = %d\n", __func__, task->tk_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004777}
4778
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004779static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4780{
4781 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4782 nfs4_lock_prepare(task, calldata);
4783}
4784
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004785static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4786{
4787 struct nfs4_lockdata *data = calldata;
4788
Harvey Harrison3110ff82008-05-02 13:42:44 -07004789 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004790
Trond Myklebust14516c32010-07-31 14:29:06 -04004791 if (!nfs4_sequence_done(task, &data->res.seq_res))
4792 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004793
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004794 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004795 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004796 if (data->rpc_status == 0)
4797 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4798 else
4799 goto out;
4800 }
4801 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004802 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004803 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04004804 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004805 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004806out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004807 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004808}
4809
4810static void nfs4_lock_release(void *calldata)
4811{
4812 struct nfs4_lockdata *data = calldata;
4813
Harvey Harrison3110ff82008-05-02 13:42:44 -07004814 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004815 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004816 if (data->cancelled != 0) {
4817 struct rpc_task *task;
4818 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4819 data->arg.lock_seqid);
4820 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004821 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004822 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004823 } else
4824 nfs_free_seqid(data->arg.lock_seqid);
4825 nfs4_put_lock_state(data->lsp);
4826 put_nfs_open_context(data->ctx);
4827 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004828 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004829}
4830
4831static const struct rpc_call_ops nfs4_lock_ops = {
4832 .rpc_call_prepare = nfs4_lock_prepare,
4833 .rpc_call_done = nfs4_lock_done,
4834 .rpc_release = nfs4_lock_release,
4835};
4836
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004837static const struct rpc_call_ops nfs4_recover_lock_ops = {
4838 .rpc_call_prepare = nfs4_recover_lock_prepare,
4839 .rpc_call_done = nfs4_lock_done,
4840 .rpc_release = nfs4_lock_release,
4841};
4842
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004843static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4844{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004845 switch (error) {
4846 case -NFS4ERR_ADMIN_REVOKED:
4847 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004848 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004849 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04004850 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004851 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004852 break;
4853 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004854 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004855 case -NFS4ERR_EXPIRED:
4856 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004857 };
4858}
4859
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004860static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004861{
4862 struct nfs4_lockdata *data;
4863 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004864 struct rpc_message msg = {
4865 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4866 .rpc_cred = state->owner->so_cred,
4867 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004868 struct rpc_task_setup task_setup_data = {
4869 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004870 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004871 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004872 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004873 .flags = RPC_TASK_ASYNC,
4874 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004875 int ret;
4876
Harvey Harrison3110ff82008-05-02 13:42:44 -07004877 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004878 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004879 fl->fl_u.nfs4_fl.owner,
4880 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004881 if (data == NULL)
4882 return -ENOMEM;
4883 if (IS_SETLKW(cmd))
4884 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004885 if (recovery_type > NFS_LOCK_NEW) {
4886 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004887 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004888 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4889 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05004890 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004891 msg.rpc_argp = &data->arg;
4892 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004893 task_setup_data.callback_data = data;
4894 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004895 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004896 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004897 ret = nfs4_wait_for_completion_rpc_task(task);
4898 if (ret == 0) {
4899 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004900 if (ret)
4901 nfs4_handle_setlk_error(data->server, data->lsp,
4902 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004903 } else
4904 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004905 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004906 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004907 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908}
4909
4910static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4911{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004912 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004913 struct nfs4_exception exception = {
4914 .inode = state->inode,
4915 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004916 int err;
4917
4918 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004919 /* Cache the lock if possible... */
4920 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4921 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004922 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004923 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004924 break;
4925 nfs4_handle_exception(server, err, &exception);
4926 } while (exception.retry);
4927 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928}
4929
4930static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4931{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004932 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004933 struct nfs4_exception exception = {
4934 .inode = state->inode,
4935 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004936 int err;
4937
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004938 err = nfs4_set_lock_state(state, request);
4939 if (err != 0)
4940 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004941 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004942 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4943 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004944 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004945 switch (err) {
4946 default:
4947 goto out;
4948 case -NFS4ERR_GRACE:
4949 case -NFS4ERR_DELAY:
4950 nfs4_handle_exception(server, err, &exception);
4951 err = 0;
4952 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004953 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004954out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004955 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956}
4957
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004958#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004959/**
4960 * nfs41_check_expired_locks - possibly free a lock stateid
4961 *
4962 * @state: NFSv4 state for an inode
4963 *
4964 * Returns NFS_OK if recovery for this stateid is now finished.
4965 * Otherwise a negative NFS4ERR value is returned.
4966 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004967static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004968{
Chuck Levereb64cf92012-07-11 16:30:05 -04004969 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004970 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004971 struct nfs_server *server = NFS_SERVER(state->inode);
4972
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004973 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04004974 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004975 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4976 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004977 /* Free the stateid unless the server
4978 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04004979 if (status != -NFS4ERR_BAD_STATEID)
4980 nfs41_free_stateid(server,
4981 &lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004982 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004983 ret = status;
4984 }
4985 }
4986 };
4987
4988 return ret;
4989}
4990
4991static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4992{
4993 int status = NFS_OK;
4994
4995 if (test_bit(LK_STATE_IN_USE, &state->flags))
4996 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04004997 if (status != NFS_OK)
4998 status = nfs4_lock_expired(state, request);
4999 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005000}
5001#endif
5002
Linus Torvalds1da177e2005-04-16 15:20:36 -07005003static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5004{
Trond Myklebust19e03c52008-12-23 15:21:44 -05005005 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005006 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005007 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005008
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005009 if ((fl_flags & FL_POSIX) &&
5010 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5011 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005012 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005013 status = nfs4_set_lock_state(state, request);
5014 if (status != 0)
5015 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005016 request->fl_flags |= FL_ACCESS;
5017 status = do_vfs_lock(request->fl_file, request);
5018 if (status < 0)
5019 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005020 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005021 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04005022 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005023 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005024 request->fl_flags = fl_flags & ~FL_SLEEP;
5025 status = do_vfs_lock(request->fl_file, request);
5026 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005027 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005028 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005029 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04005030 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005031 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005032 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005033 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005034 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5035 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005036out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05005037 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005038out:
5039 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040 return status;
5041}
5042
5043static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5044{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005045 struct nfs4_exception exception = {
5046 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04005047 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005048 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005049 int err;
5050
5051 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07005052 err = _nfs4_proc_setlk(state, cmd, request);
5053 if (err == -NFS4ERR_DENIED)
5054 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005055 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07005056 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005057 } while (exception.retry);
5058 return err;
5059}
5060
5061static int
5062nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5063{
5064 struct nfs_open_context *ctx;
5065 struct nfs4_state *state;
5066 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5067 int status;
5068
5069 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005070 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071 state = ctx->state;
5072
5073 if (request->fl_start < 0 || request->fl_end < 0)
5074 return -EINVAL;
5075
Trond Myklebustd9531262009-07-21 19:22:38 -04005076 if (IS_GETLK(cmd)) {
5077 if (state != NULL)
5078 return nfs4_proc_getlk(state, F_GETLK, request);
5079 return 0;
5080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081
5082 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5083 return -EINVAL;
5084
Trond Myklebustd9531262009-07-21 19:22:38 -04005085 if (request->fl_type == F_UNLCK) {
5086 if (state != NULL)
5087 return nfs4_proc_unlck(state, cmd, request);
5088 return 0;
5089 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090
Trond Myklebustd9531262009-07-21 19:22:38 -04005091 if (state == NULL)
5092 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04005093 /*
5094 * Don't rely on the VFS having checked the file open mode,
5095 * since it won't do this for flock() locks.
5096 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04005097 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04005098 case F_RDLCK:
5099 if (!(filp->f_mode & FMODE_READ))
5100 return -EBADF;
5101 break;
5102 case F_WRLCK:
5103 if (!(filp->f_mode & FMODE_WRITE))
5104 return -EBADF;
5105 }
5106
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107 do {
5108 status = nfs4_proc_setlk(state, cmd, request);
5109 if ((status != -EAGAIN) || IS_SETLK(cmd))
5110 break;
5111 timeout = nfs4_set_lock_task_retry(timeout);
5112 status = -ERESTARTSYS;
5113 if (signalled())
5114 break;
5115 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116 return status;
5117}
5118
Trond Myklebust888e6942005-11-04 15:38:11 -05005119int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
5120{
5121 struct nfs_server *server = NFS_SERVER(state->inode);
5122 struct nfs4_exception exception = { };
5123 int err;
5124
5125 err = nfs4_set_lock_state(state, fl);
5126 if (err != 0)
5127 goto out;
5128 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005129 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07005130 switch (err) {
5131 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005132 printk(KERN_ERR "NFS: %s: unhandled error "
5133 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07005134 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005135 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07005136 goto out;
5137 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04005138 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07005139 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005140 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005141 nfs4_schedule_lease_recovery(server->nfs_client);
5142 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05005143 case -NFS4ERR_BADSESSION:
5144 case -NFS4ERR_BADSLOT:
5145 case -NFS4ERR_BAD_HIGH_SLOT:
5146 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
5147 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04005148 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07005149 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07005150 case -ERESTARTSYS:
5151 /*
5152 * The show must go on: exit, but mark the
5153 * stateid as needing recovery.
5154 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005155 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005156 case -NFS4ERR_ADMIN_REVOKED:
5157 case -NFS4ERR_BAD_STATEID:
5158 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005159 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07005160 err = 0;
5161 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04005162 case -EKEYEXPIRED:
5163 /*
5164 * User RPCSEC_GSS context has expired.
5165 * We cannot recover this stateid now, so
5166 * skip it and allow recovery thread to
5167 * proceed.
5168 */
5169 err = 0;
5170 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07005171 case -ENOMEM:
5172 case -NFS4ERR_DENIED:
5173 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
5174 err = 0;
5175 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07005176 case -NFS4ERR_DELAY:
5177 break;
5178 }
Trond Myklebust888e6942005-11-04 15:38:11 -05005179 err = nfs4_handle_exception(server, err, &exception);
5180 } while (exception.retry);
5181out:
5182 return err;
5183}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005184
Trond Myklebustcf470c32012-03-07 13:49:12 -05005185struct nfs_release_lockowner_data {
5186 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005187 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005188 struct nfs_release_lockowner_args args;
5189};
5190
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005191static void nfs4_release_lockowner_release(void *calldata)
5192{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005193 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005194 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005195 kfree(calldata);
5196}
5197
Trond Myklebust17280172012-03-11 13:11:00 -04005198static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005199 .rpc_release = nfs4_release_lockowner_release,
5200};
5201
Trond Myklebustcf470c32012-03-07 13:49:12 -05005202int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005203{
5204 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005205 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005206 struct rpc_message msg = {
5207 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5208 };
5209
5210 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005211 return -EINVAL;
5212 data = kmalloc(sizeof(*data), GFP_NOFS);
5213 if (!data)
5214 return -ENOMEM;
5215 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005216 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005217 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5218 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5219 data->args.lock_owner.s_dev = server->s_dev;
5220 msg.rpc_argp = &data->args;
5221 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5222 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005223}
5224
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005225#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5226
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005227static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5228 const void *buf, size_t buflen,
5229 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005230{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005231 if (strcmp(key, "") != 0)
5232 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005233
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005234 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005235}
5236
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005237static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5238 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005239{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005240 if (strcmp(key, "") != 0)
5241 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005242
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005243 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005244}
5245
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005246static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5247 size_t list_len, const char *name,
5248 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005249{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005250 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005251
J. Bruce Fields096455a2006-03-20 23:23:42 -05005252 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5253 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005254
5255 if (list && len <= list_len)
5256 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005257 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005258}
5259
Andy Adamson533eb462011-06-13 18:25:56 -04005260/*
5261 * nfs_fhget will use either the mounted_on_fileid or the fileid
5262 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005263static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5264{
Andy Adamson533eb462011-06-13 18:25:56 -04005265 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5266 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5267 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005268 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005269 return;
5270
5271 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005272 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005273 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5274 fattr->nlink = 2;
5275}
5276
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005277static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5278 const struct qstr *name,
5279 struct nfs4_fs_locations *fs_locations,
5280 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005281{
5282 struct nfs_server *server = NFS_SERVER(dir);
5283 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005284 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005285 };
5286 struct nfs4_fs_locations_arg args = {
5287 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005288 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005289 .page = page,
5290 .bitmask = bitmask,
5291 };
Benny Halevy22958462009-04-01 09:22:02 -04005292 struct nfs4_fs_locations_res res = {
5293 .fs_locations = fs_locations,
5294 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005295 struct rpc_message msg = {
5296 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5297 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005298 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005299 };
5300 int status;
5301
Harvey Harrison3110ff82008-05-02 13:42:44 -07005302 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005303
5304 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5305 * is not supported */
5306 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5307 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5308 else
5309 bitmask[0] |= FATTR4_WORD0_FILEID;
5310
Trond Myklebustc228fd32007-01-13 02:28:11 -05005311 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005312 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005313 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005314 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005315 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005316 return status;
5317}
5318
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005319int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5320 const struct qstr *name,
5321 struct nfs4_fs_locations *fs_locations,
5322 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005323{
5324 struct nfs4_exception exception = { };
5325 int err;
5326 do {
5327 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005328 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005329 &exception);
5330 } while (exception.retry);
5331 return err;
5332}
5333
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005334static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5335{
5336 int status;
5337 struct nfs4_secinfo_arg args = {
5338 .dir_fh = NFS_FH(dir),
5339 .name = name,
5340 };
5341 struct nfs4_secinfo_res res = {
5342 .flavors = flavors,
5343 };
5344 struct rpc_message msg = {
5345 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5346 .rpc_argp = &args,
5347 .rpc_resp = &res,
5348 };
5349
5350 dprintk("NFS call secinfo %s\n", name->name);
5351 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5352 dprintk("NFS reply secinfo: %d\n", status);
5353 return status;
5354}
5355
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005356int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5357 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005358{
5359 struct nfs4_exception exception = { };
5360 int err;
5361 do {
5362 err = nfs4_handle_exception(NFS_SERVER(dir),
5363 _nfs4_proc_secinfo(dir, name, flavors),
5364 &exception);
5365 } while (exception.retry);
5366 return err;
5367}
5368
Andy Adamson557134a2009-04-01 09:21:53 -04005369#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005370/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005371 * Check the exchange flags returned by the server for invalid flags, having
5372 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5373 * DS flags set.
5374 */
5375static int nfs4_check_cl_exchange_flags(u32 flags)
5376{
5377 if (flags & ~EXCHGID4_FLAG_MASK_R)
5378 goto out_inval;
5379 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5380 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5381 goto out_inval;
5382 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5383 goto out_inval;
5384 return NFS_OK;
5385out_inval:
5386 return -NFS4ERR_INVAL;
5387}
5388
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005389static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005390nfs41_same_server_scope(struct nfs41_server_scope *a,
5391 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005392{
5393 if (a->server_scope_sz == b->server_scope_sz &&
5394 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5395 return true;
5396
5397 return false;
5398}
5399
Andy Adamson357f54d2010-12-14 10:11:57 -05005400/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005401 * nfs4_proc_bind_conn_to_session()
5402 *
5403 * The 4.1 client currently uses the same TCP connection for the
5404 * fore and backchannel.
5405 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005406int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005407{
5408 int status;
5409 struct nfs41_bind_conn_to_session_res res;
5410 struct rpc_message msg = {
5411 .rpc_proc =
5412 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5413 .rpc_argp = clp,
5414 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005415 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005416 };
5417
5418 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005419
5420 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5421 if (unlikely(res.session == NULL)) {
5422 status = -ENOMEM;
5423 goto out;
5424 }
5425
5426 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5427 if (status == 0) {
5428 if (memcmp(res.session->sess_id.data,
5429 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5430 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5431 status = -EIO;
5432 goto out_session;
5433 }
5434 if (res.dir != NFS4_CDFS4_BOTH) {
5435 dprintk("NFS: %s: Unexpected direction from server\n",
5436 __func__);
5437 status = -EIO;
5438 goto out_session;
5439 }
5440 if (res.use_conn_in_rdma_mode) {
5441 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5442 __func__);
5443 status = -EIO;
5444 goto out_session;
5445 }
5446 }
5447out_session:
5448 kfree(res.session);
5449out:
5450 dprintk("<-- %s status= %d\n", __func__, status);
5451 return status;
5452}
5453
5454/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005455 * nfs4_proc_exchange_id()
5456 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005457 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5458 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04005459 * Since the clientid has expired, all compounds using sessions
5460 * associated with the stale clientid will be returning
5461 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5462 * be in some phase of session reset.
5463 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005464int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005465{
5466 nfs4_verifier verifier;
5467 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005468 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005469 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005470 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005471 };
5472 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005473 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005474 };
5475 int status;
5476 struct rpc_message msg = {
5477 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5478 .rpc_argp = &args,
5479 .rpc_resp = &res,
5480 .rpc_cred = cred,
5481 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005482
Chuck Leverf0920752012-05-21 22:45:41 -04005483 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04005484 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5485 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005486 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5487 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5488 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005489
Chuck Leveracdeb692012-05-21 22:46:16 -04005490 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005491 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005492 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005493 status = -ENOMEM;
5494 goto out;
5495 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005496
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005497 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005498 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005499 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005500 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005501 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005502 }
5503
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005504 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005505 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005506 status = -ENOMEM;
5507 goto out_server_scope;
5508 }
5509
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005510 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005511 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005512 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005513
Chuck Lever177313f2012-05-21 22:44:58 -04005514 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005515 clp->cl_clientid = res.clientid;
5516 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5517 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5518 clp->cl_seqid = res.seqid;
5519
Chuck Leveracdeb692012-05-21 22:46:16 -04005520 kfree(clp->cl_serverowner);
5521 clp->cl_serverowner = res.server_owner;
5522 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005523
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005524 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005525 kfree(clp->cl_implid);
5526 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005527
Chuck Lever177313f2012-05-21 22:44:58 -04005528 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005529 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005530 res.server_scope)) {
5531 dprintk("%s: server_scope mismatch detected\n",
5532 __func__);
5533 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005534 kfree(clp->cl_serverscope);
5535 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005536 }
5537
Chuck Lever177313f2012-05-21 22:44:58 -04005538 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005539 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005540 goto out;
5541 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005542 } else
5543 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005544
Chuck Leveracdeb692012-05-21 22:46:16 -04005545out_server_owner:
5546 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005547out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005548 kfree(res.server_scope);
5549out:
Chuck Lever177313f2012-05-21 22:44:58 -04005550 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005551 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005552 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005553 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04005554 clp->cl_implid->date.seconds,
5555 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005556 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005557 return status;
5558}
5559
Trond Myklebust66245532012-05-25 17:18:09 -04005560static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5561 struct rpc_cred *cred)
5562{
5563 struct rpc_message msg = {
5564 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5565 .rpc_argp = clp,
5566 .rpc_cred = cred,
5567 };
5568 int status;
5569
5570 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5571 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04005572 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04005573 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5574 return status;
5575}
5576
5577static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5578 struct rpc_cred *cred)
5579{
5580 unsigned int loop;
5581 int ret;
5582
5583 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5584 ret = _nfs4_proc_destroy_clientid(clp, cred);
5585 switch (ret) {
5586 case -NFS4ERR_DELAY:
5587 case -NFS4ERR_CLIENTID_BUSY:
5588 ssleep(1);
5589 break;
5590 default:
5591 return ret;
5592 }
5593 }
5594 return 0;
5595}
5596
5597int nfs4_destroy_clientid(struct nfs_client *clp)
5598{
5599 struct rpc_cred *cred;
5600 int ret = 0;
5601
5602 if (clp->cl_mvops->minor_version < 1)
5603 goto out;
5604 if (clp->cl_exchange_flags == 0)
5605 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04005606 if (clp->cl_preserve_clid)
5607 goto out;
Trond Myklebust66245532012-05-25 17:18:09 -04005608 cred = nfs4_get_exchange_id_cred(clp);
5609 ret = nfs4_proc_destroy_clientid(clp, cred);
5610 if (cred)
5611 put_rpccred(cred);
5612 switch (ret) {
5613 case 0:
5614 case -NFS4ERR_STALE_CLIENTID:
5615 clp->cl_exchange_flags = 0;
5616 }
5617out:
5618 return ret;
5619}
5620
Andy Adamson2050f0c2009-04-01 09:22:30 -04005621struct nfs4_get_lease_time_data {
5622 struct nfs4_get_lease_time_args *args;
5623 struct nfs4_get_lease_time_res *res;
5624 struct nfs_client *clp;
5625};
5626
5627static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5628 void *calldata)
5629{
5630 int ret;
5631 struct nfs4_get_lease_time_data *data =
5632 (struct nfs4_get_lease_time_data *)calldata;
5633
5634 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005635 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005636 /* just setup sequence, do not trigger session recovery
5637 since we're invoked within one */
5638 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005639 &data->args->la_seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005640 &data->res->lr_seq_res, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005641
Trond Myklebustd3edcf92012-10-15 13:14:43 -04005642 if (ret != -EAGAIN)
5643 rpc_call_start(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005644 dprintk("<-- %s\n", __func__);
5645}
5646
5647/*
5648 * Called from nfs4_state_manager thread for session setup, so don't recover
5649 * from sequence operation or clientid errors.
5650 */
5651static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5652{
5653 struct nfs4_get_lease_time_data *data =
5654 (struct nfs4_get_lease_time_data *)calldata;
5655
5656 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005657 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5658 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005659 switch (task->tk_status) {
5660 case -NFS4ERR_DELAY:
5661 case -NFS4ERR_GRACE:
5662 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5663 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5664 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005665 /* fall through */
5666 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005667 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005668 return;
5669 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005670 dprintk("<-- %s\n", __func__);
5671}
5672
Trond Myklebust17280172012-03-11 13:11:00 -04005673static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005674 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5675 .rpc_call_done = nfs4_get_lease_time_done,
5676};
5677
5678int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5679{
5680 struct rpc_task *task;
5681 struct nfs4_get_lease_time_args args;
5682 struct nfs4_get_lease_time_res res = {
5683 .lr_fsinfo = fsinfo,
5684 };
5685 struct nfs4_get_lease_time_data data = {
5686 .args = &args,
5687 .res = &res,
5688 .clp = clp,
5689 };
5690 struct rpc_message msg = {
5691 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5692 .rpc_argp = &args,
5693 .rpc_resp = &res,
5694 };
5695 struct rpc_task_setup task_setup = {
5696 .rpc_client = clp->cl_rpcclient,
5697 .rpc_message = &msg,
5698 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005699 .callback_data = &data,
5700 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005701 };
5702 int status;
5703
Trond Myklebust9d12b212012-01-17 22:04:25 -05005704 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005705 dprintk("--> %s\n", __func__);
5706 task = rpc_run_task(&task_setup);
5707
5708 if (IS_ERR(task))
5709 status = PTR_ERR(task);
5710 else {
5711 status = task->tk_status;
5712 rpc_put_task(task);
5713 }
5714 dprintk("<-- %s return %d\n", __func__, status);
5715
5716 return status;
5717}
5718
Trond Myklebust87dda672012-11-20 19:49:20 -05005719static int nfs4_grow_slot_table(struct nfs4_slot_table *tbl,
5720 u32 max_reqs, u32 ivalue)
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005721{
Trond Myklebust87dda672012-11-20 19:49:20 -05005722 if (max_reqs <= tbl->max_slots)
5723 return 0;
Trond Myklebust69d206b2012-11-22 13:21:02 -05005724 if (!IS_ERR(nfs4_find_or_create_slot(tbl, max_reqs - 1, ivalue, GFP_NOFS)))
Trond Myklebust87dda672012-11-20 19:49:20 -05005725 return 0;
5726 return -ENOMEM;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005727}
5728
Trond Myklebust87dda672012-11-20 19:49:20 -05005729static void nfs4_reset_slot_table(struct nfs4_slot_table *tbl,
5730 u32 server_highest_slotid,
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005731 u32 ivalue)
5732{
Trond Myklebust87dda672012-11-20 19:49:20 -05005733 struct nfs4_slot **p;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005734
Trond Myklebust87dda672012-11-20 19:49:20 -05005735 nfs4_shrink_slot_table(tbl, server_highest_slotid + 1);
5736 p = &tbl->slots;
5737 while (*p) {
5738 (*p)->seq_nr = ivalue;
5739 p = &(*p)->next;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005740 }
Trond Myklebustf9b1ef52012-10-29 16:48:40 -04005741 tbl->highest_used_slotid = NFS4_NO_SLOT;
Trond Myklebust87dda672012-11-20 19:49:20 -05005742 tbl->target_highest_slotid = server_highest_slotid;
5743 tbl->server_highest_slotid = server_highest_slotid;
5744 tbl->max_slotid = server_highest_slotid;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005745}
5746
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005747/*
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005748 * (re)Initialise a slot table
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005749 */
Trond Myklebust87dda672012-11-20 19:49:20 -05005750static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl,
5751 u32 max_reqs, u32 ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005752{
Trond Myklebust87dda672012-11-20 19:49:20 -05005753 int ret;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005754
Andy Adamson104aeba2010-01-14 17:45:10 -05005755 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5756 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005757
Trond Myklebust87dda672012-11-20 19:49:20 -05005758 if (max_reqs > NFS4_MAX_SLOT_TABLE)
5759 max_reqs = NFS4_MAX_SLOT_TABLE;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005760
Trond Myklebust87dda672012-11-20 19:49:20 -05005761 ret = nfs4_grow_slot_table(tbl, max_reqs, ivalue);
5762 if (ret)
5763 goto out;
5764
5765 spin_lock(&tbl->slot_tbl_lock);
5766 nfs4_reset_slot_table(tbl, max_reqs - 1, ivalue);
5767 spin_unlock(&tbl->slot_tbl_lock);
5768
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005769 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5770 tbl, tbl->slots, tbl->max_slots);
5771out:
5772 dprintk("<-- %s: return %d\n", __func__, ret);
5773 return ret;
5774}
5775
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005776/* Destroy the slot table */
5777static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5778{
Trond Myklebust87dda672012-11-20 19:49:20 -05005779 nfs4_shrink_slot_table(&session->fc_slot_table, 0);
5780 nfs4_shrink_slot_table(&session->bc_slot_table, 0);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005781}
5782
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005783/*
Andy Adamsonaacd5532011-11-09 13:58:21 -05005784 * Initialize or reset the forechannel and backchannel tables
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005785 */
Andy Adamsonaacd5532011-11-09 13:58:21 -05005786static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005787{
Trond Myklebustf26468f2009-12-05 19:32:11 -05005788 struct nfs4_slot_table *tbl;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005789 int status;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005790
Andy Adamsonaacd5532011-11-09 13:58:21 -05005791 dprintk("--> %s\n", __func__);
5792 /* Fore channel */
5793 tbl = &ses->fc_slot_table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -05005794 tbl->session = ses;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005795 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5796 if (status) /* -ENOMEM */
5797 return status;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005798 /* Back channel */
5799 tbl = &ses->bc_slot_table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -05005800 tbl->session = ses;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005801 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
5802 if (status && tbl->slots == NULL)
5803 /* Fore and back channel share a connection so get
5804 * both slot tables or neither */
5805 nfs4_destroy_slot_tables(ses);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005806 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04005807}
5808
5809struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5810{
5811 struct nfs4_session *session;
5812 struct nfs4_slot_table *tbl;
5813
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005814 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04005815 if (!session)
5816 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04005817
Andy Adamson557134a2009-04-01 09:21:53 -04005818 tbl = &session->fc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005819 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamson557134a2009-04-01 09:21:53 -04005820 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005821 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005822 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005823
5824 tbl = &session->bc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005825 tbl->highest_used_slotid = NFS4_NO_SLOT;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005826 spin_lock_init(&tbl->slot_tbl_lock);
5827 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005828 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005829
Trond Myklebust1055d762010-06-16 09:52:27 -04005830 session->session_state = 1<<NFS4_SESSION_INITING;
5831
Andy Adamson557134a2009-04-01 09:21:53 -04005832 session->clp = clp;
5833 return session;
5834}
5835
5836void nfs4_destroy_session(struct nfs4_session *session)
5837{
Trond Myklebust2446ab62012-03-01 17:00:56 -05005838 struct rpc_xprt *xprt;
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005839 struct rpc_cred *cred;
Trond Myklebust2446ab62012-03-01 17:00:56 -05005840
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005841 cred = nfs4_get_exchange_id_cred(session->clp);
5842 nfs4_proc_destroy_session(session, cred);
5843 if (cred)
5844 put_rpccred(cred);
Trond Myklebust2446ab62012-03-01 17:00:56 -05005845
5846 rcu_read_lock();
5847 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
5848 rcu_read_unlock();
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005849 dprintk("%s Destroy backchannel for xprt %p\n",
Trond Myklebust2446ab62012-03-01 17:00:56 -05005850 __func__, xprt);
5851 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005852 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04005853 kfree(session);
5854}
5855
Andy Adamsonfc931582009-04-01 09:22:31 -04005856/*
5857 * Initialize the values to be used by the client in CREATE_SESSION
5858 * If nfs4_init_session set the fore channel request and response sizes,
5859 * use them.
5860 *
5861 * Set the back channel max_resp_sz_cached to zero to force the client to
5862 * always set csa_cachethis to FALSE because the current implementation
5863 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5864 */
5865static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5866{
5867 struct nfs4_session *session = args->client->cl_session;
Trond Myklebustae72ae672012-11-20 11:02:55 -05005868 unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
5869 mxresp_sz = session->fc_target_max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005870
5871 if (mxrqst_sz == 0)
5872 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5873 if (mxresp_sz == 0)
5874 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5875 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005876 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5877 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005878 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005879 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005880
5881 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005882 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005883 __func__,
5884 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005885 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005886
5887 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005888 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5889 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5890 args->bc_attrs.max_resp_sz_cached = 0;
5891 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5892 args->bc_attrs.max_reqs = 1;
5893
5894 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5895 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5896 __func__,
5897 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5898 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5899 args->bc_attrs.max_reqs);
5900}
5901
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005902static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005903{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005904 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5905 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5906
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005907 if (rcvd->max_resp_sz > sent->max_resp_sz)
5908 return -EINVAL;
5909 /*
5910 * Our requested max_ops is the minimum we need; we're not
5911 * prepared to break up compounds into smaller pieces than that.
5912 * So, no point even trying to continue if the server won't
5913 * cooperate:
5914 */
5915 if (rcvd->max_ops < sent->max_ops)
5916 return -EINVAL;
5917 if (rcvd->max_reqs == 0)
5918 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005919 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5920 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005921 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005922}
5923
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005924static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5925{
5926 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5927 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005928
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005929 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5930 return -EINVAL;
5931 if (rcvd->max_resp_sz < sent->max_resp_sz)
5932 return -EINVAL;
5933 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5934 return -EINVAL;
5935 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005936 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005937 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005938 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005939 return -EINVAL;
5940 return 0;
5941}
Andy Adamson8d353012009-04-01 09:22:32 -04005942
Andy Adamson8d353012009-04-01 09:22:32 -04005943static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5944 struct nfs4_session *session)
5945{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005946 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005947
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005948 ret = nfs4_verify_fore_channel_attrs(args, session);
5949 if (ret)
5950 return ret;
5951 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005952}
5953
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005954static int _nfs4_proc_create_session(struct nfs_client *clp,
5955 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005956{
5957 struct nfs4_session *session = clp->cl_session;
5958 struct nfs41_create_session_args args = {
5959 .client = clp,
5960 .cb_program = NFS4_CALLBACK,
5961 };
5962 struct nfs41_create_session_res res = {
5963 .client = clp,
5964 };
5965 struct rpc_message msg = {
5966 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5967 .rpc_argp = &args,
5968 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005969 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04005970 };
5971 int status;
5972
5973 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005974 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005975
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005976 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005977
Trond Myklebust43095d32012-11-20 11:13:12 -05005978 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04005979 /* Verify the session's negotiated channel_attrs values */
5980 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005981 /* Increment the clientid slot sequence id */
5982 clp->cl_seqid++;
5983 }
5984
5985 return status;
5986}
5987
5988/*
5989 * Issues a CREATE_SESSION operation to the server.
5990 * It is the responsibility of the caller to verify the session is
5991 * expired before calling this routine.
5992 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005993int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005994{
5995 int status;
5996 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005997 struct nfs4_session *session = clp->cl_session;
5998
5999 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
6000
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006001 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04006002 if (status)
6003 goto out;
6004
Andy Adamsonaacd5532011-11-09 13:58:21 -05006005 /* Init or reset the session slot tables */
6006 status = nfs4_setup_session_slot_tables(session);
6007 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04006008 if (status)
6009 goto out;
6010
6011 ptr = (unsigned *)&session->sess_id.data[0];
6012 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
6013 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04006014out:
6015 dprintk("<-- %s\n", __func__);
6016 return status;
6017}
6018
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006019/*
6020 * Issue the over-the-wire RPC DESTROY_SESSION.
6021 * The caller must serialize access to this routine.
6022 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006023int nfs4_proc_destroy_session(struct nfs4_session *session,
6024 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006025{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006026 struct rpc_message msg = {
6027 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
6028 .rpc_argp = session,
6029 .rpc_cred = cred,
6030 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006031 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006032
6033 dprintk("--> nfs4_proc_destroy_session\n");
6034
6035 /* session is still being setup */
6036 if (session->clp->cl_cons_state != NFS_CS_READY)
6037 return status;
6038
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006039 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006040
6041 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04006042 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006043 "Session has been destroyed regardless...\n", status);
6044
6045 dprintk("<-- nfs4_proc_destroy_session\n");
6046 return status;
6047}
6048
Trond Myklebust7b38c362012-05-23 13:23:31 -04006049/*
6050 * With sessions, the client is not marked ready until after a
6051 * successful EXCHANGE_ID and CREATE_SESSION.
6052 *
6053 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
6054 * other versions of NFS can be tried.
6055 */
6056static int nfs41_check_session_ready(struct nfs_client *clp)
6057{
6058 int ret;
6059
6060 if (clp->cl_cons_state == NFS_CS_SESSION_INITING) {
6061 ret = nfs4_client_recover_expired_lease(clp);
6062 if (ret)
6063 return ret;
6064 }
6065 if (clp->cl_cons_state < NFS_CS_READY)
6066 return -EPROTONOSUPPORT;
Trond Myklebust54ac4712012-05-23 13:26:10 -04006067 smp_rmb();
Trond Myklebust7b38c362012-05-23 13:23:31 -04006068 return 0;
6069}
6070
Trond Myklebustfccba802009-07-21 16:48:07 -04006071int nfs4_init_session(struct nfs_server *server)
6072{
6073 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05006074 struct nfs4_session *session;
Trond Myklebustae72ae672012-11-20 11:02:55 -05006075 unsigned int target_max_rqst_sz = NFS_MAX_FILE_IO_SIZE;
6076 unsigned int target_max_resp_sz = NFS_MAX_FILE_IO_SIZE;
Trond Myklebustfccba802009-07-21 16:48:07 -04006077
6078 if (!nfs4_has_session(clp))
6079 return 0;
6080
Trond Myklebustae72ae672012-11-20 11:02:55 -05006081 if (server->rsize != 0)
6082 target_max_resp_sz = server->rsize;
6083 target_max_resp_sz += nfs41_maxread_overhead;
6084
6085 if (server->wsize != 0)
6086 target_max_rqst_sz = server->wsize;
6087 target_max_rqst_sz += nfs41_maxwrite_overhead;
6088
Trond Myklebust1055d762010-06-16 09:52:27 -04006089 session = clp->cl_session;
Trond Myklebust7b38c362012-05-23 13:23:31 -04006090 spin_lock(&clp->cl_lock);
6091 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
Trond Myklebustae72ae672012-11-20 11:02:55 -05006092 /* Initialise targets and channel attributes */
6093 session->fc_target_max_rqst_sz = target_max_rqst_sz;
6094 session->fc_attrs.max_rqst_sz = target_max_rqst_sz;
6095 session->fc_target_max_resp_sz = target_max_resp_sz;
6096 session->fc_attrs.max_resp_sz = target_max_resp_sz;
Trond Myklebust688a9022012-11-20 10:53:39 -05006097 } else {
6098 /* Just adjust the targets */
6099 if (target_max_rqst_sz > session->fc_target_max_rqst_sz) {
6100 session->fc_target_max_rqst_sz = target_max_rqst_sz;
6101 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
6102 }
6103 if (target_max_resp_sz > session->fc_target_max_resp_sz) {
6104 session->fc_target_max_resp_sz = target_max_resp_sz;
6105 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
6106 }
Trond Myklebust7b38c362012-05-23 13:23:31 -04006107 }
6108 spin_unlock(&clp->cl_lock);
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05006109
Trond Myklebust688a9022012-11-20 10:53:39 -05006110 if (test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state))
6111 nfs4_schedule_lease_recovery(clp);
6112
Trond Myklebust7b38c362012-05-23 13:23:31 -04006113 return nfs41_check_session_ready(clp);
Trond Myklebustfccba802009-07-21 16:48:07 -04006114}
6115
Trond Myklebust7b38c362012-05-23 13:23:31 -04006116int nfs4_init_ds_session(struct nfs_client *clp, unsigned long lease_time)
Andy Adamsond83217c2011-03-01 01:34:17 +00006117{
6118 struct nfs4_session *session = clp->cl_session;
6119 int ret;
6120
Trond Myklebust7b38c362012-05-23 13:23:31 -04006121 spin_lock(&clp->cl_lock);
6122 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
6123 /*
6124 * Do not set NFS_CS_CHECK_LEASE_TIME instead set the
6125 * DS lease to be equal to the MDS lease.
6126 */
6127 clp->cl_lease_time = lease_time;
6128 clp->cl_last_renewal = jiffies;
6129 }
6130 spin_unlock(&clp->cl_lock);
Andy Adamsond83217c2011-03-01 01:34:17 +00006131
Trond Myklebust7b38c362012-05-23 13:23:31 -04006132 ret = nfs41_check_session_ready(clp);
6133 if (ret)
6134 return ret;
6135 /* Test for the DS role */
6136 if (!is_ds_client(clp))
6137 return -ENODEV;
6138 return 0;
Andy Adamsond83217c2011-03-01 01:34:17 +00006139}
6140EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
6141
6142
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006143/*
6144 * Renew the cl_session lease.
6145 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006146struct nfs4_sequence_data {
6147 struct nfs_client *clp;
6148 struct nfs4_sequence_args args;
6149 struct nfs4_sequence_res res;
6150};
6151
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006152static void nfs41_sequence_release(void *data)
6153{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006154 struct nfs4_sequence_data *calldata = data;
6155 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006156
Alexandros Batsakis71358402010-02-05 03:45:05 -08006157 if (atomic_read(&clp->cl_count) > 1)
6158 nfs4_schedule_state_renewal(clp);
6159 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006160 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006161}
6162
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006163static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6164{
6165 switch(task->tk_status) {
6166 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006167 rpc_delay(task, NFS4_POLL_RETRY_MAX);
6168 return -EAGAIN;
6169 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006170 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006171 }
6172 return 0;
6173}
6174
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006175static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006176{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006177 struct nfs4_sequence_data *calldata = data;
6178 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006179
Trond Myklebust14516c32010-07-31 14:29:06 -04006180 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
6181 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006182
6183 if (task->tk_status < 0) {
6184 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006185 if (atomic_read(&clp->cl_count) == 1)
6186 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006187
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006188 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
6189 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006190 return;
6191 }
6192 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006193 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006194out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006195 dprintk("<-- %s\n", __func__);
6196}
6197
6198static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
6199{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006200 struct nfs4_sequence_data *calldata = data;
6201 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006202 struct nfs4_sequence_args *args;
6203 struct nfs4_sequence_res *res;
6204
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006205 args = task->tk_msg.rpc_argp;
6206 res = task->tk_msg.rpc_resp;
6207
Trond Myklebust9d12b212012-01-17 22:04:25 -05006208 if (nfs41_setup_sequence(clp->cl_session, args, res, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006209 return;
6210 rpc_call_start(task);
6211}
6212
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05006213static void nfs41_sequence_prepare_privileged(struct rpc_task *task, void *data)
6214{
6215 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
6216 nfs41_sequence_prepare(task, data);
6217}
6218
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006219static const struct rpc_call_ops nfs41_sequence_ops = {
6220 .rpc_call_done = nfs41_sequence_call_done,
6221 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006222 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006223};
6224
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05006225static const struct rpc_call_ops nfs41_sequence_privileged_ops = {
6226 .rpc_call_done = nfs41_sequence_call_done,
6227 .rpc_call_prepare = nfs41_sequence_prepare_privileged,
6228 .rpc_release = nfs41_sequence_release,
6229};
6230
6231static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred,
6232 const struct rpc_call_ops *seq_ops)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006233{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006234 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006235 struct rpc_message msg = {
6236 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
6237 .rpc_cred = cred,
6238 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006239 struct rpc_task_setup task_setup_data = {
6240 .rpc_client = clp->cl_rpcclient,
6241 .rpc_message = &msg,
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05006242 .callback_ops = seq_ops,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006243 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
6244 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006245
Alexandros Batsakis71358402010-02-05 03:45:05 -08006246 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006247 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04006248 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006249 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08006250 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006251 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006252 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05006253 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006254 msg.rpc_argp = &calldata->args;
6255 msg.rpc_resp = &calldata->res;
6256 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006257 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006258
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006259 return rpc_run_task(&task_setup_data);
6260}
6261
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006262static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006263{
6264 struct rpc_task *task;
6265 int ret = 0;
6266
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006267 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
6268 return 0;
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05006269 task = _nfs41_proc_sequence(clp, cred, &nfs41_sequence_ops);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006270 if (IS_ERR(task))
6271 ret = PTR_ERR(task);
6272 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08006273 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006274 dprintk("<-- %s status=%d\n", __func__, ret);
6275 return ret;
6276}
6277
6278static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6279{
6280 struct rpc_task *task;
6281 int ret;
6282
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05006283 task = _nfs41_proc_sequence(clp, cred, &nfs41_sequence_privileged_ops);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006284 if (IS_ERR(task)) {
6285 ret = PTR_ERR(task);
6286 goto out;
6287 }
6288 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05006289 if (!ret) {
6290 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6291
6292 if (task->tk_status == 0)
6293 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006294 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05006295 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006296 rpc_put_task(task);
6297out:
6298 dprintk("<-- %s status=%d\n", __func__, ret);
6299 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006300}
6301
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006302struct nfs4_reclaim_complete_data {
6303 struct nfs_client *clp;
6304 struct nfs41_reclaim_complete_args arg;
6305 struct nfs41_reclaim_complete_res res;
6306};
6307
6308static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6309{
6310 struct nfs4_reclaim_complete_data *calldata = data;
6311
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006312 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04006313 if (nfs41_setup_sequence(calldata->clp->cl_session,
6314 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006315 &calldata->res.seq_res, task))
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006316 return;
6317
6318 rpc_call_start(task);
6319}
6320
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006321static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6322{
6323 switch(task->tk_status) {
6324 case 0:
6325 case -NFS4ERR_COMPLETE_ALREADY:
6326 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6327 break;
6328 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006329 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006330 /* fall through */
6331 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006332 return -EAGAIN;
6333 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006334 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006335 }
6336 return 0;
6337}
6338
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006339static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6340{
6341 struct nfs4_reclaim_complete_data *calldata = data;
6342 struct nfs_client *clp = calldata->clp;
6343 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6344
6345 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006346 if (!nfs41_sequence_done(task, res))
6347 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006348
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006349 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6350 rpc_restart_call_prepare(task);
6351 return;
6352 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006353 dprintk("<-- %s\n", __func__);
6354}
6355
6356static void nfs4_free_reclaim_complete_data(void *data)
6357{
6358 struct nfs4_reclaim_complete_data *calldata = data;
6359
6360 kfree(calldata);
6361}
6362
6363static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6364 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6365 .rpc_call_done = nfs4_reclaim_complete_done,
6366 .rpc_release = nfs4_free_reclaim_complete_data,
6367};
6368
6369/*
6370 * Issue a global reclaim complete.
6371 */
6372static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
6373{
6374 struct nfs4_reclaim_complete_data *calldata;
6375 struct rpc_task *task;
6376 struct rpc_message msg = {
6377 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
6378 };
6379 struct rpc_task_setup task_setup_data = {
6380 .rpc_client = clp->cl_rpcclient,
6381 .rpc_message = &msg,
6382 .callback_ops = &nfs4_reclaim_complete_call_ops,
6383 .flags = RPC_TASK_ASYNC,
6384 };
6385 int status = -ENOMEM;
6386
6387 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006388 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006389 if (calldata == NULL)
6390 goto out;
6391 calldata->clp = clp;
6392 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006393
Trond Myklebust9d12b212012-01-17 22:04:25 -05006394 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006395 msg.rpc_argp = &calldata->arg;
6396 msg.rpc_resp = &calldata->res;
6397 task_setup_data.callback_data = calldata;
6398 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006399 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006400 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006401 goto out;
6402 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05006403 status = nfs4_wait_for_completion_rpc_task(task);
6404 if (status == 0)
6405 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006406 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006407 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006408out:
6409 dprintk("<-- %s status=%d\n", __func__, status);
6410 return status;
6411}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006412
6413static void
6414nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6415{
6416 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00006417 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006418
6419 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00006420 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6421 * right now covering the LAYOUTGET we are about to send.
6422 * However, that is not so catastrophic, and there seems
6423 * to be no way to prevent it completely.
6424 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006425 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006426 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006427 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006428 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6429 NFS_I(lgp->args.inode)->layout,
6430 lgp->args.ctx->state)) {
6431 rpc_exit(task, NFS4_OK);
6432 return;
6433 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006434 rpc_call_start(task);
6435}
6436
6437static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6438{
6439 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07006440 struct inode *inode = lgp->args.inode;
6441 struct nfs_server *server = NFS_SERVER(inode);
6442 struct pnfs_layout_hdr *lo;
6443 struct nfs4_state *state = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006444
6445 dprintk("--> %s\n", __func__);
6446
6447 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07006448 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006449
6450 switch (task->tk_status) {
6451 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07006452 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006453 case -NFS4ERR_LAYOUTTRYLATER:
6454 case -NFS4ERR_RECALLCONFLICT:
6455 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07006456 break;
6457 case -NFS4ERR_EXPIRED:
6458 case -NFS4ERR_BAD_STATEID:
6459 spin_lock(&inode->i_lock);
6460 lo = NFS_I(inode)->layout;
6461 if (!lo || list_empty(&lo->plh_segs)) {
6462 spin_unlock(&inode->i_lock);
6463 /* If the open stateid was bad, then recover it. */
6464 state = lgp->args.ctx->state;
6465 } else {
6466 LIST_HEAD(head);
6467
6468 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6469 spin_unlock(&inode->i_lock);
6470 /* Mark the bad layout state as invalid, then
6471 * retry using the open stateid. */
6472 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006473 }
6474 }
Trond Myklebustee314c22012-10-01 17:25:48 -07006475 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6476 rpc_restart_call_prepare(task);
6477out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006478 dprintk("<-- %s\n", __func__);
6479}
6480
Idan Kedar85541162012-08-02 11:47:10 +03006481static size_t max_response_pages(struct nfs_server *server)
6482{
6483 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6484 return nfs_page_array_len(0, max_resp_sz);
6485}
6486
6487static void nfs4_free_pages(struct page **pages, size_t size)
6488{
6489 int i;
6490
6491 if (!pages)
6492 return;
6493
6494 for (i = 0; i < size; i++) {
6495 if (!pages[i])
6496 break;
6497 __free_page(pages[i]);
6498 }
6499 kfree(pages);
6500}
6501
6502static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6503{
6504 struct page **pages;
6505 int i;
6506
6507 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6508 if (!pages) {
6509 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6510 return NULL;
6511 }
6512
6513 for (i = 0; i < size; i++) {
6514 pages[i] = alloc_page(gfp_flags);
6515 if (!pages[i]) {
6516 dprintk("%s: failed to allocate page\n", __func__);
6517 nfs4_free_pages(pages, size);
6518 return NULL;
6519 }
6520 }
6521
6522 return pages;
6523}
6524
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006525static void nfs4_layoutget_release(void *calldata)
6526{
6527 struct nfs4_layoutget *lgp = calldata;
Idan Kedar85541162012-08-02 11:47:10 +03006528 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6529 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006530
6531 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006532 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006533 put_nfs_open_context(lgp->args.ctx);
6534 kfree(calldata);
6535 dprintk("<-- %s\n", __func__);
6536}
6537
6538static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6539 .rpc_call_prepare = nfs4_layoutget_prepare,
6540 .rpc_call_done = nfs4_layoutget_done,
6541 .rpc_release = nfs4_layoutget_release,
6542};
6543
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006544struct pnfs_layout_segment *
6545nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006546{
6547 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Idan Kedar85541162012-08-02 11:47:10 +03006548 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006549 struct rpc_task *task;
6550 struct rpc_message msg = {
6551 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6552 .rpc_argp = &lgp->args,
6553 .rpc_resp = &lgp->res,
6554 };
6555 struct rpc_task_setup task_setup_data = {
6556 .rpc_client = server->client,
6557 .rpc_message = &msg,
6558 .callback_ops = &nfs4_layoutget_call_ops,
6559 .callback_data = lgp,
6560 .flags = RPC_TASK_ASYNC,
6561 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006562 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006563 int status = 0;
6564
6565 dprintk("--> %s\n", __func__);
6566
Idan Kedar85541162012-08-02 11:47:10 +03006567 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6568 if (!lgp->args.layout.pages) {
6569 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006570 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006571 }
6572 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
6573
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006574 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006575 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006576 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006577 task = rpc_run_task(&task_setup_data);
6578 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006579 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006580 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006581 if (status == 0)
6582 status = task->tk_status;
6583 if (status == 0)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006584 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006585 rpc_put_task(task);
6586 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006587 if (status)
6588 return ERR_PTR(status);
6589 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006590}
6591
Benny Halevycbe82602011-05-22 19:52:37 +03006592static void
6593nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6594{
6595 struct nfs4_layoutreturn *lrp = calldata;
6596
6597 dprintk("--> %s\n", __func__);
6598 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006599 &lrp->res.seq_res, task))
Benny Halevycbe82602011-05-22 19:52:37 +03006600 return;
6601 rpc_call_start(task);
6602}
6603
6604static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6605{
6606 struct nfs4_layoutreturn *lrp = calldata;
6607 struct nfs_server *server;
6608
6609 dprintk("--> %s\n", __func__);
6610
6611 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
6612 return;
6613
6614 server = NFS_SERVER(lrp->args.inode);
6615 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006616 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006617 return;
6618 }
Benny Halevycbe82602011-05-22 19:52:37 +03006619 dprintk("<-- %s\n", __func__);
6620}
6621
6622static void nfs4_layoutreturn_release(void *calldata)
6623{
6624 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04006625 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006626
6627 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04006628 spin_lock(&lo->plh_inode->i_lock);
6629 if (lrp->res.lrs_present)
6630 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6631 lo->plh_block_lgets--;
6632 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006633 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006634 kfree(calldata);
6635 dprintk("<-- %s\n", __func__);
6636}
6637
6638static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6639 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6640 .rpc_call_done = nfs4_layoutreturn_done,
6641 .rpc_release = nfs4_layoutreturn_release,
6642};
6643
6644int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6645{
6646 struct rpc_task *task;
6647 struct rpc_message msg = {
6648 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6649 .rpc_argp = &lrp->args,
6650 .rpc_resp = &lrp->res,
6651 };
6652 struct rpc_task_setup task_setup_data = {
6653 .rpc_client = lrp->clp->cl_rpcclient,
6654 .rpc_message = &msg,
6655 .callback_ops = &nfs4_layoutreturn_call_ops,
6656 .callback_data = lrp,
6657 };
6658 int status;
6659
6660 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006661 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006662 task = rpc_run_task(&task_setup_data);
6663 if (IS_ERR(task))
6664 return PTR_ERR(task);
6665 status = task->tk_status;
6666 dprintk("<-- %s status=%d\n", __func__, status);
6667 rpc_put_task(task);
6668 return status;
6669}
6670
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006671/*
6672 * Retrieve the list of Data Server devices from the MDS.
6673 */
6674static int _nfs4_getdevicelist(struct nfs_server *server,
6675 const struct nfs_fh *fh,
6676 struct pnfs_devicelist *devlist)
6677{
6678 struct nfs4_getdevicelist_args args = {
6679 .fh = fh,
6680 .layoutclass = server->pnfs_curr_ld->id,
6681 };
6682 struct nfs4_getdevicelist_res res = {
6683 .devlist = devlist,
6684 };
6685 struct rpc_message msg = {
6686 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6687 .rpc_argp = &args,
6688 .rpc_resp = &res,
6689 };
6690 int status;
6691
6692 dprintk("--> %s\n", __func__);
6693 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6694 &res.seq_res, 0);
6695 dprintk("<-- %s status=%d\n", __func__, status);
6696 return status;
6697}
6698
6699int nfs4_proc_getdevicelist(struct nfs_server *server,
6700 const struct nfs_fh *fh,
6701 struct pnfs_devicelist *devlist)
6702{
6703 struct nfs4_exception exception = { };
6704 int err;
6705
6706 do {
6707 err = nfs4_handle_exception(server,
6708 _nfs4_getdevicelist(server, fh, devlist),
6709 &exception);
6710 } while (exception.retry);
6711
6712 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6713 err, devlist->num_devs);
6714
6715 return err;
6716}
6717EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6718
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006719static int
6720_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6721{
6722 struct nfs4_getdeviceinfo_args args = {
6723 .pdev = pdev,
6724 };
6725 struct nfs4_getdeviceinfo_res res = {
6726 .pdev = pdev,
6727 };
6728 struct rpc_message msg = {
6729 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6730 .rpc_argp = &args,
6731 .rpc_resp = &res,
6732 };
6733 int status;
6734
6735 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006736 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006737 dprintk("<-- %s status=%d\n", __func__, status);
6738
6739 return status;
6740}
6741
6742int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6743{
6744 struct nfs4_exception exception = { };
6745 int err;
6746
6747 do {
6748 err = nfs4_handle_exception(server,
6749 _nfs4_proc_getdeviceinfo(server, pdev),
6750 &exception);
6751 } while (exception.retry);
6752 return err;
6753}
6754EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6755
Andy Adamson863a3c62011-03-23 13:27:54 +00006756static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6757{
6758 struct nfs4_layoutcommit_data *data = calldata;
6759 struct nfs_server *server = NFS_SERVER(data->args.inode);
6760
6761 if (nfs4_setup_sequence(server, &data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006762 &data->res.seq_res, task))
Andy Adamson863a3c62011-03-23 13:27:54 +00006763 return;
6764 rpc_call_start(task);
6765}
6766
6767static void
6768nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6769{
6770 struct nfs4_layoutcommit_data *data = calldata;
6771 struct nfs_server *server = NFS_SERVER(data->args.inode);
6772
6773 if (!nfs4_sequence_done(task, &data->res.seq_res))
6774 return;
6775
6776 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006777 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6778 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6779 case -NFS4ERR_BADLAYOUT: /* no layout */
6780 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006781 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006782 break;
6783 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006784 nfs_post_op_update_inode_force_wcc(data->args.inode,
6785 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006786 break;
6787 default:
6788 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6789 rpc_restart_call_prepare(task);
6790 return;
6791 }
6792 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006793}
6794
6795static void nfs4_layoutcommit_release(void *calldata)
6796{
6797 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04006798 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07006799 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00006800
Andy Adamsondb29c082011-07-30 20:52:38 -04006801 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006802 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04006803 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6804 list_del_init(&lseg->pls_lc_list);
6805 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6806 &lseg->pls_flags))
Trond Myklebust9369a432012-09-18 20:57:08 -04006807 pnfs_put_lseg(lseg);
Peng Taoa9bae562011-07-30 20:52:33 -04006808 }
Peng Tao92407e72011-10-23 20:21:17 -07006809
6810 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6811 smp_mb__after_clear_bit();
6812 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6813
Andy Adamson863a3c62011-03-23 13:27:54 +00006814 put_rpccred(data->cred);
6815 kfree(data);
6816}
6817
6818static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6819 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6820 .rpc_call_done = nfs4_layoutcommit_done,
6821 .rpc_release = nfs4_layoutcommit_release,
6822};
6823
6824int
Andy Adamsonef311532011-03-12 02:58:10 -05006825nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006826{
6827 struct rpc_message msg = {
6828 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6829 .rpc_argp = &data->args,
6830 .rpc_resp = &data->res,
6831 .rpc_cred = data->cred,
6832 };
6833 struct rpc_task_setup task_setup_data = {
6834 .task = &data->task,
6835 .rpc_client = NFS_CLIENT(data->args.inode),
6836 .rpc_message = &msg,
6837 .callback_ops = &nfs4_layoutcommit_ops,
6838 .callback_data = data,
6839 .flags = RPC_TASK_ASYNC,
6840 };
6841 struct rpc_task *task;
6842 int status = 0;
6843
6844 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6845 "lbw: %llu inode %lu\n",
6846 data->task.tk_pid, sync,
6847 data->args.lastbytewritten,
6848 data->args.inode->i_ino);
6849
Trond Myklebust9d12b212012-01-17 22:04:25 -05006850 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006851 task = rpc_run_task(&task_setup_data);
6852 if (IS_ERR(task))
6853 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006854 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006855 goto out;
6856 status = nfs4_wait_for_completion_rpc_task(task);
6857 if (status != 0)
6858 goto out;
6859 status = task->tk_status;
6860out:
6861 dprintk("%s: status %d\n", __func__, status);
6862 rpc_put_task(task);
6863 return status;
6864}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006865
6866static int
6867_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6868 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6869{
6870 struct nfs41_secinfo_no_name_args args = {
6871 .style = SECINFO_STYLE_CURRENT_FH,
6872 };
6873 struct nfs4_secinfo_res res = {
6874 .flavors = flavors,
6875 };
6876 struct rpc_message msg = {
6877 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6878 .rpc_argp = &args,
6879 .rpc_resp = &res,
6880 };
6881 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6882}
6883
6884static int
6885nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6886 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6887{
6888 struct nfs4_exception exception = { };
6889 int err;
6890 do {
6891 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6892 switch (err) {
6893 case 0:
6894 case -NFS4ERR_WRONGSEC:
6895 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006896 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006897 default:
6898 err = nfs4_handle_exception(server, err, &exception);
6899 }
6900 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006901out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006902 return err;
6903}
6904
6905static int
6906nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6907 struct nfs_fsinfo *info)
6908{
6909 int err;
6910 struct page *page;
6911 rpc_authflavor_t flavor;
6912 struct nfs4_secinfo_flavors *flavors;
6913
6914 page = alloc_page(GFP_KERNEL);
6915 if (!page) {
6916 err = -ENOMEM;
6917 goto out;
6918 }
6919
6920 flavors = page_address(page);
6921 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6922
6923 /*
6924 * Fall back on "guess and check" method if
6925 * the server doesn't support SECINFO_NO_NAME
6926 */
6927 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6928 err = nfs4_find_root_sec(server, fhandle, info);
6929 goto out_freepage;
6930 }
6931 if (err)
6932 goto out_freepage;
6933
6934 flavor = nfs_find_best_sec(flavors);
6935 if (err == 0)
6936 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6937
6938out_freepage:
6939 put_page(page);
6940 if (err == -EACCES)
6941 return -EPERM;
6942out:
6943 return err;
6944}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006945
6946static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006947{
6948 int status;
6949 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006950 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006951 };
6952 struct nfs41_test_stateid_res res;
6953 struct rpc_message msg = {
6954 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6955 .rpc_argp = &args,
6956 .rpc_resp = &res,
6957 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006958
Chuck Lever38527b12012-07-11 16:30:23 -04006959 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006960 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006961 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Chuck Lever38527b12012-07-11 16:30:23 -04006962 if (status != NFS_OK) {
6963 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04006964 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04006965 }
6966 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04006967 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006968}
6969
Chuck Lever38527b12012-07-11 16:30:23 -04006970/**
6971 * nfs41_test_stateid - perform a TEST_STATEID operation
6972 *
6973 * @server: server / transport on which to perform the operation
6974 * @stateid: state ID to test
6975 *
6976 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6977 * Otherwise a negative NFS4ERR value is returned if the operation
6978 * failed or the state ID is not currently valid.
6979 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006980static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006981{
6982 struct nfs4_exception exception = { };
6983 int err;
6984 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006985 err = _nfs41_test_stateid(server, stateid);
6986 if (err != -NFS4ERR_DELAY)
6987 break;
6988 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04006989 } while (exception.retry);
6990 return err;
6991}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006992
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006993static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006994{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006995 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006996 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006997 };
6998 struct nfs41_free_stateid_res res;
6999 struct rpc_message msg = {
7000 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
7001 .rpc_argp = &args,
7002 .rpc_resp = &res,
7003 };
Chuck Lever38527b12012-07-11 16:30:23 -04007004 int status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007005
Chuck Lever38527b12012-07-11 16:30:23 -04007006 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05007007 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Chuck Lever38527b12012-07-11 16:30:23 -04007008 status = nfs4_call_sync_sequence(server->client, server, &msg,
Chuck Lever377e5072012-07-11 16:29:45 -04007009 &args.seq_args, &res.seq_res, 1);
Chuck Lever38527b12012-07-11 16:30:23 -04007010 dprintk("NFS reply free_stateid: %d\n", status);
7011 return status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007012}
7013
Chuck Lever38527b12012-07-11 16:30:23 -04007014/**
7015 * nfs41_free_stateid - perform a FREE_STATEID operation
7016 *
7017 * @server: server / transport on which to perform the operation
7018 * @stateid: state ID to release
7019 *
7020 * Returns NFS_OK if the server freed "stateid". Otherwise a
7021 * negative NFS4ERR value is returned.
7022 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007023static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007024{
7025 struct nfs4_exception exception = { };
7026 int err;
7027 do {
Chuck Lever377e5072012-07-11 16:29:45 -04007028 err = _nfs4_free_stateid(server, stateid);
7029 if (err != -NFS4ERR_DELAY)
7030 break;
7031 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007032 } while (exception.retry);
7033 return err;
7034}
Trond Myklebust36281ca2012-03-04 18:13:56 -05007035
7036static bool nfs41_match_stateid(const nfs4_stateid *s1,
7037 const nfs4_stateid *s2)
7038{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007039 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007040 return false;
7041
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007042 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007043 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007044 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007045 return true;
7046
7047 return false;
7048}
7049
Andy Adamson557134a2009-04-01 09:21:53 -04007050#endif /* CONFIG_NFS_V4_1 */
7051
Trond Myklebust36281ca2012-03-04 18:13:56 -05007052static bool nfs4_match_stateid(const nfs4_stateid *s1,
7053 const nfs4_stateid *s2)
7054{
Trond Myklebustf597c532012-03-04 18:13:56 -05007055 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05007056}
7057
7058
Trond Myklebust17280172012-03-11 13:11:00 -04007059static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05007060 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05007061 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007062 .recover_open = nfs4_open_reclaim,
7063 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04007064 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04007065 .get_clid_cred = nfs4_get_setclientid_cred,
Chuck Lever05f4c352012-09-14 17:24:32 -04007066 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007067};
7068
Andy Adamson591d71c2009-04-01 09:22:47 -04007069#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007070static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04007071 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
7072 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
7073 .recover_open = nfs4_open_reclaim,
7074 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05007075 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04007076 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007077 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04007078 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04007079};
7080#endif /* CONFIG_NFS_V4_1 */
7081
Trond Myklebust17280172012-03-11 13:11:00 -04007082static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05007083 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05007084 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007085 .recover_open = nfs4_open_expired,
7086 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04007087 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04007088 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007089};
7090
Andy Adamson591d71c2009-04-01 09:22:47 -04007091#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007092static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04007093 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
7094 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04007095 .recover_open = nfs41_open_expired,
7096 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05007097 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04007098 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04007099};
7100#endif /* CONFIG_NFS_V4_1 */
7101
Trond Myklebust17280172012-03-11 13:11:00 -04007102static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04007103 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04007104 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04007105 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04007106};
7107
7108#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007109static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04007110 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04007111 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04007112 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04007113};
7114#endif
7115
Trond Myklebust97dc1352010-06-16 09:52:26 -04007116static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
7117 .minor_version = 0,
7118 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05007119 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007120 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04007121 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
7122 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
7123 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007124};
7125
7126#if defined(CONFIG_NFS_V4_1)
7127static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
7128 .minor_version = 1,
7129 .call_sync = _nfs4_call_sync_session,
Trond Myklebust36281ca2012-03-04 18:13:56 -05007130 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007131 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04007132 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
7133 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
7134 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007135};
7136#endif
7137
Trond Myklebust97dc1352010-06-16 09:52:26 -04007138const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
7139 [0] = &nfs_v4_0_minor_ops,
7140#if defined(CONFIG_NFS_V4_1)
7141 [1] = &nfs_v4_1_minor_ops,
7142#endif
7143};
7144
Bryan Schumaker73a79702012-07-16 16:39:12 -04007145const struct inode_operations nfs4_dir_inode_operations = {
7146 .create = nfs_create,
7147 .lookup = nfs_lookup,
7148 .atomic_open = nfs_atomic_open,
7149 .link = nfs_link,
7150 .unlink = nfs_unlink,
7151 .symlink = nfs_symlink,
7152 .mkdir = nfs_mkdir,
7153 .rmdir = nfs_rmdir,
7154 .mknod = nfs_mknod,
7155 .rename = nfs_rename,
7156 .permission = nfs_permission,
7157 .getattr = nfs_getattr,
7158 .setattr = nfs_setattr,
7159 .getxattr = generic_getxattr,
7160 .setxattr = generic_setxattr,
7161 .listxattr = generic_listxattr,
7162 .removexattr = generic_removexattr,
7163};
7164
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08007165static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007166 .permission = nfs_permission,
7167 .getattr = nfs_getattr,
7168 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007169 .getxattr = generic_getxattr,
7170 .setxattr = generic_setxattr,
7171 .listxattr = generic_listxattr,
7172 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007173};
7174
David Howells509de812006-08-22 20:06:11 -04007175const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007176 .version = 4, /* protocol version */
7177 .dentry_ops = &nfs4_dentry_operations,
7178 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007179 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04007180 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007181 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04007182 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04007183 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007184 .getattr = nfs4_proc_getattr,
7185 .setattr = nfs4_proc_setattr,
7186 .lookup = nfs4_proc_lookup,
7187 .access = nfs4_proc_access,
7188 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007189 .create = nfs4_proc_create,
7190 .remove = nfs4_proc_remove,
7191 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04007192 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007193 .unlink_done = nfs4_proc_unlink_done,
7194 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04007195 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04007196 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04007197 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007198 .link = nfs4_proc_link,
7199 .symlink = nfs4_proc_symlink,
7200 .mkdir = nfs4_proc_mkdir,
7201 .rmdir = nfs4_proc_remove,
7202 .readdir = nfs4_proc_readdir,
7203 .mknod = nfs4_proc_mknod,
7204 .statfs = nfs4_proc_statfs,
7205 .fsinfo = nfs4_proc_fsinfo,
7206 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04007207 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007208 .decode_dirent = nfs4_decode_dirent,
7209 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04007210 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04007211 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05007212 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007213 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04007214 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04007215 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007216 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007217 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04007218 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007219 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007220 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00007221 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04007222 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04007223 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04007224 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04007225 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04007226 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00007227 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04007228 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04007229 .create_server = nfs4_create_server,
7230 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007231};
7232
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007233static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
7234 .prefix = XATTR_NAME_NFSV4_ACL,
7235 .list = nfs4_xattr_list_nfs4_acl,
7236 .get = nfs4_xattr_get_nfs4_acl,
7237 .set = nfs4_xattr_set_nfs4_acl,
7238};
7239
7240const struct xattr_handler *nfs4_xattr_handlers[] = {
7241 &nfs4_xattr_nfs4_acl_handler,
7242 NULL
7243};
7244
Linus Torvalds1da177e2005-04-16 15:20:36 -07007245/*
7246 * Local variables:
7247 * c-basic-offset: 8
7248 * End:
7249 */