blob: ad1515521a6abc2369fd48b54211a889ee9faaec [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>
Bryan Schumaker8f70e952011-03-24 17:12:31 +000046#include <linux/sunrpc/gss_api.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/nfs.h>
48#include <linux/nfs4.h>
49#include <linux/nfs_fs.h>
50#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040051#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070053#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040054#include <linux/module.h>
Trond Myklebust6926afd2012-01-07 13:22:46 -050055#include <linux/nfs_idmap.h>
Andy Adamson5a0ffe52009-04-01 09:23:18 -040056#include <linux/sunrpc/bc_xprt.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000057#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050058#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010059#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Trond Myklebust4ce79712005-06-22 17:16:21 +000061#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050063#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050064#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040065#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040066#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040067#include "netns.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69#define NFSDBG_FACILITY NFSDBG_PROC
70
Trond Myklebust2066fe82006-09-15 08:30:46 -040071#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define NFS4_POLL_RETRY_MAX (15*HZ)
73
Trond Myklebusta78cb572009-08-09 15:06:19 -040074#define NFS4_MAX_LOOP_ON_RECOVER (10)
75
Trond Myklebustef159e92012-02-06 19:50:40 -050076static unsigned short max_session_slots = NFS4_DEF_SLOT_TABLE_SIZE;
77
Trond Myklebustcdd4e682006-01-03 09:55:12 +010078struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010079static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080080static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050082static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Chuck Lever81934dd2012-03-01 17:01:57 -050083static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Bryan Schumakerbae36242012-05-10 15:07:31 -040084static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *);
Trond Myklebust99367812007-07-17 21:52:41 -040085static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040086static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
87 struct nfs_fattr *fattr, struct iattr *sattr,
88 struct nfs4_state *state);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040089#ifdef CONFIG_NFS_V4_1
Bryan Schumaker1cab0652012-01-31 10:39:29 -050090static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *);
91static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040092#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070093/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050094static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
Trond Myklebust52567b02009-10-23 14:46:42 -040096 if (err >= -1000)
97 return err;
98 switch (err) {
99 case -NFS4ERR_RESOURCE:
100 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000101 case -NFS4ERR_WRONGSEC:
102 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800103 case -NFS4ERR_BADOWNER:
104 case -NFS4ERR_BADNAME:
105 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400106 case -NFS4ERR_SHARE_DENIED:
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154const u32 nfs4_statfs_bitmap[2] = {
155 FATTR4_WORD0_FILES_AVAIL
156 | FATTR4_WORD0_FILES_FREE
157 | FATTR4_WORD0_FILES_TOTAL,
158 FATTR4_WORD1_SPACE_AVAIL
159 | FATTR4_WORD1_SPACE_FREE
160 | FATTR4_WORD1_SPACE_TOTAL
161};
162
Trond Myklebust4ce79712005-06-22 17:16:21 +0000163const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 FATTR4_WORD0_MAXLINK
165 | FATTR4_WORD0_MAXNAME,
166 0
167};
168
Fred Isamandae100c2011-07-30 20:52:37 -0400169const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 | FATTR4_WORD0_MAXREAD
171 | FATTR4_WORD0_MAXWRITE
172 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700173 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400174 | FATTR4_WORD1_FS_LAYOUT_TYPES,
175 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176};
177
Manoj Naik830b8e32006-06-09 09:34:25 -0400178const u32 nfs4_fs_locations_bitmap[2] = {
179 FATTR4_WORD0_TYPE
180 | FATTR4_WORD0_CHANGE
181 | FATTR4_WORD0_SIZE
182 | FATTR4_WORD0_FSID
183 | FATTR4_WORD0_FILEID
184 | FATTR4_WORD0_FS_LOCATIONS,
185 FATTR4_WORD1_MODE
186 | FATTR4_WORD1_NUMLINKS
187 | FATTR4_WORD1_OWNER
188 | FATTR4_WORD1_OWNER_GROUP
189 | FATTR4_WORD1_RAWDEV
190 | FATTR4_WORD1_SPACE_USED
191 | FATTR4_WORD1_TIME_ACCESS
192 | FATTR4_WORD1_TIME_METADATA
193 | FATTR4_WORD1_TIME_MODIFY
194 | FATTR4_WORD1_MOUNTED_ON_FILEID
195};
196
Al Virobc4785c2006-10-19 23:28:51 -0700197static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 struct nfs4_readdir_arg *readdir)
199{
Al Viro0dbb4c62006-10-19 23:28:49 -0700200 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 BUG_ON(readdir->count < 80);
203 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000204 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
206 return;
207 }
208
209 readdir->cookie = 0;
210 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
211 if (cookie == 2)
212 return;
213
214 /*
215 * NFSv4 servers do not return entries for '.' and '..'
216 * Therefore, we fake these entries here. We let '.'
217 * have cookie 0 and '..' have cookie 1. Note that
218 * when talking to the server, we always send cookie 0
219 * instead of 1 or 2.
220 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800221 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223 if (cookie == 0) {
224 *p++ = xdr_one; /* next */
225 *p++ = xdr_zero; /* cookie, first word */
226 *p++ = xdr_one; /* cookie, second word */
227 *p++ = xdr_one; /* entry len */
228 memcpy(p, ".\0\0\0", 4); /* entry */
229 p++;
230 *p++ = xdr_one; /* bitmap length */
231 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
232 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400233 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 }
235
236 *p++ = xdr_one; /* next */
237 *p++ = xdr_zero; /* cookie, first word */
238 *p++ = xdr_two; /* cookie, second word */
239 *p++ = xdr_two; /* entry len */
240 memcpy(p, "..\0\0", 4); /* entry */
241 p++;
242 *p++ = xdr_one; /* bitmap length */
243 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
244 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400245 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247 readdir->pgbase = (char *)p - (char *)start;
248 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800249 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250}
251
Trond Myklebust65de8722008-12-23 15:21:44 -0500252static int nfs4_wait_clnt_recover(struct nfs_client *clp)
253{
254 int res;
255
256 might_sleep();
257
Trond Myklebuste005e802008-12-23 15:21:48 -0500258 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400259 nfs_wait_bit_killable, TASK_KILLABLE);
Trond Myklebust65de8722008-12-23 15:21:44 -0500260 return res;
261}
262
263static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
264{
265 int res = 0;
266
267 might_sleep();
268
269 if (*timeout <= 0)
270 *timeout = NFS4_POLL_RETRY_MIN;
271 if (*timeout > NFS4_POLL_RETRY_MAX)
272 *timeout = NFS4_POLL_RETRY_MAX;
Jeff Laytond3103102011-12-01 22:44:39 +0100273 freezable_schedule_timeout_killable(*timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500274 if (fatal_signal_pending(current))
275 res = -ERESTARTSYS;
276 *timeout <<= 1;
277 return res;
278}
279
280/* This is the error handling routine for processes that are allowed
281 * to sleep.
282 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800283static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500284{
285 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500286 struct nfs4_state *state = exception->state;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500287 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500288 int ret = errorcode;
289
290 exception->retry = 0;
291 switch(errorcode) {
292 case 0:
293 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500294 case -NFS4ERR_OPENMODE:
Trond Myklebust14977482012-03-27 18:31:25 -0400295 if (inode && nfs_have_delegation(inode, FMODE_READ)) {
Trond Myklebust3114ea72012-03-07 16:39:06 -0500296 nfs_inode_return_delegation(inode);
297 exception->retry = 1;
298 return 0;
299 }
300 if (state == NULL)
301 break;
302 nfs4_schedule_stateid_recovery(server, state);
303 goto wait_on_recovery;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500304 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500305 case -NFS4ERR_ADMIN_REVOKED:
306 case -NFS4ERR_BAD_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500307 if (state == NULL)
308 break;
Trond Myklebust14977482012-03-27 18:31:25 -0400309 nfs_remove_bad_delegation(state->inode);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500310 nfs4_schedule_stateid_recovery(server, state);
311 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400312 case -NFS4ERR_EXPIRED:
313 if (state != NULL)
314 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust65de8722008-12-23 15:21:44 -0500315 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500316 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500317 nfs4_schedule_lease_recovery(clp);
318 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500319#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400320 case -NFS4ERR_BADSESSION:
321 case -NFS4ERR_BADSLOT:
322 case -NFS4ERR_BAD_HIGH_SLOT:
323 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
324 case -NFS4ERR_DEADSESSION:
325 case -NFS4ERR_SEQ_FALSE_RETRY:
326 case -NFS4ERR_SEQ_MISORDERED:
327 dprintk("%s ERROR: %d Reset session\n", __func__,
328 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400329 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Andy Adamson4745e312009-04-01 09:22:42 -0400330 exception->retry = 1;
Andy Adamsonb9179232009-12-04 15:55:32 -0500331 break;
Trond Myklebust03391692010-01-26 15:42:38 -0500332#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500333 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500334 if (exception->timeout > HZ) {
335 /* We have retried a decent amount, time to
336 * fail
337 */
338 ret = -EBUSY;
339 break;
340 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500341 case -NFS4ERR_GRACE:
342 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -0500343 case -EKEYEXPIRED:
Trond Myklebust65de8722008-12-23 15:21:44 -0500344 ret = nfs4_delay(server->client, &exception->timeout);
345 if (ret != 0)
346 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400347 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500348 case -NFS4ERR_OLD_STATEID:
349 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800350 break;
351 case -NFS4ERR_BADOWNER:
352 /* The following works around a Linux server bug! */
353 case -NFS4ERR_BADNAME:
354 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
355 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
356 exception->retry = 1;
357 printk(KERN_WARNING "NFS: v4 server %s "
358 "does not accept raw "
359 "uid/gids. "
360 "Reenabling the idmapper.\n",
361 server->nfs_client->cl_hostname);
362 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500363 }
364 /* We failed to handle the error */
365 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500366wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500367 ret = nfs4_wait_clnt_recover(clp);
368 if (ret == 0)
369 exception->retry = 1;
370 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500371}
372
373
Trond Myklebust452e9352010-07-31 14:29:06 -0400374static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 spin_lock(&clp->cl_lock);
377 if (time_before(clp->cl_last_renewal,timestamp))
378 clp->cl_last_renewal = timestamp;
379 spin_unlock(&clp->cl_lock);
380}
381
Trond Myklebust452e9352010-07-31 14:29:06 -0400382static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
383{
384 do_renew_lease(server->nfs_client, timestamp);
385}
386
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400387#if defined(CONFIG_NFS_V4_1)
388
Benny Halevy510b8172009-04-01 09:22:14 -0400389/*
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400390 * nfs4_free_slot - free a slot and efficiently update slot table.
391 *
392 * freeing a slot is trivially done by clearing its respective bit
393 * in the bitmap.
394 * If the freed slotid equals highest_used_slotid we want to update it
395 * so that the server would be able to size down the slot table if needed,
396 * otherwise we know that the highest_used_slotid is still in use.
397 * When updating highest_used_slotid there may be "holes" in the bitmap
398 * so we need to scan down from highest_used_slotid to 0 looking for the now
399 * highest slotid in use.
Trond Myklebust45d43c22012-02-06 19:38:51 -0500400 * If none found, highest_used_slotid is set to NFS4_NO_SLOT.
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500401 *
402 * Must be called while holding tbl->slot_tbl_lock
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400403 */
404static void
Trond Myklebust45d43c22012-02-06 19:38:51 -0500405nfs4_free_slot(struct nfs4_slot_table *tbl, u32 slotid)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400406{
Trond Myklebust45d43c22012-02-06 19:38:51 -0500407 BUG_ON(slotid >= NFS4_MAX_SLOT_TABLE);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400408 /* clear used bit in bitmap */
409 __clear_bit(slotid, tbl->used_slots);
410
411 /* update highest_used_slotid when it is freed */
412 if (slotid == tbl->highest_used_slotid) {
413 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500414 if (slotid < tbl->max_slots)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400415 tbl->highest_used_slotid = slotid;
416 else
Trond Myklebust45d43c22012-02-06 19:38:51 -0500417 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400418 }
Trond Myklebust45d43c22012-02-06 19:38:51 -0500419 dprintk("%s: slotid %u highest_used_slotid %d\n", __func__,
420 slotid, tbl->highest_used_slotid);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400421}
422
Trond Myklebust961a8282012-01-17 22:57:37 -0500423bool nfs4_set_task_privileged(struct rpc_task *task, void *dummy)
424{
425 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
426 return true;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400427}
428
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800429/*
Andy Adamson42acd022011-01-06 02:04:34 +0000430 * Signal state manager thread if session fore channel is drained
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800431 */
Andy Adamson42acd022011-01-06 02:04:34 +0000432static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800433{
Trond Myklebusta2118c32010-06-16 09:52:26 -0400434 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
Trond Myklebust961a8282012-01-17 22:57:37 -0500435 rpc_wake_up_first(&ses->fc_slot_table.slot_tbl_waitq,
436 nfs4_set_task_privileged, NULL);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800437 return;
438 }
439
Trond Myklebust45d43c22012-02-06 19:38:51 -0500440 if (ses->fc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800441 return;
442
Andy Adamson42acd022011-01-06 02:04:34 +0000443 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
444 complete(&ses->fc_slot_table.complete);
445}
446
447/*
448 * Signal state manager thread if session back channel is drained
449 */
450void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
451{
452 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
Trond Myklebust45d43c22012-02-06 19:38:51 -0500453 ses->bc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
Andy Adamson42acd022011-01-06 02:04:34 +0000454 return;
455 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
456 complete(&ses->bc_slot_table.complete);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800457}
458
Trond Myklebustd185a332010-06-16 09:52:25 -0400459static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400460{
461 struct nfs4_slot_table *tbl;
462
Trond Myklebustd185a332010-06-16 09:52:25 -0400463 tbl = &res->sr_session->fc_slot_table;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400464 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400465 /* just wake up the next guy waiting since
466 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500467 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500468 return;
Andy Adamson13615872009-04-01 09:22:17 -0400469 }
Andy Adamsonea028ac2009-12-04 15:55:38 -0500470
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500471 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonaabd0b42011-11-09 13:58:22 -0500472 nfs4_free_slot(tbl, res->sr_slot - tbl->slots);
Andy Adamson42acd022011-01-06 02:04:34 +0000473 nfs4_check_drain_fc_complete(res->sr_session);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500474 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400475 res->sr_slot = NULL;
Andy Adamson13615872009-04-01 09:22:17 -0400476}
477
Trond Myklebust14516c32010-07-31 14:29:06 -0400478static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400479{
480 unsigned long timestamp;
Trond Myklebust14516c32010-07-31 14:29:06 -0400481 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400482
483 /*
484 * sr_status remains 1 if an RPC level error occurred. The server
485 * may or may not have processed the sequence operation..
486 * Proceed as if the server received and processed the sequence
487 * operation.
488 */
489 if (res->sr_status == 1)
490 res->sr_status = NFS_OK;
491
Bryan Schumaker468f8612011-04-18 15:57:32 -0400492 /* don't increment the sequence number if the task wasn't sent */
493 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400494 goto out;
495
Andy Adamson691daf32009-12-04 15:55:39 -0500496 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400497 switch (res->sr_status) {
498 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400499 /* Update the slot's sequence and clientid lease timer */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400500 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400501 timestamp = res->sr_renewal_time;
Trond Myklebust14516c32010-07-31 14:29:06 -0400502 clp = res->sr_session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400503 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500504 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500505 if (res->sr_status_flags != 0)
506 nfs4_schedule_lease_recovery(clp);
Trond Myklebust14516c32010-07-31 14:29:06 -0400507 break;
508 case -NFS4ERR_DELAY:
509 /* The server detected a resend of the RPC call and
510 * returned NFS4ERR_DELAY as per Section 2.10.6.2
511 * of RFC5661.
512 */
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200513 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400514 __func__,
515 res->sr_slot - res->sr_session->fc_slot_table.slots,
516 res->sr_slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400517 goto out_retry;
518 default:
519 /* Just update the slot sequence no. */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400520 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400521 }
522out:
523 /* The session may be reset by one of the error handlers. */
524 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400525 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400526 return 1;
527out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400528 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400529 goto out;
530 rpc_delay(task, NFS4_POLL_RETRY_MAX);
531 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400532}
533
Trond Myklebust14516c32010-07-31 14:29:06 -0400534static int nfs4_sequence_done(struct rpc_task *task,
535 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400536{
Trond Myklebust14516c32010-07-31 14:29:06 -0400537 if (res->sr_session == NULL)
538 return 1;
539 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400540}
541
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400542/*
Benny Halevy510b8172009-04-01 09:22:14 -0400543 * nfs4_find_slot - efficiently look for a free slot
544 *
545 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
546 * If found, we mark the slot as used, update the highest_used_slotid,
547 * and respectively set up the sequence operation args.
Trond Myklebust45d43c22012-02-06 19:38:51 -0500548 * The slot number is returned if found, or NFS4_NO_SLOT otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400549 *
550 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400551 */
Trond Myklebust45d43c22012-02-06 19:38:51 -0500552static u32
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800553nfs4_find_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400554{
Trond Myklebust45d43c22012-02-06 19:38:51 -0500555 u32 slotid;
556 u32 ret_id = NFS4_NO_SLOT;
Benny Halevy510b8172009-04-01 09:22:14 -0400557
Trond Myklebust45d43c22012-02-06 19:38:51 -0500558 dprintk("--> %s used_slots=%04lx highest_used=%u max_slots=%u\n",
Benny Halevy510b8172009-04-01 09:22:14 -0400559 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
560 tbl->max_slots);
561 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
562 if (slotid >= tbl->max_slots)
563 goto out;
564 __set_bit(slotid, tbl->used_slots);
Trond Myklebust45d43c22012-02-06 19:38:51 -0500565 if (slotid > tbl->highest_used_slotid ||
566 tbl->highest_used_slotid == NFS4_NO_SLOT)
Benny Halevy510b8172009-04-01 09:22:14 -0400567 tbl->highest_used_slotid = slotid;
568 ret_id = slotid;
569out:
570 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
571 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400572 return ret_id;
573}
574
Trond Myklebust9d12b212012-01-17 22:04:25 -0500575static void nfs41_init_sequence(struct nfs4_sequence_args *args,
576 struct nfs4_sequence_res *res, int cache_reply)
577{
578 args->sa_session = NULL;
579 args->sa_cache_this = 0;
580 if (cache_reply)
581 args->sa_cache_this = 1;
582 res->sr_session = NULL;
583 res->sr_slot = NULL;
584}
585
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000586int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400587 struct nfs4_sequence_args *args,
588 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400589 struct rpc_task *task)
590{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400591 struct nfs4_slot *slot;
592 struct nfs4_slot_table *tbl;
Trond Myklebust45d43c22012-02-06 19:38:51 -0500593 u32 slotid;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400594
595 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400596 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400597 if (res->sr_slot != NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400598 return 0;
599
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400600 tbl = &session->fc_slot_table;
601
602 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400603 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800604 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500605 /* The state manager will wait until the slot table is empty */
Andy Adamson05f0d232009-12-04 15:55:37 -0500606 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400607 spin_unlock(&tbl->slot_tbl_lock);
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500608 dprintk("%s session is draining\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500609 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400610 }
611
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800612 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
613 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
614 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
615 spin_unlock(&tbl->slot_tbl_lock);
616 dprintk("%s enforce FIFO order\n", __func__);
617 return -EAGAIN;
618 }
619
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800620 slotid = nfs4_find_slot(tbl);
Trond Myklebust45d43c22012-02-06 19:38:51 -0500621 if (slotid == NFS4_NO_SLOT) {
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400622 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
623 spin_unlock(&tbl->slot_tbl_lock);
624 dprintk("<-- %s: no free slots\n", __func__);
625 return -EAGAIN;
626 }
627 spin_unlock(&tbl->slot_tbl_lock);
628
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800629 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400630 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400631 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400632 args->sa_slotid = slotid;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400633
634 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
635
Benny Halevy99fe60d2009-04-01 09:22:29 -0400636 res->sr_session = session;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400637 res->sr_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400638 res->sr_renewal_time = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400639 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400640 /*
641 * sr_status is only set in decode_sequence, and so will remain
642 * set to 1 if an rpc level failure occurs.
643 */
644 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400645 return 0;
646}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000647EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400648
Trond Myklebust035168a2010-06-16 09:52:26 -0400649int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400650 struct nfs4_sequence_args *args,
651 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400652 struct rpc_task *task)
653{
Trond Myklebust035168a2010-06-16 09:52:26 -0400654 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400655 int ret = 0;
656
Trond Myklebust9d12b212012-01-17 22:04:25 -0500657 if (session == NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400658 goto out;
Trond Myklebust035168a2010-06-16 09:52:26 -0400659
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200660 dprintk("--> %s clp %p session %p sr_slot %td\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400661 __func__, session->clp, session, res->sr_slot ?
662 res->sr_slot - session->fc_slot_table.slots : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400663
Trond Myklebust9d12b212012-01-17 22:04:25 -0500664 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400665out:
666 dprintk("<-- %s status=%d\n", __func__, ret);
667 return ret;
668}
669
670struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400671 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400672 struct nfs4_sequence_args *seq_args;
673 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400674};
675
676static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
677{
678 struct nfs41_call_sync_data *data = calldata;
679
Trond Myklebust035168a2010-06-16 09:52:26 -0400680 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
681
682 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -0500683 data->seq_res, task))
Andy Adamsonce5039c2009-04-01 09:22:13 -0400684 return;
685 rpc_call_start(task);
686}
687
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800688static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
689{
690 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
691 nfs41_call_sync_prepare(task, calldata);
692}
693
Andy Adamson69ab40c2009-04-01 09:22:19 -0400694static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
695{
696 struct nfs41_call_sync_data *data = calldata;
697
Trond Myklebust14516c32010-07-31 14:29:06 -0400698 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400699}
700
Trond Myklebust17280172012-03-11 13:11:00 -0400701static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400702 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400703 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400704};
705
Trond Myklebust17280172012-03-11 13:11:00 -0400706static const struct rpc_call_ops nfs41_call_priv_sync_ops = {
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800707 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
708 .rpc_call_done = nfs41_call_sync_done,
709};
710
Bryan Schumaker7c513052011-03-24 17:12:24 +0000711static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
712 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400713 struct rpc_message *msg,
714 struct nfs4_sequence_args *args,
715 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800716 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400717{
718 int ret;
719 struct rpc_task *task;
720 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400721 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400722 .seq_args = args,
723 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400724 };
725 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000726 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400727 .rpc_message = msg,
728 .callback_ops = &nfs41_call_sync_ops,
729 .callback_data = &data
730 };
731
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800732 if (privileged)
733 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400734 task = rpc_run_task(&task_setup);
735 if (IS_ERR(task))
736 ret = PTR_ERR(task);
737 else {
738 ret = task->tk_status;
739 rpc_put_task(task);
740 }
741 return ret;
742}
743
Bryan Schumaker7c513052011-03-24 17:12:24 +0000744int _nfs4_call_sync_session(struct rpc_clnt *clnt,
745 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400746 struct rpc_message *msg,
747 struct nfs4_sequence_args *args,
748 struct nfs4_sequence_res *res,
749 int cache_reply)
750{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500751 nfs41_init_sequence(args, res, cache_reply);
752 return nfs4_call_sync_sequence(clnt, server, msg, args, res, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400753}
754
Trond Myklebustdf896452010-06-16 09:52:26 -0400755#else
Trond Myklebust9d12b212012-01-17 22:04:25 -0500756static inline
757void nfs41_init_sequence(struct nfs4_sequence_args *args,
758 struct nfs4_sequence_res *res, int cache_reply)
759{
760}
761
Trond Myklebust14516c32010-07-31 14:29:06 -0400762static int nfs4_sequence_done(struct rpc_task *task,
763 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400764{
Trond Myklebust14516c32010-07-31 14:29:06 -0400765 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400766}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400767#endif /* CONFIG_NFS_V4_1 */
768
Bryan Schumaker7c513052011-03-24 17:12:24 +0000769int _nfs4_call_sync(struct rpc_clnt *clnt,
770 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400771 struct rpc_message *msg,
772 struct nfs4_sequence_args *args,
773 struct nfs4_sequence_res *res,
774 int cache_reply)
775{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500776 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000777 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400778}
779
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000780static inline
Bryan Schumaker7c513052011-03-24 17:12:24 +0000781int nfs4_call_sync(struct rpc_clnt *clnt,
782 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000783 struct rpc_message *msg,
784 struct nfs4_sequence_args *args,
785 struct nfs4_sequence_res *res,
786 int cache_reply)
787{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000788 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
789 args, res, cache_reply);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000790}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400791
Trond Myklebust38478b22006-05-25 01:40:57 -0400792static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793{
Trond Myklebust38478b22006-05-25 01:40:57 -0400794 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Trond Myklebust38478b22006-05-25 01:40:57 -0400796 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400797 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700798 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400799 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700800 dir->i_version = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400801 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802}
803
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100804struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400805 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100806 struct nfs_openargs o_arg;
807 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100808 struct nfs_open_confirmargs c_arg;
809 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500810 struct nfs4_string owner_name;
811 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100812 struct nfs_fattr f_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100813 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400814 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100815 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400816 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100817 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100818 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400819 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100820 int rpc_status;
821 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100822};
823
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400824
825static void nfs4_init_opendata_res(struct nfs4_opendata *p)
826{
827 p->o_res.f_attr = &p->f_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400828 p->o_res.seqid = p->o_arg.seqid;
829 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400830 p->o_res.server = p->o_arg.server;
831 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500832 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400833}
834
Al Viro82a2c1b2011-06-22 18:30:55 -0400835static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500836 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400837 const struct iattr *attrs,
838 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100839{
Al Viro82a2c1b2011-06-22 18:30:55 -0400840 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100841 struct inode *dir = parent->d_inode;
842 struct nfs_server *server = NFS_SERVER(dir);
843 struct nfs4_opendata *p;
844
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400845 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100846 if (p == NULL)
847 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400848 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100849 if (p->o_arg.seqid == NULL)
850 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400851 nfs_sb_active(dentry->d_sb);
852 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100853 p->dir = parent;
854 p->owner = sp;
855 atomic_inc(&sp->so_count);
856 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500857 p->o_arg.open_flags = flags;
858 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400859 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400860 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
861 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400862 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100863 p->o_arg.server = server;
864 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust1549210f2012-06-05 09:16:47 -0400865 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100866 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust536e43d2012-01-17 22:04:26 -0500867 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -0500868 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400869
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100870 p->o_arg.u.attrs = &p->attrs;
871 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500872
873 verf[0] = jiffies;
874 verf[1] = current->pid;
875 memcpy(p->o_arg.u.verifier.data, verf,
876 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100877 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100878 p->c_arg.fh = &p->o_res.fh;
879 p->c_arg.stateid = &p->o_res.stateid;
880 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400881 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400882 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100883 return p;
884err_free:
885 kfree(p);
886err:
887 dput(parent);
888 return NULL;
889}
890
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400891static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100892{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400893 struct nfs4_opendata *p = container_of(kref,
894 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400895 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400896
897 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400898 if (p->state != NULL)
899 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400900 nfs4_put_state_owner(p->owner);
901 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400902 dput(p->dentry);
903 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500904 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400905 kfree(p);
906}
907
908static void nfs4_opendata_put(struct nfs4_opendata *p)
909{
910 if (p != NULL)
911 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100912}
913
Trond Myklebust06f814a2006-01-03 09:55:07 +0100914static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
915{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100916 int ret;
917
Trond Myklebust06f814a2006-01-03 09:55:07 +0100918 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100919 return ret;
920}
921
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500922static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400923{
924 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500925
Trond Myklebust536e43d2012-01-17 22:04:26 -0500926 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500927 goto out;
928 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400929 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500930 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
931 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400932 break;
933 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500934 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
935 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400936 break;
937 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500938 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
939 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400940 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500941out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400942 return ret;
943}
944
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500945static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400946{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500947 if (delegation == NULL)
948 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500949 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400950 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500951 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400952 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500953 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400954 return 1;
955}
956
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500957static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100958{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500959 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100960 case FMODE_WRITE:
961 state->n_wronly++;
962 break;
963 case FMODE_READ:
964 state->n_rdonly++;
965 break;
966 case FMODE_READ|FMODE_WRITE:
967 state->n_rdwr++;
968 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500969 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100970}
971
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500972static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400973{
974 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -0500975 nfs4_stateid_copy(&state->stateid, stateid);
976 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500977 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400978 case FMODE_READ:
979 set_bit(NFS_O_RDONLY_STATE, &state->flags);
980 break;
981 case FMODE_WRITE:
982 set_bit(NFS_O_WRONLY_STATE, &state->flags);
983 break;
984 case FMODE_READ|FMODE_WRITE:
985 set_bit(NFS_O_RDWR_STATE, &state->flags);
986 }
987}
988
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500989static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400990{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400991 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500992 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400993 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400994}
995
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500996static 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 -0700997{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400998 /*
999 * Protect the call to nfs4_state_set_mode_locked and
1000 * serialise the stateid update
1001 */
1002 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001003 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001004 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001005 set_bit(NFS_DELEGATED_STATE, &state->flags);
1006 }
1007 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001008 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001009 write_sequnlock(&state->seqlock);
1010 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001011 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001012 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013}
1014
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001015static 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 -05001016{
1017 struct nfs_inode *nfsi = NFS_I(state->inode);
1018 struct nfs_delegation *deleg_cur;
1019 int ret = 0;
1020
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001021 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001022
1023 rcu_read_lock();
1024 deleg_cur = rcu_dereference(nfsi->delegation);
1025 if (deleg_cur == NULL)
1026 goto no_delegation;
1027
1028 spin_lock(&deleg_cur->lock);
1029 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001030 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001031 goto no_delegation_unlock;
1032
1033 if (delegation == NULL)
1034 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001035 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001036 goto no_delegation_unlock;
1037
Trond Myklebustb7391f42008-12-23 15:21:52 -05001038 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001039 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001040 ret = 1;
1041no_delegation_unlock:
1042 spin_unlock(&deleg_cur->lock);
1043no_delegation:
1044 rcu_read_unlock();
1045
1046 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001047 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001048 ret = 1;
1049 }
1050
1051 return ret;
1052}
1053
1054
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001055static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001056{
1057 struct nfs_delegation *delegation;
1058
1059 rcu_read_lock();
1060 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001061 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001062 rcu_read_unlock();
1063 return;
1064 }
1065 rcu_read_unlock();
1066 nfs_inode_return_delegation(inode);
1067}
1068
Trond Myklebust6ee41262007-07-08 14:11:36 -04001069static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001070{
1071 struct nfs4_state *state = opendata->state;
1072 struct nfs_inode *nfsi = NFS_I(state->inode);
1073 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -05001074 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001075 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001076 nfs4_stateid stateid;
1077 int ret = -EAGAIN;
1078
Trond Myklebustaac00a82007-07-05 19:02:21 -04001079 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001080 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001081 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001082 if (can_open_cached(state, fmode, open_mode)) {
1083 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001084 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001085 goto out_return_state;
1086 }
1087 spin_unlock(&state->owner->so_lock);
1088 }
Trond Myklebust34310432008-12-23 15:21:38 -05001089 rcu_read_lock();
1090 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001091 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001092 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001093 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001094 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001095 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001096 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001097 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001098 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001099 if (ret != 0)
1100 goto out;
1101 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001102
1103 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001104 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001105 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001106 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001107out:
1108 return ERR_PTR(ret);
1109out_return_state:
1110 atomic_inc(&state->count);
1111 return state;
1112}
1113
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001114static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1115{
1116 struct inode *inode;
1117 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -04001118 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001119 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001120
Trond Myklebustaac00a82007-07-05 19:02:21 -04001121 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001122 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001123 goto out;
1124 }
1125
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001126 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001127 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001128 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001129 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001130 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001131 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001132 goto err;
1133 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001134 state = nfs4_get_open_state(inode, data->owner);
1135 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001136 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001137 if (data->o_res.delegation_type != 0) {
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001138 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001139 int delegation_flags = 0;
1140
Trond Myklebust003707c2007-07-05 18:07:55 -04001141 rcu_read_lock();
1142 delegation = rcu_dereference(NFS_I(inode)->delegation);
1143 if (delegation)
1144 delegation_flags = delegation->flags;
1145 rcu_read_unlock();
Trond Myklebust652f89f2011-12-09 19:05:58 -05001146 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1147 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1148 "returning a delegation for "
1149 "OPEN(CLAIM_DELEGATE_CUR)\n",
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001150 clp->cl_hostname);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001151 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001152 nfs_inode_set_delegation(state->inode,
1153 data->owner->so_cred,
1154 &data->o_res);
1155 else
1156 nfs_inode_reclaim_delegation(state->inode,
1157 data->owner->so_cred,
1158 &data->o_res);
1159 }
Trond Myklebust34310432008-12-23 15:21:38 -05001160
1161 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001162 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001163 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001164out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001165 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001166err_put_inode:
1167 iput(inode);
1168err:
1169 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001170}
1171
Trond Myklebust864472e2006-01-03 09:55:15 +01001172static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1173{
1174 struct nfs_inode *nfsi = NFS_I(state->inode);
1175 struct nfs_open_context *ctx;
1176
1177 spin_lock(&state->inode->i_lock);
1178 list_for_each_entry(ctx, &nfsi->open_files, list) {
1179 if (ctx->state != state)
1180 continue;
1181 get_nfs_open_context(ctx);
1182 spin_unlock(&state->inode->i_lock);
1183 return ctx;
1184 }
1185 spin_unlock(&state->inode->i_lock);
1186 return ERR_PTR(-ENOENT);
1187}
1188
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001189static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1190{
1191 struct nfs4_opendata *opendata;
1192
Al Viro3d4ff432011-06-22 18:40:12 -04001193 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001194 if (opendata == NULL)
1195 return ERR_PTR(-ENOMEM);
1196 opendata->state = state;
1197 atomic_inc(&state->count);
1198 return opendata;
1199}
1200
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001201static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001202{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001203 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001204 int ret;
1205
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001206 opendata->o_arg.open_flags = 0;
1207 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001208 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1209 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1210 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001211 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001212 if (ret != 0)
1213 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001214 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001215 if (IS_ERR(newstate))
1216 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001217 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001218 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001219 return 0;
1220}
1221
1222static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1223{
Trond Myklebust864472e2006-01-03 09:55:15 +01001224 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001225 int ret;
1226
1227 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001228 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001229 smp_rmb();
1230 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001231 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001232 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001233 if (ret != 0)
1234 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001235 if (newstate != state)
1236 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001237 }
1238 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001239 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001240 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001241 if (ret != 0)
1242 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001243 if (newstate != state)
1244 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001245 }
1246 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001247 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001248 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001249 if (ret != 0)
1250 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001251 if (newstate != state)
1252 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001253 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001254 /*
1255 * We may have performed cached opens for all three recoveries.
1256 * Check if we need to update the current stateid.
1257 */
1258 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001259 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001260 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001261 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001262 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001263 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001264 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001265 return 0;
1266}
1267
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268/*
1269 * OPEN_RECLAIM:
1270 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001272static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001274 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001275 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001276 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 int status;
1278
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001279 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1280 if (IS_ERR(opendata))
1281 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001282 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1283 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001284 rcu_read_lock();
1285 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001286 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001287 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001288 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001289 opendata->o_arg.u.delegation_type = delegation_type;
1290 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001291 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 return status;
1293}
1294
Trond Myklebust539cd032007-06-05 11:46:42 -04001295static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296{
1297 struct nfs_server *server = NFS_SERVER(state->inode);
1298 struct nfs4_exception exception = { };
1299 int err;
1300 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001301 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001302 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001303 break;
1304 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 } while (exception.retry);
1306 return err;
1307}
1308
Trond Myklebust864472e2006-01-03 09:55:15 +01001309static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1310{
1311 struct nfs_open_context *ctx;
1312 int ret;
1313
1314 ctx = nfs4_state_find_open_context(state);
1315 if (IS_ERR(ctx))
1316 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001317 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001318 put_nfs_open_context(ctx);
1319 return ret;
1320}
1321
Trond Myklebust13437e12007-07-06 15:10:43 -04001322static 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 -07001323{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001324 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001325 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001327 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1328 if (IS_ERR(opendata))
1329 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001330 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebustf597c532012-03-04 18:13:56 -05001331 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust864472e2006-01-03 09:55:15 +01001332 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001333 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001334 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335}
1336
Trond Myklebust13437e12007-07-06 15:10:43 -04001337int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338{
1339 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001340 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 int err;
1342 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001343 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 switch (err) {
1345 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001346 case -ENOENT:
1347 case -ESTALE:
1348 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001349 case -NFS4ERR_BADSESSION:
1350 case -NFS4ERR_BADSLOT:
1351 case -NFS4ERR_BAD_HIGH_SLOT:
1352 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1353 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04001354 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001355 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 case -NFS4ERR_STALE_CLIENTID:
1357 case -NFS4ERR_STALE_STATEID:
1358 case -NFS4ERR_EXPIRED:
1359 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001360 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001361 goto out;
1362 case -ERESTARTSYS:
1363 /*
1364 * The show must go on: exit, but mark the
1365 * stateid as needing recovery.
1366 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001367 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001368 case -NFS4ERR_ADMIN_REVOKED:
1369 case -NFS4ERR_BAD_STATEID:
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001370 nfs_inode_find_state_and_recover(state->inode,
1371 stateid);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001372 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001373 case -EKEYEXPIRED:
1374 /*
1375 * User RPCSEC_GSS context has expired.
1376 * We cannot recover this stateid now, so
1377 * skip it and allow recovery thread to
1378 * proceed.
1379 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001380 case -ENOMEM:
1381 err = 0;
1382 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 }
1384 err = nfs4_handle_exception(server, err, &exception);
1385 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001386out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 return err;
1388}
1389
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001390static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1391{
1392 struct nfs4_opendata *data = calldata;
1393
1394 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001395 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001396 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001397 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001398 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001399 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001400 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001401}
1402
1403static void nfs4_open_confirm_release(void *calldata)
1404{
1405 struct nfs4_opendata *data = calldata;
1406 struct nfs4_state *state = NULL;
1407
1408 /* If this request hasn't been cancelled, do nothing */
1409 if (data->cancelled == 0)
1410 goto out_free;
1411 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001412 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001413 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001414 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001415 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001416 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001417out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001418 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001419}
1420
1421static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001422 .rpc_call_done = nfs4_open_confirm_done,
1423 .rpc_release = nfs4_open_confirm_release,
1424};
1425
1426/*
1427 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1428 */
1429static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1430{
1431 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1432 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001433 struct rpc_message msg = {
1434 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1435 .rpc_argp = &data->c_arg,
1436 .rpc_resp = &data->c_res,
1437 .rpc_cred = data->owner->so_cred,
1438 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001439 struct rpc_task_setup task_setup_data = {
1440 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001441 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001442 .callback_ops = &nfs4_open_confirm_ops,
1443 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001444 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001445 .flags = RPC_TASK_ASYNC,
1446 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 int status;
1448
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001449 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001450 data->rpc_done = 0;
1451 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001452 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001453 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001454 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001455 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001456 status = nfs4_wait_for_completion_rpc_task(task);
1457 if (status != 0) {
1458 data->cancelled = 1;
1459 smp_wmb();
1460 } else
1461 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001462 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 return status;
1464}
1465
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001466static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001468 struct nfs4_opendata *data = calldata;
1469 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001470
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001471 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1472 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001473 /*
1474 * Check if we still need to send an OPEN call, or if we can use
1475 * a delegation instead.
1476 */
1477 if (data->state != NULL) {
1478 struct nfs_delegation *delegation;
1479
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001480 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001481 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001482 rcu_read_lock();
1483 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001484 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1485 can_open_delegated(delegation, data->o_arg.fmode))
1486 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001487 rcu_read_unlock();
1488 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001489 /* Update client id. */
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001490 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001491 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001492 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001493 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1494 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001495 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001496 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001497 &data->o_arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05001498 &data->o_res.seq_res, task))
Andy Adamsond8985282009-04-01 09:22:21 -04001499 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001500 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001501 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001502unlock_no_action:
1503 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001504out_no_action:
1505 task->tk_action = NULL;
1506
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001507}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001509static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1510{
1511 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1512 nfs4_open_prepare(task, calldata);
1513}
1514
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001515static void nfs4_open_done(struct rpc_task *task, void *calldata)
1516{
1517 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001519 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001520
Trond Myklebust14516c32010-07-31 14:29:06 -04001521 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1522 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001523
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001524 if (task->tk_status == 0) {
1525 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001526 case S_IFREG:
1527 break;
1528 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001529 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001530 break;
1531 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001532 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001533 break;
1534 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001535 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001536 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001537 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001538 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1539 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001540 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001541 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001542}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001543
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001544static void nfs4_open_release(void *calldata)
1545{
1546 struct nfs4_opendata *data = calldata;
1547 struct nfs4_state *state = NULL;
1548
1549 /* If this request hasn't been cancelled, do nothing */
1550 if (data->cancelled == 0)
1551 goto out_free;
1552 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001553 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001554 goto out_free;
1555 /* In case we need an open_confirm, no cleanup! */
1556 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1557 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001558 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001559 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001560 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001561out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001562 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001563}
1564
1565static const struct rpc_call_ops nfs4_open_ops = {
1566 .rpc_call_prepare = nfs4_open_prepare,
1567 .rpc_call_done = nfs4_open_done,
1568 .rpc_release = nfs4_open_release,
1569};
1570
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001571static const struct rpc_call_ops nfs4_recover_open_ops = {
1572 .rpc_call_prepare = nfs4_recover_open_prepare,
1573 .rpc_call_done = nfs4_open_done,
1574 .rpc_release = nfs4_open_release,
1575};
1576
1577static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001578{
1579 struct inode *dir = data->dir->d_inode;
1580 struct nfs_server *server = NFS_SERVER(dir);
1581 struct nfs_openargs *o_arg = &data->o_arg;
1582 struct nfs_openres *o_res = &data->o_res;
1583 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001584 struct rpc_message msg = {
1585 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1586 .rpc_argp = o_arg,
1587 .rpc_resp = o_res,
1588 .rpc_cred = data->owner->so_cred,
1589 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001590 struct rpc_task_setup task_setup_data = {
1591 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001592 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001593 .callback_ops = &nfs4_open_ops,
1594 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001595 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001596 .flags = RPC_TASK_ASYNC,
1597 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001598 int status;
1599
Trond Myklebust9d12b212012-01-17 22:04:25 -05001600 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001601 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001602 data->rpc_done = 0;
1603 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001604 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001605 if (isrecover)
1606 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001607 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001608 if (IS_ERR(task))
1609 return PTR_ERR(task);
1610 status = nfs4_wait_for_completion_rpc_task(task);
1611 if (status != 0) {
1612 data->cancelled = 1;
1613 smp_wmb();
1614 } else
1615 status = data->rpc_status;
1616 rpc_put_task(task);
1617
1618 return status;
1619}
1620
1621static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1622{
1623 struct inode *dir = data->dir->d_inode;
1624 struct nfs_openres *o_res = &data->o_res;
1625 int status;
1626
1627 status = nfs4_run_open_task(data, 1);
1628 if (status != 0 || !data->rpc_done)
1629 return status;
1630
Trond Myklebust6926afd2012-01-07 13:22:46 -05001631 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1632
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001633 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1634 status = _nfs4_proc_open_confirm(data);
1635 if (status != 0)
1636 return status;
1637 }
1638
1639 return status;
1640}
1641
1642/*
1643 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1644 */
1645static int _nfs4_proc_open(struct nfs4_opendata *data)
1646{
1647 struct inode *dir = data->dir->d_inode;
1648 struct nfs_server *server = NFS_SERVER(dir);
1649 struct nfs_openargs *o_arg = &data->o_arg;
1650 struct nfs_openres *o_res = &data->o_res;
1651 int status;
1652
1653 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001654 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001655 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001656 if (status != 0) {
1657 if (status == -NFS4ERR_BADNAME &&
1658 !(o_arg->open_flags & O_CREAT))
1659 return -ENOENT;
1660 return status;
1661 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001662
Trond Myklebust6926afd2012-01-07 13:22:46 -05001663 nfs_fattr_map_and_free_names(server, &data->f_attr);
1664
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001665 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001666 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001667 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1668 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001670 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001672 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 }
1674 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001675 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001676 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677}
1678
Andy Adamsond83217c2011-03-01 01:34:17 +00001679static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +01001680{
Trond Myklebusta78cb572009-08-09 15:06:19 -04001681 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001682 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001683
Trond Myklebusta78cb572009-08-09 15:06:19 -04001684 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001685 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001686 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001687 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001688 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1689 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001690 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001691 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001692 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001693 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001694 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001695}
1696
Andy Adamsond83217c2011-03-01 01:34:17 +00001697static int nfs4_recover_expired_lease(struct nfs_server *server)
1698{
1699 return nfs4_client_recover_expired_lease(server->nfs_client);
1700}
1701
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702/*
1703 * OPEN_EXPIRED:
1704 * reclaim state on the server after a network partition.
1705 * Assumes caller holds the appropriate lock
1706 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001707static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001709 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001710 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001712 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1713 if (IS_ERR(opendata))
1714 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001715 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001716 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001717 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001718 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001719 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720}
1721
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001722static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001723{
Trond Myklebust539cd032007-06-05 11:46:42 -04001724 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001725 struct nfs4_exception exception = { };
1726 int err;
1727
1728 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001729 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001730 switch (err) {
1731 default:
1732 goto out;
1733 case -NFS4ERR_GRACE:
1734 case -NFS4ERR_DELAY:
1735 nfs4_handle_exception(server, err, &exception);
1736 err = 0;
1737 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001738 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001739out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001740 return err;
1741}
1742
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1744{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001746 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747
Trond Myklebust864472e2006-01-03 09:55:15 +01001748 ctx = nfs4_state_find_open_context(state);
1749 if (IS_ERR(ctx))
1750 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001751 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001752 put_nfs_open_context(ctx);
1753 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754}
1755
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001756#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001757static int nfs41_check_expired_stateid(struct nfs4_state *state, nfs4_stateid *stateid, unsigned int flags)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001758{
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001759 int status = NFS_OK;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001760 struct nfs_server *server = NFS_SERVER(state->inode);
1761
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001762 if (state->flags & flags) {
1763 status = nfs41_test_stateid(server, stateid);
1764 if (status != NFS_OK) {
1765 nfs41_free_stateid(server, stateid);
1766 state->flags &= ~flags;
1767 }
1768 }
1769 return status;
1770}
1771
1772static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1773{
1774 int deleg_status, open_status;
1775 int deleg_flags = 1 << NFS_DELEGATED_STATE;
1776 int open_flags = (1 << NFS_O_RDONLY_STATE) | (1 << NFS_O_WRONLY_STATE) | (1 << NFS_O_RDWR_STATE);
1777
1778 deleg_status = nfs41_check_expired_stateid(state, &state->stateid, deleg_flags);
1779 open_status = nfs41_check_expired_stateid(state, &state->open_stateid, open_flags);
1780
1781 if ((deleg_status == NFS_OK) && (open_status == NFS_OK))
1782 return NFS_OK;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001783 return nfs4_open_expired(sp, state);
1784}
1785#endif
1786
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001788 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1789 * fields corresponding to attributes that were used to store the verifier.
1790 * Make sure we clobber those fields in the later setattr call
1791 */
1792static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1793{
1794 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1795 !(sattr->ia_valid & ATTR_ATIME_SET))
1796 sattr->ia_valid |= ATTR_ATIME;
1797
1798 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1799 !(sattr->ia_valid & ATTR_MTIME_SET))
1800 sattr->ia_valid |= ATTR_MTIME;
1801}
1802
1803/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001804 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 */
Andy Adamson82be4172012-05-23 05:02:35 -04001806static int _nfs4_do_open(struct inode *dir,
1807 struct dentry *dentry,
1808 fmode_t fmode,
1809 int flags,
1810 struct iattr *sattr,
1811 struct rpc_cred *cred,
1812 struct nfs4_state **res,
1813 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814{
1815 struct nfs4_state_owner *sp;
1816 struct nfs4_state *state = NULL;
1817 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001818 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001819 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820
1821 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001823 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1824 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1826 goto out_err;
1827 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001828 status = nfs4_recover_expired_lease(server);
1829 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001830 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001831 if (dentry->d_inode != NULL)
1832 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001833 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001834 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001835 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001836 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
Andy Adamson82be4172012-05-23 05:02:35 -04001838 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
1839 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
1840 if (!opendata->f_attr.mdsthreshold)
1841 goto err_opendata_put;
Trond Myklebust1549210f2012-06-05 09:16:47 -04001842 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04001843 }
Al Viro82a2c1b2011-06-22 18:30:55 -04001844 if (dentry->d_inode != NULL)
1845 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001846
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001847 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001849 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001851 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001852 status = PTR_ERR(state);
1853 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001854 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001855 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001856 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001857
1858 if (opendata->o_arg.open_flags & O_EXCL) {
1859 nfs4_exclusive_attrset(opendata, sattr);
1860
1861 nfs_fattr_init(opendata->o_res.f_attr);
1862 status = nfs4_do_setattr(state->inode, cred,
1863 opendata->o_res.f_attr, sattr,
1864 state);
1865 if (status == 0)
1866 nfs_setattr_update_inode(state->inode, sattr);
1867 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1868 }
Andy Adamson82be4172012-05-23 05:02:35 -04001869
1870 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
1871 *ctx_th = opendata->f_attr.mdsthreshold;
1872 else
1873 kfree(opendata->f_attr.mdsthreshold);
1874 opendata->f_attr.mdsthreshold = NULL;
1875
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001876 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 *res = state;
1879 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001880err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04001881 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001882 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001883err_put_state_owner:
1884 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 *res = NULL;
1887 return status;
1888}
1889
1890
Andy Adamson82be4172012-05-23 05:02:35 -04001891static struct nfs4_state *nfs4_do_open(struct inode *dir,
1892 struct dentry *dentry,
1893 fmode_t fmode,
1894 int flags,
1895 struct iattr *sattr,
1896 struct rpc_cred *cred,
1897 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898{
1899 struct nfs4_exception exception = { };
1900 struct nfs4_state *res;
1901 int status;
1902
1903 do {
Andy Adamson82be4172012-05-23 05:02:35 -04001904 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
1905 &res, ctx_th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 if (status == 0)
1907 break;
1908 /* NOTE: BAD_SEQID means the server and client disagree about the
1909 * book-keeping w.r.t. state-changing operations
1910 * (OPEN/CLOSE/LOCK/LOCKU...)
1911 * It is actually a sign of a bug on the client or on the server.
1912 *
1913 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001914 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 * have unhashed the old state_owner for us, and that we can
1916 * therefore safely retry using a new one. We should still warn
1917 * the user though...
1918 */
1919 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04001920 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001921 " returned a bad sequence-id error!\n",
1922 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 exception.retry = 1;
1924 continue;
1925 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001926 /*
1927 * BAD_STATEID on OPEN means that the server cancelled our
1928 * state before it received the OPEN_CONFIRM.
1929 * Recover by retrying the request as per the discussion
1930 * on Page 181 of RFC3530.
1931 */
1932 if (status == -NFS4ERR_BAD_STATEID) {
1933 exception.retry = 1;
1934 continue;
1935 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001936 if (status == -EAGAIN) {
1937 /* We must have found a delegation */
1938 exception.retry = 1;
1939 continue;
1940 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1942 status, &exception));
1943 } while (exception.retry);
1944 return res;
1945}
1946
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001947static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1948 struct nfs_fattr *fattr, struct iattr *sattr,
1949 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001951 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001953 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 .iap = sattr,
1955 .server = server,
1956 .bitmask = server->attr_bitmask,
1957 };
1958 struct nfs_setattrres res = {
1959 .fattr = fattr,
1960 .server = server,
1961 };
1962 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001963 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1964 .rpc_argp = &arg,
1965 .rpc_resp = &res,
1966 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001968 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001969 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
Trond Myklebust0e574af2005-10-27 22:12:38 -04001971 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
Trond Myklebust4fc87962012-03-08 17:42:01 -05001973 if (state != NULL) {
1974 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
1975 current->files, current->tgid);
1976 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
1977 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001978 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001979 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05001980 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
Bryan Schumaker7c513052011-03-24 17:12:24 +00001982 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001983 if (status == 0 && state != NULL)
1984 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001985 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986}
1987
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001988static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1989 struct nfs_fattr *fattr, struct iattr *sattr,
1990 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001992 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001993 struct nfs4_exception exception = {
1994 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05001995 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001996 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 int err;
1998 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04001999 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2000 switch (err) {
2001 case -NFS4ERR_OPENMODE:
2002 if (state && !(state->state & FMODE_WRITE)) {
2003 err = -EBADF;
2004 if (sattr->ia_valid & ATTR_OPEN)
2005 err = -EACCES;
2006 goto out;
2007 }
2008 }
2009 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002011out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 return err;
2013}
2014
2015struct nfs4_closedata {
2016 struct inode *inode;
2017 struct nfs4_state *state;
2018 struct nfs_closeargs arg;
2019 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002020 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002021 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002022 bool roc;
2023 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024};
2025
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002026static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002027{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002028 struct nfs4_closedata *calldata = data;
2029 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002030 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002031
Fred Isamanf7e89172011-01-06 11:36:32 +00002032 if (calldata->roc)
2033 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002034 nfs4_put_open_state(calldata->state);
2035 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002036 nfs4_put_state_owner(sp);
Al Viro643168c2011-06-22 18:20:23 -04002037 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002038 kfree(calldata);
2039}
2040
Trond Myklebust88069f72009-12-08 08:33:16 -05002041static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2042 fmode_t fmode)
2043{
2044 spin_lock(&state->owner->so_lock);
2045 if (!(fmode & FMODE_READ))
2046 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2047 if (!(fmode & FMODE_WRITE))
2048 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2049 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2050 spin_unlock(&state->owner->so_lock);
2051}
2052
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002053static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002055 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 struct nfs_server *server = NFS_SERVER(calldata->inode);
2058
Chuck Levera3ca5652012-03-01 17:00:40 -05002059 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002060 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2061 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 /* hmm. we are done with the inode, and in the process of freeing
2063 * the state_owner. we keep this around to process errors
2064 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 switch (task->tk_status) {
2066 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002067 if (calldata->roc)
2068 pnfs_roc_set_barrier(state->inode,
2069 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002070 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002071 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002072 nfs4_close_clear_stateid_flags(state,
2073 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 break;
2075 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002076 case -NFS4ERR_OLD_STATEID:
2077 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002079 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002080 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002082 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2083 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002085 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002086 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002087 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088}
2089
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002090static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002092 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002093 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05002094 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002095
Chuck Levera3ca5652012-03-01 17:00:40 -05002096 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002097 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07002098 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04002099
Trond Myklebust88069f72009-12-08 08:33:16 -05002100 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2101 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002102 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002103 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002104 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002105 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002106 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2107 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2108 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002109 }
2110 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002111 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2112 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2113 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002114 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002115 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002116 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002117
2118 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002119 /* Note: exit _without_ calling nfs4_close_done */
2120 task->tk_action = NULL;
Chuck Levera3ca5652012-03-01 17:00:40 -05002121 goto out;
Trond Myklebust95121352005-10-18 14:20:12 -07002122 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002123
Fred Isamanf7e89172011-01-06 11:36:32 +00002124 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002125 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002126 if (calldata->roc &&
2127 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
2128 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
2129 task, NULL);
Chuck Levera3ca5652012-03-01 17:00:40 -05002130 goto out;
Fred Isamanf7e89172011-01-06 11:36:32 +00002131 }
2132 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002133
Trond Myklebust516a6af2005-10-27 22:12:41 -04002134 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002135 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04002136 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002137 &calldata->arg.seq_args,
2138 &calldata->res.seq_res,
2139 task))
Chuck Levera3ca5652012-03-01 17:00:40 -05002140 goto out;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002141 rpc_call_start(task);
Chuck Levera3ca5652012-03-01 17:00:40 -05002142out:
2143 dprintk("%s: done!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144}
2145
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002146static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002147 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002148 .rpc_call_done = nfs4_close_done,
2149 .rpc_release = nfs4_free_closedata,
2150};
2151
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152/*
2153 * It is possible for data to be read/written from a mem-mapped file
2154 * after the sys_close call (which hits the vfs layer as a flush).
2155 * This means that we can't safely call nfsv4 close on a file until
2156 * the inode is cleared. This in turn means that we are not good
2157 * NFSv4 citizens - we do not indicate to the server to update the file's
2158 * share state even when we are done with one of the three share
2159 * stateid's in the inode.
2160 *
2161 * NOTE: Caller must be holding the sp->so_owner semaphore!
2162 */
Al Viro643168c2011-06-22 18:20:23 -04002163int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002165 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002167 struct nfs4_state_owner *sp = state->owner;
2168 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002169 struct rpc_message msg = {
2170 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2171 .rpc_cred = state->owner->so_cred,
2172 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002173 struct rpc_task_setup task_setup_data = {
2174 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002175 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002176 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002177 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002178 .flags = RPC_TASK_ASYNC,
2179 };
Trond Myklebust95121352005-10-18 14:20:12 -07002180 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002182 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002184 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002185 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002186 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002188 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002189 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002191 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002192 if (calldata->arg.seqid == NULL)
2193 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002194 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002195 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002196 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002197 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002198 calldata->res.server = server;
Fred Isamanf7e89172011-01-06 11:36:32 +00002199 calldata->roc = roc;
Al Viro643168c2011-06-22 18:20:23 -04002200 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002201
Trond Myklebust1174dd12010-12-21 10:52:24 -05002202 msg.rpc_argp = &calldata->arg;
2203 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002204 task_setup_data.callback_data = calldata;
2205 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002206 if (IS_ERR(task))
2207 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002208 status = 0;
2209 if (wait)
2210 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002211 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002212 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002213out_free_calldata:
2214 kfree(calldata);
2215out:
Fred Isamanf7e89172011-01-06 11:36:32 +00002216 if (roc)
2217 pnfs_roc_release(state->inode);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002218 nfs4_put_open_state(state);
2219 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002220 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221}
2222
Trond Myklebust2b484292010-09-17 10:56:51 -04002223static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002224nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 struct nfs4_state *state;
2227
Trond Myklebust565277f2007-10-15 18:17:53 -04002228 /* Protect against concurrent sillydeletes */
Andy Adamson82be4172012-05-23 05:02:35 -04002229 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2230 ctx->cred, &ctx->mdsthreshold);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002231 if (IS_ERR(state))
2232 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002233 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002234 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235}
2236
Trond Myklebust1185a552009-12-03 15:54:02 -05002237static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002238{
2239 if (ctx->state == NULL)
2240 return;
2241 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002242 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002243 else
Al Viro643168c2011-06-22 18:20:23 -04002244 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002245}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246
2247static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2248{
Benny Halevy43652ad2009-04-01 09:21:54 -04002249 struct nfs4_server_caps_arg args = {
2250 .fhandle = fhandle,
2251 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 struct nfs4_server_caps_res res = {};
2253 struct rpc_message msg = {
2254 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002255 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 .rpc_resp = &res,
2257 };
2258 int status;
2259
Bryan Schumaker7c513052011-03-24 17:12:24 +00002260 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 if (status == 0) {
2262 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002263 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2264 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2265 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2266 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2267 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2269 server->caps |= NFS_CAP_ACLS;
2270 if (res.has_links != 0)
2271 server->caps |= NFS_CAP_HARDLINKS;
2272 if (res.has_symlinks != 0)
2273 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002274 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2275 server->caps |= NFS_CAP_FILEID;
2276 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2277 server->caps |= NFS_CAP_MODE;
2278 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2279 server->caps |= NFS_CAP_NLINK;
2280 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2281 server->caps |= NFS_CAP_OWNER;
2282 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2283 server->caps |= NFS_CAP_OWNER_GROUP;
2284 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2285 server->caps |= NFS_CAP_ATIME;
2286 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2287 server->caps |= NFS_CAP_CTIME;
2288 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2289 server->caps |= NFS_CAP_MTIME;
2290
Trond Myklebusta65318b2009-03-11 14:10:28 -04002291 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2292 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2293 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002295 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002297
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 return status;
2299}
2300
Trond Myklebust55a97592006-06-09 09:34:19 -04002301int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302{
2303 struct nfs4_exception exception = { };
2304 int err;
2305 do {
2306 err = nfs4_handle_exception(server,
2307 _nfs4_server_capabilities(server, fhandle),
2308 &exception);
2309 } while (exception.retry);
2310 return err;
2311}
2312
2313static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2314 struct nfs_fsinfo *info)
2315{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 struct nfs4_lookup_root_arg args = {
2317 .bitmask = nfs4_fattr_bitmap,
2318 };
2319 struct nfs4_lookup_res res = {
2320 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002321 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 .fh = fhandle,
2323 };
2324 struct rpc_message msg = {
2325 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2326 .rpc_argp = &args,
2327 .rpc_resp = &res,
2328 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002329
Trond Myklebust0e574af2005-10-27 22:12:38 -04002330 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002331 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332}
2333
2334static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2335 struct nfs_fsinfo *info)
2336{
2337 struct nfs4_exception exception = { };
2338 int err;
2339 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002340 err = _nfs4_lookup_root(server, fhandle, info);
2341 switch (err) {
2342 case 0:
2343 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002344 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002345 default:
2346 err = nfs4_handle_exception(server, err, &exception);
2347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002349out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 return err;
2351}
2352
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002353static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2354 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2355{
2356 struct rpc_auth *auth;
2357 int ret;
2358
2359 auth = rpcauth_create(flavor, server->client);
2360 if (!auth) {
2361 ret = -EIO;
2362 goto out;
2363 }
2364 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002365out:
2366 return ret;
2367}
2368
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002369static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002370 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002372 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002373 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002375 len = gss_mech_list_pseudoflavors(&flav_array[0]);
2376 flav_array[len] = RPC_AUTH_NULL;
2377 len += 1;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002378
2379 for (i = 0; i < len; i++) {
2380 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002381 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002382 continue;
2383 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002384 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002385 /*
2386 * -EACCESS could mean that the user doesn't have correct permissions
2387 * to access the mount. It could also mean that we tried to mount
2388 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2389 * existing mount programs don't handle -EACCES very well so it should
2390 * be mapped to -EPERM instead.
2391 */
2392 if (status == -EACCES)
2393 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002394 return status;
2395}
2396
2397/*
2398 * get the file handle for the "/" directory on the server
2399 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002400int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2401 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002402{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002403 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002404 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002405 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2406 /*
2407 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2408 * by nfs4_map_errors() as this function exits.
2409 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002410 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 if (status == 0)
2412 status = nfs4_server_capabilities(server, fhandle);
2413 if (status == 0)
2414 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002415 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416}
2417
Bryan Schumakerbae36242012-05-10 15:07:31 -04002418static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2419 struct nfs_fsinfo *info)
2420{
2421 int error;
2422 struct nfs_fattr *fattr = info->fattr;
2423
2424 error = nfs4_server_capabilities(server, mntfh);
2425 if (error < 0) {
2426 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2427 return error;
2428 }
2429
2430 error = nfs4_proc_getattr(server, mntfh, fattr);
2431 if (error < 0) {
2432 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2433 return error;
2434 }
2435
2436 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2437 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2438 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2439
2440 return error;
2441}
2442
Manoj Naik6b97fd32006-06-09 09:34:29 -04002443/*
2444 * Get locations and (maybe) other attributes of a referral.
2445 * Note that we'll actually follow the referral later when
2446 * we detect fsid mismatch in inode revalidation
2447 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002448static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2449 const struct qstr *name, struct nfs_fattr *fattr,
2450 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002451{
2452 int status = -ENOMEM;
2453 struct page *page = NULL;
2454 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002455
2456 page = alloc_page(GFP_KERNEL);
2457 if (page == NULL)
2458 goto out;
2459 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2460 if (locations == NULL)
2461 goto out;
2462
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002463 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002464 if (status != 0)
2465 goto out;
2466 /* Make sure server returned a different fsid for the referral */
2467 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002468 dprintk("%s: server did not return a different fsid for"
2469 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002470 status = -EIO;
2471 goto out;
2472 }
Andy Adamson533eb462011-06-13 18:25:56 -04002473 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2474 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002475
Andy Adamson533eb462011-06-13 18:25:56 -04002476 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002477 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002478 memset(fhandle, 0, sizeof(struct nfs_fh));
2479out:
2480 if (page)
2481 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002482 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002483 return status;
2484}
2485
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2487{
2488 struct nfs4_getattr_arg args = {
2489 .fh = fhandle,
2490 .bitmask = server->attr_bitmask,
2491 };
2492 struct nfs4_getattr_res res = {
2493 .fattr = fattr,
2494 .server = server,
2495 };
2496 struct rpc_message msg = {
2497 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2498 .rpc_argp = &args,
2499 .rpc_resp = &res,
2500 };
2501
Trond Myklebust0e574af2005-10-27 22:12:38 -04002502 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002503 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504}
2505
2506static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2507{
2508 struct nfs4_exception exception = { };
2509 int err;
2510 do {
2511 err = nfs4_handle_exception(server,
2512 _nfs4_proc_getattr(server, fhandle, fattr),
2513 &exception);
2514 } while (exception.retry);
2515 return err;
2516}
2517
2518/*
2519 * The file is not closed if it is opened due to the a request to change
2520 * the size of the file. The open call will not be needed once the
2521 * VFS layer lookup-intents are implemented.
2522 *
2523 * Close is called when the inode is destroyed.
2524 * If we haven't opened the file for O_WRONLY, we
2525 * need to in the size_change case to obtain a stateid.
2526 *
2527 * Got race?
2528 * Because OPEN is always done by name in nfsv4, it is
2529 * possible that we opened a different file by the same
2530 * name. We can recognize this race condition, but we
2531 * can't do anything about it besides returning an error.
2532 *
2533 * This will be fixed with VFS changes (lookup-intent).
2534 */
2535static int
2536nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2537 struct iattr *sattr)
2538{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002539 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002540 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002541 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 int status;
2543
Benny Halevy8a1636c2010-07-14 15:43:57 -04002544 if (pnfs_ld_layoutret_on_setattr(inode))
2545 pnfs_return_layout(inode);
2546
Trond Myklebust0e574af2005-10-27 22:12:38 -04002547 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548
Trond Myklebustd5308382005-11-04 15:33:38 -05002549 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002550 if (sattr->ia_valid & ATTR_FILE) {
2551 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002552
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002553 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002554 if (ctx) {
2555 cred = ctx->cred;
2556 state = ctx->state;
2557 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002558 }
2559
Trond Myklebust536e43d2012-01-17 22:04:26 -05002560 /* Deal with open(O_TRUNC) */
2561 if (sattr->ia_valid & ATTR_OPEN)
2562 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2563
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002564 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002565 if (status == 0)
2566 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 return status;
2568}
2569
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002570static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2571 const struct qstr *name, struct nfs_fh *fhandle,
2572 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002573{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002574 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002575 int status;
2576 struct nfs4_lookup_arg args = {
2577 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002578 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002579 .name = name,
2580 };
2581 struct nfs4_lookup_res res = {
2582 .server = server,
2583 .fattr = fattr,
2584 .fh = fhandle,
2585 };
2586 struct rpc_message msg = {
2587 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2588 .rpc_argp = &args,
2589 .rpc_resp = &res,
2590 };
2591
2592 nfs_fattr_init(fattr);
2593
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002595 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 dprintk("NFS reply lookup: %d\n", status);
2597 return status;
2598}
2599
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002600static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002601{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002602 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002603 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002604 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2605 fattr->nlink = 2;
2606}
2607
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002608static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2609 struct qstr *name, struct nfs_fh *fhandle,
2610 struct nfs_fattr *fattr)
2611{
2612 struct nfs4_exception exception = { };
2613 struct rpc_clnt *client = *clnt;
2614 int err;
2615 do {
2616 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2617 switch (err) {
2618 case -NFS4ERR_BADNAME:
2619 err = -ENOENT;
2620 goto out;
2621 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002622 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002623 goto out;
2624 case -NFS4ERR_WRONGSEC:
2625 err = -EPERM;
2626 if (client != *clnt)
2627 goto out;
2628
2629 client = nfs4_create_sec_client(client, dir, name);
2630 if (IS_ERR(client))
2631 return PTR_ERR(client);
2632
2633 exception.retry = 1;
2634 break;
2635 default:
2636 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2637 }
2638 } while (exception.retry);
2639
2640out:
2641 if (err == 0)
2642 *clnt = client;
2643 else if (client != *clnt)
2644 rpc_shutdown_client(client);
2645
2646 return err;
2647}
2648
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002649static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002650 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002652 int status;
2653 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002654
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002655 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2656 if (client != NFS_CLIENT(dir)) {
2657 rpc_shutdown_client(client);
2658 nfs_fixup_secinfo_attributes(fattr);
2659 }
2660 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661}
2662
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002663struct rpc_clnt *
2664nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2665 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2666{
2667 int status;
2668 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2669
2670 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2671 if (status < 0) {
2672 rpc_shutdown_client(client);
2673 return ERR_PTR(status);
2674 }
2675 return client;
2676}
2677
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2679{
Trond Myklebust76b32992007-08-10 17:45:11 -04002680 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 struct nfs4_accessargs args = {
2682 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002683 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002685 struct nfs4_accessres res = {
2686 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002687 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 struct rpc_message msg = {
2689 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2690 .rpc_argp = &args,
2691 .rpc_resp = &res,
2692 .rpc_cred = entry->cred,
2693 };
2694 int mode = entry->mask;
2695 int status;
2696
2697 /*
2698 * Determine which access bits we want to ask for...
2699 */
2700 if (mode & MAY_READ)
2701 args.access |= NFS4_ACCESS_READ;
2702 if (S_ISDIR(inode->i_mode)) {
2703 if (mode & MAY_WRITE)
2704 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2705 if (mode & MAY_EXEC)
2706 args.access |= NFS4_ACCESS_LOOKUP;
2707 } else {
2708 if (mode & MAY_WRITE)
2709 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2710 if (mode & MAY_EXEC)
2711 args.access |= NFS4_ACCESS_EXECUTE;
2712 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002713
2714 res.fattr = nfs_alloc_fattr();
2715 if (res.fattr == NULL)
2716 return -ENOMEM;
2717
Bryan Schumaker7c513052011-03-24 17:12:24 +00002718 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 if (!status) {
2720 entry->mask = 0;
2721 if (res.access & NFS4_ACCESS_READ)
2722 entry->mask |= MAY_READ;
2723 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2724 entry->mask |= MAY_WRITE;
2725 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2726 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002727 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002729 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 return status;
2731}
2732
2733static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2734{
2735 struct nfs4_exception exception = { };
2736 int err;
2737 do {
2738 err = nfs4_handle_exception(NFS_SERVER(inode),
2739 _nfs4_proc_access(inode, entry),
2740 &exception);
2741 } while (exception.retry);
2742 return err;
2743}
2744
2745/*
2746 * TODO: For the time being, we don't try to get any attributes
2747 * along with any of the zero-copy operations READ, READDIR,
2748 * READLINK, WRITE.
2749 *
2750 * In the case of the first three, we want to put the GETATTR
2751 * after the read-type operation -- this is because it is hard
2752 * to predict the length of a GETATTR response in v4, and thus
2753 * align the READ data correctly. This means that the GETATTR
2754 * may end up partially falling into the page cache, and we should
2755 * shift it into the 'tail' of the xdr_buf before processing.
2756 * To do this efficiently, we need to know the total length
2757 * of data received, which doesn't seem to be available outside
2758 * of the RPC layer.
2759 *
2760 * In the case of WRITE, we also want to put the GETATTR after
2761 * the operation -- in this case because we want to make sure
2762 * we get the post-operation mtime and size. This means that
2763 * we can't use xdr_encode_pages() as written: we need a variant
2764 * of it which would leave room in the 'tail' iovec.
2765 *
2766 * Both of these changes to the XDR layer would in fact be quite
2767 * minor, but I decided to leave them for a subsequent patch.
2768 */
2769static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2770 unsigned int pgbase, unsigned int pglen)
2771{
2772 struct nfs4_readlink args = {
2773 .fh = NFS_FH(inode),
2774 .pgbase = pgbase,
2775 .pglen = pglen,
2776 .pages = &page,
2777 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002778 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 struct rpc_message msg = {
2780 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2781 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002782 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 };
2784
Bryan Schumaker7c513052011-03-24 17:12:24 +00002785 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 -07002786}
2787
2788static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2789 unsigned int pgbase, unsigned int pglen)
2790{
2791 struct nfs4_exception exception = { };
2792 int err;
2793 do {
2794 err = nfs4_handle_exception(NFS_SERVER(inode),
2795 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2796 &exception);
2797 } while (exception.retry);
2798 return err;
2799}
2800
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801/*
2802 * Got race?
2803 * We will need to arrange for the VFS layer to provide an atomic open.
2804 * Until then, this create/open method is prone to inefficiency and race
2805 * conditions due to the lookup, create, and open VFS calls from sys_open()
2806 * placed on the wire.
2807 *
2808 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2809 * The file will be opened again in the subsequent VFS open call
2810 * (nfs4_proc_file_open).
2811 *
2812 * The open for read will just hang around to be used by any process that
2813 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2814 */
2815
2816static int
2817nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002818 int flags, struct nfs_open_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819{
Al Viro82a2c1b2011-06-22 18:30:55 -04002820 struct dentry *de = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 struct nfs4_state *state;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002822 struct rpc_cred *cred = NULL;
2823 fmode_t fmode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 int status = 0;
2825
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002826 if (ctx != NULL) {
2827 cred = ctx->cred;
Al Viro3d4ff432011-06-22 18:40:12 -04002828 de = ctx->dentry;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002829 fmode = ctx->mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 }
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002831 sattr->ia_mode &= ~current_umask();
Andy Adamson82be4172012-05-23 05:02:35 -04002832 state = nfs4_do_open(dir, de, fmode, flags, sattr, cred, NULL);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002833 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 if (IS_ERR(state)) {
2835 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002836 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002838 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002839 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002840 if (ctx != NULL)
2841 ctx->state = state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002842 else
Al Viro643168c2011-06-22 18:20:23 -04002843 nfs4_close_sync(state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844out:
2845 return status;
2846}
2847
2848static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2849{
Trond Myklebust16e42952005-10-27 22:12:44 -04002850 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002851 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07002853 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002855 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002856 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002857 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002859 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2860 .rpc_argp = &args,
2861 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 };
Trond Myklebust778d2812012-04-27 13:48:19 -04002863 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04002864
Bryan Schumaker7c513052011-03-24 17:12:24 +00002865 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04002866 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04002867 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 return status;
2869}
2870
2871static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2872{
2873 struct nfs4_exception exception = { };
2874 int err;
2875 do {
2876 err = nfs4_handle_exception(NFS_SERVER(dir),
2877 _nfs4_proc_remove(dir, name),
2878 &exception);
2879 } while (exception.retry);
2880 return err;
2881}
2882
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002883static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002885 struct nfs_server *server = NFS_SERVER(dir);
2886 struct nfs_removeargs *args = msg->rpc_argp;
2887 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002889 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05002891 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892}
2893
Bryan Schumaker34e137c2012-03-19 14:54:41 -04002894static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
2895{
2896 if (nfs4_setup_sequence(NFS_SERVER(data->dir),
2897 &data->args.seq_args,
2898 &data->res.seq_res,
2899 task))
2900 return;
2901 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902}
2903
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002904static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002906 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2907
Trond Myklebust14516c32010-07-31 14:29:06 -04002908 if (!nfs4_sequence_done(task, &res->seq_res))
2909 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002910 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002911 return 0;
2912 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002913 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914}
2915
Jeff Laytond3d41522010-09-17 17:31:57 -04002916static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2917{
2918 struct nfs_server *server = NFS_SERVER(dir);
2919 struct nfs_renameargs *arg = msg->rpc_argp;
2920 struct nfs_renameres *res = msg->rpc_resp;
2921
2922 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04002923 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002924 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04002925}
2926
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04002927static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
2928{
2929 if (nfs4_setup_sequence(NFS_SERVER(data->old_dir),
2930 &data->args.seq_args,
2931 &data->res.seq_res,
2932 task))
2933 return;
2934 rpc_call_start(task);
Jeff Laytond3d41522010-09-17 17:31:57 -04002935}
2936
2937static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2938 struct inode *new_dir)
2939{
2940 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2941
2942 if (!nfs4_sequence_done(task, &res->seq_res))
2943 return 0;
2944 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2945 return 0;
2946
2947 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04002948 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04002949 return 1;
2950}
2951
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2953 struct inode *new_dir, struct qstr *new_name)
2954{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002955 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002956 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 .old_dir = NFS_FH(old_dir),
2958 .new_dir = NFS_FH(new_dir),
2959 .old_name = old_name,
2960 .new_name = new_name,
2961 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002962 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002963 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002964 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 struct rpc_message msg = {
2966 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2967 .rpc_argp = &arg,
2968 .rpc_resp = &res,
2969 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002970 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971
Bryan Schumaker7c513052011-03-24 17:12:24 +00002972 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 if (!status) {
2974 update_changeattr(old_dir, &res.old_cinfo);
2975 update_changeattr(new_dir, &res.new_cinfo);
2976 }
2977 return status;
2978}
2979
2980static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2981 struct inode *new_dir, struct qstr *new_name)
2982{
2983 struct nfs4_exception exception = { };
2984 int err;
2985 do {
2986 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2987 _nfs4_proc_rename(old_dir, old_name,
2988 new_dir, new_name),
2989 &exception);
2990 } while (exception.retry);
2991 return err;
2992}
2993
2994static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2995{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002996 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 struct nfs4_link_arg arg = {
2998 .fh = NFS_FH(inode),
2999 .dir_fh = NFS_FH(dir),
3000 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003001 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003003 struct nfs4_link_res res = {
3004 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003005 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 struct rpc_message msg = {
3007 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3008 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003009 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003011 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012
Trond Myklebust136f2622010-04-16 16:22:49 -04003013 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003014 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003015 goto out;
3016
Bryan Schumaker7c513052011-03-24 17:12:24 +00003017 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003018 if (!status) {
3019 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04003020 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003021 }
Trond Myklebust136f2622010-04-16 16:22:49 -04003022out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003023 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 return status;
3025}
3026
3027static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3028{
3029 struct nfs4_exception exception = { };
3030 int err;
3031 do {
3032 err = nfs4_handle_exception(NFS_SERVER(inode),
3033 _nfs4_proc_link(inode, dir, name),
3034 &exception);
3035 } while (exception.retry);
3036 return err;
3037}
3038
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003039struct nfs4_createdata {
3040 struct rpc_message msg;
3041 struct nfs4_create_arg arg;
3042 struct nfs4_create_res res;
3043 struct nfs_fh fh;
3044 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003045};
3046
3047static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3048 struct qstr *name, struct iattr *sattr, u32 ftype)
3049{
3050 struct nfs4_createdata *data;
3051
3052 data = kzalloc(sizeof(*data), GFP_KERNEL);
3053 if (data != NULL) {
3054 struct nfs_server *server = NFS_SERVER(dir);
3055
3056 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3057 data->msg.rpc_argp = &data->arg;
3058 data->msg.rpc_resp = &data->res;
3059 data->arg.dir_fh = NFS_FH(dir);
3060 data->arg.server = server;
3061 data->arg.name = name;
3062 data->arg.attrs = sattr;
3063 data->arg.ftype = ftype;
3064 data->arg.bitmask = server->attr_bitmask;
3065 data->res.server = server;
3066 data->res.fh = &data->fh;
3067 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003068 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003069 }
3070 return data;
3071}
3072
3073static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3074{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003075 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003076 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003077 if (status == 0) {
3078 update_changeattr(dir, &data->res.dir_cinfo);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003079 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3080 }
3081 return status;
3082}
3083
3084static void nfs4_free_createdata(struct nfs4_createdata *data)
3085{
3086 kfree(data);
3087}
3088
Chuck Lever4f390c12006-08-22 20:06:22 -04003089static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003090 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003092 struct nfs4_createdata *data;
3093 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094
Chuck Lever94a6d752006-08-22 20:06:23 -04003095 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003096 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003097
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003098 status = -ENOMEM;
3099 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3100 if (data == NULL)
3101 goto out;
3102
3103 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3104 data->arg.u.symlink.pages = &page;
3105 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003107 status = nfs4_do_create(dir, dentry, data);
3108
3109 nfs4_free_createdata(data);
3110out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 return status;
3112}
3113
Chuck Lever4f390c12006-08-22 20:06:22 -04003114static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003115 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116{
3117 struct nfs4_exception exception = { };
3118 int err;
3119 do {
3120 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003121 _nfs4_proc_symlink(dir, dentry, page,
3122 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 &exception);
3124 } while (exception.retry);
3125 return err;
3126}
3127
3128static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3129 struct iattr *sattr)
3130{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003131 struct nfs4_createdata *data;
3132 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003134 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3135 if (data == NULL)
3136 goto out;
3137
3138 status = nfs4_do_create(dir, dentry, data);
3139
3140 nfs4_free_createdata(data);
3141out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 return status;
3143}
3144
3145static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3146 struct iattr *sattr)
3147{
3148 struct nfs4_exception exception = { };
3149 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003150
3151 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 do {
3153 err = nfs4_handle_exception(NFS_SERVER(dir),
3154 _nfs4_proc_mkdir(dir, dentry, sattr),
3155 &exception);
3156 } while (exception.retry);
3157 return err;
3158}
3159
3160static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003161 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162{
3163 struct inode *dir = dentry->d_inode;
3164 struct nfs4_readdir_arg args = {
3165 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003166 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 .pgbase = 0,
3168 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003169 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003170 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 };
3172 struct nfs4_readdir_res res;
3173 struct rpc_message msg = {
3174 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3175 .rpc_argp = &args,
3176 .rpc_resp = &res,
3177 .rpc_cred = cred,
3178 };
3179 int status;
3180
Harvey Harrison3110ff82008-05-02 13:42:44 -07003181 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003182 dentry->d_parent->d_name.name,
3183 dentry->d_name.name,
3184 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
3186 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003187 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 -05003188 if (status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003190 status += args.pgbase;
3191 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003192
3193 nfs_invalidate_atime(dir);
3194
Harvey Harrison3110ff82008-05-02 13:42:44 -07003195 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 return status;
3197}
3198
3199static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003200 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201{
3202 struct nfs4_exception exception = { };
3203 int err;
3204 do {
3205 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3206 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003207 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 &exception);
3209 } while (exception.retry);
3210 return err;
3211}
3212
3213static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3214 struct iattr *sattr, dev_t rdev)
3215{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003216 struct nfs4_createdata *data;
3217 int mode = sattr->ia_mode;
3218 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219
3220 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3221 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003222
3223 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3224 if (data == NULL)
3225 goto out;
3226
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003228 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003230 data->arg.ftype = NF4BLK;
3231 data->arg.u.device.specdata1 = MAJOR(rdev);
3232 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 }
3234 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003235 data->arg.ftype = NF4CHR;
3236 data->arg.u.device.specdata1 = MAJOR(rdev);
3237 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003240 status = nfs4_do_create(dir, dentry, data);
3241
3242 nfs4_free_createdata(data);
3243out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 return status;
3245}
3246
3247static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3248 struct iattr *sattr, dev_t rdev)
3249{
3250 struct nfs4_exception exception = { };
3251 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003252
3253 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 do {
3255 err = nfs4_handle_exception(NFS_SERVER(dir),
3256 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3257 &exception);
3258 } while (exception.retry);
3259 return err;
3260}
3261
3262static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3263 struct nfs_fsstat *fsstat)
3264{
3265 struct nfs4_statfs_arg args = {
3266 .fh = fhandle,
3267 .bitmask = server->attr_bitmask,
3268 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003269 struct nfs4_statfs_res res = {
3270 .fsstat = fsstat,
3271 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 struct rpc_message msg = {
3273 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3274 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003275 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 };
3277
Trond Myklebust0e574af2005-10-27 22:12:38 -04003278 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003279 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280}
3281
3282static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3283{
3284 struct nfs4_exception exception = { };
3285 int err;
3286 do {
3287 err = nfs4_handle_exception(server,
3288 _nfs4_proc_statfs(server, fhandle, fsstat),
3289 &exception);
3290 } while (exception.retry);
3291 return err;
3292}
3293
3294static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3295 struct nfs_fsinfo *fsinfo)
3296{
3297 struct nfs4_fsinfo_arg args = {
3298 .fh = fhandle,
3299 .bitmask = server->attr_bitmask,
3300 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003301 struct nfs4_fsinfo_res res = {
3302 .fsinfo = fsinfo,
3303 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 struct rpc_message msg = {
3305 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3306 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003307 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 };
3309
Bryan Schumaker7c513052011-03-24 17:12:24 +00003310 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311}
3312
3313static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3314{
3315 struct nfs4_exception exception = { };
3316 int err;
3317
3318 do {
3319 err = nfs4_handle_exception(server,
3320 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3321 &exception);
3322 } while (exception.retry);
3323 return err;
3324}
3325
3326static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3327{
Trond Myklebust0e574af2005-10-27 22:12:38 -04003328 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3330}
3331
3332static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3333 struct nfs_pathconf *pathconf)
3334{
3335 struct nfs4_pathconf_arg args = {
3336 .fh = fhandle,
3337 .bitmask = server->attr_bitmask,
3338 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003339 struct nfs4_pathconf_res res = {
3340 .pathconf = pathconf,
3341 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 struct rpc_message msg = {
3343 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3344 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003345 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 };
3347
3348 /* None of the pathconf attributes are mandatory to implement */
3349 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3350 memset(pathconf, 0, sizeof(*pathconf));
3351 return 0;
3352 }
3353
Trond Myklebust0e574af2005-10-27 22:12:38 -04003354 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003355 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356}
3357
3358static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3359 struct nfs_pathconf *pathconf)
3360{
3361 struct nfs4_exception exception = { };
3362 int err;
3363
3364 do {
3365 err = nfs4_handle_exception(server,
3366 _nfs4_proc_pathconf(server, fhandle, pathconf),
3367 &exception);
3368 } while (exception.retry);
3369 return err;
3370}
3371
Benny Halevyd20581a2011-05-22 19:52:03 +03003372void __nfs4_read_done_cb(struct nfs_read_data *data)
3373{
Fred Isamancd841602012-04-20 14:47:44 -04003374 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003375}
3376
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003377static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378{
Fred Isamancd841602012-04-20 14:47:44 -04003379 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003381 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003382 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003383 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003385
Benny Halevyd20581a2011-05-22 19:52:03 +03003386 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003388 renew_lease(server, data->timestamp);
3389 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390}
3391
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003392static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3393{
3394
3395 dprintk("--> %s\n", __func__);
3396
3397 if (!nfs4_sequence_done(task, &data->res.seq_res))
3398 return -EAGAIN;
3399
Benny Halevyd20581a2011-05-22 19:52:03 +03003400 return data->read_done_cb ? data->read_done_cb(task, data) :
3401 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003402}
3403
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003404static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003407 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003408 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003409 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410}
3411
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003412static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3413{
Fred Isamancd841602012-04-20 14:47:44 -04003414 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003415 &data->args.seq_args,
3416 &data->res.seq_res,
3417 task))
3418 return;
3419 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420}
3421
Fred Isamanb029bc92011-03-03 15:13:42 +00003422static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423{
Fred Isamancd841602012-04-20 14:47:44 -04003424 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003426 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003427 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003428 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003430 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003432 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003433 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003434 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435}
3436
Fred Isamanb029bc92011-03-03 15:13:42 +00003437static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3438{
3439 if (!nfs4_sequence_done(task, &data->res.seq_res))
3440 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003441 return data->write_done_cb ? data->write_done_cb(task, data) :
3442 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003443}
3444
Trond Myklebust5a37f852012-04-28 14:55:16 -04003445static
3446bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003447{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003448 const struct nfs_pgio_header *hdr = data->header;
3449
3450 /* Don't request attributes for pNFS or O_DIRECT writes */
3451 if (data->ds_clp != NULL || hdr->dreq != NULL)
3452 return false;
3453 /* Otherwise, request attributes if and only if we don't hold
3454 * a delegation
3455 */
3456 return nfs_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003457}
Fred Isamana69aef12011-03-03 15:13:47 +00003458
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003459static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460{
Fred Isamancd841602012-04-20 14:47:44 -04003461 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003462
Trond Myklebust5a37f852012-04-28 14:55:16 -04003463 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003464 data->args.bitmask = NULL;
3465 data->res.fattr = NULL;
3466 } else
3467 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003468
Fred Isamanb029bc92011-03-03 15:13:42 +00003469 if (!data->write_done_cb)
3470 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003471 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 data->timestamp = jiffies;
3473
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003474 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003475 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476}
3477
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003478static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3479{
Fred Isamancd841602012-04-20 14:47:44 -04003480 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003481 &data->args.seq_args,
3482 &data->res.seq_res,
3483 task))
3484 return;
3485 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486}
3487
Fred Isaman0b7c0152012-04-20 14:47:39 -04003488static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3489{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3491 &data->args.seq_args,
Trond Myklebust788e7a82006-03-20 13:44:27 -05003492 &data->res.seq_res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 return;
3495 rpc_call_start(task);
Trond Myklebust14516c32010-07-31 14:29:06 -04003496}
3497
Fred Isaman0b7c0152012-04-20 14:47:39 -04003498static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499{
3500 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003501
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003502 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003503 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003504 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003506 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507}
3508
Fred Isaman0b7c0152012-04-20 14:47:39 -04003509static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003510{
3511 if (!nfs4_sequence_done(task, &data->res.seq_res))
3512 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003513 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003514}
3515
Fred Isaman0b7c0152012-04-20 14:47:39 -04003516static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003518 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003519
Fred Isaman0b7c0152012-04-20 14:47:39 -04003520 if (data->commit_done_cb == NULL)
3521 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003522 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003523 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003524 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525}
3526
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003527struct nfs4_renewdata {
3528 struct nfs_client *client;
3529 unsigned long timestamp;
3530};
3531
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532/*
3533 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3534 * standalone procedure for queueing an asynchronous RENEW.
3535 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003536static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003537{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003538 struct nfs4_renewdata *data = calldata;
3539 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003540
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003541 if (atomic_read(&clp->cl_count) > 1)
3542 nfs4_schedule_state_renewal(clp);
3543 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003544 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003545}
3546
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003547static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003549 struct nfs4_renewdata *data = calldata;
3550 struct nfs_client *clp = data->client;
3551 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552
3553 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003554 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003555 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3556 return;
3557 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003558 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003559 return;
3560 }
3561 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003563 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564}
3565
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003566static const struct rpc_call_ops nfs4_renew_ops = {
3567 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003568 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003569};
3570
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003571static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572{
3573 struct rpc_message msg = {
3574 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3575 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003576 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003578 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003580 if (renew_flags == 0)
3581 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003582 if (!atomic_inc_not_zero(&clp->cl_count))
3583 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003584 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003585 if (data == NULL)
3586 return -ENOMEM;
3587 data->client = clp;
3588 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003590 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591}
3592
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003593static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594{
3595 struct rpc_message msg = {
3596 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3597 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003598 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599 };
3600 unsigned long now = jiffies;
3601 int status;
3602
3603 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3604 if (status < 0)
3605 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003606 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607 return 0;
3608}
3609
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003610static inline int nfs4_server_supports_acls(struct nfs_server *server)
3611{
3612 return (server->caps & NFS_CAP_ACLS)
3613 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3614 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3615}
3616
3617/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3618 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3619 * the stack.
3620 */
3621#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3622
Neil Hormane9e3d722011-03-04 19:26:03 -05003623static int buf_to_pages_noslab(const void *buf, size_t buflen,
3624 struct page **pages, unsigned int *pgbase)
3625{
3626 struct page *newpage, **spages;
3627 int rc = 0;
3628 size_t len;
3629 spages = pages;
3630
3631 do {
Jovi Zhang43b7c3f2011-03-02 23:19:37 +00003632 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003633 newpage = alloc_page(GFP_KERNEL);
3634
3635 if (newpage == NULL)
3636 goto unwind;
3637 memcpy(page_address(newpage), buf, len);
3638 buf += len;
3639 buflen -= len;
3640 *pages++ = newpage;
3641 rc++;
3642 } while (buflen != 0);
3643
3644 return rc;
3645
3646unwind:
3647 for(; rc > 0; rc--)
3648 __free_page(spages[rc-1]);
3649 return -ENOMEM;
3650}
3651
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003652struct nfs4_cached_acl {
3653 int cached;
3654 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003655 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003656};
3657
3658static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003659{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003660 struct nfs_inode *nfsi = NFS_I(inode);
3661
3662 spin_lock(&inode->i_lock);
3663 kfree(nfsi->nfs4_acl);
3664 nfsi->nfs4_acl = acl;
3665 spin_unlock(&inode->i_lock);
3666}
3667
3668static void nfs4_zap_acl_attr(struct inode *inode)
3669{
3670 nfs4_set_cached_acl(inode, NULL);
3671}
3672
3673static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3674{
3675 struct nfs_inode *nfsi = NFS_I(inode);
3676 struct nfs4_cached_acl *acl;
3677 int ret = -ENOENT;
3678
3679 spin_lock(&inode->i_lock);
3680 acl = nfsi->nfs4_acl;
3681 if (acl == NULL)
3682 goto out;
3683 if (buf == NULL) /* user is just asking for length */
3684 goto out_len;
3685 if (acl->cached == 0)
3686 goto out;
3687 ret = -ERANGE; /* see getxattr(2) man page */
3688 if (acl->len > buflen)
3689 goto out;
3690 memcpy(buf, acl->data, acl->len);
3691out_len:
3692 ret = acl->len;
3693out:
3694 spin_unlock(&inode->i_lock);
3695 return ret;
3696}
3697
Sachin Prabhu5794d212012-04-17 14:36:40 +01003698static 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 +00003699{
3700 struct nfs4_cached_acl *acl;
3701
Sachin Prabhu5794d212012-04-17 14:36:40 +01003702 if (pages && acl_len <= PAGE_SIZE) {
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003703 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3704 if (acl == NULL)
3705 goto out;
3706 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003707 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003708 } else {
3709 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3710 if (acl == NULL)
3711 goto out;
3712 acl->cached = 0;
3713 }
3714 acl->len = acl_len;
3715out:
3716 nfs4_set_cached_acl(inode, acl);
3717}
3718
Andy Adamsonbf118a32011-12-07 11:55:27 -05003719/*
3720 * The getxattr API returns the required buffer length when called with a
3721 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3722 * the required buf. On a NULL buf, we send a page of data to the server
3723 * guessing that the ACL request can be serviced by a page. If so, we cache
3724 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3725 * the cache. If not so, we throw away the page, and cache the required
3726 * length. The next getxattr call will then produce another round trip to
3727 * the server, this time with the input buf of the required size.
3728 */
Trond Myklebust16b42892006-08-24 12:27:15 -04003729static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003730{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003731 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003732 struct nfs_getaclargs args = {
3733 .fh = NFS_FH(inode),
3734 .acl_pages = pages,
3735 .acl_len = buflen,
3736 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003737 struct nfs_getaclres res = {
3738 .acl_len = buflen,
3739 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003740 struct rpc_message msg = {
3741 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3742 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003743 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003744 };
Andy Adamsonbf118a32011-12-07 11:55:27 -05003745 int ret = -ENOMEM, npages, i, acl_len = 0;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003746
Andy Adamsonbf118a32011-12-07 11:55:27 -05003747 npages = (buflen + PAGE_SIZE - 1) >> PAGE_SHIFT;
3748 /* As long as we're doing a round trip to the server anyway,
3749 * let's be prepared for a page of acl data. */
3750 if (npages == 0)
3751 npages = 1;
3752
Sachin Prabhu5a006892012-04-17 14:35:39 +01003753 /* Add an extra page to handle the bitmap returned */
3754 npages++;
3755
Andy Adamsonbf118a32011-12-07 11:55:27 -05003756 for (i = 0; i < npages; i++) {
3757 pages[i] = alloc_page(GFP_KERNEL);
3758 if (!pages[i])
3759 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003760 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01003761
3762 /* for decoding across pages */
3763 res.acl_scratch = alloc_page(GFP_KERNEL);
3764 if (!res.acl_scratch)
3765 goto out_free;
3766
Andy Adamsonbf118a32011-12-07 11:55:27 -05003767 args.acl_len = npages * PAGE_SIZE;
3768 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003769
Andy Adamsonbf118a32011-12-07 11:55:27 -05003770 /* Let decode_getfacl know not to fail if the ACL data is larger than
3771 * the page we send as a guess */
3772 if (buf == NULL)
3773 res.acl_flags |= NFS4_ACL_LEN_REQUEST;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003774
Peng Taode040be2012-01-10 22:42:47 +08003775 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003776 __func__, buf, buflen, npages, args.acl_len);
3777 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3778 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003779 if (ret)
3780 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003781
3782 acl_len = res.acl_len - res.acl_data_offset;
3783 if (acl_len > args.acl_len)
Sachin Prabhu5794d212012-04-17 14:36:40 +01003784 nfs4_write_cached_acl(inode, NULL, 0, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003785 else
Sachin Prabhu5794d212012-04-17 14:36:40 +01003786 nfs4_write_cached_acl(inode, pages, res.acl_data_offset,
Andy Adamsonbf118a32011-12-07 11:55:27 -05003787 acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003788 if (buf) {
3789 ret = -ERANGE;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003790 if (acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003791 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003792 _copy_from_pages(buf, pages, res.acl_data_offset,
Sachin Prabhu20e0fa92012-03-22 16:46:28 +00003793 acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003794 }
Andy Adamsonbf118a32011-12-07 11:55:27 -05003795 ret = acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003796out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003797 for (i = 0; i < npages; i++)
3798 if (pages[i])
3799 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05003800 if (res.acl_scratch)
3801 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003802 return ret;
3803}
3804
Trond Myklebust16b42892006-08-24 12:27:15 -04003805static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3806{
3807 struct nfs4_exception exception = { };
3808 ssize_t ret;
3809 do {
3810 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3811 if (ret >= 0)
3812 break;
3813 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3814 } while (exception.retry);
3815 return ret;
3816}
3817
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003818static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3819{
3820 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003821 int ret;
3822
3823 if (!nfs4_server_supports_acls(server))
3824 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003825 ret = nfs_revalidate_inode(server, inode);
3826 if (ret < 0)
3827 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003828 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3829 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003830 ret = nfs4_read_cached_acl(inode, buf, buflen);
3831 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003832 /* -ENOENT is returned if there is no ACL or if there is an ACL
3833 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003834 return ret;
3835 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003836}
3837
Trond Myklebust16b42892006-08-24 12:27:15 -04003838static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003839{
3840 struct nfs_server *server = NFS_SERVER(inode);
3841 struct page *pages[NFS4ACL_MAXPAGES];
3842 struct nfs_setaclargs arg = {
3843 .fh = NFS_FH(inode),
3844 .acl_pages = pages,
3845 .acl_len = buflen,
3846 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003847 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003848 struct rpc_message msg = {
3849 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3850 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003851 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003852 };
Neil Hormane9e3d722011-03-04 19:26:03 -05003853 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003854
3855 if (!nfs4_server_supports_acls(server))
3856 return -EOPNOTSUPP;
Neil Hormane9e3d722011-03-04 19:26:03 -05003857 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3858 if (i < 0)
3859 return i;
Trond Myklebust642ac542005-10-18 14:20:19 -07003860 nfs_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003861 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003862
3863 /*
3864 * Free each page after tx, so the only ref left is
3865 * held by the network stack
3866 */
3867 for (; i > 0; i--)
3868 put_page(pages[i-1]);
3869
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003870 /*
3871 * Acl update can result in inode attribute update.
3872 * so mark the attribute cache invalid.
3873 */
3874 spin_lock(&inode->i_lock);
3875 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3876 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003877 nfs_access_zap_cache(inode);
3878 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003879 return ret;
3880}
3881
Trond Myklebust16b42892006-08-24 12:27:15 -04003882static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3883{
3884 struct nfs4_exception exception = { };
3885 int err;
3886 do {
3887 err = nfs4_handle_exception(NFS_SERVER(inode),
3888 __nfs4_proc_set_acl(inode, buf, buflen),
3889 &exception);
3890 } while (exception.retry);
3891 return err;
3892}
3893
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003895nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003897 struct nfs_client *clp = server->nfs_client;
3898
3899 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900 return 0;
3901 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05003902 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003903 case -NFS4ERR_ADMIN_REVOKED:
3904 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04003905 if (state == NULL)
3906 break;
3907 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003908 case -NFS4ERR_OPENMODE:
3909 if (state == NULL)
3910 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003911 nfs4_schedule_stateid_recovery(server, state);
3912 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003913 case -NFS4ERR_EXPIRED:
3914 if (state != NULL)
3915 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003917 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003918 nfs4_schedule_lease_recovery(clp);
3919 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003920#if defined(CONFIG_NFS_V4_1)
3921 case -NFS4ERR_BADSESSION:
3922 case -NFS4ERR_BADSLOT:
3923 case -NFS4ERR_BAD_HIGH_SLOT:
3924 case -NFS4ERR_DEADSESSION:
3925 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3926 case -NFS4ERR_SEQ_FALSE_RETRY:
3927 case -NFS4ERR_SEQ_MISORDERED:
3928 dprintk("%s ERROR %d, Reset session\n", __func__,
3929 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04003930 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04003931 task->tk_status = 0;
3932 return -EAGAIN;
3933#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003935 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003936 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003937 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3939 task->tk_status = 0;
3940 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04003941 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 case -NFS4ERR_OLD_STATEID:
3943 task->tk_status = 0;
3944 return -EAGAIN;
3945 }
3946 task->tk_status = nfs4_map_errors(task->tk_status);
3947 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003948wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003949 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003950 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3951 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3952 task->tk_status = 0;
3953 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954}
3955
Chuck Leverf0920752012-05-21 22:45:41 -04003956static void nfs4_init_boot_verifier(const struct nfs_client *clp,
3957 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05003958{
3959 __be32 verf[2];
3960
Chuck Lever2c820d92012-05-21 22:45:33 -04003961 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
3962 /* An impossible timestamp guarantees this value
3963 * will never match a generated boot time. */
3964 verf[0] = 0;
3965 verf[1] = (__be32)(NSEC_PER_SEC + 1);
3966 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04003967 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
3968 verf[0] = (__be32)nn->boot_time.tv_sec;
3969 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04003970 }
Chuck Levercd937102012-03-02 17:14:31 -05003971 memcpy(bootverf->data, verf, sizeof(bootverf->data));
3972}
3973
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003974int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3975 unsigned short port, struct rpc_cred *cred,
3976 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977{
3978 nfs4_verifier sc_verifier;
3979 struct nfs4_setclientid setclientid = {
3980 .sc_verifier = &sc_verifier,
3981 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00003982 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983 };
3984 struct rpc_message msg = {
3985 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3986 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003987 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003988 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990 int loop = 0;
3991 int status;
3992
Chuck Leverf0920752012-05-21 22:45:41 -04003993 nfs4_init_boot_verifier(clp, &sc_verifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994
3995 for(;;) {
Trond Myklebust2446ab62012-03-01 17:00:56 -05003996 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003998 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003999 clp->cl_ipaddr,
4000 rpc_peeraddr2str(clp->cl_rpcclient,
4001 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05004002 rpc_peeraddr2str(clp->cl_rpcclient,
4003 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04004004 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005 clp->cl_id_uniquifier);
4006 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004007 sizeof(setclientid.sc_netid),
4008 rpc_peeraddr2str(clp->cl_rpcclient,
4009 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004011 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 clp->cl_ipaddr, port >> 8, port & 255);
Trond Myklebust2446ab62012-03-01 17:00:56 -05004013 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004015 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016 if (status != -NFS4ERR_CLID_INUSE)
4017 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004018 if (loop != 0) {
4019 ++clp->cl_id_uniquifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004021 }
4022 ++loop;
4023 ssleep(clp->cl_lease_time / HZ + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 }
4025 return status;
4026}
4027
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004028int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004029 struct nfs4_setclientid_res *arg,
4030 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031{
4032 struct nfs_fsinfo fsinfo;
4033 struct rpc_message msg = {
4034 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004035 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004037 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 };
4039 unsigned long now;
4040 int status;
4041
4042 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004043 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 if (status == 0) {
4045 spin_lock(&clp->cl_lock);
4046 clp->cl_lease_time = fsinfo.lease_time * HZ;
4047 clp->cl_last_renewal = now;
4048 spin_unlock(&clp->cl_lock);
4049 }
4050 return status;
4051}
4052
Trond Myklebustfe650402006-01-03 09:55:18 +01004053struct nfs4_delegreturndata {
4054 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004055 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004056 struct nfs_fh fh;
4057 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004058 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004059 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004060 int rpc_status;
4061};
4062
Trond Myklebustfe650402006-01-03 09:55:18 +01004063static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4064{
4065 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004066
Trond Myklebust14516c32010-07-31 14:29:06 -04004067 if (!nfs4_sequence_done(task, &data->res.seq_res))
4068 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004069
Ricardo Labiaga79708862009-12-07 09:23:21 -05004070 switch (task->tk_status) {
4071 case -NFS4ERR_STALE_STATEID:
4072 case -NFS4ERR_EXPIRED:
4073 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004074 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004075 break;
4076 default:
4077 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4078 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004079 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004080 return;
4081 }
4082 }
4083 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004084}
4085
4086static void nfs4_delegreturn_release(void *calldata)
4087{
Trond Myklebustfe650402006-01-03 09:55:18 +01004088 kfree(calldata);
4089}
4090
Andy Adamson938e1012009-04-01 09:22:28 -04004091#if defined(CONFIG_NFS_V4_1)
4092static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4093{
4094 struct nfs4_delegreturndata *d_data;
4095
4096 d_data = (struct nfs4_delegreturndata *)data;
4097
Trond Myklebust035168a2010-06-16 09:52:26 -04004098 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04004099 &d_data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004100 &d_data->res.seq_res, task))
Andy Adamson938e1012009-04-01 09:22:28 -04004101 return;
4102 rpc_call_start(task);
4103}
4104#endif /* CONFIG_NFS_V4_1 */
4105
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004106static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004107#if defined(CONFIG_NFS_V4_1)
4108 .rpc_call_prepare = nfs4_delegreturn_prepare,
4109#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004110 .rpc_call_done = nfs4_delegreturn_done,
4111 .rpc_release = nfs4_delegreturn_release,
4112};
4113
Trond Myklebuste6f81072008-01-24 18:14:34 -05004114static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004115{
4116 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004117 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004118 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004119 struct rpc_message msg = {
4120 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4121 .rpc_cred = cred,
4122 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004123 struct rpc_task_setup task_setup_data = {
4124 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004125 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004126 .callback_ops = &nfs4_delegreturn_ops,
4127 .flags = RPC_TASK_ASYNC,
4128 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004129 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004130
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004131 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004132 if (data == NULL)
4133 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004134 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004135 data->args.fhandle = &data->fh;
4136 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004137 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004138 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004139 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004140 data->res.fattr = &data->fattr;
4141 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004142 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004143 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004144 data->rpc_status = 0;
4145
Trond Myklebustc970aa82007-07-14 15:39:59 -04004146 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004147 msg.rpc_argp = &data->args;
4148 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004149 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004150 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004151 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004152 if (!issync)
4153 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004154 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004155 if (status != 0)
4156 goto out;
4157 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004158 if (status == 0)
4159 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4160 else
4161 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004162out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004163 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004164 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165}
4166
Trond Myklebuste6f81072008-01-24 18:14:34 -05004167int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168{
4169 struct nfs_server *server = NFS_SERVER(inode);
4170 struct nfs4_exception exception = { };
4171 int err;
4172 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004173 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 switch (err) {
4175 case -NFS4ERR_STALE_STATEID:
4176 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177 case 0:
4178 return 0;
4179 }
4180 err = nfs4_handle_exception(server, err, &exception);
4181 } while (exception.retry);
4182 return err;
4183}
4184
4185#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4186#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4187
4188/*
4189 * sleep, with exponential backoff, and retry the LOCK operation.
4190 */
4191static unsigned long
4192nfs4_set_lock_task_retry(unsigned long timeout)
4193{
Jeff Laytond3103102011-12-01 22:44:39 +01004194 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 timeout <<= 1;
4196 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4197 return NFS4_LOCK_MAXTIMEOUT;
4198 return timeout;
4199}
4200
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4202{
4203 struct inode *inode = state->inode;
4204 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004205 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004206 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004208 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004210 struct nfs_lockt_res res = {
4211 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212 };
4213 struct rpc_message msg = {
4214 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4215 .rpc_argp = &arg,
4216 .rpc_resp = &res,
4217 .rpc_cred = state->owner->so_cred,
4218 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219 struct nfs4_lock_state *lsp;
4220 int status;
4221
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004222 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004223 status = nfs4_set_lock_state(state, request);
4224 if (status != 0)
4225 goto out;
4226 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004227 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004228 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004229 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004230 switch (status) {
4231 case 0:
4232 request->fl_type = F_UNLCK;
4233 break;
4234 case -NFS4ERR_DENIED:
4235 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004237 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004238out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239 return status;
4240}
4241
4242static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4243{
4244 struct nfs4_exception exception = { };
4245 int err;
4246
4247 do {
4248 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4249 _nfs4_proc_getlk(state, cmd, request),
4250 &exception);
4251 } while (exception.retry);
4252 return err;
4253}
4254
4255static int do_vfs_lock(struct file *file, struct file_lock *fl)
4256{
4257 int res = 0;
4258 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4259 case FL_POSIX:
4260 res = posix_lock_file_wait(file, fl);
4261 break;
4262 case FL_FLOCK:
4263 res = flock_lock_file_wait(file, fl);
4264 break;
4265 default:
4266 BUG();
4267 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268 return res;
4269}
4270
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004271struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004272 struct nfs_locku_args arg;
4273 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004274 struct nfs4_lock_state *lsp;
4275 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004276 struct file_lock fl;
4277 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004278 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004279};
4280
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004281static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4282 struct nfs_open_context *ctx,
4283 struct nfs4_lock_state *lsp,
4284 struct nfs_seqid *seqid)
4285{
4286 struct nfs4_unlockdata *p;
4287 struct inode *inode = lsp->ls_state->inode;
4288
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004289 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004290 if (p == NULL)
4291 return NULL;
4292 p->arg.fh = NFS_FH(inode);
4293 p->arg.fl = &p->fl;
4294 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004295 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004296 p->arg.stateid = &lsp->ls_stateid;
4297 p->lsp = lsp;
4298 atomic_inc(&lsp->ls_count);
4299 /* Ensure we don't close file until we're done freeing locks! */
4300 p->ctx = get_nfs_open_context(ctx);
4301 memcpy(&p->fl, fl, sizeof(p->fl));
4302 p->server = NFS_SERVER(inode);
4303 return p;
4304}
4305
Trond Myklebust06f814a2006-01-03 09:55:07 +01004306static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004307{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004308 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004309 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004310 nfs4_put_lock_state(calldata->lsp);
4311 put_nfs_open_context(calldata->ctx);
4312 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004313}
4314
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004315static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004316{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004317 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004318
Trond Myklebust14516c32010-07-31 14:29:06 -04004319 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4320 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004321 switch (task->tk_status) {
4322 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004323 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4324 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004325 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004326 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004327 case -NFS4ERR_BAD_STATEID:
4328 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004329 case -NFS4ERR_STALE_STATEID:
4330 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004331 break;
4332 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004333 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004334 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004335 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004336}
4337
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004338static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004339{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004340 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004342 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004343 return;
4344 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004345 /* Note: exit _without_ running nfs4_locku_done */
4346 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004347 return;
4348 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004349 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004350 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004351 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004352 &calldata->res.seq_res, task))
Andy Adamsona8936932009-04-01 09:22:23 -04004353 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004354 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355}
4356
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004357static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004358 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004359 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004360 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004361};
4362
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004363static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4364 struct nfs_open_context *ctx,
4365 struct nfs4_lock_state *lsp,
4366 struct nfs_seqid *seqid)
4367{
4368 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004369 struct rpc_message msg = {
4370 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4371 .rpc_cred = ctx->cred,
4372 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004373 struct rpc_task_setup task_setup_data = {
4374 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004375 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004376 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004377 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004378 .flags = RPC_TASK_ASYNC,
4379 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004380
Frank Filz137d6ac2007-07-09 15:32:29 -07004381 /* Ensure this is an unlock - when canceling a lock, the
4382 * canceled lock is passed in, and it won't be an unlock.
4383 */
4384 fl->fl_type = F_UNLCK;
4385
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004386 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4387 if (data == NULL) {
4388 nfs_free_seqid(seqid);
4389 return ERR_PTR(-ENOMEM);
4390 }
4391
Trond Myklebust9d12b212012-01-17 22:04:25 -05004392 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004393 msg.rpc_argp = &data->arg;
4394 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004395 task_setup_data.callback_data = data;
4396 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004397}
4398
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4400{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004401 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004402 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004403 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004404 struct rpc_task *task;
4405 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004406 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407
Trond Myklebust9b073572006-06-29 16:38:34 -04004408 status = nfs4_set_lock_state(state, request);
4409 /* Unlock _before_ we do the RPC call */
4410 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004411 down_read(&nfsi->rwsem);
4412 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4413 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004414 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004415 }
4416 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004417 if (status != 0)
4418 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004419 /* Is this a delegated lock? */
4420 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004421 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004422 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004423 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004424 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004425 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004426 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004427 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004428 status = PTR_ERR(task);
4429 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004430 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004431 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004432 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004433out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004434 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004435 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436}
4437
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004438struct nfs4_lockdata {
4439 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004440 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004441 struct nfs4_lock_state *lsp;
4442 struct nfs_open_context *ctx;
4443 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004444 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004445 int rpc_status;
4446 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004447 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004448};
4449
4450static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004451 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4452 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004453{
4454 struct nfs4_lockdata *p;
4455 struct inode *inode = lsp->ls_state->inode;
4456 struct nfs_server *server = NFS_SERVER(inode);
4457
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004458 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004459 if (p == NULL)
4460 return NULL;
4461
4462 p->arg.fh = NFS_FH(inode);
4463 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004464 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004465 if (p->arg.open_seqid == NULL)
4466 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004467 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004468 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004469 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004470 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004471 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004472 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004473 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004474 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004475 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004476 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004477 atomic_inc(&lsp->ls_count);
4478 p->ctx = get_nfs_open_context(ctx);
4479 memcpy(&p->fl, fl, sizeof(p->fl));
4480 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004481out_free_seqid:
4482 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004483out_free:
4484 kfree(p);
4485 return NULL;
4486}
4487
4488static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4489{
4490 struct nfs4_lockdata *data = calldata;
4491 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492
Harvey Harrison3110ff82008-05-02 13:42:44 -07004493 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004494 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4495 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004496 /* Do we need to do an open_to_lock_owner? */
4497 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004498 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4499 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004500 data->arg.open_stateid = &state->stateid;
4501 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004502 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004503 } else
4504 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004505 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004506 if (nfs4_setup_sequence(data->server,
4507 &data->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004508 &data->res.seq_res, task))
Andy Adamson66179ef2009-04-01 09:22:22 -04004509 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004510 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004511 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004512}
4513
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004514static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4515{
4516 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4517 nfs4_lock_prepare(task, calldata);
4518}
4519
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004520static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4521{
4522 struct nfs4_lockdata *data = calldata;
4523
Harvey Harrison3110ff82008-05-02 13:42:44 -07004524 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004525
Trond Myklebust14516c32010-07-31 14:29:06 -04004526 if (!nfs4_sequence_done(task, &data->res.seq_res))
4527 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004528
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004529 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004530 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004531 if (data->rpc_status == 0)
4532 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4533 else
4534 goto out;
4535 }
4536 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004537 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004538 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Al Viro3d4ff432011-06-22 18:40:12 -04004539 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004540 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004541out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004542 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004543}
4544
4545static void nfs4_lock_release(void *calldata)
4546{
4547 struct nfs4_lockdata *data = calldata;
4548
Harvey Harrison3110ff82008-05-02 13:42:44 -07004549 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004550 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004551 if (data->cancelled != 0) {
4552 struct rpc_task *task;
4553 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4554 data->arg.lock_seqid);
4555 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004556 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004557 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004558 } else
4559 nfs_free_seqid(data->arg.lock_seqid);
4560 nfs4_put_lock_state(data->lsp);
4561 put_nfs_open_context(data->ctx);
4562 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004563 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004564}
4565
4566static const struct rpc_call_ops nfs4_lock_ops = {
4567 .rpc_call_prepare = nfs4_lock_prepare,
4568 .rpc_call_done = nfs4_lock_done,
4569 .rpc_release = nfs4_lock_release,
4570};
4571
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004572static const struct rpc_call_ops nfs4_recover_lock_ops = {
4573 .rpc_call_prepare = nfs4_recover_lock_prepare,
4574 .rpc_call_done = nfs4_lock_done,
4575 .rpc_release = nfs4_lock_release,
4576};
4577
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004578static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4579{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004580 switch (error) {
4581 case -NFS4ERR_ADMIN_REVOKED:
4582 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004583 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004584 if (new_lock_owner != 0 ||
4585 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004586 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004587 break;
4588 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004589 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004590 case -NFS4ERR_EXPIRED:
4591 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004592 };
4593}
4594
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004595static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004596{
4597 struct nfs4_lockdata *data;
4598 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004599 struct rpc_message msg = {
4600 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4601 .rpc_cred = state->owner->so_cred,
4602 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004603 struct rpc_task_setup task_setup_data = {
4604 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004605 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004606 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004607 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004608 .flags = RPC_TASK_ASYNC,
4609 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004610 int ret;
4611
Harvey Harrison3110ff82008-05-02 13:42:44 -07004612 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004613 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004614 fl->fl_u.nfs4_fl.owner,
4615 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004616 if (data == NULL)
4617 return -ENOMEM;
4618 if (IS_SETLKW(cmd))
4619 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004620 if (recovery_type > NFS_LOCK_NEW) {
4621 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004622 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004623 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4624 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05004625 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004626 msg.rpc_argp = &data->arg;
4627 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004628 task_setup_data.callback_data = data;
4629 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004630 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004631 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004632 ret = nfs4_wait_for_completion_rpc_task(task);
4633 if (ret == 0) {
4634 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004635 if (ret)
4636 nfs4_handle_setlk_error(data->server, data->lsp,
4637 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004638 } else
4639 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004640 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004641 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004642 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643}
4644
4645static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4646{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004647 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004648 struct nfs4_exception exception = {
4649 .inode = state->inode,
4650 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004651 int err;
4652
4653 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004654 /* Cache the lock if possible... */
4655 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4656 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004657 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004658 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004659 break;
4660 nfs4_handle_exception(server, err, &exception);
4661 } while (exception.retry);
4662 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663}
4664
4665static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4666{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004667 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004668 struct nfs4_exception exception = {
4669 .inode = state->inode,
4670 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004671 int err;
4672
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004673 err = nfs4_set_lock_state(state, request);
4674 if (err != 0)
4675 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004676 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004677 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4678 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004679 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004680 switch (err) {
4681 default:
4682 goto out;
4683 case -NFS4ERR_GRACE:
4684 case -NFS4ERR_DELAY:
4685 nfs4_handle_exception(server, err, &exception);
4686 err = 0;
4687 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004688 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004689out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004690 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691}
4692
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004693#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004694static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004695{
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004696 int status, ret = NFS_OK;
4697 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004698 struct nfs_server *server = NFS_SERVER(state->inode);
4699
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004700 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
4701 if (lsp->ls_flags & NFS_LOCK_INITIALIZED) {
4702 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4703 if (status != NFS_OK) {
4704 nfs41_free_stateid(server, &lsp->ls_stateid);
4705 lsp->ls_flags &= ~NFS_LOCK_INITIALIZED;
4706 ret = status;
4707 }
4708 }
4709 };
4710
4711 return ret;
4712}
4713
4714static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4715{
4716 int status = NFS_OK;
4717
4718 if (test_bit(LK_STATE_IN_USE, &state->flags))
4719 status = nfs41_check_expired_locks(state);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004720 if (status == NFS_OK)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004721 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004722 return nfs4_lock_expired(state, request);
4723}
4724#endif
4725
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4727{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004728 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004729 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004730 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004732 if ((fl_flags & FL_POSIX) &&
4733 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4734 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004735 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004736 status = nfs4_set_lock_state(state, request);
4737 if (status != 0)
4738 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004739 request->fl_flags |= FL_ACCESS;
4740 status = do_vfs_lock(request->fl_file, request);
4741 if (status < 0)
4742 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004743 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004744 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004745 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004746 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004747 request->fl_flags = fl_flags & ~FL_SLEEP;
4748 status = do_vfs_lock(request->fl_file, request);
4749 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004750 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004751 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004752 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004753 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004754 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004755 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004756 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004757 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4758 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004759out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004760 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004761out:
4762 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763 return status;
4764}
4765
4766static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4767{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004768 struct nfs4_exception exception = {
4769 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04004770 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004771 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772 int err;
4773
4774 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004775 err = _nfs4_proc_setlk(state, cmd, request);
4776 if (err == -NFS4ERR_DENIED)
4777 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004779 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780 } while (exception.retry);
4781 return err;
4782}
4783
4784static int
4785nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4786{
4787 struct nfs_open_context *ctx;
4788 struct nfs4_state *state;
4789 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4790 int status;
4791
4792 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004793 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794 state = ctx->state;
4795
4796 if (request->fl_start < 0 || request->fl_end < 0)
4797 return -EINVAL;
4798
Trond Myklebustd9531262009-07-21 19:22:38 -04004799 if (IS_GETLK(cmd)) {
4800 if (state != NULL)
4801 return nfs4_proc_getlk(state, F_GETLK, request);
4802 return 0;
4803 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804
4805 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4806 return -EINVAL;
4807
Trond Myklebustd9531262009-07-21 19:22:38 -04004808 if (request->fl_type == F_UNLCK) {
4809 if (state != NULL)
4810 return nfs4_proc_unlck(state, cmd, request);
4811 return 0;
4812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813
Trond Myklebustd9531262009-07-21 19:22:38 -04004814 if (state == NULL)
4815 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04004816 /*
4817 * Don't rely on the VFS having checked the file open mode,
4818 * since it won't do this for flock() locks.
4819 */
4820 switch (request->fl_type & (F_RDLCK|F_WRLCK|F_UNLCK)) {
4821 case F_RDLCK:
4822 if (!(filp->f_mode & FMODE_READ))
4823 return -EBADF;
4824 break;
4825 case F_WRLCK:
4826 if (!(filp->f_mode & FMODE_WRITE))
4827 return -EBADF;
4828 }
4829
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830 do {
4831 status = nfs4_proc_setlk(state, cmd, request);
4832 if ((status != -EAGAIN) || IS_SETLK(cmd))
4833 break;
4834 timeout = nfs4_set_lock_task_retry(timeout);
4835 status = -ERESTARTSYS;
4836 if (signalled())
4837 break;
4838 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839 return status;
4840}
4841
Trond Myklebust888e6942005-11-04 15:38:11 -05004842int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4843{
4844 struct nfs_server *server = NFS_SERVER(state->inode);
4845 struct nfs4_exception exception = { };
4846 int err;
4847
4848 err = nfs4_set_lock_state(state, fl);
4849 if (err != 0)
4850 goto out;
4851 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004852 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004853 switch (err) {
4854 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004855 printk(KERN_ERR "NFS: %s: unhandled error "
4856 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004857 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004858 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004859 goto out;
4860 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004861 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07004862 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004863 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004864 nfs4_schedule_lease_recovery(server->nfs_client);
4865 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004866 case -NFS4ERR_BADSESSION:
4867 case -NFS4ERR_BADSLOT:
4868 case -NFS4ERR_BAD_HIGH_SLOT:
4869 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4870 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04004871 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004872 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004873 case -ERESTARTSYS:
4874 /*
4875 * The show must go on: exit, but mark the
4876 * stateid as needing recovery.
4877 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004878 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004879 case -NFS4ERR_ADMIN_REVOKED:
4880 case -NFS4ERR_BAD_STATEID:
4881 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004882 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004883 err = 0;
4884 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004885 case -EKEYEXPIRED:
4886 /*
4887 * User RPCSEC_GSS context has expired.
4888 * We cannot recover this stateid now, so
4889 * skip it and allow recovery thread to
4890 * proceed.
4891 */
4892 err = 0;
4893 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004894 case -ENOMEM:
4895 case -NFS4ERR_DENIED:
4896 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4897 err = 0;
4898 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004899 case -NFS4ERR_DELAY:
4900 break;
4901 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004902 err = nfs4_handle_exception(server, err, &exception);
4903 } while (exception.retry);
4904out:
4905 return err;
4906}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004907
Trond Myklebustcf470c32012-03-07 13:49:12 -05004908struct nfs_release_lockowner_data {
4909 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004910 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004911 struct nfs_release_lockowner_args args;
4912};
4913
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004914static void nfs4_release_lockowner_release(void *calldata)
4915{
Trond Myklebustcf470c32012-03-07 13:49:12 -05004916 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004917 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004918 kfree(calldata);
4919}
4920
Trond Myklebust17280172012-03-11 13:11:00 -04004921static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004922 .rpc_release = nfs4_release_lockowner_release,
4923};
4924
Trond Myklebustcf470c32012-03-07 13:49:12 -05004925int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004926{
4927 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004928 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004929 struct rpc_message msg = {
4930 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4931 };
4932
4933 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05004934 return -EINVAL;
4935 data = kmalloc(sizeof(*data), GFP_NOFS);
4936 if (!data)
4937 return -ENOMEM;
4938 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004939 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004940 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
4941 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
4942 data->args.lock_owner.s_dev = server->s_dev;
4943 msg.rpc_argp = &data->args;
4944 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
4945 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004946}
4947
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004948#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4949
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004950static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4951 const void *buf, size_t buflen,
4952 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004953{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004954 if (strcmp(key, "") != 0)
4955 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004956
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004957 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004958}
4959
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004960static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4961 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004962{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004963 if (strcmp(key, "") != 0)
4964 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004965
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004966 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004967}
4968
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004969static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4970 size_t list_len, const char *name,
4971 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004972{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004973 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004974
J. Bruce Fields096455a2006-03-20 23:23:42 -05004975 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4976 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004977
4978 if (list && len <= list_len)
4979 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004980 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004981}
4982
Andy Adamson533eb462011-06-13 18:25:56 -04004983/*
4984 * nfs_fhget will use either the mounted_on_fileid or the fileid
4985 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004986static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4987{
Andy Adamson533eb462011-06-13 18:25:56 -04004988 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
4989 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
4990 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05004991 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004992 return;
4993
4994 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05004995 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004996 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4997 fattr->nlink = 2;
4998}
4999
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005000static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5001 const struct qstr *name,
5002 struct nfs4_fs_locations *fs_locations,
5003 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005004{
5005 struct nfs_server *server = NFS_SERVER(dir);
5006 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005007 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005008 };
5009 struct nfs4_fs_locations_arg args = {
5010 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005011 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005012 .page = page,
5013 .bitmask = bitmask,
5014 };
Benny Halevy22958462009-04-01 09:22:02 -04005015 struct nfs4_fs_locations_res res = {
5016 .fs_locations = fs_locations,
5017 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005018 struct rpc_message msg = {
5019 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5020 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005021 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005022 };
5023 int status;
5024
Harvey Harrison3110ff82008-05-02 13:42:44 -07005025 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005026
5027 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5028 * is not supported */
5029 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5030 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5031 else
5032 bitmask[0] |= FATTR4_WORD0_FILEID;
5033
Trond Myklebustc228fd32007-01-13 02:28:11 -05005034 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005035 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005036 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005037 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005038 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005039 return status;
5040}
5041
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005042int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5043 const struct qstr *name,
5044 struct nfs4_fs_locations *fs_locations,
5045 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005046{
5047 struct nfs4_exception exception = { };
5048 int err;
5049 do {
5050 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005051 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005052 &exception);
5053 } while (exception.retry);
5054 return err;
5055}
5056
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005057static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5058{
5059 int status;
5060 struct nfs4_secinfo_arg args = {
5061 .dir_fh = NFS_FH(dir),
5062 .name = name,
5063 };
5064 struct nfs4_secinfo_res res = {
5065 .flavors = flavors,
5066 };
5067 struct rpc_message msg = {
5068 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5069 .rpc_argp = &args,
5070 .rpc_resp = &res,
5071 };
5072
5073 dprintk("NFS call secinfo %s\n", name->name);
5074 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5075 dprintk("NFS reply secinfo: %d\n", status);
5076 return status;
5077}
5078
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005079int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5080 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005081{
5082 struct nfs4_exception exception = { };
5083 int err;
5084 do {
5085 err = nfs4_handle_exception(NFS_SERVER(dir),
5086 _nfs4_proc_secinfo(dir, name, flavors),
5087 &exception);
5088 } while (exception.retry);
5089 return err;
5090}
5091
Andy Adamson557134a2009-04-01 09:21:53 -04005092#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005093/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005094 * Check the exchange flags returned by the server for invalid flags, having
5095 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5096 * DS flags set.
5097 */
5098static int nfs4_check_cl_exchange_flags(u32 flags)
5099{
5100 if (flags & ~EXCHGID4_FLAG_MASK_R)
5101 goto out_inval;
5102 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5103 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5104 goto out_inval;
5105 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5106 goto out_inval;
5107 return NFS_OK;
5108out_inval:
5109 return -NFS4ERR_INVAL;
5110}
5111
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005112static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005113nfs41_same_server_scope(struct nfs41_server_scope *a,
5114 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005115{
5116 if (a->server_scope_sz == b->server_scope_sz &&
5117 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5118 return true;
5119
5120 return false;
5121}
5122
Andy Adamson357f54d2010-12-14 10:11:57 -05005123/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005124 * nfs4_proc_bind_conn_to_session()
5125 *
5126 * The 4.1 client currently uses the same TCP connection for the
5127 * fore and backchannel.
5128 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005129int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005130{
5131 int status;
5132 struct nfs41_bind_conn_to_session_res res;
5133 struct rpc_message msg = {
5134 .rpc_proc =
5135 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5136 .rpc_argp = clp,
5137 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005138 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005139 };
5140
5141 dprintk("--> %s\n", __func__);
5142 BUG_ON(clp == NULL);
5143
5144 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5145 if (unlikely(res.session == NULL)) {
5146 status = -ENOMEM;
5147 goto out;
5148 }
5149
5150 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5151 if (status == 0) {
5152 if (memcmp(res.session->sess_id.data,
5153 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5154 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5155 status = -EIO;
5156 goto out_session;
5157 }
5158 if (res.dir != NFS4_CDFS4_BOTH) {
5159 dprintk("NFS: %s: Unexpected direction from server\n",
5160 __func__);
5161 status = -EIO;
5162 goto out_session;
5163 }
5164 if (res.use_conn_in_rdma_mode) {
5165 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5166 __func__);
5167 status = -EIO;
5168 goto out_session;
5169 }
5170 }
5171out_session:
5172 kfree(res.session);
5173out:
5174 dprintk("<-- %s status= %d\n", __func__, status);
5175 return status;
5176}
5177
5178/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005179 * nfs4_proc_exchange_id()
5180 *
5181 * Since the clientid has expired, all compounds using sessions
5182 * associated with the stale clientid will be returning
5183 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5184 * be in some phase of session reset.
5185 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005186int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005187{
5188 nfs4_verifier verifier;
5189 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005190 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005191 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005192 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005193 };
5194 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005195 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005196 };
5197 int status;
5198 struct rpc_message msg = {
5199 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5200 .rpc_argp = &args,
5201 .rpc_resp = &res,
5202 .rpc_cred = cred,
5203 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005204
5205 dprintk("--> %s\n", __func__);
5206 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04005207
Chuck Leverf0920752012-05-21 22:45:41 -04005208 nfs4_init_boot_verifier(clp, &verifier);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005209
Andy Adamsonc7a360b2011-01-25 19:15:32 -05005210 args.id_len = scnprintf(args.id, sizeof(args.id),
Trond Myklebust3617e502012-04-30 12:04:58 -04005211 "%s/%s/%u",
Andy Adamsonc7a360b2011-01-25 19:15:32 -05005212 clp->cl_ipaddr,
Trond Myklebust3617e502012-04-30 12:04:58 -04005213 clp->cl_rpcclient->cl_nodename,
Andy Adamsonc7a360b2011-01-25 19:15:32 -05005214 clp->cl_rpcclient->cl_auth->au_flavor);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005215
Chuck Leveracdeb692012-05-21 22:46:16 -04005216 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005217 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005218 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005219 status = -ENOMEM;
5220 goto out;
5221 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005222
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005223 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005224 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005225 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005226 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005227 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005228 }
5229
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005230 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005231 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005232 status = -ENOMEM;
5233 goto out_server_scope;
5234 }
5235
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005236 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005237 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005238 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005239
Chuck Lever177313f2012-05-21 22:44:58 -04005240 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005241 clp->cl_clientid = res.clientid;
5242 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5243 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5244 clp->cl_seqid = res.seqid;
5245
Chuck Leveracdeb692012-05-21 22:46:16 -04005246 kfree(clp->cl_serverowner);
5247 clp->cl_serverowner = res.server_owner;
5248 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005249
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005250 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005251 kfree(clp->cl_implid);
5252 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005253
Chuck Lever177313f2012-05-21 22:44:58 -04005254 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005255 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005256 res.server_scope)) {
5257 dprintk("%s: server_scope mismatch detected\n",
5258 __func__);
5259 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005260 kfree(clp->cl_serverscope);
5261 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005262 }
5263
Chuck Lever177313f2012-05-21 22:44:58 -04005264 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005265 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005266 goto out;
5267 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005268 } else
5269 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005270
Chuck Leveracdeb692012-05-21 22:46:16 -04005271out_server_owner:
5272 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005273out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005274 kfree(res.server_scope);
5275out:
Chuck Lever177313f2012-05-21 22:44:58 -04005276 if (clp->cl_implid != NULL)
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005277 dprintk("%s: Server Implementation ID: "
5278 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever59155542012-05-21 22:44:41 -04005279 __func__, clp->cl_implid->domain, clp->cl_implid->name,
5280 clp->cl_implid->date.seconds,
5281 clp->cl_implid->date.nseconds);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005282 dprintk("<-- %s status= %d\n", __func__, status);
5283 return status;
5284}
5285
Trond Myklebust66245532012-05-25 17:18:09 -04005286static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5287 struct rpc_cred *cred)
5288{
5289 struct rpc_message msg = {
5290 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5291 .rpc_argp = clp,
5292 .rpc_cred = cred,
5293 };
5294 int status;
5295
5296 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5297 if (status)
5298 pr_warn("NFS: Got error %d from the server %s on "
5299 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5300 return status;
5301}
5302
5303static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5304 struct rpc_cred *cred)
5305{
5306 unsigned int loop;
5307 int ret;
5308
5309 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5310 ret = _nfs4_proc_destroy_clientid(clp, cred);
5311 switch (ret) {
5312 case -NFS4ERR_DELAY:
5313 case -NFS4ERR_CLIENTID_BUSY:
5314 ssleep(1);
5315 break;
5316 default:
5317 return ret;
5318 }
5319 }
5320 return 0;
5321}
5322
5323int nfs4_destroy_clientid(struct nfs_client *clp)
5324{
5325 struct rpc_cred *cred;
5326 int ret = 0;
5327
5328 if (clp->cl_mvops->minor_version < 1)
5329 goto out;
5330 if (clp->cl_exchange_flags == 0)
5331 goto out;
5332 cred = nfs4_get_exchange_id_cred(clp);
5333 ret = nfs4_proc_destroy_clientid(clp, cred);
5334 if (cred)
5335 put_rpccred(cred);
5336 switch (ret) {
5337 case 0:
5338 case -NFS4ERR_STALE_CLIENTID:
5339 clp->cl_exchange_flags = 0;
5340 }
5341out:
5342 return ret;
5343}
5344
Andy Adamson2050f0c2009-04-01 09:22:30 -04005345struct nfs4_get_lease_time_data {
5346 struct nfs4_get_lease_time_args *args;
5347 struct nfs4_get_lease_time_res *res;
5348 struct nfs_client *clp;
5349};
5350
5351static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5352 void *calldata)
5353{
5354 int ret;
5355 struct nfs4_get_lease_time_data *data =
5356 (struct nfs4_get_lease_time_data *)calldata;
5357
5358 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005359 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005360 /* just setup sequence, do not trigger session recovery
5361 since we're invoked within one */
5362 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005363 &data->args->la_seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005364 &data->res->lr_seq_res, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005365
5366 BUG_ON(ret == -EAGAIN);
5367 rpc_call_start(task);
5368 dprintk("<-- %s\n", __func__);
5369}
5370
5371/*
5372 * Called from nfs4_state_manager thread for session setup, so don't recover
5373 * from sequence operation or clientid errors.
5374 */
5375static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5376{
5377 struct nfs4_get_lease_time_data *data =
5378 (struct nfs4_get_lease_time_data *)calldata;
5379
5380 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005381 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5382 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005383 switch (task->tk_status) {
5384 case -NFS4ERR_DELAY:
5385 case -NFS4ERR_GRACE:
5386 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5387 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5388 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005389 /* fall through */
5390 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005391 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005392 return;
5393 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005394 dprintk("<-- %s\n", __func__);
5395}
5396
Trond Myklebust17280172012-03-11 13:11:00 -04005397static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005398 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5399 .rpc_call_done = nfs4_get_lease_time_done,
5400};
5401
5402int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5403{
5404 struct rpc_task *task;
5405 struct nfs4_get_lease_time_args args;
5406 struct nfs4_get_lease_time_res res = {
5407 .lr_fsinfo = fsinfo,
5408 };
5409 struct nfs4_get_lease_time_data data = {
5410 .args = &args,
5411 .res = &res,
5412 .clp = clp,
5413 };
5414 struct rpc_message msg = {
5415 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5416 .rpc_argp = &args,
5417 .rpc_resp = &res,
5418 };
5419 struct rpc_task_setup task_setup = {
5420 .rpc_client = clp->cl_rpcclient,
5421 .rpc_message = &msg,
5422 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005423 .callback_data = &data,
5424 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005425 };
5426 int status;
5427
Trond Myklebust9d12b212012-01-17 22:04:25 -05005428 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005429 dprintk("--> %s\n", __func__);
5430 task = rpc_run_task(&task_setup);
5431
5432 if (IS_ERR(task))
5433 status = PTR_ERR(task);
5434 else {
5435 status = task->tk_status;
5436 rpc_put_task(task);
5437 }
5438 dprintk("<-- %s return %d\n", __func__, status);
5439
5440 return status;
5441}
5442
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005443static struct nfs4_slot *nfs4_alloc_slots(u32 max_slots, gfp_t gfp_flags)
5444{
5445 return kcalloc(max_slots, sizeof(struct nfs4_slot), gfp_flags);
5446}
5447
5448static void nfs4_add_and_init_slots(struct nfs4_slot_table *tbl,
5449 struct nfs4_slot *new,
5450 u32 max_slots,
5451 u32 ivalue)
5452{
5453 struct nfs4_slot *old = NULL;
5454 u32 i;
5455
5456 spin_lock(&tbl->slot_tbl_lock);
5457 if (new) {
5458 old = tbl->slots;
5459 tbl->slots = new;
5460 tbl->max_slots = max_slots;
5461 }
5462 tbl->highest_used_slotid = -1; /* no slot is currently used */
5463 for (i = 0; i < tbl->max_slots; i++)
5464 tbl->slots[i].seq_nr = ivalue;
5465 spin_unlock(&tbl->slot_tbl_lock);
5466 kfree(old);
5467}
5468
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005469/*
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005470 * (re)Initialise a slot table
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005471 */
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005472static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
5473 u32 ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005474{
Andy Adamson104aeba2010-01-14 17:45:10 -05005475 struct nfs4_slot *new = NULL;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005476 int ret = -ENOMEM;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005477
Andy Adamson104aeba2010-01-14 17:45:10 -05005478 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5479 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005480
Andy Adamson104aeba2010-01-14 17:45:10 -05005481 /* Does the newly negotiated max_reqs match the existing slot table? */
5482 if (max_reqs != tbl->max_slots) {
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005483 new = nfs4_alloc_slots(max_reqs, GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05005484 if (!new)
5485 goto out;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005486 }
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005487 ret = 0;
5488
5489 nfs4_add_and_init_slots(tbl, new, max_reqs, ivalue);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005490 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5491 tbl, tbl->slots, tbl->max_slots);
5492out:
5493 dprintk("<-- %s: return %d\n", __func__, ret);
5494 return ret;
5495}
5496
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005497/* Destroy the slot table */
5498static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5499{
5500 if (session->fc_slot_table.slots != NULL) {
5501 kfree(session->fc_slot_table.slots);
5502 session->fc_slot_table.slots = NULL;
5503 }
5504 if (session->bc_slot_table.slots != NULL) {
5505 kfree(session->bc_slot_table.slots);
5506 session->bc_slot_table.slots = NULL;
5507 }
5508 return;
5509}
5510
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005511/*
Andy Adamsonaacd5532011-11-09 13:58:21 -05005512 * Initialize or reset the forechannel and backchannel tables
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005513 */
Andy Adamsonaacd5532011-11-09 13:58:21 -05005514static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005515{
Trond Myklebustf26468f2009-12-05 19:32:11 -05005516 struct nfs4_slot_table *tbl;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005517 int status;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005518
Andy Adamsonaacd5532011-11-09 13:58:21 -05005519 dprintk("--> %s\n", __func__);
5520 /* Fore channel */
5521 tbl = &ses->fc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005522 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5523 if (status) /* -ENOMEM */
5524 return status;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005525 /* Back channel */
5526 tbl = &ses->bc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005527 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
5528 if (status && tbl->slots == NULL)
5529 /* Fore and back channel share a connection so get
5530 * both slot tables or neither */
5531 nfs4_destroy_slot_tables(ses);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005532 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04005533}
5534
5535struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5536{
5537 struct nfs4_session *session;
5538 struct nfs4_slot_table *tbl;
5539
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005540 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04005541 if (!session)
5542 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04005543
Andy Adamson557134a2009-04-01 09:21:53 -04005544 tbl = &session->fc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005545 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamson557134a2009-04-01 09:21:53 -04005546 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005547 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005548 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005549
5550 tbl = &session->bc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005551 tbl->highest_used_slotid = NFS4_NO_SLOT;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005552 spin_lock_init(&tbl->slot_tbl_lock);
5553 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005554 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005555
Trond Myklebust1055d762010-06-16 09:52:27 -04005556 session->session_state = 1<<NFS4_SESSION_INITING;
5557
Andy Adamson557134a2009-04-01 09:21:53 -04005558 session->clp = clp;
5559 return session;
5560}
5561
5562void nfs4_destroy_session(struct nfs4_session *session)
5563{
Trond Myklebust2446ab62012-03-01 17:00:56 -05005564 struct rpc_xprt *xprt;
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005565 struct rpc_cred *cred;
Trond Myklebust2446ab62012-03-01 17:00:56 -05005566
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005567 cred = nfs4_get_exchange_id_cred(session->clp);
5568 nfs4_proc_destroy_session(session, cred);
5569 if (cred)
5570 put_rpccred(cred);
Trond Myklebust2446ab62012-03-01 17:00:56 -05005571
5572 rcu_read_lock();
5573 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
5574 rcu_read_unlock();
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005575 dprintk("%s Destroy backchannel for xprt %p\n",
Trond Myklebust2446ab62012-03-01 17:00:56 -05005576 __func__, xprt);
5577 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005578 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04005579 kfree(session);
5580}
5581
Andy Adamsonfc931582009-04-01 09:22:31 -04005582/*
5583 * Initialize the values to be used by the client in CREATE_SESSION
5584 * If nfs4_init_session set the fore channel request and response sizes,
5585 * use them.
5586 *
5587 * Set the back channel max_resp_sz_cached to zero to force the client to
5588 * always set csa_cachethis to FALSE because the current implementation
5589 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5590 */
5591static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5592{
5593 struct nfs4_session *session = args->client->cl_session;
5594 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5595 mxresp_sz = session->fc_attrs.max_resp_sz;
5596
5597 if (mxrqst_sz == 0)
5598 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5599 if (mxresp_sz == 0)
5600 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5601 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005602 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5603 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005604 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005605 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005606
5607 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005608 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005609 __func__,
5610 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005611 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005612
5613 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005614 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5615 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5616 args->bc_attrs.max_resp_sz_cached = 0;
5617 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5618 args->bc_attrs.max_reqs = 1;
5619
5620 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5621 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5622 __func__,
5623 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5624 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5625 args->bc_attrs.max_reqs);
5626}
5627
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005628static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005629{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005630 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5631 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5632
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005633 if (rcvd->max_resp_sz > sent->max_resp_sz)
5634 return -EINVAL;
5635 /*
5636 * Our requested max_ops is the minimum we need; we're not
5637 * prepared to break up compounds into smaller pieces than that.
5638 * So, no point even trying to continue if the server won't
5639 * cooperate:
5640 */
5641 if (rcvd->max_ops < sent->max_ops)
5642 return -EINVAL;
5643 if (rcvd->max_reqs == 0)
5644 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005645 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5646 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005647 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005648}
5649
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005650static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5651{
5652 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5653 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005654
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005655 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5656 return -EINVAL;
5657 if (rcvd->max_resp_sz < sent->max_resp_sz)
5658 return -EINVAL;
5659 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5660 return -EINVAL;
5661 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005662 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005663 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005664 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005665 return -EINVAL;
5666 return 0;
5667}
Andy Adamson8d353012009-04-01 09:22:32 -04005668
Andy Adamson8d353012009-04-01 09:22:32 -04005669static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5670 struct nfs4_session *session)
5671{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005672 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005673
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005674 ret = nfs4_verify_fore_channel_attrs(args, session);
5675 if (ret)
5676 return ret;
5677 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005678}
5679
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005680static int _nfs4_proc_create_session(struct nfs_client *clp,
5681 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005682{
5683 struct nfs4_session *session = clp->cl_session;
5684 struct nfs41_create_session_args args = {
5685 .client = clp,
5686 .cb_program = NFS4_CALLBACK,
5687 };
5688 struct nfs41_create_session_res res = {
5689 .client = clp,
5690 };
5691 struct rpc_message msg = {
5692 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5693 .rpc_argp = &args,
5694 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005695 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04005696 };
5697 int status;
5698
5699 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005700 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005701
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005702 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005703
Andy Adamson8d353012009-04-01 09:22:32 -04005704 if (!status)
5705 /* Verify the session's negotiated channel_attrs values */
5706 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005707 if (!status) {
5708 /* Increment the clientid slot sequence id */
5709 clp->cl_seqid++;
5710 }
5711
5712 return status;
5713}
5714
5715/*
5716 * Issues a CREATE_SESSION operation to the server.
5717 * It is the responsibility of the caller to verify the session is
5718 * expired before calling this routine.
5719 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005720int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005721{
5722 int status;
5723 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005724 struct nfs4_session *session = clp->cl_session;
5725
5726 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5727
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005728 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04005729 if (status)
5730 goto out;
5731
Andy Adamsonaacd5532011-11-09 13:58:21 -05005732 /* Init or reset the session slot tables */
5733 status = nfs4_setup_session_slot_tables(session);
5734 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005735 if (status)
5736 goto out;
5737
5738 ptr = (unsigned *)&session->sess_id.data[0];
5739 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5740 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005741out:
5742 dprintk("<-- %s\n", __func__);
5743 return status;
5744}
5745
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005746/*
5747 * Issue the over-the-wire RPC DESTROY_SESSION.
5748 * The caller must serialize access to this routine.
5749 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005750int nfs4_proc_destroy_session(struct nfs4_session *session,
5751 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005752{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005753 struct rpc_message msg = {
5754 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5755 .rpc_argp = session,
5756 .rpc_cred = cred,
5757 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005758 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005759
5760 dprintk("--> nfs4_proc_destroy_session\n");
5761
5762 /* session is still being setup */
5763 if (session->clp->cl_cons_state != NFS_CS_READY)
5764 return status;
5765
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005766 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005767
5768 if (status)
5769 printk(KERN_WARNING
Weston Andros Adamsonf9fd2d92012-01-26 13:32:22 -05005770 "NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005771 "Session has been destroyed regardless...\n", status);
5772
5773 dprintk("<-- nfs4_proc_destroy_session\n");
5774 return status;
5775}
5776
Trond Myklebust7b38c362012-05-23 13:23:31 -04005777/*
5778 * With sessions, the client is not marked ready until after a
5779 * successful EXCHANGE_ID and CREATE_SESSION.
5780 *
5781 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
5782 * other versions of NFS can be tried.
5783 */
5784static int nfs41_check_session_ready(struct nfs_client *clp)
5785{
5786 int ret;
5787
5788 if (clp->cl_cons_state == NFS_CS_SESSION_INITING) {
5789 ret = nfs4_client_recover_expired_lease(clp);
5790 if (ret)
5791 return ret;
5792 }
5793 if (clp->cl_cons_state < NFS_CS_READY)
5794 return -EPROTONOSUPPORT;
Trond Myklebust54ac4712012-05-23 13:26:10 -04005795 smp_rmb();
Trond Myklebust7b38c362012-05-23 13:23:31 -04005796 return 0;
5797}
5798
Trond Myklebustfccba802009-07-21 16:48:07 -04005799int nfs4_init_session(struct nfs_server *server)
5800{
5801 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005802 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005803 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005804
5805 if (!nfs4_has_session(clp))
5806 return 0;
5807
Trond Myklebust1055d762010-06-16 09:52:27 -04005808 session = clp->cl_session;
Trond Myklebust7b38c362012-05-23 13:23:31 -04005809 spin_lock(&clp->cl_lock);
5810 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
Trond Myklebust1055d762010-06-16 09:52:27 -04005811
Trond Myklebust7b38c362012-05-23 13:23:31 -04005812 rsize = server->rsize;
5813 if (rsize == 0)
5814 rsize = NFS_MAX_FILE_IO_SIZE;
5815 wsize = server->wsize;
5816 if (wsize == 0)
5817 wsize = NFS_MAX_FILE_IO_SIZE;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005818
Trond Myklebust7b38c362012-05-23 13:23:31 -04005819 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5820 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
5821 }
5822 spin_unlock(&clp->cl_lock);
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005823
Trond Myklebust7b38c362012-05-23 13:23:31 -04005824 return nfs41_check_session_ready(clp);
Trond Myklebustfccba802009-07-21 16:48:07 -04005825}
5826
Trond Myklebust7b38c362012-05-23 13:23:31 -04005827int nfs4_init_ds_session(struct nfs_client *clp, unsigned long lease_time)
Andy Adamsond83217c2011-03-01 01:34:17 +00005828{
5829 struct nfs4_session *session = clp->cl_session;
5830 int ret;
5831
Trond Myklebust7b38c362012-05-23 13:23:31 -04005832 spin_lock(&clp->cl_lock);
5833 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
5834 /*
5835 * Do not set NFS_CS_CHECK_LEASE_TIME instead set the
5836 * DS lease to be equal to the MDS lease.
5837 */
5838 clp->cl_lease_time = lease_time;
5839 clp->cl_last_renewal = jiffies;
5840 }
5841 spin_unlock(&clp->cl_lock);
Andy Adamsond83217c2011-03-01 01:34:17 +00005842
Trond Myklebust7b38c362012-05-23 13:23:31 -04005843 ret = nfs41_check_session_ready(clp);
5844 if (ret)
5845 return ret;
5846 /* Test for the DS role */
5847 if (!is_ds_client(clp))
5848 return -ENODEV;
5849 return 0;
Andy Adamsond83217c2011-03-01 01:34:17 +00005850}
5851EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5852
5853
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005854/*
5855 * Renew the cl_session lease.
5856 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005857struct nfs4_sequence_data {
5858 struct nfs_client *clp;
5859 struct nfs4_sequence_args args;
5860 struct nfs4_sequence_res res;
5861};
5862
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005863static void nfs41_sequence_release(void *data)
5864{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005865 struct nfs4_sequence_data *calldata = data;
5866 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005867
Alexandros Batsakis71358402010-02-05 03:45:05 -08005868 if (atomic_read(&clp->cl_count) > 1)
5869 nfs4_schedule_state_renewal(clp);
5870 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005871 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005872}
5873
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005874static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5875{
5876 switch(task->tk_status) {
5877 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005878 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5879 return -EAGAIN;
5880 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005881 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005882 }
5883 return 0;
5884}
5885
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005886static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005887{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005888 struct nfs4_sequence_data *calldata = data;
5889 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005890
Trond Myklebust14516c32010-07-31 14:29:06 -04005891 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5892 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005893
5894 if (task->tk_status < 0) {
5895 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005896 if (atomic_read(&clp->cl_count) == 1)
5897 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005898
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005899 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5900 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005901 return;
5902 }
5903 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005904 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005905out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005906 dprintk("<-- %s\n", __func__);
5907}
5908
5909static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5910{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005911 struct nfs4_sequence_data *calldata = data;
5912 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005913 struct nfs4_sequence_args *args;
5914 struct nfs4_sequence_res *res;
5915
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005916 args = task->tk_msg.rpc_argp;
5917 res = task->tk_msg.rpc_resp;
5918
Trond Myklebust9d12b212012-01-17 22:04:25 -05005919 if (nfs41_setup_sequence(clp->cl_session, args, res, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005920 return;
5921 rpc_call_start(task);
5922}
5923
5924static const struct rpc_call_ops nfs41_sequence_ops = {
5925 .rpc_call_done = nfs41_sequence_call_done,
5926 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005927 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005928};
5929
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005930static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005931{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005932 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005933 struct rpc_message msg = {
5934 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5935 .rpc_cred = cred,
5936 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005937 struct rpc_task_setup task_setup_data = {
5938 .rpc_client = clp->cl_rpcclient,
5939 .rpc_message = &msg,
5940 .callback_ops = &nfs41_sequence_ops,
5941 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5942 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005943
Alexandros Batsakis71358402010-02-05 03:45:05 -08005944 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005945 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005946 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005947 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005948 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005949 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005950 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05005951 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005952 msg.rpc_argp = &calldata->args;
5953 msg.rpc_resp = &calldata->res;
5954 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005955 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005956
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005957 return rpc_run_task(&task_setup_data);
5958}
5959
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005960static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005961{
5962 struct rpc_task *task;
5963 int ret = 0;
5964
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005965 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5966 return 0;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005967 task = _nfs41_proc_sequence(clp, cred);
5968 if (IS_ERR(task))
5969 ret = PTR_ERR(task);
5970 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005971 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005972 dprintk("<-- %s status=%d\n", __func__, ret);
5973 return ret;
5974}
5975
5976static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5977{
5978 struct rpc_task *task;
5979 int ret;
5980
5981 task = _nfs41_proc_sequence(clp, cred);
5982 if (IS_ERR(task)) {
5983 ret = PTR_ERR(task);
5984 goto out;
5985 }
5986 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005987 if (!ret) {
5988 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5989
5990 if (task->tk_status == 0)
5991 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005992 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005993 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005994 rpc_put_task(task);
5995out:
5996 dprintk("<-- %s status=%d\n", __func__, ret);
5997 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005998}
5999
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006000struct nfs4_reclaim_complete_data {
6001 struct nfs_client *clp;
6002 struct nfs41_reclaim_complete_args arg;
6003 struct nfs41_reclaim_complete_res res;
6004};
6005
6006static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6007{
6008 struct nfs4_reclaim_complete_data *calldata = data;
6009
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006010 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04006011 if (nfs41_setup_sequence(calldata->clp->cl_session,
6012 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006013 &calldata->res.seq_res, task))
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006014 return;
6015
6016 rpc_call_start(task);
6017}
6018
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006019static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6020{
6021 switch(task->tk_status) {
6022 case 0:
6023 case -NFS4ERR_COMPLETE_ALREADY:
6024 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6025 break;
6026 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006027 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006028 /* fall through */
6029 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006030 return -EAGAIN;
6031 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006032 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006033 }
6034 return 0;
6035}
6036
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006037static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6038{
6039 struct nfs4_reclaim_complete_data *calldata = data;
6040 struct nfs_client *clp = calldata->clp;
6041 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6042
6043 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006044 if (!nfs41_sequence_done(task, res))
6045 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006046
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006047 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6048 rpc_restart_call_prepare(task);
6049 return;
6050 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006051 dprintk("<-- %s\n", __func__);
6052}
6053
6054static void nfs4_free_reclaim_complete_data(void *data)
6055{
6056 struct nfs4_reclaim_complete_data *calldata = data;
6057
6058 kfree(calldata);
6059}
6060
6061static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6062 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6063 .rpc_call_done = nfs4_reclaim_complete_done,
6064 .rpc_release = nfs4_free_reclaim_complete_data,
6065};
6066
6067/*
6068 * Issue a global reclaim complete.
6069 */
6070static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
6071{
6072 struct nfs4_reclaim_complete_data *calldata;
6073 struct rpc_task *task;
6074 struct rpc_message msg = {
6075 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
6076 };
6077 struct rpc_task_setup task_setup_data = {
6078 .rpc_client = clp->cl_rpcclient,
6079 .rpc_message = &msg,
6080 .callback_ops = &nfs4_reclaim_complete_call_ops,
6081 .flags = RPC_TASK_ASYNC,
6082 };
6083 int status = -ENOMEM;
6084
6085 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006086 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006087 if (calldata == NULL)
6088 goto out;
6089 calldata->clp = clp;
6090 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006091
Trond Myklebust9d12b212012-01-17 22:04:25 -05006092 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006093 msg.rpc_argp = &calldata->arg;
6094 msg.rpc_resp = &calldata->res;
6095 task_setup_data.callback_data = calldata;
6096 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006097 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006098 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006099 goto out;
6100 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05006101 status = nfs4_wait_for_completion_rpc_task(task);
6102 if (status == 0)
6103 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006104 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006105 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006106out:
6107 dprintk("<-- %s status=%d\n", __func__, status);
6108 return status;
6109}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006110
6111static void
6112nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6113{
6114 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00006115 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006116
6117 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00006118 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6119 * right now covering the LAYOUTGET we are about to send.
6120 * However, that is not so catastrophic, and there seems
6121 * to be no way to prevent it completely.
6122 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006123 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006124 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006125 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006126 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6127 NFS_I(lgp->args.inode)->layout,
6128 lgp->args.ctx->state)) {
6129 rpc_exit(task, NFS4_OK);
6130 return;
6131 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006132 rpc_call_start(task);
6133}
6134
6135static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6136{
6137 struct nfs4_layoutget *lgp = calldata;
6138 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6139
6140 dprintk("--> %s\n", __func__);
6141
6142 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
6143 return;
6144
6145 switch (task->tk_status) {
6146 case 0:
6147 break;
6148 case -NFS4ERR_LAYOUTTRYLATER:
6149 case -NFS4ERR_RECALLCONFLICT:
6150 task->tk_status = -NFS4ERR_DELAY;
6151 /* Fall through */
6152 default:
6153 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6154 rpc_restart_call_prepare(task);
6155 return;
6156 }
6157 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006158 dprintk("<-- %s\n", __func__);
6159}
6160
6161static void nfs4_layoutget_release(void *calldata)
6162{
6163 struct nfs4_layoutget *lgp = calldata;
6164
6165 dprintk("--> %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006166 put_nfs_open_context(lgp->args.ctx);
6167 kfree(calldata);
6168 dprintk("<-- %s\n", __func__);
6169}
6170
6171static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6172 .rpc_call_prepare = nfs4_layoutget_prepare,
6173 .rpc_call_done = nfs4_layoutget_done,
6174 .rpc_release = nfs4_layoutget_release,
6175};
6176
6177int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
6178{
6179 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6180 struct rpc_task *task;
6181 struct rpc_message msg = {
6182 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6183 .rpc_argp = &lgp->args,
6184 .rpc_resp = &lgp->res,
6185 };
6186 struct rpc_task_setup task_setup_data = {
6187 .rpc_client = server->client,
6188 .rpc_message = &msg,
6189 .callback_ops = &nfs4_layoutget_call_ops,
6190 .callback_data = lgp,
6191 .flags = RPC_TASK_ASYNC,
6192 };
6193 int status = 0;
6194
6195 dprintk("--> %s\n", __func__);
6196
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006197 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006198 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006199 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006200 task = rpc_run_task(&task_setup_data);
6201 if (IS_ERR(task))
6202 return PTR_ERR(task);
6203 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006204 if (status == 0)
6205 status = task->tk_status;
6206 if (status == 0)
6207 status = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006208 rpc_put_task(task);
6209 dprintk("<-- %s status=%d\n", __func__, status);
6210 return status;
6211}
6212
Benny Halevycbe82602011-05-22 19:52:37 +03006213static void
6214nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6215{
6216 struct nfs4_layoutreturn *lrp = calldata;
6217
6218 dprintk("--> %s\n", __func__);
6219 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006220 &lrp->res.seq_res, task))
Benny Halevycbe82602011-05-22 19:52:37 +03006221 return;
6222 rpc_call_start(task);
6223}
6224
6225static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6226{
6227 struct nfs4_layoutreturn *lrp = calldata;
6228 struct nfs_server *server;
Trond Myklebusta56aaa02011-06-15 11:59:10 -04006229 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006230
6231 dprintk("--> %s\n", __func__);
6232
6233 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
6234 return;
6235
6236 server = NFS_SERVER(lrp->args.inode);
6237 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006238 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006239 return;
6240 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04006241 spin_lock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03006242 if (task->tk_status == 0) {
Benny Halevycbe82602011-05-22 19:52:37 +03006243 if (lrp->res.lrs_present) {
Benny Halevycbe82602011-05-22 19:52:37 +03006244 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Benny Halevycbe82602011-05-22 19:52:37 +03006245 } else
6246 BUG_ON(!list_empty(&lo->plh_segs));
6247 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04006248 lo->plh_block_lgets--;
6249 spin_unlock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03006250 dprintk("<-- %s\n", __func__);
6251}
6252
6253static void nfs4_layoutreturn_release(void *calldata)
6254{
6255 struct nfs4_layoutreturn *lrp = calldata;
6256
6257 dprintk("--> %s\n", __func__);
Trond Myklebusta56aaa02011-06-15 11:59:10 -04006258 put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006259 kfree(calldata);
6260 dprintk("<-- %s\n", __func__);
6261}
6262
6263static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6264 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6265 .rpc_call_done = nfs4_layoutreturn_done,
6266 .rpc_release = nfs4_layoutreturn_release,
6267};
6268
6269int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6270{
6271 struct rpc_task *task;
6272 struct rpc_message msg = {
6273 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6274 .rpc_argp = &lrp->args,
6275 .rpc_resp = &lrp->res,
6276 };
6277 struct rpc_task_setup task_setup_data = {
6278 .rpc_client = lrp->clp->cl_rpcclient,
6279 .rpc_message = &msg,
6280 .callback_ops = &nfs4_layoutreturn_call_ops,
6281 .callback_data = lrp,
6282 };
6283 int status;
6284
6285 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006286 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006287 task = rpc_run_task(&task_setup_data);
6288 if (IS_ERR(task))
6289 return PTR_ERR(task);
6290 status = task->tk_status;
6291 dprintk("<-- %s status=%d\n", __func__, status);
6292 rpc_put_task(task);
6293 return status;
6294}
6295
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006296/*
6297 * Retrieve the list of Data Server devices from the MDS.
6298 */
6299static int _nfs4_getdevicelist(struct nfs_server *server,
6300 const struct nfs_fh *fh,
6301 struct pnfs_devicelist *devlist)
6302{
6303 struct nfs4_getdevicelist_args args = {
6304 .fh = fh,
6305 .layoutclass = server->pnfs_curr_ld->id,
6306 };
6307 struct nfs4_getdevicelist_res res = {
6308 .devlist = devlist,
6309 };
6310 struct rpc_message msg = {
6311 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6312 .rpc_argp = &args,
6313 .rpc_resp = &res,
6314 };
6315 int status;
6316
6317 dprintk("--> %s\n", __func__);
6318 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6319 &res.seq_res, 0);
6320 dprintk("<-- %s status=%d\n", __func__, status);
6321 return status;
6322}
6323
6324int nfs4_proc_getdevicelist(struct nfs_server *server,
6325 const struct nfs_fh *fh,
6326 struct pnfs_devicelist *devlist)
6327{
6328 struct nfs4_exception exception = { };
6329 int err;
6330
6331 do {
6332 err = nfs4_handle_exception(server,
6333 _nfs4_getdevicelist(server, fh, devlist),
6334 &exception);
6335 } while (exception.retry);
6336
6337 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6338 err, devlist->num_devs);
6339
6340 return err;
6341}
6342EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6343
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006344static int
6345_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6346{
6347 struct nfs4_getdeviceinfo_args args = {
6348 .pdev = pdev,
6349 };
6350 struct nfs4_getdeviceinfo_res res = {
6351 .pdev = pdev,
6352 };
6353 struct rpc_message msg = {
6354 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6355 .rpc_argp = &args,
6356 .rpc_resp = &res,
6357 };
6358 int status;
6359
6360 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006361 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006362 dprintk("<-- %s status=%d\n", __func__, status);
6363
6364 return status;
6365}
6366
6367int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6368{
6369 struct nfs4_exception exception = { };
6370 int err;
6371
6372 do {
6373 err = nfs4_handle_exception(server,
6374 _nfs4_proc_getdeviceinfo(server, pdev),
6375 &exception);
6376 } while (exception.retry);
6377 return err;
6378}
6379EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6380
Andy Adamson863a3c62011-03-23 13:27:54 +00006381static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6382{
6383 struct nfs4_layoutcommit_data *data = calldata;
6384 struct nfs_server *server = NFS_SERVER(data->args.inode);
6385
6386 if (nfs4_setup_sequence(server, &data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006387 &data->res.seq_res, task))
Andy Adamson863a3c62011-03-23 13:27:54 +00006388 return;
6389 rpc_call_start(task);
6390}
6391
6392static void
6393nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6394{
6395 struct nfs4_layoutcommit_data *data = calldata;
6396 struct nfs_server *server = NFS_SERVER(data->args.inode);
6397
6398 if (!nfs4_sequence_done(task, &data->res.seq_res))
6399 return;
6400
6401 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006402 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6403 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6404 case -NFS4ERR_BADLAYOUT: /* no layout */
6405 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006406 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006407 break;
6408 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006409 nfs_post_op_update_inode_force_wcc(data->args.inode,
6410 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006411 break;
6412 default:
6413 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6414 rpc_restart_call_prepare(task);
6415 return;
6416 }
6417 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006418}
6419
6420static void nfs4_layoutcommit_release(void *calldata)
6421{
6422 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04006423 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07006424 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00006425
Andy Adamsondb29c082011-07-30 20:52:38 -04006426 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006427 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04006428 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6429 list_del_init(&lseg->pls_lc_list);
6430 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6431 &lseg->pls_flags))
6432 put_lseg(lseg);
6433 }
Peng Tao92407e72011-10-23 20:21:17 -07006434
6435 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6436 smp_mb__after_clear_bit();
6437 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6438
Andy Adamson863a3c62011-03-23 13:27:54 +00006439 put_rpccred(data->cred);
6440 kfree(data);
6441}
6442
6443static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6444 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6445 .rpc_call_done = nfs4_layoutcommit_done,
6446 .rpc_release = nfs4_layoutcommit_release,
6447};
6448
6449int
Andy Adamsonef311532011-03-12 02:58:10 -05006450nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006451{
6452 struct rpc_message msg = {
6453 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6454 .rpc_argp = &data->args,
6455 .rpc_resp = &data->res,
6456 .rpc_cred = data->cred,
6457 };
6458 struct rpc_task_setup task_setup_data = {
6459 .task = &data->task,
6460 .rpc_client = NFS_CLIENT(data->args.inode),
6461 .rpc_message = &msg,
6462 .callback_ops = &nfs4_layoutcommit_ops,
6463 .callback_data = data,
6464 .flags = RPC_TASK_ASYNC,
6465 };
6466 struct rpc_task *task;
6467 int status = 0;
6468
6469 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6470 "lbw: %llu inode %lu\n",
6471 data->task.tk_pid, sync,
6472 data->args.lastbytewritten,
6473 data->args.inode->i_ino);
6474
Trond Myklebust9d12b212012-01-17 22:04:25 -05006475 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006476 task = rpc_run_task(&task_setup_data);
6477 if (IS_ERR(task))
6478 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006479 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006480 goto out;
6481 status = nfs4_wait_for_completion_rpc_task(task);
6482 if (status != 0)
6483 goto out;
6484 status = task->tk_status;
6485out:
6486 dprintk("%s: status %d\n", __func__, status);
6487 rpc_put_task(task);
6488 return status;
6489}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006490
6491static int
6492_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6493 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6494{
6495 struct nfs41_secinfo_no_name_args args = {
6496 .style = SECINFO_STYLE_CURRENT_FH,
6497 };
6498 struct nfs4_secinfo_res res = {
6499 .flavors = flavors,
6500 };
6501 struct rpc_message msg = {
6502 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6503 .rpc_argp = &args,
6504 .rpc_resp = &res,
6505 };
6506 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6507}
6508
6509static int
6510nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6511 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6512{
6513 struct nfs4_exception exception = { };
6514 int err;
6515 do {
6516 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6517 switch (err) {
6518 case 0:
6519 case -NFS4ERR_WRONGSEC:
6520 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006521 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006522 default:
6523 err = nfs4_handle_exception(server, err, &exception);
6524 }
6525 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006526out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006527 return err;
6528}
6529
6530static int
6531nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6532 struct nfs_fsinfo *info)
6533{
6534 int err;
6535 struct page *page;
6536 rpc_authflavor_t flavor;
6537 struct nfs4_secinfo_flavors *flavors;
6538
6539 page = alloc_page(GFP_KERNEL);
6540 if (!page) {
6541 err = -ENOMEM;
6542 goto out;
6543 }
6544
6545 flavors = page_address(page);
6546 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6547
6548 /*
6549 * Fall back on "guess and check" method if
6550 * the server doesn't support SECINFO_NO_NAME
6551 */
6552 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6553 err = nfs4_find_root_sec(server, fhandle, info);
6554 goto out_freepage;
6555 }
6556 if (err)
6557 goto out_freepage;
6558
6559 flavor = nfs_find_best_sec(flavors);
6560 if (err == 0)
6561 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6562
6563out_freepage:
6564 put_page(page);
6565 if (err == -EACCES)
6566 return -EPERM;
6567out:
6568 return err;
6569}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006570
6571static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006572{
6573 int status;
6574 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006575 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006576 };
6577 struct nfs41_test_stateid_res res;
6578 struct rpc_message msg = {
6579 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6580 .rpc_argp = &args,
6581 .rpc_resp = &res,
6582 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006583
Trond Myklebust9d12b212012-01-17 22:04:25 -05006584 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006585 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
6586
6587 if (status == NFS_OK)
6588 return res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006589 return status;
6590}
6591
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006592static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006593{
6594 struct nfs4_exception exception = { };
6595 int err;
6596 do {
6597 err = nfs4_handle_exception(server,
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006598 _nfs41_test_stateid(server, stateid),
Bryan Schumaker7d974792011-06-02 14:59:08 -04006599 &exception);
6600 } while (exception.retry);
6601 return err;
6602}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006603
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006604static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006605{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006606 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006607 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006608 };
6609 struct nfs41_free_stateid_res res;
6610 struct rpc_message msg = {
6611 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6612 .rpc_argp = &args,
6613 .rpc_resp = &res,
6614 };
6615
Trond Myklebust9d12b212012-01-17 22:04:25 -05006616 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6617 return nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006618}
6619
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006620static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006621{
6622 struct nfs4_exception exception = { };
6623 int err;
6624 do {
6625 err = nfs4_handle_exception(server,
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006626 _nfs4_free_stateid(server, stateid),
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006627 &exception);
6628 } while (exception.retry);
6629 return err;
6630}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006631
6632static bool nfs41_match_stateid(const nfs4_stateid *s1,
6633 const nfs4_stateid *s2)
6634{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006635 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006636 return false;
6637
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006638 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006639 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006640 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006641 return true;
6642
6643 return false;
6644}
6645
Andy Adamson557134a2009-04-01 09:21:53 -04006646#endif /* CONFIG_NFS_V4_1 */
6647
Trond Myklebust36281ca2012-03-04 18:13:56 -05006648static bool nfs4_match_stateid(const nfs4_stateid *s1,
6649 const nfs4_stateid *s2)
6650{
Trond Myklebustf597c532012-03-04 18:13:56 -05006651 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006652}
6653
6654
Trond Myklebust17280172012-03-11 13:11:00 -04006655static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006656 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006657 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006658 .recover_open = nfs4_open_reclaim,
6659 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006660 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006661 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006662};
6663
Andy Adamson591d71c2009-04-01 09:22:47 -04006664#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006665static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006666 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6667 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6668 .recover_open = nfs4_open_reclaim,
6669 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006670 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006671 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006672 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04006673};
6674#endif /* CONFIG_NFS_V4_1 */
6675
Trond Myklebust17280172012-03-11 13:11:00 -04006676static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006677 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006678 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006679 .recover_open = nfs4_open_expired,
6680 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006681 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006682 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006683};
6684
Andy Adamson591d71c2009-04-01 09:22:47 -04006685#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006686static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006687 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6688 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006689 .recover_open = nfs41_open_expired,
6690 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006691 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006692 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006693};
6694#endif /* CONFIG_NFS_V4_1 */
6695
Trond Myklebust17280172012-03-11 13:11:00 -04006696static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006697 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006698 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006699 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006700};
6701
6702#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006703static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006704 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006705 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006706 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006707};
6708#endif
6709
Trond Myklebust97dc1352010-06-16 09:52:26 -04006710static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6711 .minor_version = 0,
6712 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006713 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006714 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006715 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6716 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6717 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006718};
6719
6720#if defined(CONFIG_NFS_V4_1)
6721static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6722 .minor_version = 1,
6723 .call_sync = _nfs4_call_sync_session,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006724 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006725 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006726 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6727 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6728 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006729};
6730#endif
6731
Trond Myklebust97dc1352010-06-16 09:52:26 -04006732const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6733 [0] = &nfs_v4_0_minor_ops,
6734#if defined(CONFIG_NFS_V4_1)
6735 [1] = &nfs_v4_1_minor_ops,
6736#endif
6737};
6738
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006739static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006740 .permission = nfs_permission,
6741 .getattr = nfs_getattr,
6742 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006743 .getxattr = generic_getxattr,
6744 .setxattr = generic_setxattr,
6745 .listxattr = generic_listxattr,
6746 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006747};
6748
David Howells509de812006-08-22 20:06:11 -04006749const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006750 .version = 4, /* protocol version */
6751 .dentry_ops = &nfs4_dentry_operations,
6752 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006753 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006754 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006755 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04006756 .submount = nfs4_submount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006757 .getattr = nfs4_proc_getattr,
6758 .setattr = nfs4_proc_setattr,
6759 .lookup = nfs4_proc_lookup,
6760 .access = nfs4_proc_access,
6761 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006762 .create = nfs4_proc_create,
6763 .remove = nfs4_proc_remove,
6764 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04006765 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006766 .unlink_done = nfs4_proc_unlink_done,
6767 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006768 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04006769 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04006770 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006771 .link = nfs4_proc_link,
6772 .symlink = nfs4_proc_symlink,
6773 .mkdir = nfs4_proc_mkdir,
6774 .rmdir = nfs4_proc_remove,
6775 .readdir = nfs4_proc_readdir,
6776 .mknod = nfs4_proc_mknod,
6777 .statfs = nfs4_proc_statfs,
6778 .fsinfo = nfs4_proc_fsinfo,
6779 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006780 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006781 .decode_dirent = nfs4_decode_dirent,
6782 .read_setup = nfs4_proc_read_setup,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04006783 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05006784 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006785 .write_setup = nfs4_proc_write_setup,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04006786 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006787 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006788 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04006789 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006790 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006791 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006792 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006793 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006794 .open_context = nfs4_atomic_open,
Andy Adamson45a52a02011-03-01 01:34:08 +00006795 .init_client = nfs4_init_client,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006796};
6797
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006798static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6799 .prefix = XATTR_NAME_NFSV4_ACL,
6800 .list = nfs4_xattr_list_nfs4_acl,
6801 .get = nfs4_xattr_get_nfs4_acl,
6802 .set = nfs4_xattr_set_nfs4_acl,
6803};
6804
6805const struct xattr_handler *nfs4_xattr_handlers[] = {
6806 &nfs4_xattr_nfs4_acl_handler,
6807 NULL
6808};
6809
Trond Myklebustef159e92012-02-06 19:50:40 -05006810module_param(max_session_slots, ushort, 0644);
6811MODULE_PARM_DESC(max_session_slots, "Maximum number of outstanding NFSv4.1 "
6812 "requests the client will negotiate");
6813
Linus Torvalds1da177e2005-04-16 15:20:36 -07006814/*
6815 * Local variables:
6816 * c-basic-offset: 8
6817 * End:
6818 */