blob: f3f56f4a3b725181a9c0c56a7194c2d1ac507701 [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68#define NFSDBG_FACILITY NFSDBG_PROC
69
Trond Myklebust2066fe82006-09-15 08:30:46 -040070#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define NFS4_POLL_RETRY_MAX (15*HZ)
72
Trond Myklebusta78cb572009-08-09 15:06:19 -040073#define NFS4_MAX_LOOP_ON_RECOVER (10)
74
Trond Myklebustcdd4e682006-01-03 09:55:12 +010075struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010076static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080077static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050079static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Trond Myklebust99367812007-07-17 21:52:41 -040080static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040081static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
82 struct nfs_fattr *fattr, struct iattr *sattr,
83 struct nfs4_state *state);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040084#ifdef CONFIG_NFS_V4_1
Bryan Schumaker1cab0652012-01-31 10:39:29 -050085static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *);
86static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040087#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070088/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050089static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
Trond Myklebust52567b02009-10-23 14:46:42 -040091 if (err >= -1000)
92 return err;
93 switch (err) {
94 case -NFS4ERR_RESOURCE:
95 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +000096 case -NFS4ERR_WRONGSEC:
97 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -080098 case -NFS4ERR_BADOWNER:
99 case -NFS4ERR_BADNAME:
100 return -EINVAL;
Trond Myklebust52567b02009-10-23 14:46:42 -0400101 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700103 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400104 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400106 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
108
109/*
110 * This is our standard bitmap for GETATTR requests.
111 */
112const u32 nfs4_fattr_bitmap[2] = {
113 FATTR4_WORD0_TYPE
114 | FATTR4_WORD0_CHANGE
115 | FATTR4_WORD0_SIZE
116 | FATTR4_WORD0_FSID
117 | FATTR4_WORD0_FILEID,
118 FATTR4_WORD1_MODE
119 | FATTR4_WORD1_NUMLINKS
120 | FATTR4_WORD1_OWNER
121 | FATTR4_WORD1_OWNER_GROUP
122 | FATTR4_WORD1_RAWDEV
123 | FATTR4_WORD1_SPACE_USED
124 | FATTR4_WORD1_TIME_ACCESS
125 | FATTR4_WORD1_TIME_METADATA
126 | FATTR4_WORD1_TIME_MODIFY
127};
128
129const u32 nfs4_statfs_bitmap[2] = {
130 FATTR4_WORD0_FILES_AVAIL
131 | FATTR4_WORD0_FILES_FREE
132 | FATTR4_WORD0_FILES_TOTAL,
133 FATTR4_WORD1_SPACE_AVAIL
134 | FATTR4_WORD1_SPACE_FREE
135 | FATTR4_WORD1_SPACE_TOTAL
136};
137
Trond Myklebust4ce79712005-06-22 17:16:21 +0000138const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 FATTR4_WORD0_MAXLINK
140 | FATTR4_WORD0_MAXNAME,
141 0
142};
143
Fred Isamandae100c2011-07-30 20:52:37 -0400144const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 | FATTR4_WORD0_MAXREAD
146 | FATTR4_WORD0_MAXWRITE
147 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700148 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400149 | FATTR4_WORD1_FS_LAYOUT_TYPES,
150 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151};
152
Manoj Naik830b8e32006-06-09 09:34:25 -0400153const u32 nfs4_fs_locations_bitmap[2] = {
154 FATTR4_WORD0_TYPE
155 | FATTR4_WORD0_CHANGE
156 | FATTR4_WORD0_SIZE
157 | FATTR4_WORD0_FSID
158 | FATTR4_WORD0_FILEID
159 | FATTR4_WORD0_FS_LOCATIONS,
160 FATTR4_WORD1_MODE
161 | FATTR4_WORD1_NUMLINKS
162 | FATTR4_WORD1_OWNER
163 | FATTR4_WORD1_OWNER_GROUP
164 | FATTR4_WORD1_RAWDEV
165 | FATTR4_WORD1_SPACE_USED
166 | FATTR4_WORD1_TIME_ACCESS
167 | FATTR4_WORD1_TIME_METADATA
168 | FATTR4_WORD1_TIME_MODIFY
169 | FATTR4_WORD1_MOUNTED_ON_FILEID
170};
171
Al Virobc4785c2006-10-19 23:28:51 -0700172static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 struct nfs4_readdir_arg *readdir)
174{
Al Viro0dbb4c62006-10-19 23:28:49 -0700175 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177 BUG_ON(readdir->count < 80);
178 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000179 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
181 return;
182 }
183
184 readdir->cookie = 0;
185 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
186 if (cookie == 2)
187 return;
188
189 /*
190 * NFSv4 servers do not return entries for '.' and '..'
191 * Therefore, we fake these entries here. We let '.'
192 * have cookie 0 and '..' have cookie 1. Note that
193 * when talking to the server, we always send cookie 0
194 * instead of 1 or 2.
195 */
Al Viro0dbb4c62006-10-19 23:28:49 -0700196 start = p = kmap_atomic(*readdir->pages, KM_USER0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198 if (cookie == 0) {
199 *p++ = xdr_one; /* next */
200 *p++ = xdr_zero; /* cookie, first word */
201 *p++ = xdr_one; /* cookie, second word */
202 *p++ = xdr_one; /* entry len */
203 memcpy(p, ".\0\0\0", 4); /* entry */
204 p++;
205 *p++ = xdr_one; /* bitmap length */
206 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
207 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400208 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 }
210
211 *p++ = xdr_one; /* next */
212 *p++ = xdr_zero; /* cookie, first word */
213 *p++ = xdr_two; /* cookie, second word */
214 *p++ = xdr_two; /* entry len */
215 memcpy(p, "..\0\0", 4); /* entry */
216 p++;
217 *p++ = xdr_one; /* bitmap length */
218 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
219 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400220 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222 readdir->pgbase = (char *)p - (char *)start;
223 readdir->count -= readdir->pgbase;
224 kunmap_atomic(start, KM_USER0);
225}
226
Trond Myklebust65de8722008-12-23 15:21:44 -0500227static int nfs4_wait_clnt_recover(struct nfs_client *clp)
228{
229 int res;
230
231 might_sleep();
232
Trond Myklebuste005e802008-12-23 15:21:48 -0500233 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400234 nfs_wait_bit_killable, TASK_KILLABLE);
Trond Myklebust65de8722008-12-23 15:21:44 -0500235 return res;
236}
237
238static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
239{
240 int res = 0;
241
242 might_sleep();
243
244 if (*timeout <= 0)
245 *timeout = NFS4_POLL_RETRY_MIN;
246 if (*timeout > NFS4_POLL_RETRY_MAX)
247 *timeout = NFS4_POLL_RETRY_MAX;
Jeff Laytond3103102011-12-01 22:44:39 +0100248 freezable_schedule_timeout_killable(*timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500249 if (fatal_signal_pending(current))
250 res = -ERESTARTSYS;
251 *timeout <<= 1;
252 return res;
253}
254
255/* This is the error handling routine for processes that are allowed
256 * to sleep.
257 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800258static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500259{
260 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500261 struct nfs4_state *state = exception->state;
Trond Myklebust65de8722008-12-23 15:21:44 -0500262 int ret = errorcode;
263
264 exception->retry = 0;
265 switch(errorcode) {
266 case 0:
267 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500268 case -NFS4ERR_ADMIN_REVOKED:
269 case -NFS4ERR_BAD_STATEID:
270 case -NFS4ERR_OPENMODE:
271 if (state == NULL)
272 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500273 nfs4_schedule_stateid_recovery(server, state);
274 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400275 case -NFS4ERR_EXPIRED:
276 if (state != NULL)
277 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust65de8722008-12-23 15:21:44 -0500278 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500279 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500280 nfs4_schedule_lease_recovery(clp);
281 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500282#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400283 case -NFS4ERR_BADSESSION:
284 case -NFS4ERR_BADSLOT:
285 case -NFS4ERR_BAD_HIGH_SLOT:
286 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
287 case -NFS4ERR_DEADSESSION:
288 case -NFS4ERR_SEQ_FALSE_RETRY:
289 case -NFS4ERR_SEQ_MISORDERED:
290 dprintk("%s ERROR: %d Reset session\n", __func__,
291 errorcode);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500292 nfs4_schedule_session_recovery(clp->cl_session);
Andy Adamson4745e312009-04-01 09:22:42 -0400293 exception->retry = 1;
Andy Adamsonb9179232009-12-04 15:55:32 -0500294 break;
Trond Myklebust03391692010-01-26 15:42:38 -0500295#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500296 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500297 if (exception->timeout > HZ) {
298 /* We have retried a decent amount, time to
299 * fail
300 */
301 ret = -EBUSY;
302 break;
303 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500304 case -NFS4ERR_GRACE:
305 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -0500306 case -EKEYEXPIRED:
Trond Myklebust65de8722008-12-23 15:21:44 -0500307 ret = nfs4_delay(server->client, &exception->timeout);
308 if (ret != 0)
309 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400310 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500311 case -NFS4ERR_OLD_STATEID:
312 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800313 break;
314 case -NFS4ERR_BADOWNER:
315 /* The following works around a Linux server bug! */
316 case -NFS4ERR_BADNAME:
317 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
318 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
319 exception->retry = 1;
320 printk(KERN_WARNING "NFS: v4 server %s "
321 "does not accept raw "
322 "uid/gids. "
323 "Reenabling the idmapper.\n",
324 server->nfs_client->cl_hostname);
325 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500326 }
327 /* We failed to handle the error */
328 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500329wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500330 ret = nfs4_wait_clnt_recover(clp);
331 if (ret == 0)
332 exception->retry = 1;
333 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500334}
335
336
Trond Myklebust452e9352010-07-31 14:29:06 -0400337static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 spin_lock(&clp->cl_lock);
340 if (time_before(clp->cl_last_renewal,timestamp))
341 clp->cl_last_renewal = timestamp;
342 spin_unlock(&clp->cl_lock);
343}
344
Trond Myklebust452e9352010-07-31 14:29:06 -0400345static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
346{
347 do_renew_lease(server->nfs_client, timestamp);
348}
349
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400350#if defined(CONFIG_NFS_V4_1)
351
Benny Halevy510b8172009-04-01 09:22:14 -0400352/*
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400353 * nfs4_free_slot - free a slot and efficiently update slot table.
354 *
355 * freeing a slot is trivially done by clearing its respective bit
356 * in the bitmap.
357 * If the freed slotid equals highest_used_slotid we want to update it
358 * so that the server would be able to size down the slot table if needed,
359 * otherwise we know that the highest_used_slotid is still in use.
360 * When updating highest_used_slotid there may be "holes" in the bitmap
361 * so we need to scan down from highest_used_slotid to 0 looking for the now
362 * highest slotid in use.
Trond Myklebust45d43c22012-02-06 19:38:51 -0500363 * If none found, highest_used_slotid is set to NFS4_NO_SLOT.
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500364 *
365 * Must be called while holding tbl->slot_tbl_lock
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400366 */
367static void
Trond Myklebust45d43c22012-02-06 19:38:51 -0500368nfs4_free_slot(struct nfs4_slot_table *tbl, u32 slotid)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400369{
Trond Myklebust45d43c22012-02-06 19:38:51 -0500370 BUG_ON(slotid >= NFS4_MAX_SLOT_TABLE);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400371 /* clear used bit in bitmap */
372 __clear_bit(slotid, tbl->used_slots);
373
374 /* update highest_used_slotid when it is freed */
375 if (slotid == tbl->highest_used_slotid) {
376 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500377 if (slotid < tbl->max_slots)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400378 tbl->highest_used_slotid = slotid;
379 else
Trond Myklebust45d43c22012-02-06 19:38:51 -0500380 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400381 }
Trond Myklebust45d43c22012-02-06 19:38:51 -0500382 dprintk("%s: slotid %u highest_used_slotid %d\n", __func__,
383 slotid, tbl->highest_used_slotid);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400384}
385
Trond Myklebust961a8282012-01-17 22:57:37 -0500386bool nfs4_set_task_privileged(struct rpc_task *task, void *dummy)
387{
388 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
389 return true;
390}
391
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800392/*
Andy Adamson42acd022011-01-06 02:04:34 +0000393 * Signal state manager thread if session fore channel is drained
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800394 */
Andy Adamson42acd022011-01-06 02:04:34 +0000395static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800396{
Trond Myklebusta2118c32010-06-16 09:52:26 -0400397 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
Trond Myklebust961a8282012-01-17 22:57:37 -0500398 rpc_wake_up_first(&ses->fc_slot_table.slot_tbl_waitq,
399 nfs4_set_task_privileged, NULL);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800400 return;
401 }
402
Trond Myklebust45d43c22012-02-06 19:38:51 -0500403 if (ses->fc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800404 return;
405
Andy Adamson42acd022011-01-06 02:04:34 +0000406 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
407 complete(&ses->fc_slot_table.complete);
408}
409
410/*
411 * Signal state manager thread if session back channel is drained
412 */
413void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
414{
415 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
Trond Myklebust45d43c22012-02-06 19:38:51 -0500416 ses->bc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
Andy Adamson42acd022011-01-06 02:04:34 +0000417 return;
418 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
419 complete(&ses->bc_slot_table.complete);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800420}
421
Trond Myklebustd185a332010-06-16 09:52:25 -0400422static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400423{
424 struct nfs4_slot_table *tbl;
425
Trond Myklebustd185a332010-06-16 09:52:25 -0400426 tbl = &res->sr_session->fc_slot_table;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400427 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400428 /* just wake up the next guy waiting since
429 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500430 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500431 return;
Andy Adamson13615872009-04-01 09:22:17 -0400432 }
Andy Adamsonea028ac2009-12-04 15:55:38 -0500433
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500434 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonaabd0b42011-11-09 13:58:22 -0500435 nfs4_free_slot(tbl, res->sr_slot - tbl->slots);
Andy Adamson42acd022011-01-06 02:04:34 +0000436 nfs4_check_drain_fc_complete(res->sr_session);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500437 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400438 res->sr_slot = NULL;
Andy Adamson13615872009-04-01 09:22:17 -0400439}
440
Trond Myklebust14516c32010-07-31 14:29:06 -0400441static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400442{
443 unsigned long timestamp;
Trond Myklebust14516c32010-07-31 14:29:06 -0400444 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400445
446 /*
447 * sr_status remains 1 if an RPC level error occurred. The server
448 * may or may not have processed the sequence operation..
449 * Proceed as if the server received and processed the sequence
450 * operation.
451 */
452 if (res->sr_status == 1)
453 res->sr_status = NFS_OK;
454
Bryan Schumaker468f8612011-04-18 15:57:32 -0400455 /* don't increment the sequence number if the task wasn't sent */
456 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400457 goto out;
458
Andy Adamson691daf32009-12-04 15:55:39 -0500459 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400460 switch (res->sr_status) {
461 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400462 /* Update the slot's sequence and clientid lease timer */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400463 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400464 timestamp = res->sr_renewal_time;
Trond Myklebust14516c32010-07-31 14:29:06 -0400465 clp = res->sr_session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400466 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500467 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500468 if (res->sr_status_flags != 0)
469 nfs4_schedule_lease_recovery(clp);
Trond Myklebust14516c32010-07-31 14:29:06 -0400470 break;
471 case -NFS4ERR_DELAY:
472 /* The server detected a resend of the RPC call and
473 * returned NFS4ERR_DELAY as per Section 2.10.6.2
474 * of RFC5661.
475 */
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200476 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400477 __func__,
478 res->sr_slot - res->sr_session->fc_slot_table.slots,
479 res->sr_slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400480 goto out_retry;
481 default:
482 /* Just update the slot sequence no. */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400483 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400484 }
485out:
486 /* The session may be reset by one of the error handlers. */
487 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400488 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400489 return 1;
490out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400491 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400492 goto out;
493 rpc_delay(task, NFS4_POLL_RETRY_MAX);
494 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400495}
496
Trond Myklebust14516c32010-07-31 14:29:06 -0400497static int nfs4_sequence_done(struct rpc_task *task,
498 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400499{
Trond Myklebust14516c32010-07-31 14:29:06 -0400500 if (res->sr_session == NULL)
501 return 1;
502 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400503}
504
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400505/*
Benny Halevy510b8172009-04-01 09:22:14 -0400506 * nfs4_find_slot - efficiently look for a free slot
507 *
508 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
509 * If found, we mark the slot as used, update the highest_used_slotid,
510 * and respectively set up the sequence operation args.
Trond Myklebust45d43c22012-02-06 19:38:51 -0500511 * The slot number is returned if found, or NFS4_NO_SLOT otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400512 *
513 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400514 */
Trond Myklebust45d43c22012-02-06 19:38:51 -0500515static u32
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800516nfs4_find_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400517{
Trond Myklebust45d43c22012-02-06 19:38:51 -0500518 u32 slotid;
519 u32 ret_id = NFS4_NO_SLOT;
Benny Halevy510b8172009-04-01 09:22:14 -0400520
Trond Myklebust45d43c22012-02-06 19:38:51 -0500521 dprintk("--> %s used_slots=%04lx highest_used=%u max_slots=%u\n",
Benny Halevy510b8172009-04-01 09:22:14 -0400522 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
523 tbl->max_slots);
524 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
525 if (slotid >= tbl->max_slots)
526 goto out;
527 __set_bit(slotid, tbl->used_slots);
Trond Myklebust45d43c22012-02-06 19:38:51 -0500528 if (slotid > tbl->highest_used_slotid ||
529 tbl->highest_used_slotid == NFS4_NO_SLOT)
Benny Halevy510b8172009-04-01 09:22:14 -0400530 tbl->highest_used_slotid = slotid;
531 ret_id = slotid;
532out:
533 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
534 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400535 return ret_id;
536}
537
Trond Myklebust9d12b212012-01-17 22:04:25 -0500538static void nfs41_init_sequence(struct nfs4_sequence_args *args,
539 struct nfs4_sequence_res *res, int cache_reply)
540{
541 args->sa_session = NULL;
542 args->sa_cache_this = 0;
543 if (cache_reply)
544 args->sa_cache_this = 1;
545 res->sr_session = NULL;
546 res->sr_slot = NULL;
547}
548
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000549int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400550 struct nfs4_sequence_args *args,
551 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400552 struct rpc_task *task)
553{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400554 struct nfs4_slot *slot;
555 struct nfs4_slot_table *tbl;
Trond Myklebust45d43c22012-02-06 19:38:51 -0500556 u32 slotid;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400557
558 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400559 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400560 if (res->sr_slot != NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400561 return 0;
562
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400563 tbl = &session->fc_slot_table;
564
565 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400566 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800567 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500568 /* The state manager will wait until the slot table is empty */
Andy Adamson05f0d232009-12-04 15:55:37 -0500569 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400570 spin_unlock(&tbl->slot_tbl_lock);
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500571 dprintk("%s session is draining\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500572 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400573 }
574
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800575 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
576 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
577 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
578 spin_unlock(&tbl->slot_tbl_lock);
579 dprintk("%s enforce FIFO order\n", __func__);
580 return -EAGAIN;
581 }
582
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800583 slotid = nfs4_find_slot(tbl);
Trond Myklebust45d43c22012-02-06 19:38:51 -0500584 if (slotid == NFS4_NO_SLOT) {
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400585 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
586 spin_unlock(&tbl->slot_tbl_lock);
587 dprintk("<-- %s: no free slots\n", __func__);
588 return -EAGAIN;
589 }
590 spin_unlock(&tbl->slot_tbl_lock);
591
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800592 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400593 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400594 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400595 args->sa_slotid = slotid;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400596
597 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
598
Benny Halevy99fe60d2009-04-01 09:22:29 -0400599 res->sr_session = session;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400600 res->sr_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400601 res->sr_renewal_time = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400602 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400603 /*
604 * sr_status is only set in decode_sequence, and so will remain
605 * set to 1 if an rpc level failure occurs.
606 */
607 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400608 return 0;
609}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000610EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400611
Trond Myklebust035168a2010-06-16 09:52:26 -0400612int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400613 struct nfs4_sequence_args *args,
614 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400615 struct rpc_task *task)
616{
Trond Myklebust035168a2010-06-16 09:52:26 -0400617 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400618 int ret = 0;
619
Trond Myklebust9d12b212012-01-17 22:04:25 -0500620 if (session == NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400621 goto out;
Trond Myklebust035168a2010-06-16 09:52:26 -0400622
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200623 dprintk("--> %s clp %p session %p sr_slot %td\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400624 __func__, session->clp, session, res->sr_slot ?
625 res->sr_slot - session->fc_slot_table.slots : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400626
Trond Myklebust9d12b212012-01-17 22:04:25 -0500627 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400628out:
629 dprintk("<-- %s status=%d\n", __func__, ret);
630 return ret;
631}
632
633struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400634 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400635 struct nfs4_sequence_args *seq_args;
636 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400637};
638
639static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
640{
641 struct nfs41_call_sync_data *data = calldata;
642
Trond Myklebust035168a2010-06-16 09:52:26 -0400643 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
644
645 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -0500646 data->seq_res, task))
Andy Adamsonce5039c2009-04-01 09:22:13 -0400647 return;
648 rpc_call_start(task);
649}
650
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800651static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
652{
653 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
654 nfs41_call_sync_prepare(task, calldata);
655}
656
Andy Adamson69ab40c2009-04-01 09:22:19 -0400657static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
658{
659 struct nfs41_call_sync_data *data = calldata;
660
Trond Myklebust14516c32010-07-31 14:29:06 -0400661 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400662}
663
Andy Adamsonce5039c2009-04-01 09:22:13 -0400664struct rpc_call_ops nfs41_call_sync_ops = {
665 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400666 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400667};
668
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800669struct rpc_call_ops nfs41_call_priv_sync_ops = {
670 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
671 .rpc_call_done = nfs41_call_sync_done,
672};
673
Bryan Schumaker7c513052011-03-24 17:12:24 +0000674static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
675 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400676 struct rpc_message *msg,
677 struct nfs4_sequence_args *args,
678 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800679 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400680{
681 int ret;
682 struct rpc_task *task;
683 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400684 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400685 .seq_args = args,
686 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400687 };
688 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000689 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400690 .rpc_message = msg,
691 .callback_ops = &nfs41_call_sync_ops,
692 .callback_data = &data
693 };
694
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800695 if (privileged)
696 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400697 task = rpc_run_task(&task_setup);
698 if (IS_ERR(task))
699 ret = PTR_ERR(task);
700 else {
701 ret = task->tk_status;
702 rpc_put_task(task);
703 }
704 return ret;
705}
706
Bryan Schumaker7c513052011-03-24 17:12:24 +0000707int _nfs4_call_sync_session(struct rpc_clnt *clnt,
708 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400709 struct rpc_message *msg,
710 struct nfs4_sequence_args *args,
711 struct nfs4_sequence_res *res,
712 int cache_reply)
713{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500714 nfs41_init_sequence(args, res, cache_reply);
715 return nfs4_call_sync_sequence(clnt, server, msg, args, res, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400716}
717
Trond Myklebustdf896452010-06-16 09:52:26 -0400718#else
Trond Myklebust9d12b212012-01-17 22:04:25 -0500719static inline
720void nfs41_init_sequence(struct nfs4_sequence_args *args,
721 struct nfs4_sequence_res *res, int cache_reply)
722{
723}
724
Trond Myklebust14516c32010-07-31 14:29:06 -0400725static int nfs4_sequence_done(struct rpc_task *task,
726 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400727{
Trond Myklebust14516c32010-07-31 14:29:06 -0400728 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400729}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400730#endif /* CONFIG_NFS_V4_1 */
731
Bryan Schumaker7c513052011-03-24 17:12:24 +0000732int _nfs4_call_sync(struct rpc_clnt *clnt,
733 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400734 struct rpc_message *msg,
735 struct nfs4_sequence_args *args,
736 struct nfs4_sequence_res *res,
737 int cache_reply)
738{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500739 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000740 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400741}
742
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000743static inline
Bryan Schumaker7c513052011-03-24 17:12:24 +0000744int nfs4_call_sync(struct rpc_clnt *clnt,
745 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000746 struct rpc_message *msg,
747 struct nfs4_sequence_args *args,
748 struct nfs4_sequence_res *res,
749 int cache_reply)
750{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000751 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
752 args, res, cache_reply);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000753}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400754
Trond Myklebust38478b22006-05-25 01:40:57 -0400755static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
Trond Myklebust38478b22006-05-25 01:40:57 -0400757 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Trond Myklebust38478b22006-05-25 01:40:57 -0400759 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400760 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700761 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400762 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700763 dir->i_version = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400764 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765}
766
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100767struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400768 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100769 struct nfs_openargs o_arg;
770 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100771 struct nfs_open_confirmargs c_arg;
772 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500773 struct nfs4_string owner_name;
774 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100775 struct nfs_fattr f_attr;
776 struct nfs_fattr dir_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100777 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400778 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100779 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400780 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100781 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100782 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400783 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100784 int rpc_status;
785 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100786};
787
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400788
789static void nfs4_init_opendata_res(struct nfs4_opendata *p)
790{
791 p->o_res.f_attr = &p->f_attr;
792 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400793 p->o_res.seqid = p->o_arg.seqid;
794 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400795 p->o_res.server = p->o_arg.server;
796 nfs_fattr_init(&p->f_attr);
797 nfs_fattr_init(&p->dir_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500798 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400799}
800
Al Viro82a2c1b2011-06-22 18:30:55 -0400801static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500802 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400803 const struct iattr *attrs,
804 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100805{
Al Viro82a2c1b2011-06-22 18:30:55 -0400806 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100807 struct inode *dir = parent->d_inode;
808 struct nfs_server *server = NFS_SERVER(dir);
809 struct nfs4_opendata *p;
810
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400811 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100812 if (p == NULL)
813 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400814 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100815 if (p->o_arg.seqid == NULL)
816 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400817 nfs_sb_active(dentry->d_sb);
818 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100819 p->dir = parent;
820 p->owner = sp;
821 atomic_inc(&sp->so_count);
822 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500823 p->o_arg.open_flags = flags;
824 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400825 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -0500826 p->o_arg.id = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400827 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100828 p->o_arg.server = server;
829 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500830 p->o_arg.dir_bitmask = server->cache_consistency_bitmask;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100831 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust536e43d2012-01-17 22:04:26 -0500832 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400833 u32 *s;
834
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100835 p->o_arg.u.attrs = &p->attrs;
836 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400837 s = (u32 *) p->o_arg.u.verifier.data;
838 s[0] = jiffies;
839 s[1] = current->pid;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100840 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100841 p->c_arg.fh = &p->o_res.fh;
842 p->c_arg.stateid = &p->o_res.stateid;
843 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400844 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400845 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100846 return p;
847err_free:
848 kfree(p);
849err:
850 dput(parent);
851 return NULL;
852}
853
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400854static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100855{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400856 struct nfs4_opendata *p = container_of(kref,
857 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400858 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400859
860 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400861 if (p->state != NULL)
862 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400863 nfs4_put_state_owner(p->owner);
864 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400865 dput(p->dentry);
866 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500867 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400868 kfree(p);
869}
870
871static void nfs4_opendata_put(struct nfs4_opendata *p)
872{
873 if (p != NULL)
874 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100875}
876
Trond Myklebust06f814a2006-01-03 09:55:07 +0100877static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
878{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100879 int ret;
880
Trond Myklebust06f814a2006-01-03 09:55:07 +0100881 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100882 return ret;
883}
884
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500885static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400886{
887 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500888
Trond Myklebust536e43d2012-01-17 22:04:26 -0500889 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500890 goto out;
891 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400892 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500893 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
894 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400895 break;
896 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500897 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
898 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400899 break;
900 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500901 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
902 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400903 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500904out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400905 return ret;
906}
907
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500908static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400909{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500910 if (delegation == NULL)
911 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500912 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400913 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500914 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400915 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500916 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400917 return 1;
918}
919
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500920static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100921{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500922 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100923 case FMODE_WRITE:
924 state->n_wronly++;
925 break;
926 case FMODE_READ:
927 state->n_rdonly++;
928 break;
929 case FMODE_READ|FMODE_WRITE:
930 state->n_rdwr++;
931 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500932 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100933}
934
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500935static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400936{
937 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
938 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
939 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500940 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400941 case FMODE_READ:
942 set_bit(NFS_O_RDONLY_STATE, &state->flags);
943 break;
944 case FMODE_WRITE:
945 set_bit(NFS_O_WRONLY_STATE, &state->flags);
946 break;
947 case FMODE_READ|FMODE_WRITE:
948 set_bit(NFS_O_RDWR_STATE, &state->flags);
949 }
950}
951
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500952static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400953{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400954 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500955 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400956 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400957}
958
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500959static 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 -0700960{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400961 /*
962 * Protect the call to nfs4_state_set_mode_locked and
963 * serialise the stateid update
964 */
965 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400966 if (deleg_stateid != NULL) {
967 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
968 set_bit(NFS_DELEGATED_STATE, &state->flags);
969 }
970 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500971 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400972 write_sequnlock(&state->seqlock);
973 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500974 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700975 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976}
977
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500978static 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 -0500979{
980 struct nfs_inode *nfsi = NFS_I(state->inode);
981 struct nfs_delegation *deleg_cur;
982 int ret = 0;
983
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500984 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500985
986 rcu_read_lock();
987 deleg_cur = rcu_dereference(nfsi->delegation);
988 if (deleg_cur == NULL)
989 goto no_delegation;
990
991 spin_lock(&deleg_cur->lock);
992 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500993 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500994 goto no_delegation_unlock;
995
996 if (delegation == NULL)
997 delegation = &deleg_cur->stateid;
998 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
999 goto no_delegation_unlock;
1000
Trond Myklebustb7391f42008-12-23 15:21:52 -05001001 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001002 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001003 ret = 1;
1004no_delegation_unlock:
1005 spin_unlock(&deleg_cur->lock);
1006no_delegation:
1007 rcu_read_unlock();
1008
1009 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001010 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001011 ret = 1;
1012 }
1013
1014 return ret;
1015}
1016
1017
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001018static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001019{
1020 struct nfs_delegation *delegation;
1021
1022 rcu_read_lock();
1023 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001024 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001025 rcu_read_unlock();
1026 return;
1027 }
1028 rcu_read_unlock();
1029 nfs_inode_return_delegation(inode);
1030}
1031
Trond Myklebust6ee41262007-07-08 14:11:36 -04001032static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001033{
1034 struct nfs4_state *state = opendata->state;
1035 struct nfs_inode *nfsi = NFS_I(state->inode);
1036 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -05001037 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001038 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001039 nfs4_stateid stateid;
1040 int ret = -EAGAIN;
1041
Trond Myklebustaac00a82007-07-05 19:02:21 -04001042 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001043 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001044 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001045 if (can_open_cached(state, fmode, open_mode)) {
1046 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001047 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001048 goto out_return_state;
1049 }
1050 spin_unlock(&state->owner->so_lock);
1051 }
Trond Myklebust34310432008-12-23 15:21:38 -05001052 rcu_read_lock();
1053 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001054 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001055 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001056 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001057 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001058 /* Save the delegation */
1059 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
1060 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001061 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001062 if (ret != 0)
1063 goto out;
1064 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001065
1066 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001067 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001068 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001069 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001070out:
1071 return ERR_PTR(ret);
1072out_return_state:
1073 atomic_inc(&state->count);
1074 return state;
1075}
1076
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001077static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1078{
1079 struct inode *inode;
1080 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -04001081 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001082 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001083
Trond Myklebustaac00a82007-07-05 19:02:21 -04001084 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001085 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001086 goto out;
1087 }
1088
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001089 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001090 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001091 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001092 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001093 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001094 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001095 goto err;
1096 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001097 state = nfs4_get_open_state(inode, data->owner);
1098 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001099 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001100 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001101 int delegation_flags = 0;
1102
Trond Myklebust003707c2007-07-05 18:07:55 -04001103 rcu_read_lock();
1104 delegation = rcu_dereference(NFS_I(inode)->delegation);
1105 if (delegation)
1106 delegation_flags = delegation->flags;
1107 rcu_read_unlock();
Trond Myklebust652f89f2011-12-09 19:05:58 -05001108 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1109 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1110 "returning a delegation for "
1111 "OPEN(CLAIM_DELEGATE_CUR)\n",
1112 NFS_CLIENT(inode)->cl_server);
1113 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001114 nfs_inode_set_delegation(state->inode,
1115 data->owner->so_cred,
1116 &data->o_res);
1117 else
1118 nfs_inode_reclaim_delegation(state->inode,
1119 data->owner->so_cred,
1120 &data->o_res);
1121 }
Trond Myklebust34310432008-12-23 15:21:38 -05001122
1123 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001124 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001125 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001126out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001127 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001128err_put_inode:
1129 iput(inode);
1130err:
1131 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001132}
1133
Trond Myklebust864472e2006-01-03 09:55:15 +01001134static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1135{
1136 struct nfs_inode *nfsi = NFS_I(state->inode);
1137 struct nfs_open_context *ctx;
1138
1139 spin_lock(&state->inode->i_lock);
1140 list_for_each_entry(ctx, &nfsi->open_files, list) {
1141 if (ctx->state != state)
1142 continue;
1143 get_nfs_open_context(ctx);
1144 spin_unlock(&state->inode->i_lock);
1145 return ctx;
1146 }
1147 spin_unlock(&state->inode->i_lock);
1148 return ERR_PTR(-ENOENT);
1149}
1150
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001151static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1152{
1153 struct nfs4_opendata *opendata;
1154
Al Viro3d4ff432011-06-22 18:40:12 -04001155 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001156 if (opendata == NULL)
1157 return ERR_PTR(-ENOMEM);
1158 opendata->state = state;
1159 atomic_inc(&state->count);
1160 return opendata;
1161}
1162
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001163static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001164{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001165 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001166 int ret;
1167
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001168 opendata->o_arg.open_flags = 0;
1169 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001170 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1171 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1172 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001173 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001174 if (ret != 0)
1175 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001176 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001177 if (IS_ERR(newstate))
1178 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001179 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001180 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001181 return 0;
1182}
1183
1184static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1185{
Trond Myklebust864472e2006-01-03 09:55:15 +01001186 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001187 int ret;
1188
1189 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001190 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001191 smp_rmb();
1192 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001193 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001194 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001195 if (ret != 0)
1196 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001197 if (newstate != state)
1198 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001199 }
1200 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001201 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001202 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001203 if (ret != 0)
1204 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001205 if (newstate != state)
1206 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001207 }
1208 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001209 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001210 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001211 if (ret != 0)
1212 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001213 if (newstate != state)
1214 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001215 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001216 /*
1217 * We may have performed cached opens for all three recoveries.
1218 * Check if we need to update the current stateid.
1219 */
1220 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1221 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001222 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001223 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1224 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001225 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001226 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001227 return 0;
1228}
1229
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230/*
1231 * OPEN_RECLAIM:
1232 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001234static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001236 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001237 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001238 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 int status;
1240
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001241 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1242 if (IS_ERR(opendata))
1243 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001244 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1245 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001246 rcu_read_lock();
1247 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001248 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001249 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001250 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001251 opendata->o_arg.u.delegation_type = delegation_type;
1252 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001253 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 return status;
1255}
1256
Trond Myklebust539cd032007-06-05 11:46:42 -04001257static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258{
1259 struct nfs_server *server = NFS_SERVER(state->inode);
1260 struct nfs4_exception exception = { };
1261 int err;
1262 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001263 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001264 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001265 break;
1266 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 } while (exception.retry);
1268 return err;
1269}
1270
Trond Myklebust864472e2006-01-03 09:55:15 +01001271static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1272{
1273 struct nfs_open_context *ctx;
1274 int ret;
1275
1276 ctx = nfs4_state_find_open_context(state);
1277 if (IS_ERR(ctx))
1278 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001279 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001280 put_nfs_open_context(ctx);
1281 return ret;
1282}
1283
Trond Myklebust13437e12007-07-06 15:10:43 -04001284static 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 -07001285{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001286 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001287 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001289 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1290 if (IS_ERR(opendata))
1291 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001292 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -04001293 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001294 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +01001295 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001296 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001297 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298}
1299
Trond Myklebust13437e12007-07-06 15:10:43 -04001300int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301{
1302 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001303 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 int err;
1305 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001306 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 switch (err) {
1308 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001309 case -ENOENT:
1310 case -ESTALE:
1311 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001312 case -NFS4ERR_BADSESSION:
1313 case -NFS4ERR_BADSLOT:
1314 case -NFS4ERR_BAD_HIGH_SLOT:
1315 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1316 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001317 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001318 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 case -NFS4ERR_STALE_CLIENTID:
1320 case -NFS4ERR_STALE_STATEID:
1321 case -NFS4ERR_EXPIRED:
1322 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001323 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001324 goto out;
1325 case -ERESTARTSYS:
1326 /*
1327 * The show must go on: exit, but mark the
1328 * stateid as needing recovery.
1329 */
1330 case -NFS4ERR_ADMIN_REVOKED:
1331 case -NFS4ERR_BAD_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001332 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001333 case -EKEYEXPIRED:
1334 /*
1335 * User RPCSEC_GSS context has expired.
1336 * We cannot recover this stateid now, so
1337 * skip it and allow recovery thread to
1338 * proceed.
1339 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001340 case -ENOMEM:
1341 err = 0;
1342 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 }
1344 err = nfs4_handle_exception(server, err, &exception);
1345 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001346out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 return err;
1348}
1349
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001350static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1351{
1352 struct nfs4_opendata *data = calldata;
1353
1354 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001355 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001356 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1357 sizeof(data->o_res.stateid.data));
Trond Myklebustbb226292008-01-02 15:19:18 -05001358 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001359 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001360 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001361 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001362}
1363
1364static void nfs4_open_confirm_release(void *calldata)
1365{
1366 struct nfs4_opendata *data = calldata;
1367 struct nfs4_state *state = NULL;
1368
1369 /* If this request hasn't been cancelled, do nothing */
1370 if (data->cancelled == 0)
1371 goto out_free;
1372 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001373 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001374 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001375 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001376 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001377 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001378out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001379 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001380}
1381
1382static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001383 .rpc_call_done = nfs4_open_confirm_done,
1384 .rpc_release = nfs4_open_confirm_release,
1385};
1386
1387/*
1388 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1389 */
1390static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1391{
1392 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1393 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001394 struct rpc_message msg = {
1395 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1396 .rpc_argp = &data->c_arg,
1397 .rpc_resp = &data->c_res,
1398 .rpc_cred = data->owner->so_cred,
1399 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001400 struct rpc_task_setup task_setup_data = {
1401 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001402 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001403 .callback_ops = &nfs4_open_confirm_ops,
1404 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001405 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001406 .flags = RPC_TASK_ASYNC,
1407 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 int status;
1409
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001410 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001411 data->rpc_done = 0;
1412 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001413 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001414 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001415 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001416 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001417 status = nfs4_wait_for_completion_rpc_task(task);
1418 if (status != 0) {
1419 data->cancelled = 1;
1420 smp_wmb();
1421 } else
1422 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001423 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 return status;
1425}
1426
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001427static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001429 struct nfs4_opendata *data = calldata;
1430 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001431
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001432 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1433 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001434 /*
1435 * Check if we still need to send an OPEN call, or if we can use
1436 * a delegation instead.
1437 */
1438 if (data->state != NULL) {
1439 struct nfs_delegation *delegation;
1440
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001441 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001442 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001443 rcu_read_lock();
1444 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001445 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1446 can_open_delegated(delegation, data->o_arg.fmode))
1447 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001448 rcu_read_unlock();
1449 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001450 /* Update sequence id. */
Trond Myklebust48c22eb2012-01-17 22:04:25 -05001451 data->o_arg.id = sp->so_seqid.owner_id;
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001452 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001453 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001454 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001455 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1456 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001457 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001458 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001459 &data->o_arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05001460 &data->o_res.seq_res, task))
Andy Adamsond8985282009-04-01 09:22:21 -04001461 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001462 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001463 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001464unlock_no_action:
1465 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001466out_no_action:
1467 task->tk_action = NULL;
1468
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001469}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001471static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1472{
1473 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1474 nfs4_open_prepare(task, calldata);
1475}
1476
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001477static void nfs4_open_done(struct rpc_task *task, void *calldata)
1478{
1479 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001481 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001482
Trond Myklebust14516c32010-07-31 14:29:06 -04001483 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1484 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001485
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001486 if (task->tk_status == 0) {
1487 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001488 case S_IFREG:
1489 break;
1490 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001491 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001492 break;
1493 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001494 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001495 break;
1496 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001497 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001498 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001499 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001500 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1501 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001502 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001503 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001504}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001505
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001506static void nfs4_open_release(void *calldata)
1507{
1508 struct nfs4_opendata *data = calldata;
1509 struct nfs4_state *state = NULL;
1510
1511 /* If this request hasn't been cancelled, do nothing */
1512 if (data->cancelled == 0)
1513 goto out_free;
1514 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001515 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001516 goto out_free;
1517 /* In case we need an open_confirm, no cleanup! */
1518 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1519 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001520 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001521 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001522 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001523out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001524 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001525}
1526
1527static const struct rpc_call_ops nfs4_open_ops = {
1528 .rpc_call_prepare = nfs4_open_prepare,
1529 .rpc_call_done = nfs4_open_done,
1530 .rpc_release = nfs4_open_release,
1531};
1532
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001533static const struct rpc_call_ops nfs4_recover_open_ops = {
1534 .rpc_call_prepare = nfs4_recover_open_prepare,
1535 .rpc_call_done = nfs4_open_done,
1536 .rpc_release = nfs4_open_release,
1537};
1538
1539static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001540{
1541 struct inode *dir = data->dir->d_inode;
1542 struct nfs_server *server = NFS_SERVER(dir);
1543 struct nfs_openargs *o_arg = &data->o_arg;
1544 struct nfs_openres *o_res = &data->o_res;
1545 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001546 struct rpc_message msg = {
1547 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1548 .rpc_argp = o_arg,
1549 .rpc_resp = o_res,
1550 .rpc_cred = data->owner->so_cred,
1551 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001552 struct rpc_task_setup task_setup_data = {
1553 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001554 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001555 .callback_ops = &nfs4_open_ops,
1556 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001557 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001558 .flags = RPC_TASK_ASYNC,
1559 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001560 int status;
1561
Trond Myklebust9d12b212012-01-17 22:04:25 -05001562 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001563 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001564 data->rpc_done = 0;
1565 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001566 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001567 if (isrecover)
1568 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001569 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001570 if (IS_ERR(task))
1571 return PTR_ERR(task);
1572 status = nfs4_wait_for_completion_rpc_task(task);
1573 if (status != 0) {
1574 data->cancelled = 1;
1575 smp_wmb();
1576 } else
1577 status = data->rpc_status;
1578 rpc_put_task(task);
1579
1580 return status;
1581}
1582
1583static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1584{
1585 struct inode *dir = data->dir->d_inode;
1586 struct nfs_openres *o_res = &data->o_res;
1587 int status;
1588
1589 status = nfs4_run_open_task(data, 1);
1590 if (status != 0 || !data->rpc_done)
1591 return status;
1592
Trond Myklebust6926afd2012-01-07 13:22:46 -05001593 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1594
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001595 nfs_refresh_inode(dir, o_res->dir_attr);
1596
1597 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1598 status = _nfs4_proc_open_confirm(data);
1599 if (status != 0)
1600 return status;
1601 }
1602
1603 return status;
1604}
1605
1606/*
1607 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1608 */
1609static int _nfs4_proc_open(struct nfs4_opendata *data)
1610{
1611 struct inode *dir = data->dir->d_inode;
1612 struct nfs_server *server = NFS_SERVER(dir);
1613 struct nfs_openargs *o_arg = &data->o_arg;
1614 struct nfs_openres *o_res = &data->o_res;
1615 int status;
1616
1617 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001618 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001619 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001620 if (status != 0) {
1621 if (status == -NFS4ERR_BADNAME &&
1622 !(o_arg->open_flags & O_CREAT))
1623 return -ENOENT;
1624 return status;
1625 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001626
Trond Myklebust6926afd2012-01-07 13:22:46 -05001627 nfs_fattr_map_and_free_names(server, &data->f_attr);
1628
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001629 if (o_arg->open_flags & O_CREAT) {
1630 update_changeattr(dir, &o_res->cinfo);
1631 nfs_post_op_update_inode(dir, o_res->dir_attr);
1632 } else
1633 nfs_refresh_inode(dir, o_res->dir_attr);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001634 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1635 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001637 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001639 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 }
1641 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001642 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001643 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644}
1645
Andy Adamsond83217c2011-03-01 01:34:17 +00001646static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +01001647{
Trond Myklebusta78cb572009-08-09 15:06:19 -04001648 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001649 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001650
Trond Myklebusta78cb572009-08-09 15:06:19 -04001651 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001652 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001653 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001654 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001655 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1656 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001657 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001658 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001659 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001660 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001661 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001662}
1663
Andy Adamsond83217c2011-03-01 01:34:17 +00001664static int nfs4_recover_expired_lease(struct nfs_server *server)
1665{
1666 return nfs4_client_recover_expired_lease(server->nfs_client);
1667}
1668
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669/*
1670 * OPEN_EXPIRED:
1671 * reclaim state on the server after a network partition.
1672 * Assumes caller holds the appropriate lock
1673 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001674static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001676 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001677 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001679 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1680 if (IS_ERR(opendata))
1681 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001682 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001683 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001684 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001685 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001686 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687}
1688
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001689static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001690{
Trond Myklebust539cd032007-06-05 11:46:42 -04001691 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001692 struct nfs4_exception exception = { };
1693 int err;
1694
1695 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001696 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001697 switch (err) {
1698 default:
1699 goto out;
1700 case -NFS4ERR_GRACE:
1701 case -NFS4ERR_DELAY:
1702 nfs4_handle_exception(server, err, &exception);
1703 err = 0;
1704 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001705 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001706out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001707 return err;
1708}
1709
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1711{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001713 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
Trond Myklebust864472e2006-01-03 09:55:15 +01001715 ctx = nfs4_state_find_open_context(state);
1716 if (IS_ERR(ctx))
1717 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001718 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001719 put_nfs_open_context(ctx);
1720 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721}
1722
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001723#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001724static int nfs41_check_expired_stateid(struct nfs4_state *state, nfs4_stateid *stateid, unsigned int flags)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001725{
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001726 int status = NFS_OK;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001727 struct nfs_server *server = NFS_SERVER(state->inode);
1728
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001729 if (state->flags & flags) {
1730 status = nfs41_test_stateid(server, stateid);
1731 if (status != NFS_OK) {
1732 nfs41_free_stateid(server, stateid);
1733 state->flags &= ~flags;
1734 }
1735 }
1736 return status;
1737}
1738
1739static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1740{
1741 int deleg_status, open_status;
1742 int deleg_flags = 1 << NFS_DELEGATED_STATE;
1743 int open_flags = (1 << NFS_O_RDONLY_STATE) | (1 << NFS_O_WRONLY_STATE) | (1 << NFS_O_RDWR_STATE);
1744
1745 deleg_status = nfs41_check_expired_stateid(state, &state->stateid, deleg_flags);
1746 open_status = nfs41_check_expired_stateid(state, &state->open_stateid, open_flags);
1747
1748 if ((deleg_status == NFS_OK) && (open_status == NFS_OK))
1749 return NFS_OK;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001750 return nfs4_open_expired(sp, state);
1751}
1752#endif
1753
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001755 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1756 * fields corresponding to attributes that were used to store the verifier.
1757 * Make sure we clobber those fields in the later setattr call
1758 */
1759static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1760{
1761 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1762 !(sattr->ia_valid & ATTR_ATIME_SET))
1763 sattr->ia_valid |= ATTR_ATIME;
1764
1765 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1766 !(sattr->ia_valid & ATTR_MTIME_SET))
1767 sattr->ia_valid |= ATTR_MTIME;
1768}
1769
1770/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001771 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 */
Al Viro82a2c1b2011-06-22 18:30:55 -04001773static int _nfs4_do_open(struct inode *dir, struct dentry *dentry, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774{
1775 struct nfs4_state_owner *sp;
1776 struct nfs4_state *state = NULL;
1777 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001778 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001779 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
1781 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001783 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1784 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1786 goto out_err;
1787 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001788 status = nfs4_recover_expired_lease(server);
1789 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001790 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001791 if (dentry->d_inode != NULL)
1792 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001793 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001794 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001795 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001796 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
Al Viro82a2c1b2011-06-22 18:30:55 -04001798 if (dentry->d_inode != NULL)
1799 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001800
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001801 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001803 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001805 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001806 status = PTR_ERR(state);
1807 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001808 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001809 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001810 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001811
1812 if (opendata->o_arg.open_flags & O_EXCL) {
1813 nfs4_exclusive_attrset(opendata, sattr);
1814
1815 nfs_fattr_init(opendata->o_res.f_attr);
1816 status = nfs4_do_setattr(state->inode, cred,
1817 opendata->o_res.f_attr, sattr,
1818 state);
1819 if (status == 0)
1820 nfs_setattr_update_inode(state->inode, sattr);
1821 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1822 }
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001823 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 *res = state;
1826 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001827err_opendata_put:
1828 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001829err_put_state_owner:
1830 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 *res = NULL;
1833 return status;
1834}
1835
1836
Al Viro82a2c1b2011-06-22 18:30:55 -04001837static struct nfs4_state *nfs4_do_open(struct inode *dir, struct dentry *dentry, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838{
1839 struct nfs4_exception exception = { };
1840 struct nfs4_state *res;
1841 int status;
1842
1843 do {
Al Viro82a2c1b2011-06-22 18:30:55 -04001844 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 if (status == 0)
1846 break;
1847 /* NOTE: BAD_SEQID means the server and client disagree about the
1848 * book-keeping w.r.t. state-changing operations
1849 * (OPEN/CLOSE/LOCK/LOCKU...)
1850 * It is actually a sign of a bug on the client or on the server.
1851 *
1852 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001853 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 * have unhashed the old state_owner for us, and that we can
1855 * therefore safely retry using a new one. We should still warn
1856 * the user though...
1857 */
1858 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001859 printk(KERN_WARNING "NFS: v4 server %s "
1860 " returned a bad sequence-id error!\n",
1861 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 exception.retry = 1;
1863 continue;
1864 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001865 /*
1866 * BAD_STATEID on OPEN means that the server cancelled our
1867 * state before it received the OPEN_CONFIRM.
1868 * Recover by retrying the request as per the discussion
1869 * on Page 181 of RFC3530.
1870 */
1871 if (status == -NFS4ERR_BAD_STATEID) {
1872 exception.retry = 1;
1873 continue;
1874 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001875 if (status == -EAGAIN) {
1876 /* We must have found a delegation */
1877 exception.retry = 1;
1878 continue;
1879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1881 status, &exception));
1882 } while (exception.retry);
1883 return res;
1884}
1885
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001886static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1887 struct nfs_fattr *fattr, struct iattr *sattr,
1888 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001890 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001892 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 .iap = sattr,
1894 .server = server,
1895 .bitmask = server->attr_bitmask,
1896 };
1897 struct nfs_setattrres res = {
1898 .fattr = fattr,
1899 .server = server,
1900 };
1901 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001902 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1903 .rpc_argp = &arg,
1904 .rpc_resp = &res,
1905 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001907 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001908 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909
Trond Myklebust0e574af2005-10-27 22:12:38 -04001910 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001912 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1913 /* Use that stateid */
1914 } else if (state != NULL) {
Trond Myklebust77041ed2010-07-01 12:49:11 -04001915 nfs4_copy_stateid(&arg.stateid, state, current->files, current->tgid);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001916 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1918
Bryan Schumaker7c513052011-03-24 17:12:24 +00001919 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001920 if (status == 0 && state != NULL)
1921 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001922 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923}
1924
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001925static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1926 struct nfs_fattr *fattr, struct iattr *sattr,
1927 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001929 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 struct nfs4_exception exception = { };
1931 int err;
1932 do {
1933 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001934 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 &exception);
1936 } while (exception.retry);
1937 return err;
1938}
1939
1940struct nfs4_closedata {
1941 struct inode *inode;
1942 struct nfs4_state *state;
1943 struct nfs_closeargs arg;
1944 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001945 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001946 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00001947 bool roc;
1948 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949};
1950
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001951static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001952{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001953 struct nfs4_closedata *calldata = data;
1954 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04001955 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07001956
Fred Isamanf7e89172011-01-06 11:36:32 +00001957 if (calldata->roc)
1958 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07001959 nfs4_put_open_state(calldata->state);
1960 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001961 nfs4_put_state_owner(sp);
Al Viro643168c2011-06-22 18:20:23 -04001962 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07001963 kfree(calldata);
1964}
1965
Trond Myklebust88069f72009-12-08 08:33:16 -05001966static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1967 fmode_t fmode)
1968{
1969 spin_lock(&state->owner->so_lock);
1970 if (!(fmode & FMODE_READ))
1971 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1972 if (!(fmode & FMODE_WRITE))
1973 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1974 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1975 spin_unlock(&state->owner->so_lock);
1976}
1977
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001978static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001980 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 struct nfs_server *server = NFS_SERVER(calldata->inode);
1983
Trond Myklebust14516c32010-07-31 14:29:06 -04001984 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
1985 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 /* hmm. we are done with the inode, and in the process of freeing
1987 * the state_owner. we keep this around to process errors
1988 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 switch (task->tk_status) {
1990 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00001991 if (calldata->roc)
1992 pnfs_roc_set_barrier(state->inode,
1993 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04001994 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001995 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05001996 nfs4_close_clear_stateid_flags(state,
1997 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 break;
1999 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002000 case -NFS4ERR_OLD_STATEID:
2001 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002003 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002004 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002006 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2007 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002009 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002010 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011}
2012
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002013static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002015 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002016 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05002017 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002018
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002019 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07002020 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04002021
Trond Myklebust88069f72009-12-08 08:33:16 -05002022 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2023 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002024 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002025 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002026 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002027 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002028 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2029 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2030 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002031 }
2032 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002033 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2034 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2035 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002036 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002037 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002038 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002039
2040 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002041 /* Note: exit _without_ calling nfs4_close_done */
2042 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07002043 return;
2044 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002045
Fred Isamanf7e89172011-01-06 11:36:32 +00002046 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002047 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002048 if (calldata->roc &&
2049 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
2050 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
2051 task, NULL);
2052 return;
2053 }
2054 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002055
Trond Myklebust516a6af2005-10-27 22:12:41 -04002056 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002057 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04002058 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002059 &calldata->arg.seq_args,
2060 &calldata->res.seq_res,
2061 task))
Andy Adamson19ddab02009-04-01 09:22:20 -04002062 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002063 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064}
2065
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002066static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002067 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002068 .rpc_call_done = nfs4_close_done,
2069 .rpc_release = nfs4_free_closedata,
2070};
2071
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072/*
2073 * It is possible for data to be read/written from a mem-mapped file
2074 * after the sys_close call (which hits the vfs layer as a flush).
2075 * This means that we can't safely call nfsv4 close on a file until
2076 * the inode is cleared. This in turn means that we are not good
2077 * NFSv4 citizens - we do not indicate to the server to update the file's
2078 * share state even when we are done with one of the three share
2079 * stateid's in the inode.
2080 *
2081 * NOTE: Caller must be holding the sp->so_owner semaphore!
2082 */
Al Viro643168c2011-06-22 18:20:23 -04002083int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002085 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002087 struct nfs4_state_owner *sp = state->owner;
2088 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002089 struct rpc_message msg = {
2090 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2091 .rpc_cred = state->owner->so_cred,
2092 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002093 struct rpc_task_setup task_setup_data = {
2094 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002095 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002096 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002097 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002098 .flags = RPC_TASK_ASYNC,
2099 };
Trond Myklebust95121352005-10-18 14:20:12 -07002100 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002102 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002104 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002105 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002106 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002108 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002109 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002111 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002112 if (calldata->arg.seqid == NULL)
2113 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002114 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002115 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002116 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002117 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002118 calldata->res.server = server;
Fred Isamanf7e89172011-01-06 11:36:32 +00002119 calldata->roc = roc;
Al Viro643168c2011-06-22 18:20:23 -04002120 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002121
Trond Myklebust1174dd12010-12-21 10:52:24 -05002122 msg.rpc_argp = &calldata->arg;
2123 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002124 task_setup_data.callback_data = calldata;
2125 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002126 if (IS_ERR(task))
2127 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002128 status = 0;
2129 if (wait)
2130 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002131 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002132 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002133out_free_calldata:
2134 kfree(calldata);
2135out:
Fred Isamanf7e89172011-01-06 11:36:32 +00002136 if (roc)
2137 pnfs_roc_release(state->inode);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002138 nfs4_put_open_state(state);
2139 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002140 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141}
2142
Trond Myklebust2b484292010-09-17 10:56:51 -04002143static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002144nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 struct nfs4_state *state;
2147
Trond Myklebust565277f2007-10-15 18:17:53 -04002148 /* Protect against concurrent sillydeletes */
Al Viro3d4ff432011-06-22 18:40:12 -04002149 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr, ctx->cred);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002150 if (IS_ERR(state))
2151 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002152 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002153 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154}
2155
Trond Myklebust1185a552009-12-03 15:54:02 -05002156static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002157{
2158 if (ctx->state == NULL)
2159 return;
2160 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002161 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002162 else
Al Viro643168c2011-06-22 18:20:23 -04002163 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002164}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
2166static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2167{
Benny Halevy43652ad2009-04-01 09:21:54 -04002168 struct nfs4_server_caps_arg args = {
2169 .fhandle = fhandle,
2170 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 struct nfs4_server_caps_res res = {};
2172 struct rpc_message msg = {
2173 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002174 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 .rpc_resp = &res,
2176 };
2177 int status;
2178
Bryan Schumaker7c513052011-03-24 17:12:24 +00002179 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 if (status == 0) {
2181 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04002182 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2183 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2184 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2185 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2186 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2188 server->caps |= NFS_CAP_ACLS;
2189 if (res.has_links != 0)
2190 server->caps |= NFS_CAP_HARDLINKS;
2191 if (res.has_symlinks != 0)
2192 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04002193 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2194 server->caps |= NFS_CAP_FILEID;
2195 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2196 server->caps |= NFS_CAP_MODE;
2197 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2198 server->caps |= NFS_CAP_NLINK;
2199 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2200 server->caps |= NFS_CAP_OWNER;
2201 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2202 server->caps |= NFS_CAP_OWNER_GROUP;
2203 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2204 server->caps |= NFS_CAP_ATIME;
2205 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2206 server->caps |= NFS_CAP_CTIME;
2207 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2208 server->caps |= NFS_CAP_MTIME;
2209
Trond Myklebusta65318b2009-03-11 14:10:28 -04002210 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2211 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2212 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 server->acl_bitmask = res.acl_bitmask;
2214 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002215
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 return status;
2217}
2218
Trond Myklebust55a97592006-06-09 09:34:19 -04002219int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220{
2221 struct nfs4_exception exception = { };
2222 int err;
2223 do {
2224 err = nfs4_handle_exception(server,
2225 _nfs4_server_capabilities(server, fhandle),
2226 &exception);
2227 } while (exception.retry);
2228 return err;
2229}
2230
2231static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2232 struct nfs_fsinfo *info)
2233{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 struct nfs4_lookup_root_arg args = {
2235 .bitmask = nfs4_fattr_bitmap,
2236 };
2237 struct nfs4_lookup_res res = {
2238 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002239 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 .fh = fhandle,
2241 };
2242 struct rpc_message msg = {
2243 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2244 .rpc_argp = &args,
2245 .rpc_resp = &res,
2246 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002247
Trond Myklebust0e574af2005-10-27 22:12:38 -04002248 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002249 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250}
2251
2252static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2253 struct nfs_fsinfo *info)
2254{
2255 struct nfs4_exception exception = { };
2256 int err;
2257 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002258 err = _nfs4_lookup_root(server, fhandle, info);
2259 switch (err) {
2260 case 0:
2261 case -NFS4ERR_WRONGSEC:
2262 break;
2263 default:
2264 err = nfs4_handle_exception(server, err, &exception);
2265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 } while (exception.retry);
2267 return err;
2268}
2269
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002270static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2271 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2272{
2273 struct rpc_auth *auth;
2274 int ret;
2275
2276 auth = rpcauth_create(flavor, server->client);
2277 if (!auth) {
2278 ret = -EIO;
2279 goto out;
2280 }
2281 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002282out:
2283 return ret;
2284}
2285
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002286static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002287 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002289 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002290 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002292 len = gss_mech_list_pseudoflavors(&flav_array[0]);
2293 flav_array[len] = RPC_AUTH_NULL;
2294 len += 1;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002295
2296 for (i = 0; i < len; i++) {
2297 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002298 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002299 continue;
2300 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002301 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002302 /*
2303 * -EACCESS could mean that the user doesn't have correct permissions
2304 * to access the mount. It could also mean that we tried to mount
2305 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2306 * existing mount programs don't handle -EACCES very well so it should
2307 * be mapped to -EPERM instead.
2308 */
2309 if (status == -EACCES)
2310 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002311 return status;
2312}
2313
2314/*
2315 * get the file handle for the "/" directory on the server
2316 */
2317static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2318 struct nfs_fsinfo *info)
2319{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002320 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002321 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002322 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2323 /*
2324 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2325 * by nfs4_map_errors() as this function exits.
2326 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002327 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 if (status == 0)
2329 status = nfs4_server_capabilities(server, fhandle);
2330 if (status == 0)
2331 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002332 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333}
2334
Andy Adamson533eb462011-06-13 18:25:56 -04002335static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002336/*
2337 * Get locations and (maybe) other attributes of a referral.
2338 * Note that we'll actually follow the referral later when
2339 * we detect fsid mismatch in inode revalidation
2340 */
Andy Adamson533eb462011-06-13 18:25:56 -04002341static int nfs4_get_referral(struct inode *dir, const struct qstr *name,
2342 struct nfs_fattr *fattr, struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002343{
2344 int status = -ENOMEM;
2345 struct page *page = NULL;
2346 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002347
2348 page = alloc_page(GFP_KERNEL);
2349 if (page == NULL)
2350 goto out;
2351 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2352 if (locations == NULL)
2353 goto out;
2354
Trond Myklebustc228fd32007-01-13 02:28:11 -05002355 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002356 if (status != 0)
2357 goto out;
2358 /* Make sure server returned a different fsid for the referral */
2359 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002360 dprintk("%s: server did not return a different fsid for"
2361 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002362 status = -EIO;
2363 goto out;
2364 }
Andy Adamson533eb462011-06-13 18:25:56 -04002365 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2366 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002367
Andy Adamson533eb462011-06-13 18:25:56 -04002368 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002369 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002370 memset(fhandle, 0, sizeof(struct nfs_fh));
2371out:
2372 if (page)
2373 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002374 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002375 return status;
2376}
2377
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2379{
2380 struct nfs4_getattr_arg args = {
2381 .fh = fhandle,
2382 .bitmask = server->attr_bitmask,
2383 };
2384 struct nfs4_getattr_res res = {
2385 .fattr = fattr,
2386 .server = server,
2387 };
2388 struct rpc_message msg = {
2389 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2390 .rpc_argp = &args,
2391 .rpc_resp = &res,
2392 };
2393
Trond Myklebust0e574af2005-10-27 22:12:38 -04002394 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002395 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396}
2397
2398static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2399{
2400 struct nfs4_exception exception = { };
2401 int err;
2402 do {
2403 err = nfs4_handle_exception(server,
2404 _nfs4_proc_getattr(server, fhandle, fattr),
2405 &exception);
2406 } while (exception.retry);
2407 return err;
2408}
2409
2410/*
2411 * The file is not closed if it is opened due to the a request to change
2412 * the size of the file. The open call will not be needed once the
2413 * VFS layer lookup-intents are implemented.
2414 *
2415 * Close is called when the inode is destroyed.
2416 * If we haven't opened the file for O_WRONLY, we
2417 * need to in the size_change case to obtain a stateid.
2418 *
2419 * Got race?
2420 * Because OPEN is always done by name in nfsv4, it is
2421 * possible that we opened a different file by the same
2422 * name. We can recognize this race condition, but we
2423 * can't do anything about it besides returning an error.
2424 *
2425 * This will be fixed with VFS changes (lookup-intent).
2426 */
2427static int
2428nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2429 struct iattr *sattr)
2430{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002431 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002432 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002433 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 int status;
2435
Benny Halevy8a1636c2010-07-14 15:43:57 -04002436 if (pnfs_ld_layoutret_on_setattr(inode))
2437 pnfs_return_layout(inode);
2438
Trond Myklebust0e574af2005-10-27 22:12:38 -04002439 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440
Trond Myklebustd5308382005-11-04 15:33:38 -05002441 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002442 if (sattr->ia_valid & ATTR_FILE) {
2443 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002444
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002445 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002446 if (ctx) {
2447 cred = ctx->cred;
2448 state = ctx->state;
2449 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002450 }
2451
Trond Myklebust536e43d2012-01-17 22:04:26 -05002452 /* Deal with open(O_TRUNC) */
2453 if (sattr->ia_valid & ATTR_OPEN)
2454 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2455
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002456 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002457 if (status == 0)
2458 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 return status;
2460}
2461
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002462static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2463 const struct qstr *name, struct nfs_fh *fhandle,
2464 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002465{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002466 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002467 int status;
2468 struct nfs4_lookup_arg args = {
2469 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002470 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002471 .name = name,
2472 };
2473 struct nfs4_lookup_res res = {
2474 .server = server,
2475 .fattr = fattr,
2476 .fh = fhandle,
2477 };
2478 struct rpc_message msg = {
2479 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2480 .rpc_argp = &args,
2481 .rpc_resp = &res,
2482 };
2483
2484 nfs_fattr_init(fattr);
2485
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002487 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 dprintk("NFS reply lookup: %d\n", status);
2489 return status;
2490}
2491
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002492void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr, struct nfs_fh *fh)
2493{
2494 memset(fh, 0, sizeof(struct nfs_fh));
2495 fattr->fsid.major = 1;
2496 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2497 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_FSID | NFS_ATTR_FATTR_MOUNTPOINT;
2498 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2499 fattr->nlink = 2;
2500}
2501
Bryan Schumaker7c513052011-03-24 17:12:24 +00002502static int nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir, struct qstr *name,
2503 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504{
2505 struct nfs4_exception exception = { };
2506 int err;
2507 do {
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002508 int status;
2509
2510 status = _nfs4_proc_lookup(clnt, dir, name, fhandle, fattr);
2511 switch (status) {
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002512 case -NFS4ERR_BADNAME:
2513 return -ENOENT;
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002514 case -NFS4ERR_MOVED:
Trond Myklebusta6f498a2011-11-07 15:50:32 -05002515 return nfs4_get_referral(dir, name, fattr, fhandle);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002516 case -NFS4ERR_WRONGSEC:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002517 nfs_fixup_secinfo_attributes(fattr, fhandle);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002518 }
2519 err = nfs4_handle_exception(NFS_SERVER(dir),
2520 status, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 } while (exception.retry);
2522 return err;
2523}
2524
2525static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2526{
Trond Myklebust76b32992007-08-10 17:45:11 -04002527 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 struct nfs4_accessargs args = {
2529 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002530 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002532 struct nfs4_accessres res = {
2533 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002534 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 struct rpc_message msg = {
2536 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2537 .rpc_argp = &args,
2538 .rpc_resp = &res,
2539 .rpc_cred = entry->cred,
2540 };
2541 int mode = entry->mask;
2542 int status;
2543
2544 /*
2545 * Determine which access bits we want to ask for...
2546 */
2547 if (mode & MAY_READ)
2548 args.access |= NFS4_ACCESS_READ;
2549 if (S_ISDIR(inode->i_mode)) {
2550 if (mode & MAY_WRITE)
2551 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2552 if (mode & MAY_EXEC)
2553 args.access |= NFS4_ACCESS_LOOKUP;
2554 } else {
2555 if (mode & MAY_WRITE)
2556 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2557 if (mode & MAY_EXEC)
2558 args.access |= NFS4_ACCESS_EXECUTE;
2559 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002560
2561 res.fattr = nfs_alloc_fattr();
2562 if (res.fattr == NULL)
2563 return -ENOMEM;
2564
Bryan Schumaker7c513052011-03-24 17:12:24 +00002565 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 if (!status) {
2567 entry->mask = 0;
2568 if (res.access & NFS4_ACCESS_READ)
2569 entry->mask |= MAY_READ;
2570 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2571 entry->mask |= MAY_WRITE;
2572 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2573 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002574 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002576 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 return status;
2578}
2579
2580static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2581{
2582 struct nfs4_exception exception = { };
2583 int err;
2584 do {
2585 err = nfs4_handle_exception(NFS_SERVER(inode),
2586 _nfs4_proc_access(inode, entry),
2587 &exception);
2588 } while (exception.retry);
2589 return err;
2590}
2591
2592/*
2593 * TODO: For the time being, we don't try to get any attributes
2594 * along with any of the zero-copy operations READ, READDIR,
2595 * READLINK, WRITE.
2596 *
2597 * In the case of the first three, we want to put the GETATTR
2598 * after the read-type operation -- this is because it is hard
2599 * to predict the length of a GETATTR response in v4, and thus
2600 * align the READ data correctly. This means that the GETATTR
2601 * may end up partially falling into the page cache, and we should
2602 * shift it into the 'tail' of the xdr_buf before processing.
2603 * To do this efficiently, we need to know the total length
2604 * of data received, which doesn't seem to be available outside
2605 * of the RPC layer.
2606 *
2607 * In the case of WRITE, we also want to put the GETATTR after
2608 * the operation -- in this case because we want to make sure
2609 * we get the post-operation mtime and size. This means that
2610 * we can't use xdr_encode_pages() as written: we need a variant
2611 * of it which would leave room in the 'tail' iovec.
2612 *
2613 * Both of these changes to the XDR layer would in fact be quite
2614 * minor, but I decided to leave them for a subsequent patch.
2615 */
2616static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2617 unsigned int pgbase, unsigned int pglen)
2618{
2619 struct nfs4_readlink args = {
2620 .fh = NFS_FH(inode),
2621 .pgbase = pgbase,
2622 .pglen = pglen,
2623 .pages = &page,
2624 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002625 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 struct rpc_message msg = {
2627 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2628 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002629 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 };
2631
Bryan Schumaker7c513052011-03-24 17:12:24 +00002632 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 -07002633}
2634
2635static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2636 unsigned int pgbase, unsigned int pglen)
2637{
2638 struct nfs4_exception exception = { };
2639 int err;
2640 do {
2641 err = nfs4_handle_exception(NFS_SERVER(inode),
2642 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2643 &exception);
2644 } while (exception.retry);
2645 return err;
2646}
2647
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648/*
2649 * Got race?
2650 * We will need to arrange for the VFS layer to provide an atomic open.
2651 * Until then, this create/open method is prone to inefficiency and race
2652 * conditions due to the lookup, create, and open VFS calls from sys_open()
2653 * placed on the wire.
2654 *
2655 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2656 * The file will be opened again in the subsequent VFS open call
2657 * (nfs4_proc_file_open).
2658 *
2659 * The open for read will just hang around to be used by any process that
2660 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2661 */
2662
2663static int
2664nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002665 int flags, struct nfs_open_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666{
Al Viro82a2c1b2011-06-22 18:30:55 -04002667 struct dentry *de = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 struct nfs4_state *state;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002669 struct rpc_cred *cred = NULL;
2670 fmode_t fmode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 int status = 0;
2672
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002673 if (ctx != NULL) {
2674 cred = ctx->cred;
Al Viro3d4ff432011-06-22 18:40:12 -04002675 de = ctx->dentry;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002676 fmode = ctx->mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 }
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002678 sattr->ia_mode &= ~current_umask();
Al Viro82a2c1b2011-06-22 18:30:55 -04002679 state = nfs4_do_open(dir, de, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002680 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 if (IS_ERR(state)) {
2682 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002683 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002685 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002686 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002687 if (ctx != NULL)
2688 ctx->state = state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002689 else
Al Viro643168c2011-06-22 18:20:23 -04002690 nfs4_close_sync(state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691out:
2692 return status;
2693}
2694
2695static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2696{
Trond Myklebust16e42952005-10-27 22:12:44 -04002697 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002698 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002700 .name.len = name->len,
2701 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002702 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002704 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002705 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002706 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002708 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2709 .rpc_argp = &args,
2710 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 };
Trond Myklebustd3468902010-04-16 16:22:50 -04002712 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713
Trond Myklebustd3468902010-04-16 16:22:50 -04002714 res.dir_attr = nfs_alloc_fattr();
2715 if (res.dir_attr == NULL)
2716 goto out;
2717
Bryan Schumaker7c513052011-03-24 17:12:24 +00002718 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002719 if (status == 0) {
2720 update_changeattr(dir, &res.cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002721 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002722 }
Trond Myklebustd3468902010-04-16 16:22:50 -04002723 nfs_free_fattr(res.dir_attr);
2724out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 return status;
2726}
2727
2728static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2729{
2730 struct nfs4_exception exception = { };
2731 int err;
2732 do {
2733 err = nfs4_handle_exception(NFS_SERVER(dir),
2734 _nfs4_proc_remove(dir, name),
2735 &exception);
2736 } while (exception.retry);
2737 return err;
2738}
2739
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002740static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002742 struct nfs_server *server = NFS_SERVER(dir);
2743 struct nfs_removeargs *args = msg->rpc_argp;
2744 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745
Trond Myklebusta65318b2009-03-11 14:10:28 -04002746 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002747 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05002749 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750}
2751
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002752static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002754 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2755
Trond Myklebust14516c32010-07-31 14:29:06 -04002756 if (!nfs4_sequence_done(task, &res->seq_res))
2757 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002758 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002759 return 0;
2760 update_changeattr(dir, &res->cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002761 nfs_post_op_update_inode(dir, res->dir_attr);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002762 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763}
2764
Jeff Laytond3d41522010-09-17 17:31:57 -04002765static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2766{
2767 struct nfs_server *server = NFS_SERVER(dir);
2768 struct nfs_renameargs *arg = msg->rpc_argp;
2769 struct nfs_renameres *res = msg->rpc_resp;
2770
2771 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2772 arg->bitmask = server->attr_bitmask;
2773 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002774 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04002775}
2776
2777static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2778 struct inode *new_dir)
2779{
2780 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2781
2782 if (!nfs4_sequence_done(task, &res->seq_res))
2783 return 0;
2784 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2785 return 0;
2786
2787 update_changeattr(old_dir, &res->old_cinfo);
2788 nfs_post_op_update_inode(old_dir, res->old_fattr);
2789 update_changeattr(new_dir, &res->new_cinfo);
2790 nfs_post_op_update_inode(new_dir, res->new_fattr);
2791 return 1;
2792}
2793
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2795 struct inode *new_dir, struct qstr *new_name)
2796{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002797 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002798 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 .old_dir = NFS_FH(old_dir),
2800 .new_dir = NFS_FH(new_dir),
2801 .old_name = old_name,
2802 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002803 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002805 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002806 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002807 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 struct rpc_message msg = {
2809 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2810 .rpc_argp = &arg,
2811 .rpc_resp = &res,
2812 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002813 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814
Trond Myklebust011fff72010-04-16 16:22:49 -04002815 res.old_fattr = nfs_alloc_fattr();
2816 res.new_fattr = nfs_alloc_fattr();
2817 if (res.old_fattr == NULL || res.new_fattr == NULL)
2818 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819
Bryan Schumaker7c513052011-03-24 17:12:24 +00002820 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 if (!status) {
2822 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002823 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002825 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 }
Trond Myklebust011fff72010-04-16 16:22:49 -04002827out:
2828 nfs_free_fattr(res.new_fattr);
2829 nfs_free_fattr(res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 return status;
2831}
2832
2833static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2834 struct inode *new_dir, struct qstr *new_name)
2835{
2836 struct nfs4_exception exception = { };
2837 int err;
2838 do {
2839 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2840 _nfs4_proc_rename(old_dir, old_name,
2841 new_dir, new_name),
2842 &exception);
2843 } while (exception.retry);
2844 return err;
2845}
2846
2847static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2848{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002849 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 struct nfs4_link_arg arg = {
2851 .fh = NFS_FH(inode),
2852 .dir_fh = NFS_FH(dir),
2853 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002854 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002856 struct nfs4_link_res res = {
2857 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002858 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 struct rpc_message msg = {
2860 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2861 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002862 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 };
Trond Myklebust136f2622010-04-16 16:22:49 -04002864 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865
Trond Myklebust136f2622010-04-16 16:22:49 -04002866 res.fattr = nfs_alloc_fattr();
2867 res.dir_attr = nfs_alloc_fattr();
2868 if (res.fattr == NULL || res.dir_attr == NULL)
2869 goto out;
2870
Bryan Schumaker7c513052011-03-24 17:12:24 +00002871 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002872 if (!status) {
2873 update_changeattr(dir, &res.cinfo);
2874 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002875 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002876 }
Trond Myklebust136f2622010-04-16 16:22:49 -04002877out:
2878 nfs_free_fattr(res.dir_attr);
2879 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 return status;
2881}
2882
2883static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2884{
2885 struct nfs4_exception exception = { };
2886 int err;
2887 do {
2888 err = nfs4_handle_exception(NFS_SERVER(inode),
2889 _nfs4_proc_link(inode, dir, name),
2890 &exception);
2891 } while (exception.retry);
2892 return err;
2893}
2894
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002895struct nfs4_createdata {
2896 struct rpc_message msg;
2897 struct nfs4_create_arg arg;
2898 struct nfs4_create_res res;
2899 struct nfs_fh fh;
2900 struct nfs_fattr fattr;
2901 struct nfs_fattr dir_fattr;
2902};
2903
2904static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2905 struct qstr *name, struct iattr *sattr, u32 ftype)
2906{
2907 struct nfs4_createdata *data;
2908
2909 data = kzalloc(sizeof(*data), GFP_KERNEL);
2910 if (data != NULL) {
2911 struct nfs_server *server = NFS_SERVER(dir);
2912
2913 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2914 data->msg.rpc_argp = &data->arg;
2915 data->msg.rpc_resp = &data->res;
2916 data->arg.dir_fh = NFS_FH(dir);
2917 data->arg.server = server;
2918 data->arg.name = name;
2919 data->arg.attrs = sattr;
2920 data->arg.ftype = ftype;
2921 data->arg.bitmask = server->attr_bitmask;
2922 data->res.server = server;
2923 data->res.fh = &data->fh;
2924 data->res.fattr = &data->fattr;
2925 data->res.dir_fattr = &data->dir_fattr;
2926 nfs_fattr_init(data->res.fattr);
2927 nfs_fattr_init(data->res.dir_fattr);
2928 }
2929 return data;
2930}
2931
2932static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2933{
Bryan Schumaker7c513052011-03-24 17:12:24 +00002934 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00002935 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002936 if (status == 0) {
2937 update_changeattr(dir, &data->res.dir_cinfo);
2938 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2939 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2940 }
2941 return status;
2942}
2943
2944static void nfs4_free_createdata(struct nfs4_createdata *data)
2945{
2946 kfree(data);
2947}
2948
Chuck Lever4f390c12006-08-22 20:06:22 -04002949static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002950 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002952 struct nfs4_createdata *data;
2953 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954
Chuck Lever94a6d752006-08-22 20:06:23 -04002955 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002956 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002957
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002958 status = -ENOMEM;
2959 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2960 if (data == NULL)
2961 goto out;
2962
2963 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2964 data->arg.u.symlink.pages = &page;
2965 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002967 status = nfs4_do_create(dir, dentry, data);
2968
2969 nfs4_free_createdata(data);
2970out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 return status;
2972}
2973
Chuck Lever4f390c12006-08-22 20:06:22 -04002974static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002975 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976{
2977 struct nfs4_exception exception = { };
2978 int err;
2979 do {
2980 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002981 _nfs4_proc_symlink(dir, dentry, page,
2982 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 &exception);
2984 } while (exception.retry);
2985 return err;
2986}
2987
2988static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2989 struct iattr *sattr)
2990{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002991 struct nfs4_createdata *data;
2992 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002994 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2995 if (data == NULL)
2996 goto out;
2997
2998 status = nfs4_do_create(dir, dentry, data);
2999
3000 nfs4_free_createdata(data);
3001out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 return status;
3003}
3004
3005static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3006 struct iattr *sattr)
3007{
3008 struct nfs4_exception exception = { };
3009 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003010
3011 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 do {
3013 err = nfs4_handle_exception(NFS_SERVER(dir),
3014 _nfs4_proc_mkdir(dir, dentry, sattr),
3015 &exception);
3016 } while (exception.retry);
3017 return err;
3018}
3019
3020static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003021 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022{
3023 struct inode *dir = dentry->d_inode;
3024 struct nfs4_readdir_arg args = {
3025 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003026 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 .pgbase = 0,
3028 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003029 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003030 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 };
3032 struct nfs4_readdir_res res;
3033 struct rpc_message msg = {
3034 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3035 .rpc_argp = &args,
3036 .rpc_resp = &res,
3037 .rpc_cred = cred,
3038 };
3039 int status;
3040
Harvey Harrison3110ff82008-05-02 13:42:44 -07003041 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003042 dentry->d_parent->d_name.name,
3043 dentry->d_name.name,
3044 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
3046 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003047 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 -05003048 if (status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003050 status += args.pgbase;
3051 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003052
3053 nfs_invalidate_atime(dir);
3054
Harvey Harrison3110ff82008-05-02 13:42:44 -07003055 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 return status;
3057}
3058
3059static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003060 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061{
3062 struct nfs4_exception exception = { };
3063 int err;
3064 do {
3065 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3066 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003067 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 &exception);
3069 } while (exception.retry);
3070 return err;
3071}
3072
3073static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3074 struct iattr *sattr, dev_t rdev)
3075{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003076 struct nfs4_createdata *data;
3077 int mode = sattr->ia_mode;
3078 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079
3080 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3081 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003082
3083 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3084 if (data == NULL)
3085 goto out;
3086
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003088 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003090 data->arg.ftype = NF4BLK;
3091 data->arg.u.device.specdata1 = MAJOR(rdev);
3092 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 }
3094 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003095 data->arg.ftype = NF4CHR;
3096 data->arg.u.device.specdata1 = MAJOR(rdev);
3097 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003100 status = nfs4_do_create(dir, dentry, data);
3101
3102 nfs4_free_createdata(data);
3103out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 return status;
3105}
3106
3107static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3108 struct iattr *sattr, dev_t rdev)
3109{
3110 struct nfs4_exception exception = { };
3111 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003112
3113 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 do {
3115 err = nfs4_handle_exception(NFS_SERVER(dir),
3116 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3117 &exception);
3118 } while (exception.retry);
3119 return err;
3120}
3121
3122static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3123 struct nfs_fsstat *fsstat)
3124{
3125 struct nfs4_statfs_arg args = {
3126 .fh = fhandle,
3127 .bitmask = server->attr_bitmask,
3128 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003129 struct nfs4_statfs_res res = {
3130 .fsstat = fsstat,
3131 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 struct rpc_message msg = {
3133 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3134 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003135 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 };
3137
Trond Myklebust0e574af2005-10-27 22:12:38 -04003138 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003139 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140}
3141
3142static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3143{
3144 struct nfs4_exception exception = { };
3145 int err;
3146 do {
3147 err = nfs4_handle_exception(server,
3148 _nfs4_proc_statfs(server, fhandle, fsstat),
3149 &exception);
3150 } while (exception.retry);
3151 return err;
3152}
3153
3154static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3155 struct nfs_fsinfo *fsinfo)
3156{
3157 struct nfs4_fsinfo_arg args = {
3158 .fh = fhandle,
3159 .bitmask = server->attr_bitmask,
3160 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003161 struct nfs4_fsinfo_res res = {
3162 .fsinfo = fsinfo,
3163 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 struct rpc_message msg = {
3165 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3166 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003167 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 };
3169
Bryan Schumaker7c513052011-03-24 17:12:24 +00003170 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171}
3172
3173static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3174{
3175 struct nfs4_exception exception = { };
3176 int err;
3177
3178 do {
3179 err = nfs4_handle_exception(server,
3180 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3181 &exception);
3182 } while (exception.retry);
3183 return err;
3184}
3185
3186static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3187{
Trond Myklebust0e574af2005-10-27 22:12:38 -04003188 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3190}
3191
3192static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3193 struct nfs_pathconf *pathconf)
3194{
3195 struct nfs4_pathconf_arg args = {
3196 .fh = fhandle,
3197 .bitmask = server->attr_bitmask,
3198 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003199 struct nfs4_pathconf_res res = {
3200 .pathconf = pathconf,
3201 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 struct rpc_message msg = {
3203 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3204 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003205 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 };
3207
3208 /* None of the pathconf attributes are mandatory to implement */
3209 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3210 memset(pathconf, 0, sizeof(*pathconf));
3211 return 0;
3212 }
3213
Trond Myklebust0e574af2005-10-27 22:12:38 -04003214 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003215 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216}
3217
3218static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3219 struct nfs_pathconf *pathconf)
3220{
3221 struct nfs4_exception exception = { };
3222 int err;
3223
3224 do {
3225 err = nfs4_handle_exception(server,
3226 _nfs4_proc_pathconf(server, fhandle, pathconf),
3227 &exception);
3228 } while (exception.retry);
3229 return err;
3230}
3231
Benny Halevyd20581a2011-05-22 19:52:03 +03003232void __nfs4_read_done_cb(struct nfs_read_data *data)
3233{
3234 nfs_invalidate_atime(data->inode);
3235}
3236
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003237static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238{
Trond Myklebustec06c092006-03-20 13:44:27 -05003239 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003241 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003242 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003243 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003245
Benny Halevyd20581a2011-05-22 19:52:03 +03003246 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003248 renew_lease(server, data->timestamp);
3249 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250}
3251
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003252static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3253{
3254
3255 dprintk("--> %s\n", __func__);
3256
3257 if (!nfs4_sequence_done(task, &data->res.seq_res))
3258 return -EAGAIN;
3259
Benny Halevyd20581a2011-05-22 19:52:03 +03003260 return data->read_done_cb ? data->read_done_cb(task, data) :
3261 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003262}
3263
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003264static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 data->timestamp = jiffies;
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003267 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003268 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003269 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270}
3271
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003272/* Reset the the nfs_read_data to send the read to the MDS. */
3273void nfs4_reset_read(struct rpc_task *task, struct nfs_read_data *data)
3274{
3275 dprintk("%s Reset task for i/o through\n", __func__);
3276 put_lseg(data->lseg);
3277 data->lseg = NULL;
3278 /* offsets will differ in the dense stripe case */
3279 data->args.offset = data->mds_offset;
3280 data->ds_clp = NULL;
3281 data->args.fh = NFS_FH(data->inode);
3282 data->read_done_cb = nfs4_read_done_cb;
3283 task->tk_ops = data->mds_ops;
3284 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3285}
3286EXPORT_SYMBOL_GPL(nfs4_reset_read);
3287
Fred Isamanb029bc92011-03-03 15:13:42 +00003288static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 struct inode *inode = data->inode;
3291
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003292 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003293 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003294 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003296 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003298 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003299 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003300 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301}
3302
Fred Isamanb029bc92011-03-03 15:13:42 +00003303static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3304{
3305 if (!nfs4_sequence_done(task, &data->res.seq_res))
3306 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003307 return data->write_done_cb ? data->write_done_cb(task, data) :
3308 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003309}
3310
Fred Isamana69aef12011-03-03 15:13:47 +00003311/* Reset the the nfs_write_data to send the write to the MDS. */
3312void nfs4_reset_write(struct rpc_task *task, struct nfs_write_data *data)
3313{
3314 dprintk("%s Reset task for i/o through\n", __func__);
3315 put_lseg(data->lseg);
3316 data->lseg = NULL;
3317 data->ds_clp = NULL;
3318 data->write_done_cb = nfs4_write_done_cb;
3319 data->args.fh = NFS_FH(data->inode);
3320 data->args.bitmask = data->res.server->cache_consistency_bitmask;
3321 data->args.offset = data->mds_offset;
3322 data->res.fattr = &data->fattr;
3323 task->tk_ops = data->mds_ops;
3324 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3325}
3326EXPORT_SYMBOL_GPL(nfs4_reset_write);
3327
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003328static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003330 struct nfs_server *server = NFS_SERVER(data->inode);
3331
Fred Isaman7ffd1062011-03-03 15:13:46 +00003332 if (data->lseg) {
3333 data->args.bitmask = NULL;
3334 data->res.fattr = NULL;
3335 } else
3336 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isamanb029bc92011-03-03 15:13:42 +00003337 if (!data->write_done_cb)
3338 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003339 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 data->timestamp = jiffies;
3341
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003342 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003343 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344}
3345
Fred Isaman5f452432011-03-23 13:27:46 +00003346static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003349
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003350 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003351 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003352 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003354 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003355 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356}
3357
Fred Isaman5f452432011-03-23 13:27:46 +00003358static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3359{
3360 if (!nfs4_sequence_done(task, &data->res.seq_res))
3361 return -EAGAIN;
3362 return data->write_done_cb(task, data);
3363}
3364
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003365static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003367 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003368
3369 if (data->lseg) {
3370 data->args.bitmask = NULL;
3371 data->res.fattr = NULL;
3372 } else
3373 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isaman5f452432011-03-23 13:27:46 +00003374 if (!data->write_done_cb)
3375 data->write_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003376 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003377 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003378 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379}
3380
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003381struct nfs4_renewdata {
3382 struct nfs_client *client;
3383 unsigned long timestamp;
3384};
3385
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386/*
3387 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3388 * standalone procedure for queueing an asynchronous RENEW.
3389 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003390static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003391{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003392 struct nfs4_renewdata *data = calldata;
3393 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003394
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003395 if (atomic_read(&clp->cl_count) > 1)
3396 nfs4_schedule_state_renewal(clp);
3397 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003398 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003399}
3400
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003401static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003403 struct nfs4_renewdata *data = calldata;
3404 struct nfs_client *clp = data->client;
3405 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406
3407 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003408 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003409 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3410 return;
3411 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003412 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003413 return;
3414 }
3415 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003417 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418}
3419
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003420static const struct rpc_call_ops nfs4_renew_ops = {
3421 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003422 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003423};
3424
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003425static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426{
3427 struct rpc_message msg = {
3428 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3429 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003430 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003432 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003434 if (renew_flags == 0)
3435 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003436 if (!atomic_inc_not_zero(&clp->cl_count))
3437 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003438 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003439 if (data == NULL)
3440 return -ENOMEM;
3441 data->client = clp;
3442 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003444 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445}
3446
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003447static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448{
3449 struct rpc_message msg = {
3450 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3451 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003452 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 };
3454 unsigned long now = jiffies;
3455 int status;
3456
3457 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3458 if (status < 0)
3459 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003460 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 return 0;
3462}
3463
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003464static inline int nfs4_server_supports_acls(struct nfs_server *server)
3465{
3466 return (server->caps & NFS_CAP_ACLS)
3467 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3468 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3469}
3470
3471/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3472 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3473 * the stack.
3474 */
3475#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3476
Neil Hormane9e3d722011-03-04 19:26:03 -05003477static int buf_to_pages_noslab(const void *buf, size_t buflen,
3478 struct page **pages, unsigned int *pgbase)
3479{
3480 struct page *newpage, **spages;
3481 int rc = 0;
3482 size_t len;
3483 spages = pages;
3484
3485 do {
Jovi Zhang43b7c3f2011-03-02 23:19:37 +00003486 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003487 newpage = alloc_page(GFP_KERNEL);
3488
3489 if (newpage == NULL)
3490 goto unwind;
3491 memcpy(page_address(newpage), buf, len);
3492 buf += len;
3493 buflen -= len;
3494 *pages++ = newpage;
3495 rc++;
3496 } while (buflen != 0);
3497
3498 return rc;
3499
3500unwind:
3501 for(; rc > 0; rc--)
3502 __free_page(spages[rc-1]);
3503 return -ENOMEM;
3504}
3505
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003506struct nfs4_cached_acl {
3507 int cached;
3508 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003509 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003510};
3511
3512static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003513{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003514 struct nfs_inode *nfsi = NFS_I(inode);
3515
3516 spin_lock(&inode->i_lock);
3517 kfree(nfsi->nfs4_acl);
3518 nfsi->nfs4_acl = acl;
3519 spin_unlock(&inode->i_lock);
3520}
3521
3522static void nfs4_zap_acl_attr(struct inode *inode)
3523{
3524 nfs4_set_cached_acl(inode, NULL);
3525}
3526
3527static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3528{
3529 struct nfs_inode *nfsi = NFS_I(inode);
3530 struct nfs4_cached_acl *acl;
3531 int ret = -ENOENT;
3532
3533 spin_lock(&inode->i_lock);
3534 acl = nfsi->nfs4_acl;
3535 if (acl == NULL)
3536 goto out;
3537 if (buf == NULL) /* user is just asking for length */
3538 goto out_len;
3539 if (acl->cached == 0)
3540 goto out;
3541 ret = -ERANGE; /* see getxattr(2) man page */
3542 if (acl->len > buflen)
3543 goto out;
3544 memcpy(buf, acl->data, acl->len);
3545out_len:
3546 ret = acl->len;
3547out:
3548 spin_unlock(&inode->i_lock);
3549 return ret;
3550}
3551
3552static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3553{
3554 struct nfs4_cached_acl *acl;
3555
3556 if (buf && acl_len <= PAGE_SIZE) {
3557 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3558 if (acl == NULL)
3559 goto out;
3560 acl->cached = 1;
3561 memcpy(acl->data, buf, acl_len);
3562 } else {
3563 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3564 if (acl == NULL)
3565 goto out;
3566 acl->cached = 0;
3567 }
3568 acl->len = acl_len;
3569out:
3570 nfs4_set_cached_acl(inode, acl);
3571}
3572
Andy Adamsonbf118a32011-12-07 11:55:27 -05003573/*
3574 * The getxattr API returns the required buffer length when called with a
3575 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3576 * the required buf. On a NULL buf, we send a page of data to the server
3577 * guessing that the ACL request can be serviced by a page. If so, we cache
3578 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3579 * the cache. If not so, we throw away the page, and cache the required
3580 * length. The next getxattr call will then produce another round trip to
3581 * the server, this time with the input buf of the required size.
3582 */
Trond Myklebust16b42892006-08-24 12:27:15 -04003583static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003584{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003585 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003586 struct nfs_getaclargs args = {
3587 .fh = NFS_FH(inode),
3588 .acl_pages = pages,
3589 .acl_len = buflen,
3590 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003591 struct nfs_getaclres res = {
3592 .acl_len = buflen,
3593 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003594 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003595 struct rpc_message msg = {
3596 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3597 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003598 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003599 };
Andy Adamsonbf118a32011-12-07 11:55:27 -05003600 int ret = -ENOMEM, npages, i, acl_len = 0;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003601
Andy Adamsonbf118a32011-12-07 11:55:27 -05003602 npages = (buflen + PAGE_SIZE - 1) >> PAGE_SHIFT;
3603 /* As long as we're doing a round trip to the server anyway,
3604 * let's be prepared for a page of acl data. */
3605 if (npages == 0)
3606 npages = 1;
3607
3608 for (i = 0; i < npages; i++) {
3609 pages[i] = alloc_page(GFP_KERNEL);
3610 if (!pages[i])
3611 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003612 }
Andy Adamsonbf118a32011-12-07 11:55:27 -05003613 if (npages > 1) {
3614 /* for decoding across pages */
3615 args.acl_scratch = alloc_page(GFP_KERNEL);
3616 if (!args.acl_scratch)
3617 goto out_free;
3618 }
3619 args.acl_len = npages * PAGE_SIZE;
3620 args.acl_pgbase = 0;
3621 /* Let decode_getfacl know not to fail if the ACL data is larger than
3622 * the page we send as a guess */
3623 if (buf == NULL)
3624 res.acl_flags |= NFS4_ACL_LEN_REQUEST;
3625 resp_buf = page_address(pages[0]);
3626
Peng Taode040be2012-01-10 22:42:47 +08003627 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003628 __func__, buf, buflen, npages, args.acl_len);
3629 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3630 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003631 if (ret)
3632 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003633
3634 acl_len = res.acl_len - res.acl_data_offset;
3635 if (acl_len > args.acl_len)
3636 nfs4_write_cached_acl(inode, NULL, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003637 else
Andy Adamsonbf118a32011-12-07 11:55:27 -05003638 nfs4_write_cached_acl(inode, resp_buf + res.acl_data_offset,
3639 acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003640 if (buf) {
3641 ret = -ERANGE;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003642 if (acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003643 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003644 _copy_from_pages(buf, pages, res.acl_data_offset,
3645 res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003646 }
Andy Adamsonbf118a32011-12-07 11:55:27 -05003647 ret = acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003648out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003649 for (i = 0; i < npages; i++)
3650 if (pages[i])
3651 __free_page(pages[i]);
3652 if (args.acl_scratch)
3653 __free_page(args.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003654 return ret;
3655}
3656
Trond Myklebust16b42892006-08-24 12:27:15 -04003657static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3658{
3659 struct nfs4_exception exception = { };
3660 ssize_t ret;
3661 do {
3662 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3663 if (ret >= 0)
3664 break;
3665 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3666 } while (exception.retry);
3667 return ret;
3668}
3669
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003670static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3671{
3672 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003673 int ret;
3674
3675 if (!nfs4_server_supports_acls(server))
3676 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003677 ret = nfs_revalidate_inode(server, inode);
3678 if (ret < 0)
3679 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003680 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3681 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003682 ret = nfs4_read_cached_acl(inode, buf, buflen);
3683 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003684 /* -ENOENT is returned if there is no ACL or if there is an ACL
3685 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003686 return ret;
3687 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003688}
3689
Trond Myklebust16b42892006-08-24 12:27:15 -04003690static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003691{
3692 struct nfs_server *server = NFS_SERVER(inode);
3693 struct page *pages[NFS4ACL_MAXPAGES];
3694 struct nfs_setaclargs arg = {
3695 .fh = NFS_FH(inode),
3696 .acl_pages = pages,
3697 .acl_len = buflen,
3698 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003699 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003700 struct rpc_message msg = {
3701 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3702 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003703 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003704 };
Neil Hormane9e3d722011-03-04 19:26:03 -05003705 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003706
3707 if (!nfs4_server_supports_acls(server))
3708 return -EOPNOTSUPP;
Neil Hormane9e3d722011-03-04 19:26:03 -05003709 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3710 if (i < 0)
3711 return i;
Trond Myklebust642ac542005-10-18 14:20:19 -07003712 nfs_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003713 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003714
3715 /*
3716 * Free each page after tx, so the only ref left is
3717 * held by the network stack
3718 */
3719 for (; i > 0; i--)
3720 put_page(pages[i-1]);
3721
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003722 /*
3723 * Acl update can result in inode attribute update.
3724 * so mark the attribute cache invalid.
3725 */
3726 spin_lock(&inode->i_lock);
3727 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3728 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003729 nfs_access_zap_cache(inode);
3730 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003731 return ret;
3732}
3733
Trond Myklebust16b42892006-08-24 12:27:15 -04003734static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3735{
3736 struct nfs4_exception exception = { };
3737 int err;
3738 do {
3739 err = nfs4_handle_exception(NFS_SERVER(inode),
3740 __nfs4_proc_set_acl(inode, buf, buflen),
3741 &exception);
3742 } while (exception.retry);
3743 return err;
3744}
3745
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003747nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003749 struct nfs_client *clp = server->nfs_client;
3750
3751 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752 return 0;
3753 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003754 case -NFS4ERR_ADMIN_REVOKED:
3755 case -NFS4ERR_BAD_STATEID:
3756 case -NFS4ERR_OPENMODE:
3757 if (state == NULL)
3758 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003759 nfs4_schedule_stateid_recovery(server, state);
3760 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003761 case -NFS4ERR_EXPIRED:
3762 if (state != NULL)
3763 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003765 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003766 nfs4_schedule_lease_recovery(clp);
3767 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003768#if defined(CONFIG_NFS_V4_1)
3769 case -NFS4ERR_BADSESSION:
3770 case -NFS4ERR_BADSLOT:
3771 case -NFS4ERR_BAD_HIGH_SLOT:
3772 case -NFS4ERR_DEADSESSION:
3773 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3774 case -NFS4ERR_SEQ_FALSE_RETRY:
3775 case -NFS4ERR_SEQ_MISORDERED:
3776 dprintk("%s ERROR %d, Reset session\n", __func__,
3777 task->tk_status);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003778 nfs4_schedule_session_recovery(clp->cl_session);
Andy Adamson4745e312009-04-01 09:22:42 -04003779 task->tk_status = 0;
3780 return -EAGAIN;
3781#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003783 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003784 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003785 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3787 task->tk_status = 0;
3788 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04003789 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 case -NFS4ERR_OLD_STATEID:
3791 task->tk_status = 0;
3792 return -EAGAIN;
3793 }
3794 task->tk_status = nfs4_map_errors(task->tk_status);
3795 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003796wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003797 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003798 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3799 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3800 task->tk_status = 0;
3801 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802}
3803
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003804int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3805 unsigned short port, struct rpc_cred *cred,
3806 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807{
3808 nfs4_verifier sc_verifier;
3809 struct nfs4_setclientid setclientid = {
3810 .sc_verifier = &sc_verifier,
3811 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00003812 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 };
3814 struct rpc_message msg = {
3815 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3816 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003817 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003818 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 };
Al Virobc4785c2006-10-19 23:28:51 -07003820 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 int loop = 0;
3822 int status;
3823
Al Virobc4785c2006-10-19 23:28:51 -07003824 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3826 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3827
3828 for(;;) {
3829 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003830 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003831 clp->cl_ipaddr,
3832 rpc_peeraddr2str(clp->cl_rpcclient,
3833 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003834 rpc_peeraddr2str(clp->cl_rpcclient,
3835 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003836 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837 clp->cl_id_uniquifier);
3838 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003839 sizeof(setclientid.sc_netid),
3840 rpc_peeraddr2str(clp->cl_rpcclient,
3841 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003843 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844 clp->cl_ipaddr, port >> 8, port & 255);
3845
Trond Myklebust1bd714f2011-04-24 14:29:33 -04003846 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847 if (status != -NFS4ERR_CLID_INUSE)
3848 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003849 if (loop != 0) {
3850 ++clp->cl_id_uniquifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003852 }
3853 ++loop;
3854 ssleep(clp->cl_lease_time / HZ + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855 }
3856 return status;
3857}
3858
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003859int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003860 struct nfs4_setclientid_res *arg,
3861 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862{
3863 struct nfs_fsinfo fsinfo;
3864 struct rpc_message msg = {
3865 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003866 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003868 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869 };
3870 unsigned long now;
3871 int status;
3872
3873 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04003874 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875 if (status == 0) {
3876 spin_lock(&clp->cl_lock);
3877 clp->cl_lease_time = fsinfo.lease_time * HZ;
3878 clp->cl_last_renewal = now;
3879 spin_unlock(&clp->cl_lock);
3880 }
3881 return status;
3882}
3883
Trond Myklebustfe650402006-01-03 09:55:18 +01003884struct nfs4_delegreturndata {
3885 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003886 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003887 struct nfs_fh fh;
3888 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003889 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003890 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003891 int rpc_status;
3892};
3893
Trond Myklebustfe650402006-01-03 09:55:18 +01003894static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3895{
3896 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003897
Trond Myklebust14516c32010-07-31 14:29:06 -04003898 if (!nfs4_sequence_done(task, &data->res.seq_res))
3899 return;
Andy Adamson938e1012009-04-01 09:22:28 -04003900
Ricardo Labiaga79708862009-12-07 09:23:21 -05003901 switch (task->tk_status) {
3902 case -NFS4ERR_STALE_STATEID:
3903 case -NFS4ERR_EXPIRED:
3904 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01003905 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003906 break;
3907 default:
3908 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3909 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003910 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003911 return;
3912 }
3913 }
3914 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01003915}
3916
3917static void nfs4_delegreturn_release(void *calldata)
3918{
Trond Myklebustfe650402006-01-03 09:55:18 +01003919 kfree(calldata);
3920}
3921
Andy Adamson938e1012009-04-01 09:22:28 -04003922#if defined(CONFIG_NFS_V4_1)
3923static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3924{
3925 struct nfs4_delegreturndata *d_data;
3926
3927 d_data = (struct nfs4_delegreturndata *)data;
3928
Trond Myklebust035168a2010-06-16 09:52:26 -04003929 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04003930 &d_data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003931 &d_data->res.seq_res, task))
Andy Adamson938e1012009-04-01 09:22:28 -04003932 return;
3933 rpc_call_start(task);
3934}
3935#endif /* CONFIG_NFS_V4_1 */
3936
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003937static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04003938#if defined(CONFIG_NFS_V4_1)
3939 .rpc_call_prepare = nfs4_delegreturn_prepare,
3940#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01003941 .rpc_call_done = nfs4_delegreturn_done,
3942 .rpc_release = nfs4_delegreturn_release,
3943};
3944
Trond Myklebuste6f81072008-01-24 18:14:34 -05003945static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003946{
3947 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003948 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003949 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003950 struct rpc_message msg = {
3951 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3952 .rpc_cred = cred,
3953 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003954 struct rpc_task_setup task_setup_data = {
3955 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003956 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003957 .callback_ops = &nfs4_delegreturn_ops,
3958 .flags = RPC_TASK_ASYNC,
3959 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003960 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003961
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003962 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01003963 if (data == NULL)
3964 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05003965 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01003966 data->args.fhandle = &data->fh;
3967 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003968 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003969 nfs_copy_fh(&data->fh, NFS_FH(inode));
3970 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003971 data->res.fattr = &data->fattr;
3972 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003973 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003974 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003975 data->rpc_status = 0;
3976
Trond Myklebustc970aa82007-07-14 15:39:59 -04003977 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05003978 msg.rpc_argp = &data->args;
3979 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003980 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003981 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003982 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003983 if (!issync)
3984 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003985 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003986 if (status != 0)
3987 goto out;
3988 status = data->rpc_status;
3989 if (status != 0)
3990 goto out;
3991 nfs_refresh_inode(inode, &data->fattr);
3992out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003993 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003994 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995}
3996
Trond Myklebuste6f81072008-01-24 18:14:34 -05003997int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998{
3999 struct nfs_server *server = NFS_SERVER(inode);
4000 struct nfs4_exception exception = { };
4001 int err;
4002 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004003 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 switch (err) {
4005 case -NFS4ERR_STALE_STATEID:
4006 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007 case 0:
4008 return 0;
4009 }
4010 err = nfs4_handle_exception(server, err, &exception);
4011 } while (exception.retry);
4012 return err;
4013}
4014
4015#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4016#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4017
4018/*
4019 * sleep, with exponential backoff, and retry the LOCK operation.
4020 */
4021static unsigned long
4022nfs4_set_lock_task_retry(unsigned long timeout)
4023{
Jeff Laytond3103102011-12-01 22:44:39 +01004024 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025 timeout <<= 1;
4026 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4027 return NFS4_LOCK_MAXTIMEOUT;
4028 return timeout;
4029}
4030
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4032{
4033 struct inode *inode = state->inode;
4034 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004035 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004036 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004038 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004040 struct nfs_lockt_res res = {
4041 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042 };
4043 struct rpc_message msg = {
4044 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4045 .rpc_argp = &arg,
4046 .rpc_resp = &res,
4047 .rpc_cred = state->owner->so_cred,
4048 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049 struct nfs4_lock_state *lsp;
4050 int status;
4051
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004052 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004053 status = nfs4_set_lock_state(state, request);
4054 if (status != 0)
4055 goto out;
4056 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004057 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004058 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004059 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004060 switch (status) {
4061 case 0:
4062 request->fl_type = F_UNLCK;
4063 break;
4064 case -NFS4ERR_DENIED:
4065 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004067 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004068out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069 return status;
4070}
4071
4072static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4073{
4074 struct nfs4_exception exception = { };
4075 int err;
4076
4077 do {
4078 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4079 _nfs4_proc_getlk(state, cmd, request),
4080 &exception);
4081 } while (exception.retry);
4082 return err;
4083}
4084
4085static int do_vfs_lock(struct file *file, struct file_lock *fl)
4086{
4087 int res = 0;
4088 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4089 case FL_POSIX:
4090 res = posix_lock_file_wait(file, fl);
4091 break;
4092 case FL_FLOCK:
4093 res = flock_lock_file_wait(file, fl);
4094 break;
4095 default:
4096 BUG();
4097 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098 return res;
4099}
4100
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004101struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004102 struct nfs_locku_args arg;
4103 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004104 struct nfs4_lock_state *lsp;
4105 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004106 struct file_lock fl;
4107 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004108 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004109};
4110
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004111static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4112 struct nfs_open_context *ctx,
4113 struct nfs4_lock_state *lsp,
4114 struct nfs_seqid *seqid)
4115{
4116 struct nfs4_unlockdata *p;
4117 struct inode *inode = lsp->ls_state->inode;
4118
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004119 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004120 if (p == NULL)
4121 return NULL;
4122 p->arg.fh = NFS_FH(inode);
4123 p->arg.fl = &p->fl;
4124 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004125 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004126 p->arg.stateid = &lsp->ls_stateid;
4127 p->lsp = lsp;
4128 atomic_inc(&lsp->ls_count);
4129 /* Ensure we don't close file until we're done freeing locks! */
4130 p->ctx = get_nfs_open_context(ctx);
4131 memcpy(&p->fl, fl, sizeof(p->fl));
4132 p->server = NFS_SERVER(inode);
4133 return p;
4134}
4135
Trond Myklebust06f814a2006-01-03 09:55:07 +01004136static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004137{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004138 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004139 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004140 nfs4_put_lock_state(calldata->lsp);
4141 put_nfs_open_context(calldata->ctx);
4142 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004143}
4144
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004145static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004146{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004147 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004148
Trond Myklebust14516c32010-07-31 14:29:06 -04004149 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4150 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004151 switch (task->tk_status) {
4152 case 0:
4153 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004154 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004155 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01004156 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004157 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004158 case -NFS4ERR_BAD_STATEID:
4159 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004160 case -NFS4ERR_STALE_STATEID:
4161 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004162 break;
4163 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004164 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004165 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004166 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004167}
4168
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004169static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004170{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004171 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004173 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004174 return;
4175 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004176 /* Note: exit _without_ running nfs4_locku_done */
4177 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004178 return;
4179 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004180 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004181 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004182 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004183 &calldata->res.seq_res, task))
Andy Adamsona8936932009-04-01 09:22:23 -04004184 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004185 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186}
4187
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004188static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004189 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004190 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004191 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004192};
4193
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004194static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4195 struct nfs_open_context *ctx,
4196 struct nfs4_lock_state *lsp,
4197 struct nfs_seqid *seqid)
4198{
4199 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004200 struct rpc_message msg = {
4201 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4202 .rpc_cred = ctx->cred,
4203 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004204 struct rpc_task_setup task_setup_data = {
4205 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004206 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004207 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004208 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004209 .flags = RPC_TASK_ASYNC,
4210 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004211
Frank Filz137d6ac2007-07-09 15:32:29 -07004212 /* Ensure this is an unlock - when canceling a lock, the
4213 * canceled lock is passed in, and it won't be an unlock.
4214 */
4215 fl->fl_type = F_UNLCK;
4216
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004217 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4218 if (data == NULL) {
4219 nfs_free_seqid(seqid);
4220 return ERR_PTR(-ENOMEM);
4221 }
4222
Trond Myklebust9d12b212012-01-17 22:04:25 -05004223 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004224 msg.rpc_argp = &data->arg;
4225 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004226 task_setup_data.callback_data = data;
4227 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004228}
4229
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4231{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004232 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004233 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004234 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004235 struct rpc_task *task;
4236 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004237 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238
Trond Myklebust9b073572006-06-29 16:38:34 -04004239 status = nfs4_set_lock_state(state, request);
4240 /* Unlock _before_ we do the RPC call */
4241 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004242 down_read(&nfsi->rwsem);
4243 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4244 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004245 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004246 }
4247 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004248 if (status != 0)
4249 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004250 /* Is this a delegated lock? */
4251 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004252 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004253 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004254 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004255 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004256 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004257 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004258 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004259 status = PTR_ERR(task);
4260 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004261 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004262 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004263 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004264out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004265 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004266 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267}
4268
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004269struct nfs4_lockdata {
4270 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004271 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004272 struct nfs4_lock_state *lsp;
4273 struct nfs_open_context *ctx;
4274 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004275 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004276 int rpc_status;
4277 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004278 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004279};
4280
4281static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004282 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4283 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004284{
4285 struct nfs4_lockdata *p;
4286 struct inode *inode = lsp->ls_state->inode;
4287 struct nfs_server *server = NFS_SERVER(inode);
4288
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004289 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004290 if (p == NULL)
4291 return NULL;
4292
4293 p->arg.fh = NFS_FH(inode);
4294 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004295 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004296 if (p->arg.open_seqid == NULL)
4297 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004298 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004299 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004300 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004301 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004302 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004303 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004304 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004305 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004306 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004307 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004308 atomic_inc(&lsp->ls_count);
4309 p->ctx = get_nfs_open_context(ctx);
4310 memcpy(&p->fl, fl, sizeof(p->fl));
4311 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004312out_free_seqid:
4313 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004314out_free:
4315 kfree(p);
4316 return NULL;
4317}
4318
4319static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4320{
4321 struct nfs4_lockdata *data = calldata;
4322 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323
Harvey Harrison3110ff82008-05-02 13:42:44 -07004324 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004325 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4326 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004327 /* Do we need to do an open_to_lock_owner? */
4328 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004329 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4330 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004331 data->arg.open_stateid = &state->stateid;
4332 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004333 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004334 } else
4335 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004336 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004337 if (nfs4_setup_sequence(data->server,
4338 &data->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004339 &data->res.seq_res, task))
Andy Adamson66179ef2009-04-01 09:22:22 -04004340 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004341 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004342 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004343}
4344
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004345static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4346{
4347 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4348 nfs4_lock_prepare(task, calldata);
4349}
4350
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004351static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4352{
4353 struct nfs4_lockdata *data = calldata;
4354
Harvey Harrison3110ff82008-05-02 13:42:44 -07004355 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004356
Trond Myklebust14516c32010-07-31 14:29:06 -04004357 if (!nfs4_sequence_done(task, &data->res.seq_res))
4358 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004359
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004360 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004361 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004362 if (data->rpc_status == 0)
4363 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4364 else
4365 goto out;
4366 }
4367 if (data->rpc_status == 0) {
4368 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4369 sizeof(data->lsp->ls_stateid.data));
4370 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Al Viro3d4ff432011-06-22 18:40:12 -04004371 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004372 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004373out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004374 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004375}
4376
4377static void nfs4_lock_release(void *calldata)
4378{
4379 struct nfs4_lockdata *data = calldata;
4380
Harvey Harrison3110ff82008-05-02 13:42:44 -07004381 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004382 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004383 if (data->cancelled != 0) {
4384 struct rpc_task *task;
4385 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4386 data->arg.lock_seqid);
4387 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004388 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004389 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004390 } else
4391 nfs_free_seqid(data->arg.lock_seqid);
4392 nfs4_put_lock_state(data->lsp);
4393 put_nfs_open_context(data->ctx);
4394 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004395 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004396}
4397
4398static const struct rpc_call_ops nfs4_lock_ops = {
4399 .rpc_call_prepare = nfs4_lock_prepare,
4400 .rpc_call_done = nfs4_lock_done,
4401 .rpc_release = nfs4_lock_release,
4402};
4403
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004404static const struct rpc_call_ops nfs4_recover_lock_ops = {
4405 .rpc_call_prepare = nfs4_recover_lock_prepare,
4406 .rpc_call_done = nfs4_lock_done,
4407 .rpc_release = nfs4_lock_release,
4408};
4409
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004410static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4411{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004412 switch (error) {
4413 case -NFS4ERR_ADMIN_REVOKED:
4414 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004415 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004416 if (new_lock_owner != 0 ||
4417 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004418 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004419 break;
4420 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004421 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004422 case -NFS4ERR_EXPIRED:
4423 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004424 };
4425}
4426
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004427static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004428{
4429 struct nfs4_lockdata *data;
4430 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004431 struct rpc_message msg = {
4432 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4433 .rpc_cred = state->owner->so_cred,
4434 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004435 struct rpc_task_setup task_setup_data = {
4436 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004437 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004438 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004439 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004440 .flags = RPC_TASK_ASYNC,
4441 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004442 int ret;
4443
Harvey Harrison3110ff82008-05-02 13:42:44 -07004444 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004445 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004446 fl->fl_u.nfs4_fl.owner,
4447 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004448 if (data == NULL)
4449 return -ENOMEM;
4450 if (IS_SETLKW(cmd))
4451 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004452 if (recovery_type > NFS_LOCK_NEW) {
4453 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004454 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004455 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4456 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05004457 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004458 msg.rpc_argp = &data->arg;
4459 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004460 task_setup_data.callback_data = data;
4461 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004462 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004463 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004464 ret = nfs4_wait_for_completion_rpc_task(task);
4465 if (ret == 0) {
4466 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004467 if (ret)
4468 nfs4_handle_setlk_error(data->server, data->lsp,
4469 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004470 } else
4471 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004472 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004473 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004474 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475}
4476
4477static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4478{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004479 struct nfs_server *server = NFS_SERVER(state->inode);
4480 struct nfs4_exception exception = { };
4481 int err;
4482
4483 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004484 /* Cache the lock if possible... */
4485 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4486 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004487 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004488 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004489 break;
4490 nfs4_handle_exception(server, err, &exception);
4491 } while (exception.retry);
4492 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493}
4494
4495static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4496{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004497 struct nfs_server *server = NFS_SERVER(state->inode);
4498 struct nfs4_exception exception = { };
4499 int err;
4500
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004501 err = nfs4_set_lock_state(state, request);
4502 if (err != 0)
4503 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004504 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004505 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4506 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004507 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004508 switch (err) {
4509 default:
4510 goto out;
4511 case -NFS4ERR_GRACE:
4512 case -NFS4ERR_DELAY:
4513 nfs4_handle_exception(server, err, &exception);
4514 err = 0;
4515 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004516 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004517out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004518 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519}
4520
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004521#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004522static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004523{
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004524 int status, ret = NFS_OK;
4525 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004526 struct nfs_server *server = NFS_SERVER(state->inode);
4527
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004528 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
4529 if (lsp->ls_flags & NFS_LOCK_INITIALIZED) {
4530 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4531 if (status != NFS_OK) {
4532 nfs41_free_stateid(server, &lsp->ls_stateid);
4533 lsp->ls_flags &= ~NFS_LOCK_INITIALIZED;
4534 ret = status;
4535 }
4536 }
4537 };
4538
4539 return ret;
4540}
4541
4542static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4543{
4544 int status = NFS_OK;
4545
4546 if (test_bit(LK_STATE_IN_USE, &state->flags))
4547 status = nfs41_check_expired_locks(state);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004548 if (status == NFS_OK)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004549 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004550 return nfs4_lock_expired(state, request);
4551}
4552#endif
4553
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4555{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004556 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004557 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004558 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004560 if ((fl_flags & FL_POSIX) &&
4561 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4562 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004563 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004564 status = nfs4_set_lock_state(state, request);
4565 if (status != 0)
4566 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004567 request->fl_flags |= FL_ACCESS;
4568 status = do_vfs_lock(request->fl_file, request);
4569 if (status < 0)
4570 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004571 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004572 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004573 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004574 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004575 request->fl_flags = fl_flags & ~FL_SLEEP;
4576 status = do_vfs_lock(request->fl_file, request);
4577 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004578 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004579 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004580 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004581 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004582 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004583 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004584 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004585 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4586 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004587out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004588 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004589out:
4590 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591 return status;
4592}
4593
4594static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4595{
4596 struct nfs4_exception exception = { };
4597 int err;
4598
4599 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004600 err = _nfs4_proc_setlk(state, cmd, request);
4601 if (err == -NFS4ERR_DENIED)
4602 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004604 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605 } while (exception.retry);
4606 return err;
4607}
4608
4609static int
4610nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4611{
4612 struct nfs_open_context *ctx;
4613 struct nfs4_state *state;
4614 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4615 int status;
4616
4617 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004618 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619 state = ctx->state;
4620
4621 if (request->fl_start < 0 || request->fl_end < 0)
4622 return -EINVAL;
4623
Trond Myklebustd9531262009-07-21 19:22:38 -04004624 if (IS_GETLK(cmd)) {
4625 if (state != NULL)
4626 return nfs4_proc_getlk(state, F_GETLK, request);
4627 return 0;
4628 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629
4630 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4631 return -EINVAL;
4632
Trond Myklebustd9531262009-07-21 19:22:38 -04004633 if (request->fl_type == F_UNLCK) {
4634 if (state != NULL)
4635 return nfs4_proc_unlck(state, cmd, request);
4636 return 0;
4637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638
Trond Myklebustd9531262009-07-21 19:22:38 -04004639 if (state == NULL)
4640 return -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641 do {
4642 status = nfs4_proc_setlk(state, cmd, request);
4643 if ((status != -EAGAIN) || IS_SETLK(cmd))
4644 break;
4645 timeout = nfs4_set_lock_task_retry(timeout);
4646 status = -ERESTARTSYS;
4647 if (signalled())
4648 break;
4649 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650 return status;
4651}
4652
Trond Myklebust888e6942005-11-04 15:38:11 -05004653int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4654{
4655 struct nfs_server *server = NFS_SERVER(state->inode);
4656 struct nfs4_exception exception = { };
4657 int err;
4658
4659 err = nfs4_set_lock_state(state, fl);
4660 if (err != 0)
4661 goto out;
4662 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004663 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004664 switch (err) {
4665 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004666 printk(KERN_ERR "NFS: %s: unhandled error "
4667 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004668 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004669 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004670 goto out;
4671 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004672 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07004673 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004674 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004675 nfs4_schedule_lease_recovery(server->nfs_client);
4676 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004677 case -NFS4ERR_BADSESSION:
4678 case -NFS4ERR_BADSLOT:
4679 case -NFS4ERR_BAD_HIGH_SLOT:
4680 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4681 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004682 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Trond Myklebustd5122202009-06-17 13:22:58 -07004683 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004684 case -ERESTARTSYS:
4685 /*
4686 * The show must go on: exit, but mark the
4687 * stateid as needing recovery.
4688 */
4689 case -NFS4ERR_ADMIN_REVOKED:
4690 case -NFS4ERR_BAD_STATEID:
4691 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004692 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004693 err = 0;
4694 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004695 case -EKEYEXPIRED:
4696 /*
4697 * User RPCSEC_GSS context has expired.
4698 * We cannot recover this stateid now, so
4699 * skip it and allow recovery thread to
4700 * proceed.
4701 */
4702 err = 0;
4703 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004704 case -ENOMEM:
4705 case -NFS4ERR_DENIED:
4706 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4707 err = 0;
4708 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004709 case -NFS4ERR_DELAY:
4710 break;
4711 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004712 err = nfs4_handle_exception(server, err, &exception);
4713 } while (exception.retry);
4714out:
4715 return err;
4716}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004717
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004718static void nfs4_release_lockowner_release(void *calldata)
4719{
4720 kfree(calldata);
4721}
4722
4723const struct rpc_call_ops nfs4_release_lockowner_ops = {
4724 .rpc_release = nfs4_release_lockowner_release,
4725};
4726
4727void nfs4_release_lockowner(const struct nfs4_lock_state *lsp)
4728{
4729 struct nfs_server *server = lsp->ls_state->owner->so_server;
4730 struct nfs_release_lockowner_args *args;
4731 struct rpc_message msg = {
4732 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4733 };
4734
4735 if (server->nfs_client->cl_mvops->minor_version != 0)
4736 return;
4737 args = kmalloc(sizeof(*args), GFP_NOFS);
4738 if (!args)
4739 return;
4740 args->lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004741 args->lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004742 args->lock_owner.s_dev = server->s_dev;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004743 msg.rpc_argp = args;
4744 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, args);
4745}
4746
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004747#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4748
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004749static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4750 const void *buf, size_t buflen,
4751 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004752{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004753 if (strcmp(key, "") != 0)
4754 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004755
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004756 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004757}
4758
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004759static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4760 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004761{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004762 if (strcmp(key, "") != 0)
4763 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004764
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004765 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004766}
4767
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004768static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4769 size_t list_len, const char *name,
4770 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004771{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004772 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004773
J. Bruce Fields096455a2006-03-20 23:23:42 -05004774 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4775 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004776
4777 if (list && len <= list_len)
4778 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004779 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004780}
4781
Andy Adamson533eb462011-06-13 18:25:56 -04004782/*
4783 * nfs_fhget will use either the mounted_on_fileid or the fileid
4784 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004785static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4786{
Andy Adamson533eb462011-06-13 18:25:56 -04004787 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
4788 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
4789 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4790 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004791 return;
4792
4793 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4794 NFS_ATTR_FATTR_NLINK;
4795 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4796 fattr->nlink = 2;
4797}
4798
Trond Myklebust56659e92007-07-17 21:52:39 -04004799int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004800 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004801{
4802 struct nfs_server *server = NFS_SERVER(dir);
4803 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004804 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004805 };
4806 struct nfs4_fs_locations_arg args = {
4807 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004808 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004809 .page = page,
4810 .bitmask = bitmask,
4811 };
Benny Halevy22958462009-04-01 09:22:02 -04004812 struct nfs4_fs_locations_res res = {
4813 .fs_locations = fs_locations,
4814 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004815 struct rpc_message msg = {
4816 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4817 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004818 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004819 };
4820 int status;
4821
Harvey Harrison3110ff82008-05-02 13:42:44 -07004822 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04004823
4824 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
4825 * is not supported */
4826 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
4827 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
4828 else
4829 bitmask[0] |= FATTR4_WORD0_FILEID;
4830
Trond Myklebustc228fd32007-01-13 02:28:11 -05004831 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004832 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004833 fs_locations->nlocations = 0;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004834 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004835 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004836 return status;
4837}
4838
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00004839static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4840{
4841 int status;
4842 struct nfs4_secinfo_arg args = {
4843 .dir_fh = NFS_FH(dir),
4844 .name = name,
4845 };
4846 struct nfs4_secinfo_res res = {
4847 .flavors = flavors,
4848 };
4849 struct rpc_message msg = {
4850 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
4851 .rpc_argp = &args,
4852 .rpc_resp = &res,
4853 };
4854
4855 dprintk("NFS call secinfo %s\n", name->name);
4856 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4857 dprintk("NFS reply secinfo: %d\n", status);
4858 return status;
4859}
4860
4861int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4862{
4863 struct nfs4_exception exception = { };
4864 int err;
4865 do {
4866 err = nfs4_handle_exception(NFS_SERVER(dir),
4867 _nfs4_proc_secinfo(dir, name, flavors),
4868 &exception);
4869 } while (exception.retry);
4870 return err;
4871}
4872
Andy Adamson557134a2009-04-01 09:21:53 -04004873#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04004874/*
Andy Adamson357f54d2010-12-14 10:11:57 -05004875 * Check the exchange flags returned by the server for invalid flags, having
4876 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
4877 * DS flags set.
4878 */
4879static int nfs4_check_cl_exchange_flags(u32 flags)
4880{
4881 if (flags & ~EXCHGID4_FLAG_MASK_R)
4882 goto out_inval;
4883 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
4884 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
4885 goto out_inval;
4886 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
4887 goto out_inval;
4888 return NFS_OK;
4889out_inval:
4890 return -NFS4ERR_INVAL;
4891}
4892
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004893static bool
4894nfs41_same_server_scope(struct server_scope *a, struct server_scope *b)
4895{
4896 if (a->server_scope_sz == b->server_scope_sz &&
4897 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
4898 return true;
4899
4900 return false;
4901}
4902
Andy Adamson357f54d2010-12-14 10:11:57 -05004903/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04004904 * nfs4_proc_exchange_id()
4905 *
4906 * Since the clientid has expired, all compounds using sessions
4907 * associated with the stale clientid will be returning
4908 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4909 * be in some phase of session reset.
4910 */
Andy Adamson4d643d12009-12-04 15:52:24 -05004911int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004912{
4913 nfs4_verifier verifier;
4914 struct nfs41_exchange_id_args args = {
4915 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05004916 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04004917 };
4918 struct nfs41_exchange_id_res res = {
4919 .client = clp,
4920 };
4921 int status;
4922 struct rpc_message msg = {
4923 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4924 .rpc_argp = &args,
4925 .rpc_resp = &res,
4926 .rpc_cred = cred,
4927 };
4928 __be32 *p;
4929
4930 dprintk("--> %s\n", __func__);
4931 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04004932
Benny Halevy99fe60d2009-04-01 09:22:29 -04004933 p = (u32 *)verifier.data;
4934 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4935 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4936 args.verifier = &verifier;
4937
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004938 args.id_len = scnprintf(args.id, sizeof(args.id),
4939 "%s/%s.%s/%u",
4940 clp->cl_ipaddr,
4941 init_utsname()->nodename,
4942 init_utsname()->domainname,
4943 clp->cl_rpcclient->cl_auth->au_flavor);
Benny Halevy99fe60d2009-04-01 09:22:29 -04004944
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004945 res.server_scope = kzalloc(sizeof(struct server_scope), GFP_KERNEL);
4946 if (unlikely(!res.server_scope))
4947 return -ENOMEM;
4948
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004949 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004950 if (!status)
4951 status = nfs4_check_cl_exchange_flags(clp->cl_exchange_flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004952
4953 if (!status) {
4954 if (clp->server_scope &&
4955 !nfs41_same_server_scope(clp->server_scope,
4956 res.server_scope)) {
4957 dprintk("%s: server_scope mismatch detected\n",
4958 __func__);
4959 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
4960 kfree(clp->server_scope);
4961 clp->server_scope = NULL;
4962 }
4963
4964 if (!clp->server_scope)
4965 clp->server_scope = res.server_scope;
4966 else
4967 kfree(res.server_scope);
4968 }
4969
Benny Halevy99fe60d2009-04-01 09:22:29 -04004970 dprintk("<-- %s status= %d\n", __func__, status);
4971 return status;
4972}
4973
Andy Adamson2050f0c2009-04-01 09:22:30 -04004974struct nfs4_get_lease_time_data {
4975 struct nfs4_get_lease_time_args *args;
4976 struct nfs4_get_lease_time_res *res;
4977 struct nfs_client *clp;
4978};
4979
4980static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4981 void *calldata)
4982{
4983 int ret;
4984 struct nfs4_get_lease_time_data *data =
4985 (struct nfs4_get_lease_time_data *)calldata;
4986
4987 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004988 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004989 /* just setup sequence, do not trigger session recovery
4990 since we're invoked within one */
4991 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004992 &data->args->la_seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004993 &data->res->lr_seq_res, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004994
4995 BUG_ON(ret == -EAGAIN);
4996 rpc_call_start(task);
4997 dprintk("<-- %s\n", __func__);
4998}
4999
5000/*
5001 * Called from nfs4_state_manager thread for session setup, so don't recover
5002 * from sequence operation or clientid errors.
5003 */
5004static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5005{
5006 struct nfs4_get_lease_time_data *data =
5007 (struct nfs4_get_lease_time_data *)calldata;
5008
5009 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005010 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5011 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005012 switch (task->tk_status) {
5013 case -NFS4ERR_DELAY:
5014 case -NFS4ERR_GRACE:
5015 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5016 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5017 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005018 /* fall through */
5019 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005020 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005021 return;
5022 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005023 dprintk("<-- %s\n", __func__);
5024}
5025
5026struct rpc_call_ops nfs4_get_lease_time_ops = {
5027 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5028 .rpc_call_done = nfs4_get_lease_time_done,
5029};
5030
5031int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5032{
5033 struct rpc_task *task;
5034 struct nfs4_get_lease_time_args args;
5035 struct nfs4_get_lease_time_res res = {
5036 .lr_fsinfo = fsinfo,
5037 };
5038 struct nfs4_get_lease_time_data data = {
5039 .args = &args,
5040 .res = &res,
5041 .clp = clp,
5042 };
5043 struct rpc_message msg = {
5044 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5045 .rpc_argp = &args,
5046 .rpc_resp = &res,
5047 };
5048 struct rpc_task_setup task_setup = {
5049 .rpc_client = clp->cl_rpcclient,
5050 .rpc_message = &msg,
5051 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005052 .callback_data = &data,
5053 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005054 };
5055 int status;
5056
Trond Myklebust9d12b212012-01-17 22:04:25 -05005057 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005058 dprintk("--> %s\n", __func__);
5059 task = rpc_run_task(&task_setup);
5060
5061 if (IS_ERR(task))
5062 status = PTR_ERR(task);
5063 else {
5064 status = task->tk_status;
5065 rpc_put_task(task);
5066 }
5067 dprintk("<-- %s return %d\n", __func__, status);
5068
5069 return status;
5070}
5071
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005072/*
5073 * Reset a slot table
5074 */
Andy Adamson104aeba2010-01-14 17:45:10 -05005075static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
5076 int ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005077{
Andy Adamson104aeba2010-01-14 17:45:10 -05005078 struct nfs4_slot *new = NULL;
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005079 int i;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005080 int ret = 0;
5081
Andy Adamson104aeba2010-01-14 17:45:10 -05005082 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5083 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005084
Andy Adamson104aeba2010-01-14 17:45:10 -05005085 /* Does the newly negotiated max_reqs match the existing slot table? */
5086 if (max_reqs != tbl->max_slots) {
5087 ret = -ENOMEM;
5088 new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005089 GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05005090 if (!new)
5091 goto out;
5092 ret = 0;
5093 kfree(tbl->slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005094 }
5095 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson104aeba2010-01-14 17:45:10 -05005096 if (new) {
5097 tbl->slots = new;
5098 tbl->max_slots = max_reqs;
5099 }
5100 for (i = 0; i < tbl->max_slots; ++i)
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005101 tbl->slots[i].seq_nr = ivalue;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005102 spin_unlock(&tbl->slot_tbl_lock);
5103 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5104 tbl, tbl->slots, tbl->max_slots);
5105out:
5106 dprintk("<-- %s: return %d\n", __func__, ret);
5107 return ret;
5108}
5109
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005110/* Destroy the slot table */
5111static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5112{
5113 if (session->fc_slot_table.slots != NULL) {
5114 kfree(session->fc_slot_table.slots);
5115 session->fc_slot_table.slots = NULL;
5116 }
5117 if (session->bc_slot_table.slots != NULL) {
5118 kfree(session->bc_slot_table.slots);
5119 session->bc_slot_table.slots = NULL;
5120 }
5121 return;
5122}
5123
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005124/*
Andy Adamsonfc931582009-04-01 09:22:31 -04005125 * Initialize slot table
5126 */
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005127static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
5128 int max_slots, int ivalue)
Andy Adamsonfc931582009-04-01 09:22:31 -04005129{
Andy Adamsonfc931582009-04-01 09:22:31 -04005130 struct nfs4_slot *slot;
5131 int ret = -ENOMEM;
5132
5133 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
5134
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005135 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
Andy Adamsonfc931582009-04-01 09:22:31 -04005136
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005137 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_NOFS);
Andy Adamsonfc931582009-04-01 09:22:31 -04005138 if (!slot)
5139 goto out;
Andy Adamsonfc931582009-04-01 09:22:31 -04005140 ret = 0;
5141
5142 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonfc931582009-04-01 09:22:31 -04005143 tbl->max_slots = max_slots;
5144 tbl->slots = slot;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005145 tbl->highest_used_slotid = NFS4_NO_SLOT; /* no slot is currently used */
Andy Adamsonfc931582009-04-01 09:22:31 -04005146 spin_unlock(&tbl->slot_tbl_lock);
5147 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5148 tbl, tbl->slots, tbl->max_slots);
5149out:
5150 dprintk("<-- %s: return %d\n", __func__, ret);
5151 return ret;
Andy Adamsonfc931582009-04-01 09:22:31 -04005152}
5153
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005154/*
Andy Adamsonaacd5532011-11-09 13:58:21 -05005155 * Initialize or reset the forechannel and backchannel tables
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005156 */
Andy Adamsonaacd5532011-11-09 13:58:21 -05005157static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005158{
Trond Myklebustf26468f2009-12-05 19:32:11 -05005159 struct nfs4_slot_table *tbl;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005160 int status;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005161
Andy Adamsonaacd5532011-11-09 13:58:21 -05005162 dprintk("--> %s\n", __func__);
5163 /* Fore channel */
5164 tbl = &ses->fc_slot_table;
Trond Myklebustf26468f2009-12-05 19:32:11 -05005165 if (tbl->slots == NULL) {
Andy Adamsonaacd5532011-11-09 13:58:21 -05005166 status = nfs4_init_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5167 if (status) /* -ENOMEM */
5168 return status;
5169 } else {
5170 status = nfs4_reset_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
Trond Myklebustf26468f2009-12-05 19:32:11 -05005171 if (status)
5172 return status;
5173 }
Andy Adamsonaacd5532011-11-09 13:58:21 -05005174 /* Back channel */
5175 tbl = &ses->bc_slot_table;
Trond Myklebustf26468f2009-12-05 19:32:11 -05005176 if (tbl->slots == NULL) {
Andy Adamsonaacd5532011-11-09 13:58:21 -05005177 status = nfs4_init_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
Trond Myklebustf26468f2009-12-05 19:32:11 -05005178 if (status)
Andy Adamsonaacd5532011-11-09 13:58:21 -05005179 /* Fore and back channel share a connection so get
5180 * both slot tables or neither */
5181 nfs4_destroy_slot_tables(ses);
5182 } else
5183 status = nfs4_reset_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005184 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04005185}
5186
5187struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5188{
5189 struct nfs4_session *session;
5190 struct nfs4_slot_table *tbl;
5191
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005192 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04005193 if (!session)
5194 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04005195
Andy Adamson557134a2009-04-01 09:21:53 -04005196 tbl = &session->fc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005197 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamson557134a2009-04-01 09:21:53 -04005198 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005199 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005200 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005201
5202 tbl = &session->bc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005203 tbl->highest_used_slotid = NFS4_NO_SLOT;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005204 spin_lock_init(&tbl->slot_tbl_lock);
5205 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005206 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005207
Trond Myklebust1055d762010-06-16 09:52:27 -04005208 session->session_state = 1<<NFS4_SESSION_INITING;
5209
Andy Adamson557134a2009-04-01 09:21:53 -04005210 session->clp = clp;
5211 return session;
5212}
5213
5214void nfs4_destroy_session(struct nfs4_session *session)
5215{
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005216 nfs4_proc_destroy_session(session);
5217 dprintk("%s Destroy backchannel for xprt %p\n",
5218 __func__, session->clp->cl_rpcclient->cl_xprt);
5219 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
5220 NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005221 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04005222 kfree(session);
5223}
5224
Andy Adamsonfc931582009-04-01 09:22:31 -04005225/*
5226 * Initialize the values to be used by the client in CREATE_SESSION
5227 * If nfs4_init_session set the fore channel request and response sizes,
5228 * use them.
5229 *
5230 * Set the back channel max_resp_sz_cached to zero to force the client to
5231 * always set csa_cachethis to FALSE because the current implementation
5232 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5233 */
5234static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5235{
5236 struct nfs4_session *session = args->client->cl_session;
5237 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5238 mxresp_sz = session->fc_attrs.max_resp_sz;
5239
5240 if (mxrqst_sz == 0)
5241 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5242 if (mxresp_sz == 0)
5243 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5244 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005245 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5246 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005247 args->fc_attrs.max_ops = NFS4_MAX_OPS;
5248 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
5249
5250 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005251 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005252 __func__,
5253 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005254 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005255
5256 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005257 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5258 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5259 args->bc_attrs.max_resp_sz_cached = 0;
5260 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5261 args->bc_attrs.max_reqs = 1;
5262
5263 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5264 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5265 __func__,
5266 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5267 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5268 args->bc_attrs.max_reqs);
5269}
5270
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005271static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005272{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005273 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5274 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5275
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005276 if (rcvd->max_resp_sz > sent->max_resp_sz)
5277 return -EINVAL;
5278 /*
5279 * Our requested max_ops is the minimum we need; we're not
5280 * prepared to break up compounds into smaller pieces than that.
5281 * So, no point even trying to continue if the server won't
5282 * cooperate:
5283 */
5284 if (rcvd->max_ops < sent->max_ops)
5285 return -EINVAL;
5286 if (rcvd->max_reqs == 0)
5287 return -EINVAL;
5288 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005289}
5290
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005291static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5292{
5293 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5294 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005295
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005296 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5297 return -EINVAL;
5298 if (rcvd->max_resp_sz < sent->max_resp_sz)
5299 return -EINVAL;
5300 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5301 return -EINVAL;
5302 /* These would render the backchannel useless: */
5303 if (rcvd->max_ops == 0)
5304 return -EINVAL;
5305 if (rcvd->max_reqs == 0)
5306 return -EINVAL;
5307 return 0;
5308}
Andy Adamson8d353012009-04-01 09:22:32 -04005309
Andy Adamson8d353012009-04-01 09:22:32 -04005310static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5311 struct nfs4_session *session)
5312{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005313 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005314
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005315 ret = nfs4_verify_fore_channel_attrs(args, session);
5316 if (ret)
5317 return ret;
5318 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005319}
5320
Andy Adamsonfc931582009-04-01 09:22:31 -04005321static int _nfs4_proc_create_session(struct nfs_client *clp)
5322{
5323 struct nfs4_session *session = clp->cl_session;
5324 struct nfs41_create_session_args args = {
5325 .client = clp,
5326 .cb_program = NFS4_CALLBACK,
5327 };
5328 struct nfs41_create_session_res res = {
5329 .client = clp,
5330 };
5331 struct rpc_message msg = {
5332 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5333 .rpc_argp = &args,
5334 .rpc_resp = &res,
5335 };
5336 int status;
5337
5338 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005339 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005340
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005341 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005342
Andy Adamson8d353012009-04-01 09:22:32 -04005343 if (!status)
5344 /* Verify the session's negotiated channel_attrs values */
5345 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005346 if (!status) {
5347 /* Increment the clientid slot sequence id */
5348 clp->cl_seqid++;
5349 }
5350
5351 return status;
5352}
5353
5354/*
5355 * Issues a CREATE_SESSION operation to the server.
5356 * It is the responsibility of the caller to verify the session is
5357 * expired before calling this routine.
5358 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05005359int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04005360{
5361 int status;
5362 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005363 struct nfs4_session *session = clp->cl_session;
5364
5365 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5366
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005367 status = _nfs4_proc_create_session(clp);
Andy Adamsonfc931582009-04-01 09:22:31 -04005368 if (status)
5369 goto out;
5370
Andy Adamsonaacd5532011-11-09 13:58:21 -05005371 /* Init or reset the session slot tables */
5372 status = nfs4_setup_session_slot_tables(session);
5373 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005374 if (status)
5375 goto out;
5376
5377 ptr = (unsigned *)&session->sess_id.data[0];
5378 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5379 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005380out:
5381 dprintk("<-- %s\n", __func__);
5382 return status;
5383}
5384
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005385/*
5386 * Issue the over-the-wire RPC DESTROY_SESSION.
5387 * The caller must serialize access to this routine.
5388 */
5389int nfs4_proc_destroy_session(struct nfs4_session *session)
5390{
5391 int status = 0;
5392 struct rpc_message msg;
5393
5394 dprintk("--> nfs4_proc_destroy_session\n");
5395
5396 /* session is still being setup */
5397 if (session->clp->cl_cons_state != NFS_CS_READY)
5398 return status;
5399
5400 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5401 msg.rpc_argp = session;
5402 msg.rpc_resp = NULL;
5403 msg.rpc_cred = NULL;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005404 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005405
5406 if (status)
5407 printk(KERN_WARNING
Weston Andros Adamsonf9fd2d92012-01-26 13:32:22 -05005408 "NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005409 "Session has been destroyed regardless...\n", status);
5410
5411 dprintk("<-- nfs4_proc_destroy_session\n");
5412 return status;
5413}
5414
Trond Myklebustfccba802009-07-21 16:48:07 -04005415int nfs4_init_session(struct nfs_server *server)
5416{
5417 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005418 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005419 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005420 int ret;
5421
5422 if (!nfs4_has_session(clp))
5423 return 0;
5424
Trond Myklebust1055d762010-06-16 09:52:27 -04005425 session = clp->cl_session;
5426 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5427 return 0;
5428
Andy Adamson68bf05e2009-12-15 12:55:02 -05005429 rsize = server->rsize;
5430 if (rsize == 0)
5431 rsize = NFS_MAX_FILE_IO_SIZE;
5432 wsize = server->wsize;
5433 if (wsize == 0)
5434 wsize = NFS_MAX_FILE_IO_SIZE;
5435
Andy Adamson68bf05e2009-12-15 12:55:02 -05005436 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5437 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005438
Trond Myklebustfccba802009-07-21 16:48:07 -04005439 ret = nfs4_recover_expired_lease(server);
5440 if (!ret)
5441 ret = nfs4_check_client_ready(clp);
5442 return ret;
5443}
5444
Andy Adamsond83217c2011-03-01 01:34:17 +00005445int nfs4_init_ds_session(struct nfs_client *clp)
5446{
5447 struct nfs4_session *session = clp->cl_session;
5448 int ret;
5449
5450 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5451 return 0;
5452
5453 ret = nfs4_client_recover_expired_lease(clp);
5454 if (!ret)
5455 /* Test for the DS role */
5456 if (!is_ds_client(clp))
5457 ret = -ENODEV;
5458 if (!ret)
5459 ret = nfs4_check_client_ready(clp);
5460 return ret;
5461
5462}
5463EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5464
5465
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005466/*
5467 * Renew the cl_session lease.
5468 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005469struct nfs4_sequence_data {
5470 struct nfs_client *clp;
5471 struct nfs4_sequence_args args;
5472 struct nfs4_sequence_res res;
5473};
5474
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005475static void nfs41_sequence_release(void *data)
5476{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005477 struct nfs4_sequence_data *calldata = data;
5478 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005479
Alexandros Batsakis71358402010-02-05 03:45:05 -08005480 if (atomic_read(&clp->cl_count) > 1)
5481 nfs4_schedule_state_renewal(clp);
5482 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005483 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005484}
5485
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005486static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5487{
5488 switch(task->tk_status) {
5489 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005490 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5491 return -EAGAIN;
5492 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005493 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005494 }
5495 return 0;
5496}
5497
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005498static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005499{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005500 struct nfs4_sequence_data *calldata = data;
5501 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005502
Trond Myklebust14516c32010-07-31 14:29:06 -04005503 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5504 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005505
5506 if (task->tk_status < 0) {
5507 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005508 if (atomic_read(&clp->cl_count) == 1)
5509 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005510
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005511 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5512 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005513 return;
5514 }
5515 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005516 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005517out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005518 dprintk("<-- %s\n", __func__);
5519}
5520
5521static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5522{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005523 struct nfs4_sequence_data *calldata = data;
5524 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005525 struct nfs4_sequence_args *args;
5526 struct nfs4_sequence_res *res;
5527
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005528 args = task->tk_msg.rpc_argp;
5529 res = task->tk_msg.rpc_resp;
5530
Trond Myklebust9d12b212012-01-17 22:04:25 -05005531 if (nfs41_setup_sequence(clp->cl_session, args, res, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005532 return;
5533 rpc_call_start(task);
5534}
5535
5536static const struct rpc_call_ops nfs41_sequence_ops = {
5537 .rpc_call_done = nfs41_sequence_call_done,
5538 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005539 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005540};
5541
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005542static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005543{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005544 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005545 struct rpc_message msg = {
5546 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5547 .rpc_cred = cred,
5548 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005549 struct rpc_task_setup task_setup_data = {
5550 .rpc_client = clp->cl_rpcclient,
5551 .rpc_message = &msg,
5552 .callback_ops = &nfs41_sequence_ops,
5553 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5554 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005555
Alexandros Batsakis71358402010-02-05 03:45:05 -08005556 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005557 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005558 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005559 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005560 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005561 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005562 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05005563 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005564 msg.rpc_argp = &calldata->args;
5565 msg.rpc_resp = &calldata->res;
5566 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005567 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005568
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005569 return rpc_run_task(&task_setup_data);
5570}
5571
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005572static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005573{
5574 struct rpc_task *task;
5575 int ret = 0;
5576
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005577 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5578 return 0;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005579 task = _nfs41_proc_sequence(clp, cred);
5580 if (IS_ERR(task))
5581 ret = PTR_ERR(task);
5582 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005583 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005584 dprintk("<-- %s status=%d\n", __func__, ret);
5585 return ret;
5586}
5587
5588static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5589{
5590 struct rpc_task *task;
5591 int ret;
5592
5593 task = _nfs41_proc_sequence(clp, cred);
5594 if (IS_ERR(task)) {
5595 ret = PTR_ERR(task);
5596 goto out;
5597 }
5598 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005599 if (!ret) {
5600 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5601
5602 if (task->tk_status == 0)
5603 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005604 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005605 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005606 rpc_put_task(task);
5607out:
5608 dprintk("<-- %s status=%d\n", __func__, ret);
5609 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005610}
5611
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005612struct nfs4_reclaim_complete_data {
5613 struct nfs_client *clp;
5614 struct nfs41_reclaim_complete_args arg;
5615 struct nfs41_reclaim_complete_res res;
5616};
5617
5618static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5619{
5620 struct nfs4_reclaim_complete_data *calldata = data;
5621
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005622 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04005623 if (nfs41_setup_sequence(calldata->clp->cl_session,
5624 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005625 &calldata->res.seq_res, task))
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005626 return;
5627
5628 rpc_call_start(task);
5629}
5630
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005631static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5632{
5633 switch(task->tk_status) {
5634 case 0:
5635 case -NFS4ERR_COMPLETE_ALREADY:
5636 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5637 break;
5638 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005639 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005640 /* fall through */
5641 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005642 return -EAGAIN;
5643 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005644 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005645 }
5646 return 0;
5647}
5648
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005649static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5650{
5651 struct nfs4_reclaim_complete_data *calldata = data;
5652 struct nfs_client *clp = calldata->clp;
5653 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5654
5655 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005656 if (!nfs41_sequence_done(task, res))
5657 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005658
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005659 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5660 rpc_restart_call_prepare(task);
5661 return;
5662 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005663 dprintk("<-- %s\n", __func__);
5664}
5665
5666static void nfs4_free_reclaim_complete_data(void *data)
5667{
5668 struct nfs4_reclaim_complete_data *calldata = data;
5669
5670 kfree(calldata);
5671}
5672
5673static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5674 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5675 .rpc_call_done = nfs4_reclaim_complete_done,
5676 .rpc_release = nfs4_free_reclaim_complete_data,
5677};
5678
5679/*
5680 * Issue a global reclaim complete.
5681 */
5682static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5683{
5684 struct nfs4_reclaim_complete_data *calldata;
5685 struct rpc_task *task;
5686 struct rpc_message msg = {
5687 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5688 };
5689 struct rpc_task_setup task_setup_data = {
5690 .rpc_client = clp->cl_rpcclient,
5691 .rpc_message = &msg,
5692 .callback_ops = &nfs4_reclaim_complete_call_ops,
5693 .flags = RPC_TASK_ASYNC,
5694 };
5695 int status = -ENOMEM;
5696
5697 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005698 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005699 if (calldata == NULL)
5700 goto out;
5701 calldata->clp = clp;
5702 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005703
Trond Myklebust9d12b212012-01-17 22:04:25 -05005704 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005705 msg.rpc_argp = &calldata->arg;
5706 msg.rpc_resp = &calldata->res;
5707 task_setup_data.callback_data = calldata;
5708 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005709 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005710 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005711 goto out;
5712 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05005713 status = nfs4_wait_for_completion_rpc_task(task);
5714 if (status == 0)
5715 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005716 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005717 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005718out:
5719 dprintk("<-- %s status=%d\n", __func__, status);
5720 return status;
5721}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005722
5723static void
5724nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5725{
5726 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00005727 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005728
5729 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00005730 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5731 * right now covering the LAYOUTGET we are about to send.
5732 * However, that is not so catastrophic, and there seems
5733 * to be no way to prevent it completely.
5734 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005735 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005736 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005737 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00005738 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5739 NFS_I(lgp->args.inode)->layout,
5740 lgp->args.ctx->state)) {
5741 rpc_exit(task, NFS4_OK);
5742 return;
5743 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005744 rpc_call_start(task);
5745}
5746
5747static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5748{
5749 struct nfs4_layoutget *lgp = calldata;
5750 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5751
5752 dprintk("--> %s\n", __func__);
5753
5754 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
5755 return;
5756
5757 switch (task->tk_status) {
5758 case 0:
5759 break;
5760 case -NFS4ERR_LAYOUTTRYLATER:
5761 case -NFS4ERR_RECALLCONFLICT:
5762 task->tk_status = -NFS4ERR_DELAY;
5763 /* Fall through */
5764 default:
5765 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5766 rpc_restart_call_prepare(task);
5767 return;
5768 }
5769 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005770 dprintk("<-- %s\n", __func__);
5771}
5772
5773static void nfs4_layoutget_release(void *calldata)
5774{
5775 struct nfs4_layoutget *lgp = calldata;
5776
5777 dprintk("--> %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005778 put_nfs_open_context(lgp->args.ctx);
5779 kfree(calldata);
5780 dprintk("<-- %s\n", __func__);
5781}
5782
5783static const struct rpc_call_ops nfs4_layoutget_call_ops = {
5784 .rpc_call_prepare = nfs4_layoutget_prepare,
5785 .rpc_call_done = nfs4_layoutget_done,
5786 .rpc_release = nfs4_layoutget_release,
5787};
5788
5789int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
5790{
5791 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5792 struct rpc_task *task;
5793 struct rpc_message msg = {
5794 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
5795 .rpc_argp = &lgp->args,
5796 .rpc_resp = &lgp->res,
5797 };
5798 struct rpc_task_setup task_setup_data = {
5799 .rpc_client = server->client,
5800 .rpc_message = &msg,
5801 .callback_ops = &nfs4_layoutget_call_ops,
5802 .callback_data = lgp,
5803 .flags = RPC_TASK_ASYNC,
5804 };
5805 int status = 0;
5806
5807 dprintk("--> %s\n", __func__);
5808
Weston Andros Adamson35124a02011-03-24 16:48:21 -04005809 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005810 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05005811 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005812 task = rpc_run_task(&task_setup_data);
5813 if (IS_ERR(task))
5814 return PTR_ERR(task);
5815 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00005816 if (status == 0)
5817 status = task->tk_status;
5818 if (status == 0)
5819 status = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005820 rpc_put_task(task);
5821 dprintk("<-- %s status=%d\n", __func__, status);
5822 return status;
5823}
5824
Benny Halevycbe82602011-05-22 19:52:37 +03005825static void
5826nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
5827{
5828 struct nfs4_layoutreturn *lrp = calldata;
5829
5830 dprintk("--> %s\n", __func__);
5831 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005832 &lrp->res.seq_res, task))
Benny Halevycbe82602011-05-22 19:52:37 +03005833 return;
5834 rpc_call_start(task);
5835}
5836
5837static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
5838{
5839 struct nfs4_layoutreturn *lrp = calldata;
5840 struct nfs_server *server;
Trond Myklebusta56aaa02011-06-15 11:59:10 -04005841 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03005842
5843 dprintk("--> %s\n", __func__);
5844
5845 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
5846 return;
5847
5848 server = NFS_SERVER(lrp->args.inode);
5849 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005850 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03005851 return;
5852 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04005853 spin_lock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03005854 if (task->tk_status == 0) {
Benny Halevycbe82602011-05-22 19:52:37 +03005855 if (lrp->res.lrs_present) {
Benny Halevycbe82602011-05-22 19:52:37 +03005856 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Benny Halevycbe82602011-05-22 19:52:37 +03005857 } else
5858 BUG_ON(!list_empty(&lo->plh_segs));
5859 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04005860 lo->plh_block_lgets--;
5861 spin_unlock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03005862 dprintk("<-- %s\n", __func__);
5863}
5864
5865static void nfs4_layoutreturn_release(void *calldata)
5866{
5867 struct nfs4_layoutreturn *lrp = calldata;
5868
5869 dprintk("--> %s\n", __func__);
Trond Myklebusta56aaa02011-06-15 11:59:10 -04005870 put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03005871 kfree(calldata);
5872 dprintk("<-- %s\n", __func__);
5873}
5874
5875static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
5876 .rpc_call_prepare = nfs4_layoutreturn_prepare,
5877 .rpc_call_done = nfs4_layoutreturn_done,
5878 .rpc_release = nfs4_layoutreturn_release,
5879};
5880
5881int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
5882{
5883 struct rpc_task *task;
5884 struct rpc_message msg = {
5885 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
5886 .rpc_argp = &lrp->args,
5887 .rpc_resp = &lrp->res,
5888 };
5889 struct rpc_task_setup task_setup_data = {
5890 .rpc_client = lrp->clp->cl_rpcclient,
5891 .rpc_message = &msg,
5892 .callback_ops = &nfs4_layoutreturn_call_ops,
5893 .callback_data = lrp,
5894 };
5895 int status;
5896
5897 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05005898 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03005899 task = rpc_run_task(&task_setup_data);
5900 if (IS_ERR(task))
5901 return PTR_ERR(task);
5902 status = task->tk_status;
5903 dprintk("<-- %s status=%d\n", __func__, status);
5904 rpc_put_task(task);
5905 return status;
5906}
5907
Andy Adamson7f11d8d2011-07-30 20:52:35 -04005908/*
5909 * Retrieve the list of Data Server devices from the MDS.
5910 */
5911static int _nfs4_getdevicelist(struct nfs_server *server,
5912 const struct nfs_fh *fh,
5913 struct pnfs_devicelist *devlist)
5914{
5915 struct nfs4_getdevicelist_args args = {
5916 .fh = fh,
5917 .layoutclass = server->pnfs_curr_ld->id,
5918 };
5919 struct nfs4_getdevicelist_res res = {
5920 .devlist = devlist,
5921 };
5922 struct rpc_message msg = {
5923 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
5924 .rpc_argp = &args,
5925 .rpc_resp = &res,
5926 };
5927 int status;
5928
5929 dprintk("--> %s\n", __func__);
5930 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
5931 &res.seq_res, 0);
5932 dprintk("<-- %s status=%d\n", __func__, status);
5933 return status;
5934}
5935
5936int nfs4_proc_getdevicelist(struct nfs_server *server,
5937 const struct nfs_fh *fh,
5938 struct pnfs_devicelist *devlist)
5939{
5940 struct nfs4_exception exception = { };
5941 int err;
5942
5943 do {
5944 err = nfs4_handle_exception(server,
5945 _nfs4_getdevicelist(server, fh, devlist),
5946 &exception);
5947 } while (exception.retry);
5948
5949 dprintk("%s: err=%d, num_devs=%u\n", __func__,
5950 err, devlist->num_devs);
5951
5952 return err;
5953}
5954EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
5955
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005956static int
5957_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5958{
5959 struct nfs4_getdeviceinfo_args args = {
5960 .pdev = pdev,
5961 };
5962 struct nfs4_getdeviceinfo_res res = {
5963 .pdev = pdev,
5964 };
5965 struct rpc_message msg = {
5966 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
5967 .rpc_argp = &args,
5968 .rpc_resp = &res,
5969 };
5970 int status;
5971
5972 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005973 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005974 dprintk("<-- %s status=%d\n", __func__, status);
5975
5976 return status;
5977}
5978
5979int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5980{
5981 struct nfs4_exception exception = { };
5982 int err;
5983
5984 do {
5985 err = nfs4_handle_exception(server,
5986 _nfs4_proc_getdeviceinfo(server, pdev),
5987 &exception);
5988 } while (exception.retry);
5989 return err;
5990}
5991EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
5992
Andy Adamson863a3c62011-03-23 13:27:54 +00005993static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
5994{
5995 struct nfs4_layoutcommit_data *data = calldata;
5996 struct nfs_server *server = NFS_SERVER(data->args.inode);
5997
5998 if (nfs4_setup_sequence(server, &data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005999 &data->res.seq_res, task))
Andy Adamson863a3c62011-03-23 13:27:54 +00006000 return;
6001 rpc_call_start(task);
6002}
6003
6004static void
6005nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6006{
6007 struct nfs4_layoutcommit_data *data = calldata;
6008 struct nfs_server *server = NFS_SERVER(data->args.inode);
6009
6010 if (!nfs4_sequence_done(task, &data->res.seq_res))
6011 return;
6012
6013 switch (task->tk_status) { /* Just ignore these failures */
6014 case NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6015 case NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6016 case NFS4ERR_BADLAYOUT: /* no layout */
6017 case NFS4ERR_GRACE: /* loca_recalim always false */
6018 task->tk_status = 0;
6019 }
6020
6021 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006022 rpc_restart_call_prepare(task);
Andy Adamson863a3c62011-03-23 13:27:54 +00006023 return;
6024 }
6025
6026 if (task->tk_status == 0)
6027 nfs_post_op_update_inode_force_wcc(data->args.inode,
6028 data->res.fattr);
6029}
6030
6031static void nfs4_layoutcommit_release(void *calldata)
6032{
6033 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04006034 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07006035 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00006036
Andy Adamsondb29c082011-07-30 20:52:38 -04006037 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006038 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04006039 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6040 list_del_init(&lseg->pls_lc_list);
6041 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6042 &lseg->pls_flags))
6043 put_lseg(lseg);
6044 }
Peng Tao92407e72011-10-23 20:21:17 -07006045
6046 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6047 smp_mb__after_clear_bit();
6048 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6049
Andy Adamson863a3c62011-03-23 13:27:54 +00006050 put_rpccred(data->cred);
6051 kfree(data);
6052}
6053
6054static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6055 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6056 .rpc_call_done = nfs4_layoutcommit_done,
6057 .rpc_release = nfs4_layoutcommit_release,
6058};
6059
6060int
Andy Adamsonef311532011-03-12 02:58:10 -05006061nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006062{
6063 struct rpc_message msg = {
6064 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6065 .rpc_argp = &data->args,
6066 .rpc_resp = &data->res,
6067 .rpc_cred = data->cred,
6068 };
6069 struct rpc_task_setup task_setup_data = {
6070 .task = &data->task,
6071 .rpc_client = NFS_CLIENT(data->args.inode),
6072 .rpc_message = &msg,
6073 .callback_ops = &nfs4_layoutcommit_ops,
6074 .callback_data = data,
6075 .flags = RPC_TASK_ASYNC,
6076 };
6077 struct rpc_task *task;
6078 int status = 0;
6079
6080 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6081 "lbw: %llu inode %lu\n",
6082 data->task.tk_pid, sync,
6083 data->args.lastbytewritten,
6084 data->args.inode->i_ino);
6085
Trond Myklebust9d12b212012-01-17 22:04:25 -05006086 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006087 task = rpc_run_task(&task_setup_data);
6088 if (IS_ERR(task))
6089 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006090 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006091 goto out;
6092 status = nfs4_wait_for_completion_rpc_task(task);
6093 if (status != 0)
6094 goto out;
6095 status = task->tk_status;
6096out:
6097 dprintk("%s: status %d\n", __func__, status);
6098 rpc_put_task(task);
6099 return status;
6100}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006101
6102static int
6103_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6104 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6105{
6106 struct nfs41_secinfo_no_name_args args = {
6107 .style = SECINFO_STYLE_CURRENT_FH,
6108 };
6109 struct nfs4_secinfo_res res = {
6110 .flavors = flavors,
6111 };
6112 struct rpc_message msg = {
6113 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6114 .rpc_argp = &args,
6115 .rpc_resp = &res,
6116 };
6117 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6118}
6119
6120static int
6121nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6122 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6123{
6124 struct nfs4_exception exception = { };
6125 int err;
6126 do {
6127 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6128 switch (err) {
6129 case 0:
6130 case -NFS4ERR_WRONGSEC:
6131 case -NFS4ERR_NOTSUPP:
6132 break;
6133 default:
6134 err = nfs4_handle_exception(server, err, &exception);
6135 }
6136 } while (exception.retry);
6137 return err;
6138}
6139
6140static int
6141nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6142 struct nfs_fsinfo *info)
6143{
6144 int err;
6145 struct page *page;
6146 rpc_authflavor_t flavor;
6147 struct nfs4_secinfo_flavors *flavors;
6148
6149 page = alloc_page(GFP_KERNEL);
6150 if (!page) {
6151 err = -ENOMEM;
6152 goto out;
6153 }
6154
6155 flavors = page_address(page);
6156 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6157
6158 /*
6159 * Fall back on "guess and check" method if
6160 * the server doesn't support SECINFO_NO_NAME
6161 */
6162 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6163 err = nfs4_find_root_sec(server, fhandle, info);
6164 goto out_freepage;
6165 }
6166 if (err)
6167 goto out_freepage;
6168
6169 flavor = nfs_find_best_sec(flavors);
6170 if (err == 0)
6171 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6172
6173out_freepage:
6174 put_page(page);
6175 if (err == -EACCES)
6176 return -EPERM;
6177out:
6178 return err;
6179}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006180
6181static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006182{
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006183 int status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006184 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006185 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006186 };
6187 struct nfs41_test_stateid_res res;
6188 struct rpc_message msg = {
6189 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6190 .rpc_argp = &args,
6191 .rpc_resp = &res,
6192 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006193
Trond Myklebust9d12b212012-01-17 22:04:25 -05006194 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006195 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
6196
6197 if (status == NFS_OK)
6198 return res.status;
6199 return status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006200}
6201
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006202static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006203{
6204 struct nfs4_exception exception = { };
6205 int err;
6206 do {
6207 err = nfs4_handle_exception(server,
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006208 _nfs41_test_stateid(server, stateid),
Bryan Schumaker7d974792011-06-02 14:59:08 -04006209 &exception);
6210 } while (exception.retry);
6211 return err;
6212}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006213
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006214static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006215{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006216 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006217 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006218 };
6219 struct nfs41_free_stateid_res res;
6220 struct rpc_message msg = {
6221 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6222 .rpc_argp = &args,
6223 .rpc_resp = &res,
6224 };
6225
Trond Myklebust9d12b212012-01-17 22:04:25 -05006226 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6227 return nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006228}
6229
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006230static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006231{
6232 struct nfs4_exception exception = { };
6233 int err;
6234 do {
6235 err = nfs4_handle_exception(server,
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006236 _nfs4_free_stateid(server, stateid),
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006237 &exception);
6238 } while (exception.retry);
6239 return err;
6240}
Andy Adamson557134a2009-04-01 09:21:53 -04006241#endif /* CONFIG_NFS_V4_1 */
6242
Andy Adamson591d71c2009-04-01 09:22:47 -04006243struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006244 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006245 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006246 .recover_open = nfs4_open_reclaim,
6247 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006248 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006249 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006250};
6251
Andy Adamson591d71c2009-04-01 09:22:47 -04006252#if defined(CONFIG_NFS_V4_1)
6253struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
6254 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6255 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6256 .recover_open = nfs4_open_reclaim,
6257 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006258 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006259 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006260 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04006261};
6262#endif /* CONFIG_NFS_V4_1 */
6263
6264struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006265 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006266 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006267 .recover_open = nfs4_open_expired,
6268 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006269 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006270 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006271};
6272
Andy Adamson591d71c2009-04-01 09:22:47 -04006273#if defined(CONFIG_NFS_V4_1)
6274struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
6275 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6276 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006277 .recover_open = nfs41_open_expired,
6278 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006279 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006280 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006281};
6282#endif /* CONFIG_NFS_V4_1 */
6283
Benny Halevy29fba382009-04-01 09:22:44 -04006284struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
6285 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006286 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006287 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006288};
6289
6290#if defined(CONFIG_NFS_V4_1)
6291struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
6292 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006293 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006294 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006295};
6296#endif
6297
Trond Myklebust97dc1352010-06-16 09:52:26 -04006298static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6299 .minor_version = 0,
6300 .call_sync = _nfs4_call_sync,
Trond Myklebuste047a102010-06-16 09:52:27 -04006301 .validate_stateid = nfs4_validate_delegation_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006302 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006303 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6304 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6305 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006306};
6307
6308#if defined(CONFIG_NFS_V4_1)
6309static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6310 .minor_version = 1,
6311 .call_sync = _nfs4_call_sync_session,
Trond Myklebuste047a102010-06-16 09:52:27 -04006312 .validate_stateid = nfs41_validate_delegation_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006313 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006314 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6315 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6316 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006317};
6318#endif
6319
Trond Myklebust97dc1352010-06-16 09:52:26 -04006320const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6321 [0] = &nfs_v4_0_minor_ops,
6322#if defined(CONFIG_NFS_V4_1)
6323 [1] = &nfs_v4_1_minor_ops,
6324#endif
6325};
6326
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006327static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006328 .permission = nfs_permission,
6329 .getattr = nfs_getattr,
6330 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006331 .getxattr = generic_getxattr,
6332 .setxattr = generic_setxattr,
6333 .listxattr = generic_listxattr,
6334 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006335};
6336
David Howells509de812006-08-22 20:06:11 -04006337const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006338 .version = 4, /* protocol version */
6339 .dentry_ops = &nfs4_dentry_operations,
6340 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006341 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006342 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006343 .getroot = nfs4_proc_get_root,
6344 .getattr = nfs4_proc_getattr,
6345 .setattr = nfs4_proc_setattr,
6346 .lookup = nfs4_proc_lookup,
6347 .access = nfs4_proc_access,
6348 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006349 .create = nfs4_proc_create,
6350 .remove = nfs4_proc_remove,
6351 .unlink_setup = nfs4_proc_unlink_setup,
6352 .unlink_done = nfs4_proc_unlink_done,
6353 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006354 .rename_setup = nfs4_proc_rename_setup,
6355 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006356 .link = nfs4_proc_link,
6357 .symlink = nfs4_proc_symlink,
6358 .mkdir = nfs4_proc_mkdir,
6359 .rmdir = nfs4_proc_remove,
6360 .readdir = nfs4_proc_readdir,
6361 .mknod = nfs4_proc_mknod,
6362 .statfs = nfs4_proc_statfs,
6363 .fsinfo = nfs4_proc_fsinfo,
6364 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006365 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006366 .decode_dirent = nfs4_decode_dirent,
6367 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05006368 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006369 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006370 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006371 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006372 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006373 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006374 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006375 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006376 .open_context = nfs4_atomic_open,
Andy Adamson45a52a02011-03-01 01:34:08 +00006377 .init_client = nfs4_init_client,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006378 .secinfo = nfs4_proc_secinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006379};
6380
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006381static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6382 .prefix = XATTR_NAME_NFSV4_ACL,
6383 .list = nfs4_xattr_list_nfs4_acl,
6384 .get = nfs4_xattr_get_nfs4_acl,
6385 .set = nfs4_xattr_set_nfs4_acl,
6386};
6387
6388const struct xattr_handler *nfs4_xattr_handlers[] = {
6389 &nfs4_xattr_nfs4_acl_handler,
6390 NULL
6391};
6392
Linus Torvalds1da177e2005-04-16 15:20:36 -07006393/*
6394 * Local variables:
6395 * c-basic-offset: 8
6396 * End:
6397 */