blob: a0c35ab12a6bfebf6d6306b94d3a54bfd67c5932 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050042#include <linux/ratelimit.h>
43#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sunrpc/clnt.h>
46#include <linux/nfs.h>
47#include <linux/nfs4.h>
48#include <linux/nfs_fs.h>
49#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040050#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070052#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040053#include <linux/module.h>
Trond Myklebust6926afd2012-01-07 13:22:46 -050054#include <linux/nfs_idmap.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000055#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050056#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010057#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Trond Myklebust4ce79712005-06-22 17:16:21 +000059#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050061#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050062#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040063#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040064#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040065#include "netns.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050066#include "nfs4session.h"
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69#define NFSDBG_FACILITY NFSDBG_PROC
70
Trond Myklebust2066fe82006-09-15 08:30:46 -040071#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define NFS4_POLL_RETRY_MAX (15*HZ)
73
Trond Myklebustcdd4e682006-01-03 09:55:12 +010074struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010075static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080076static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050078static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Chuck Lever81934dd2012-03-01 17:01:57 -050079static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Bryan Schumakerbae36242012-05-10 15:07:31 -040080static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *);
Trond Myklebust99367812007-07-17 21:52:41 -040081static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040082static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
83 struct nfs_fattr *fattr, struct iattr *sattr,
84 struct nfs4_state *state);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040085#ifdef CONFIG_NFS_V4_1
Bryan Schumaker1cab0652012-01-31 10:39:29 -050086static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *);
87static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040088#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070089/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050090static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Trond Myklebust52567b02009-10-23 14:46:42 -040092 if (err >= -1000)
93 return err;
94 switch (err) {
95 case -NFS4ERR_RESOURCE:
96 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +000097 case -NFS4ERR_WRONGSEC:
98 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -080099 case -NFS4ERR_BADOWNER:
100 case -NFS4ERR_BADNAME:
101 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400102 case -NFS4ERR_SHARE_DENIED:
103 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400104 case -NFS4ERR_MINOR_VERS_MISMATCH:
105 return -EPROTONOSUPPORT;
Weston Andros Adamson6168f622012-09-10 14:00:46 -0400106 case -NFS4ERR_ACCESS:
107 return -EACCES;
Trond Myklebust52567b02009-10-23 14:46:42 -0400108 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700110 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400111 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400113 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114}
115
116/*
117 * This is our standard bitmap for GETATTR requests.
118 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400119const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 FATTR4_WORD0_TYPE
121 | FATTR4_WORD0_CHANGE
122 | FATTR4_WORD0_SIZE
123 | FATTR4_WORD0_FSID
124 | FATTR4_WORD0_FILEID,
125 FATTR4_WORD1_MODE
126 | FATTR4_WORD1_NUMLINKS
127 | FATTR4_WORD1_OWNER
128 | FATTR4_WORD1_OWNER_GROUP
129 | FATTR4_WORD1_RAWDEV
130 | FATTR4_WORD1_SPACE_USED
131 | FATTR4_WORD1_TIME_ACCESS
132 | FATTR4_WORD1_TIME_METADATA
133 | FATTR4_WORD1_TIME_MODIFY
134};
135
Trond Myklebust1549210f2012-06-05 09:16:47 -0400136static const u32 nfs4_pnfs_open_bitmap[3] = {
137 FATTR4_WORD0_TYPE
138 | FATTR4_WORD0_CHANGE
139 | FATTR4_WORD0_SIZE
140 | FATTR4_WORD0_FSID
141 | FATTR4_WORD0_FILEID,
142 FATTR4_WORD1_MODE
143 | FATTR4_WORD1_NUMLINKS
144 | FATTR4_WORD1_OWNER
145 | FATTR4_WORD1_OWNER_GROUP
146 | FATTR4_WORD1_RAWDEV
147 | FATTR4_WORD1_SPACE_USED
148 | FATTR4_WORD1_TIME_ACCESS
149 | FATTR4_WORD1_TIME_METADATA
150 | FATTR4_WORD1_TIME_MODIFY,
151 FATTR4_WORD2_MDSTHRESHOLD
152};
153
Andy Adamsone23008e2012-10-02 21:07:32 -0400154static const u32 nfs4_open_noattr_bitmap[3] = {
155 FATTR4_WORD0_TYPE
156 | FATTR4_WORD0_CHANGE
157 | FATTR4_WORD0_FILEID,
158};
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160const u32 nfs4_statfs_bitmap[2] = {
161 FATTR4_WORD0_FILES_AVAIL
162 | FATTR4_WORD0_FILES_FREE
163 | FATTR4_WORD0_FILES_TOTAL,
164 FATTR4_WORD1_SPACE_AVAIL
165 | FATTR4_WORD1_SPACE_FREE
166 | FATTR4_WORD1_SPACE_TOTAL
167};
168
Trond Myklebust4ce79712005-06-22 17:16:21 +0000169const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 FATTR4_WORD0_MAXLINK
171 | FATTR4_WORD0_MAXNAME,
172 0
173};
174
Fred Isamandae100c2011-07-30 20:52:37 -0400175const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 | FATTR4_WORD0_MAXREAD
177 | FATTR4_WORD0_MAXWRITE
178 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700179 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400180 | FATTR4_WORD1_FS_LAYOUT_TYPES,
181 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182};
183
Manoj Naik830b8e32006-06-09 09:34:25 -0400184const u32 nfs4_fs_locations_bitmap[2] = {
185 FATTR4_WORD0_TYPE
186 | FATTR4_WORD0_CHANGE
187 | FATTR4_WORD0_SIZE
188 | FATTR4_WORD0_FSID
189 | FATTR4_WORD0_FILEID
190 | FATTR4_WORD0_FS_LOCATIONS,
191 FATTR4_WORD1_MODE
192 | FATTR4_WORD1_NUMLINKS
193 | FATTR4_WORD1_OWNER
194 | FATTR4_WORD1_OWNER_GROUP
195 | FATTR4_WORD1_RAWDEV
196 | FATTR4_WORD1_SPACE_USED
197 | FATTR4_WORD1_TIME_ACCESS
198 | FATTR4_WORD1_TIME_METADATA
199 | FATTR4_WORD1_TIME_MODIFY
200 | FATTR4_WORD1_MOUNTED_ON_FILEID
201};
202
Al Virobc4785c2006-10-19 23:28:51 -0700203static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 struct nfs4_readdir_arg *readdir)
205{
Al Viro0dbb4c62006-10-19 23:28:49 -0700206 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000209 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
211 return;
212 }
213
214 readdir->cookie = 0;
215 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
216 if (cookie == 2)
217 return;
218
219 /*
220 * NFSv4 servers do not return entries for '.' and '..'
221 * Therefore, we fake these entries here. We let '.'
222 * have cookie 0 and '..' have cookie 1. Note that
223 * when talking to the server, we always send cookie 0
224 * instead of 1 or 2.
225 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800226 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 if (cookie == 0) {
229 *p++ = xdr_one; /* next */
230 *p++ = xdr_zero; /* cookie, first word */
231 *p++ = xdr_one; /* cookie, second word */
232 *p++ = xdr_one; /* entry len */
233 memcpy(p, ".\0\0\0", 4); /* entry */
234 p++;
235 *p++ = xdr_one; /* bitmap length */
236 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
237 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400238 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 }
240
241 *p++ = xdr_one; /* next */
242 *p++ = xdr_zero; /* cookie, first word */
243 *p++ = xdr_two; /* cookie, second word */
244 *p++ = xdr_two; /* entry len */
245 memcpy(p, "..\0\0", 4); /* entry */
246 p++;
247 *p++ = xdr_one; /* bitmap length */
248 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
249 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400250 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 readdir->pgbase = (char *)p - (char *)start;
253 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800254 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255}
256
Trond Myklebust65de8722008-12-23 15:21:44 -0500257static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
258{
259 int res = 0;
260
261 might_sleep();
262
263 if (*timeout <= 0)
264 *timeout = NFS4_POLL_RETRY_MIN;
265 if (*timeout > NFS4_POLL_RETRY_MAX)
266 *timeout = NFS4_POLL_RETRY_MAX;
Jeff Laytond3103102011-12-01 22:44:39 +0100267 freezable_schedule_timeout_killable(*timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500268 if (fatal_signal_pending(current))
269 res = -ERESTARTSYS;
270 *timeout <<= 1;
271 return res;
272}
273
274/* This is the error handling routine for processes that are allowed
275 * to sleep.
276 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800277static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500278{
279 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500280 struct nfs4_state *state = exception->state;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500281 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500282 int ret = errorcode;
283
284 exception->retry = 0;
285 switch(errorcode) {
286 case 0:
287 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500288 case -NFS4ERR_OPENMODE:
Bryan Schumaker011e2a72012-06-20 15:53:43 -0400289 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400290 nfs4_inode_return_delegation(inode);
Trond Myklebust3114ea72012-03-07 16:39:06 -0500291 exception->retry = 1;
292 return 0;
293 }
294 if (state == NULL)
295 break;
296 nfs4_schedule_stateid_recovery(server, state);
297 goto wait_on_recovery;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500298 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500299 case -NFS4ERR_ADMIN_REVOKED:
300 case -NFS4ERR_BAD_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500301 if (state == NULL)
302 break;
Trond Myklebust14977482012-03-27 18:31:25 -0400303 nfs_remove_bad_delegation(state->inode);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500304 nfs4_schedule_stateid_recovery(server, state);
305 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400306 case -NFS4ERR_EXPIRED:
307 if (state != NULL)
308 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust65de8722008-12-23 15:21:44 -0500309 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500310 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500311 nfs4_schedule_lease_recovery(clp);
312 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500313#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400314 case -NFS4ERR_BADSESSION:
315 case -NFS4ERR_BADSLOT:
316 case -NFS4ERR_BAD_HIGH_SLOT:
317 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
318 case -NFS4ERR_DEADSESSION:
319 case -NFS4ERR_SEQ_FALSE_RETRY:
320 case -NFS4ERR_SEQ_MISORDERED:
321 dprintk("%s ERROR: %d Reset session\n", __func__,
322 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400323 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400324 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500325#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500326 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500327 if (exception->timeout > HZ) {
328 /* We have retried a decent amount, time to
329 * fail
330 */
331 ret = -EBUSY;
332 break;
333 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500334 case -NFS4ERR_GRACE:
335 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -0500336 case -EKEYEXPIRED:
Trond Myklebust65de8722008-12-23 15:21:44 -0500337 ret = nfs4_delay(server->client, &exception->timeout);
338 if (ret != 0)
339 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400340 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500341 case -NFS4ERR_OLD_STATEID:
342 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800343 break;
344 case -NFS4ERR_BADOWNER:
345 /* The following works around a Linux server bug! */
346 case -NFS4ERR_BADNAME:
347 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
348 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
349 exception->retry = 1;
350 printk(KERN_WARNING "NFS: v4 server %s "
351 "does not accept raw "
352 "uid/gids. "
353 "Reenabling the idmapper.\n",
354 server->nfs_client->cl_hostname);
355 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500356 }
357 /* We failed to handle the error */
358 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500359wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500360 ret = nfs4_wait_clnt_recover(clp);
361 if (ret == 0)
362 exception->retry = 1;
363 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500364}
365
366
Trond Myklebust452e9352010-07-31 14:29:06 -0400367static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 spin_lock(&clp->cl_lock);
370 if (time_before(clp->cl_last_renewal,timestamp))
371 clp->cl_last_renewal = timestamp;
372 spin_unlock(&clp->cl_lock);
373}
374
Trond Myklebust452e9352010-07-31 14:29:06 -0400375static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
376{
377 do_renew_lease(server->nfs_client, timestamp);
378}
379
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400380#if defined(CONFIG_NFS_V4_1)
381
Trond Myklebust961a8282012-01-17 22:57:37 -0500382bool nfs4_set_task_privileged(struct rpc_task *task, void *dummy)
383{
384 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
385 return true;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400386}
387
Trond Myklebustd185a332010-06-16 09:52:25 -0400388static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400389{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500390 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400391 struct nfs4_slot_table *tbl;
392
Benny Halevydfb4f3092010-09-24 09:17:01 -0400393 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400394 /* just wake up the next guy waiting since
395 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500396 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500397 return;
Andy Adamson13615872009-04-01 09:22:17 -0400398 }
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500399 tbl = res->sr_slot->table;
400 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500401
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500402 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustf4af6e2a2012-11-20 14:17:32 -0500403 nfs4_free_slot(tbl, res->sr_slot);
Trond Myklebust5d633602012-11-23 13:09:38 -0500404 if (!nfs4_session_draining(session))
405 rpc_wake_up_first(&tbl->slot_tbl_waitq,
406 nfs4_set_task_privileged, NULL);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500407 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400408 res->sr_slot = NULL;
Andy Adamson13615872009-04-01 09:22:17 -0400409}
410
Trond Myklebust14516c32010-07-31 14:29:06 -0400411static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400412{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500413 struct nfs4_session *session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500414 struct nfs4_slot *slot;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400415 unsigned long timestamp;
Trond Myklebust14516c32010-07-31 14:29:06 -0400416 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400417
418 /*
419 * sr_status remains 1 if an RPC level error occurred. The server
420 * may or may not have processed the sequence operation..
421 * Proceed as if the server received and processed the sequence
422 * operation.
423 */
424 if (res->sr_status == 1)
425 res->sr_status = NFS_OK;
426
Bryan Schumaker468f8612011-04-18 15:57:32 -0400427 /* don't increment the sequence number if the task wasn't sent */
428 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400429 goto out;
430
Trond Myklebust933602e2012-11-16 12:12:38 -0500431 slot = res->sr_slot;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500432 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500433
Andy Adamson691daf32009-12-04 15:55:39 -0500434 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400435 switch (res->sr_status) {
436 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400437 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust933602e2012-11-16 12:12:38 -0500438 ++slot->seq_nr;
439 timestamp = slot->renewal_time;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500440 clp = session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400441 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500442 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500443 if (res->sr_status_flags != 0)
444 nfs4_schedule_lease_recovery(clp);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500445 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400446 break;
447 case -NFS4ERR_DELAY:
448 /* The server detected a resend of the RPC call and
449 * returned NFS4ERR_DELAY as per Section 2.10.6.2
450 * of RFC5661.
451 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500452 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400453 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500454 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500455 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400456 goto out_retry;
457 default:
458 /* Just update the slot sequence no. */
Trond Myklebust933602e2012-11-16 12:12:38 -0500459 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400460 }
461out:
462 /* The session may be reset by one of the error handlers. */
463 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400464 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400465 return 1;
466out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400467 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400468 goto out;
469 rpc_delay(task, NFS4_POLL_RETRY_MAX);
470 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400471}
472
Trond Myklebust14516c32010-07-31 14:29:06 -0400473static int nfs4_sequence_done(struct rpc_task *task,
474 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400475{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500476 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400477 return 1;
478 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400479}
480
Trond Myklebust9d12b212012-01-17 22:04:25 -0500481static void nfs41_init_sequence(struct nfs4_sequence_args *args,
482 struct nfs4_sequence_res *res, int cache_reply)
483{
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500484 args->sa_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500485 args->sa_cache_this = 0;
486 if (cache_reply)
487 args->sa_cache_this = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500488 res->sr_slot = NULL;
489}
490
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000491int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400492 struct nfs4_sequence_args *args,
493 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400494 struct rpc_task *task)
495{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400496 struct nfs4_slot *slot;
497 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400498
499 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400500 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400501 if (res->sr_slot != NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400502 return 0;
503
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400504 tbl = &session->fc_slot_table;
505
Trond Myklebust69d206b2012-11-22 13:21:02 -0500506 task->tk_timeout = 0;
507
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400508 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400509 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800510 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500511 /* The state manager will wait until the slot table is empty */
Andy Adamson05f0d232009-12-04 15:55:37 -0500512 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400513 spin_unlock(&tbl->slot_tbl_lock);
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500514 dprintk("%s session is draining\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500515 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400516 }
517
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800518 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
519 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
520 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
521 spin_unlock(&tbl->slot_tbl_lock);
522 dprintk("%s enforce FIFO order\n", __func__);
523 return -EAGAIN;
524 }
525
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500526 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500527 if (IS_ERR(slot)) {
528 /* If out of memory, try again in 1/4 second */
529 if (slot == ERR_PTR(-ENOMEM))
530 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400531 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
532 spin_unlock(&tbl->slot_tbl_lock);
533 dprintk("<-- %s: no free slots\n", __func__);
534 return -EAGAIN;
535 }
536 spin_unlock(&tbl->slot_tbl_lock);
537
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800538 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500539
540 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400541
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500542 dprintk("<-- %s slotid=%d seqid=%d\n", __func__,
543 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400544
Benny Halevydfb4f3092010-09-24 09:17:01 -0400545 res->sr_slot = slot;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400546 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400547 /*
548 * sr_status is only set in decode_sequence, and so will remain
549 * set to 1 if an rpc level failure occurs.
550 */
551 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400552 return 0;
553}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000554EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400555
Trond Myklebust035168a2010-06-16 09:52:26 -0400556int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400557 struct nfs4_sequence_args *args,
558 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400559 struct rpc_task *task)
560{
Trond Myklebust035168a2010-06-16 09:52:26 -0400561 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400562 int ret = 0;
563
Trond Myklebust9d12b212012-01-17 22:04:25 -0500564 if (session == NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400565 goto out;
Trond Myklebust035168a2010-06-16 09:52:26 -0400566
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500567 dprintk("--> %s clp %p session %p sr_slot %d\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400568 __func__, session->clp, session, res->sr_slot ?
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500569 res->sr_slot->slot_nr : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400570
Trond Myklebust9d12b212012-01-17 22:04:25 -0500571 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400572out:
573 dprintk("<-- %s status=%d\n", __func__, ret);
574 return ret;
575}
576
577struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400578 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400579 struct nfs4_sequence_args *seq_args;
580 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400581};
582
583static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
584{
585 struct nfs41_call_sync_data *data = calldata;
586
Trond Myklebust035168a2010-06-16 09:52:26 -0400587 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
588
589 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -0500590 data->seq_res, task))
Andy Adamsonce5039c2009-04-01 09:22:13 -0400591 return;
592 rpc_call_start(task);
593}
594
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800595static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
596{
597 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
598 nfs41_call_sync_prepare(task, calldata);
599}
600
Andy Adamson69ab40c2009-04-01 09:22:19 -0400601static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
602{
603 struct nfs41_call_sync_data *data = calldata;
604
Trond Myklebust14516c32010-07-31 14:29:06 -0400605 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400606}
607
Trond Myklebust17280172012-03-11 13:11:00 -0400608static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400609 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400610 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400611};
612
Trond Myklebust17280172012-03-11 13:11:00 -0400613static const struct rpc_call_ops nfs41_call_priv_sync_ops = {
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800614 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
615 .rpc_call_done = nfs41_call_sync_done,
616};
617
Bryan Schumaker7c513052011-03-24 17:12:24 +0000618static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
619 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400620 struct rpc_message *msg,
621 struct nfs4_sequence_args *args,
622 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800623 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400624{
625 int ret;
626 struct rpc_task *task;
627 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400628 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400629 .seq_args = args,
630 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400631 };
632 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000633 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400634 .rpc_message = msg,
635 .callback_ops = &nfs41_call_sync_ops,
636 .callback_data = &data
637 };
638
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800639 if (privileged)
640 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400641 task = rpc_run_task(&task_setup);
642 if (IS_ERR(task))
643 ret = PTR_ERR(task);
644 else {
645 ret = task->tk_status;
646 rpc_put_task(task);
647 }
648 return ret;
649}
650
Bryan Schumaker7c513052011-03-24 17:12:24 +0000651int _nfs4_call_sync_session(struct rpc_clnt *clnt,
652 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400653 struct rpc_message *msg,
654 struct nfs4_sequence_args *args,
655 struct nfs4_sequence_res *res,
656 int cache_reply)
657{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500658 nfs41_init_sequence(args, res, cache_reply);
659 return nfs4_call_sync_sequence(clnt, server, msg, args, res, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400660}
661
Trond Myklebustdf896452010-06-16 09:52:26 -0400662#else
Trond Myklebust9d12b212012-01-17 22:04:25 -0500663static inline
664void nfs41_init_sequence(struct nfs4_sequence_args *args,
665 struct nfs4_sequence_res *res, int cache_reply)
666{
667}
668
Trond Myklebust14516c32010-07-31 14:29:06 -0400669static int nfs4_sequence_done(struct rpc_task *task,
670 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400671{
Trond Myklebust14516c32010-07-31 14:29:06 -0400672 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400673}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400674#endif /* CONFIG_NFS_V4_1 */
675
Bryan Schumaker7c513052011-03-24 17:12:24 +0000676int _nfs4_call_sync(struct rpc_clnt *clnt,
677 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400678 struct rpc_message *msg,
679 struct nfs4_sequence_args *args,
680 struct nfs4_sequence_res *res,
681 int cache_reply)
682{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500683 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000684 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400685}
686
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000687static inline
Bryan Schumaker7c513052011-03-24 17:12:24 +0000688int nfs4_call_sync(struct rpc_clnt *clnt,
689 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000690 struct rpc_message *msg,
691 struct nfs4_sequence_args *args,
692 struct nfs4_sequence_res *res,
693 int cache_reply)
694{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000695 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
696 args, res, cache_reply);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000697}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400698
Trond Myklebust38478b22006-05-25 01:40:57 -0400699static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
Trond Myklebust38478b22006-05-25 01:40:57 -0400701 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Trond Myklebust38478b22006-05-25 01:40:57 -0400703 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400704 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700705 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400706 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700707 dir->i_version = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400708 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709}
710
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100711struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400712 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100713 struct nfs_openargs o_arg;
714 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100715 struct nfs_open_confirmargs c_arg;
716 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500717 struct nfs4_string owner_name;
718 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100719 struct nfs_fattr f_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100720 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400721 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100722 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400723 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100724 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100725 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400726 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100727 int rpc_status;
728 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100729};
730
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400731
732static void nfs4_init_opendata_res(struct nfs4_opendata *p)
733{
734 p->o_res.f_attr = &p->f_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400735 p->o_res.seqid = p->o_arg.seqid;
736 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400737 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400738 p->o_res.access_request = p->o_arg.access;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400739 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500740 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400741}
742
Al Viro82a2c1b2011-06-22 18:30:55 -0400743static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500744 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400745 const struct iattr *attrs,
746 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100747{
Al Viro82a2c1b2011-06-22 18:30:55 -0400748 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100749 struct inode *dir = parent->d_inode;
750 struct nfs_server *server = NFS_SERVER(dir);
751 struct nfs4_opendata *p;
752
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400753 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100754 if (p == NULL)
755 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400756 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100757 if (p->o_arg.seqid == NULL)
758 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400759 nfs_sb_active(dentry->d_sb);
760 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100761 p->dir = parent;
762 p->owner = sp;
763 atomic_inc(&sp->so_count);
764 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500765 p->o_arg.open_flags = flags;
766 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700767 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
768 * will return permission denied for all bits until close */
769 if (!(flags & O_EXCL)) {
770 /* ask server to check for all possible rights as results
771 * are cached */
772 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
773 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700774 }
David Howells7539bba2006-08-22 20:06:09 -0400775 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400776 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
777 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400778 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100779 p->o_arg.server = server;
780 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust1549210f2012-06-05 09:16:47 -0400781 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100782 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust536e43d2012-01-17 22:04:26 -0500783 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -0500784 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400785
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100786 p->o_arg.u.attrs = &p->attrs;
787 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500788
789 verf[0] = jiffies;
790 verf[1] = current->pid;
791 memcpy(p->o_arg.u.verifier.data, verf,
792 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100793 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100794 p->c_arg.fh = &p->o_res.fh;
795 p->c_arg.stateid = &p->o_res.stateid;
796 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400797 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400798 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100799 return p;
800err_free:
801 kfree(p);
802err:
803 dput(parent);
804 return NULL;
805}
806
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400807static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100808{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400809 struct nfs4_opendata *p = container_of(kref,
810 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400811 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400812
813 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400814 if (p->state != NULL)
815 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400816 nfs4_put_state_owner(p->owner);
817 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400818 dput(p->dentry);
819 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500820 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400821 kfree(p);
822}
823
824static void nfs4_opendata_put(struct nfs4_opendata *p)
825{
826 if (p != NULL)
827 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100828}
829
Trond Myklebust06f814a2006-01-03 09:55:07 +0100830static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
831{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100832 int ret;
833
Trond Myklebust06f814a2006-01-03 09:55:07 +0100834 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100835 return ret;
836}
837
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500838static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400839{
840 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500841
Trond Myklebust536e43d2012-01-17 22:04:26 -0500842 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500843 goto out;
844 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400845 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500846 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
847 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400848 break;
849 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500850 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
851 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400852 break;
853 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500854 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
855 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400856 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500857out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400858 return ret;
859}
860
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500861static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400862{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500863 if (delegation == NULL)
864 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500865 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400866 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500867 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400868 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500869 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400870 return 1;
871}
872
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500873static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100874{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500875 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100876 case FMODE_WRITE:
877 state->n_wronly++;
878 break;
879 case FMODE_READ:
880 state->n_rdonly++;
881 break;
882 case FMODE_READ|FMODE_WRITE:
883 state->n_rdwr++;
884 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500885 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100886}
887
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500888static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400889{
890 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -0500891 nfs4_stateid_copy(&state->stateid, stateid);
892 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500893 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400894 case FMODE_READ:
895 set_bit(NFS_O_RDONLY_STATE, &state->flags);
896 break;
897 case FMODE_WRITE:
898 set_bit(NFS_O_WRONLY_STATE, &state->flags);
899 break;
900 case FMODE_READ|FMODE_WRITE:
901 set_bit(NFS_O_RDWR_STATE, &state->flags);
902 }
903}
904
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500905static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400906{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400907 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500908 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400909 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400910}
911
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500912static 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 -0700913{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400914 /*
915 * Protect the call to nfs4_state_set_mode_locked and
916 * serialise the stateid update
917 */
918 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400919 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -0500920 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -0400921 set_bit(NFS_DELEGATED_STATE, &state->flags);
922 }
923 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500924 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400925 write_sequnlock(&state->seqlock);
926 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500927 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700928 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929}
930
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500931static 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 -0500932{
933 struct nfs_inode *nfsi = NFS_I(state->inode);
934 struct nfs_delegation *deleg_cur;
935 int ret = 0;
936
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500937 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500938
939 rcu_read_lock();
940 deleg_cur = rcu_dereference(nfsi->delegation);
941 if (deleg_cur == NULL)
942 goto no_delegation;
943
944 spin_lock(&deleg_cur->lock);
945 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500946 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500947 goto no_delegation_unlock;
948
949 if (delegation == NULL)
950 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -0500951 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -0500952 goto no_delegation_unlock;
953
Trond Myklebustb7391f42008-12-23 15:21:52 -0500954 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500955 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500956 ret = 1;
957no_delegation_unlock:
958 spin_unlock(&deleg_cur->lock);
959no_delegation:
960 rcu_read_unlock();
961
962 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500963 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500964 ret = 1;
965 }
966
967 return ret;
968}
969
970
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500971static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400972{
973 struct nfs_delegation *delegation;
974
975 rcu_read_lock();
976 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500977 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -0400978 rcu_read_unlock();
979 return;
980 }
981 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400982 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400983}
984
Trond Myklebust6ee41262007-07-08 14:11:36 -0400985static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400986{
987 struct nfs4_state *state = opendata->state;
988 struct nfs_inode *nfsi = NFS_I(state->inode);
989 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -0500990 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500991 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400992 nfs4_stateid stateid;
993 int ret = -EAGAIN;
994
Trond Myklebustaac00a82007-07-05 19:02:21 -0400995 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500996 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400997 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500998 if (can_open_cached(state, fmode, open_mode)) {
999 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001000 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001001 goto out_return_state;
1002 }
1003 spin_unlock(&state->owner->so_lock);
1004 }
Trond Myklebust34310432008-12-23 15:21:38 -05001005 rcu_read_lock();
1006 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001007 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001008 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001009 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001010 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001011 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001012 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001013 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001014 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001015 if (ret != 0)
1016 goto out;
1017 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001018
1019 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001020 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001021 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001022 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001023out:
1024 return ERR_PTR(ret);
1025out_return_state:
1026 atomic_inc(&state->count);
1027 return state;
1028}
1029
Andy Adamsone23008e2012-10-02 21:07:32 -04001030static void
1031nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1032{
1033 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1034 struct nfs_delegation *delegation;
1035 int delegation_flags = 0;
1036
1037 rcu_read_lock();
1038 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1039 if (delegation)
1040 delegation_flags = delegation->flags;
1041 rcu_read_unlock();
1042 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1043 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1044 "returning a delegation for "
1045 "OPEN(CLAIM_DELEGATE_CUR)\n",
1046 clp->cl_hostname);
1047 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1048 nfs_inode_set_delegation(state->inode,
1049 data->owner->so_cred,
1050 &data->o_res);
1051 else
1052 nfs_inode_reclaim_delegation(state->inode,
1053 data->owner->so_cred,
1054 &data->o_res);
1055}
1056
1057/*
1058 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1059 * and update the nfs4_state.
1060 */
1061static struct nfs4_state *
1062_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1063{
1064 struct inode *inode = data->state->inode;
1065 struct nfs4_state *state = data->state;
1066 int ret;
1067
1068 if (!data->rpc_done) {
1069 ret = data->rpc_status;
1070 goto err;
1071 }
1072
1073 ret = -ESTALE;
1074 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1075 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1076 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1077 goto err;
1078
1079 ret = -ENOMEM;
1080 state = nfs4_get_open_state(inode, data->owner);
1081 if (state == NULL)
1082 goto err;
1083
1084 ret = nfs_refresh_inode(inode, &data->f_attr);
1085 if (ret)
1086 goto err;
1087
1088 if (data->o_res.delegation_type != 0)
1089 nfs4_opendata_check_deleg(data, state);
1090 update_open_stateid(state, &data->o_res.stateid, NULL,
1091 data->o_arg.fmode);
1092
1093 return state;
1094err:
1095 return ERR_PTR(ret);
1096
1097}
1098
1099static struct nfs4_state *
1100_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001101{
1102 struct inode *inode;
1103 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001104 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001105
Trond Myklebustaac00a82007-07-05 19:02:21 -04001106 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001107 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001108 goto out;
1109 }
1110
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001111 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001112 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001113 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001114 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001115 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001116 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001117 goto err;
1118 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001119 state = nfs4_get_open_state(inode, data->owner);
1120 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001121 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001122 if (data->o_res.delegation_type != 0)
1123 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001124 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001125 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001126 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001127out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001128 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001129err_put_inode:
1130 iput(inode);
1131err:
1132 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001133}
1134
Andy Adamsone23008e2012-10-02 21:07:32 -04001135static struct nfs4_state *
1136nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1137{
1138 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1139 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1140 return _nfs4_opendata_to_nfs4_state(data);
1141}
1142
Trond Myklebust864472e2006-01-03 09:55:15 +01001143static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1144{
1145 struct nfs_inode *nfsi = NFS_I(state->inode);
1146 struct nfs_open_context *ctx;
1147
1148 spin_lock(&state->inode->i_lock);
1149 list_for_each_entry(ctx, &nfsi->open_files, list) {
1150 if (ctx->state != state)
1151 continue;
1152 get_nfs_open_context(ctx);
1153 spin_unlock(&state->inode->i_lock);
1154 return ctx;
1155 }
1156 spin_unlock(&state->inode->i_lock);
1157 return ERR_PTR(-ENOENT);
1158}
1159
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001160static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1161{
1162 struct nfs4_opendata *opendata;
1163
Al Viro3d4ff432011-06-22 18:40:12 -04001164 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001165 if (opendata == NULL)
1166 return ERR_PTR(-ENOMEM);
1167 opendata->state = state;
1168 atomic_inc(&state->count);
1169 return opendata;
1170}
1171
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001172static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001173{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001174 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001175 int ret;
1176
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001177 opendata->o_arg.open_flags = 0;
1178 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001179 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1180 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1181 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001182 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001183 if (ret != 0)
1184 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001185 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001186 if (IS_ERR(newstate))
1187 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001188 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001189 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001190 return 0;
1191}
1192
1193static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1194{
Trond Myklebust864472e2006-01-03 09:55:15 +01001195 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001196 int ret;
1197
1198 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001199 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001200 smp_rmb();
1201 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001202 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001203 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001204 if (ret != 0)
1205 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001206 if (newstate != state)
1207 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001208 }
1209 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001210 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001211 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001212 if (ret != 0)
1213 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001214 if (newstate != state)
1215 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001216 }
1217 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001218 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001219 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001220 if (ret != 0)
1221 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001222 if (newstate != state)
1223 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001224 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001225 /*
1226 * We may have performed cached opens for all three recoveries.
1227 * Check if we need to update the current stateid.
1228 */
1229 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001230 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001231 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001232 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001233 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001234 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001235 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001236 return 0;
1237}
1238
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239/*
1240 * OPEN_RECLAIM:
1241 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001243static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001245 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001246 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001247 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 int status;
1249
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001250 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1251 if (IS_ERR(opendata))
1252 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001253 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1254 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001255 rcu_read_lock();
1256 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001257 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001258 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001259 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001260 opendata->o_arg.u.delegation_type = delegation_type;
1261 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001262 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 return status;
1264}
1265
Trond Myklebust539cd032007-06-05 11:46:42 -04001266static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267{
1268 struct nfs_server *server = NFS_SERVER(state->inode);
1269 struct nfs4_exception exception = { };
1270 int err;
1271 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001272 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001273 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001274 break;
1275 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 } while (exception.retry);
1277 return err;
1278}
1279
Trond Myklebust864472e2006-01-03 09:55:15 +01001280static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1281{
1282 struct nfs_open_context *ctx;
1283 int ret;
1284
1285 ctx = nfs4_state_find_open_context(state);
1286 if (IS_ERR(ctx))
1287 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001288 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001289 put_nfs_open_context(ctx);
1290 return ret;
1291}
1292
Trond Myklebust13437e12007-07-06 15:10:43 -04001293static 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 -07001294{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001295 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001296 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001298 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1299 if (IS_ERR(opendata))
1300 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001301 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebustf597c532012-03-04 18:13:56 -05001302 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust864472e2006-01-03 09:55:15 +01001303 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001304 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001305 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306}
1307
Trond Myklebust13437e12007-07-06 15:10:43 -04001308int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309{
1310 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001311 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 int err;
1313 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001314 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 switch (err) {
1316 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001317 case -ENOENT:
1318 case -ESTALE:
1319 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001320 case -NFS4ERR_BADSESSION:
1321 case -NFS4ERR_BADSLOT:
1322 case -NFS4ERR_BAD_HIGH_SLOT:
1323 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1324 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04001325 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001326 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 case -NFS4ERR_STALE_CLIENTID:
1328 case -NFS4ERR_STALE_STATEID:
1329 case -NFS4ERR_EXPIRED:
1330 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001331 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001332 goto out;
1333 case -ERESTARTSYS:
1334 /*
1335 * The show must go on: exit, but mark the
1336 * stateid as needing recovery.
1337 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001338 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001339 case -NFS4ERR_ADMIN_REVOKED:
1340 case -NFS4ERR_BAD_STATEID:
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001341 nfs_inode_find_state_and_recover(state->inode,
1342 stateid);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001343 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001344 case -EKEYEXPIRED:
1345 /*
1346 * User RPCSEC_GSS context has expired.
1347 * We cannot recover this stateid now, so
1348 * skip it and allow recovery thread to
1349 * proceed.
1350 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001351 case -ENOMEM:
1352 err = 0;
1353 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 }
1355 err = nfs4_handle_exception(server, err, &exception);
1356 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001357out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 return err;
1359}
1360
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001361static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1362{
1363 struct nfs4_opendata *data = calldata;
1364
1365 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001366 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001367 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001368 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001369 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001370 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001371 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001372}
1373
1374static void nfs4_open_confirm_release(void *calldata)
1375{
1376 struct nfs4_opendata *data = calldata;
1377 struct nfs4_state *state = NULL;
1378
1379 /* If this request hasn't been cancelled, do nothing */
1380 if (data->cancelled == 0)
1381 goto out_free;
1382 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001383 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001384 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001385 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001386 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001387 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001388out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001389 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001390}
1391
1392static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001393 .rpc_call_done = nfs4_open_confirm_done,
1394 .rpc_release = nfs4_open_confirm_release,
1395};
1396
1397/*
1398 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1399 */
1400static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1401{
1402 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1403 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001404 struct rpc_message msg = {
1405 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1406 .rpc_argp = &data->c_arg,
1407 .rpc_resp = &data->c_res,
1408 .rpc_cred = data->owner->so_cred,
1409 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001410 struct rpc_task_setup task_setup_data = {
1411 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001412 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001413 .callback_ops = &nfs4_open_confirm_ops,
1414 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001415 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001416 .flags = RPC_TASK_ASYNC,
1417 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 int status;
1419
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001420 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001421 data->rpc_done = 0;
1422 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001423 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001424 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001425 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001426 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001427 status = nfs4_wait_for_completion_rpc_task(task);
1428 if (status != 0) {
1429 data->cancelled = 1;
1430 smp_wmb();
1431 } else
1432 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001433 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 return status;
1435}
1436
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001437static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001439 struct nfs4_opendata *data = calldata;
1440 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001441
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001442 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1443 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001444 /*
1445 * Check if we still need to send an OPEN call, or if we can use
1446 * a delegation instead.
1447 */
1448 if (data->state != NULL) {
1449 struct nfs_delegation *delegation;
1450
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001451 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001452 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001453 rcu_read_lock();
1454 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001455 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1456 can_open_delegated(delegation, data->o_arg.fmode))
1457 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001458 rcu_read_unlock();
1459 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001460 /* Update client id. */
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001461 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001462 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001463 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Andy Adamsone23008e2012-10-02 21:07:32 -04001464 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001465 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1466 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001467 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001468 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001469 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001470 &data->o_res.seq_res,
1471 task) != 0)
1472 nfs_release_seqid(data->o_arg.seqid);
1473 else
1474 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001475 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001476unlock_no_action:
1477 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001478out_no_action:
1479 task->tk_action = NULL;
1480
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001481}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001483static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1484{
1485 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1486 nfs4_open_prepare(task, calldata);
1487}
1488
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001489static void nfs4_open_done(struct rpc_task *task, void *calldata)
1490{
1491 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001493 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001494
Trond Myklebust14516c32010-07-31 14:29:06 -04001495 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1496 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001497
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001498 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001499 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1500 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001501 case S_IFREG:
1502 break;
1503 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001504 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001505 break;
1506 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001507 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001508 break;
1509 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001510 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001511 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001512 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001513 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001514 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1515 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001516 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001517 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001518}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001519
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001520static void nfs4_open_release(void *calldata)
1521{
1522 struct nfs4_opendata *data = calldata;
1523 struct nfs4_state *state = NULL;
1524
1525 /* If this request hasn't been cancelled, do nothing */
1526 if (data->cancelled == 0)
1527 goto out_free;
1528 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001529 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001530 goto out_free;
1531 /* In case we need an open_confirm, no cleanup! */
1532 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1533 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001534 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001535 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001536 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001537out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001538 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001539}
1540
1541static const struct rpc_call_ops nfs4_open_ops = {
1542 .rpc_call_prepare = nfs4_open_prepare,
1543 .rpc_call_done = nfs4_open_done,
1544 .rpc_release = nfs4_open_release,
1545};
1546
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001547static const struct rpc_call_ops nfs4_recover_open_ops = {
1548 .rpc_call_prepare = nfs4_recover_open_prepare,
1549 .rpc_call_done = nfs4_open_done,
1550 .rpc_release = nfs4_open_release,
1551};
1552
1553static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001554{
1555 struct inode *dir = data->dir->d_inode;
1556 struct nfs_server *server = NFS_SERVER(dir);
1557 struct nfs_openargs *o_arg = &data->o_arg;
1558 struct nfs_openres *o_res = &data->o_res;
1559 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001560 struct rpc_message msg = {
1561 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1562 .rpc_argp = o_arg,
1563 .rpc_resp = o_res,
1564 .rpc_cred = data->owner->so_cred,
1565 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001566 struct rpc_task_setup task_setup_data = {
1567 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001568 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001569 .callback_ops = &nfs4_open_ops,
1570 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001571 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001572 .flags = RPC_TASK_ASYNC,
1573 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001574 int status;
1575
Trond Myklebust9d12b212012-01-17 22:04:25 -05001576 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001577 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001578 data->rpc_done = 0;
1579 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001580 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001581 if (isrecover)
1582 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001583 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001584 if (IS_ERR(task))
1585 return PTR_ERR(task);
1586 status = nfs4_wait_for_completion_rpc_task(task);
1587 if (status != 0) {
1588 data->cancelled = 1;
1589 smp_wmb();
1590 } else
1591 status = data->rpc_status;
1592 rpc_put_task(task);
1593
1594 return status;
1595}
1596
1597static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1598{
1599 struct inode *dir = data->dir->d_inode;
1600 struct nfs_openres *o_res = &data->o_res;
1601 int status;
1602
1603 status = nfs4_run_open_task(data, 1);
1604 if (status != 0 || !data->rpc_done)
1605 return status;
1606
Trond Myklebust6926afd2012-01-07 13:22:46 -05001607 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1608
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001609 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1610 status = _nfs4_proc_open_confirm(data);
1611 if (status != 0)
1612 return status;
1613 }
1614
1615 return status;
1616}
1617
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001618static int nfs4_opendata_access(struct rpc_cred *cred,
1619 struct nfs4_opendata *opendata,
1620 struct nfs4_state *state, fmode_t fmode)
1621{
1622 struct nfs_access_entry cache;
1623 u32 mask;
1624
1625 /* access call failed or for some reason the server doesn't
1626 * support any access modes -- defer access call until later */
1627 if (opendata->o_res.access_supported == 0)
1628 return 0;
1629
1630 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001631 /* don't check MAY_WRITE - a newly created file may not have
1632 * write mode bits, but POSIX allows the creating process to write */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001633 if (fmode & FMODE_READ)
1634 mask |= MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001635 if (fmode & FMODE_EXEC)
1636 mask |= MAY_EXEC;
1637
1638 cache.cred = cred;
1639 cache.jiffies = jiffies;
1640 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1641 nfs_access_add_cache(state->inode, &cache);
1642
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001643 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001644 return 0;
1645
1646 /* even though OPEN succeeded, access is denied. Close the file */
1647 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001648 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001649}
1650
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001651/*
1652 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1653 */
1654static int _nfs4_proc_open(struct nfs4_opendata *data)
1655{
1656 struct inode *dir = data->dir->d_inode;
1657 struct nfs_server *server = NFS_SERVER(dir);
1658 struct nfs_openargs *o_arg = &data->o_arg;
1659 struct nfs_openres *o_res = &data->o_res;
1660 int status;
1661
1662 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001663 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001664 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001665 if (status != 0) {
1666 if (status == -NFS4ERR_BADNAME &&
1667 !(o_arg->open_flags & O_CREAT))
1668 return -ENOENT;
1669 return status;
1670 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001671
Trond Myklebust6926afd2012-01-07 13:22:46 -05001672 nfs_fattr_map_and_free_names(server, &data->f_attr);
1673
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001674 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001675 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001676 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1677 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001679 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001681 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 }
1683 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001684 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001685 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686}
1687
Andy Adamsond83217c2011-03-01 01:34:17 +00001688static int nfs4_recover_expired_lease(struct nfs_server *server)
1689{
1690 return nfs4_client_recover_expired_lease(server->nfs_client);
1691}
1692
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693/*
1694 * OPEN_EXPIRED:
1695 * reclaim state on the server after a network partition.
1696 * Assumes caller holds the appropriate lock
1697 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001698static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001700 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001701 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001703 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1704 if (IS_ERR(opendata))
1705 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001706 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001707 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001708 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001709 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001710 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711}
1712
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001713static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001714{
Trond Myklebust539cd032007-06-05 11:46:42 -04001715 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001716 struct nfs4_exception exception = { };
1717 int err;
1718
1719 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001720 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001721 switch (err) {
1722 default:
1723 goto out;
1724 case -NFS4ERR_GRACE:
1725 case -NFS4ERR_DELAY:
1726 nfs4_handle_exception(server, err, &exception);
1727 err = 0;
1728 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001729 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001730out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001731 return err;
1732}
1733
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1735{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001737 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
Trond Myklebust864472e2006-01-03 09:55:15 +01001739 ctx = nfs4_state_find_open_context(state);
1740 if (IS_ERR(ctx))
1741 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001742 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001743 put_nfs_open_context(ctx);
1744 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745}
1746
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001747#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001748static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001749{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001750 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001751 nfs4_stateid *stateid = &state->stateid;
1752 int status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001753
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001754 /* If a state reset has been done, test_stateid is unneeded */
1755 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1756 return;
1757
1758 status = nfs41_test_stateid(server, stateid);
1759 if (status != NFS_OK) {
1760 /* Free the stateid unless the server explicitly
1761 * informs us the stateid is unrecognized. */
1762 if (status != -NFS4ERR_BAD_STATEID)
1763 nfs41_free_stateid(server, stateid);
Bryan Schumaker69388672012-09-26 15:25:52 -04001764 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001765
Bryan Schumaker69388672012-09-26 15:25:52 -04001766 write_seqlock(&state->seqlock);
1767 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1768 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001769 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1770 }
1771}
1772
1773/**
1774 * nfs41_check_open_stateid - possibly free an open stateid
1775 *
1776 * @state: NFSv4 state for an inode
1777 *
1778 * Returns NFS_OK if recovery for this stateid is now finished.
1779 * Otherwise a negative NFS4ERR value is returned.
1780 */
1781static int nfs41_check_open_stateid(struct nfs4_state *state)
1782{
1783 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04001784 nfs4_stateid *stateid = &state->open_stateid;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001785 int status;
1786
1787 /* If a state reset has been done, test_stateid is unneeded */
1788 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1789 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1790 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1791 return -NFS4ERR_BAD_STATEID;
1792
1793 status = nfs41_test_stateid(server, stateid);
1794 if (status != NFS_OK) {
1795 /* Free the stateid unless the server explicitly
1796 * informs us the stateid is unrecognized. */
1797 if (status != -NFS4ERR_BAD_STATEID)
1798 nfs41_free_stateid(server, stateid);
1799
1800 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1801 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1802 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001803 }
1804 return status;
1805}
1806
1807static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1808{
Chuck Levereb64cf92012-07-11 16:30:05 -04001809 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001810
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001811 nfs41_clear_delegation_stateid(state);
1812 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04001813 if (status != NFS_OK)
1814 status = nfs4_open_expired(sp, state);
1815 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001816}
1817#endif
1818
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001820 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1821 * fields corresponding to attributes that were used to store the verifier.
1822 * Make sure we clobber those fields in the later setattr call
1823 */
1824static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1825{
1826 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1827 !(sattr->ia_valid & ATTR_ATIME_SET))
1828 sattr->ia_valid |= ATTR_ATIME;
1829
1830 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1831 !(sattr->ia_valid & ATTR_MTIME_SET))
1832 sattr->ia_valid |= ATTR_MTIME;
1833}
1834
1835/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001836 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 */
Andy Adamson82be4172012-05-23 05:02:35 -04001838static int _nfs4_do_open(struct inode *dir,
1839 struct dentry *dentry,
1840 fmode_t fmode,
1841 int flags,
1842 struct iattr *sattr,
1843 struct rpc_cred *cred,
1844 struct nfs4_state **res,
1845 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846{
1847 struct nfs4_state_owner *sp;
1848 struct nfs4_state *state = NULL;
1849 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001850 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001851 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852
1853 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001855 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1856 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1858 goto out_err;
1859 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001860 status = nfs4_recover_expired_lease(server);
1861 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001862 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001863 if (dentry->d_inode != NULL)
1864 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001865 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001866 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001867 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001868 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
Andy Adamson82be4172012-05-23 05:02:35 -04001870 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
1871 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
1872 if (!opendata->f_attr.mdsthreshold)
1873 goto err_opendata_put;
Trond Myklebust1549210f2012-06-05 09:16:47 -04001874 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04001875 }
Al Viro82a2c1b2011-06-22 18:30:55 -04001876 if (dentry->d_inode != NULL)
1877 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001878
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001879 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001881 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001883 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001884 status = PTR_ERR(state);
1885 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001886 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001887 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001888 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001889
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001890 status = nfs4_opendata_access(cred, opendata, state, fmode);
1891 if (status != 0)
1892 goto err_opendata_put;
1893
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001894 if (opendata->o_arg.open_flags & O_EXCL) {
1895 nfs4_exclusive_attrset(opendata, sattr);
1896
1897 nfs_fattr_init(opendata->o_res.f_attr);
1898 status = nfs4_do_setattr(state->inode, cred,
1899 opendata->o_res.f_attr, sattr,
1900 state);
1901 if (status == 0)
1902 nfs_setattr_update_inode(state->inode, sattr);
1903 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1904 }
Andy Adamson82be4172012-05-23 05:02:35 -04001905
1906 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
1907 *ctx_th = opendata->f_attr.mdsthreshold;
1908 else
1909 kfree(opendata->f_attr.mdsthreshold);
1910 opendata->f_attr.mdsthreshold = NULL;
1911
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001912 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 *res = state;
1915 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001916err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04001917 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001918 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001919err_put_state_owner:
1920 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 *res = NULL;
1923 return status;
1924}
1925
1926
Andy Adamson82be4172012-05-23 05:02:35 -04001927static struct nfs4_state *nfs4_do_open(struct inode *dir,
1928 struct dentry *dentry,
1929 fmode_t fmode,
1930 int flags,
1931 struct iattr *sattr,
1932 struct rpc_cred *cred,
1933 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934{
1935 struct nfs4_exception exception = { };
1936 struct nfs4_state *res;
1937 int status;
1938
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001939 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 do {
Andy Adamson82be4172012-05-23 05:02:35 -04001941 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
1942 &res, ctx_th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 if (status == 0)
1944 break;
1945 /* NOTE: BAD_SEQID means the server and client disagree about the
1946 * book-keeping w.r.t. state-changing operations
1947 * (OPEN/CLOSE/LOCK/LOCKU...)
1948 * It is actually a sign of a bug on the client or on the server.
1949 *
1950 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001951 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 * have unhashed the old state_owner for us, and that we can
1953 * therefore safely retry using a new one. We should still warn
1954 * the user though...
1955 */
1956 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04001957 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001958 " returned a bad sequence-id error!\n",
1959 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 exception.retry = 1;
1961 continue;
1962 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001963 /*
1964 * BAD_STATEID on OPEN means that the server cancelled our
1965 * state before it received the OPEN_CONFIRM.
1966 * Recover by retrying the request as per the discussion
1967 * on Page 181 of RFC3530.
1968 */
1969 if (status == -NFS4ERR_BAD_STATEID) {
1970 exception.retry = 1;
1971 continue;
1972 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001973 if (status == -EAGAIN) {
1974 /* We must have found a delegation */
1975 exception.retry = 1;
1976 continue;
1977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1979 status, &exception));
1980 } while (exception.retry);
1981 return res;
1982}
1983
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001984static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1985 struct nfs_fattr *fattr, struct iattr *sattr,
1986 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001988 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001990 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 .iap = sattr,
1992 .server = server,
1993 .bitmask = server->attr_bitmask,
1994 };
1995 struct nfs_setattrres res = {
1996 .fattr = fattr,
1997 .server = server,
1998 };
1999 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002000 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2001 .rpc_argp = &arg,
2002 .rpc_resp = &res,
2003 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002005 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04002006 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
Trond Myklebust0e574af2005-10-27 22:12:38 -04002008 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
Trond Myklebust4fc87962012-03-08 17:42:01 -05002010 if (state != NULL) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002011 struct nfs_lockowner lockowner = {
2012 .l_owner = current->files,
2013 .l_pid = current->tgid,
2014 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002015 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002016 &lockowner);
Trond Myklebust4fc87962012-03-08 17:42:01 -05002017 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2018 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002019 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002020 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002021 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022
Bryan Schumaker7c513052011-03-24 17:12:24 +00002023 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002024 if (status == 0 && state != NULL)
2025 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002026 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027}
2028
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002029static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2030 struct nfs_fattr *fattr, struct iattr *sattr,
2031 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002033 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002034 struct nfs4_exception exception = {
2035 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002036 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002037 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 int err;
2039 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04002040 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2041 switch (err) {
2042 case -NFS4ERR_OPENMODE:
2043 if (state && !(state->state & FMODE_WRITE)) {
2044 err = -EBADF;
2045 if (sattr->ia_valid & ATTR_OPEN)
2046 err = -EACCES;
2047 goto out;
2048 }
2049 }
2050 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002052out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 return err;
2054}
2055
2056struct nfs4_closedata {
2057 struct inode *inode;
2058 struct nfs4_state *state;
2059 struct nfs_closeargs arg;
2060 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002061 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002062 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002063 bool roc;
2064 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065};
2066
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002067static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002068{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002069 struct nfs4_closedata *calldata = data;
2070 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002071 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002072
Fred Isamanf7e89172011-01-06 11:36:32 +00002073 if (calldata->roc)
2074 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002075 nfs4_put_open_state(calldata->state);
2076 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002077 nfs4_put_state_owner(sp);
Weston Andros Adamson324d0032012-10-30 17:01:39 -04002078 nfs_sb_deactive_async(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002079 kfree(calldata);
2080}
2081
Trond Myklebust88069f72009-12-08 08:33:16 -05002082static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2083 fmode_t fmode)
2084{
2085 spin_lock(&state->owner->so_lock);
2086 if (!(fmode & FMODE_READ))
2087 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2088 if (!(fmode & FMODE_WRITE))
2089 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2090 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2091 spin_unlock(&state->owner->so_lock);
2092}
2093
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002094static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002096 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 struct nfs_server *server = NFS_SERVER(calldata->inode);
2099
Chuck Levera3ca5652012-03-01 17:00:40 -05002100 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002101 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2102 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 /* hmm. we are done with the inode, and in the process of freeing
2104 * the state_owner. we keep this around to process errors
2105 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 switch (task->tk_status) {
2107 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002108 if (calldata->roc)
2109 pnfs_roc_set_barrier(state->inode,
2110 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002111 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002112 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002113 nfs4_close_clear_stateid_flags(state,
2114 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 break;
2116 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002117 case -NFS4ERR_OLD_STATEID:
2118 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002120 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002121 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002123 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2124 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002126 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002127 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002128 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129}
2130
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002131static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002133 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002134 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002135 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002136 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002137
Chuck Levera3ca5652012-03-01 17:00:40 -05002138 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002139 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07002140 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04002141
Trond Myklebust88069f72009-12-08 08:33:16 -05002142 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2143 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002144 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002145 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002146 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002147 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002148 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2149 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2150 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002151 }
2152 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002153 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2154 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2155 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002156 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002157 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002158 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002159
2160 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002161 /* Note: exit _without_ calling nfs4_close_done */
2162 task->tk_action = NULL;
Chuck Levera3ca5652012-03-01 17:00:40 -05002163 goto out;
Trond Myklebust95121352005-10-18 14:20:12 -07002164 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002165
Fred Isamanf7e89172011-01-06 11:36:32 +00002166 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002167 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002168 if (calldata->roc &&
Trond Myklebust7fdab062012-09-20 20:15:57 -04002169 pnfs_roc_drain(inode, &calldata->roc_barrier, task))
Chuck Levera3ca5652012-03-01 17:00:40 -05002170 goto out;
Fred Isamanf7e89172011-01-06 11:36:32 +00002171 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002172
Trond Myklebust516a6af2005-10-27 22:12:41 -04002173 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002174 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002175 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002176 &calldata->arg.seq_args,
2177 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002178 task) != 0)
2179 nfs_release_seqid(calldata->arg.seqid);
2180 else
2181 rpc_call_start(task);
Chuck Levera3ca5652012-03-01 17:00:40 -05002182out:
2183 dprintk("%s: done!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184}
2185
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002186static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002187 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002188 .rpc_call_done = nfs4_close_done,
2189 .rpc_release = nfs4_free_closedata,
2190};
2191
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192/*
2193 * It is possible for data to be read/written from a mem-mapped file
2194 * after the sys_close call (which hits the vfs layer as a flush).
2195 * This means that we can't safely call nfsv4 close on a file until
2196 * the inode is cleared. This in turn means that we are not good
2197 * NFSv4 citizens - we do not indicate to the server to update the file's
2198 * share state even when we are done with one of the three share
2199 * stateid's in the inode.
2200 *
2201 * NOTE: Caller must be holding the sp->so_owner semaphore!
2202 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002203int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002205 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002207 struct nfs4_state_owner *sp = state->owner;
2208 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002209 struct rpc_message msg = {
2210 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2211 .rpc_cred = state->owner->so_cred,
2212 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002213 struct rpc_task_setup task_setup_data = {
2214 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002215 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002216 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002217 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002218 .flags = RPC_TASK_ASYNC,
2219 };
Trond Myklebust95121352005-10-18 14:20:12 -07002220 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002222 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002224 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002225 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002226 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002228 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002229 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002231 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002232 if (calldata->arg.seqid == NULL)
2233 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002234 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002235 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002236 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002237 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002238 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002239 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002240 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002241
Trond Myklebust1174dd12010-12-21 10:52:24 -05002242 msg.rpc_argp = &calldata->arg;
2243 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002244 task_setup_data.callback_data = calldata;
2245 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002246 if (IS_ERR(task))
2247 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002248 status = 0;
2249 if (wait)
2250 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002251 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002252 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002253out_free_calldata:
2254 kfree(calldata);
2255out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002256 nfs4_put_open_state(state);
2257 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002258 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259}
2260
Trond Myklebust2b484292010-09-17 10:56:51 -04002261static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002262nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 struct nfs4_state *state;
2265
Trond Myklebust565277f2007-10-15 18:17:53 -04002266 /* Protect against concurrent sillydeletes */
Andy Adamson82be4172012-05-23 05:02:35 -04002267 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2268 ctx->cred, &ctx->mdsthreshold);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002269 if (IS_ERR(state))
2270 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002271 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002272 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273}
2274
Trond Myklebust1185a552009-12-03 15:54:02 -05002275static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002276{
2277 if (ctx->state == NULL)
2278 return;
2279 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002280 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002281 else
Al Viro643168c2011-06-22 18:20:23 -04002282 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002283}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284
2285static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2286{
Benny Halevy43652ad2009-04-01 09:21:54 -04002287 struct nfs4_server_caps_arg args = {
2288 .fhandle = fhandle,
2289 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 struct nfs4_server_caps_res res = {};
2291 struct rpc_message msg = {
2292 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002293 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 .rpc_resp = &res,
2295 };
2296 int status;
2297
Bryan Schumaker7c513052011-03-24 17:12:24 +00002298 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 if (status == 0) {
2300 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002301 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2302 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2303 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2304 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2305 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2307 server->caps |= NFS_CAP_ACLS;
2308 if (res.has_links != 0)
2309 server->caps |= NFS_CAP_HARDLINKS;
2310 if (res.has_symlinks != 0)
2311 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002312 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2313 server->caps |= NFS_CAP_FILEID;
2314 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2315 server->caps |= NFS_CAP_MODE;
2316 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2317 server->caps |= NFS_CAP_NLINK;
2318 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2319 server->caps |= NFS_CAP_OWNER;
2320 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2321 server->caps |= NFS_CAP_OWNER_GROUP;
2322 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2323 server->caps |= NFS_CAP_ATIME;
2324 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2325 server->caps |= NFS_CAP_CTIME;
2326 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2327 server->caps |= NFS_CAP_MTIME;
2328
Trond Myklebusta65318b2009-03-11 14:10:28 -04002329 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2330 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2331 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002333 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002335
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 return status;
2337}
2338
Trond Myklebust55a97592006-06-09 09:34:19 -04002339int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340{
2341 struct nfs4_exception exception = { };
2342 int err;
2343 do {
2344 err = nfs4_handle_exception(server,
2345 _nfs4_server_capabilities(server, fhandle),
2346 &exception);
2347 } while (exception.retry);
2348 return err;
2349}
2350
2351static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2352 struct nfs_fsinfo *info)
2353{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 struct nfs4_lookup_root_arg args = {
2355 .bitmask = nfs4_fattr_bitmap,
2356 };
2357 struct nfs4_lookup_res res = {
2358 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002359 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 .fh = fhandle,
2361 };
2362 struct rpc_message msg = {
2363 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2364 .rpc_argp = &args,
2365 .rpc_resp = &res,
2366 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002367
Trond Myklebust0e574af2005-10-27 22:12:38 -04002368 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002369 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370}
2371
2372static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2373 struct nfs_fsinfo *info)
2374{
2375 struct nfs4_exception exception = { };
2376 int err;
2377 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002378 err = _nfs4_lookup_root(server, fhandle, info);
2379 switch (err) {
2380 case 0:
2381 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002382 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002383 default:
2384 err = nfs4_handle_exception(server, err, &exception);
2385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002387out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 return err;
2389}
2390
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002391static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2392 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2393{
2394 struct rpc_auth *auth;
2395 int ret;
2396
2397 auth = rpcauth_create(flavor, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002398 if (IS_ERR(auth)) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002399 ret = -EIO;
2400 goto out;
2401 }
2402 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002403out:
2404 return ret;
2405}
2406
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002407static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002408 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002410 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002411 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002413 len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04002414 if (len < 0)
2415 return len;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002416
2417 for (i = 0; i < len; i++) {
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002418 /* AUTH_UNIX is the default flavor if none was specified,
2419 * thus has already been tried. */
2420 if (flav_array[i] == RPC_AUTH_UNIX)
2421 continue;
2422
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002423 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002424 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002425 continue;
2426 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002427 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002428 /*
2429 * -EACCESS could mean that the user doesn't have correct permissions
2430 * to access the mount. It could also mean that we tried to mount
2431 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2432 * existing mount programs don't handle -EACCES very well so it should
2433 * be mapped to -EPERM instead.
2434 */
2435 if (status == -EACCES)
2436 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002437 return status;
2438}
2439
2440/*
2441 * get the file handle for the "/" directory on the server
2442 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002443int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2444 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002445{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002446 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002447 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002448 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2449 /*
2450 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2451 * by nfs4_map_errors() as this function exits.
2452 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002453 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 if (status == 0)
2455 status = nfs4_server_capabilities(server, fhandle);
2456 if (status == 0)
2457 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002458 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459}
2460
Bryan Schumakerbae36242012-05-10 15:07:31 -04002461static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2462 struct nfs_fsinfo *info)
2463{
2464 int error;
2465 struct nfs_fattr *fattr = info->fattr;
2466
2467 error = nfs4_server_capabilities(server, mntfh);
2468 if (error < 0) {
2469 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2470 return error;
2471 }
2472
2473 error = nfs4_proc_getattr(server, mntfh, fattr);
2474 if (error < 0) {
2475 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2476 return error;
2477 }
2478
2479 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2480 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2481 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2482
2483 return error;
2484}
2485
Manoj Naik6b97fd32006-06-09 09:34:29 -04002486/*
2487 * Get locations and (maybe) other attributes of a referral.
2488 * Note that we'll actually follow the referral later when
2489 * we detect fsid mismatch in inode revalidation
2490 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002491static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2492 const struct qstr *name, struct nfs_fattr *fattr,
2493 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002494{
2495 int status = -ENOMEM;
2496 struct page *page = NULL;
2497 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002498
2499 page = alloc_page(GFP_KERNEL);
2500 if (page == NULL)
2501 goto out;
2502 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2503 if (locations == NULL)
2504 goto out;
2505
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002506 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002507 if (status != 0)
2508 goto out;
2509 /* Make sure server returned a different fsid for the referral */
2510 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002511 dprintk("%s: server did not return a different fsid for"
2512 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002513 status = -EIO;
2514 goto out;
2515 }
Andy Adamson533eb462011-06-13 18:25:56 -04002516 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2517 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002518
Andy Adamson533eb462011-06-13 18:25:56 -04002519 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002520 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002521 memset(fhandle, 0, sizeof(struct nfs_fh));
2522out:
2523 if (page)
2524 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002525 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002526 return status;
2527}
2528
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2530{
2531 struct nfs4_getattr_arg args = {
2532 .fh = fhandle,
2533 .bitmask = server->attr_bitmask,
2534 };
2535 struct nfs4_getattr_res res = {
2536 .fattr = fattr,
2537 .server = server,
2538 };
2539 struct rpc_message msg = {
2540 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2541 .rpc_argp = &args,
2542 .rpc_resp = &res,
2543 };
2544
Trond Myklebust0e574af2005-10-27 22:12:38 -04002545 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002546 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547}
2548
2549static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2550{
2551 struct nfs4_exception exception = { };
2552 int err;
2553 do {
2554 err = nfs4_handle_exception(server,
2555 _nfs4_proc_getattr(server, fhandle, fattr),
2556 &exception);
2557 } while (exception.retry);
2558 return err;
2559}
2560
2561/*
2562 * The file is not closed if it is opened due to the a request to change
2563 * the size of the file. The open call will not be needed once the
2564 * VFS layer lookup-intents are implemented.
2565 *
2566 * Close is called when the inode is destroyed.
2567 * If we haven't opened the file for O_WRONLY, we
2568 * need to in the size_change case to obtain a stateid.
2569 *
2570 * Got race?
2571 * Because OPEN is always done by name in nfsv4, it is
2572 * possible that we opened a different file by the same
2573 * name. We can recognize this race condition, but we
2574 * can't do anything about it besides returning an error.
2575 *
2576 * This will be fixed with VFS changes (lookup-intent).
2577 */
2578static int
2579nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2580 struct iattr *sattr)
2581{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002582 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002583 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002584 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 int status;
2586
Benny Halevy8a1636c2010-07-14 15:43:57 -04002587 if (pnfs_ld_layoutret_on_setattr(inode))
2588 pnfs_return_layout(inode);
2589
Trond Myklebust0e574af2005-10-27 22:12:38 -04002590 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591
Andy Adamson26699402012-05-30 16:12:24 -04002592 /* Deal with open(O_TRUNC) */
2593 if (sattr->ia_valid & ATTR_OPEN)
2594 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2595
2596 /* Optimization: if the end result is no change, don't RPC */
2597 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2598 return 0;
2599
Trond Myklebustd5308382005-11-04 15:33:38 -05002600 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002601 if (sattr->ia_valid & ATTR_FILE) {
2602 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002603
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002604 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002605 if (ctx) {
2606 cred = ctx->cred;
2607 state = ctx->state;
2608 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002609 }
2610
2611 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002612 if (status == 0)
2613 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 return status;
2615}
2616
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002617static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2618 const struct qstr *name, struct nfs_fh *fhandle,
2619 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002620{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002621 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002622 int status;
2623 struct nfs4_lookup_arg args = {
2624 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002625 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002626 .name = name,
2627 };
2628 struct nfs4_lookup_res res = {
2629 .server = server,
2630 .fattr = fattr,
2631 .fh = fhandle,
2632 };
2633 struct rpc_message msg = {
2634 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2635 .rpc_argp = &args,
2636 .rpc_resp = &res,
2637 };
2638
2639 nfs_fattr_init(fattr);
2640
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002642 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 dprintk("NFS reply lookup: %d\n", status);
2644 return status;
2645}
2646
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002647static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002648{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002649 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002650 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002651 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2652 fattr->nlink = 2;
2653}
2654
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002655static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2656 struct qstr *name, struct nfs_fh *fhandle,
2657 struct nfs_fattr *fattr)
2658{
2659 struct nfs4_exception exception = { };
2660 struct rpc_clnt *client = *clnt;
2661 int err;
2662 do {
2663 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2664 switch (err) {
2665 case -NFS4ERR_BADNAME:
2666 err = -ENOENT;
2667 goto out;
2668 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002669 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002670 goto out;
2671 case -NFS4ERR_WRONGSEC:
2672 err = -EPERM;
2673 if (client != *clnt)
2674 goto out;
2675
2676 client = nfs4_create_sec_client(client, dir, name);
2677 if (IS_ERR(client))
2678 return PTR_ERR(client);
2679
2680 exception.retry = 1;
2681 break;
2682 default:
2683 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2684 }
2685 } while (exception.retry);
2686
2687out:
2688 if (err == 0)
2689 *clnt = client;
2690 else if (client != *clnt)
2691 rpc_shutdown_client(client);
2692
2693 return err;
2694}
2695
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002696static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002697 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002699 int status;
2700 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002701
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002702 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2703 if (client != NFS_CLIENT(dir)) {
2704 rpc_shutdown_client(client);
2705 nfs_fixup_secinfo_attributes(fattr);
2706 }
2707 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708}
2709
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002710struct rpc_clnt *
2711nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2712 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2713{
2714 int status;
2715 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2716
2717 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2718 if (status < 0) {
2719 rpc_shutdown_client(client);
2720 return ERR_PTR(status);
2721 }
2722 return client;
2723}
2724
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2726{
Trond Myklebust76b32992007-08-10 17:45:11 -04002727 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 struct nfs4_accessargs args = {
2729 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002730 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002732 struct nfs4_accessres res = {
2733 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002734 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 struct rpc_message msg = {
2736 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2737 .rpc_argp = &args,
2738 .rpc_resp = &res,
2739 .rpc_cred = entry->cred,
2740 };
2741 int mode = entry->mask;
2742 int status;
2743
2744 /*
2745 * Determine which access bits we want to ask for...
2746 */
2747 if (mode & MAY_READ)
2748 args.access |= NFS4_ACCESS_READ;
2749 if (S_ISDIR(inode->i_mode)) {
2750 if (mode & MAY_WRITE)
2751 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2752 if (mode & MAY_EXEC)
2753 args.access |= NFS4_ACCESS_LOOKUP;
2754 } else {
2755 if (mode & MAY_WRITE)
2756 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2757 if (mode & MAY_EXEC)
2758 args.access |= NFS4_ACCESS_EXECUTE;
2759 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002760
2761 res.fattr = nfs_alloc_fattr();
2762 if (res.fattr == NULL)
2763 return -ENOMEM;
2764
Bryan Schumaker7c513052011-03-24 17:12:24 +00002765 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002767 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04002768 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002770 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 return status;
2772}
2773
2774static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2775{
2776 struct nfs4_exception exception = { };
2777 int err;
2778 do {
2779 err = nfs4_handle_exception(NFS_SERVER(inode),
2780 _nfs4_proc_access(inode, entry),
2781 &exception);
2782 } while (exception.retry);
2783 return err;
2784}
2785
2786/*
2787 * TODO: For the time being, we don't try to get any attributes
2788 * along with any of the zero-copy operations READ, READDIR,
2789 * READLINK, WRITE.
2790 *
2791 * In the case of the first three, we want to put the GETATTR
2792 * after the read-type operation -- this is because it is hard
2793 * to predict the length of a GETATTR response in v4, and thus
2794 * align the READ data correctly. This means that the GETATTR
2795 * may end up partially falling into the page cache, and we should
2796 * shift it into the 'tail' of the xdr_buf before processing.
2797 * To do this efficiently, we need to know the total length
2798 * of data received, which doesn't seem to be available outside
2799 * of the RPC layer.
2800 *
2801 * In the case of WRITE, we also want to put the GETATTR after
2802 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04002803 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 *
2805 * Both of these changes to the XDR layer would in fact be quite
2806 * minor, but I decided to leave them for a subsequent patch.
2807 */
2808static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2809 unsigned int pgbase, unsigned int pglen)
2810{
2811 struct nfs4_readlink args = {
2812 .fh = NFS_FH(inode),
2813 .pgbase = pgbase,
2814 .pglen = pglen,
2815 .pages = &page,
2816 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002817 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 struct rpc_message msg = {
2819 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2820 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002821 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 };
2823
Bryan Schumaker7c513052011-03-24 17:12:24 +00002824 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 -07002825}
2826
2827static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2828 unsigned int pgbase, unsigned int pglen)
2829{
2830 struct nfs4_exception exception = { };
2831 int err;
2832 do {
2833 err = nfs4_handle_exception(NFS_SERVER(inode),
2834 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2835 &exception);
2836 } while (exception.retry);
2837 return err;
2838}
2839
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002841 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843static int
2844nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002845 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846{
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002847 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 int status = 0;
2850
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002851 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
2852 if (IS_ERR(ctx))
2853 return PTR_ERR(ctx);
2854
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002855 sattr->ia_mode &= ~current_umask();
Trond Myklebust8626e4a2012-07-16 12:01:42 -04002856 state = nfs4_do_open(dir, dentry, ctx->mode,
2857 flags, sattr, ctx->cred,
2858 &ctx->mdsthreshold);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002859 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 if (IS_ERR(state)) {
2861 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002862 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002864 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002865 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002866 ctx->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867out:
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002868 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 return status;
2870}
2871
2872static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2873{
Trond Myklebust16e42952005-10-27 22:12:44 -04002874 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002875 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07002877 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002879 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002880 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002881 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002883 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2884 .rpc_argp = &args,
2885 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 };
Trond Myklebust778d2812012-04-27 13:48:19 -04002887 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04002888
Bryan Schumaker7c513052011-03-24 17:12:24 +00002889 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04002890 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04002891 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 return status;
2893}
2894
2895static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2896{
2897 struct nfs4_exception exception = { };
2898 int err;
2899 do {
2900 err = nfs4_handle_exception(NFS_SERVER(dir),
2901 _nfs4_proc_remove(dir, name),
2902 &exception);
2903 } while (exception.retry);
2904 return err;
2905}
2906
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002907static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002909 struct nfs_server *server = NFS_SERVER(dir);
2910 struct nfs_removeargs *args = msg->rpc_argp;
2911 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002913 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05002915 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916}
2917
Bryan Schumaker34e137c2012-03-19 14:54:41 -04002918static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
2919{
2920 if (nfs4_setup_sequence(NFS_SERVER(data->dir),
2921 &data->args.seq_args,
2922 &data->res.seq_res,
2923 task))
2924 return;
2925 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926}
2927
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002928static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002930 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2931
Trond Myklebust14516c32010-07-31 14:29:06 -04002932 if (!nfs4_sequence_done(task, &res->seq_res))
2933 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002934 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002935 return 0;
2936 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002937 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938}
2939
Jeff Laytond3d41522010-09-17 17:31:57 -04002940static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2941{
2942 struct nfs_server *server = NFS_SERVER(dir);
2943 struct nfs_renameargs *arg = msg->rpc_argp;
2944 struct nfs_renameres *res = msg->rpc_resp;
2945
2946 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04002947 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002948 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04002949}
2950
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04002951static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
2952{
2953 if (nfs4_setup_sequence(NFS_SERVER(data->old_dir),
2954 &data->args.seq_args,
2955 &data->res.seq_res,
2956 task))
2957 return;
2958 rpc_call_start(task);
Jeff Laytond3d41522010-09-17 17:31:57 -04002959}
2960
2961static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2962 struct inode *new_dir)
2963{
2964 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2965
2966 if (!nfs4_sequence_done(task, &res->seq_res))
2967 return 0;
2968 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2969 return 0;
2970
2971 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04002972 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04002973 return 1;
2974}
2975
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2977 struct inode *new_dir, struct qstr *new_name)
2978{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002979 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002980 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 .old_dir = NFS_FH(old_dir),
2982 .new_dir = NFS_FH(new_dir),
2983 .old_name = old_name,
2984 .new_name = new_name,
2985 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002986 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002987 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002988 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 struct rpc_message msg = {
2990 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2991 .rpc_argp = &arg,
2992 .rpc_resp = &res,
2993 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002994 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995
Bryan Schumaker7c513052011-03-24 17:12:24 +00002996 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 if (!status) {
2998 update_changeattr(old_dir, &res.old_cinfo);
2999 update_changeattr(new_dir, &res.new_cinfo);
3000 }
3001 return status;
3002}
3003
3004static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3005 struct inode *new_dir, struct qstr *new_name)
3006{
3007 struct nfs4_exception exception = { };
3008 int err;
3009 do {
3010 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3011 _nfs4_proc_rename(old_dir, old_name,
3012 new_dir, new_name),
3013 &exception);
3014 } while (exception.retry);
3015 return err;
3016}
3017
3018static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3019{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003020 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 struct nfs4_link_arg arg = {
3022 .fh = NFS_FH(inode),
3023 .dir_fh = NFS_FH(dir),
3024 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003025 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003027 struct nfs4_link_res res = {
3028 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003029 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 struct rpc_message msg = {
3031 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3032 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003033 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003035 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036
Trond Myklebust136f2622010-04-16 16:22:49 -04003037 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003038 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003039 goto out;
3040
Bryan Schumaker7c513052011-03-24 17:12:24 +00003041 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003042 if (!status) {
3043 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04003044 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003045 }
Trond Myklebust136f2622010-04-16 16:22:49 -04003046out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003047 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 return status;
3049}
3050
3051static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3052{
3053 struct nfs4_exception exception = { };
3054 int err;
3055 do {
3056 err = nfs4_handle_exception(NFS_SERVER(inode),
3057 _nfs4_proc_link(inode, dir, name),
3058 &exception);
3059 } while (exception.retry);
3060 return err;
3061}
3062
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003063struct nfs4_createdata {
3064 struct rpc_message msg;
3065 struct nfs4_create_arg arg;
3066 struct nfs4_create_res res;
3067 struct nfs_fh fh;
3068 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003069};
3070
3071static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3072 struct qstr *name, struct iattr *sattr, u32 ftype)
3073{
3074 struct nfs4_createdata *data;
3075
3076 data = kzalloc(sizeof(*data), GFP_KERNEL);
3077 if (data != NULL) {
3078 struct nfs_server *server = NFS_SERVER(dir);
3079
3080 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3081 data->msg.rpc_argp = &data->arg;
3082 data->msg.rpc_resp = &data->res;
3083 data->arg.dir_fh = NFS_FH(dir);
3084 data->arg.server = server;
3085 data->arg.name = name;
3086 data->arg.attrs = sattr;
3087 data->arg.ftype = ftype;
3088 data->arg.bitmask = server->attr_bitmask;
3089 data->res.server = server;
3090 data->res.fh = &data->fh;
3091 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003092 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003093 }
3094 return data;
3095}
3096
3097static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3098{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003099 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003100 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003101 if (status == 0) {
3102 update_changeattr(dir, &data->res.dir_cinfo);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003103 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3104 }
3105 return status;
3106}
3107
3108static void nfs4_free_createdata(struct nfs4_createdata *data)
3109{
3110 kfree(data);
3111}
3112
Chuck Lever4f390c12006-08-22 20:06:22 -04003113static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003114 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003116 struct nfs4_createdata *data;
3117 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118
Chuck Lever94a6d752006-08-22 20:06:23 -04003119 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003120 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003121
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003122 status = -ENOMEM;
3123 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3124 if (data == NULL)
3125 goto out;
3126
3127 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3128 data->arg.u.symlink.pages = &page;
3129 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003131 status = nfs4_do_create(dir, dentry, data);
3132
3133 nfs4_free_createdata(data);
3134out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 return status;
3136}
3137
Chuck Lever4f390c12006-08-22 20:06:22 -04003138static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003139 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140{
3141 struct nfs4_exception exception = { };
3142 int err;
3143 do {
3144 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003145 _nfs4_proc_symlink(dir, dentry, page,
3146 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 &exception);
3148 } while (exception.retry);
3149 return err;
3150}
3151
3152static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3153 struct iattr *sattr)
3154{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003155 struct nfs4_createdata *data;
3156 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003158 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3159 if (data == NULL)
3160 goto out;
3161
3162 status = nfs4_do_create(dir, dentry, data);
3163
3164 nfs4_free_createdata(data);
3165out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 return status;
3167}
3168
3169static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3170 struct iattr *sattr)
3171{
3172 struct nfs4_exception exception = { };
3173 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003174
3175 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 do {
3177 err = nfs4_handle_exception(NFS_SERVER(dir),
3178 _nfs4_proc_mkdir(dir, dentry, sattr),
3179 &exception);
3180 } while (exception.retry);
3181 return err;
3182}
3183
3184static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003185 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186{
3187 struct inode *dir = dentry->d_inode;
3188 struct nfs4_readdir_arg args = {
3189 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003190 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 .pgbase = 0,
3192 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003193 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003194 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 };
3196 struct nfs4_readdir_res res;
3197 struct rpc_message msg = {
3198 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3199 .rpc_argp = &args,
3200 .rpc_resp = &res,
3201 .rpc_cred = cred,
3202 };
3203 int status;
3204
Harvey Harrison3110ff82008-05-02 13:42:44 -07003205 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003206 dentry->d_parent->d_name.name,
3207 dentry->d_name.name,
3208 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003209 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003211 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 -05003212 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003213 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003214 status += args.pgbase;
3215 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003216
3217 nfs_invalidate_atime(dir);
3218
Harvey Harrison3110ff82008-05-02 13:42:44 -07003219 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 return status;
3221}
3222
3223static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003224 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225{
3226 struct nfs4_exception exception = { };
3227 int err;
3228 do {
3229 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3230 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003231 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 &exception);
3233 } while (exception.retry);
3234 return err;
3235}
3236
3237static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3238 struct iattr *sattr, dev_t rdev)
3239{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003240 struct nfs4_createdata *data;
3241 int mode = sattr->ia_mode;
3242 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003244 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3245 if (data == NULL)
3246 goto out;
3247
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003249 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003251 data->arg.ftype = NF4BLK;
3252 data->arg.u.device.specdata1 = MAJOR(rdev);
3253 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 }
3255 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003256 data->arg.ftype = NF4CHR;
3257 data->arg.u.device.specdata1 = MAJOR(rdev);
3258 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003259 } else if (!S_ISSOCK(mode)) {
3260 status = -EINVAL;
3261 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003264 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003265out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003266 nfs4_free_createdata(data);
3267out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 return status;
3269}
3270
3271static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3272 struct iattr *sattr, dev_t rdev)
3273{
3274 struct nfs4_exception exception = { };
3275 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003276
3277 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 do {
3279 err = nfs4_handle_exception(NFS_SERVER(dir),
3280 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3281 &exception);
3282 } while (exception.retry);
3283 return err;
3284}
3285
3286static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3287 struct nfs_fsstat *fsstat)
3288{
3289 struct nfs4_statfs_arg args = {
3290 .fh = fhandle,
3291 .bitmask = server->attr_bitmask,
3292 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003293 struct nfs4_statfs_res res = {
3294 .fsstat = fsstat,
3295 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 struct rpc_message msg = {
3297 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3298 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003299 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 };
3301
Trond Myklebust0e574af2005-10-27 22:12:38 -04003302 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003303 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304}
3305
3306static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3307{
3308 struct nfs4_exception exception = { };
3309 int err;
3310 do {
3311 err = nfs4_handle_exception(server,
3312 _nfs4_proc_statfs(server, fhandle, fsstat),
3313 &exception);
3314 } while (exception.retry);
3315 return err;
3316}
3317
3318static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3319 struct nfs_fsinfo *fsinfo)
3320{
3321 struct nfs4_fsinfo_arg args = {
3322 .fh = fhandle,
3323 .bitmask = server->attr_bitmask,
3324 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003325 struct nfs4_fsinfo_res res = {
3326 .fsinfo = fsinfo,
3327 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328 struct rpc_message msg = {
3329 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3330 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003331 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 };
3333
Bryan Schumaker7c513052011-03-24 17:12:24 +00003334 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335}
3336
3337static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3338{
3339 struct nfs4_exception exception = { };
3340 int err;
3341
3342 do {
3343 err = nfs4_handle_exception(server,
3344 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3345 &exception);
3346 } while (exception.retry);
3347 return err;
3348}
3349
3350static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3351{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003352 int error;
3353
Trond Myklebust0e574af2005-10-27 22:12:38 -04003354 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003355 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003356 if (error == 0) {
3357 /* block layout checks this! */
3358 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003359 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003360 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003361
3362 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363}
3364
3365static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3366 struct nfs_pathconf *pathconf)
3367{
3368 struct nfs4_pathconf_arg args = {
3369 .fh = fhandle,
3370 .bitmask = server->attr_bitmask,
3371 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003372 struct nfs4_pathconf_res res = {
3373 .pathconf = pathconf,
3374 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 struct rpc_message msg = {
3376 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3377 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003378 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 };
3380
3381 /* None of the pathconf attributes are mandatory to implement */
3382 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3383 memset(pathconf, 0, sizeof(*pathconf));
3384 return 0;
3385 }
3386
Trond Myklebust0e574af2005-10-27 22:12:38 -04003387 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003388 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389}
3390
3391static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3392 struct nfs_pathconf *pathconf)
3393{
3394 struct nfs4_exception exception = { };
3395 int err;
3396
3397 do {
3398 err = nfs4_handle_exception(server,
3399 _nfs4_proc_pathconf(server, fhandle, pathconf),
3400 &exception);
3401 } while (exception.retry);
3402 return err;
3403}
3404
Benny Halevyd20581a2011-05-22 19:52:03 +03003405void __nfs4_read_done_cb(struct nfs_read_data *data)
3406{
Fred Isamancd841602012-04-20 14:47:44 -04003407 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003408}
3409
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003410static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411{
Fred Isamancd841602012-04-20 14:47:44 -04003412 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003414 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003415 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003416 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003418
Benny Halevyd20581a2011-05-22 19:52:03 +03003419 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003421 renew_lease(server, data->timestamp);
3422 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423}
3424
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003425static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3426{
3427
3428 dprintk("--> %s\n", __func__);
3429
3430 if (!nfs4_sequence_done(task, &data->res.seq_res))
3431 return -EAGAIN;
3432
Benny Halevyd20581a2011-05-22 19:52:03 +03003433 return data->read_done_cb ? data->read_done_cb(task, data) :
3434 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003435}
3436
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003437static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003440 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003441 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003442 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443}
3444
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003445static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3446{
Fred Isamancd841602012-04-20 14:47:44 -04003447 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003448 &data->args.seq_args,
3449 &data->res.seq_res,
3450 task))
3451 return;
3452 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453}
3454
Fred Isamanb029bc92011-03-03 15:13:42 +00003455static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456{
Fred Isamancd841602012-04-20 14:47:44 -04003457 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003459 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003460 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003461 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003463 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003465 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003466 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003467 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468}
3469
Fred Isamanb029bc92011-03-03 15:13:42 +00003470static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3471{
3472 if (!nfs4_sequence_done(task, &data->res.seq_res))
3473 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003474 return data->write_done_cb ? data->write_done_cb(task, data) :
3475 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003476}
3477
Trond Myklebust5a37f852012-04-28 14:55:16 -04003478static
3479bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003480{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003481 const struct nfs_pgio_header *hdr = data->header;
3482
3483 /* Don't request attributes for pNFS or O_DIRECT writes */
3484 if (data->ds_clp != NULL || hdr->dreq != NULL)
3485 return false;
3486 /* Otherwise, request attributes if and only if we don't hold
3487 * a delegation
3488 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04003489 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003490}
Fred Isamana69aef12011-03-03 15:13:47 +00003491
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003492static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493{
Fred Isamancd841602012-04-20 14:47:44 -04003494 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003495
Trond Myklebust5a37f852012-04-28 14:55:16 -04003496 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003497 data->args.bitmask = NULL;
3498 data->res.fattr = NULL;
3499 } else
3500 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003501
Fred Isamanb029bc92011-03-03 15:13:42 +00003502 if (!data->write_done_cb)
3503 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003504 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 data->timestamp = jiffies;
3506
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003507 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003508 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509}
3510
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003511static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3512{
Fred Isamancd841602012-04-20 14:47:44 -04003513 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003514 &data->args.seq_args,
3515 &data->res.seq_res,
3516 task))
3517 return;
3518 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519}
3520
Fred Isaman0b7c0152012-04-20 14:47:39 -04003521static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3522{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3524 &data->args.seq_args,
Trond Myklebust788e7a82006-03-20 13:44:27 -05003525 &data->res.seq_res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527 return;
3528 rpc_call_start(task);
Trond Myklebust14516c32010-07-31 14:29:06 -04003529}
3530
Fred Isaman0b7c0152012-04-20 14:47:39 -04003531static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532{
3533 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003534
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003535 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003536 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003537 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003539 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540}
3541
Fred Isaman0b7c0152012-04-20 14:47:39 -04003542static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003543{
3544 if (!nfs4_sequence_done(task, &data->res.seq_res))
3545 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003546 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003547}
3548
Fred Isaman0b7c0152012-04-20 14:47:39 -04003549static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003551 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003552
Fred Isaman0b7c0152012-04-20 14:47:39 -04003553 if (data->commit_done_cb == NULL)
3554 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003555 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003556 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003557 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558}
3559
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003560struct nfs4_renewdata {
3561 struct nfs_client *client;
3562 unsigned long timestamp;
3563};
3564
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565/*
3566 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3567 * standalone procedure for queueing an asynchronous RENEW.
3568 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003569static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003570{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003571 struct nfs4_renewdata *data = calldata;
3572 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003573
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003574 if (atomic_read(&clp->cl_count) > 1)
3575 nfs4_schedule_state_renewal(clp);
3576 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003577 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003578}
3579
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003580static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003582 struct nfs4_renewdata *data = calldata;
3583 struct nfs_client *clp = data->client;
3584 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585
3586 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003587 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003588 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3589 return;
3590 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003591 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003592 return;
3593 }
3594 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003596 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597}
3598
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003599static const struct rpc_call_ops nfs4_renew_ops = {
3600 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003601 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003602};
3603
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003604static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605{
3606 struct rpc_message msg = {
3607 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3608 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003609 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003611 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003613 if (renew_flags == 0)
3614 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003615 if (!atomic_inc_not_zero(&clp->cl_count))
3616 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003617 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003618 if (data == NULL)
3619 return -ENOMEM;
3620 data->client = clp;
3621 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003623 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624}
3625
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003626static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627{
3628 struct rpc_message msg = {
3629 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3630 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003631 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632 };
3633 unsigned long now = jiffies;
3634 int status;
3635
3636 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3637 if (status < 0)
3638 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003639 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640 return 0;
3641}
3642
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003643static inline int nfs4_server_supports_acls(struct nfs_server *server)
3644{
3645 return (server->caps & NFS_CAP_ACLS)
3646 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3647 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3648}
3649
Trond Myklebust21f498c2012-08-24 10:59:25 -04003650/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3651 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003652 * the stack.
3653 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04003654#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003655
Neil Hormane9e3d722011-03-04 19:26:03 -05003656static int buf_to_pages_noslab(const void *buf, size_t buflen,
3657 struct page **pages, unsigned int *pgbase)
3658{
3659 struct page *newpage, **spages;
3660 int rc = 0;
3661 size_t len;
3662 spages = pages;
3663
3664 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04003665 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003666 newpage = alloc_page(GFP_KERNEL);
3667
3668 if (newpage == NULL)
3669 goto unwind;
3670 memcpy(page_address(newpage), buf, len);
3671 buf += len;
3672 buflen -= len;
3673 *pages++ = newpage;
3674 rc++;
3675 } while (buflen != 0);
3676
3677 return rc;
3678
3679unwind:
3680 for(; rc > 0; rc--)
3681 __free_page(spages[rc-1]);
3682 return -ENOMEM;
3683}
3684
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003685struct nfs4_cached_acl {
3686 int cached;
3687 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003688 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003689};
3690
3691static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003692{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003693 struct nfs_inode *nfsi = NFS_I(inode);
3694
3695 spin_lock(&inode->i_lock);
3696 kfree(nfsi->nfs4_acl);
3697 nfsi->nfs4_acl = acl;
3698 spin_unlock(&inode->i_lock);
3699}
3700
3701static void nfs4_zap_acl_attr(struct inode *inode)
3702{
3703 nfs4_set_cached_acl(inode, NULL);
3704}
3705
3706static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3707{
3708 struct nfs_inode *nfsi = NFS_I(inode);
3709 struct nfs4_cached_acl *acl;
3710 int ret = -ENOENT;
3711
3712 spin_lock(&inode->i_lock);
3713 acl = nfsi->nfs4_acl;
3714 if (acl == NULL)
3715 goto out;
3716 if (buf == NULL) /* user is just asking for length */
3717 goto out_len;
3718 if (acl->cached == 0)
3719 goto out;
3720 ret = -ERANGE; /* see getxattr(2) man page */
3721 if (acl->len > buflen)
3722 goto out;
3723 memcpy(buf, acl->data, acl->len);
3724out_len:
3725 ret = acl->len;
3726out:
3727 spin_unlock(&inode->i_lock);
3728 return ret;
3729}
3730
Sachin Prabhu5794d212012-04-17 14:36:40 +01003731static 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 +00003732{
3733 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003734 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003735
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003736 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003737 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003738 if (acl == NULL)
3739 goto out;
3740 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003741 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003742 } else {
3743 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3744 if (acl == NULL)
3745 goto out;
3746 acl->cached = 0;
3747 }
3748 acl->len = acl_len;
3749out:
3750 nfs4_set_cached_acl(inode, acl);
3751}
3752
Andy Adamsonbf118a32011-12-07 11:55:27 -05003753/*
3754 * The getxattr API returns the required buffer length when called with a
3755 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3756 * the required buf. On a NULL buf, we send a page of data to the server
3757 * guessing that the ACL request can be serviced by a page. If so, we cache
3758 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3759 * the cache. If not so, we throw away the page, and cache the required
3760 * length. The next getxattr call will then produce another round trip to
3761 * the server, this time with the input buf of the required size.
3762 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003763static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003764{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003765 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003766 struct nfs_getaclargs args = {
3767 .fh = NFS_FH(inode),
3768 .acl_pages = pages,
3769 .acl_len = buflen,
3770 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003771 struct nfs_getaclres res = {
3772 .acl_len = buflen,
3773 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003774 struct rpc_message msg = {
3775 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3776 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003777 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003778 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003779 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3780 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003781
Andy Adamsonbf118a32011-12-07 11:55:27 -05003782 /* As long as we're doing a round trip to the server anyway,
3783 * let's be prepared for a page of acl data. */
3784 if (npages == 0)
3785 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003786 if (npages > ARRAY_SIZE(pages))
3787 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003788
Andy Adamsonbf118a32011-12-07 11:55:27 -05003789 for (i = 0; i < npages; i++) {
3790 pages[i] = alloc_page(GFP_KERNEL);
3791 if (!pages[i])
3792 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003793 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01003794
3795 /* for decoding across pages */
3796 res.acl_scratch = alloc_page(GFP_KERNEL);
3797 if (!res.acl_scratch)
3798 goto out_free;
3799
Andy Adamsonbf118a32011-12-07 11:55:27 -05003800 args.acl_len = npages * PAGE_SIZE;
3801 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003802
Peng Taode040be2012-01-10 22:42:47 +08003803 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003804 __func__, buf, buflen, npages, args.acl_len);
3805 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3806 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003807 if (ret)
3808 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003809
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003810 /* Handle the case where the passed-in buffer is too short */
3811 if (res.acl_flags & NFS4_ACL_TRUNC) {
3812 /* Did the user only issue a request for the acl length? */
3813 if (buf == NULL)
3814 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003815 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003816 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003817 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003818 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
3819 if (buf)
3820 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
3821out_ok:
3822 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003823out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003824 for (i = 0; i < npages; i++)
3825 if (pages[i])
3826 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05003827 if (res.acl_scratch)
3828 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003829 return ret;
3830}
3831
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003832static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3833{
3834 struct nfs4_exception exception = { };
3835 ssize_t ret;
3836 do {
3837 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3838 if (ret >= 0)
3839 break;
3840 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3841 } while (exception.retry);
3842 return ret;
3843}
3844
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003845static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3846{
3847 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003848 int ret;
3849
3850 if (!nfs4_server_supports_acls(server))
3851 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003852 ret = nfs_revalidate_inode(server, inode);
3853 if (ret < 0)
3854 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003855 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3856 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003857 ret = nfs4_read_cached_acl(inode, buf, buflen);
3858 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003859 /* -ENOENT is returned if there is no ACL or if there is an ACL
3860 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003861 return ret;
3862 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003863}
3864
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003865static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003866{
3867 struct nfs_server *server = NFS_SERVER(inode);
3868 struct page *pages[NFS4ACL_MAXPAGES];
3869 struct nfs_setaclargs arg = {
3870 .fh = NFS_FH(inode),
3871 .acl_pages = pages,
3872 .acl_len = buflen,
3873 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003874 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003875 struct rpc_message msg = {
3876 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3877 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003878 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003879 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003880 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05003881 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003882
3883 if (!nfs4_server_supports_acls(server))
3884 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003885 if (npages > ARRAY_SIZE(pages))
3886 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05003887 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3888 if (i < 0)
3889 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04003890 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003891 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003892
3893 /*
3894 * Free each page after tx, so the only ref left is
3895 * held by the network stack
3896 */
3897 for (; i > 0; i--)
3898 put_page(pages[i-1]);
3899
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003900 /*
3901 * Acl update can result in inode attribute update.
3902 * so mark the attribute cache invalid.
3903 */
3904 spin_lock(&inode->i_lock);
3905 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3906 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003907 nfs_access_zap_cache(inode);
3908 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003909 return ret;
3910}
3911
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003912static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3913{
3914 struct nfs4_exception exception = { };
3915 int err;
3916 do {
3917 err = nfs4_handle_exception(NFS_SERVER(inode),
3918 __nfs4_proc_set_acl(inode, buf, buflen),
3919 &exception);
3920 } while (exception.retry);
3921 return err;
3922}
3923
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003925nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003927 struct nfs_client *clp = server->nfs_client;
3928
3929 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930 return 0;
3931 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05003932 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003933 case -NFS4ERR_ADMIN_REVOKED:
3934 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04003935 if (state == NULL)
3936 break;
3937 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003938 case -NFS4ERR_OPENMODE:
3939 if (state == NULL)
3940 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003941 nfs4_schedule_stateid_recovery(server, state);
3942 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003943 case -NFS4ERR_EXPIRED:
3944 if (state != NULL)
3945 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003947 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003948 nfs4_schedule_lease_recovery(clp);
3949 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003950#if defined(CONFIG_NFS_V4_1)
3951 case -NFS4ERR_BADSESSION:
3952 case -NFS4ERR_BADSLOT:
3953 case -NFS4ERR_BAD_HIGH_SLOT:
3954 case -NFS4ERR_DEADSESSION:
3955 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3956 case -NFS4ERR_SEQ_FALSE_RETRY:
3957 case -NFS4ERR_SEQ_MISORDERED:
3958 dprintk("%s ERROR %d, Reset session\n", __func__,
3959 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04003960 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04003961 task->tk_status = 0;
3962 return -EAGAIN;
3963#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003965 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003966 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003967 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3969 task->tk_status = 0;
3970 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04003971 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972 case -NFS4ERR_OLD_STATEID:
3973 task->tk_status = 0;
3974 return -EAGAIN;
3975 }
3976 task->tk_status = nfs4_map_errors(task->tk_status);
3977 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003978wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003979 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003980 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3981 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3982 task->tk_status = 0;
3983 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984}
3985
Chuck Leverf0920752012-05-21 22:45:41 -04003986static void nfs4_init_boot_verifier(const struct nfs_client *clp,
3987 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05003988{
3989 __be32 verf[2];
3990
Chuck Lever2c820d92012-05-21 22:45:33 -04003991 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
3992 /* An impossible timestamp guarantees this value
3993 * will never match a generated boot time. */
3994 verf[0] = 0;
3995 verf[1] = (__be32)(NSEC_PER_SEC + 1);
3996 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04003997 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
3998 verf[0] = (__be32)nn->boot_time.tv_sec;
3999 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04004000 }
Chuck Levercd937102012-03-02 17:14:31 -05004001 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4002}
4003
Chuck Levere984a552012-09-14 17:24:21 -04004004static unsigned int
4005nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4006 char *buf, size_t len)
4007{
4008 unsigned int result;
4009
4010 rcu_read_lock();
4011 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4012 clp->cl_ipaddr,
4013 rpc_peeraddr2str(clp->cl_rpcclient,
4014 RPC_DISPLAY_ADDR),
4015 rpc_peeraddr2str(clp->cl_rpcclient,
4016 RPC_DISPLAY_PROTO));
4017 rcu_read_unlock();
4018 return result;
4019}
4020
4021static unsigned int
4022nfs4_init_uniform_client_string(const struct nfs_client *clp,
4023 char *buf, size_t len)
4024{
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004025 char *nodename = clp->cl_rpcclient->cl_nodename;
4026
4027 if (nfs4_client_id_uniquifier[0] != '\0')
4028 nodename = nfs4_client_id_uniquifier;
Chuck Levere984a552012-09-14 17:24:21 -04004029 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4030 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004031 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004032}
4033
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004034/**
4035 * nfs4_proc_setclientid - Negotiate client ID
4036 * @clp: state data structure
4037 * @program: RPC program for NFSv4 callback service
4038 * @port: IP port number for NFS4 callback service
4039 * @cred: RPC credential to use for this call
4040 * @res: where to place the result
4041 *
4042 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4043 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004044int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4045 unsigned short port, struct rpc_cred *cred,
4046 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047{
4048 nfs4_verifier sc_verifier;
4049 struct nfs4_setclientid setclientid = {
4050 .sc_verifier = &sc_verifier,
4051 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004052 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053 };
4054 struct rpc_message msg = {
4055 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4056 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004057 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004058 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004060 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061
Chuck Leverde734832012-07-11 16:30:50 -04004062 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004063 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004064 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4065 setclientid.sc_name_len =
4066 nfs4_init_uniform_client_string(clp,
4067 setclientid.sc_name,
4068 sizeof(setclientid.sc_name));
4069 else
4070 setclientid.sc_name_len =
4071 nfs4_init_nonuniform_client_string(clp,
4072 setclientid.sc_name,
4073 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004074 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004075 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004076 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004077 sizeof(setclientid.sc_netid),
4078 rpc_peeraddr2str(clp->cl_rpcclient,
4079 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004080 rcu_read_unlock();
4081 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004082 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083 clp->cl_ipaddr, port >> 8, port & 255);
4084
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004085 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4086 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4087 setclientid.sc_name_len, setclientid.sc_name);
4088 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4089 dprintk("NFS reply setclientid: %d\n", status);
4090 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091}
4092
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004093/**
4094 * nfs4_proc_setclientid_confirm - Confirm client ID
4095 * @clp: state data structure
4096 * @res: result of a previous SETCLIENTID
4097 * @cred: RPC credential to use for this call
4098 *
4099 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4100 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004101int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004102 struct nfs4_setclientid_res *arg,
4103 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104{
4105 struct nfs_fsinfo fsinfo;
4106 struct rpc_message msg = {
4107 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004108 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004110 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 };
4112 unsigned long now;
4113 int status;
4114
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004115 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4116 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4117 clp->cl_clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004119 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120 if (status == 0) {
4121 spin_lock(&clp->cl_lock);
4122 clp->cl_lease_time = fsinfo.lease_time * HZ;
4123 clp->cl_last_renewal = now;
4124 spin_unlock(&clp->cl_lock);
4125 }
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004126 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 return status;
4128}
4129
Trond Myklebustfe650402006-01-03 09:55:18 +01004130struct nfs4_delegreturndata {
4131 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004132 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004133 struct nfs_fh fh;
4134 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004135 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004136 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004137 int rpc_status;
4138};
4139
Trond Myklebustfe650402006-01-03 09:55:18 +01004140static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4141{
4142 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004143
Trond Myklebust14516c32010-07-31 14:29:06 -04004144 if (!nfs4_sequence_done(task, &data->res.seq_res))
4145 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004146
Ricardo Labiaga79708862009-12-07 09:23:21 -05004147 switch (task->tk_status) {
4148 case -NFS4ERR_STALE_STATEID:
4149 case -NFS4ERR_EXPIRED:
4150 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004151 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004152 break;
4153 default:
4154 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4155 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004156 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004157 return;
4158 }
4159 }
4160 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004161}
4162
4163static void nfs4_delegreturn_release(void *calldata)
4164{
Trond Myklebustfe650402006-01-03 09:55:18 +01004165 kfree(calldata);
4166}
4167
Andy Adamson938e1012009-04-01 09:22:28 -04004168#if defined(CONFIG_NFS_V4_1)
4169static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4170{
4171 struct nfs4_delegreturndata *d_data;
4172
4173 d_data = (struct nfs4_delegreturndata *)data;
4174
Trond Myklebust035168a2010-06-16 09:52:26 -04004175 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04004176 &d_data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004177 &d_data->res.seq_res, task))
Andy Adamson938e1012009-04-01 09:22:28 -04004178 return;
4179 rpc_call_start(task);
4180}
4181#endif /* CONFIG_NFS_V4_1 */
4182
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004183static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004184#if defined(CONFIG_NFS_V4_1)
4185 .rpc_call_prepare = nfs4_delegreturn_prepare,
4186#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004187 .rpc_call_done = nfs4_delegreturn_done,
4188 .rpc_release = nfs4_delegreturn_release,
4189};
4190
Trond Myklebuste6f81072008-01-24 18:14:34 -05004191static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004192{
4193 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004194 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004195 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004196 struct rpc_message msg = {
4197 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4198 .rpc_cred = cred,
4199 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004200 struct rpc_task_setup task_setup_data = {
4201 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004202 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004203 .callback_ops = &nfs4_delegreturn_ops,
4204 .flags = RPC_TASK_ASYNC,
4205 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004206 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004207
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004208 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004209 if (data == NULL)
4210 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004211 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004212 data->args.fhandle = &data->fh;
4213 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004214 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004215 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004216 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004217 data->res.fattr = &data->fattr;
4218 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004219 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004220 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004221 data->rpc_status = 0;
4222
Trond Myklebustc970aa82007-07-14 15:39:59 -04004223 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004224 msg.rpc_argp = &data->args;
4225 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004226 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004227 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004228 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004229 if (!issync)
4230 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004231 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004232 if (status != 0)
4233 goto out;
4234 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004235 if (status == 0)
4236 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4237 else
4238 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004239out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004240 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004241 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242}
4243
Trond Myklebuste6f81072008-01-24 18:14:34 -05004244int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245{
4246 struct nfs_server *server = NFS_SERVER(inode);
4247 struct nfs4_exception exception = { };
4248 int err;
4249 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004250 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251 switch (err) {
4252 case -NFS4ERR_STALE_STATEID:
4253 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254 case 0:
4255 return 0;
4256 }
4257 err = nfs4_handle_exception(server, err, &exception);
4258 } while (exception.retry);
4259 return err;
4260}
4261
4262#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4263#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4264
4265/*
4266 * sleep, with exponential backoff, and retry the LOCK operation.
4267 */
4268static unsigned long
4269nfs4_set_lock_task_retry(unsigned long timeout)
4270{
Jeff Laytond3103102011-12-01 22:44:39 +01004271 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272 timeout <<= 1;
4273 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4274 return NFS4_LOCK_MAXTIMEOUT;
4275 return timeout;
4276}
4277
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4279{
4280 struct inode *inode = state->inode;
4281 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004282 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004283 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004285 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004287 struct nfs_lockt_res res = {
4288 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289 };
4290 struct rpc_message msg = {
4291 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4292 .rpc_argp = &arg,
4293 .rpc_resp = &res,
4294 .rpc_cred = state->owner->so_cred,
4295 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 struct nfs4_lock_state *lsp;
4297 int status;
4298
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004299 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004300 status = nfs4_set_lock_state(state, request);
4301 if (status != 0)
4302 goto out;
4303 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004304 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004305 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004306 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004307 switch (status) {
4308 case 0:
4309 request->fl_type = F_UNLCK;
4310 break;
4311 case -NFS4ERR_DENIED:
4312 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004314 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004315out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316 return status;
4317}
4318
4319static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4320{
4321 struct nfs4_exception exception = { };
4322 int err;
4323
4324 do {
4325 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4326 _nfs4_proc_getlk(state, cmd, request),
4327 &exception);
4328 } while (exception.retry);
4329 return err;
4330}
4331
4332static int do_vfs_lock(struct file *file, struct file_lock *fl)
4333{
4334 int res = 0;
4335 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4336 case FL_POSIX:
4337 res = posix_lock_file_wait(file, fl);
4338 break;
4339 case FL_FLOCK:
4340 res = flock_lock_file_wait(file, fl);
4341 break;
4342 default:
4343 BUG();
4344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 return res;
4346}
4347
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004348struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004349 struct nfs_locku_args arg;
4350 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004351 struct nfs4_lock_state *lsp;
4352 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004353 struct file_lock fl;
4354 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004355 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004356};
4357
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004358static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4359 struct nfs_open_context *ctx,
4360 struct nfs4_lock_state *lsp,
4361 struct nfs_seqid *seqid)
4362{
4363 struct nfs4_unlockdata *p;
4364 struct inode *inode = lsp->ls_state->inode;
4365
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004366 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004367 if (p == NULL)
4368 return NULL;
4369 p->arg.fh = NFS_FH(inode);
4370 p->arg.fl = &p->fl;
4371 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004372 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004373 p->arg.stateid = &lsp->ls_stateid;
4374 p->lsp = lsp;
4375 atomic_inc(&lsp->ls_count);
4376 /* Ensure we don't close file until we're done freeing locks! */
4377 p->ctx = get_nfs_open_context(ctx);
4378 memcpy(&p->fl, fl, sizeof(p->fl));
4379 p->server = NFS_SERVER(inode);
4380 return p;
4381}
4382
Trond Myklebust06f814a2006-01-03 09:55:07 +01004383static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004384{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004385 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004386 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004387 nfs4_put_lock_state(calldata->lsp);
4388 put_nfs_open_context(calldata->ctx);
4389 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004390}
4391
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004392static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004393{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004394 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004395
Trond Myklebust14516c32010-07-31 14:29:06 -04004396 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4397 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004398 switch (task->tk_status) {
4399 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004400 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4401 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004402 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004403 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004404 case -NFS4ERR_BAD_STATEID:
4405 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004406 case -NFS4ERR_STALE_STATEID:
4407 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004408 break;
4409 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004410 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004411 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004412 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04004413 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004414}
4415
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004416static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004417{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004418 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004420 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004421 return;
Trond Myklebust795a88c2012-09-10 13:26:49 -04004422 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004423 /* Note: exit _without_ running nfs4_locku_done */
4424 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004425 return;
4426 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004427 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004428 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004429 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004430 &calldata->res.seq_res,
4431 task) != 0)
4432 nfs_release_seqid(calldata->arg.seqid);
4433 else
4434 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435}
4436
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004437static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004438 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004439 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004440 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004441};
4442
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004443static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4444 struct nfs_open_context *ctx,
4445 struct nfs4_lock_state *lsp,
4446 struct nfs_seqid *seqid)
4447{
4448 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004449 struct rpc_message msg = {
4450 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4451 .rpc_cred = ctx->cred,
4452 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004453 struct rpc_task_setup task_setup_data = {
4454 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004455 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004456 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004457 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004458 .flags = RPC_TASK_ASYNC,
4459 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004460
Frank Filz137d6ac2007-07-09 15:32:29 -07004461 /* Ensure this is an unlock - when canceling a lock, the
4462 * canceled lock is passed in, and it won't be an unlock.
4463 */
4464 fl->fl_type = F_UNLCK;
4465
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004466 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4467 if (data == NULL) {
4468 nfs_free_seqid(seqid);
4469 return ERR_PTR(-ENOMEM);
4470 }
4471
Trond Myklebust9d12b212012-01-17 22:04:25 -05004472 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004473 msg.rpc_argp = &data->arg;
4474 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004475 task_setup_data.callback_data = data;
4476 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004477}
4478
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4480{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004481 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004482 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004483 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004484 struct rpc_task *task;
4485 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004486 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487
Trond Myklebust9b073572006-06-29 16:38:34 -04004488 status = nfs4_set_lock_state(state, request);
4489 /* Unlock _before_ we do the RPC call */
4490 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004491 down_read(&nfsi->rwsem);
4492 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4493 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004494 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004495 }
4496 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004497 if (status != 0)
4498 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004499 /* Is this a delegated lock? */
4500 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004501 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004502 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004503 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004504 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004505 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004506 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004507 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004508 status = PTR_ERR(task);
4509 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004510 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004511 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004512 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004513out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004514 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004515 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516}
4517
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004518struct nfs4_lockdata {
4519 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004520 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004521 struct nfs4_lock_state *lsp;
4522 struct nfs_open_context *ctx;
4523 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004524 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004525 int rpc_status;
4526 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004527 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004528};
4529
4530static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004531 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4532 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004533{
4534 struct nfs4_lockdata *p;
4535 struct inode *inode = lsp->ls_state->inode;
4536 struct nfs_server *server = NFS_SERVER(inode);
4537
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004538 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004539 if (p == NULL)
4540 return NULL;
4541
4542 p->arg.fh = NFS_FH(inode);
4543 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004544 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004545 if (p->arg.open_seqid == NULL)
4546 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004547 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004548 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004549 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004550 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004551 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004552 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004553 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004554 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004555 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004556 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004557 atomic_inc(&lsp->ls_count);
4558 p->ctx = get_nfs_open_context(ctx);
4559 memcpy(&p->fl, fl, sizeof(p->fl));
4560 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004561out_free_seqid:
4562 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004563out_free:
4564 kfree(p);
4565 return NULL;
4566}
4567
4568static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4569{
4570 struct nfs4_lockdata *data = calldata;
4571 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572
Harvey Harrison3110ff82008-05-02 13:42:44 -07004573 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004574 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4575 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004576 /* Do we need to do an open_to_lock_owner? */
4577 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004578 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004579 goto out_release_lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004580 data->arg.open_stateid = &state->stateid;
4581 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004582 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004583 } else
4584 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004585 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004586 if (nfs4_setup_sequence(data->server,
4587 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004588 &data->res.seq_res,
4589 task) == 0) {
4590 rpc_call_start(task);
Andy Adamson66179ef2009-04-01 09:22:22 -04004591 return;
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004592 }
4593 nfs_release_seqid(data->arg.open_seqid);
4594out_release_lock_seqid:
4595 nfs_release_seqid(data->arg.lock_seqid);
4596 dprintk("%s: done!, ret = %d\n", __func__, task->tk_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004597}
4598
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004599static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4600{
4601 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4602 nfs4_lock_prepare(task, calldata);
4603}
4604
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004605static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4606{
4607 struct nfs4_lockdata *data = calldata;
4608
Harvey Harrison3110ff82008-05-02 13:42:44 -07004609 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004610
Trond Myklebust14516c32010-07-31 14:29:06 -04004611 if (!nfs4_sequence_done(task, &data->res.seq_res))
4612 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004613
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004614 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004615 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004616 if (data->rpc_status == 0)
4617 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4618 else
4619 goto out;
4620 }
4621 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004622 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004623 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04004624 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004625 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004626out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004627 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004628}
4629
4630static void nfs4_lock_release(void *calldata)
4631{
4632 struct nfs4_lockdata *data = calldata;
4633
Harvey Harrison3110ff82008-05-02 13:42:44 -07004634 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004635 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004636 if (data->cancelled != 0) {
4637 struct rpc_task *task;
4638 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4639 data->arg.lock_seqid);
4640 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004641 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004642 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004643 } else
4644 nfs_free_seqid(data->arg.lock_seqid);
4645 nfs4_put_lock_state(data->lsp);
4646 put_nfs_open_context(data->ctx);
4647 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004648 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004649}
4650
4651static const struct rpc_call_ops nfs4_lock_ops = {
4652 .rpc_call_prepare = nfs4_lock_prepare,
4653 .rpc_call_done = nfs4_lock_done,
4654 .rpc_release = nfs4_lock_release,
4655};
4656
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004657static const struct rpc_call_ops nfs4_recover_lock_ops = {
4658 .rpc_call_prepare = nfs4_recover_lock_prepare,
4659 .rpc_call_done = nfs4_lock_done,
4660 .rpc_release = nfs4_lock_release,
4661};
4662
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004663static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4664{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004665 switch (error) {
4666 case -NFS4ERR_ADMIN_REVOKED:
4667 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004668 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004669 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04004670 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004671 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004672 break;
4673 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004674 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004675 case -NFS4ERR_EXPIRED:
4676 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004677 };
4678}
4679
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004680static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004681{
4682 struct nfs4_lockdata *data;
4683 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004684 struct rpc_message msg = {
4685 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4686 .rpc_cred = state->owner->so_cred,
4687 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004688 struct rpc_task_setup task_setup_data = {
4689 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004690 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004691 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004692 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004693 .flags = RPC_TASK_ASYNC,
4694 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004695 int ret;
4696
Harvey Harrison3110ff82008-05-02 13:42:44 -07004697 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004698 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004699 fl->fl_u.nfs4_fl.owner,
4700 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004701 if (data == NULL)
4702 return -ENOMEM;
4703 if (IS_SETLKW(cmd))
4704 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004705 if (recovery_type > NFS_LOCK_NEW) {
4706 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004707 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004708 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4709 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05004710 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004711 msg.rpc_argp = &data->arg;
4712 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004713 task_setup_data.callback_data = data;
4714 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004715 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004716 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004717 ret = nfs4_wait_for_completion_rpc_task(task);
4718 if (ret == 0) {
4719 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004720 if (ret)
4721 nfs4_handle_setlk_error(data->server, data->lsp,
4722 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004723 } else
4724 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004725 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004726 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004727 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728}
4729
4730static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4731{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004732 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004733 struct nfs4_exception exception = {
4734 .inode = state->inode,
4735 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004736 int err;
4737
4738 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004739 /* Cache the lock if possible... */
4740 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4741 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004742 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004743 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004744 break;
4745 nfs4_handle_exception(server, err, &exception);
4746 } while (exception.retry);
4747 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748}
4749
4750static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4751{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004752 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004753 struct nfs4_exception exception = {
4754 .inode = state->inode,
4755 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004756 int err;
4757
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004758 err = nfs4_set_lock_state(state, request);
4759 if (err != 0)
4760 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004761 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004762 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4763 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004764 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004765 switch (err) {
4766 default:
4767 goto out;
4768 case -NFS4ERR_GRACE:
4769 case -NFS4ERR_DELAY:
4770 nfs4_handle_exception(server, err, &exception);
4771 err = 0;
4772 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004773 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004774out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004775 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776}
4777
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004778#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004779/**
4780 * nfs41_check_expired_locks - possibly free a lock stateid
4781 *
4782 * @state: NFSv4 state for an inode
4783 *
4784 * Returns NFS_OK if recovery for this stateid is now finished.
4785 * Otherwise a negative NFS4ERR value is returned.
4786 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004787static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004788{
Chuck Levereb64cf92012-07-11 16:30:05 -04004789 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004790 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004791 struct nfs_server *server = NFS_SERVER(state->inode);
4792
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004793 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04004794 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004795 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4796 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004797 /* Free the stateid unless the server
4798 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04004799 if (status != -NFS4ERR_BAD_STATEID)
4800 nfs41_free_stateid(server,
4801 &lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004802 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004803 ret = status;
4804 }
4805 }
4806 };
4807
4808 return ret;
4809}
4810
4811static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4812{
4813 int status = NFS_OK;
4814
4815 if (test_bit(LK_STATE_IN_USE, &state->flags))
4816 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04004817 if (status != NFS_OK)
4818 status = nfs4_lock_expired(state, request);
4819 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004820}
4821#endif
4822
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4824{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004825 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004826 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004827 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004829 if ((fl_flags & FL_POSIX) &&
4830 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4831 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004832 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004833 status = nfs4_set_lock_state(state, request);
4834 if (status != 0)
4835 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004836 request->fl_flags |= FL_ACCESS;
4837 status = do_vfs_lock(request->fl_file, request);
4838 if (status < 0)
4839 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004840 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004841 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004842 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004843 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004844 request->fl_flags = fl_flags & ~FL_SLEEP;
4845 status = do_vfs_lock(request->fl_file, request);
4846 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004847 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004848 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004849 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004850 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004851 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004852 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004853 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004854 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4855 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004856out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004857 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004858out:
4859 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860 return status;
4861}
4862
4863static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4864{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004865 struct nfs4_exception exception = {
4866 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04004867 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004868 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869 int err;
4870
4871 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004872 err = _nfs4_proc_setlk(state, cmd, request);
4873 if (err == -NFS4ERR_DENIED)
4874 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004876 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877 } while (exception.retry);
4878 return err;
4879}
4880
4881static int
4882nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4883{
4884 struct nfs_open_context *ctx;
4885 struct nfs4_state *state;
4886 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4887 int status;
4888
4889 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004890 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891 state = ctx->state;
4892
4893 if (request->fl_start < 0 || request->fl_end < 0)
4894 return -EINVAL;
4895
Trond Myklebustd9531262009-07-21 19:22:38 -04004896 if (IS_GETLK(cmd)) {
4897 if (state != NULL)
4898 return nfs4_proc_getlk(state, F_GETLK, request);
4899 return 0;
4900 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901
4902 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4903 return -EINVAL;
4904
Trond Myklebustd9531262009-07-21 19:22:38 -04004905 if (request->fl_type == F_UNLCK) {
4906 if (state != NULL)
4907 return nfs4_proc_unlck(state, cmd, request);
4908 return 0;
4909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910
Trond Myklebustd9531262009-07-21 19:22:38 -04004911 if (state == NULL)
4912 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04004913 /*
4914 * Don't rely on the VFS having checked the file open mode,
4915 * since it won't do this for flock() locks.
4916 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04004917 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04004918 case F_RDLCK:
4919 if (!(filp->f_mode & FMODE_READ))
4920 return -EBADF;
4921 break;
4922 case F_WRLCK:
4923 if (!(filp->f_mode & FMODE_WRITE))
4924 return -EBADF;
4925 }
4926
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927 do {
4928 status = nfs4_proc_setlk(state, cmd, request);
4929 if ((status != -EAGAIN) || IS_SETLK(cmd))
4930 break;
4931 timeout = nfs4_set_lock_task_retry(timeout);
4932 status = -ERESTARTSYS;
4933 if (signalled())
4934 break;
4935 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936 return status;
4937}
4938
Trond Myklebust888e6942005-11-04 15:38:11 -05004939int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4940{
4941 struct nfs_server *server = NFS_SERVER(state->inode);
4942 struct nfs4_exception exception = { };
4943 int err;
4944
4945 err = nfs4_set_lock_state(state, fl);
4946 if (err != 0)
4947 goto out;
4948 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004949 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004950 switch (err) {
4951 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004952 printk(KERN_ERR "NFS: %s: unhandled error "
4953 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004954 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004955 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004956 goto out;
4957 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004958 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07004959 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004960 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004961 nfs4_schedule_lease_recovery(server->nfs_client);
4962 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004963 case -NFS4ERR_BADSESSION:
4964 case -NFS4ERR_BADSLOT:
4965 case -NFS4ERR_BAD_HIGH_SLOT:
4966 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4967 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04004968 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004969 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004970 case -ERESTARTSYS:
4971 /*
4972 * The show must go on: exit, but mark the
4973 * stateid as needing recovery.
4974 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004975 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004976 case -NFS4ERR_ADMIN_REVOKED:
4977 case -NFS4ERR_BAD_STATEID:
4978 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004979 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004980 err = 0;
4981 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004982 case -EKEYEXPIRED:
4983 /*
4984 * User RPCSEC_GSS context has expired.
4985 * We cannot recover this stateid now, so
4986 * skip it and allow recovery thread to
4987 * proceed.
4988 */
4989 err = 0;
4990 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004991 case -ENOMEM:
4992 case -NFS4ERR_DENIED:
4993 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4994 err = 0;
4995 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004996 case -NFS4ERR_DELAY:
4997 break;
4998 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004999 err = nfs4_handle_exception(server, err, &exception);
5000 } while (exception.retry);
5001out:
5002 return err;
5003}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005004
Trond Myklebustcf470c32012-03-07 13:49:12 -05005005struct nfs_release_lockowner_data {
5006 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005007 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005008 struct nfs_release_lockowner_args args;
5009};
5010
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005011static void nfs4_release_lockowner_release(void *calldata)
5012{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005013 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005014 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005015 kfree(calldata);
5016}
5017
Trond Myklebust17280172012-03-11 13:11:00 -04005018static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005019 .rpc_release = nfs4_release_lockowner_release,
5020};
5021
Trond Myklebustcf470c32012-03-07 13:49:12 -05005022int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005023{
5024 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005025 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005026 struct rpc_message msg = {
5027 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5028 };
5029
5030 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005031 return -EINVAL;
5032 data = kmalloc(sizeof(*data), GFP_NOFS);
5033 if (!data)
5034 return -ENOMEM;
5035 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005036 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005037 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5038 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5039 data->args.lock_owner.s_dev = server->s_dev;
5040 msg.rpc_argp = &data->args;
5041 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5042 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005043}
5044
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005045#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5046
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005047static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5048 const void *buf, size_t buflen,
5049 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005050{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005051 if (strcmp(key, "") != 0)
5052 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005053
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005054 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005055}
5056
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005057static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5058 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005059{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005060 if (strcmp(key, "") != 0)
5061 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005062
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005063 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005064}
5065
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005066static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5067 size_t list_len, const char *name,
5068 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005069{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005070 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005071
J. Bruce Fields096455a2006-03-20 23:23:42 -05005072 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5073 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005074
5075 if (list && len <= list_len)
5076 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005077 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005078}
5079
Andy Adamson533eb462011-06-13 18:25:56 -04005080/*
5081 * nfs_fhget will use either the mounted_on_fileid or the fileid
5082 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005083static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5084{
Andy Adamson533eb462011-06-13 18:25:56 -04005085 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5086 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5087 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005088 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005089 return;
5090
5091 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005092 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005093 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5094 fattr->nlink = 2;
5095}
5096
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005097static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5098 const struct qstr *name,
5099 struct nfs4_fs_locations *fs_locations,
5100 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005101{
5102 struct nfs_server *server = NFS_SERVER(dir);
5103 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005104 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005105 };
5106 struct nfs4_fs_locations_arg args = {
5107 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005108 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005109 .page = page,
5110 .bitmask = bitmask,
5111 };
Benny Halevy22958462009-04-01 09:22:02 -04005112 struct nfs4_fs_locations_res res = {
5113 .fs_locations = fs_locations,
5114 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005115 struct rpc_message msg = {
5116 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5117 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005118 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005119 };
5120 int status;
5121
Harvey Harrison3110ff82008-05-02 13:42:44 -07005122 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005123
5124 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5125 * is not supported */
5126 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5127 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5128 else
5129 bitmask[0] |= FATTR4_WORD0_FILEID;
5130
Trond Myklebustc228fd32007-01-13 02:28:11 -05005131 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005132 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005133 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005134 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005135 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005136 return status;
5137}
5138
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005139int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5140 const struct qstr *name,
5141 struct nfs4_fs_locations *fs_locations,
5142 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005143{
5144 struct nfs4_exception exception = { };
5145 int err;
5146 do {
5147 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005148 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005149 &exception);
5150 } while (exception.retry);
5151 return err;
5152}
5153
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005154static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5155{
5156 int status;
5157 struct nfs4_secinfo_arg args = {
5158 .dir_fh = NFS_FH(dir),
5159 .name = name,
5160 };
5161 struct nfs4_secinfo_res res = {
5162 .flavors = flavors,
5163 };
5164 struct rpc_message msg = {
5165 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5166 .rpc_argp = &args,
5167 .rpc_resp = &res,
5168 };
5169
5170 dprintk("NFS call secinfo %s\n", name->name);
5171 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5172 dprintk("NFS reply secinfo: %d\n", status);
5173 return status;
5174}
5175
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005176int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5177 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005178{
5179 struct nfs4_exception exception = { };
5180 int err;
5181 do {
5182 err = nfs4_handle_exception(NFS_SERVER(dir),
5183 _nfs4_proc_secinfo(dir, name, flavors),
5184 &exception);
5185 } while (exception.retry);
5186 return err;
5187}
5188
Andy Adamson557134a2009-04-01 09:21:53 -04005189#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005190/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005191 * Check the exchange flags returned by the server for invalid flags, having
5192 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5193 * DS flags set.
5194 */
5195static int nfs4_check_cl_exchange_flags(u32 flags)
5196{
5197 if (flags & ~EXCHGID4_FLAG_MASK_R)
5198 goto out_inval;
5199 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5200 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5201 goto out_inval;
5202 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5203 goto out_inval;
5204 return NFS_OK;
5205out_inval:
5206 return -NFS4ERR_INVAL;
5207}
5208
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005209static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005210nfs41_same_server_scope(struct nfs41_server_scope *a,
5211 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005212{
5213 if (a->server_scope_sz == b->server_scope_sz &&
5214 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5215 return true;
5216
5217 return false;
5218}
5219
Andy Adamson357f54d2010-12-14 10:11:57 -05005220/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005221 * nfs4_proc_bind_conn_to_session()
5222 *
5223 * The 4.1 client currently uses the same TCP connection for the
5224 * fore and backchannel.
5225 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005226int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005227{
5228 int status;
5229 struct nfs41_bind_conn_to_session_res res;
5230 struct rpc_message msg = {
5231 .rpc_proc =
5232 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5233 .rpc_argp = clp,
5234 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005235 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005236 };
5237
5238 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005239
5240 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5241 if (unlikely(res.session == NULL)) {
5242 status = -ENOMEM;
5243 goto out;
5244 }
5245
5246 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5247 if (status == 0) {
5248 if (memcmp(res.session->sess_id.data,
5249 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5250 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5251 status = -EIO;
5252 goto out_session;
5253 }
5254 if (res.dir != NFS4_CDFS4_BOTH) {
5255 dprintk("NFS: %s: Unexpected direction from server\n",
5256 __func__);
5257 status = -EIO;
5258 goto out_session;
5259 }
5260 if (res.use_conn_in_rdma_mode) {
5261 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5262 __func__);
5263 status = -EIO;
5264 goto out_session;
5265 }
5266 }
5267out_session:
5268 kfree(res.session);
5269out:
5270 dprintk("<-- %s status= %d\n", __func__, status);
5271 return status;
5272}
5273
5274/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005275 * nfs4_proc_exchange_id()
5276 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005277 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5278 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04005279 * Since the clientid has expired, all compounds using sessions
5280 * associated with the stale clientid will be returning
5281 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5282 * be in some phase of session reset.
5283 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005284int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005285{
5286 nfs4_verifier verifier;
5287 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005288 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005289 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005290 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005291 };
5292 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005293 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005294 };
5295 int status;
5296 struct rpc_message msg = {
5297 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5298 .rpc_argp = &args,
5299 .rpc_resp = &res,
5300 .rpc_cred = cred,
5301 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005302
Chuck Leverf0920752012-05-21 22:45:41 -04005303 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04005304 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5305 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005306 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5307 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5308 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005309
Chuck Leveracdeb692012-05-21 22:46:16 -04005310 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005311 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005312 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005313 status = -ENOMEM;
5314 goto out;
5315 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005316
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005317 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005318 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005319 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005320 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005321 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005322 }
5323
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005324 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005325 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005326 status = -ENOMEM;
5327 goto out_server_scope;
5328 }
5329
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005330 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005331 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005332 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005333
Chuck Lever177313f2012-05-21 22:44:58 -04005334 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005335 clp->cl_clientid = res.clientid;
5336 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5337 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5338 clp->cl_seqid = res.seqid;
5339
Chuck Leveracdeb692012-05-21 22:46:16 -04005340 kfree(clp->cl_serverowner);
5341 clp->cl_serverowner = res.server_owner;
5342 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005343
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005344 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005345 kfree(clp->cl_implid);
5346 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005347
Chuck Lever177313f2012-05-21 22:44:58 -04005348 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005349 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005350 res.server_scope)) {
5351 dprintk("%s: server_scope mismatch detected\n",
5352 __func__);
5353 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005354 kfree(clp->cl_serverscope);
5355 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005356 }
5357
Chuck Lever177313f2012-05-21 22:44:58 -04005358 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005359 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005360 goto out;
5361 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005362 } else
5363 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005364
Chuck Leveracdeb692012-05-21 22:46:16 -04005365out_server_owner:
5366 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005367out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005368 kfree(res.server_scope);
5369out:
Chuck Lever177313f2012-05-21 22:44:58 -04005370 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005371 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005372 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005373 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04005374 clp->cl_implid->date.seconds,
5375 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005376 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005377 return status;
5378}
5379
Trond Myklebust66245532012-05-25 17:18:09 -04005380static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5381 struct rpc_cred *cred)
5382{
5383 struct rpc_message msg = {
5384 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5385 .rpc_argp = clp,
5386 .rpc_cred = cred,
5387 };
5388 int status;
5389
5390 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5391 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04005392 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04005393 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5394 return status;
5395}
5396
5397static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5398 struct rpc_cred *cred)
5399{
5400 unsigned int loop;
5401 int ret;
5402
5403 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5404 ret = _nfs4_proc_destroy_clientid(clp, cred);
5405 switch (ret) {
5406 case -NFS4ERR_DELAY:
5407 case -NFS4ERR_CLIENTID_BUSY:
5408 ssleep(1);
5409 break;
5410 default:
5411 return ret;
5412 }
5413 }
5414 return 0;
5415}
5416
5417int nfs4_destroy_clientid(struct nfs_client *clp)
5418{
5419 struct rpc_cred *cred;
5420 int ret = 0;
5421
5422 if (clp->cl_mvops->minor_version < 1)
5423 goto out;
5424 if (clp->cl_exchange_flags == 0)
5425 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04005426 if (clp->cl_preserve_clid)
5427 goto out;
Trond Myklebust66245532012-05-25 17:18:09 -04005428 cred = nfs4_get_exchange_id_cred(clp);
5429 ret = nfs4_proc_destroy_clientid(clp, cred);
5430 if (cred)
5431 put_rpccred(cred);
5432 switch (ret) {
5433 case 0:
5434 case -NFS4ERR_STALE_CLIENTID:
5435 clp->cl_exchange_flags = 0;
5436 }
5437out:
5438 return ret;
5439}
5440
Andy Adamson2050f0c2009-04-01 09:22:30 -04005441struct nfs4_get_lease_time_data {
5442 struct nfs4_get_lease_time_args *args;
5443 struct nfs4_get_lease_time_res *res;
5444 struct nfs_client *clp;
5445};
5446
5447static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5448 void *calldata)
5449{
5450 int ret;
5451 struct nfs4_get_lease_time_data *data =
5452 (struct nfs4_get_lease_time_data *)calldata;
5453
5454 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005455 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005456 /* just setup sequence, do not trigger session recovery
5457 since we're invoked within one */
5458 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005459 &data->args->la_seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005460 &data->res->lr_seq_res, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005461
Trond Myklebustd3edcf92012-10-15 13:14:43 -04005462 if (ret != -EAGAIN)
5463 rpc_call_start(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005464 dprintk("<-- %s\n", __func__);
5465}
5466
5467/*
5468 * Called from nfs4_state_manager thread for session setup, so don't recover
5469 * from sequence operation or clientid errors.
5470 */
5471static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5472{
5473 struct nfs4_get_lease_time_data *data =
5474 (struct nfs4_get_lease_time_data *)calldata;
5475
5476 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005477 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5478 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005479 switch (task->tk_status) {
5480 case -NFS4ERR_DELAY:
5481 case -NFS4ERR_GRACE:
5482 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5483 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5484 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005485 /* fall through */
5486 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005487 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005488 return;
5489 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005490 dprintk("<-- %s\n", __func__);
5491}
5492
Trond Myklebust17280172012-03-11 13:11:00 -04005493static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005494 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5495 .rpc_call_done = nfs4_get_lease_time_done,
5496};
5497
5498int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5499{
5500 struct rpc_task *task;
5501 struct nfs4_get_lease_time_args args;
5502 struct nfs4_get_lease_time_res res = {
5503 .lr_fsinfo = fsinfo,
5504 };
5505 struct nfs4_get_lease_time_data data = {
5506 .args = &args,
5507 .res = &res,
5508 .clp = clp,
5509 };
5510 struct rpc_message msg = {
5511 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5512 .rpc_argp = &args,
5513 .rpc_resp = &res,
5514 };
5515 struct rpc_task_setup task_setup = {
5516 .rpc_client = clp->cl_rpcclient,
5517 .rpc_message = &msg,
5518 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005519 .callback_data = &data,
5520 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005521 };
5522 int status;
5523
Trond Myklebust9d12b212012-01-17 22:04:25 -05005524 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005525 dprintk("--> %s\n", __func__);
5526 task = rpc_run_task(&task_setup);
5527
5528 if (IS_ERR(task))
5529 status = PTR_ERR(task);
5530 else {
5531 status = task->tk_status;
5532 rpc_put_task(task);
5533 }
5534 dprintk("<-- %s return %d\n", __func__, status);
5535
5536 return status;
5537}
5538
Andy Adamsonfc931582009-04-01 09:22:31 -04005539/*
5540 * Initialize the values to be used by the client in CREATE_SESSION
5541 * If nfs4_init_session set the fore channel request and response sizes,
5542 * use them.
5543 *
5544 * Set the back channel max_resp_sz_cached to zero to force the client to
5545 * always set csa_cachethis to FALSE because the current implementation
5546 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5547 */
5548static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5549{
5550 struct nfs4_session *session = args->client->cl_session;
Trond Myklebustae72ae672012-11-20 11:02:55 -05005551 unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
5552 mxresp_sz = session->fc_target_max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005553
5554 if (mxrqst_sz == 0)
5555 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5556 if (mxresp_sz == 0)
5557 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5558 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005559 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5560 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005561 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005562 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005563
5564 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005565 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005566 __func__,
5567 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005568 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005569
5570 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005571 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5572 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5573 args->bc_attrs.max_resp_sz_cached = 0;
5574 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5575 args->bc_attrs.max_reqs = 1;
5576
5577 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5578 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5579 __func__,
5580 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5581 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5582 args->bc_attrs.max_reqs);
5583}
5584
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005585static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005586{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005587 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5588 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5589
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005590 if (rcvd->max_resp_sz > sent->max_resp_sz)
5591 return -EINVAL;
5592 /*
5593 * Our requested max_ops is the minimum we need; we're not
5594 * prepared to break up compounds into smaller pieces than that.
5595 * So, no point even trying to continue if the server won't
5596 * cooperate:
5597 */
5598 if (rcvd->max_ops < sent->max_ops)
5599 return -EINVAL;
5600 if (rcvd->max_reqs == 0)
5601 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005602 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5603 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005604 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005605}
5606
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005607static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5608{
5609 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5610 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005611
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005612 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5613 return -EINVAL;
5614 if (rcvd->max_resp_sz < sent->max_resp_sz)
5615 return -EINVAL;
5616 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5617 return -EINVAL;
5618 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005619 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005620 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005621 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005622 return -EINVAL;
5623 return 0;
5624}
Andy Adamson8d353012009-04-01 09:22:32 -04005625
Andy Adamson8d353012009-04-01 09:22:32 -04005626static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5627 struct nfs4_session *session)
5628{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005629 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005630
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005631 ret = nfs4_verify_fore_channel_attrs(args, session);
5632 if (ret)
5633 return ret;
5634 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005635}
5636
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005637static int _nfs4_proc_create_session(struct nfs_client *clp,
5638 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005639{
5640 struct nfs4_session *session = clp->cl_session;
5641 struct nfs41_create_session_args args = {
5642 .client = clp,
5643 .cb_program = NFS4_CALLBACK,
5644 };
5645 struct nfs41_create_session_res res = {
5646 .client = clp,
5647 };
5648 struct rpc_message msg = {
5649 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5650 .rpc_argp = &args,
5651 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005652 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04005653 };
5654 int status;
5655
5656 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005657 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005658
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005659 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005660
Trond Myklebust43095d32012-11-20 11:13:12 -05005661 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04005662 /* Verify the session's negotiated channel_attrs values */
5663 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005664 /* Increment the clientid slot sequence id */
5665 clp->cl_seqid++;
5666 }
5667
5668 return status;
5669}
5670
5671/*
5672 * Issues a CREATE_SESSION operation to the server.
5673 * It is the responsibility of the caller to verify the session is
5674 * expired before calling this routine.
5675 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005676int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005677{
5678 int status;
5679 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005680 struct nfs4_session *session = clp->cl_session;
5681
5682 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5683
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005684 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04005685 if (status)
5686 goto out;
5687
Andy Adamsonaacd5532011-11-09 13:58:21 -05005688 /* Init or reset the session slot tables */
5689 status = nfs4_setup_session_slot_tables(session);
5690 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005691 if (status)
5692 goto out;
5693
5694 ptr = (unsigned *)&session->sess_id.data[0];
5695 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5696 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005697out:
5698 dprintk("<-- %s\n", __func__);
5699 return status;
5700}
5701
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005702/*
5703 * Issue the over-the-wire RPC DESTROY_SESSION.
5704 * The caller must serialize access to this routine.
5705 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005706int nfs4_proc_destroy_session(struct nfs4_session *session,
5707 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005708{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005709 struct rpc_message msg = {
5710 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5711 .rpc_argp = session,
5712 .rpc_cred = cred,
5713 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005714 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005715
5716 dprintk("--> nfs4_proc_destroy_session\n");
5717
5718 /* session is still being setup */
5719 if (session->clp->cl_cons_state != NFS_CS_READY)
5720 return status;
5721
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005722 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005723
5724 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04005725 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005726 "Session has been destroyed regardless...\n", status);
5727
5728 dprintk("<-- nfs4_proc_destroy_session\n");
5729 return status;
5730}
5731
Trond Myklebust7b38c362012-05-23 13:23:31 -04005732/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005733 * Renew the cl_session lease.
5734 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005735struct nfs4_sequence_data {
5736 struct nfs_client *clp;
5737 struct nfs4_sequence_args args;
5738 struct nfs4_sequence_res res;
5739};
5740
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005741static void nfs41_sequence_release(void *data)
5742{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005743 struct nfs4_sequence_data *calldata = data;
5744 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005745
Alexandros Batsakis71358402010-02-05 03:45:05 -08005746 if (atomic_read(&clp->cl_count) > 1)
5747 nfs4_schedule_state_renewal(clp);
5748 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005749 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005750}
5751
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005752static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5753{
5754 switch(task->tk_status) {
5755 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005756 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5757 return -EAGAIN;
5758 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005759 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005760 }
5761 return 0;
5762}
5763
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005764static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005765{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005766 struct nfs4_sequence_data *calldata = data;
5767 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005768
Trond Myklebust14516c32010-07-31 14:29:06 -04005769 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5770 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005771
5772 if (task->tk_status < 0) {
5773 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005774 if (atomic_read(&clp->cl_count) == 1)
5775 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005776
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005777 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5778 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005779 return;
5780 }
5781 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005782 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005783out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005784 dprintk("<-- %s\n", __func__);
5785}
5786
5787static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5788{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005789 struct nfs4_sequence_data *calldata = data;
5790 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005791 struct nfs4_sequence_args *args;
5792 struct nfs4_sequence_res *res;
5793
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005794 args = task->tk_msg.rpc_argp;
5795 res = task->tk_msg.rpc_resp;
5796
Trond Myklebust9d12b212012-01-17 22:04:25 -05005797 if (nfs41_setup_sequence(clp->cl_session, args, res, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005798 return;
5799 rpc_call_start(task);
5800}
5801
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05005802static void nfs41_sequence_prepare_privileged(struct rpc_task *task, void *data)
5803{
5804 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
5805 nfs41_sequence_prepare(task, data);
5806}
5807
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005808static const struct rpc_call_ops nfs41_sequence_ops = {
5809 .rpc_call_done = nfs41_sequence_call_done,
5810 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005811 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005812};
5813
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05005814static const struct rpc_call_ops nfs41_sequence_privileged_ops = {
5815 .rpc_call_done = nfs41_sequence_call_done,
5816 .rpc_call_prepare = nfs41_sequence_prepare_privileged,
5817 .rpc_release = nfs41_sequence_release,
5818};
5819
5820static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred,
5821 const struct rpc_call_ops *seq_ops)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005822{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005823 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005824 struct rpc_message msg = {
5825 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5826 .rpc_cred = cred,
5827 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005828 struct rpc_task_setup task_setup_data = {
5829 .rpc_client = clp->cl_rpcclient,
5830 .rpc_message = &msg,
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05005831 .callback_ops = seq_ops,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005832 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5833 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005834
Alexandros Batsakis71358402010-02-05 03:45:05 -08005835 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005836 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005837 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005838 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005839 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005840 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005841 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05005842 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005843 msg.rpc_argp = &calldata->args;
5844 msg.rpc_resp = &calldata->res;
5845 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005846 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005847
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005848 return rpc_run_task(&task_setup_data);
5849}
5850
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005851static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005852{
5853 struct rpc_task *task;
5854 int ret = 0;
5855
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005856 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5857 return 0;
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05005858 task = _nfs41_proc_sequence(clp, cred, &nfs41_sequence_ops);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005859 if (IS_ERR(task))
5860 ret = PTR_ERR(task);
5861 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005862 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005863 dprintk("<-- %s status=%d\n", __func__, ret);
5864 return ret;
5865}
5866
5867static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5868{
5869 struct rpc_task *task;
5870 int ret;
5871
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05005872 task = _nfs41_proc_sequence(clp, cred, &nfs41_sequence_privileged_ops);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005873 if (IS_ERR(task)) {
5874 ret = PTR_ERR(task);
5875 goto out;
5876 }
5877 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005878 if (!ret) {
5879 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5880
5881 if (task->tk_status == 0)
5882 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005883 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005884 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005885 rpc_put_task(task);
5886out:
5887 dprintk("<-- %s status=%d\n", __func__, ret);
5888 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005889}
5890
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005891struct nfs4_reclaim_complete_data {
5892 struct nfs_client *clp;
5893 struct nfs41_reclaim_complete_args arg;
5894 struct nfs41_reclaim_complete_res res;
5895};
5896
5897static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5898{
5899 struct nfs4_reclaim_complete_data *calldata = data;
5900
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005901 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04005902 if (nfs41_setup_sequence(calldata->clp->cl_session,
5903 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005904 &calldata->res.seq_res, task))
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005905 return;
5906
5907 rpc_call_start(task);
5908}
5909
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005910static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5911{
5912 switch(task->tk_status) {
5913 case 0:
5914 case -NFS4ERR_COMPLETE_ALREADY:
5915 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5916 break;
5917 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005918 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005919 /* fall through */
5920 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005921 return -EAGAIN;
5922 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005923 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005924 }
5925 return 0;
5926}
5927
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005928static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5929{
5930 struct nfs4_reclaim_complete_data *calldata = data;
5931 struct nfs_client *clp = calldata->clp;
5932 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5933
5934 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005935 if (!nfs41_sequence_done(task, res))
5936 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005937
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005938 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5939 rpc_restart_call_prepare(task);
5940 return;
5941 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005942 dprintk("<-- %s\n", __func__);
5943}
5944
5945static void nfs4_free_reclaim_complete_data(void *data)
5946{
5947 struct nfs4_reclaim_complete_data *calldata = data;
5948
5949 kfree(calldata);
5950}
5951
5952static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5953 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5954 .rpc_call_done = nfs4_reclaim_complete_done,
5955 .rpc_release = nfs4_free_reclaim_complete_data,
5956};
5957
5958/*
5959 * Issue a global reclaim complete.
5960 */
5961static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5962{
5963 struct nfs4_reclaim_complete_data *calldata;
5964 struct rpc_task *task;
5965 struct rpc_message msg = {
5966 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5967 };
5968 struct rpc_task_setup task_setup_data = {
5969 .rpc_client = clp->cl_rpcclient,
5970 .rpc_message = &msg,
5971 .callback_ops = &nfs4_reclaim_complete_call_ops,
5972 .flags = RPC_TASK_ASYNC,
5973 };
5974 int status = -ENOMEM;
5975
5976 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005977 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005978 if (calldata == NULL)
5979 goto out;
5980 calldata->clp = clp;
5981 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005982
Trond Myklebust9d12b212012-01-17 22:04:25 -05005983 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005984 msg.rpc_argp = &calldata->arg;
5985 msg.rpc_resp = &calldata->res;
5986 task_setup_data.callback_data = calldata;
5987 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005988 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005989 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005990 goto out;
5991 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05005992 status = nfs4_wait_for_completion_rpc_task(task);
5993 if (status == 0)
5994 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005995 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005996 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005997out:
5998 dprintk("<-- %s status=%d\n", __func__, status);
5999 return status;
6000}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006001
6002static void
6003nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6004{
6005 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00006006 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006007
6008 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00006009 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6010 * right now covering the LAYOUTGET we are about to send.
6011 * However, that is not so catastrophic, and there seems
6012 * to be no way to prevent it completely.
6013 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006014 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006015 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006016 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006017 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6018 NFS_I(lgp->args.inode)->layout,
6019 lgp->args.ctx->state)) {
6020 rpc_exit(task, NFS4_OK);
6021 return;
6022 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006023 rpc_call_start(task);
6024}
6025
6026static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6027{
6028 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07006029 struct inode *inode = lgp->args.inode;
6030 struct nfs_server *server = NFS_SERVER(inode);
6031 struct pnfs_layout_hdr *lo;
6032 struct nfs4_state *state = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006033
6034 dprintk("--> %s\n", __func__);
6035
6036 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07006037 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006038
6039 switch (task->tk_status) {
6040 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07006041 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006042 case -NFS4ERR_LAYOUTTRYLATER:
6043 case -NFS4ERR_RECALLCONFLICT:
6044 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07006045 break;
6046 case -NFS4ERR_EXPIRED:
6047 case -NFS4ERR_BAD_STATEID:
6048 spin_lock(&inode->i_lock);
6049 lo = NFS_I(inode)->layout;
6050 if (!lo || list_empty(&lo->plh_segs)) {
6051 spin_unlock(&inode->i_lock);
6052 /* If the open stateid was bad, then recover it. */
6053 state = lgp->args.ctx->state;
6054 } else {
6055 LIST_HEAD(head);
6056
6057 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6058 spin_unlock(&inode->i_lock);
6059 /* Mark the bad layout state as invalid, then
6060 * retry using the open stateid. */
6061 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006062 }
6063 }
Trond Myklebustee314c22012-10-01 17:25:48 -07006064 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6065 rpc_restart_call_prepare(task);
6066out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006067 dprintk("<-- %s\n", __func__);
6068}
6069
Idan Kedar85541162012-08-02 11:47:10 +03006070static size_t max_response_pages(struct nfs_server *server)
6071{
6072 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6073 return nfs_page_array_len(0, max_resp_sz);
6074}
6075
6076static void nfs4_free_pages(struct page **pages, size_t size)
6077{
6078 int i;
6079
6080 if (!pages)
6081 return;
6082
6083 for (i = 0; i < size; i++) {
6084 if (!pages[i])
6085 break;
6086 __free_page(pages[i]);
6087 }
6088 kfree(pages);
6089}
6090
6091static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6092{
6093 struct page **pages;
6094 int i;
6095
6096 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6097 if (!pages) {
6098 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6099 return NULL;
6100 }
6101
6102 for (i = 0; i < size; i++) {
6103 pages[i] = alloc_page(gfp_flags);
6104 if (!pages[i]) {
6105 dprintk("%s: failed to allocate page\n", __func__);
6106 nfs4_free_pages(pages, size);
6107 return NULL;
6108 }
6109 }
6110
6111 return pages;
6112}
6113
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006114static void nfs4_layoutget_release(void *calldata)
6115{
6116 struct nfs4_layoutget *lgp = calldata;
Idan Kedar85541162012-08-02 11:47:10 +03006117 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6118 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006119
6120 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006121 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006122 put_nfs_open_context(lgp->args.ctx);
6123 kfree(calldata);
6124 dprintk("<-- %s\n", __func__);
6125}
6126
6127static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6128 .rpc_call_prepare = nfs4_layoutget_prepare,
6129 .rpc_call_done = nfs4_layoutget_done,
6130 .rpc_release = nfs4_layoutget_release,
6131};
6132
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006133struct pnfs_layout_segment *
6134nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006135{
6136 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Idan Kedar85541162012-08-02 11:47:10 +03006137 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006138 struct rpc_task *task;
6139 struct rpc_message msg = {
6140 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6141 .rpc_argp = &lgp->args,
6142 .rpc_resp = &lgp->res,
6143 };
6144 struct rpc_task_setup task_setup_data = {
6145 .rpc_client = server->client,
6146 .rpc_message = &msg,
6147 .callback_ops = &nfs4_layoutget_call_ops,
6148 .callback_data = lgp,
6149 .flags = RPC_TASK_ASYNC,
6150 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006151 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006152 int status = 0;
6153
6154 dprintk("--> %s\n", __func__);
6155
Idan Kedar85541162012-08-02 11:47:10 +03006156 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6157 if (!lgp->args.layout.pages) {
6158 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006159 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006160 }
6161 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
6162
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006163 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006164 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006165 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006166 task = rpc_run_task(&task_setup_data);
6167 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006168 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006169 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006170 if (status == 0)
6171 status = task->tk_status;
6172 if (status == 0)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006173 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006174 rpc_put_task(task);
6175 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006176 if (status)
6177 return ERR_PTR(status);
6178 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006179}
6180
Benny Halevycbe82602011-05-22 19:52:37 +03006181static void
6182nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6183{
6184 struct nfs4_layoutreturn *lrp = calldata;
6185
6186 dprintk("--> %s\n", __func__);
6187 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006188 &lrp->res.seq_res, task))
Benny Halevycbe82602011-05-22 19:52:37 +03006189 return;
6190 rpc_call_start(task);
6191}
6192
6193static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6194{
6195 struct nfs4_layoutreturn *lrp = calldata;
6196 struct nfs_server *server;
6197
6198 dprintk("--> %s\n", __func__);
6199
6200 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
6201 return;
6202
6203 server = NFS_SERVER(lrp->args.inode);
6204 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006205 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006206 return;
6207 }
Benny Halevycbe82602011-05-22 19:52:37 +03006208 dprintk("<-- %s\n", __func__);
6209}
6210
6211static void nfs4_layoutreturn_release(void *calldata)
6212{
6213 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04006214 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006215
6216 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04006217 spin_lock(&lo->plh_inode->i_lock);
6218 if (lrp->res.lrs_present)
6219 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6220 lo->plh_block_lgets--;
6221 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006222 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006223 kfree(calldata);
6224 dprintk("<-- %s\n", __func__);
6225}
6226
6227static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6228 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6229 .rpc_call_done = nfs4_layoutreturn_done,
6230 .rpc_release = nfs4_layoutreturn_release,
6231};
6232
6233int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6234{
6235 struct rpc_task *task;
6236 struct rpc_message msg = {
6237 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6238 .rpc_argp = &lrp->args,
6239 .rpc_resp = &lrp->res,
6240 };
6241 struct rpc_task_setup task_setup_data = {
6242 .rpc_client = lrp->clp->cl_rpcclient,
6243 .rpc_message = &msg,
6244 .callback_ops = &nfs4_layoutreturn_call_ops,
6245 .callback_data = lrp,
6246 };
6247 int status;
6248
6249 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006250 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006251 task = rpc_run_task(&task_setup_data);
6252 if (IS_ERR(task))
6253 return PTR_ERR(task);
6254 status = task->tk_status;
6255 dprintk("<-- %s status=%d\n", __func__, status);
6256 rpc_put_task(task);
6257 return status;
6258}
6259
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006260/*
6261 * Retrieve the list of Data Server devices from the MDS.
6262 */
6263static int _nfs4_getdevicelist(struct nfs_server *server,
6264 const struct nfs_fh *fh,
6265 struct pnfs_devicelist *devlist)
6266{
6267 struct nfs4_getdevicelist_args args = {
6268 .fh = fh,
6269 .layoutclass = server->pnfs_curr_ld->id,
6270 };
6271 struct nfs4_getdevicelist_res res = {
6272 .devlist = devlist,
6273 };
6274 struct rpc_message msg = {
6275 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6276 .rpc_argp = &args,
6277 .rpc_resp = &res,
6278 };
6279 int status;
6280
6281 dprintk("--> %s\n", __func__);
6282 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6283 &res.seq_res, 0);
6284 dprintk("<-- %s status=%d\n", __func__, status);
6285 return status;
6286}
6287
6288int nfs4_proc_getdevicelist(struct nfs_server *server,
6289 const struct nfs_fh *fh,
6290 struct pnfs_devicelist *devlist)
6291{
6292 struct nfs4_exception exception = { };
6293 int err;
6294
6295 do {
6296 err = nfs4_handle_exception(server,
6297 _nfs4_getdevicelist(server, fh, devlist),
6298 &exception);
6299 } while (exception.retry);
6300
6301 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6302 err, devlist->num_devs);
6303
6304 return err;
6305}
6306EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6307
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006308static int
6309_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6310{
6311 struct nfs4_getdeviceinfo_args args = {
6312 .pdev = pdev,
6313 };
6314 struct nfs4_getdeviceinfo_res res = {
6315 .pdev = pdev,
6316 };
6317 struct rpc_message msg = {
6318 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6319 .rpc_argp = &args,
6320 .rpc_resp = &res,
6321 };
6322 int status;
6323
6324 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006325 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006326 dprintk("<-- %s status=%d\n", __func__, status);
6327
6328 return status;
6329}
6330
6331int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6332{
6333 struct nfs4_exception exception = { };
6334 int err;
6335
6336 do {
6337 err = nfs4_handle_exception(server,
6338 _nfs4_proc_getdeviceinfo(server, pdev),
6339 &exception);
6340 } while (exception.retry);
6341 return err;
6342}
6343EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6344
Andy Adamson863a3c62011-03-23 13:27:54 +00006345static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6346{
6347 struct nfs4_layoutcommit_data *data = calldata;
6348 struct nfs_server *server = NFS_SERVER(data->args.inode);
6349
6350 if (nfs4_setup_sequence(server, &data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006351 &data->res.seq_res, task))
Andy Adamson863a3c62011-03-23 13:27:54 +00006352 return;
6353 rpc_call_start(task);
6354}
6355
6356static void
6357nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6358{
6359 struct nfs4_layoutcommit_data *data = calldata;
6360 struct nfs_server *server = NFS_SERVER(data->args.inode);
6361
6362 if (!nfs4_sequence_done(task, &data->res.seq_res))
6363 return;
6364
6365 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006366 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6367 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6368 case -NFS4ERR_BADLAYOUT: /* no layout */
6369 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006370 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006371 break;
6372 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006373 nfs_post_op_update_inode_force_wcc(data->args.inode,
6374 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006375 break;
6376 default:
6377 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6378 rpc_restart_call_prepare(task);
6379 return;
6380 }
6381 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006382}
6383
6384static void nfs4_layoutcommit_release(void *calldata)
6385{
6386 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04006387 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07006388 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00006389
Andy Adamsondb29c082011-07-30 20:52:38 -04006390 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006391 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04006392 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6393 list_del_init(&lseg->pls_lc_list);
6394 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6395 &lseg->pls_flags))
Trond Myklebust9369a432012-09-18 20:57:08 -04006396 pnfs_put_lseg(lseg);
Peng Taoa9bae562011-07-30 20:52:33 -04006397 }
Peng Tao92407e72011-10-23 20:21:17 -07006398
6399 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6400 smp_mb__after_clear_bit();
6401 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6402
Andy Adamson863a3c62011-03-23 13:27:54 +00006403 put_rpccred(data->cred);
6404 kfree(data);
6405}
6406
6407static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6408 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6409 .rpc_call_done = nfs4_layoutcommit_done,
6410 .rpc_release = nfs4_layoutcommit_release,
6411};
6412
6413int
Andy Adamsonef311532011-03-12 02:58:10 -05006414nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006415{
6416 struct rpc_message msg = {
6417 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6418 .rpc_argp = &data->args,
6419 .rpc_resp = &data->res,
6420 .rpc_cred = data->cred,
6421 };
6422 struct rpc_task_setup task_setup_data = {
6423 .task = &data->task,
6424 .rpc_client = NFS_CLIENT(data->args.inode),
6425 .rpc_message = &msg,
6426 .callback_ops = &nfs4_layoutcommit_ops,
6427 .callback_data = data,
6428 .flags = RPC_TASK_ASYNC,
6429 };
6430 struct rpc_task *task;
6431 int status = 0;
6432
6433 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6434 "lbw: %llu inode %lu\n",
6435 data->task.tk_pid, sync,
6436 data->args.lastbytewritten,
6437 data->args.inode->i_ino);
6438
Trond Myklebust9d12b212012-01-17 22:04:25 -05006439 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006440 task = rpc_run_task(&task_setup_data);
6441 if (IS_ERR(task))
6442 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006443 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006444 goto out;
6445 status = nfs4_wait_for_completion_rpc_task(task);
6446 if (status != 0)
6447 goto out;
6448 status = task->tk_status;
6449out:
6450 dprintk("%s: status %d\n", __func__, status);
6451 rpc_put_task(task);
6452 return status;
6453}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006454
6455static int
6456_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6457 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6458{
6459 struct nfs41_secinfo_no_name_args args = {
6460 .style = SECINFO_STYLE_CURRENT_FH,
6461 };
6462 struct nfs4_secinfo_res res = {
6463 .flavors = flavors,
6464 };
6465 struct rpc_message msg = {
6466 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6467 .rpc_argp = &args,
6468 .rpc_resp = &res,
6469 };
6470 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6471}
6472
6473static int
6474nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6475 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6476{
6477 struct nfs4_exception exception = { };
6478 int err;
6479 do {
6480 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6481 switch (err) {
6482 case 0:
6483 case -NFS4ERR_WRONGSEC:
6484 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006485 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006486 default:
6487 err = nfs4_handle_exception(server, err, &exception);
6488 }
6489 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006490out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006491 return err;
6492}
6493
6494static int
6495nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6496 struct nfs_fsinfo *info)
6497{
6498 int err;
6499 struct page *page;
6500 rpc_authflavor_t flavor;
6501 struct nfs4_secinfo_flavors *flavors;
6502
6503 page = alloc_page(GFP_KERNEL);
6504 if (!page) {
6505 err = -ENOMEM;
6506 goto out;
6507 }
6508
6509 flavors = page_address(page);
6510 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6511
6512 /*
6513 * Fall back on "guess and check" method if
6514 * the server doesn't support SECINFO_NO_NAME
6515 */
6516 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6517 err = nfs4_find_root_sec(server, fhandle, info);
6518 goto out_freepage;
6519 }
6520 if (err)
6521 goto out_freepage;
6522
6523 flavor = nfs_find_best_sec(flavors);
6524 if (err == 0)
6525 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6526
6527out_freepage:
6528 put_page(page);
6529 if (err == -EACCES)
6530 return -EPERM;
6531out:
6532 return err;
6533}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006534
6535static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006536{
6537 int status;
6538 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006539 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006540 };
6541 struct nfs41_test_stateid_res res;
6542 struct rpc_message msg = {
6543 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6544 .rpc_argp = &args,
6545 .rpc_resp = &res,
6546 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006547
Chuck Lever38527b12012-07-11 16:30:23 -04006548 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006549 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006550 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Chuck Lever38527b12012-07-11 16:30:23 -04006551 if (status != NFS_OK) {
6552 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04006553 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04006554 }
6555 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04006556 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006557}
6558
Chuck Lever38527b12012-07-11 16:30:23 -04006559/**
6560 * nfs41_test_stateid - perform a TEST_STATEID operation
6561 *
6562 * @server: server / transport on which to perform the operation
6563 * @stateid: state ID to test
6564 *
6565 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6566 * Otherwise a negative NFS4ERR value is returned if the operation
6567 * failed or the state ID is not currently valid.
6568 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006569static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006570{
6571 struct nfs4_exception exception = { };
6572 int err;
6573 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006574 err = _nfs41_test_stateid(server, stateid);
6575 if (err != -NFS4ERR_DELAY)
6576 break;
6577 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04006578 } while (exception.retry);
6579 return err;
6580}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006581
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006582static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006583{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006584 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006585 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006586 };
6587 struct nfs41_free_stateid_res res;
6588 struct rpc_message msg = {
6589 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6590 .rpc_argp = &args,
6591 .rpc_resp = &res,
6592 };
Chuck Lever38527b12012-07-11 16:30:23 -04006593 int status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006594
Chuck Lever38527b12012-07-11 16:30:23 -04006595 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006596 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Chuck Lever38527b12012-07-11 16:30:23 -04006597 status = nfs4_call_sync_sequence(server->client, server, &msg,
Chuck Lever377e5072012-07-11 16:29:45 -04006598 &args.seq_args, &res.seq_res, 1);
Chuck Lever38527b12012-07-11 16:30:23 -04006599 dprintk("NFS reply free_stateid: %d\n", status);
6600 return status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006601}
6602
Chuck Lever38527b12012-07-11 16:30:23 -04006603/**
6604 * nfs41_free_stateid - perform a FREE_STATEID operation
6605 *
6606 * @server: server / transport on which to perform the operation
6607 * @stateid: state ID to release
6608 *
6609 * Returns NFS_OK if the server freed "stateid". Otherwise a
6610 * negative NFS4ERR value is returned.
6611 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006612static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006613{
6614 struct nfs4_exception exception = { };
6615 int err;
6616 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006617 err = _nfs4_free_stateid(server, stateid);
6618 if (err != -NFS4ERR_DELAY)
6619 break;
6620 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006621 } while (exception.retry);
6622 return err;
6623}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006624
6625static bool nfs41_match_stateid(const nfs4_stateid *s1,
6626 const nfs4_stateid *s2)
6627{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006628 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006629 return false;
6630
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006631 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006632 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006633 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006634 return true;
6635
6636 return false;
6637}
6638
Andy Adamson557134a2009-04-01 09:21:53 -04006639#endif /* CONFIG_NFS_V4_1 */
6640
Trond Myklebust36281ca2012-03-04 18:13:56 -05006641static bool nfs4_match_stateid(const nfs4_stateid *s1,
6642 const nfs4_stateid *s2)
6643{
Trond Myklebustf597c532012-03-04 18:13:56 -05006644 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006645}
6646
6647
Trond Myklebust17280172012-03-11 13:11:00 -04006648static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006649 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006650 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006651 .recover_open = nfs4_open_reclaim,
6652 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006653 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006654 .get_clid_cred = nfs4_get_setclientid_cred,
Chuck Lever05f4c352012-09-14 17:24:32 -04006655 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006656};
6657
Andy Adamson591d71c2009-04-01 09:22:47 -04006658#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006659static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006660 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6661 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6662 .recover_open = nfs4_open_reclaim,
6663 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006664 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006665 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006666 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04006667 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04006668};
6669#endif /* CONFIG_NFS_V4_1 */
6670
Trond Myklebust17280172012-03-11 13:11:00 -04006671static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006672 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006673 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006674 .recover_open = nfs4_open_expired,
6675 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006676 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006677 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006678};
6679
Andy Adamson591d71c2009-04-01 09:22:47 -04006680#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006681static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006682 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6683 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006684 .recover_open = nfs41_open_expired,
6685 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006686 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006687 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006688};
6689#endif /* CONFIG_NFS_V4_1 */
6690
Trond Myklebust17280172012-03-11 13:11:00 -04006691static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006692 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006693 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006694 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006695};
6696
6697#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006698static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006699 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006700 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006701 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006702};
6703#endif
6704
Trond Myklebust97dc1352010-06-16 09:52:26 -04006705static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6706 .minor_version = 0,
6707 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006708 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006709 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006710 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6711 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6712 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006713};
6714
6715#if defined(CONFIG_NFS_V4_1)
6716static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6717 .minor_version = 1,
6718 .call_sync = _nfs4_call_sync_session,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006719 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006720 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006721 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6722 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6723 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006724};
6725#endif
6726
Trond Myklebust97dc1352010-06-16 09:52:26 -04006727const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6728 [0] = &nfs_v4_0_minor_ops,
6729#if defined(CONFIG_NFS_V4_1)
6730 [1] = &nfs_v4_1_minor_ops,
6731#endif
6732};
6733
Bryan Schumaker73a79702012-07-16 16:39:12 -04006734const struct inode_operations nfs4_dir_inode_operations = {
6735 .create = nfs_create,
6736 .lookup = nfs_lookup,
6737 .atomic_open = nfs_atomic_open,
6738 .link = nfs_link,
6739 .unlink = nfs_unlink,
6740 .symlink = nfs_symlink,
6741 .mkdir = nfs_mkdir,
6742 .rmdir = nfs_rmdir,
6743 .mknod = nfs_mknod,
6744 .rename = nfs_rename,
6745 .permission = nfs_permission,
6746 .getattr = nfs_getattr,
6747 .setattr = nfs_setattr,
6748 .getxattr = generic_getxattr,
6749 .setxattr = generic_setxattr,
6750 .listxattr = generic_listxattr,
6751 .removexattr = generic_removexattr,
6752};
6753
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006754static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006755 .permission = nfs_permission,
6756 .getattr = nfs_getattr,
6757 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006758 .getxattr = generic_getxattr,
6759 .setxattr = generic_setxattr,
6760 .listxattr = generic_listxattr,
6761 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006762};
6763
David Howells509de812006-08-22 20:06:11 -04006764const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006765 .version = 4, /* protocol version */
6766 .dentry_ops = &nfs4_dentry_operations,
6767 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006768 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006769 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006770 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04006771 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04006772 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006773 .getattr = nfs4_proc_getattr,
6774 .setattr = nfs4_proc_setattr,
6775 .lookup = nfs4_proc_lookup,
6776 .access = nfs4_proc_access,
6777 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006778 .create = nfs4_proc_create,
6779 .remove = nfs4_proc_remove,
6780 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04006781 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006782 .unlink_done = nfs4_proc_unlink_done,
6783 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006784 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04006785 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04006786 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006787 .link = nfs4_proc_link,
6788 .symlink = nfs4_proc_symlink,
6789 .mkdir = nfs4_proc_mkdir,
6790 .rmdir = nfs4_proc_remove,
6791 .readdir = nfs4_proc_readdir,
6792 .mknod = nfs4_proc_mknod,
6793 .statfs = nfs4_proc_statfs,
6794 .fsinfo = nfs4_proc_fsinfo,
6795 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006796 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006797 .decode_dirent = nfs4_decode_dirent,
6798 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04006799 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04006800 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05006801 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006802 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04006803 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04006804 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006805 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006806 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04006807 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006808 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006809 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006810 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006811 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006812 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04006813 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04006814 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04006815 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00006816 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04006817 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04006818 .create_server = nfs4_create_server,
6819 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006820};
6821
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006822static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6823 .prefix = XATTR_NAME_NFSV4_ACL,
6824 .list = nfs4_xattr_list_nfs4_acl,
6825 .get = nfs4_xattr_get_nfs4_acl,
6826 .set = nfs4_xattr_set_nfs4_acl,
6827};
6828
6829const struct xattr_handler *nfs4_xattr_handlers[] = {
6830 &nfs4_xattr_nfs4_acl_handler,
6831 NULL
6832};
6833
Linus Torvalds1da177e2005-04-16 15:20:36 -07006834/*
6835 * Local variables:
6836 * c-basic-offset: 8
6837 * End:
6838 */