blob: 42d9e9ca7d2fbe22af780544eb86b4d40ce3f31a [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 */
119const u32 nfs4_fattr_bitmap[2] = {
120 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
136const u32 nfs4_statfs_bitmap[2] = {
137 FATTR4_WORD0_FILES_AVAIL
138 | FATTR4_WORD0_FILES_FREE
139 | FATTR4_WORD0_FILES_TOTAL,
140 FATTR4_WORD1_SPACE_AVAIL
141 | FATTR4_WORD1_SPACE_FREE
142 | FATTR4_WORD1_SPACE_TOTAL
143};
144
Trond Myklebust4ce79712005-06-22 17:16:21 +0000145const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 FATTR4_WORD0_MAXLINK
147 | FATTR4_WORD0_MAXNAME,
148 0
149};
150
Fred Isamandae100c2011-07-30 20:52:37 -0400151const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 | FATTR4_WORD0_MAXREAD
153 | FATTR4_WORD0_MAXWRITE
154 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700155 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400156 | FATTR4_WORD1_FS_LAYOUT_TYPES,
157 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158};
159
Manoj Naik830b8e32006-06-09 09:34:25 -0400160const u32 nfs4_fs_locations_bitmap[2] = {
161 FATTR4_WORD0_TYPE
162 | FATTR4_WORD0_CHANGE
163 | FATTR4_WORD0_SIZE
164 | FATTR4_WORD0_FSID
165 | FATTR4_WORD0_FILEID
166 | FATTR4_WORD0_FS_LOCATIONS,
167 FATTR4_WORD1_MODE
168 | FATTR4_WORD1_NUMLINKS
169 | FATTR4_WORD1_OWNER
170 | FATTR4_WORD1_OWNER_GROUP
171 | FATTR4_WORD1_RAWDEV
172 | FATTR4_WORD1_SPACE_USED
173 | FATTR4_WORD1_TIME_ACCESS
174 | FATTR4_WORD1_TIME_METADATA
175 | FATTR4_WORD1_TIME_MODIFY
176 | FATTR4_WORD1_MOUNTED_ON_FILEID
177};
178
Al Virobc4785c2006-10-19 23:28:51 -0700179static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 struct nfs4_readdir_arg *readdir)
181{
Al Viro0dbb4c62006-10-19 23:28:49 -0700182 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184 BUG_ON(readdir->count < 80);
185 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000186 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
188 return;
189 }
190
191 readdir->cookie = 0;
192 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
193 if (cookie == 2)
194 return;
195
196 /*
197 * NFSv4 servers do not return entries for '.' and '..'
198 * Therefore, we fake these entries here. We let '.'
199 * have cookie 0 and '..' have cookie 1. Note that
200 * when talking to the server, we always send cookie 0
201 * instead of 1 or 2.
202 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800203 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 if (cookie == 0) {
206 *p++ = xdr_one; /* next */
207 *p++ = xdr_zero; /* cookie, first word */
208 *p++ = xdr_one; /* cookie, second word */
209 *p++ = xdr_one; /* entry len */
210 memcpy(p, ".\0\0\0", 4); /* entry */
211 p++;
212 *p++ = xdr_one; /* bitmap length */
213 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
214 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400215 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 }
217
218 *p++ = xdr_one; /* next */
219 *p++ = xdr_zero; /* cookie, first word */
220 *p++ = xdr_two; /* cookie, second word */
221 *p++ = xdr_two; /* entry len */
222 memcpy(p, "..\0\0", 4); /* entry */
223 p++;
224 *p++ = xdr_one; /* bitmap length */
225 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
226 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400227 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229 readdir->pgbase = (char *)p - (char *)start;
230 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800231 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
233
Trond Myklebust65de8722008-12-23 15:21:44 -0500234static int nfs4_wait_clnt_recover(struct nfs_client *clp)
235{
236 int res;
237
238 might_sleep();
239
Trond Myklebuste005e802008-12-23 15:21:48 -0500240 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400241 nfs_wait_bit_killable, TASK_KILLABLE);
Trond Myklebust65de8722008-12-23 15:21:44 -0500242 return res;
243}
244
245static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
246{
247 int res = 0;
248
249 might_sleep();
250
251 if (*timeout <= 0)
252 *timeout = NFS4_POLL_RETRY_MIN;
253 if (*timeout > NFS4_POLL_RETRY_MAX)
254 *timeout = NFS4_POLL_RETRY_MAX;
Jeff Laytond3103102011-12-01 22:44:39 +0100255 freezable_schedule_timeout_killable(*timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500256 if (fatal_signal_pending(current))
257 res = -ERESTARTSYS;
258 *timeout <<= 1;
259 return res;
260}
261
262/* This is the error handling routine for processes that are allowed
263 * to sleep.
264 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800265static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500266{
267 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500268 struct nfs4_state *state = exception->state;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500269 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500270 int ret = errorcode;
271
272 exception->retry = 0;
273 switch(errorcode) {
274 case 0:
275 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500276 case -NFS4ERR_OPENMODE:
Trond Myklebust14977482012-03-27 18:31:25 -0400277 if (inode && nfs_have_delegation(inode, FMODE_READ)) {
Trond Myklebust3114ea72012-03-07 16:39:06 -0500278 nfs_inode_return_delegation(inode);
279 exception->retry = 1;
280 return 0;
281 }
282 if (state == NULL)
283 break;
284 nfs4_schedule_stateid_recovery(server, state);
285 goto wait_on_recovery;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500286 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500287 case -NFS4ERR_ADMIN_REVOKED:
288 case -NFS4ERR_BAD_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500289 if (state == NULL)
290 break;
Trond Myklebust14977482012-03-27 18:31:25 -0400291 nfs_remove_bad_delegation(state->inode);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500292 nfs4_schedule_stateid_recovery(server, state);
293 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400294 case -NFS4ERR_EXPIRED:
295 if (state != NULL)
296 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust65de8722008-12-23 15:21:44 -0500297 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500298 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500299 nfs4_schedule_lease_recovery(clp);
300 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500301#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400302 case -NFS4ERR_BADSESSION:
303 case -NFS4ERR_BADSLOT:
304 case -NFS4ERR_BAD_HIGH_SLOT:
305 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
306 case -NFS4ERR_DEADSESSION:
307 case -NFS4ERR_SEQ_FALSE_RETRY:
308 case -NFS4ERR_SEQ_MISORDERED:
309 dprintk("%s ERROR: %d Reset session\n", __func__,
310 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400311 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Andy Adamson4745e312009-04-01 09:22:42 -0400312 exception->retry = 1;
Andy Adamsonb9179232009-12-04 15:55:32 -0500313 break;
Trond Myklebust03391692010-01-26 15:42:38 -0500314#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500315 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500316 if (exception->timeout > HZ) {
317 /* We have retried a decent amount, time to
318 * fail
319 */
320 ret = -EBUSY;
321 break;
322 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500323 case -NFS4ERR_GRACE:
324 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -0500325 case -EKEYEXPIRED:
Trond Myklebust65de8722008-12-23 15:21:44 -0500326 ret = nfs4_delay(server->client, &exception->timeout);
327 if (ret != 0)
328 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400329 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500330 case -NFS4ERR_OLD_STATEID:
331 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800332 break;
333 case -NFS4ERR_BADOWNER:
334 /* The following works around a Linux server bug! */
335 case -NFS4ERR_BADNAME:
336 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
337 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
338 exception->retry = 1;
339 printk(KERN_WARNING "NFS: v4 server %s "
340 "does not accept raw "
341 "uid/gids. "
342 "Reenabling the idmapper.\n",
343 server->nfs_client->cl_hostname);
344 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500345 }
346 /* We failed to handle the error */
347 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500348wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500349 ret = nfs4_wait_clnt_recover(clp);
350 if (ret == 0)
351 exception->retry = 1;
352 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500353}
354
355
Trond Myklebust452e9352010-07-31 14:29:06 -0400356static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 spin_lock(&clp->cl_lock);
359 if (time_before(clp->cl_last_renewal,timestamp))
360 clp->cl_last_renewal = timestamp;
361 spin_unlock(&clp->cl_lock);
362}
363
Trond Myklebust452e9352010-07-31 14:29:06 -0400364static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
365{
366 do_renew_lease(server->nfs_client, timestamp);
367}
368
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400369#if defined(CONFIG_NFS_V4_1)
370
Benny Halevy510b8172009-04-01 09:22:14 -0400371/*
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400372 * nfs4_free_slot - free a slot and efficiently update slot table.
373 *
374 * freeing a slot is trivially done by clearing its respective bit
375 * in the bitmap.
376 * If the freed slotid equals highest_used_slotid we want to update it
377 * so that the server would be able to size down the slot table if needed,
378 * otherwise we know that the highest_used_slotid is still in use.
379 * When updating highest_used_slotid there may be "holes" in the bitmap
380 * so we need to scan down from highest_used_slotid to 0 looking for the now
381 * highest slotid in use.
Trond Myklebust45d43c22012-02-06 19:38:51 -0500382 * If none found, highest_used_slotid is set to NFS4_NO_SLOT.
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500383 *
384 * Must be called while holding tbl->slot_tbl_lock
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400385 */
386static void
Trond Myklebust45d43c22012-02-06 19:38:51 -0500387nfs4_free_slot(struct nfs4_slot_table *tbl, u32 slotid)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400388{
Trond Myklebust45d43c22012-02-06 19:38:51 -0500389 BUG_ON(slotid >= NFS4_MAX_SLOT_TABLE);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400390 /* clear used bit in bitmap */
391 __clear_bit(slotid, tbl->used_slots);
392
393 /* update highest_used_slotid when it is freed */
394 if (slotid == tbl->highest_used_slotid) {
395 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500396 if (slotid < tbl->max_slots)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400397 tbl->highest_used_slotid = slotid;
398 else
Trond Myklebust45d43c22012-02-06 19:38:51 -0500399 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400400 }
Trond Myklebust45d43c22012-02-06 19:38:51 -0500401 dprintk("%s: slotid %u highest_used_slotid %d\n", __func__,
402 slotid, tbl->highest_used_slotid);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400403}
404
Trond Myklebust961a8282012-01-17 22:57:37 -0500405bool nfs4_set_task_privileged(struct rpc_task *task, void *dummy)
406{
407 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
408 return true;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400409}
410
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800411/*
Andy Adamson42acd022011-01-06 02:04:34 +0000412 * Signal state manager thread if session fore channel is drained
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800413 */
Andy Adamson42acd022011-01-06 02:04:34 +0000414static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800415{
Trond Myklebusta2118c32010-06-16 09:52:26 -0400416 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
Trond Myklebust961a8282012-01-17 22:57:37 -0500417 rpc_wake_up_first(&ses->fc_slot_table.slot_tbl_waitq,
418 nfs4_set_task_privileged, NULL);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800419 return;
420 }
421
Trond Myklebust45d43c22012-02-06 19:38:51 -0500422 if (ses->fc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800423 return;
424
Andy Adamson42acd022011-01-06 02:04:34 +0000425 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
426 complete(&ses->fc_slot_table.complete);
427}
428
429/*
430 * Signal state manager thread if session back channel is drained
431 */
432void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
433{
434 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
Trond Myklebust45d43c22012-02-06 19:38:51 -0500435 ses->bc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
Andy Adamson42acd022011-01-06 02:04:34 +0000436 return;
437 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
438 complete(&ses->bc_slot_table.complete);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800439}
440
Trond Myklebustd185a332010-06-16 09:52:25 -0400441static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400442{
443 struct nfs4_slot_table *tbl;
444
Trond Myklebustd185a332010-06-16 09:52:25 -0400445 tbl = &res->sr_session->fc_slot_table;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400446 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400447 /* just wake up the next guy waiting since
448 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500449 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500450 return;
Andy Adamson13615872009-04-01 09:22:17 -0400451 }
Andy Adamsonea028ac2009-12-04 15:55:38 -0500452
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500453 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonaabd0b42011-11-09 13:58:22 -0500454 nfs4_free_slot(tbl, res->sr_slot - tbl->slots);
Andy Adamson42acd022011-01-06 02:04:34 +0000455 nfs4_check_drain_fc_complete(res->sr_session);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500456 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400457 res->sr_slot = NULL;
Andy Adamson13615872009-04-01 09:22:17 -0400458}
459
Trond Myklebust14516c32010-07-31 14:29:06 -0400460static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400461{
462 unsigned long timestamp;
Trond Myklebust14516c32010-07-31 14:29:06 -0400463 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400464
465 /*
466 * sr_status remains 1 if an RPC level error occurred. The server
467 * may or may not have processed the sequence operation..
468 * Proceed as if the server received and processed the sequence
469 * operation.
470 */
471 if (res->sr_status == 1)
472 res->sr_status = NFS_OK;
473
Bryan Schumaker468f8612011-04-18 15:57:32 -0400474 /* don't increment the sequence number if the task wasn't sent */
475 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400476 goto out;
477
Andy Adamson691daf32009-12-04 15:55:39 -0500478 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400479 switch (res->sr_status) {
480 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400481 /* Update the slot's sequence and clientid lease timer */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400482 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400483 timestamp = res->sr_renewal_time;
Trond Myklebust14516c32010-07-31 14:29:06 -0400484 clp = res->sr_session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400485 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500486 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500487 if (res->sr_status_flags != 0)
488 nfs4_schedule_lease_recovery(clp);
Trond Myklebust14516c32010-07-31 14:29:06 -0400489 break;
490 case -NFS4ERR_DELAY:
491 /* The server detected a resend of the RPC call and
492 * returned NFS4ERR_DELAY as per Section 2.10.6.2
493 * of RFC5661.
494 */
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200495 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400496 __func__,
497 res->sr_slot - res->sr_session->fc_slot_table.slots,
498 res->sr_slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400499 goto out_retry;
500 default:
501 /* Just update the slot sequence no. */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400502 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400503 }
504out:
505 /* The session may be reset by one of the error handlers. */
506 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400507 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400508 return 1;
509out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400510 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400511 goto out;
512 rpc_delay(task, NFS4_POLL_RETRY_MAX);
513 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400514}
515
Trond Myklebust14516c32010-07-31 14:29:06 -0400516static int nfs4_sequence_done(struct rpc_task *task,
517 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400518{
Trond Myklebust14516c32010-07-31 14:29:06 -0400519 if (res->sr_session == NULL)
520 return 1;
521 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400522}
523
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400524/*
Benny Halevy510b8172009-04-01 09:22:14 -0400525 * nfs4_find_slot - efficiently look for a free slot
526 *
527 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
528 * If found, we mark the slot as used, update the highest_used_slotid,
529 * and respectively set up the sequence operation args.
Trond Myklebust45d43c22012-02-06 19:38:51 -0500530 * The slot number is returned if found, or NFS4_NO_SLOT otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400531 *
532 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400533 */
Trond Myklebust45d43c22012-02-06 19:38:51 -0500534static u32
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800535nfs4_find_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400536{
Trond Myklebust45d43c22012-02-06 19:38:51 -0500537 u32 slotid;
538 u32 ret_id = NFS4_NO_SLOT;
Benny Halevy510b8172009-04-01 09:22:14 -0400539
Trond Myklebust45d43c22012-02-06 19:38:51 -0500540 dprintk("--> %s used_slots=%04lx highest_used=%u max_slots=%u\n",
Benny Halevy510b8172009-04-01 09:22:14 -0400541 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
542 tbl->max_slots);
543 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
544 if (slotid >= tbl->max_slots)
545 goto out;
546 __set_bit(slotid, tbl->used_slots);
Trond Myklebust45d43c22012-02-06 19:38:51 -0500547 if (slotid > tbl->highest_used_slotid ||
548 tbl->highest_used_slotid == NFS4_NO_SLOT)
Benny Halevy510b8172009-04-01 09:22:14 -0400549 tbl->highest_used_slotid = slotid;
550 ret_id = slotid;
551out:
552 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
553 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400554 return ret_id;
555}
556
Trond Myklebust9d12b212012-01-17 22:04:25 -0500557static void nfs41_init_sequence(struct nfs4_sequence_args *args,
558 struct nfs4_sequence_res *res, int cache_reply)
559{
560 args->sa_session = NULL;
561 args->sa_cache_this = 0;
562 if (cache_reply)
563 args->sa_cache_this = 1;
564 res->sr_session = NULL;
565 res->sr_slot = NULL;
566}
567
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000568int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400569 struct nfs4_sequence_args *args,
570 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400571 struct rpc_task *task)
572{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400573 struct nfs4_slot *slot;
574 struct nfs4_slot_table *tbl;
Trond Myklebust45d43c22012-02-06 19:38:51 -0500575 u32 slotid;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400576
577 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400578 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400579 if (res->sr_slot != NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400580 return 0;
581
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400582 tbl = &session->fc_slot_table;
583
584 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400585 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800586 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500587 /* The state manager will wait until the slot table is empty */
Andy Adamson05f0d232009-12-04 15:55:37 -0500588 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400589 spin_unlock(&tbl->slot_tbl_lock);
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500590 dprintk("%s session is draining\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500591 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400592 }
593
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800594 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
595 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
596 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
597 spin_unlock(&tbl->slot_tbl_lock);
598 dprintk("%s enforce FIFO order\n", __func__);
599 return -EAGAIN;
600 }
601
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800602 slotid = nfs4_find_slot(tbl);
Trond Myklebust45d43c22012-02-06 19:38:51 -0500603 if (slotid == NFS4_NO_SLOT) {
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400604 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
605 spin_unlock(&tbl->slot_tbl_lock);
606 dprintk("<-- %s: no free slots\n", __func__);
607 return -EAGAIN;
608 }
609 spin_unlock(&tbl->slot_tbl_lock);
610
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800611 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400612 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400613 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400614 args->sa_slotid = slotid;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400615
616 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
617
Benny Halevy99fe60d2009-04-01 09:22:29 -0400618 res->sr_session = session;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400619 res->sr_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400620 res->sr_renewal_time = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400621 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400622 /*
623 * sr_status is only set in decode_sequence, and so will remain
624 * set to 1 if an rpc level failure occurs.
625 */
626 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400627 return 0;
628}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000629EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400630
Trond Myklebust035168a2010-06-16 09:52:26 -0400631int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400632 struct nfs4_sequence_args *args,
633 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400634 struct rpc_task *task)
635{
Trond Myklebust035168a2010-06-16 09:52:26 -0400636 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400637 int ret = 0;
638
Trond Myklebust9d12b212012-01-17 22:04:25 -0500639 if (session == NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400640 goto out;
Trond Myklebust035168a2010-06-16 09:52:26 -0400641
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200642 dprintk("--> %s clp %p session %p sr_slot %td\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400643 __func__, session->clp, session, res->sr_slot ?
644 res->sr_slot - session->fc_slot_table.slots : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400645
Trond Myklebust9d12b212012-01-17 22:04:25 -0500646 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400647out:
648 dprintk("<-- %s status=%d\n", __func__, ret);
649 return ret;
650}
651
652struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400653 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400654 struct nfs4_sequence_args *seq_args;
655 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400656};
657
658static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
659{
660 struct nfs41_call_sync_data *data = calldata;
661
Trond Myklebust035168a2010-06-16 09:52:26 -0400662 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
663
664 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -0500665 data->seq_res, task))
Andy Adamsonce5039c2009-04-01 09:22:13 -0400666 return;
667 rpc_call_start(task);
668}
669
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800670static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
671{
672 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
673 nfs41_call_sync_prepare(task, calldata);
674}
675
Andy Adamson69ab40c2009-04-01 09:22:19 -0400676static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
677{
678 struct nfs41_call_sync_data *data = calldata;
679
Trond Myklebust14516c32010-07-31 14:29:06 -0400680 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400681}
682
Trond Myklebust17280172012-03-11 13:11:00 -0400683static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400684 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400685 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400686};
687
Trond Myklebust17280172012-03-11 13:11:00 -0400688static const struct rpc_call_ops nfs41_call_priv_sync_ops = {
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800689 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
690 .rpc_call_done = nfs41_call_sync_done,
691};
692
Bryan Schumaker7c513052011-03-24 17:12:24 +0000693static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
694 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400695 struct rpc_message *msg,
696 struct nfs4_sequence_args *args,
697 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800698 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400699{
700 int ret;
701 struct rpc_task *task;
702 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400703 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400704 .seq_args = args,
705 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400706 };
707 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000708 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400709 .rpc_message = msg,
710 .callback_ops = &nfs41_call_sync_ops,
711 .callback_data = &data
712 };
713
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800714 if (privileged)
715 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400716 task = rpc_run_task(&task_setup);
717 if (IS_ERR(task))
718 ret = PTR_ERR(task);
719 else {
720 ret = task->tk_status;
721 rpc_put_task(task);
722 }
723 return ret;
724}
725
Bryan Schumaker7c513052011-03-24 17:12:24 +0000726int _nfs4_call_sync_session(struct rpc_clnt *clnt,
727 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400728 struct rpc_message *msg,
729 struct nfs4_sequence_args *args,
730 struct nfs4_sequence_res *res,
731 int cache_reply)
732{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500733 nfs41_init_sequence(args, res, cache_reply);
734 return nfs4_call_sync_sequence(clnt, server, msg, args, res, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400735}
736
Trond Myklebustdf896452010-06-16 09:52:26 -0400737#else
Trond Myklebust9d12b212012-01-17 22:04:25 -0500738static inline
739void nfs41_init_sequence(struct nfs4_sequence_args *args,
740 struct nfs4_sequence_res *res, int cache_reply)
741{
742}
743
Trond Myklebust14516c32010-07-31 14:29:06 -0400744static int nfs4_sequence_done(struct rpc_task *task,
745 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400746{
Trond Myklebust14516c32010-07-31 14:29:06 -0400747 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400748}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400749#endif /* CONFIG_NFS_V4_1 */
750
Bryan Schumaker7c513052011-03-24 17:12:24 +0000751int _nfs4_call_sync(struct rpc_clnt *clnt,
752 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400753 struct rpc_message *msg,
754 struct nfs4_sequence_args *args,
755 struct nfs4_sequence_res *res,
756 int cache_reply)
757{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500758 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000759 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400760}
761
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000762static inline
Bryan Schumaker7c513052011-03-24 17:12:24 +0000763int nfs4_call_sync(struct rpc_clnt *clnt,
764 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000765 struct rpc_message *msg,
766 struct nfs4_sequence_args *args,
767 struct nfs4_sequence_res *res,
768 int cache_reply)
769{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000770 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
771 args, res, cache_reply);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000772}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400773
Trond Myklebust38478b22006-05-25 01:40:57 -0400774static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
Trond Myklebust38478b22006-05-25 01:40:57 -0400776 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Trond Myklebust38478b22006-05-25 01:40:57 -0400778 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400779 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700780 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400781 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700782 dir->i_version = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400783 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784}
785
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100786struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400787 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100788 struct nfs_openargs o_arg;
789 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100790 struct nfs_open_confirmargs c_arg;
791 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500792 struct nfs4_string owner_name;
793 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100794 struct nfs_fattr f_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100795 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400796 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100797 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400798 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100799 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100800 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400801 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100802 int rpc_status;
803 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100804};
805
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400806
807static void nfs4_init_opendata_res(struct nfs4_opendata *p)
808{
809 p->o_res.f_attr = &p->f_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400810 p->o_res.seqid = p->o_arg.seqid;
811 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400812 p->o_res.server = p->o_arg.server;
813 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500814 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400815}
816
Al Viro82a2c1b2011-06-22 18:30:55 -0400817static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500818 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400819 const struct iattr *attrs,
820 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100821{
Al Viro82a2c1b2011-06-22 18:30:55 -0400822 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100823 struct inode *dir = parent->d_inode;
824 struct nfs_server *server = NFS_SERVER(dir);
825 struct nfs4_opendata *p;
826
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400827 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100828 if (p == NULL)
829 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400830 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100831 if (p->o_arg.seqid == NULL)
832 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400833 nfs_sb_active(dentry->d_sb);
834 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100835 p->dir = parent;
836 p->owner = sp;
837 atomic_inc(&sp->so_count);
838 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500839 p->o_arg.open_flags = flags;
840 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400841 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400842 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
843 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400844 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100845 p->o_arg.server = server;
846 p->o_arg.bitmask = server->attr_bitmask;
847 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust536e43d2012-01-17 22:04:26 -0500848 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -0500849 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400850
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100851 p->o_arg.u.attrs = &p->attrs;
852 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500853
854 verf[0] = jiffies;
855 verf[1] = current->pid;
856 memcpy(p->o_arg.u.verifier.data, verf,
857 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100858 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100859 p->c_arg.fh = &p->o_res.fh;
860 p->c_arg.stateid = &p->o_res.stateid;
861 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400862 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400863 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100864 return p;
865err_free:
866 kfree(p);
867err:
868 dput(parent);
869 return NULL;
870}
871
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400872static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100873{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400874 struct nfs4_opendata *p = container_of(kref,
875 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400876 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400877
878 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400879 if (p->state != NULL)
880 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400881 nfs4_put_state_owner(p->owner);
882 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400883 dput(p->dentry);
884 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500885 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400886 kfree(p);
887}
888
889static void nfs4_opendata_put(struct nfs4_opendata *p)
890{
891 if (p != NULL)
892 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100893}
894
Trond Myklebust06f814a2006-01-03 09:55:07 +0100895static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
896{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100897 int ret;
898
Trond Myklebust06f814a2006-01-03 09:55:07 +0100899 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100900 return ret;
901}
902
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500903static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400904{
905 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500906
Trond Myklebust536e43d2012-01-17 22:04:26 -0500907 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500908 goto out;
909 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400910 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500911 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
912 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400913 break;
914 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500915 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
916 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400917 break;
918 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500919 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
920 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400921 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500922out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400923 return ret;
924}
925
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500926static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400927{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500928 if (delegation == NULL)
929 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500930 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400931 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500932 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400933 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500934 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400935 return 1;
936}
937
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500938static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100939{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500940 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100941 case FMODE_WRITE:
942 state->n_wronly++;
943 break;
944 case FMODE_READ:
945 state->n_rdonly++;
946 break;
947 case FMODE_READ|FMODE_WRITE:
948 state->n_rdwr++;
949 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500950 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100951}
952
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500953static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400954{
955 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -0500956 nfs4_stateid_copy(&state->stateid, stateid);
957 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500958 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400959 case FMODE_READ:
960 set_bit(NFS_O_RDONLY_STATE, &state->flags);
961 break;
962 case FMODE_WRITE:
963 set_bit(NFS_O_WRONLY_STATE, &state->flags);
964 break;
965 case FMODE_READ|FMODE_WRITE:
966 set_bit(NFS_O_RDWR_STATE, &state->flags);
967 }
968}
969
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500970static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400971{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400972 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500973 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400974 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400975}
976
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500977static 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 -0700978{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400979 /*
980 * Protect the call to nfs4_state_set_mode_locked and
981 * serialise the stateid update
982 */
983 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400984 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -0500985 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -0400986 set_bit(NFS_DELEGATED_STATE, &state->flags);
987 }
988 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500989 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400990 write_sequnlock(&state->seqlock);
991 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500992 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700993 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994}
995
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500996static 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 -0500997{
998 struct nfs_inode *nfsi = NFS_I(state->inode);
999 struct nfs_delegation *deleg_cur;
1000 int ret = 0;
1001
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001002 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001003
1004 rcu_read_lock();
1005 deleg_cur = rcu_dereference(nfsi->delegation);
1006 if (deleg_cur == NULL)
1007 goto no_delegation;
1008
1009 spin_lock(&deleg_cur->lock);
1010 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001011 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001012 goto no_delegation_unlock;
1013
1014 if (delegation == NULL)
1015 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001016 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001017 goto no_delegation_unlock;
1018
Trond Myklebustb7391f42008-12-23 15:21:52 -05001019 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001020 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001021 ret = 1;
1022no_delegation_unlock:
1023 spin_unlock(&deleg_cur->lock);
1024no_delegation:
1025 rcu_read_unlock();
1026
1027 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001028 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001029 ret = 1;
1030 }
1031
1032 return ret;
1033}
1034
1035
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001036static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001037{
1038 struct nfs_delegation *delegation;
1039
1040 rcu_read_lock();
1041 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001042 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001043 rcu_read_unlock();
1044 return;
1045 }
1046 rcu_read_unlock();
1047 nfs_inode_return_delegation(inode);
1048}
1049
Trond Myklebust6ee41262007-07-08 14:11:36 -04001050static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001051{
1052 struct nfs4_state *state = opendata->state;
1053 struct nfs_inode *nfsi = NFS_I(state->inode);
1054 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -05001055 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001056 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001057 nfs4_stateid stateid;
1058 int ret = -EAGAIN;
1059
Trond Myklebustaac00a82007-07-05 19:02:21 -04001060 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001061 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001062 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001063 if (can_open_cached(state, fmode, open_mode)) {
1064 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001065 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001066 goto out_return_state;
1067 }
1068 spin_unlock(&state->owner->so_lock);
1069 }
Trond Myklebust34310432008-12-23 15:21:38 -05001070 rcu_read_lock();
1071 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001072 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001073 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001074 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001075 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001076 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001077 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001078 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001079 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001080 if (ret != 0)
1081 goto out;
1082 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001083
1084 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001085 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001086 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001087 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001088out:
1089 return ERR_PTR(ret);
1090out_return_state:
1091 atomic_inc(&state->count);
1092 return state;
1093}
1094
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001095static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1096{
1097 struct inode *inode;
1098 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -04001099 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001100 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001101
Trond Myklebustaac00a82007-07-05 19:02:21 -04001102 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001103 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001104 goto out;
1105 }
1106
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001107 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001108 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001109 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001110 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001111 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001112 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001113 goto err;
1114 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001115 state = nfs4_get_open_state(inode, data->owner);
1116 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001117 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001118 if (data->o_res.delegation_type != 0) {
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001119 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001120 int delegation_flags = 0;
1121
Trond Myklebust003707c2007-07-05 18:07:55 -04001122 rcu_read_lock();
1123 delegation = rcu_dereference(NFS_I(inode)->delegation);
1124 if (delegation)
1125 delegation_flags = delegation->flags;
1126 rcu_read_unlock();
Trond Myklebust652f89f2011-12-09 19:05:58 -05001127 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1128 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1129 "returning a delegation for "
1130 "OPEN(CLAIM_DELEGATE_CUR)\n",
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001131 clp->cl_hostname);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001132 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001133 nfs_inode_set_delegation(state->inode,
1134 data->owner->so_cred,
1135 &data->o_res);
1136 else
1137 nfs_inode_reclaim_delegation(state->inode,
1138 data->owner->so_cred,
1139 &data->o_res);
1140 }
Trond Myklebust34310432008-12-23 15:21:38 -05001141
1142 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001143 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001144 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001145out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001146 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001147err_put_inode:
1148 iput(inode);
1149err:
1150 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001151}
1152
Trond Myklebust864472e2006-01-03 09:55:15 +01001153static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1154{
1155 struct nfs_inode *nfsi = NFS_I(state->inode);
1156 struct nfs_open_context *ctx;
1157
1158 spin_lock(&state->inode->i_lock);
1159 list_for_each_entry(ctx, &nfsi->open_files, list) {
1160 if (ctx->state != state)
1161 continue;
1162 get_nfs_open_context(ctx);
1163 spin_unlock(&state->inode->i_lock);
1164 return ctx;
1165 }
1166 spin_unlock(&state->inode->i_lock);
1167 return ERR_PTR(-ENOENT);
1168}
1169
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001170static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1171{
1172 struct nfs4_opendata *opendata;
1173
Al Viro3d4ff432011-06-22 18:40:12 -04001174 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001175 if (opendata == NULL)
1176 return ERR_PTR(-ENOMEM);
1177 opendata->state = state;
1178 atomic_inc(&state->count);
1179 return opendata;
1180}
1181
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001182static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001183{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001184 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001185 int ret;
1186
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001187 opendata->o_arg.open_flags = 0;
1188 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001189 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1190 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1191 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001192 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001193 if (ret != 0)
1194 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001195 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001196 if (IS_ERR(newstate))
1197 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001198 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001199 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001200 return 0;
1201}
1202
1203static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1204{
Trond Myklebust864472e2006-01-03 09:55:15 +01001205 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001206 int ret;
1207
1208 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001209 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001210 smp_rmb();
1211 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001212 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001213 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001214 if (ret != 0)
1215 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001216 if (newstate != state)
1217 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001218 }
1219 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001220 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001221 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001222 if (ret != 0)
1223 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001224 if (newstate != state)
1225 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001226 }
1227 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001228 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001229 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001230 if (ret != 0)
1231 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001232 if (newstate != state)
1233 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001234 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001235 /*
1236 * We may have performed cached opens for all three recoveries.
1237 * Check if we need to update the current stateid.
1238 */
1239 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001240 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001241 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001242 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001243 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001244 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001245 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001246 return 0;
1247}
1248
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249/*
1250 * OPEN_RECLAIM:
1251 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001253static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001255 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001256 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001257 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 int status;
1259
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001260 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1261 if (IS_ERR(opendata))
1262 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001263 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1264 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001265 rcu_read_lock();
1266 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001267 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001268 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001269 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001270 opendata->o_arg.u.delegation_type = delegation_type;
1271 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001272 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 return status;
1274}
1275
Trond Myklebust539cd032007-06-05 11:46:42 -04001276static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277{
1278 struct nfs_server *server = NFS_SERVER(state->inode);
1279 struct nfs4_exception exception = { };
1280 int err;
1281 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001282 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001283 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001284 break;
1285 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 } while (exception.retry);
1287 return err;
1288}
1289
Trond Myklebust864472e2006-01-03 09:55:15 +01001290static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1291{
1292 struct nfs_open_context *ctx;
1293 int ret;
1294
1295 ctx = nfs4_state_find_open_context(state);
1296 if (IS_ERR(ctx))
1297 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001298 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001299 put_nfs_open_context(ctx);
1300 return ret;
1301}
1302
Trond Myklebust13437e12007-07-06 15:10:43 -04001303static 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 -07001304{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001305 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001306 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001308 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1309 if (IS_ERR(opendata))
1310 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001311 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebustf597c532012-03-04 18:13:56 -05001312 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust864472e2006-01-03 09:55:15 +01001313 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001314 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001315 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316}
1317
Trond Myklebust13437e12007-07-06 15:10:43 -04001318int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319{
1320 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001321 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 int err;
1323 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001324 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 switch (err) {
1326 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001327 case -ENOENT:
1328 case -ESTALE:
1329 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001330 case -NFS4ERR_BADSESSION:
1331 case -NFS4ERR_BADSLOT:
1332 case -NFS4ERR_BAD_HIGH_SLOT:
1333 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1334 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04001335 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001336 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 case -NFS4ERR_STALE_CLIENTID:
1338 case -NFS4ERR_STALE_STATEID:
1339 case -NFS4ERR_EXPIRED:
1340 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001341 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001342 goto out;
1343 case -ERESTARTSYS:
1344 /*
1345 * The show must go on: exit, but mark the
1346 * stateid as needing recovery.
1347 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001348 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001349 case -NFS4ERR_ADMIN_REVOKED:
1350 case -NFS4ERR_BAD_STATEID:
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001351 nfs_inode_find_state_and_recover(state->inode,
1352 stateid);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001353 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001354 case -EKEYEXPIRED:
1355 /*
1356 * User RPCSEC_GSS context has expired.
1357 * We cannot recover this stateid now, so
1358 * skip it and allow recovery thread to
1359 * proceed.
1360 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001361 case -ENOMEM:
1362 err = 0;
1363 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 }
1365 err = nfs4_handle_exception(server, err, &exception);
1366 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001367out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 return err;
1369}
1370
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001371static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1372{
1373 struct nfs4_opendata *data = calldata;
1374
1375 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001376 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001377 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001378 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001379 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001380 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001381 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001382}
1383
1384static void nfs4_open_confirm_release(void *calldata)
1385{
1386 struct nfs4_opendata *data = calldata;
1387 struct nfs4_state *state = NULL;
1388
1389 /* If this request hasn't been cancelled, do nothing */
1390 if (data->cancelled == 0)
1391 goto out_free;
1392 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001393 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001394 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001395 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001396 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001397 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001398out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001399 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001400}
1401
1402static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001403 .rpc_call_done = nfs4_open_confirm_done,
1404 .rpc_release = nfs4_open_confirm_release,
1405};
1406
1407/*
1408 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1409 */
1410static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1411{
1412 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1413 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001414 struct rpc_message msg = {
1415 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1416 .rpc_argp = &data->c_arg,
1417 .rpc_resp = &data->c_res,
1418 .rpc_cred = data->owner->so_cred,
1419 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001420 struct rpc_task_setup task_setup_data = {
1421 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001422 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001423 .callback_ops = &nfs4_open_confirm_ops,
1424 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001425 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001426 .flags = RPC_TASK_ASYNC,
1427 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 int status;
1429
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001430 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001431 data->rpc_done = 0;
1432 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001433 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001434 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001435 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001436 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001437 status = nfs4_wait_for_completion_rpc_task(task);
1438 if (status != 0) {
1439 data->cancelled = 1;
1440 smp_wmb();
1441 } else
1442 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001443 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 return status;
1445}
1446
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001447static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001449 struct nfs4_opendata *data = calldata;
1450 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001451
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001452 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1453 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001454 /*
1455 * Check if we still need to send an OPEN call, or if we can use
1456 * a delegation instead.
1457 */
1458 if (data->state != NULL) {
1459 struct nfs_delegation *delegation;
1460
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001461 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001462 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001463 rcu_read_lock();
1464 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001465 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1466 can_open_delegated(delegation, data->o_arg.fmode))
1467 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001468 rcu_read_unlock();
1469 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001470 /* Update client id. */
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001471 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001472 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001473 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001474 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1475 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001476 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001477 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001478 &data->o_arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05001479 &data->o_res.seq_res, task))
Andy Adamsond8985282009-04-01 09:22:21 -04001480 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001481 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001482 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001483unlock_no_action:
1484 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001485out_no_action:
1486 task->tk_action = NULL;
1487
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001488}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001490static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1491{
1492 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1493 nfs4_open_prepare(task, calldata);
1494}
1495
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001496static void nfs4_open_done(struct rpc_task *task, void *calldata)
1497{
1498 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001500 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001501
Trond Myklebust14516c32010-07-31 14:29:06 -04001502 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1503 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001504
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001505 if (task->tk_status == 0) {
1506 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001507 case S_IFREG:
1508 break;
1509 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001510 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001511 break;
1512 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001513 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001514 break;
1515 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001516 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001517 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001518 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001519 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1520 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001521 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001522 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001523}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001524
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001525static void nfs4_open_release(void *calldata)
1526{
1527 struct nfs4_opendata *data = calldata;
1528 struct nfs4_state *state = NULL;
1529
1530 /* If this request hasn't been cancelled, do nothing */
1531 if (data->cancelled == 0)
1532 goto out_free;
1533 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001534 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001535 goto out_free;
1536 /* In case we need an open_confirm, no cleanup! */
1537 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1538 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001539 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001540 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001541 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001542out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001543 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001544}
1545
1546static const struct rpc_call_ops nfs4_open_ops = {
1547 .rpc_call_prepare = nfs4_open_prepare,
1548 .rpc_call_done = nfs4_open_done,
1549 .rpc_release = nfs4_open_release,
1550};
1551
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001552static const struct rpc_call_ops nfs4_recover_open_ops = {
1553 .rpc_call_prepare = nfs4_recover_open_prepare,
1554 .rpc_call_done = nfs4_open_done,
1555 .rpc_release = nfs4_open_release,
1556};
1557
1558static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001559{
1560 struct inode *dir = data->dir->d_inode;
1561 struct nfs_server *server = NFS_SERVER(dir);
1562 struct nfs_openargs *o_arg = &data->o_arg;
1563 struct nfs_openres *o_res = &data->o_res;
1564 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001565 struct rpc_message msg = {
1566 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1567 .rpc_argp = o_arg,
1568 .rpc_resp = o_res,
1569 .rpc_cred = data->owner->so_cred,
1570 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001571 struct rpc_task_setup task_setup_data = {
1572 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001573 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001574 .callback_ops = &nfs4_open_ops,
1575 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001576 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001577 .flags = RPC_TASK_ASYNC,
1578 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001579 int status;
1580
Trond Myklebust9d12b212012-01-17 22:04:25 -05001581 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001582 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001583 data->rpc_done = 0;
1584 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001585 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001586 if (isrecover)
1587 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001588 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001589 if (IS_ERR(task))
1590 return PTR_ERR(task);
1591 status = nfs4_wait_for_completion_rpc_task(task);
1592 if (status != 0) {
1593 data->cancelled = 1;
1594 smp_wmb();
1595 } else
1596 status = data->rpc_status;
1597 rpc_put_task(task);
1598
1599 return status;
1600}
1601
1602static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1603{
1604 struct inode *dir = data->dir->d_inode;
1605 struct nfs_openres *o_res = &data->o_res;
1606 int status;
1607
1608 status = nfs4_run_open_task(data, 1);
1609 if (status != 0 || !data->rpc_done)
1610 return status;
1611
Trond Myklebust6926afd2012-01-07 13:22:46 -05001612 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1613
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001614 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1615 status = _nfs4_proc_open_confirm(data);
1616 if (status != 0)
1617 return status;
1618 }
1619
1620 return status;
1621}
1622
1623/*
1624 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1625 */
1626static int _nfs4_proc_open(struct nfs4_opendata *data)
1627{
1628 struct inode *dir = data->dir->d_inode;
1629 struct nfs_server *server = NFS_SERVER(dir);
1630 struct nfs_openargs *o_arg = &data->o_arg;
1631 struct nfs_openres *o_res = &data->o_res;
1632 int status;
1633
1634 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001635 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001636 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001637 if (status != 0) {
1638 if (status == -NFS4ERR_BADNAME &&
1639 !(o_arg->open_flags & O_CREAT))
1640 return -ENOENT;
1641 return status;
1642 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001643
Trond Myklebust6926afd2012-01-07 13:22:46 -05001644 nfs_fattr_map_and_free_names(server, &data->f_attr);
1645
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001646 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001647 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001648 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1649 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001651 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001653 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 }
1655 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001656 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001657 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658}
1659
Andy Adamsond83217c2011-03-01 01:34:17 +00001660static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +01001661{
Trond Myklebusta78cb572009-08-09 15:06:19 -04001662 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001663 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001664
Trond Myklebusta78cb572009-08-09 15:06:19 -04001665 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001666 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001667 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001668 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001669 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1670 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001671 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001672 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001673 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001674 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001675 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001676}
1677
Andy Adamsond83217c2011-03-01 01:34:17 +00001678static int nfs4_recover_expired_lease(struct nfs_server *server)
1679{
1680 return nfs4_client_recover_expired_lease(server->nfs_client);
1681}
1682
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683/*
1684 * OPEN_EXPIRED:
1685 * reclaim state on the server after a network partition.
1686 * Assumes caller holds the appropriate lock
1687 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001688static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001690 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001691 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001693 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1694 if (IS_ERR(opendata))
1695 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001696 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001697 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001698 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001699 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001700 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701}
1702
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001703static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001704{
Trond Myklebust539cd032007-06-05 11:46:42 -04001705 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001706 struct nfs4_exception exception = { };
1707 int err;
1708
1709 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001710 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001711 switch (err) {
1712 default:
1713 goto out;
1714 case -NFS4ERR_GRACE:
1715 case -NFS4ERR_DELAY:
1716 nfs4_handle_exception(server, err, &exception);
1717 err = 0;
1718 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001719 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001720out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001721 return err;
1722}
1723
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1725{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001727 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
Trond Myklebust864472e2006-01-03 09:55:15 +01001729 ctx = nfs4_state_find_open_context(state);
1730 if (IS_ERR(ctx))
1731 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001732 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001733 put_nfs_open_context(ctx);
1734 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735}
1736
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001737#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001738static int nfs41_check_expired_stateid(struct nfs4_state *state, nfs4_stateid *stateid, unsigned int flags)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001739{
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001740 int status = NFS_OK;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001741 struct nfs_server *server = NFS_SERVER(state->inode);
1742
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001743 if (state->flags & flags) {
1744 status = nfs41_test_stateid(server, stateid);
1745 if (status != NFS_OK) {
1746 nfs41_free_stateid(server, stateid);
1747 state->flags &= ~flags;
1748 }
1749 }
1750 return status;
1751}
1752
1753static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1754{
1755 int deleg_status, open_status;
1756 int deleg_flags = 1 << NFS_DELEGATED_STATE;
1757 int open_flags = (1 << NFS_O_RDONLY_STATE) | (1 << NFS_O_WRONLY_STATE) | (1 << NFS_O_RDWR_STATE);
1758
1759 deleg_status = nfs41_check_expired_stateid(state, &state->stateid, deleg_flags);
1760 open_status = nfs41_check_expired_stateid(state, &state->open_stateid, open_flags);
1761
1762 if ((deleg_status == NFS_OK) && (open_status == NFS_OK))
1763 return NFS_OK;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001764 return nfs4_open_expired(sp, state);
1765}
1766#endif
1767
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001769 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1770 * fields corresponding to attributes that were used to store the verifier.
1771 * Make sure we clobber those fields in the later setattr call
1772 */
1773static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1774{
1775 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1776 !(sattr->ia_valid & ATTR_ATIME_SET))
1777 sattr->ia_valid |= ATTR_ATIME;
1778
1779 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1780 !(sattr->ia_valid & ATTR_MTIME_SET))
1781 sattr->ia_valid |= ATTR_MTIME;
1782}
1783
1784/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001785 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 */
Andy Adamson82be4172012-05-23 05:02:35 -04001787static int _nfs4_do_open(struct inode *dir,
1788 struct dentry *dentry,
1789 fmode_t fmode,
1790 int flags,
1791 struct iattr *sattr,
1792 struct rpc_cred *cred,
1793 struct nfs4_state **res,
1794 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795{
1796 struct nfs4_state_owner *sp;
1797 struct nfs4_state *state = NULL;
1798 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001799 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001800 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
1802 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001804 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1805 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1807 goto out_err;
1808 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001809 status = nfs4_recover_expired_lease(server);
1810 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001811 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001812 if (dentry->d_inode != NULL)
1813 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001814 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001815 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001816 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001817 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
Andy Adamson82be4172012-05-23 05:02:35 -04001819 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
1820 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
1821 if (!opendata->f_attr.mdsthreshold)
1822 goto err_opendata_put;
1823 }
Al Viro82a2c1b2011-06-22 18:30:55 -04001824 if (dentry->d_inode != NULL)
1825 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001826
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001827 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001829 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001831 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001832 status = PTR_ERR(state);
1833 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001834 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001835 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001836 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001837
1838 if (opendata->o_arg.open_flags & O_EXCL) {
1839 nfs4_exclusive_attrset(opendata, sattr);
1840
1841 nfs_fattr_init(opendata->o_res.f_attr);
1842 status = nfs4_do_setattr(state->inode, cred,
1843 opendata->o_res.f_attr, sattr,
1844 state);
1845 if (status == 0)
1846 nfs_setattr_update_inode(state->inode, sattr);
1847 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1848 }
Andy Adamson82be4172012-05-23 05:02:35 -04001849
1850 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
1851 *ctx_th = opendata->f_attr.mdsthreshold;
1852 else
1853 kfree(opendata->f_attr.mdsthreshold);
1854 opendata->f_attr.mdsthreshold = NULL;
1855
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001856 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 *res = state;
1859 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001860err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04001861 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001862 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001863err_put_state_owner:
1864 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 *res = NULL;
1867 return status;
1868}
1869
1870
Andy Adamson82be4172012-05-23 05:02:35 -04001871static struct nfs4_state *nfs4_do_open(struct inode *dir,
1872 struct dentry *dentry,
1873 fmode_t fmode,
1874 int flags,
1875 struct iattr *sattr,
1876 struct rpc_cred *cred,
1877 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878{
1879 struct nfs4_exception exception = { };
1880 struct nfs4_state *res;
1881 int status;
1882
1883 do {
Andy Adamson82be4172012-05-23 05:02:35 -04001884 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
1885 &res, ctx_th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 if (status == 0)
1887 break;
1888 /* NOTE: BAD_SEQID means the server and client disagree about the
1889 * book-keeping w.r.t. state-changing operations
1890 * (OPEN/CLOSE/LOCK/LOCKU...)
1891 * It is actually a sign of a bug on the client or on the server.
1892 *
1893 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001894 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 * have unhashed the old state_owner for us, and that we can
1896 * therefore safely retry using a new one. We should still warn
1897 * the user though...
1898 */
1899 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04001900 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001901 " returned a bad sequence-id error!\n",
1902 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 exception.retry = 1;
1904 continue;
1905 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001906 /*
1907 * BAD_STATEID on OPEN means that the server cancelled our
1908 * state before it received the OPEN_CONFIRM.
1909 * Recover by retrying the request as per the discussion
1910 * on Page 181 of RFC3530.
1911 */
1912 if (status == -NFS4ERR_BAD_STATEID) {
1913 exception.retry = 1;
1914 continue;
1915 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001916 if (status == -EAGAIN) {
1917 /* We must have found a delegation */
1918 exception.retry = 1;
1919 continue;
1920 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1922 status, &exception));
1923 } while (exception.retry);
1924 return res;
1925}
1926
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001927static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1928 struct nfs_fattr *fattr, struct iattr *sattr,
1929 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001931 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001933 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 .iap = sattr,
1935 .server = server,
1936 .bitmask = server->attr_bitmask,
1937 };
1938 struct nfs_setattrres res = {
1939 .fattr = fattr,
1940 .server = server,
1941 };
1942 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001943 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1944 .rpc_argp = &arg,
1945 .rpc_resp = &res,
1946 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001948 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001949 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950
Trond Myklebust0e574af2005-10-27 22:12:38 -04001951 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
Trond Myklebust4fc87962012-03-08 17:42:01 -05001953 if (state != NULL) {
1954 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
1955 current->files, current->tgid);
1956 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
1957 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001958 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001959 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05001960 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
Bryan Schumaker7c513052011-03-24 17:12:24 +00001962 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001963 if (status == 0 && state != NULL)
1964 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001965 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966}
1967
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001968static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1969 struct nfs_fattr *fattr, struct iattr *sattr,
1970 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001972 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001973 struct nfs4_exception exception = {
1974 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05001975 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001976 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 int err;
1978 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04001979 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
1980 switch (err) {
1981 case -NFS4ERR_OPENMODE:
1982 if (state && !(state->state & FMODE_WRITE)) {
1983 err = -EBADF;
1984 if (sattr->ia_valid & ATTR_OPEN)
1985 err = -EACCES;
1986 goto out;
1987 }
1988 }
1989 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04001991out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 return err;
1993}
1994
1995struct nfs4_closedata {
1996 struct inode *inode;
1997 struct nfs4_state *state;
1998 struct nfs_closeargs arg;
1999 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002000 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002001 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002002 bool roc;
2003 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004};
2005
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002006static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002007{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002008 struct nfs4_closedata *calldata = data;
2009 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002010 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002011
Fred Isamanf7e89172011-01-06 11:36:32 +00002012 if (calldata->roc)
2013 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002014 nfs4_put_open_state(calldata->state);
2015 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002016 nfs4_put_state_owner(sp);
Al Viro643168c2011-06-22 18:20:23 -04002017 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002018 kfree(calldata);
2019}
2020
Trond Myklebust88069f72009-12-08 08:33:16 -05002021static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2022 fmode_t fmode)
2023{
2024 spin_lock(&state->owner->so_lock);
2025 if (!(fmode & FMODE_READ))
2026 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2027 if (!(fmode & FMODE_WRITE))
2028 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2029 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2030 spin_unlock(&state->owner->so_lock);
2031}
2032
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002033static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002035 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 struct nfs_server *server = NFS_SERVER(calldata->inode);
2038
Chuck Levera3ca5652012-03-01 17:00:40 -05002039 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002040 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2041 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 /* hmm. we are done with the inode, and in the process of freeing
2043 * the state_owner. we keep this around to process errors
2044 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 switch (task->tk_status) {
2046 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002047 if (calldata->roc)
2048 pnfs_roc_set_barrier(state->inode,
2049 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002050 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002051 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002052 nfs4_close_clear_stateid_flags(state,
2053 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 break;
2055 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002056 case -NFS4ERR_OLD_STATEID:
2057 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002059 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002060 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002062 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2063 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002065 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002066 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002067 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068}
2069
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002070static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002072 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002073 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05002074 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002075
Chuck Levera3ca5652012-03-01 17:00:40 -05002076 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002077 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07002078 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04002079
Trond Myklebust88069f72009-12-08 08:33:16 -05002080 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2081 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002082 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002083 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002084 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002085 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002086 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2087 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2088 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002089 }
2090 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002091 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2092 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2093 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002094 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002095 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002096 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002097
2098 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002099 /* Note: exit _without_ calling nfs4_close_done */
2100 task->tk_action = NULL;
Chuck Levera3ca5652012-03-01 17:00:40 -05002101 goto out;
Trond Myklebust95121352005-10-18 14:20:12 -07002102 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002103
Fred Isamanf7e89172011-01-06 11:36:32 +00002104 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002105 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002106 if (calldata->roc &&
2107 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
2108 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
2109 task, NULL);
Chuck Levera3ca5652012-03-01 17:00:40 -05002110 goto out;
Fred Isamanf7e89172011-01-06 11:36:32 +00002111 }
2112 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002113
Trond Myklebust516a6af2005-10-27 22:12:41 -04002114 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002115 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04002116 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002117 &calldata->arg.seq_args,
2118 &calldata->res.seq_res,
2119 task))
Chuck Levera3ca5652012-03-01 17:00:40 -05002120 goto out;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002121 rpc_call_start(task);
Chuck Levera3ca5652012-03-01 17:00:40 -05002122out:
2123 dprintk("%s: done!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124}
2125
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002126static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002127 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002128 .rpc_call_done = nfs4_close_done,
2129 .rpc_release = nfs4_free_closedata,
2130};
2131
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132/*
2133 * It is possible for data to be read/written from a mem-mapped file
2134 * after the sys_close call (which hits the vfs layer as a flush).
2135 * This means that we can't safely call nfsv4 close on a file until
2136 * the inode is cleared. This in turn means that we are not good
2137 * NFSv4 citizens - we do not indicate to the server to update the file's
2138 * share state even when we are done with one of the three share
2139 * stateid's in the inode.
2140 *
2141 * NOTE: Caller must be holding the sp->so_owner semaphore!
2142 */
Al Viro643168c2011-06-22 18:20:23 -04002143int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002145 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002147 struct nfs4_state_owner *sp = state->owner;
2148 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002149 struct rpc_message msg = {
2150 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2151 .rpc_cred = state->owner->so_cred,
2152 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002153 struct rpc_task_setup task_setup_data = {
2154 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002155 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002156 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002157 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002158 .flags = RPC_TASK_ASYNC,
2159 };
Trond Myklebust95121352005-10-18 14:20:12 -07002160 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002162 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002164 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002165 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002166 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002168 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002169 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002171 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002172 if (calldata->arg.seqid == NULL)
2173 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002174 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002175 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002176 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002177 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002178 calldata->res.server = server;
Fred Isamanf7e89172011-01-06 11:36:32 +00002179 calldata->roc = roc;
Al Viro643168c2011-06-22 18:20:23 -04002180 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002181
Trond Myklebust1174dd12010-12-21 10:52:24 -05002182 msg.rpc_argp = &calldata->arg;
2183 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002184 task_setup_data.callback_data = calldata;
2185 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002186 if (IS_ERR(task))
2187 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002188 status = 0;
2189 if (wait)
2190 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002191 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002192 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002193out_free_calldata:
2194 kfree(calldata);
2195out:
Fred Isamanf7e89172011-01-06 11:36:32 +00002196 if (roc)
2197 pnfs_roc_release(state->inode);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002198 nfs4_put_open_state(state);
2199 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002200 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201}
2202
Trond Myklebust2b484292010-09-17 10:56:51 -04002203static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002204nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 struct nfs4_state *state;
2207
Trond Myklebust565277f2007-10-15 18:17:53 -04002208 /* Protect against concurrent sillydeletes */
Andy Adamson82be4172012-05-23 05:02:35 -04002209 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2210 ctx->cred, &ctx->mdsthreshold);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002211 if (IS_ERR(state))
2212 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002213 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002214 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215}
2216
Trond Myklebust1185a552009-12-03 15:54:02 -05002217static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002218{
2219 if (ctx->state == NULL)
2220 return;
2221 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002222 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002223 else
Al Viro643168c2011-06-22 18:20:23 -04002224 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002225}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226
2227static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2228{
Benny Halevy43652ad2009-04-01 09:21:54 -04002229 struct nfs4_server_caps_arg args = {
2230 .fhandle = fhandle,
2231 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 struct nfs4_server_caps_res res = {};
2233 struct rpc_message msg = {
2234 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002235 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 .rpc_resp = &res,
2237 };
2238 int status;
2239
Bryan Schumaker7c513052011-03-24 17:12:24 +00002240 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 if (status == 0) {
2242 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002243 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2244 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2245 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2246 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2247 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2249 server->caps |= NFS_CAP_ACLS;
2250 if (res.has_links != 0)
2251 server->caps |= NFS_CAP_HARDLINKS;
2252 if (res.has_symlinks != 0)
2253 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002254 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2255 server->caps |= NFS_CAP_FILEID;
2256 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2257 server->caps |= NFS_CAP_MODE;
2258 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2259 server->caps |= NFS_CAP_NLINK;
2260 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2261 server->caps |= NFS_CAP_OWNER;
2262 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2263 server->caps |= NFS_CAP_OWNER_GROUP;
2264 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2265 server->caps |= NFS_CAP_ATIME;
2266 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2267 server->caps |= NFS_CAP_CTIME;
2268 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2269 server->caps |= NFS_CAP_MTIME;
2270
Trond Myklebusta65318b2009-03-11 14:10:28 -04002271 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2272 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2273 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002275 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002277
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 return status;
2279}
2280
Trond Myklebust55a97592006-06-09 09:34:19 -04002281int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282{
2283 struct nfs4_exception exception = { };
2284 int err;
2285 do {
2286 err = nfs4_handle_exception(server,
2287 _nfs4_server_capabilities(server, fhandle),
2288 &exception);
2289 } while (exception.retry);
2290 return err;
2291}
2292
2293static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2294 struct nfs_fsinfo *info)
2295{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 struct nfs4_lookup_root_arg args = {
2297 .bitmask = nfs4_fattr_bitmap,
2298 };
2299 struct nfs4_lookup_res res = {
2300 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002301 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 .fh = fhandle,
2303 };
2304 struct rpc_message msg = {
2305 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2306 .rpc_argp = &args,
2307 .rpc_resp = &res,
2308 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002309
Trond Myklebust0e574af2005-10-27 22:12:38 -04002310 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002311 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312}
2313
2314static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2315 struct nfs_fsinfo *info)
2316{
2317 struct nfs4_exception exception = { };
2318 int err;
2319 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002320 err = _nfs4_lookup_root(server, fhandle, info);
2321 switch (err) {
2322 case 0:
2323 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002324 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002325 default:
2326 err = nfs4_handle_exception(server, err, &exception);
2327 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002329out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 return err;
2331}
2332
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002333static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2334 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2335{
2336 struct rpc_auth *auth;
2337 int ret;
2338
2339 auth = rpcauth_create(flavor, server->client);
2340 if (!auth) {
2341 ret = -EIO;
2342 goto out;
2343 }
2344 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002345out:
2346 return ret;
2347}
2348
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002349static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002350 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002352 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002353 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002355 len = gss_mech_list_pseudoflavors(&flav_array[0]);
2356 flav_array[len] = RPC_AUTH_NULL;
2357 len += 1;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002358
2359 for (i = 0; i < len; i++) {
2360 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002361 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002362 continue;
2363 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002364 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002365 /*
2366 * -EACCESS could mean that the user doesn't have correct permissions
2367 * to access the mount. It could also mean that we tried to mount
2368 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2369 * existing mount programs don't handle -EACCES very well so it should
2370 * be mapped to -EPERM instead.
2371 */
2372 if (status == -EACCES)
2373 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002374 return status;
2375}
2376
2377/*
2378 * get the file handle for the "/" directory on the server
2379 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002380int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2381 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002382{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002383 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002384 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002385 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2386 /*
2387 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2388 * by nfs4_map_errors() as this function exits.
2389 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002390 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 if (status == 0)
2392 status = nfs4_server_capabilities(server, fhandle);
2393 if (status == 0)
2394 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002395 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396}
2397
Bryan Schumakerbae36242012-05-10 15:07:31 -04002398static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2399 struct nfs_fsinfo *info)
2400{
2401 int error;
2402 struct nfs_fattr *fattr = info->fattr;
2403
2404 error = nfs4_server_capabilities(server, mntfh);
2405 if (error < 0) {
2406 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2407 return error;
2408 }
2409
2410 error = nfs4_proc_getattr(server, mntfh, fattr);
2411 if (error < 0) {
2412 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2413 return error;
2414 }
2415
2416 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2417 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2418 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2419
2420 return error;
2421}
2422
Manoj Naik6b97fd32006-06-09 09:34:29 -04002423/*
2424 * Get locations and (maybe) other attributes of a referral.
2425 * Note that we'll actually follow the referral later when
2426 * we detect fsid mismatch in inode revalidation
2427 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002428static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2429 const struct qstr *name, struct nfs_fattr *fattr,
2430 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002431{
2432 int status = -ENOMEM;
2433 struct page *page = NULL;
2434 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002435
2436 page = alloc_page(GFP_KERNEL);
2437 if (page == NULL)
2438 goto out;
2439 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2440 if (locations == NULL)
2441 goto out;
2442
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002443 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002444 if (status != 0)
2445 goto out;
2446 /* Make sure server returned a different fsid for the referral */
2447 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002448 dprintk("%s: server did not return a different fsid for"
2449 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002450 status = -EIO;
2451 goto out;
2452 }
Andy Adamson533eb462011-06-13 18:25:56 -04002453 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2454 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002455
Andy Adamson533eb462011-06-13 18:25:56 -04002456 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002457 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002458 memset(fhandle, 0, sizeof(struct nfs_fh));
2459out:
2460 if (page)
2461 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002462 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002463 return status;
2464}
2465
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2467{
2468 struct nfs4_getattr_arg args = {
2469 .fh = fhandle,
2470 .bitmask = server->attr_bitmask,
2471 };
2472 struct nfs4_getattr_res res = {
2473 .fattr = fattr,
2474 .server = server,
2475 };
2476 struct rpc_message msg = {
2477 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2478 .rpc_argp = &args,
2479 .rpc_resp = &res,
2480 };
2481
Trond Myklebust0e574af2005-10-27 22:12:38 -04002482 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002483 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484}
2485
2486static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2487{
2488 struct nfs4_exception exception = { };
2489 int err;
2490 do {
2491 err = nfs4_handle_exception(server,
2492 _nfs4_proc_getattr(server, fhandle, fattr),
2493 &exception);
2494 } while (exception.retry);
2495 return err;
2496}
2497
2498/*
2499 * The file is not closed if it is opened due to the a request to change
2500 * the size of the file. The open call will not be needed once the
2501 * VFS layer lookup-intents are implemented.
2502 *
2503 * Close is called when the inode is destroyed.
2504 * If we haven't opened the file for O_WRONLY, we
2505 * need to in the size_change case to obtain a stateid.
2506 *
2507 * Got race?
2508 * Because OPEN is always done by name in nfsv4, it is
2509 * possible that we opened a different file by the same
2510 * name. We can recognize this race condition, but we
2511 * can't do anything about it besides returning an error.
2512 *
2513 * This will be fixed with VFS changes (lookup-intent).
2514 */
2515static int
2516nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2517 struct iattr *sattr)
2518{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002519 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002520 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002521 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 int status;
2523
Benny Halevy8a1636c2010-07-14 15:43:57 -04002524 if (pnfs_ld_layoutret_on_setattr(inode))
2525 pnfs_return_layout(inode);
2526
Trond Myklebust0e574af2005-10-27 22:12:38 -04002527 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528
Trond Myklebustd5308382005-11-04 15:33:38 -05002529 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002530 if (sattr->ia_valid & ATTR_FILE) {
2531 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002532
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002533 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002534 if (ctx) {
2535 cred = ctx->cred;
2536 state = ctx->state;
2537 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002538 }
2539
Trond Myklebust536e43d2012-01-17 22:04:26 -05002540 /* Deal with open(O_TRUNC) */
2541 if (sattr->ia_valid & ATTR_OPEN)
2542 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2543
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002544 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002545 if (status == 0)
2546 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 return status;
2548}
2549
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002550static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2551 const struct qstr *name, struct nfs_fh *fhandle,
2552 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002553{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002554 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002555 int status;
2556 struct nfs4_lookup_arg args = {
2557 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002558 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002559 .name = name,
2560 };
2561 struct nfs4_lookup_res res = {
2562 .server = server,
2563 .fattr = fattr,
2564 .fh = fhandle,
2565 };
2566 struct rpc_message msg = {
2567 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2568 .rpc_argp = &args,
2569 .rpc_resp = &res,
2570 };
2571
2572 nfs_fattr_init(fattr);
2573
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002575 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 dprintk("NFS reply lookup: %d\n", status);
2577 return status;
2578}
2579
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002580static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002581{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002582 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002583 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002584 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2585 fattr->nlink = 2;
2586}
2587
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002588static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2589 struct qstr *name, struct nfs_fh *fhandle,
2590 struct nfs_fattr *fattr)
2591{
2592 struct nfs4_exception exception = { };
2593 struct rpc_clnt *client = *clnt;
2594 int err;
2595 do {
2596 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2597 switch (err) {
2598 case -NFS4ERR_BADNAME:
2599 err = -ENOENT;
2600 goto out;
2601 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002602 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002603 goto out;
2604 case -NFS4ERR_WRONGSEC:
2605 err = -EPERM;
2606 if (client != *clnt)
2607 goto out;
2608
2609 client = nfs4_create_sec_client(client, dir, name);
2610 if (IS_ERR(client))
2611 return PTR_ERR(client);
2612
2613 exception.retry = 1;
2614 break;
2615 default:
2616 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2617 }
2618 } while (exception.retry);
2619
2620out:
2621 if (err == 0)
2622 *clnt = client;
2623 else if (client != *clnt)
2624 rpc_shutdown_client(client);
2625
2626 return err;
2627}
2628
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002629static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002630 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002632 int status;
2633 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002634
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002635 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2636 if (client != NFS_CLIENT(dir)) {
2637 rpc_shutdown_client(client);
2638 nfs_fixup_secinfo_attributes(fattr);
2639 }
2640 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641}
2642
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002643struct rpc_clnt *
2644nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2645 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2646{
2647 int status;
2648 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2649
2650 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2651 if (status < 0) {
2652 rpc_shutdown_client(client);
2653 return ERR_PTR(status);
2654 }
2655 return client;
2656}
2657
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2659{
Trond Myklebust76b32992007-08-10 17:45:11 -04002660 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 struct nfs4_accessargs args = {
2662 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002663 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002665 struct nfs4_accessres res = {
2666 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002667 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 struct rpc_message msg = {
2669 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2670 .rpc_argp = &args,
2671 .rpc_resp = &res,
2672 .rpc_cred = entry->cred,
2673 };
2674 int mode = entry->mask;
2675 int status;
2676
2677 /*
2678 * Determine which access bits we want to ask for...
2679 */
2680 if (mode & MAY_READ)
2681 args.access |= NFS4_ACCESS_READ;
2682 if (S_ISDIR(inode->i_mode)) {
2683 if (mode & MAY_WRITE)
2684 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2685 if (mode & MAY_EXEC)
2686 args.access |= NFS4_ACCESS_LOOKUP;
2687 } else {
2688 if (mode & MAY_WRITE)
2689 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2690 if (mode & MAY_EXEC)
2691 args.access |= NFS4_ACCESS_EXECUTE;
2692 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002693
2694 res.fattr = nfs_alloc_fattr();
2695 if (res.fattr == NULL)
2696 return -ENOMEM;
2697
Bryan Schumaker7c513052011-03-24 17:12:24 +00002698 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 if (!status) {
2700 entry->mask = 0;
2701 if (res.access & NFS4_ACCESS_READ)
2702 entry->mask |= MAY_READ;
2703 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2704 entry->mask |= MAY_WRITE;
2705 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2706 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002707 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002709 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 return status;
2711}
2712
2713static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2714{
2715 struct nfs4_exception exception = { };
2716 int err;
2717 do {
2718 err = nfs4_handle_exception(NFS_SERVER(inode),
2719 _nfs4_proc_access(inode, entry),
2720 &exception);
2721 } while (exception.retry);
2722 return err;
2723}
2724
2725/*
2726 * TODO: For the time being, we don't try to get any attributes
2727 * along with any of the zero-copy operations READ, READDIR,
2728 * READLINK, WRITE.
2729 *
2730 * In the case of the first three, we want to put the GETATTR
2731 * after the read-type operation -- this is because it is hard
2732 * to predict the length of a GETATTR response in v4, and thus
2733 * align the READ data correctly. This means that the GETATTR
2734 * may end up partially falling into the page cache, and we should
2735 * shift it into the 'tail' of the xdr_buf before processing.
2736 * To do this efficiently, we need to know the total length
2737 * of data received, which doesn't seem to be available outside
2738 * of the RPC layer.
2739 *
2740 * In the case of WRITE, we also want to put the GETATTR after
2741 * the operation -- in this case because we want to make sure
2742 * we get the post-operation mtime and size. This means that
2743 * we can't use xdr_encode_pages() as written: we need a variant
2744 * of it which would leave room in the 'tail' iovec.
2745 *
2746 * Both of these changes to the XDR layer would in fact be quite
2747 * minor, but I decided to leave them for a subsequent patch.
2748 */
2749static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2750 unsigned int pgbase, unsigned int pglen)
2751{
2752 struct nfs4_readlink args = {
2753 .fh = NFS_FH(inode),
2754 .pgbase = pgbase,
2755 .pglen = pglen,
2756 .pages = &page,
2757 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002758 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 struct rpc_message msg = {
2760 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2761 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002762 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 };
2764
Bryan Schumaker7c513052011-03-24 17:12:24 +00002765 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 -07002766}
2767
2768static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2769 unsigned int pgbase, unsigned int pglen)
2770{
2771 struct nfs4_exception exception = { };
2772 int err;
2773 do {
2774 err = nfs4_handle_exception(NFS_SERVER(inode),
2775 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2776 &exception);
2777 } while (exception.retry);
2778 return err;
2779}
2780
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781/*
2782 * Got race?
2783 * We will need to arrange for the VFS layer to provide an atomic open.
2784 * Until then, this create/open method is prone to inefficiency and race
2785 * conditions due to the lookup, create, and open VFS calls from sys_open()
2786 * placed on the wire.
2787 *
2788 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2789 * The file will be opened again in the subsequent VFS open call
2790 * (nfs4_proc_file_open).
2791 *
2792 * The open for read will just hang around to be used by any process that
2793 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2794 */
2795
2796static int
2797nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002798 int flags, struct nfs_open_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799{
Al Viro82a2c1b2011-06-22 18:30:55 -04002800 struct dentry *de = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 struct nfs4_state *state;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002802 struct rpc_cred *cred = NULL;
2803 fmode_t fmode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 int status = 0;
2805
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002806 if (ctx != NULL) {
2807 cred = ctx->cred;
Al Viro3d4ff432011-06-22 18:40:12 -04002808 de = ctx->dentry;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002809 fmode = ctx->mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 }
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002811 sattr->ia_mode &= ~current_umask();
Andy Adamson82be4172012-05-23 05:02:35 -04002812 state = nfs4_do_open(dir, de, fmode, flags, sattr, cred, NULL);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002813 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 if (IS_ERR(state)) {
2815 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002816 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002818 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002819 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002820 if (ctx != NULL)
2821 ctx->state = state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002822 else
Al Viro643168c2011-06-22 18:20:23 -04002823 nfs4_close_sync(state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824out:
2825 return status;
2826}
2827
2828static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2829{
Trond Myklebust16e42952005-10-27 22:12:44 -04002830 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002831 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002833 .name.len = name->len,
2834 .name.name = name->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002836 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002837 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002838 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002840 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2841 .rpc_argp = &args,
2842 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 };
Trond Myklebust778d2812012-04-27 13:48:19 -04002844 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04002845
Bryan Schumaker7c513052011-03-24 17:12:24 +00002846 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04002847 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04002848 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 return status;
2850}
2851
2852static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2853{
2854 struct nfs4_exception exception = { };
2855 int err;
2856 do {
2857 err = nfs4_handle_exception(NFS_SERVER(dir),
2858 _nfs4_proc_remove(dir, name),
2859 &exception);
2860 } while (exception.retry);
2861 return err;
2862}
2863
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002864static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002866 struct nfs_server *server = NFS_SERVER(dir);
2867 struct nfs_removeargs *args = msg->rpc_argp;
2868 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002870 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05002872 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873}
2874
Bryan Schumaker34e137c2012-03-19 14:54:41 -04002875static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
2876{
2877 if (nfs4_setup_sequence(NFS_SERVER(data->dir),
2878 &data->args.seq_args,
2879 &data->res.seq_res,
2880 task))
2881 return;
2882 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883}
2884
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002885static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002887 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2888
Trond Myklebust14516c32010-07-31 14:29:06 -04002889 if (!nfs4_sequence_done(task, &res->seq_res))
2890 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002891 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002892 return 0;
2893 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002894 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895}
2896
Jeff Laytond3d41522010-09-17 17:31:57 -04002897static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2898{
2899 struct nfs_server *server = NFS_SERVER(dir);
2900 struct nfs_renameargs *arg = msg->rpc_argp;
2901 struct nfs_renameres *res = msg->rpc_resp;
2902
2903 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04002904 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002905 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04002906}
2907
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04002908static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
2909{
2910 if (nfs4_setup_sequence(NFS_SERVER(data->old_dir),
2911 &data->args.seq_args,
2912 &data->res.seq_res,
2913 task))
2914 return;
2915 rpc_call_start(task);
Jeff Laytond3d41522010-09-17 17:31:57 -04002916}
2917
2918static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2919 struct inode *new_dir)
2920{
2921 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2922
2923 if (!nfs4_sequence_done(task, &res->seq_res))
2924 return 0;
2925 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2926 return 0;
2927
2928 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04002929 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04002930 return 1;
2931}
2932
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2934 struct inode *new_dir, struct qstr *new_name)
2935{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002936 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002937 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 .old_dir = NFS_FH(old_dir),
2939 .new_dir = NFS_FH(new_dir),
2940 .old_name = old_name,
2941 .new_name = new_name,
2942 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002943 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002944 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002945 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 struct rpc_message msg = {
2947 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2948 .rpc_argp = &arg,
2949 .rpc_resp = &res,
2950 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002951 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952
Bryan Schumaker7c513052011-03-24 17:12:24 +00002953 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 if (!status) {
2955 update_changeattr(old_dir, &res.old_cinfo);
2956 update_changeattr(new_dir, &res.new_cinfo);
2957 }
2958 return status;
2959}
2960
2961static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2962 struct inode *new_dir, struct qstr *new_name)
2963{
2964 struct nfs4_exception exception = { };
2965 int err;
2966 do {
2967 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2968 _nfs4_proc_rename(old_dir, old_name,
2969 new_dir, new_name),
2970 &exception);
2971 } while (exception.retry);
2972 return err;
2973}
2974
2975static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2976{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002977 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 struct nfs4_link_arg arg = {
2979 .fh = NFS_FH(inode),
2980 .dir_fh = NFS_FH(dir),
2981 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002982 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002984 struct nfs4_link_res res = {
2985 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002986 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 struct rpc_message msg = {
2988 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2989 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002990 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 };
Trond Myklebust136f2622010-04-16 16:22:49 -04002992 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993
Trond Myklebust136f2622010-04-16 16:22:49 -04002994 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04002995 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04002996 goto out;
2997
Bryan Schumaker7c513052011-03-24 17:12:24 +00002998 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002999 if (!status) {
3000 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04003001 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003002 }
Trond Myklebust136f2622010-04-16 16:22:49 -04003003out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003004 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 return status;
3006}
3007
3008static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3009{
3010 struct nfs4_exception exception = { };
3011 int err;
3012 do {
3013 err = nfs4_handle_exception(NFS_SERVER(inode),
3014 _nfs4_proc_link(inode, dir, name),
3015 &exception);
3016 } while (exception.retry);
3017 return err;
3018}
3019
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003020struct nfs4_createdata {
3021 struct rpc_message msg;
3022 struct nfs4_create_arg arg;
3023 struct nfs4_create_res res;
3024 struct nfs_fh fh;
3025 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003026};
3027
3028static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3029 struct qstr *name, struct iattr *sattr, u32 ftype)
3030{
3031 struct nfs4_createdata *data;
3032
3033 data = kzalloc(sizeof(*data), GFP_KERNEL);
3034 if (data != NULL) {
3035 struct nfs_server *server = NFS_SERVER(dir);
3036
3037 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3038 data->msg.rpc_argp = &data->arg;
3039 data->msg.rpc_resp = &data->res;
3040 data->arg.dir_fh = NFS_FH(dir);
3041 data->arg.server = server;
3042 data->arg.name = name;
3043 data->arg.attrs = sattr;
3044 data->arg.ftype = ftype;
3045 data->arg.bitmask = server->attr_bitmask;
3046 data->res.server = server;
3047 data->res.fh = &data->fh;
3048 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003049 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003050 }
3051 return data;
3052}
3053
3054static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3055{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003056 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003057 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003058 if (status == 0) {
3059 update_changeattr(dir, &data->res.dir_cinfo);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003060 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3061 }
3062 return status;
3063}
3064
3065static void nfs4_free_createdata(struct nfs4_createdata *data)
3066{
3067 kfree(data);
3068}
3069
Chuck Lever4f390c12006-08-22 20:06:22 -04003070static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003071 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003073 struct nfs4_createdata *data;
3074 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075
Chuck Lever94a6d752006-08-22 20:06:23 -04003076 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003077 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003078
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003079 status = -ENOMEM;
3080 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3081 if (data == NULL)
3082 goto out;
3083
3084 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3085 data->arg.u.symlink.pages = &page;
3086 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003088 status = nfs4_do_create(dir, dentry, data);
3089
3090 nfs4_free_createdata(data);
3091out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 return status;
3093}
3094
Chuck Lever4f390c12006-08-22 20:06:22 -04003095static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003096 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097{
3098 struct nfs4_exception exception = { };
3099 int err;
3100 do {
3101 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003102 _nfs4_proc_symlink(dir, dentry, page,
3103 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 &exception);
3105 } while (exception.retry);
3106 return err;
3107}
3108
3109static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3110 struct iattr *sattr)
3111{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003112 struct nfs4_createdata *data;
3113 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003115 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3116 if (data == NULL)
3117 goto out;
3118
3119 status = nfs4_do_create(dir, dentry, data);
3120
3121 nfs4_free_createdata(data);
3122out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 return status;
3124}
3125
3126static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3127 struct iattr *sattr)
3128{
3129 struct nfs4_exception exception = { };
3130 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003131
3132 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 do {
3134 err = nfs4_handle_exception(NFS_SERVER(dir),
3135 _nfs4_proc_mkdir(dir, dentry, sattr),
3136 &exception);
3137 } while (exception.retry);
3138 return err;
3139}
3140
3141static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003142 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143{
3144 struct inode *dir = dentry->d_inode;
3145 struct nfs4_readdir_arg args = {
3146 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003147 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 .pgbase = 0,
3149 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003150 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003151 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 };
3153 struct nfs4_readdir_res res;
3154 struct rpc_message msg = {
3155 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3156 .rpc_argp = &args,
3157 .rpc_resp = &res,
3158 .rpc_cred = cred,
3159 };
3160 int status;
3161
Harvey Harrison3110ff82008-05-02 13:42:44 -07003162 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003163 dentry->d_parent->d_name.name,
3164 dentry->d_name.name,
3165 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
3167 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003168 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 -05003169 if (status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003171 status += args.pgbase;
3172 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003173
3174 nfs_invalidate_atime(dir);
3175
Harvey Harrison3110ff82008-05-02 13:42:44 -07003176 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 return status;
3178}
3179
3180static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003181 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182{
3183 struct nfs4_exception exception = { };
3184 int err;
3185 do {
3186 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3187 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003188 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 &exception);
3190 } while (exception.retry);
3191 return err;
3192}
3193
3194static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3195 struct iattr *sattr, dev_t rdev)
3196{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003197 struct nfs4_createdata *data;
3198 int mode = sattr->ia_mode;
3199 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200
3201 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3202 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003203
3204 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3205 if (data == NULL)
3206 goto out;
3207
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003209 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003211 data->arg.ftype = NF4BLK;
3212 data->arg.u.device.specdata1 = MAJOR(rdev);
3213 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 }
3215 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003216 data->arg.ftype = NF4CHR;
3217 data->arg.u.device.specdata1 = MAJOR(rdev);
3218 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003221 status = nfs4_do_create(dir, dentry, data);
3222
3223 nfs4_free_createdata(data);
3224out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 return status;
3226}
3227
3228static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3229 struct iattr *sattr, dev_t rdev)
3230{
3231 struct nfs4_exception exception = { };
3232 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003233
3234 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 do {
3236 err = nfs4_handle_exception(NFS_SERVER(dir),
3237 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3238 &exception);
3239 } while (exception.retry);
3240 return err;
3241}
3242
3243static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3244 struct nfs_fsstat *fsstat)
3245{
3246 struct nfs4_statfs_arg args = {
3247 .fh = fhandle,
3248 .bitmask = server->attr_bitmask,
3249 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003250 struct nfs4_statfs_res res = {
3251 .fsstat = fsstat,
3252 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 struct rpc_message msg = {
3254 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3255 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003256 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 };
3258
Trond Myklebust0e574af2005-10-27 22:12:38 -04003259 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003260 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261}
3262
3263static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3264{
3265 struct nfs4_exception exception = { };
3266 int err;
3267 do {
3268 err = nfs4_handle_exception(server,
3269 _nfs4_proc_statfs(server, fhandle, fsstat),
3270 &exception);
3271 } while (exception.retry);
3272 return err;
3273}
3274
3275static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3276 struct nfs_fsinfo *fsinfo)
3277{
3278 struct nfs4_fsinfo_arg args = {
3279 .fh = fhandle,
3280 .bitmask = server->attr_bitmask,
3281 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003282 struct nfs4_fsinfo_res res = {
3283 .fsinfo = fsinfo,
3284 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 struct rpc_message msg = {
3286 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3287 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003288 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 };
3290
Bryan Schumaker7c513052011-03-24 17:12:24 +00003291 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292}
3293
3294static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3295{
3296 struct nfs4_exception exception = { };
3297 int err;
3298
3299 do {
3300 err = nfs4_handle_exception(server,
3301 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3302 &exception);
3303 } while (exception.retry);
3304 return err;
3305}
3306
3307static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3308{
Trond Myklebust0e574af2005-10-27 22:12:38 -04003309 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3311}
3312
3313static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3314 struct nfs_pathconf *pathconf)
3315{
3316 struct nfs4_pathconf_arg args = {
3317 .fh = fhandle,
3318 .bitmask = server->attr_bitmask,
3319 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003320 struct nfs4_pathconf_res res = {
3321 .pathconf = pathconf,
3322 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 struct rpc_message msg = {
3324 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3325 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003326 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 };
3328
3329 /* None of the pathconf attributes are mandatory to implement */
3330 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3331 memset(pathconf, 0, sizeof(*pathconf));
3332 return 0;
3333 }
3334
Trond Myklebust0e574af2005-10-27 22:12:38 -04003335 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003336 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337}
3338
3339static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3340 struct nfs_pathconf *pathconf)
3341{
3342 struct nfs4_exception exception = { };
3343 int err;
3344
3345 do {
3346 err = nfs4_handle_exception(server,
3347 _nfs4_proc_pathconf(server, fhandle, pathconf),
3348 &exception);
3349 } while (exception.retry);
3350 return err;
3351}
3352
Benny Halevyd20581a2011-05-22 19:52:03 +03003353void __nfs4_read_done_cb(struct nfs_read_data *data)
3354{
Fred Isamancd841602012-04-20 14:47:44 -04003355 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003356}
3357
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003358static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359{
Fred Isamancd841602012-04-20 14:47:44 -04003360 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003362 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003363 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003364 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003366
Benny Halevyd20581a2011-05-22 19:52:03 +03003367 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003369 renew_lease(server, data->timestamp);
3370 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371}
3372
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003373static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3374{
3375
3376 dprintk("--> %s\n", __func__);
3377
3378 if (!nfs4_sequence_done(task, &data->res.seq_res))
3379 return -EAGAIN;
3380
Benny Halevyd20581a2011-05-22 19:52:03 +03003381 return data->read_done_cb ? data->read_done_cb(task, data) :
3382 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003383}
3384
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003385static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003388 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003389 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003390 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391}
3392
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003393static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3394{
Fred Isamancd841602012-04-20 14:47:44 -04003395 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003396 &data->args.seq_args,
3397 &data->res.seq_res,
3398 task))
3399 return;
3400 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401}
3402
Fred Isamanb029bc92011-03-03 15:13:42 +00003403static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404{
Fred Isamancd841602012-04-20 14:47:44 -04003405 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003407 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003408 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003409 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003411 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003413 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003414 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003415 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416}
3417
Fred Isamanb029bc92011-03-03 15:13:42 +00003418static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3419{
3420 if (!nfs4_sequence_done(task, &data->res.seq_res))
3421 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003422 return data->write_done_cb ? data->write_done_cb(task, data) :
3423 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003424}
3425
Trond Myklebust5a37f852012-04-28 14:55:16 -04003426static
3427bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
3428{
3429 const struct nfs_pgio_header *hdr = data->header;
3430
3431 /* Don't request attributes for pNFS or O_DIRECT writes */
3432 if (data->ds_clp != NULL || hdr->dreq != NULL)
3433 return false;
3434 /* Otherwise, request attributes if and only if we don't hold
3435 * a delegation
3436 */
3437 return nfs_have_delegation(hdr->inode, FMODE_READ) == 0;
3438}
3439
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003440static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441{
Fred Isamancd841602012-04-20 14:47:44 -04003442 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003443
Trond Myklebust5a37f852012-04-28 14:55:16 -04003444 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003445 data->args.bitmask = NULL;
3446 data->res.fattr = NULL;
3447 } else
3448 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003449
Fred Isamanb029bc92011-03-03 15:13:42 +00003450 if (!data->write_done_cb)
3451 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003452 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 data->timestamp = jiffies;
3454
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003455 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003456 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457}
3458
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003459static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3460{
Fred Isamancd841602012-04-20 14:47:44 -04003461 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003462 &data->args.seq_args,
3463 &data->res.seq_res,
3464 task))
3465 return;
3466 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467}
3468
Fred Isaman0b7c0152012-04-20 14:47:39 -04003469static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3470{
3471 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3472 &data->args.seq_args,
3473 &data->res.seq_res,
3474 task))
3475 return;
3476 rpc_call_start(task);
3477}
3478
3479static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003482
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003483 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003484 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003485 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003487 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488}
3489
Fred Isaman0b7c0152012-04-20 14:47:39 -04003490static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003491{
3492 if (!nfs4_sequence_done(task, &data->res.seq_res))
3493 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003494 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003495}
3496
Fred Isaman0b7c0152012-04-20 14:47:39 -04003497static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003499 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003500
Fred Isaman0b7c0152012-04-20 14:47:39 -04003501 if (data->commit_done_cb == NULL)
3502 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003503 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003504 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003505 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506}
3507
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003508struct nfs4_renewdata {
3509 struct nfs_client *client;
3510 unsigned long timestamp;
3511};
3512
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513/*
3514 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3515 * standalone procedure for queueing an asynchronous RENEW.
3516 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003517static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003518{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003519 struct nfs4_renewdata *data = calldata;
3520 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003521
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003522 if (atomic_read(&clp->cl_count) > 1)
3523 nfs4_schedule_state_renewal(clp);
3524 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003525 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003526}
3527
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003528static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003530 struct nfs4_renewdata *data = calldata;
3531 struct nfs_client *clp = data->client;
3532 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533
3534 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003535 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003536 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3537 return;
3538 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003539 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003540 return;
3541 }
3542 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003544 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545}
3546
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003547static const struct rpc_call_ops nfs4_renew_ops = {
3548 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003549 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003550};
3551
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003552static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553{
3554 struct rpc_message msg = {
3555 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3556 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003557 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003559 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003561 if (renew_flags == 0)
3562 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003563 if (!atomic_inc_not_zero(&clp->cl_count))
3564 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003565 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003566 if (data == NULL)
3567 return -ENOMEM;
3568 data->client = clp;
3569 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003571 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572}
3573
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003574static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575{
3576 struct rpc_message msg = {
3577 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3578 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003579 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580 };
3581 unsigned long now = jiffies;
3582 int status;
3583
3584 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3585 if (status < 0)
3586 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003587 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 return 0;
3589}
3590
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003591static inline int nfs4_server_supports_acls(struct nfs_server *server)
3592{
3593 return (server->caps & NFS_CAP_ACLS)
3594 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3595 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3596}
3597
3598/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3599 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3600 * the stack.
3601 */
3602#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3603
Neil Hormane9e3d722011-03-04 19:26:03 -05003604static int buf_to_pages_noslab(const void *buf, size_t buflen,
3605 struct page **pages, unsigned int *pgbase)
3606{
3607 struct page *newpage, **spages;
3608 int rc = 0;
3609 size_t len;
3610 spages = pages;
3611
3612 do {
Jovi Zhang43b7c3f2011-03-02 23:19:37 +00003613 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003614 newpage = alloc_page(GFP_KERNEL);
3615
3616 if (newpage == NULL)
3617 goto unwind;
3618 memcpy(page_address(newpage), buf, len);
3619 buf += len;
3620 buflen -= len;
3621 *pages++ = newpage;
3622 rc++;
3623 } while (buflen != 0);
3624
3625 return rc;
3626
3627unwind:
3628 for(; rc > 0; rc--)
3629 __free_page(spages[rc-1]);
3630 return -ENOMEM;
3631}
3632
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003633struct nfs4_cached_acl {
3634 int cached;
3635 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003636 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003637};
3638
3639static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003640{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003641 struct nfs_inode *nfsi = NFS_I(inode);
3642
3643 spin_lock(&inode->i_lock);
3644 kfree(nfsi->nfs4_acl);
3645 nfsi->nfs4_acl = acl;
3646 spin_unlock(&inode->i_lock);
3647}
3648
3649static void nfs4_zap_acl_attr(struct inode *inode)
3650{
3651 nfs4_set_cached_acl(inode, NULL);
3652}
3653
3654static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3655{
3656 struct nfs_inode *nfsi = NFS_I(inode);
3657 struct nfs4_cached_acl *acl;
3658 int ret = -ENOENT;
3659
3660 spin_lock(&inode->i_lock);
3661 acl = nfsi->nfs4_acl;
3662 if (acl == NULL)
3663 goto out;
3664 if (buf == NULL) /* user is just asking for length */
3665 goto out_len;
3666 if (acl->cached == 0)
3667 goto out;
3668 ret = -ERANGE; /* see getxattr(2) man page */
3669 if (acl->len > buflen)
3670 goto out;
3671 memcpy(buf, acl->data, acl->len);
3672out_len:
3673 ret = acl->len;
3674out:
3675 spin_unlock(&inode->i_lock);
3676 return ret;
3677}
3678
Sachin Prabhu5794d212012-04-17 14:36:40 +01003679static 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 +00003680{
3681 struct nfs4_cached_acl *acl;
3682
Sachin Prabhu5794d212012-04-17 14:36:40 +01003683 if (pages && acl_len <= PAGE_SIZE) {
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003684 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3685 if (acl == NULL)
3686 goto out;
3687 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003688 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003689 } else {
3690 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3691 if (acl == NULL)
3692 goto out;
3693 acl->cached = 0;
3694 }
3695 acl->len = acl_len;
3696out:
3697 nfs4_set_cached_acl(inode, acl);
3698}
3699
Andy Adamsonbf118a32011-12-07 11:55:27 -05003700/*
3701 * The getxattr API returns the required buffer length when called with a
3702 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3703 * the required buf. On a NULL buf, we send a page of data to the server
3704 * guessing that the ACL request can be serviced by a page. If so, we cache
3705 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3706 * the cache. If not so, we throw away the page, and cache the required
3707 * length. The next getxattr call will then produce another round trip to
3708 * the server, this time with the input buf of the required size.
3709 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003710static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003711{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003712 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003713 struct nfs_getaclargs args = {
3714 .fh = NFS_FH(inode),
3715 .acl_pages = pages,
3716 .acl_len = buflen,
3717 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003718 struct nfs_getaclres res = {
3719 .acl_len = buflen,
3720 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003721 struct rpc_message msg = {
3722 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3723 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003724 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003725 };
Andy Adamsonbf118a32011-12-07 11:55:27 -05003726 int ret = -ENOMEM, npages, i, acl_len = 0;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003727
Andy Adamsonbf118a32011-12-07 11:55:27 -05003728 npages = (buflen + PAGE_SIZE - 1) >> PAGE_SHIFT;
3729 /* As long as we're doing a round trip to the server anyway,
3730 * let's be prepared for a page of acl data. */
3731 if (npages == 0)
3732 npages = 1;
3733
Sachin Prabhu5a006892012-04-17 14:35:39 +01003734 /* Add an extra page to handle the bitmap returned */
3735 npages++;
3736
Andy Adamsonbf118a32011-12-07 11:55:27 -05003737 for (i = 0; i < npages; i++) {
3738 pages[i] = alloc_page(GFP_KERNEL);
3739 if (!pages[i])
3740 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003741 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01003742
3743 /* for decoding across pages */
3744 res.acl_scratch = alloc_page(GFP_KERNEL);
3745 if (!res.acl_scratch)
3746 goto out_free;
3747
Andy Adamsonbf118a32011-12-07 11:55:27 -05003748 args.acl_len = npages * PAGE_SIZE;
3749 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003750
Andy Adamsonbf118a32011-12-07 11:55:27 -05003751 /* Let decode_getfacl know not to fail if the ACL data is larger than
3752 * the page we send as a guess */
3753 if (buf == NULL)
3754 res.acl_flags |= NFS4_ACL_LEN_REQUEST;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003755
Peng Taode040be2012-01-10 22:42:47 +08003756 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003757 __func__, buf, buflen, npages, args.acl_len);
3758 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3759 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003760 if (ret)
3761 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003762
3763 acl_len = res.acl_len - res.acl_data_offset;
3764 if (acl_len > args.acl_len)
Sachin Prabhu5794d212012-04-17 14:36:40 +01003765 nfs4_write_cached_acl(inode, NULL, 0, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003766 else
Sachin Prabhu5794d212012-04-17 14:36:40 +01003767 nfs4_write_cached_acl(inode, pages, res.acl_data_offset,
Andy Adamsonbf118a32011-12-07 11:55:27 -05003768 acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003769 if (buf) {
3770 ret = -ERANGE;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003771 if (acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003772 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003773 _copy_from_pages(buf, pages, res.acl_data_offset,
Sachin Prabhu20e0fa92012-03-22 16:46:28 +00003774 acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003775 }
Andy Adamsonbf118a32011-12-07 11:55:27 -05003776 ret = acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003777out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003778 for (i = 0; i < npages; i++)
3779 if (pages[i])
3780 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05003781 if (res.acl_scratch)
3782 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003783 return ret;
3784}
3785
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003786static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3787{
3788 struct nfs4_exception exception = { };
3789 ssize_t ret;
3790 do {
3791 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3792 if (ret >= 0)
3793 break;
3794 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3795 } while (exception.retry);
3796 return ret;
3797}
3798
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003799static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3800{
3801 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003802 int ret;
3803
3804 if (!nfs4_server_supports_acls(server))
3805 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003806 ret = nfs_revalidate_inode(server, inode);
3807 if (ret < 0)
3808 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003809 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3810 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003811 ret = nfs4_read_cached_acl(inode, buf, buflen);
3812 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003813 /* -ENOENT is returned if there is no ACL or if there is an ACL
3814 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003815 return ret;
3816 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003817}
3818
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003819static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003820{
3821 struct nfs_server *server = NFS_SERVER(inode);
3822 struct page *pages[NFS4ACL_MAXPAGES];
3823 struct nfs_setaclargs arg = {
3824 .fh = NFS_FH(inode),
3825 .acl_pages = pages,
3826 .acl_len = buflen,
3827 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003828 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003829 struct rpc_message msg = {
3830 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3831 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003832 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003833 };
Neil Hormane9e3d722011-03-04 19:26:03 -05003834 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003835
3836 if (!nfs4_server_supports_acls(server))
3837 return -EOPNOTSUPP;
Neil Hormane9e3d722011-03-04 19:26:03 -05003838 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3839 if (i < 0)
3840 return i;
Trond Myklebust642ac542005-10-18 14:20:19 -07003841 nfs_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003842 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003843
3844 /*
3845 * Free each page after tx, so the only ref left is
3846 * held by the network stack
3847 */
3848 for (; i > 0; i--)
3849 put_page(pages[i-1]);
3850
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003851 /*
3852 * Acl update can result in inode attribute update.
3853 * so mark the attribute cache invalid.
3854 */
3855 spin_lock(&inode->i_lock);
3856 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3857 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003858 nfs_access_zap_cache(inode);
3859 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003860 return ret;
3861}
3862
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003863static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3864{
3865 struct nfs4_exception exception = { };
3866 int err;
3867 do {
3868 err = nfs4_handle_exception(NFS_SERVER(inode),
3869 __nfs4_proc_set_acl(inode, buf, buflen),
3870 &exception);
3871 } while (exception.retry);
3872 return err;
3873}
3874
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003876nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003878 struct nfs_client *clp = server->nfs_client;
3879
3880 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881 return 0;
3882 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05003883 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003884 case -NFS4ERR_ADMIN_REVOKED:
3885 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04003886 if (state == NULL)
3887 break;
3888 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003889 case -NFS4ERR_OPENMODE:
3890 if (state == NULL)
3891 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003892 nfs4_schedule_stateid_recovery(server, state);
3893 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003894 case -NFS4ERR_EXPIRED:
3895 if (state != NULL)
3896 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003898 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003899 nfs4_schedule_lease_recovery(clp);
3900 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003901#if defined(CONFIG_NFS_V4_1)
3902 case -NFS4ERR_BADSESSION:
3903 case -NFS4ERR_BADSLOT:
3904 case -NFS4ERR_BAD_HIGH_SLOT:
3905 case -NFS4ERR_DEADSESSION:
3906 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3907 case -NFS4ERR_SEQ_FALSE_RETRY:
3908 case -NFS4ERR_SEQ_MISORDERED:
3909 dprintk("%s ERROR %d, Reset session\n", __func__,
3910 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04003911 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04003912 task->tk_status = 0;
3913 return -EAGAIN;
3914#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003916 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003917 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003918 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3920 task->tk_status = 0;
3921 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04003922 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 case -NFS4ERR_OLD_STATEID:
3924 task->tk_status = 0;
3925 return -EAGAIN;
3926 }
3927 task->tk_status = nfs4_map_errors(task->tk_status);
3928 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003929wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003930 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003931 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3932 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3933 task->tk_status = 0;
3934 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935}
3936
Chuck Leverf0920752012-05-21 22:45:41 -04003937static void nfs4_init_boot_verifier(const struct nfs_client *clp,
3938 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05003939{
3940 __be32 verf[2];
3941
Chuck Lever2c820d92012-05-21 22:45:33 -04003942 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
3943 /* An impossible timestamp guarantees this value
3944 * will never match a generated boot time. */
3945 verf[0] = 0;
3946 verf[1] = (__be32)(NSEC_PER_SEC + 1);
3947 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04003948 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
3949 verf[0] = (__be32)nn->boot_time.tv_sec;
3950 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04003951 }
Chuck Levercd937102012-03-02 17:14:31 -05003952 memcpy(bootverf->data, verf, sizeof(bootverf->data));
3953}
3954
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003955int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3956 unsigned short port, struct rpc_cred *cred,
3957 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958{
3959 nfs4_verifier sc_verifier;
3960 struct nfs4_setclientid setclientid = {
3961 .sc_verifier = &sc_verifier,
3962 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00003963 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964 };
3965 struct rpc_message msg = {
3966 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3967 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003968 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003969 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971 int loop = 0;
3972 int status;
3973
Chuck Leverf0920752012-05-21 22:45:41 -04003974 nfs4_init_boot_verifier(clp, &sc_verifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975
3976 for(;;) {
Trond Myklebust2446ab62012-03-01 17:00:56 -05003977 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003979 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003980 clp->cl_ipaddr,
3981 rpc_peeraddr2str(clp->cl_rpcclient,
3982 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003983 rpc_peeraddr2str(clp->cl_rpcclient,
3984 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003985 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986 clp->cl_id_uniquifier);
3987 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003988 sizeof(setclientid.sc_netid),
3989 rpc_peeraddr2str(clp->cl_rpcclient,
3990 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003992 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993 clp->cl_ipaddr, port >> 8, port & 255);
Trond Myklebust2446ab62012-03-01 17:00:56 -05003994 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995
Trond Myklebust1bd714f2011-04-24 14:29:33 -04003996 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997 if (status != -NFS4ERR_CLID_INUSE)
3998 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003999 if (loop != 0) {
4000 ++clp->cl_id_uniquifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004002 }
4003 ++loop;
4004 ssleep(clp->cl_lease_time / HZ + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005 }
4006 return status;
4007}
4008
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004009int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004010 struct nfs4_setclientid_res *arg,
4011 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012{
4013 struct nfs_fsinfo fsinfo;
4014 struct rpc_message msg = {
4015 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004016 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004018 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 };
4020 unsigned long now;
4021 int status;
4022
4023 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004024 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025 if (status == 0) {
4026 spin_lock(&clp->cl_lock);
4027 clp->cl_lease_time = fsinfo.lease_time * HZ;
4028 clp->cl_last_renewal = now;
4029 spin_unlock(&clp->cl_lock);
4030 }
4031 return status;
4032}
4033
Trond Myklebustfe650402006-01-03 09:55:18 +01004034struct nfs4_delegreturndata {
4035 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004036 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004037 struct nfs_fh fh;
4038 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004039 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004040 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004041 int rpc_status;
4042};
4043
Trond Myklebustfe650402006-01-03 09:55:18 +01004044static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4045{
4046 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004047
Trond Myklebust14516c32010-07-31 14:29:06 -04004048 if (!nfs4_sequence_done(task, &data->res.seq_res))
4049 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004050
Ricardo Labiaga79708862009-12-07 09:23:21 -05004051 switch (task->tk_status) {
4052 case -NFS4ERR_STALE_STATEID:
4053 case -NFS4ERR_EXPIRED:
4054 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004055 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004056 break;
4057 default:
4058 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4059 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004060 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004061 return;
4062 }
4063 }
4064 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004065}
4066
4067static void nfs4_delegreturn_release(void *calldata)
4068{
Trond Myklebustfe650402006-01-03 09:55:18 +01004069 kfree(calldata);
4070}
4071
Andy Adamson938e1012009-04-01 09:22:28 -04004072#if defined(CONFIG_NFS_V4_1)
4073static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4074{
4075 struct nfs4_delegreturndata *d_data;
4076
4077 d_data = (struct nfs4_delegreturndata *)data;
4078
Trond Myklebust035168a2010-06-16 09:52:26 -04004079 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04004080 &d_data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004081 &d_data->res.seq_res, task))
Andy Adamson938e1012009-04-01 09:22:28 -04004082 return;
4083 rpc_call_start(task);
4084}
4085#endif /* CONFIG_NFS_V4_1 */
4086
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004087static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004088#if defined(CONFIG_NFS_V4_1)
4089 .rpc_call_prepare = nfs4_delegreturn_prepare,
4090#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004091 .rpc_call_done = nfs4_delegreturn_done,
4092 .rpc_release = nfs4_delegreturn_release,
4093};
4094
Trond Myklebuste6f81072008-01-24 18:14:34 -05004095static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004096{
4097 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004098 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004099 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004100 struct rpc_message msg = {
4101 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4102 .rpc_cred = cred,
4103 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004104 struct rpc_task_setup task_setup_data = {
4105 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004106 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004107 .callback_ops = &nfs4_delegreturn_ops,
4108 .flags = RPC_TASK_ASYNC,
4109 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004110 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004111
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004112 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004113 if (data == NULL)
4114 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004115 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004116 data->args.fhandle = &data->fh;
4117 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004118 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004119 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004120 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004121 data->res.fattr = &data->fattr;
4122 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004123 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004124 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004125 data->rpc_status = 0;
4126
Trond Myklebustc970aa82007-07-14 15:39:59 -04004127 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004128 msg.rpc_argp = &data->args;
4129 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004130 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004131 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004132 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004133 if (!issync)
4134 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004135 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004136 if (status != 0)
4137 goto out;
4138 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004139 if (status == 0)
4140 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4141 else
4142 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004143out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004144 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004145 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146}
4147
Trond Myklebuste6f81072008-01-24 18:14:34 -05004148int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149{
4150 struct nfs_server *server = NFS_SERVER(inode);
4151 struct nfs4_exception exception = { };
4152 int err;
4153 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004154 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 switch (err) {
4156 case -NFS4ERR_STALE_STATEID:
4157 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 case 0:
4159 return 0;
4160 }
4161 err = nfs4_handle_exception(server, err, &exception);
4162 } while (exception.retry);
4163 return err;
4164}
4165
4166#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4167#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4168
4169/*
4170 * sleep, with exponential backoff, and retry the LOCK operation.
4171 */
4172static unsigned long
4173nfs4_set_lock_task_retry(unsigned long timeout)
4174{
Jeff Laytond3103102011-12-01 22:44:39 +01004175 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 timeout <<= 1;
4177 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4178 return NFS4_LOCK_MAXTIMEOUT;
4179 return timeout;
4180}
4181
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4183{
4184 struct inode *inode = state->inode;
4185 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004186 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004187 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004189 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004191 struct nfs_lockt_res res = {
4192 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193 };
4194 struct rpc_message msg = {
4195 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4196 .rpc_argp = &arg,
4197 .rpc_resp = &res,
4198 .rpc_cred = state->owner->so_cred,
4199 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200 struct nfs4_lock_state *lsp;
4201 int status;
4202
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004203 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004204 status = nfs4_set_lock_state(state, request);
4205 if (status != 0)
4206 goto out;
4207 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004208 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004209 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004210 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004211 switch (status) {
4212 case 0:
4213 request->fl_type = F_UNLCK;
4214 break;
4215 case -NFS4ERR_DENIED:
4216 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004218 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004219out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 return status;
4221}
4222
4223static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4224{
4225 struct nfs4_exception exception = { };
4226 int err;
4227
4228 do {
4229 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4230 _nfs4_proc_getlk(state, cmd, request),
4231 &exception);
4232 } while (exception.retry);
4233 return err;
4234}
4235
4236static int do_vfs_lock(struct file *file, struct file_lock *fl)
4237{
4238 int res = 0;
4239 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4240 case FL_POSIX:
4241 res = posix_lock_file_wait(file, fl);
4242 break;
4243 case FL_FLOCK:
4244 res = flock_lock_file_wait(file, fl);
4245 break;
4246 default:
4247 BUG();
4248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249 return res;
4250}
4251
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004252struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004253 struct nfs_locku_args arg;
4254 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004255 struct nfs4_lock_state *lsp;
4256 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004257 struct file_lock fl;
4258 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004259 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004260};
4261
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004262static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4263 struct nfs_open_context *ctx,
4264 struct nfs4_lock_state *lsp,
4265 struct nfs_seqid *seqid)
4266{
4267 struct nfs4_unlockdata *p;
4268 struct inode *inode = lsp->ls_state->inode;
4269
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004270 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004271 if (p == NULL)
4272 return NULL;
4273 p->arg.fh = NFS_FH(inode);
4274 p->arg.fl = &p->fl;
4275 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004276 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004277 p->arg.stateid = &lsp->ls_stateid;
4278 p->lsp = lsp;
4279 atomic_inc(&lsp->ls_count);
4280 /* Ensure we don't close file until we're done freeing locks! */
4281 p->ctx = get_nfs_open_context(ctx);
4282 memcpy(&p->fl, fl, sizeof(p->fl));
4283 p->server = NFS_SERVER(inode);
4284 return p;
4285}
4286
Trond Myklebust06f814a2006-01-03 09:55:07 +01004287static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004288{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004289 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004290 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004291 nfs4_put_lock_state(calldata->lsp);
4292 put_nfs_open_context(calldata->ctx);
4293 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004294}
4295
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004296static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004297{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004298 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004299
Trond Myklebust14516c32010-07-31 14:29:06 -04004300 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4301 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004302 switch (task->tk_status) {
4303 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004304 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4305 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004306 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004307 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004308 case -NFS4ERR_BAD_STATEID:
4309 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004310 case -NFS4ERR_STALE_STATEID:
4311 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004312 break;
4313 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004314 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004315 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004316 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004317}
4318
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004319static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004320{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004321 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004323 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004324 return;
4325 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004326 /* Note: exit _without_ running nfs4_locku_done */
4327 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004328 return;
4329 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004330 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004331 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004332 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004333 &calldata->res.seq_res, task))
Andy Adamsona8936932009-04-01 09:22:23 -04004334 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004335 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336}
4337
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004338static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004339 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004340 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004341 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004342};
4343
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004344static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4345 struct nfs_open_context *ctx,
4346 struct nfs4_lock_state *lsp,
4347 struct nfs_seqid *seqid)
4348{
4349 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004350 struct rpc_message msg = {
4351 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4352 .rpc_cred = ctx->cred,
4353 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004354 struct rpc_task_setup task_setup_data = {
4355 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004356 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004357 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004358 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004359 .flags = RPC_TASK_ASYNC,
4360 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004361
Frank Filz137d6ac2007-07-09 15:32:29 -07004362 /* Ensure this is an unlock - when canceling a lock, the
4363 * canceled lock is passed in, and it won't be an unlock.
4364 */
4365 fl->fl_type = F_UNLCK;
4366
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004367 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4368 if (data == NULL) {
4369 nfs_free_seqid(seqid);
4370 return ERR_PTR(-ENOMEM);
4371 }
4372
Trond Myklebust9d12b212012-01-17 22:04:25 -05004373 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004374 msg.rpc_argp = &data->arg;
4375 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004376 task_setup_data.callback_data = data;
4377 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004378}
4379
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4381{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004382 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004383 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004384 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004385 struct rpc_task *task;
4386 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004387 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388
Trond Myklebust9b073572006-06-29 16:38:34 -04004389 status = nfs4_set_lock_state(state, request);
4390 /* Unlock _before_ we do the RPC call */
4391 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004392 down_read(&nfsi->rwsem);
4393 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4394 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004395 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004396 }
4397 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004398 if (status != 0)
4399 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004400 /* Is this a delegated lock? */
4401 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004402 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004403 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004404 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004405 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004406 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004407 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004408 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004409 status = PTR_ERR(task);
4410 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004411 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004412 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004413 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004414out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004415 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004416 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417}
4418
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004419struct nfs4_lockdata {
4420 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004421 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004422 struct nfs4_lock_state *lsp;
4423 struct nfs_open_context *ctx;
4424 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004425 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004426 int rpc_status;
4427 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004428 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004429};
4430
4431static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004432 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4433 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004434{
4435 struct nfs4_lockdata *p;
4436 struct inode *inode = lsp->ls_state->inode;
4437 struct nfs_server *server = NFS_SERVER(inode);
4438
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004439 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004440 if (p == NULL)
4441 return NULL;
4442
4443 p->arg.fh = NFS_FH(inode);
4444 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004445 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004446 if (p->arg.open_seqid == NULL)
4447 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004448 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004449 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004450 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004451 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004452 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004453 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004454 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004455 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004456 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004457 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004458 atomic_inc(&lsp->ls_count);
4459 p->ctx = get_nfs_open_context(ctx);
4460 memcpy(&p->fl, fl, sizeof(p->fl));
4461 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004462out_free_seqid:
4463 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004464out_free:
4465 kfree(p);
4466 return NULL;
4467}
4468
4469static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4470{
4471 struct nfs4_lockdata *data = calldata;
4472 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473
Harvey Harrison3110ff82008-05-02 13:42:44 -07004474 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004475 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4476 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004477 /* Do we need to do an open_to_lock_owner? */
4478 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004479 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4480 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004481 data->arg.open_stateid = &state->stateid;
4482 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004483 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004484 } else
4485 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004486 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004487 if (nfs4_setup_sequence(data->server,
4488 &data->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004489 &data->res.seq_res, task))
Andy Adamson66179ef2009-04-01 09:22:22 -04004490 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004491 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004492 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004493}
4494
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004495static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4496{
4497 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4498 nfs4_lock_prepare(task, calldata);
4499}
4500
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004501static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4502{
4503 struct nfs4_lockdata *data = calldata;
4504
Harvey Harrison3110ff82008-05-02 13:42:44 -07004505 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004506
Trond Myklebust14516c32010-07-31 14:29:06 -04004507 if (!nfs4_sequence_done(task, &data->res.seq_res))
4508 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004509
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004510 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004511 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004512 if (data->rpc_status == 0)
4513 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4514 else
4515 goto out;
4516 }
4517 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004518 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004519 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Al Viro3d4ff432011-06-22 18:40:12 -04004520 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004521 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004522out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004523 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004524}
4525
4526static void nfs4_lock_release(void *calldata)
4527{
4528 struct nfs4_lockdata *data = calldata;
4529
Harvey Harrison3110ff82008-05-02 13:42:44 -07004530 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004531 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004532 if (data->cancelled != 0) {
4533 struct rpc_task *task;
4534 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4535 data->arg.lock_seqid);
4536 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004537 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004538 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004539 } else
4540 nfs_free_seqid(data->arg.lock_seqid);
4541 nfs4_put_lock_state(data->lsp);
4542 put_nfs_open_context(data->ctx);
4543 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004544 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004545}
4546
4547static const struct rpc_call_ops nfs4_lock_ops = {
4548 .rpc_call_prepare = nfs4_lock_prepare,
4549 .rpc_call_done = nfs4_lock_done,
4550 .rpc_release = nfs4_lock_release,
4551};
4552
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004553static const struct rpc_call_ops nfs4_recover_lock_ops = {
4554 .rpc_call_prepare = nfs4_recover_lock_prepare,
4555 .rpc_call_done = nfs4_lock_done,
4556 .rpc_release = nfs4_lock_release,
4557};
4558
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004559static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4560{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004561 switch (error) {
4562 case -NFS4ERR_ADMIN_REVOKED:
4563 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004564 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004565 if (new_lock_owner != 0 ||
4566 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004567 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004568 break;
4569 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004570 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004571 case -NFS4ERR_EXPIRED:
4572 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004573 };
4574}
4575
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004576static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004577{
4578 struct nfs4_lockdata *data;
4579 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004580 struct rpc_message msg = {
4581 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4582 .rpc_cred = state->owner->so_cred,
4583 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004584 struct rpc_task_setup task_setup_data = {
4585 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004586 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004587 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004588 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004589 .flags = RPC_TASK_ASYNC,
4590 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004591 int ret;
4592
Harvey Harrison3110ff82008-05-02 13:42:44 -07004593 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004594 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004595 fl->fl_u.nfs4_fl.owner,
4596 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004597 if (data == NULL)
4598 return -ENOMEM;
4599 if (IS_SETLKW(cmd))
4600 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004601 if (recovery_type > NFS_LOCK_NEW) {
4602 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004603 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004604 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4605 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05004606 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004607 msg.rpc_argp = &data->arg;
4608 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004609 task_setup_data.callback_data = data;
4610 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004611 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004612 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004613 ret = nfs4_wait_for_completion_rpc_task(task);
4614 if (ret == 0) {
4615 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004616 if (ret)
4617 nfs4_handle_setlk_error(data->server, data->lsp,
4618 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004619 } else
4620 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004621 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004622 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004623 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624}
4625
4626static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4627{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004628 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004629 struct nfs4_exception exception = {
4630 .inode = state->inode,
4631 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004632 int err;
4633
4634 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004635 /* Cache the lock if possible... */
4636 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4637 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004638 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004639 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004640 break;
4641 nfs4_handle_exception(server, err, &exception);
4642 } while (exception.retry);
4643 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644}
4645
4646static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4647{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004648 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004649 struct nfs4_exception exception = {
4650 .inode = state->inode,
4651 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004652 int err;
4653
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004654 err = nfs4_set_lock_state(state, request);
4655 if (err != 0)
4656 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004657 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004658 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4659 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004660 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004661 switch (err) {
4662 default:
4663 goto out;
4664 case -NFS4ERR_GRACE:
4665 case -NFS4ERR_DELAY:
4666 nfs4_handle_exception(server, err, &exception);
4667 err = 0;
4668 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004669 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004670out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004671 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672}
4673
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004674#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004675static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004676{
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004677 int status, ret = NFS_OK;
4678 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004679 struct nfs_server *server = NFS_SERVER(state->inode);
4680
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004681 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
4682 if (lsp->ls_flags & NFS_LOCK_INITIALIZED) {
4683 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4684 if (status != NFS_OK) {
4685 nfs41_free_stateid(server, &lsp->ls_stateid);
4686 lsp->ls_flags &= ~NFS_LOCK_INITIALIZED;
4687 ret = status;
4688 }
4689 }
4690 };
4691
4692 return ret;
4693}
4694
4695static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4696{
4697 int status = NFS_OK;
4698
4699 if (test_bit(LK_STATE_IN_USE, &state->flags))
4700 status = nfs41_check_expired_locks(state);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004701 if (status == NFS_OK)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004702 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004703 return nfs4_lock_expired(state, request);
4704}
4705#endif
4706
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4708{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004709 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004710 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004711 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004713 if ((fl_flags & FL_POSIX) &&
4714 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4715 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004716 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004717 status = nfs4_set_lock_state(state, request);
4718 if (status != 0)
4719 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004720 request->fl_flags |= FL_ACCESS;
4721 status = do_vfs_lock(request->fl_file, request);
4722 if (status < 0)
4723 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004724 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004725 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004726 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004727 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004728 request->fl_flags = fl_flags & ~FL_SLEEP;
4729 status = do_vfs_lock(request->fl_file, request);
4730 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004731 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004732 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004733 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004734 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004735 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004736 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004737 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004738 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4739 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004740out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004741 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004742out:
4743 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744 return status;
4745}
4746
4747static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4748{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004749 struct nfs4_exception exception = {
4750 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04004751 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004752 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753 int err;
4754
4755 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004756 err = _nfs4_proc_setlk(state, cmd, request);
4757 if (err == -NFS4ERR_DENIED)
4758 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004760 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761 } while (exception.retry);
4762 return err;
4763}
4764
4765static int
4766nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4767{
4768 struct nfs_open_context *ctx;
4769 struct nfs4_state *state;
4770 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4771 int status;
4772
4773 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004774 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775 state = ctx->state;
4776
4777 if (request->fl_start < 0 || request->fl_end < 0)
4778 return -EINVAL;
4779
Trond Myklebustd9531262009-07-21 19:22:38 -04004780 if (IS_GETLK(cmd)) {
4781 if (state != NULL)
4782 return nfs4_proc_getlk(state, F_GETLK, request);
4783 return 0;
4784 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785
4786 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4787 return -EINVAL;
4788
Trond Myklebustd9531262009-07-21 19:22:38 -04004789 if (request->fl_type == F_UNLCK) {
4790 if (state != NULL)
4791 return nfs4_proc_unlck(state, cmd, request);
4792 return 0;
4793 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794
Trond Myklebustd9531262009-07-21 19:22:38 -04004795 if (state == NULL)
4796 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04004797 /*
4798 * Don't rely on the VFS having checked the file open mode,
4799 * since it won't do this for flock() locks.
4800 */
4801 switch (request->fl_type & (F_RDLCK|F_WRLCK|F_UNLCK)) {
4802 case F_RDLCK:
4803 if (!(filp->f_mode & FMODE_READ))
4804 return -EBADF;
4805 break;
4806 case F_WRLCK:
4807 if (!(filp->f_mode & FMODE_WRITE))
4808 return -EBADF;
4809 }
4810
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811 do {
4812 status = nfs4_proc_setlk(state, cmd, request);
4813 if ((status != -EAGAIN) || IS_SETLK(cmd))
4814 break;
4815 timeout = nfs4_set_lock_task_retry(timeout);
4816 status = -ERESTARTSYS;
4817 if (signalled())
4818 break;
4819 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820 return status;
4821}
4822
Trond Myklebust888e6942005-11-04 15:38:11 -05004823int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4824{
4825 struct nfs_server *server = NFS_SERVER(state->inode);
4826 struct nfs4_exception exception = { };
4827 int err;
4828
4829 err = nfs4_set_lock_state(state, fl);
4830 if (err != 0)
4831 goto out;
4832 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004833 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004834 switch (err) {
4835 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004836 printk(KERN_ERR "NFS: %s: unhandled error "
4837 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004838 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004839 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004840 goto out;
4841 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004842 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07004843 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004844 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004845 nfs4_schedule_lease_recovery(server->nfs_client);
4846 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004847 case -NFS4ERR_BADSESSION:
4848 case -NFS4ERR_BADSLOT:
4849 case -NFS4ERR_BAD_HIGH_SLOT:
4850 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4851 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04004852 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004853 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004854 case -ERESTARTSYS:
4855 /*
4856 * The show must go on: exit, but mark the
4857 * stateid as needing recovery.
4858 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004859 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004860 case -NFS4ERR_ADMIN_REVOKED:
4861 case -NFS4ERR_BAD_STATEID:
4862 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004863 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004864 err = 0;
4865 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004866 case -EKEYEXPIRED:
4867 /*
4868 * User RPCSEC_GSS context has expired.
4869 * We cannot recover this stateid now, so
4870 * skip it and allow recovery thread to
4871 * proceed.
4872 */
4873 err = 0;
4874 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004875 case -ENOMEM:
4876 case -NFS4ERR_DENIED:
4877 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4878 err = 0;
4879 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004880 case -NFS4ERR_DELAY:
4881 break;
4882 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004883 err = nfs4_handle_exception(server, err, &exception);
4884 } while (exception.retry);
4885out:
4886 return err;
4887}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004888
Trond Myklebustcf470c32012-03-07 13:49:12 -05004889struct nfs_release_lockowner_data {
4890 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004891 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004892 struct nfs_release_lockowner_args args;
4893};
4894
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004895static void nfs4_release_lockowner_release(void *calldata)
4896{
Trond Myklebustcf470c32012-03-07 13:49:12 -05004897 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004898 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004899 kfree(calldata);
4900}
4901
Trond Myklebust17280172012-03-11 13:11:00 -04004902static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004903 .rpc_release = nfs4_release_lockowner_release,
4904};
4905
Trond Myklebustcf470c32012-03-07 13:49:12 -05004906int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004907{
4908 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004909 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004910 struct rpc_message msg = {
4911 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4912 };
4913
4914 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05004915 return -EINVAL;
4916 data = kmalloc(sizeof(*data), GFP_NOFS);
4917 if (!data)
4918 return -ENOMEM;
4919 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004920 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004921 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
4922 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
4923 data->args.lock_owner.s_dev = server->s_dev;
4924 msg.rpc_argp = &data->args;
4925 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
4926 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004927}
4928
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004929#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4930
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004931static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4932 const void *buf, size_t buflen,
4933 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004934{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004935 if (strcmp(key, "") != 0)
4936 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004937
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004938 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004939}
4940
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004941static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4942 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004943{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004944 if (strcmp(key, "") != 0)
4945 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004946
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004947 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004948}
4949
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004950static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4951 size_t list_len, const char *name,
4952 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004953{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004954 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004955
J. Bruce Fields096455a2006-03-20 23:23:42 -05004956 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4957 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004958
4959 if (list && len <= list_len)
4960 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004961 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004962}
4963
Andy Adamson533eb462011-06-13 18:25:56 -04004964/*
4965 * nfs_fhget will use either the mounted_on_fileid or the fileid
4966 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004967static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4968{
Andy Adamson533eb462011-06-13 18:25:56 -04004969 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
4970 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
4971 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05004972 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004973 return;
4974
4975 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05004976 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004977 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4978 fattr->nlink = 2;
4979}
4980
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004981static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
4982 const struct qstr *name,
4983 struct nfs4_fs_locations *fs_locations,
4984 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004985{
4986 struct nfs_server *server = NFS_SERVER(dir);
4987 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004988 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004989 };
4990 struct nfs4_fs_locations_arg args = {
4991 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004992 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004993 .page = page,
4994 .bitmask = bitmask,
4995 };
Benny Halevy22958462009-04-01 09:22:02 -04004996 struct nfs4_fs_locations_res res = {
4997 .fs_locations = fs_locations,
4998 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004999 struct rpc_message msg = {
5000 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5001 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005002 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005003 };
5004 int status;
5005
Harvey Harrison3110ff82008-05-02 13:42:44 -07005006 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005007
5008 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5009 * is not supported */
5010 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5011 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5012 else
5013 bitmask[0] |= FATTR4_WORD0_FILEID;
5014
Trond Myklebustc228fd32007-01-13 02:28:11 -05005015 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005016 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005017 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005018 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005019 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005020 return status;
5021}
5022
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005023int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5024 const struct qstr *name,
5025 struct nfs4_fs_locations *fs_locations,
5026 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005027{
5028 struct nfs4_exception exception = { };
5029 int err;
5030 do {
5031 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005032 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005033 &exception);
5034 } while (exception.retry);
5035 return err;
5036}
5037
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005038static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5039{
5040 int status;
5041 struct nfs4_secinfo_arg args = {
5042 .dir_fh = NFS_FH(dir),
5043 .name = name,
5044 };
5045 struct nfs4_secinfo_res res = {
5046 .flavors = flavors,
5047 };
5048 struct rpc_message msg = {
5049 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5050 .rpc_argp = &args,
5051 .rpc_resp = &res,
5052 };
5053
5054 dprintk("NFS call secinfo %s\n", name->name);
5055 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5056 dprintk("NFS reply secinfo: %d\n", status);
5057 return status;
5058}
5059
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005060int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5061 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005062{
5063 struct nfs4_exception exception = { };
5064 int err;
5065 do {
5066 err = nfs4_handle_exception(NFS_SERVER(dir),
5067 _nfs4_proc_secinfo(dir, name, flavors),
5068 &exception);
5069 } while (exception.retry);
5070 return err;
5071}
5072
Andy Adamson557134a2009-04-01 09:21:53 -04005073#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005074/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005075 * Check the exchange flags returned by the server for invalid flags, having
5076 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5077 * DS flags set.
5078 */
5079static int nfs4_check_cl_exchange_flags(u32 flags)
5080{
5081 if (flags & ~EXCHGID4_FLAG_MASK_R)
5082 goto out_inval;
5083 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5084 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5085 goto out_inval;
5086 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5087 goto out_inval;
5088 return NFS_OK;
5089out_inval:
5090 return -NFS4ERR_INVAL;
5091}
5092
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005093static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005094nfs41_same_server_scope(struct nfs41_server_scope *a,
5095 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005096{
5097 if (a->server_scope_sz == b->server_scope_sz &&
5098 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5099 return true;
5100
5101 return false;
5102}
5103
Andy Adamson357f54d2010-12-14 10:11:57 -05005104/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005105 * nfs4_proc_bind_conn_to_session()
5106 *
5107 * The 4.1 client currently uses the same TCP connection for the
5108 * fore and backchannel.
5109 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005110int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005111{
5112 int status;
5113 struct nfs41_bind_conn_to_session_res res;
5114 struct rpc_message msg = {
5115 .rpc_proc =
5116 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5117 .rpc_argp = clp,
5118 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005119 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005120 };
5121
5122 dprintk("--> %s\n", __func__);
5123 BUG_ON(clp == NULL);
5124
5125 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5126 if (unlikely(res.session == NULL)) {
5127 status = -ENOMEM;
5128 goto out;
5129 }
5130
5131 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5132 if (status == 0) {
5133 if (memcmp(res.session->sess_id.data,
5134 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5135 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5136 status = -EIO;
5137 goto out_session;
5138 }
5139 if (res.dir != NFS4_CDFS4_BOTH) {
5140 dprintk("NFS: %s: Unexpected direction from server\n",
5141 __func__);
5142 status = -EIO;
5143 goto out_session;
5144 }
5145 if (res.use_conn_in_rdma_mode) {
5146 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5147 __func__);
5148 status = -EIO;
5149 goto out_session;
5150 }
5151 }
5152out_session:
5153 kfree(res.session);
5154out:
5155 dprintk("<-- %s status= %d\n", __func__, status);
5156 return status;
5157}
5158
5159/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005160 * nfs4_proc_exchange_id()
5161 *
5162 * Since the clientid has expired, all compounds using sessions
5163 * associated with the stale clientid will be returning
5164 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5165 * be in some phase of session reset.
5166 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005167int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005168{
5169 nfs4_verifier verifier;
5170 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005171 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005172 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005173 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005174 };
5175 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005176 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005177 };
5178 int status;
5179 struct rpc_message msg = {
5180 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5181 .rpc_argp = &args,
5182 .rpc_resp = &res,
5183 .rpc_cred = cred,
5184 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005185
5186 dprintk("--> %s\n", __func__);
5187 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04005188
Chuck Leverf0920752012-05-21 22:45:41 -04005189 nfs4_init_boot_verifier(clp, &verifier);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005190
Andy Adamsonc7a360b2011-01-25 19:15:32 -05005191 args.id_len = scnprintf(args.id, sizeof(args.id),
Trond Myklebust3617e502012-04-30 12:04:58 -04005192 "%s/%s/%u",
Andy Adamsonc7a360b2011-01-25 19:15:32 -05005193 clp->cl_ipaddr,
Trond Myklebust3617e502012-04-30 12:04:58 -04005194 clp->cl_rpcclient->cl_nodename,
Andy Adamsonc7a360b2011-01-25 19:15:32 -05005195 clp->cl_rpcclient->cl_auth->au_flavor);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005196
Chuck Leveracdeb692012-05-21 22:46:16 -04005197 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005198 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005199 if (unlikely(res.server_owner == NULL)) {
5200 status = -ENOMEM;
5201 goto out;
5202 }
5203
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005204 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005205 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005206 if (unlikely(res.server_scope == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005207 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005208 goto out_server_owner;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005209 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005210
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005211 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005212 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005213 status = -ENOMEM;
5214 goto out_server_scope;
5215 }
5216
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005217 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005218 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005219 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005220
Chuck Lever177313f2012-05-21 22:44:58 -04005221 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005222 clp->cl_clientid = res.clientid;
5223 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5224 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5225 clp->cl_seqid = res.seqid;
5226
Chuck Leveracdeb692012-05-21 22:46:16 -04005227 kfree(clp->cl_serverowner);
5228 clp->cl_serverowner = res.server_owner;
5229 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005230
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005231 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005232 kfree(clp->cl_implid);
5233 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005234
Chuck Lever177313f2012-05-21 22:44:58 -04005235 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005236 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005237 res.server_scope)) {
5238 dprintk("%s: server_scope mismatch detected\n",
5239 __func__);
5240 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005241 kfree(clp->cl_serverscope);
5242 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005243 }
5244
Chuck Lever177313f2012-05-21 22:44:58 -04005245 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005246 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005247 goto out;
5248 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005249 } else
5250 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005251
Chuck Leveracdeb692012-05-21 22:46:16 -04005252out_server_owner:
5253 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005254out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005255 kfree(res.server_scope);
5256out:
Chuck Lever177313f2012-05-21 22:44:58 -04005257 if (clp->cl_implid != NULL)
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005258 dprintk("%s: Server Implementation ID: "
5259 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever59155542012-05-21 22:44:41 -04005260 __func__, clp->cl_implid->domain, clp->cl_implid->name,
5261 clp->cl_implid->date.seconds,
5262 clp->cl_implid->date.nseconds);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005263 dprintk("<-- %s status= %d\n", __func__, status);
5264 return status;
5265}
5266
Trond Myklebust66245532012-05-25 17:18:09 -04005267static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5268 struct rpc_cred *cred)
5269{
5270 struct rpc_message msg = {
5271 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5272 .rpc_argp = clp,
5273 .rpc_cred = cred,
5274 };
5275 int status;
5276
5277 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5278 if (status)
5279 pr_warn("NFS: Got error %d from the server %s on "
5280 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5281 return status;
5282}
5283
5284static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5285 struct rpc_cred *cred)
5286{
5287 unsigned int loop;
5288 int ret;
5289
5290 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5291 ret = _nfs4_proc_destroy_clientid(clp, cred);
5292 switch (ret) {
5293 case -NFS4ERR_DELAY:
5294 case -NFS4ERR_CLIENTID_BUSY:
5295 ssleep(1);
5296 break;
5297 default:
5298 return ret;
5299 }
5300 }
5301 return 0;
5302}
5303
5304int nfs4_destroy_clientid(struct nfs_client *clp)
5305{
5306 struct rpc_cred *cred;
5307 int ret = 0;
5308
5309 if (clp->cl_mvops->minor_version < 1)
5310 goto out;
5311 if (clp->cl_exchange_flags == 0)
5312 goto out;
5313 cred = nfs4_get_exchange_id_cred(clp);
5314 ret = nfs4_proc_destroy_clientid(clp, cred);
5315 if (cred)
5316 put_rpccred(cred);
5317 switch (ret) {
5318 case 0:
5319 case -NFS4ERR_STALE_CLIENTID:
5320 clp->cl_exchange_flags = 0;
5321 }
5322out:
5323 return ret;
5324}
5325
Andy Adamson2050f0c2009-04-01 09:22:30 -04005326struct nfs4_get_lease_time_data {
5327 struct nfs4_get_lease_time_args *args;
5328 struct nfs4_get_lease_time_res *res;
5329 struct nfs_client *clp;
5330};
5331
5332static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5333 void *calldata)
5334{
5335 int ret;
5336 struct nfs4_get_lease_time_data *data =
5337 (struct nfs4_get_lease_time_data *)calldata;
5338
5339 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005340 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005341 /* just setup sequence, do not trigger session recovery
5342 since we're invoked within one */
5343 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005344 &data->args->la_seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005345 &data->res->lr_seq_res, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005346
5347 BUG_ON(ret == -EAGAIN);
5348 rpc_call_start(task);
5349 dprintk("<-- %s\n", __func__);
5350}
5351
5352/*
5353 * Called from nfs4_state_manager thread for session setup, so don't recover
5354 * from sequence operation or clientid errors.
5355 */
5356static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5357{
5358 struct nfs4_get_lease_time_data *data =
5359 (struct nfs4_get_lease_time_data *)calldata;
5360
5361 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005362 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5363 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005364 switch (task->tk_status) {
5365 case -NFS4ERR_DELAY:
5366 case -NFS4ERR_GRACE:
5367 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5368 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5369 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005370 /* fall through */
5371 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005372 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005373 return;
5374 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005375 dprintk("<-- %s\n", __func__);
5376}
5377
Trond Myklebust17280172012-03-11 13:11:00 -04005378static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005379 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5380 .rpc_call_done = nfs4_get_lease_time_done,
5381};
5382
5383int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5384{
5385 struct rpc_task *task;
5386 struct nfs4_get_lease_time_args args;
5387 struct nfs4_get_lease_time_res res = {
5388 .lr_fsinfo = fsinfo,
5389 };
5390 struct nfs4_get_lease_time_data data = {
5391 .args = &args,
5392 .res = &res,
5393 .clp = clp,
5394 };
5395 struct rpc_message msg = {
5396 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5397 .rpc_argp = &args,
5398 .rpc_resp = &res,
5399 };
5400 struct rpc_task_setup task_setup = {
5401 .rpc_client = clp->cl_rpcclient,
5402 .rpc_message = &msg,
5403 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005404 .callback_data = &data,
5405 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005406 };
5407 int status;
5408
Trond Myklebust9d12b212012-01-17 22:04:25 -05005409 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005410 dprintk("--> %s\n", __func__);
5411 task = rpc_run_task(&task_setup);
5412
5413 if (IS_ERR(task))
5414 status = PTR_ERR(task);
5415 else {
5416 status = task->tk_status;
5417 rpc_put_task(task);
5418 }
5419 dprintk("<-- %s return %d\n", __func__, status);
5420
5421 return status;
5422}
5423
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005424static struct nfs4_slot *nfs4_alloc_slots(u32 max_slots, gfp_t gfp_flags)
5425{
5426 return kcalloc(max_slots, sizeof(struct nfs4_slot), gfp_flags);
5427}
5428
5429static void nfs4_add_and_init_slots(struct nfs4_slot_table *tbl,
5430 struct nfs4_slot *new,
5431 u32 max_slots,
5432 u32 ivalue)
5433{
5434 struct nfs4_slot *old = NULL;
5435 u32 i;
5436
5437 spin_lock(&tbl->slot_tbl_lock);
5438 if (new) {
5439 old = tbl->slots;
5440 tbl->slots = new;
5441 tbl->max_slots = max_slots;
5442 }
5443 tbl->highest_used_slotid = -1; /* no slot is currently used */
5444 for (i = 0; i < tbl->max_slots; i++)
5445 tbl->slots[i].seq_nr = ivalue;
5446 spin_unlock(&tbl->slot_tbl_lock);
5447 kfree(old);
5448}
5449
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005450/*
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005451 * (re)Initialise a slot table
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005452 */
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005453static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
5454 u32 ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005455{
Andy Adamson104aeba2010-01-14 17:45:10 -05005456 struct nfs4_slot *new = NULL;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005457 int ret = -ENOMEM;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005458
Andy Adamson104aeba2010-01-14 17:45:10 -05005459 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5460 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005461
Andy Adamson104aeba2010-01-14 17:45:10 -05005462 /* Does the newly negotiated max_reqs match the existing slot table? */
5463 if (max_reqs != tbl->max_slots) {
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005464 new = nfs4_alloc_slots(max_reqs, GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05005465 if (!new)
5466 goto out;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005467 }
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005468 ret = 0;
5469
5470 nfs4_add_and_init_slots(tbl, new, max_reqs, ivalue);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005471 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5472 tbl, tbl->slots, tbl->max_slots);
5473out:
5474 dprintk("<-- %s: return %d\n", __func__, ret);
5475 return ret;
5476}
5477
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005478/* Destroy the slot table */
5479static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5480{
5481 if (session->fc_slot_table.slots != NULL) {
5482 kfree(session->fc_slot_table.slots);
5483 session->fc_slot_table.slots = NULL;
5484 }
5485 if (session->bc_slot_table.slots != NULL) {
5486 kfree(session->bc_slot_table.slots);
5487 session->bc_slot_table.slots = NULL;
5488 }
5489 return;
5490}
5491
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005492/*
Andy Adamsonaacd5532011-11-09 13:58:21 -05005493 * Initialize or reset the forechannel and backchannel tables
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005494 */
Andy Adamsonaacd5532011-11-09 13:58:21 -05005495static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005496{
Trond Myklebustf26468f2009-12-05 19:32:11 -05005497 struct nfs4_slot_table *tbl;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005498 int status;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005499
Andy Adamsonaacd5532011-11-09 13:58:21 -05005500 dprintk("--> %s\n", __func__);
5501 /* Fore channel */
5502 tbl = &ses->fc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005503 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5504 if (status) /* -ENOMEM */
5505 return status;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005506 /* Back channel */
5507 tbl = &ses->bc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005508 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
5509 if (status && tbl->slots == NULL)
5510 /* Fore and back channel share a connection so get
5511 * both slot tables or neither */
5512 nfs4_destroy_slot_tables(ses);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005513 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04005514}
5515
5516struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5517{
5518 struct nfs4_session *session;
5519 struct nfs4_slot_table *tbl;
5520
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005521 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04005522 if (!session)
5523 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04005524
Andy Adamson557134a2009-04-01 09:21:53 -04005525 tbl = &session->fc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005526 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamson557134a2009-04-01 09:21:53 -04005527 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005528 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005529 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005530
5531 tbl = &session->bc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005532 tbl->highest_used_slotid = NFS4_NO_SLOT;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005533 spin_lock_init(&tbl->slot_tbl_lock);
5534 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005535 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005536
Trond Myklebust1055d762010-06-16 09:52:27 -04005537 session->session_state = 1<<NFS4_SESSION_INITING;
5538
Andy Adamson557134a2009-04-01 09:21:53 -04005539 session->clp = clp;
5540 return session;
5541}
5542
5543void nfs4_destroy_session(struct nfs4_session *session)
5544{
Trond Myklebust2446ab62012-03-01 17:00:56 -05005545 struct rpc_xprt *xprt;
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005546 struct rpc_cred *cred;
Trond Myklebust2446ab62012-03-01 17:00:56 -05005547
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005548 cred = nfs4_get_exchange_id_cred(session->clp);
5549 nfs4_proc_destroy_session(session, cred);
5550 if (cred)
5551 put_rpccred(cred);
Trond Myklebust2446ab62012-03-01 17:00:56 -05005552
5553 rcu_read_lock();
5554 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
5555 rcu_read_unlock();
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005556 dprintk("%s Destroy backchannel for xprt %p\n",
Trond Myklebust2446ab62012-03-01 17:00:56 -05005557 __func__, xprt);
5558 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005559 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04005560 kfree(session);
5561}
5562
Andy Adamsonfc931582009-04-01 09:22:31 -04005563/*
5564 * Initialize the values to be used by the client in CREATE_SESSION
5565 * If nfs4_init_session set the fore channel request and response sizes,
5566 * use them.
5567 *
5568 * Set the back channel max_resp_sz_cached to zero to force the client to
5569 * always set csa_cachethis to FALSE because the current implementation
5570 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5571 */
5572static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5573{
5574 struct nfs4_session *session = args->client->cl_session;
5575 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5576 mxresp_sz = session->fc_attrs.max_resp_sz;
5577
5578 if (mxrqst_sz == 0)
5579 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5580 if (mxresp_sz == 0)
5581 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5582 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005583 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5584 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005585 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005586 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005587
5588 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005589 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005590 __func__,
5591 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005592 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005593
5594 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005595 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5596 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5597 args->bc_attrs.max_resp_sz_cached = 0;
5598 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5599 args->bc_attrs.max_reqs = 1;
5600
5601 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5602 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5603 __func__,
5604 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5605 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5606 args->bc_attrs.max_reqs);
5607}
5608
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005609static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005610{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005611 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5612 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5613
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005614 if (rcvd->max_resp_sz > sent->max_resp_sz)
5615 return -EINVAL;
5616 /*
5617 * Our requested max_ops is the minimum we need; we're not
5618 * prepared to break up compounds into smaller pieces than that.
5619 * So, no point even trying to continue if the server won't
5620 * cooperate:
5621 */
5622 if (rcvd->max_ops < sent->max_ops)
5623 return -EINVAL;
5624 if (rcvd->max_reqs == 0)
5625 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005626 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5627 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005628 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005629}
5630
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005631static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5632{
5633 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5634 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005635
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005636 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5637 return -EINVAL;
5638 if (rcvd->max_resp_sz < sent->max_resp_sz)
5639 return -EINVAL;
5640 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5641 return -EINVAL;
5642 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005643 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005644 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005645 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005646 return -EINVAL;
5647 return 0;
5648}
Andy Adamson8d353012009-04-01 09:22:32 -04005649
Andy Adamson8d353012009-04-01 09:22:32 -04005650static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5651 struct nfs4_session *session)
5652{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005653 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005654
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005655 ret = nfs4_verify_fore_channel_attrs(args, session);
5656 if (ret)
5657 return ret;
5658 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005659}
5660
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005661static int _nfs4_proc_create_session(struct nfs_client *clp,
5662 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005663{
5664 struct nfs4_session *session = clp->cl_session;
5665 struct nfs41_create_session_args args = {
5666 .client = clp,
5667 .cb_program = NFS4_CALLBACK,
5668 };
5669 struct nfs41_create_session_res res = {
5670 .client = clp,
5671 };
5672 struct rpc_message msg = {
5673 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5674 .rpc_argp = &args,
5675 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005676 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04005677 };
5678 int status;
5679
5680 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005681 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005682
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005683 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005684
Andy Adamson8d353012009-04-01 09:22:32 -04005685 if (!status)
5686 /* Verify the session's negotiated channel_attrs values */
5687 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005688 if (!status) {
5689 /* Increment the clientid slot sequence id */
5690 clp->cl_seqid++;
5691 }
5692
5693 return status;
5694}
5695
5696/*
5697 * Issues a CREATE_SESSION operation to the server.
5698 * It is the responsibility of the caller to verify the session is
5699 * expired before calling this routine.
5700 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005701int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005702{
5703 int status;
5704 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005705 struct nfs4_session *session = clp->cl_session;
5706
5707 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5708
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005709 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04005710 if (status)
5711 goto out;
5712
Andy Adamsonaacd5532011-11-09 13:58:21 -05005713 /* Init or reset the session slot tables */
5714 status = nfs4_setup_session_slot_tables(session);
5715 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005716 if (status)
5717 goto out;
5718
5719 ptr = (unsigned *)&session->sess_id.data[0];
5720 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5721 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005722out:
5723 dprintk("<-- %s\n", __func__);
5724 return status;
5725}
5726
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005727/*
5728 * Issue the over-the-wire RPC DESTROY_SESSION.
5729 * The caller must serialize access to this routine.
5730 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005731int nfs4_proc_destroy_session(struct nfs4_session *session,
5732 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005733{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005734 struct rpc_message msg = {
5735 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5736 .rpc_argp = session,
5737 .rpc_cred = cred,
5738 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005739 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005740
5741 dprintk("--> nfs4_proc_destroy_session\n");
5742
5743 /* session is still being setup */
5744 if (session->clp->cl_cons_state != NFS_CS_READY)
5745 return status;
5746
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005747 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005748
5749 if (status)
5750 printk(KERN_WARNING
Weston Andros Adamsonf9fd2d92012-01-26 13:32:22 -05005751 "NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005752 "Session has been destroyed regardless...\n", status);
5753
5754 dprintk("<-- nfs4_proc_destroy_session\n");
5755 return status;
5756}
5757
Trond Myklebust7b38c362012-05-23 13:23:31 -04005758/*
5759 * With sessions, the client is not marked ready until after a
5760 * successful EXCHANGE_ID and CREATE_SESSION.
5761 *
5762 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
5763 * other versions of NFS can be tried.
5764 */
5765static int nfs41_check_session_ready(struct nfs_client *clp)
5766{
5767 int ret;
5768
5769 if (clp->cl_cons_state == NFS_CS_SESSION_INITING) {
5770 ret = nfs4_client_recover_expired_lease(clp);
5771 if (ret)
5772 return ret;
5773 }
5774 if (clp->cl_cons_state < NFS_CS_READY)
5775 return -EPROTONOSUPPORT;
Trond Myklebust54ac4712012-05-23 13:26:10 -04005776 smp_rmb();
Trond Myklebust7b38c362012-05-23 13:23:31 -04005777 return 0;
5778}
5779
Trond Myklebustfccba802009-07-21 16:48:07 -04005780int nfs4_init_session(struct nfs_server *server)
5781{
5782 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005783 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005784 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005785
5786 if (!nfs4_has_session(clp))
5787 return 0;
5788
Trond Myklebust1055d762010-06-16 09:52:27 -04005789 session = clp->cl_session;
Trond Myklebust7b38c362012-05-23 13:23:31 -04005790 spin_lock(&clp->cl_lock);
5791 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
Trond Myklebust1055d762010-06-16 09:52:27 -04005792
Trond Myklebust7b38c362012-05-23 13:23:31 -04005793 rsize = server->rsize;
5794 if (rsize == 0)
5795 rsize = NFS_MAX_FILE_IO_SIZE;
5796 wsize = server->wsize;
5797 if (wsize == 0)
5798 wsize = NFS_MAX_FILE_IO_SIZE;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005799
Trond Myklebust7b38c362012-05-23 13:23:31 -04005800 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5801 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
5802 }
5803 spin_unlock(&clp->cl_lock);
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005804
Trond Myklebust7b38c362012-05-23 13:23:31 -04005805 return nfs41_check_session_ready(clp);
Trond Myklebustfccba802009-07-21 16:48:07 -04005806}
5807
Trond Myklebust7b38c362012-05-23 13:23:31 -04005808int nfs4_init_ds_session(struct nfs_client *clp, unsigned long lease_time)
Andy Adamsond83217c2011-03-01 01:34:17 +00005809{
5810 struct nfs4_session *session = clp->cl_session;
5811 int ret;
5812
Trond Myklebust7b38c362012-05-23 13:23:31 -04005813 spin_lock(&clp->cl_lock);
5814 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
5815 /*
5816 * Do not set NFS_CS_CHECK_LEASE_TIME instead set the
5817 * DS lease to be equal to the MDS lease.
5818 */
5819 clp->cl_lease_time = lease_time;
5820 clp->cl_last_renewal = jiffies;
5821 }
5822 spin_unlock(&clp->cl_lock);
Andy Adamsond83217c2011-03-01 01:34:17 +00005823
Trond Myklebust7b38c362012-05-23 13:23:31 -04005824 ret = nfs41_check_session_ready(clp);
5825 if (ret)
5826 return ret;
5827 /* Test for the DS role */
5828 if (!is_ds_client(clp))
5829 return -ENODEV;
5830 return 0;
Andy Adamsond83217c2011-03-01 01:34:17 +00005831}
5832EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5833
5834
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005835/*
5836 * Renew the cl_session lease.
5837 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005838struct nfs4_sequence_data {
5839 struct nfs_client *clp;
5840 struct nfs4_sequence_args args;
5841 struct nfs4_sequence_res res;
5842};
5843
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005844static void nfs41_sequence_release(void *data)
5845{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005846 struct nfs4_sequence_data *calldata = data;
5847 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005848
Alexandros Batsakis71358402010-02-05 03:45:05 -08005849 if (atomic_read(&clp->cl_count) > 1)
5850 nfs4_schedule_state_renewal(clp);
5851 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005852 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005853}
5854
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005855static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5856{
5857 switch(task->tk_status) {
5858 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005859 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5860 return -EAGAIN;
5861 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005862 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005863 }
5864 return 0;
5865}
5866
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005867static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005868{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005869 struct nfs4_sequence_data *calldata = data;
5870 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005871
Trond Myklebust14516c32010-07-31 14:29:06 -04005872 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5873 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005874
5875 if (task->tk_status < 0) {
5876 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005877 if (atomic_read(&clp->cl_count) == 1)
5878 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005879
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005880 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5881 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005882 return;
5883 }
5884 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005885 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005886out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005887 dprintk("<-- %s\n", __func__);
5888}
5889
5890static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5891{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005892 struct nfs4_sequence_data *calldata = data;
5893 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005894 struct nfs4_sequence_args *args;
5895 struct nfs4_sequence_res *res;
5896
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005897 args = task->tk_msg.rpc_argp;
5898 res = task->tk_msg.rpc_resp;
5899
Trond Myklebust9d12b212012-01-17 22:04:25 -05005900 if (nfs41_setup_sequence(clp->cl_session, args, res, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005901 return;
5902 rpc_call_start(task);
5903}
5904
5905static const struct rpc_call_ops nfs41_sequence_ops = {
5906 .rpc_call_done = nfs41_sequence_call_done,
5907 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005908 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005909};
5910
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005911static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005912{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005913 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005914 struct rpc_message msg = {
5915 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5916 .rpc_cred = cred,
5917 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005918 struct rpc_task_setup task_setup_data = {
5919 .rpc_client = clp->cl_rpcclient,
5920 .rpc_message = &msg,
5921 .callback_ops = &nfs41_sequence_ops,
5922 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5923 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005924
Alexandros Batsakis71358402010-02-05 03:45:05 -08005925 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005926 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005927 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005928 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005929 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005930 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005931 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05005932 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005933 msg.rpc_argp = &calldata->args;
5934 msg.rpc_resp = &calldata->res;
5935 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005936 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005937
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005938 return rpc_run_task(&task_setup_data);
5939}
5940
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005941static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005942{
5943 struct rpc_task *task;
5944 int ret = 0;
5945
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005946 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5947 return 0;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005948 task = _nfs41_proc_sequence(clp, cred);
5949 if (IS_ERR(task))
5950 ret = PTR_ERR(task);
5951 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005952 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005953 dprintk("<-- %s status=%d\n", __func__, ret);
5954 return ret;
5955}
5956
5957static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5958{
5959 struct rpc_task *task;
5960 int ret;
5961
5962 task = _nfs41_proc_sequence(clp, cred);
5963 if (IS_ERR(task)) {
5964 ret = PTR_ERR(task);
5965 goto out;
5966 }
5967 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005968 if (!ret) {
5969 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5970
5971 if (task->tk_status == 0)
5972 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005973 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005974 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005975 rpc_put_task(task);
5976out:
5977 dprintk("<-- %s status=%d\n", __func__, ret);
5978 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005979}
5980
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005981struct nfs4_reclaim_complete_data {
5982 struct nfs_client *clp;
5983 struct nfs41_reclaim_complete_args arg;
5984 struct nfs41_reclaim_complete_res res;
5985};
5986
5987static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5988{
5989 struct nfs4_reclaim_complete_data *calldata = data;
5990
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005991 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04005992 if (nfs41_setup_sequence(calldata->clp->cl_session,
5993 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005994 &calldata->res.seq_res, task))
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005995 return;
5996
5997 rpc_call_start(task);
5998}
5999
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006000static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6001{
6002 switch(task->tk_status) {
6003 case 0:
6004 case -NFS4ERR_COMPLETE_ALREADY:
6005 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6006 break;
6007 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006008 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006009 /* fall through */
6010 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006011 return -EAGAIN;
6012 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006013 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006014 }
6015 return 0;
6016}
6017
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006018static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6019{
6020 struct nfs4_reclaim_complete_data *calldata = data;
6021 struct nfs_client *clp = calldata->clp;
6022 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6023
6024 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006025 if (!nfs41_sequence_done(task, res))
6026 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006027
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006028 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6029 rpc_restart_call_prepare(task);
6030 return;
6031 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006032 dprintk("<-- %s\n", __func__);
6033}
6034
6035static void nfs4_free_reclaim_complete_data(void *data)
6036{
6037 struct nfs4_reclaim_complete_data *calldata = data;
6038
6039 kfree(calldata);
6040}
6041
6042static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6043 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6044 .rpc_call_done = nfs4_reclaim_complete_done,
6045 .rpc_release = nfs4_free_reclaim_complete_data,
6046};
6047
6048/*
6049 * Issue a global reclaim complete.
6050 */
6051static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
6052{
6053 struct nfs4_reclaim_complete_data *calldata;
6054 struct rpc_task *task;
6055 struct rpc_message msg = {
6056 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
6057 };
6058 struct rpc_task_setup task_setup_data = {
6059 .rpc_client = clp->cl_rpcclient,
6060 .rpc_message = &msg,
6061 .callback_ops = &nfs4_reclaim_complete_call_ops,
6062 .flags = RPC_TASK_ASYNC,
6063 };
6064 int status = -ENOMEM;
6065
6066 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006067 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006068 if (calldata == NULL)
6069 goto out;
6070 calldata->clp = clp;
6071 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006072
Trond Myklebust9d12b212012-01-17 22:04:25 -05006073 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006074 msg.rpc_argp = &calldata->arg;
6075 msg.rpc_resp = &calldata->res;
6076 task_setup_data.callback_data = calldata;
6077 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006078 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006079 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006080 goto out;
6081 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05006082 status = nfs4_wait_for_completion_rpc_task(task);
6083 if (status == 0)
6084 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006085 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006086 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006087out:
6088 dprintk("<-- %s status=%d\n", __func__, status);
6089 return status;
6090}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006091
6092static void
6093nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6094{
6095 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00006096 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006097
6098 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00006099 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6100 * right now covering the LAYOUTGET we are about to send.
6101 * However, that is not so catastrophic, and there seems
6102 * to be no way to prevent it completely.
6103 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006104 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006105 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006106 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006107 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6108 NFS_I(lgp->args.inode)->layout,
6109 lgp->args.ctx->state)) {
6110 rpc_exit(task, NFS4_OK);
6111 return;
6112 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006113 rpc_call_start(task);
6114}
6115
6116static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6117{
6118 struct nfs4_layoutget *lgp = calldata;
6119 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6120
6121 dprintk("--> %s\n", __func__);
6122
6123 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
6124 return;
6125
6126 switch (task->tk_status) {
6127 case 0:
6128 break;
6129 case -NFS4ERR_LAYOUTTRYLATER:
6130 case -NFS4ERR_RECALLCONFLICT:
6131 task->tk_status = -NFS4ERR_DELAY;
6132 /* Fall through */
6133 default:
6134 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6135 rpc_restart_call_prepare(task);
6136 return;
6137 }
6138 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006139 dprintk("<-- %s\n", __func__);
6140}
6141
6142static void nfs4_layoutget_release(void *calldata)
6143{
6144 struct nfs4_layoutget *lgp = calldata;
6145
6146 dprintk("--> %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006147 put_nfs_open_context(lgp->args.ctx);
6148 kfree(calldata);
6149 dprintk("<-- %s\n", __func__);
6150}
6151
6152static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6153 .rpc_call_prepare = nfs4_layoutget_prepare,
6154 .rpc_call_done = nfs4_layoutget_done,
6155 .rpc_release = nfs4_layoutget_release,
6156};
6157
6158int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
6159{
6160 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6161 struct rpc_task *task;
6162 struct rpc_message msg = {
6163 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6164 .rpc_argp = &lgp->args,
6165 .rpc_resp = &lgp->res,
6166 };
6167 struct rpc_task_setup task_setup_data = {
6168 .rpc_client = server->client,
6169 .rpc_message = &msg,
6170 .callback_ops = &nfs4_layoutget_call_ops,
6171 .callback_data = lgp,
6172 .flags = RPC_TASK_ASYNC,
6173 };
6174 int status = 0;
6175
6176 dprintk("--> %s\n", __func__);
6177
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006178 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006179 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006180 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006181 task = rpc_run_task(&task_setup_data);
6182 if (IS_ERR(task))
6183 return PTR_ERR(task);
6184 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006185 if (status == 0)
6186 status = task->tk_status;
6187 if (status == 0)
6188 status = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006189 rpc_put_task(task);
6190 dprintk("<-- %s status=%d\n", __func__, status);
6191 return status;
6192}
6193
Benny Halevycbe82602011-05-22 19:52:37 +03006194static void
6195nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6196{
6197 struct nfs4_layoutreturn *lrp = calldata;
6198
6199 dprintk("--> %s\n", __func__);
6200 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006201 &lrp->res.seq_res, task))
Benny Halevycbe82602011-05-22 19:52:37 +03006202 return;
6203 rpc_call_start(task);
6204}
6205
6206static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6207{
6208 struct nfs4_layoutreturn *lrp = calldata;
6209 struct nfs_server *server;
Trond Myklebusta56aaa02011-06-15 11:59:10 -04006210 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006211
6212 dprintk("--> %s\n", __func__);
6213
6214 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
6215 return;
6216
6217 server = NFS_SERVER(lrp->args.inode);
6218 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006219 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006220 return;
6221 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04006222 spin_lock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03006223 if (task->tk_status == 0) {
Benny Halevycbe82602011-05-22 19:52:37 +03006224 if (lrp->res.lrs_present) {
Benny Halevycbe82602011-05-22 19:52:37 +03006225 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Benny Halevycbe82602011-05-22 19:52:37 +03006226 } else
6227 BUG_ON(!list_empty(&lo->plh_segs));
6228 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04006229 lo->plh_block_lgets--;
6230 spin_unlock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03006231 dprintk("<-- %s\n", __func__);
6232}
6233
6234static void nfs4_layoutreturn_release(void *calldata)
6235{
6236 struct nfs4_layoutreturn *lrp = calldata;
6237
6238 dprintk("--> %s\n", __func__);
Trond Myklebusta56aaa02011-06-15 11:59:10 -04006239 put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006240 kfree(calldata);
6241 dprintk("<-- %s\n", __func__);
6242}
6243
6244static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6245 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6246 .rpc_call_done = nfs4_layoutreturn_done,
6247 .rpc_release = nfs4_layoutreturn_release,
6248};
6249
6250int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6251{
6252 struct rpc_task *task;
6253 struct rpc_message msg = {
6254 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6255 .rpc_argp = &lrp->args,
6256 .rpc_resp = &lrp->res,
6257 };
6258 struct rpc_task_setup task_setup_data = {
6259 .rpc_client = lrp->clp->cl_rpcclient,
6260 .rpc_message = &msg,
6261 .callback_ops = &nfs4_layoutreturn_call_ops,
6262 .callback_data = lrp,
6263 };
6264 int status;
6265
6266 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006267 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006268 task = rpc_run_task(&task_setup_data);
6269 if (IS_ERR(task))
6270 return PTR_ERR(task);
6271 status = task->tk_status;
6272 dprintk("<-- %s status=%d\n", __func__, status);
6273 rpc_put_task(task);
6274 return status;
6275}
6276
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006277/*
6278 * Retrieve the list of Data Server devices from the MDS.
6279 */
6280static int _nfs4_getdevicelist(struct nfs_server *server,
6281 const struct nfs_fh *fh,
6282 struct pnfs_devicelist *devlist)
6283{
6284 struct nfs4_getdevicelist_args args = {
6285 .fh = fh,
6286 .layoutclass = server->pnfs_curr_ld->id,
6287 };
6288 struct nfs4_getdevicelist_res res = {
6289 .devlist = devlist,
6290 };
6291 struct rpc_message msg = {
6292 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6293 .rpc_argp = &args,
6294 .rpc_resp = &res,
6295 };
6296 int status;
6297
6298 dprintk("--> %s\n", __func__);
6299 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6300 &res.seq_res, 0);
6301 dprintk("<-- %s status=%d\n", __func__, status);
6302 return status;
6303}
6304
6305int nfs4_proc_getdevicelist(struct nfs_server *server,
6306 const struct nfs_fh *fh,
6307 struct pnfs_devicelist *devlist)
6308{
6309 struct nfs4_exception exception = { };
6310 int err;
6311
6312 do {
6313 err = nfs4_handle_exception(server,
6314 _nfs4_getdevicelist(server, fh, devlist),
6315 &exception);
6316 } while (exception.retry);
6317
6318 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6319 err, devlist->num_devs);
6320
6321 return err;
6322}
6323EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6324
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006325static int
6326_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6327{
6328 struct nfs4_getdeviceinfo_args args = {
6329 .pdev = pdev,
6330 };
6331 struct nfs4_getdeviceinfo_res res = {
6332 .pdev = pdev,
6333 };
6334 struct rpc_message msg = {
6335 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6336 .rpc_argp = &args,
6337 .rpc_resp = &res,
6338 };
6339 int status;
6340
6341 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006342 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006343 dprintk("<-- %s status=%d\n", __func__, status);
6344
6345 return status;
6346}
6347
6348int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6349{
6350 struct nfs4_exception exception = { };
6351 int err;
6352
6353 do {
6354 err = nfs4_handle_exception(server,
6355 _nfs4_proc_getdeviceinfo(server, pdev),
6356 &exception);
6357 } while (exception.retry);
6358 return err;
6359}
6360EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6361
Andy Adamson863a3c62011-03-23 13:27:54 +00006362static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6363{
6364 struct nfs4_layoutcommit_data *data = calldata;
6365 struct nfs_server *server = NFS_SERVER(data->args.inode);
6366
6367 if (nfs4_setup_sequence(server, &data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006368 &data->res.seq_res, task))
Andy Adamson863a3c62011-03-23 13:27:54 +00006369 return;
6370 rpc_call_start(task);
6371}
6372
6373static void
6374nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6375{
6376 struct nfs4_layoutcommit_data *data = calldata;
6377 struct nfs_server *server = NFS_SERVER(data->args.inode);
6378
6379 if (!nfs4_sequence_done(task, &data->res.seq_res))
6380 return;
6381
6382 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006383 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6384 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6385 case -NFS4ERR_BADLAYOUT: /* no layout */
6386 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006387 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006388 break;
6389 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006390 nfs_post_op_update_inode_force_wcc(data->args.inode,
6391 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006392 break;
6393 default:
6394 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6395 rpc_restart_call_prepare(task);
6396 return;
6397 }
6398 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006399}
6400
6401static void nfs4_layoutcommit_release(void *calldata)
6402{
6403 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04006404 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07006405 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00006406
Andy Adamsondb29c082011-07-30 20:52:38 -04006407 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006408 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04006409 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6410 list_del_init(&lseg->pls_lc_list);
6411 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6412 &lseg->pls_flags))
6413 put_lseg(lseg);
6414 }
Peng Tao92407e72011-10-23 20:21:17 -07006415
6416 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6417 smp_mb__after_clear_bit();
6418 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6419
Andy Adamson863a3c62011-03-23 13:27:54 +00006420 put_rpccred(data->cred);
6421 kfree(data);
6422}
6423
6424static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6425 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6426 .rpc_call_done = nfs4_layoutcommit_done,
6427 .rpc_release = nfs4_layoutcommit_release,
6428};
6429
6430int
Andy Adamsonef311532011-03-12 02:58:10 -05006431nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006432{
6433 struct rpc_message msg = {
6434 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6435 .rpc_argp = &data->args,
6436 .rpc_resp = &data->res,
6437 .rpc_cred = data->cred,
6438 };
6439 struct rpc_task_setup task_setup_data = {
6440 .task = &data->task,
6441 .rpc_client = NFS_CLIENT(data->args.inode),
6442 .rpc_message = &msg,
6443 .callback_ops = &nfs4_layoutcommit_ops,
6444 .callback_data = data,
6445 .flags = RPC_TASK_ASYNC,
6446 };
6447 struct rpc_task *task;
6448 int status = 0;
6449
6450 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6451 "lbw: %llu inode %lu\n",
6452 data->task.tk_pid, sync,
6453 data->args.lastbytewritten,
6454 data->args.inode->i_ino);
6455
Trond Myklebust9d12b212012-01-17 22:04:25 -05006456 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006457 task = rpc_run_task(&task_setup_data);
6458 if (IS_ERR(task))
6459 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006460 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006461 goto out;
6462 status = nfs4_wait_for_completion_rpc_task(task);
6463 if (status != 0)
6464 goto out;
6465 status = task->tk_status;
6466out:
6467 dprintk("%s: status %d\n", __func__, status);
6468 rpc_put_task(task);
6469 return status;
6470}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006471
6472static int
6473_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6474 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6475{
6476 struct nfs41_secinfo_no_name_args args = {
6477 .style = SECINFO_STYLE_CURRENT_FH,
6478 };
6479 struct nfs4_secinfo_res res = {
6480 .flavors = flavors,
6481 };
6482 struct rpc_message msg = {
6483 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6484 .rpc_argp = &args,
6485 .rpc_resp = &res,
6486 };
6487 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6488}
6489
6490static int
6491nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6492 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6493{
6494 struct nfs4_exception exception = { };
6495 int err;
6496 do {
6497 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6498 switch (err) {
6499 case 0:
6500 case -NFS4ERR_WRONGSEC:
6501 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006502 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006503 default:
6504 err = nfs4_handle_exception(server, err, &exception);
6505 }
6506 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006507out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006508 return err;
6509}
6510
6511static int
6512nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6513 struct nfs_fsinfo *info)
6514{
6515 int err;
6516 struct page *page;
6517 rpc_authflavor_t flavor;
6518 struct nfs4_secinfo_flavors *flavors;
6519
6520 page = alloc_page(GFP_KERNEL);
6521 if (!page) {
6522 err = -ENOMEM;
6523 goto out;
6524 }
6525
6526 flavors = page_address(page);
6527 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6528
6529 /*
6530 * Fall back on "guess and check" method if
6531 * the server doesn't support SECINFO_NO_NAME
6532 */
6533 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6534 err = nfs4_find_root_sec(server, fhandle, info);
6535 goto out_freepage;
6536 }
6537 if (err)
6538 goto out_freepage;
6539
6540 flavor = nfs_find_best_sec(flavors);
6541 if (err == 0)
6542 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6543
6544out_freepage:
6545 put_page(page);
6546 if (err == -EACCES)
6547 return -EPERM;
6548out:
6549 return err;
6550}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006551
6552static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006553{
6554 int status;
6555 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006556 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006557 };
6558 struct nfs41_test_stateid_res res;
6559 struct rpc_message msg = {
6560 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6561 .rpc_argp = &args,
6562 .rpc_resp = &res,
6563 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006564
Trond Myklebust9d12b212012-01-17 22:04:25 -05006565 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006566 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
6567
6568 if (status == NFS_OK)
6569 return res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006570 return status;
6571}
6572
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006573static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006574{
6575 struct nfs4_exception exception = { };
6576 int err;
6577 do {
6578 err = nfs4_handle_exception(server,
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006579 _nfs41_test_stateid(server, stateid),
Bryan Schumaker7d974792011-06-02 14:59:08 -04006580 &exception);
6581 } while (exception.retry);
6582 return err;
6583}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006584
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006585static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006586{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006587 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006588 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006589 };
6590 struct nfs41_free_stateid_res res;
6591 struct rpc_message msg = {
6592 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6593 .rpc_argp = &args,
6594 .rpc_resp = &res,
6595 };
6596
Trond Myklebust9d12b212012-01-17 22:04:25 -05006597 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6598 return nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006599}
6600
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006601static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006602{
6603 struct nfs4_exception exception = { };
6604 int err;
6605 do {
6606 err = nfs4_handle_exception(server,
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006607 _nfs4_free_stateid(server, stateid),
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006608 &exception);
6609 } while (exception.retry);
6610 return err;
6611}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006612
6613static bool nfs41_match_stateid(const nfs4_stateid *s1,
6614 const nfs4_stateid *s2)
6615{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006616 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006617 return false;
6618
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006619 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006620 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006621 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006622 return true;
6623
6624 return false;
6625}
6626
Andy Adamson557134a2009-04-01 09:21:53 -04006627#endif /* CONFIG_NFS_V4_1 */
6628
Trond Myklebust36281ca2012-03-04 18:13:56 -05006629static bool nfs4_match_stateid(const nfs4_stateid *s1,
6630 const nfs4_stateid *s2)
6631{
Trond Myklebustf597c532012-03-04 18:13:56 -05006632 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006633}
6634
6635
Trond Myklebust17280172012-03-11 13:11:00 -04006636static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006637 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006638 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006639 .recover_open = nfs4_open_reclaim,
6640 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006641 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006642 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006643};
6644
Andy Adamson591d71c2009-04-01 09:22:47 -04006645#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006646static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006647 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6648 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6649 .recover_open = nfs4_open_reclaim,
6650 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006651 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006652 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006653 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04006654};
6655#endif /* CONFIG_NFS_V4_1 */
6656
Trond Myklebust17280172012-03-11 13:11:00 -04006657static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006658 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006659 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006660 .recover_open = nfs4_open_expired,
6661 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006662 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006663 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006664};
6665
Andy Adamson591d71c2009-04-01 09:22:47 -04006666#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006667static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006668 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6669 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006670 .recover_open = nfs41_open_expired,
6671 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006672 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006673 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006674};
6675#endif /* CONFIG_NFS_V4_1 */
6676
Trond Myklebust17280172012-03-11 13:11:00 -04006677static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006678 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006679 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006680 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006681};
6682
6683#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006684static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006685 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006686 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006687 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006688};
6689#endif
6690
Trond Myklebust97dc1352010-06-16 09:52:26 -04006691static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6692 .minor_version = 0,
6693 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006694 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006695 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006696 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6697 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6698 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006699};
6700
6701#if defined(CONFIG_NFS_V4_1)
6702static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6703 .minor_version = 1,
6704 .call_sync = _nfs4_call_sync_session,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006705 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006706 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006707 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6708 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6709 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006710};
6711#endif
6712
Trond Myklebust97dc1352010-06-16 09:52:26 -04006713const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6714 [0] = &nfs_v4_0_minor_ops,
6715#if defined(CONFIG_NFS_V4_1)
6716 [1] = &nfs_v4_1_minor_ops,
6717#endif
6718};
6719
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006720static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006721 .permission = nfs_permission,
6722 .getattr = nfs_getattr,
6723 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006724 .getxattr = generic_getxattr,
6725 .setxattr = generic_setxattr,
6726 .listxattr = generic_listxattr,
6727 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006728};
6729
David Howells509de812006-08-22 20:06:11 -04006730const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006731 .version = 4, /* protocol version */
6732 .dentry_ops = &nfs4_dentry_operations,
6733 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006734 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006735 .file_ops = &nfs4_file_operations,
Bryan Schumakerbae36242012-05-10 15:07:31 -04006736 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04006737 .submount = nfs4_submount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006738 .getattr = nfs4_proc_getattr,
6739 .setattr = nfs4_proc_setattr,
6740 .lookup = nfs4_proc_lookup,
6741 .access = nfs4_proc_access,
6742 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006743 .create = nfs4_proc_create,
6744 .remove = nfs4_proc_remove,
6745 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04006746 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006747 .unlink_done = nfs4_proc_unlink_done,
6748 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006749 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04006750 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04006751 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006752 .link = nfs4_proc_link,
6753 .symlink = nfs4_proc_symlink,
6754 .mkdir = nfs4_proc_mkdir,
6755 .rmdir = nfs4_proc_remove,
6756 .readdir = nfs4_proc_readdir,
6757 .mknod = nfs4_proc_mknod,
6758 .statfs = nfs4_proc_statfs,
6759 .fsinfo = nfs4_proc_fsinfo,
6760 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006761 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006762 .decode_dirent = nfs4_decode_dirent,
6763 .read_setup = nfs4_proc_read_setup,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04006764 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05006765 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006766 .write_setup = nfs4_proc_write_setup,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04006767 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006768 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006769 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04006770 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006771 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006772 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006773 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006774 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006775 .open_context = nfs4_atomic_open,
Andy Adamson45a52a02011-03-01 01:34:08 +00006776 .init_client = nfs4_init_client,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006777};
6778
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006779static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6780 .prefix = XATTR_NAME_NFSV4_ACL,
6781 .list = nfs4_xattr_list_nfs4_acl,
6782 .get = nfs4_xattr_get_nfs4_acl,
6783 .set = nfs4_xattr_set_nfs4_acl,
6784};
6785
6786const struct xattr_handler *nfs4_xattr_handlers[] = {
6787 &nfs4_xattr_nfs4_acl_handler,
6788 NULL
6789};
6790
Trond Myklebustef159e92012-02-06 19:50:40 -05006791module_param(max_session_slots, ushort, 0644);
6792MODULE_PARM_DESC(max_session_slots, "Maximum number of outstanding NFSv4.1 "
6793 "requests the client will negotiate");
6794
Linus Torvalds1da177e2005-04-16 15:20:36 -07006795/*
6796 * Local variables:
6797 * c-basic-offset: 8
6798 * End:
6799 */