blob: 828a76590af92e73a0f12bdd3386b05ac2a00160 [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
85static int nfs41_test_stateid(struct nfs_server *, struct nfs4_state *);
86static int nfs41_free_stateid(struct nfs_server *, struct nfs4_state *);
87#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.
363 * If none found, highest_used_slotid is set to -1.
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
Andy Adamsonaabd0b42011-11-09 13:58:22 -0500368nfs4_free_slot(struct nfs4_slot_table *tbl, u8 free_slotid)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400369{
370 int slotid = free_slotid;
371
Benny Halevydfb4f3092010-09-24 09:17:01 -0400372 BUG_ON(slotid < 0 || slotid >= NFS4_MAX_SLOT_TABLE);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400373 /* clear used bit in bitmap */
374 __clear_bit(slotid, tbl->used_slots);
375
376 /* update highest_used_slotid when it is freed */
377 if (slotid == tbl->highest_used_slotid) {
378 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500379 if (slotid < tbl->max_slots)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400380 tbl->highest_used_slotid = slotid;
381 else
382 tbl->highest_used_slotid = -1;
383 }
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400384 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
385 free_slotid, tbl->highest_used_slotid);
386}
387
Trond Myklebust961a8282012-01-17 22:57:37 -0500388bool nfs4_set_task_privileged(struct rpc_task *task, void *dummy)
389{
390 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
391 return true;
392}
393
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800394/*
Andy Adamson42acd022011-01-06 02:04:34 +0000395 * Signal state manager thread if session fore channel is drained
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800396 */
Andy Adamson42acd022011-01-06 02:04:34 +0000397static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800398{
Trond Myklebusta2118c32010-06-16 09:52:26 -0400399 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
Trond Myklebust961a8282012-01-17 22:57:37 -0500400 rpc_wake_up_first(&ses->fc_slot_table.slot_tbl_waitq,
401 nfs4_set_task_privileged, NULL);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800402 return;
403 }
404
405 if (ses->fc_slot_table.highest_used_slotid != -1)
406 return;
407
Andy Adamson42acd022011-01-06 02:04:34 +0000408 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
409 complete(&ses->fc_slot_table.complete);
410}
411
412/*
413 * Signal state manager thread if session back channel is drained
414 */
415void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
416{
417 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
418 ses->bc_slot_table.highest_used_slotid != -1)
419 return;
420 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
421 complete(&ses->bc_slot_table.complete);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800422}
423
Trond Myklebustd185a332010-06-16 09:52:25 -0400424static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400425{
426 struct nfs4_slot_table *tbl;
427
Trond Myklebustd185a332010-06-16 09:52:25 -0400428 tbl = &res->sr_session->fc_slot_table;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400429 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400430 /* just wake up the next guy waiting since
431 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500432 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500433 return;
Andy Adamson13615872009-04-01 09:22:17 -0400434 }
Andy Adamsonea028ac2009-12-04 15:55:38 -0500435
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500436 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonaabd0b42011-11-09 13:58:22 -0500437 nfs4_free_slot(tbl, res->sr_slot - tbl->slots);
Andy Adamson42acd022011-01-06 02:04:34 +0000438 nfs4_check_drain_fc_complete(res->sr_session);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500439 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400440 res->sr_slot = NULL;
Andy Adamson13615872009-04-01 09:22:17 -0400441}
442
Trond Myklebust14516c32010-07-31 14:29:06 -0400443static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400444{
445 unsigned long timestamp;
Trond Myklebust14516c32010-07-31 14:29:06 -0400446 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400447
448 /*
449 * sr_status remains 1 if an RPC level error occurred. The server
450 * may or may not have processed the sequence operation..
451 * Proceed as if the server received and processed the sequence
452 * operation.
453 */
454 if (res->sr_status == 1)
455 res->sr_status = NFS_OK;
456
Bryan Schumaker468f8612011-04-18 15:57:32 -0400457 /* don't increment the sequence number if the task wasn't sent */
458 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400459 goto out;
460
Andy Adamson691daf32009-12-04 15:55:39 -0500461 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400462 switch (res->sr_status) {
463 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400464 /* Update the slot's sequence and clientid lease timer */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400465 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400466 timestamp = res->sr_renewal_time;
Trond Myklebust14516c32010-07-31 14:29:06 -0400467 clp = res->sr_session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400468 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500469 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500470 if (res->sr_status_flags != 0)
471 nfs4_schedule_lease_recovery(clp);
Trond Myklebust14516c32010-07-31 14:29:06 -0400472 break;
473 case -NFS4ERR_DELAY:
474 /* The server detected a resend of the RPC call and
475 * returned NFS4ERR_DELAY as per Section 2.10.6.2
476 * of RFC5661.
477 */
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200478 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400479 __func__,
480 res->sr_slot - res->sr_session->fc_slot_table.slots,
481 res->sr_slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400482 goto out_retry;
483 default:
484 /* Just update the slot sequence no. */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400485 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400486 }
487out:
488 /* The session may be reset by one of the error handlers. */
489 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400490 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400491 return 1;
492out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400493 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400494 goto out;
495 rpc_delay(task, NFS4_POLL_RETRY_MAX);
496 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400497}
498
Trond Myklebust14516c32010-07-31 14:29:06 -0400499static int nfs4_sequence_done(struct rpc_task *task,
500 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400501{
Trond Myklebust14516c32010-07-31 14:29:06 -0400502 if (res->sr_session == NULL)
503 return 1;
504 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400505}
506
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400507/*
Benny Halevy510b8172009-04-01 09:22:14 -0400508 * nfs4_find_slot - efficiently look for a free slot
509 *
510 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
511 * If found, we mark the slot as used, update the highest_used_slotid,
512 * and respectively set up the sequence operation args.
513 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400514 *
515 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400516 */
517static u8
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800518nfs4_find_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400519{
520 int slotid;
521 u8 ret_id = NFS4_MAX_SLOT_TABLE;
522 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
523
Benny Halevy510b8172009-04-01 09:22:14 -0400524 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
525 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
526 tbl->max_slots);
527 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
528 if (slotid >= tbl->max_slots)
529 goto out;
530 __set_bit(slotid, tbl->used_slots);
531 if (slotid > tbl->highest_used_slotid)
532 tbl->highest_used_slotid = slotid;
533 ret_id = slotid;
534out:
535 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
536 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400537 return ret_id;
538}
539
Trond Myklebust9d12b212012-01-17 22:04:25 -0500540static void nfs41_init_sequence(struct nfs4_sequence_args *args,
541 struct nfs4_sequence_res *res, int cache_reply)
542{
543 args->sa_session = NULL;
544 args->sa_cache_this = 0;
545 if (cache_reply)
546 args->sa_cache_this = 1;
547 res->sr_session = NULL;
548 res->sr_slot = NULL;
549}
550
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000551int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400552 struct nfs4_sequence_args *args,
553 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400554 struct rpc_task *task)
555{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400556 struct nfs4_slot *slot;
557 struct nfs4_slot_table *tbl;
558 u8 slotid;
559
560 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400561 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400562 if (res->sr_slot != NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400563 return 0;
564
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400565 tbl = &session->fc_slot_table;
566
567 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400568 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800569 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500570 /* The state manager will wait until the slot table is empty */
Andy Adamson05f0d232009-12-04 15:55:37 -0500571 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400572 spin_unlock(&tbl->slot_tbl_lock);
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500573 dprintk("%s session is draining\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500574 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400575 }
576
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800577 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
578 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
579 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
580 spin_unlock(&tbl->slot_tbl_lock);
581 dprintk("%s enforce FIFO order\n", __func__);
582 return -EAGAIN;
583 }
584
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800585 slotid = nfs4_find_slot(tbl);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400586 if (slotid == NFS4_MAX_SLOT_TABLE) {
587 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
588 spin_unlock(&tbl->slot_tbl_lock);
589 dprintk("<-- %s: no free slots\n", __func__);
590 return -EAGAIN;
591 }
592 spin_unlock(&tbl->slot_tbl_lock);
593
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800594 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400595 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400596 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400597 args->sa_slotid = slotid;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400598
599 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
600
Benny Halevy99fe60d2009-04-01 09:22:29 -0400601 res->sr_session = session;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400602 res->sr_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400603 res->sr_renewal_time = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400604 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400605 /*
606 * sr_status is only set in decode_sequence, and so will remain
607 * set to 1 if an rpc level failure occurs.
608 */
609 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400610 return 0;
611}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000612EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400613
Trond Myklebust035168a2010-06-16 09:52:26 -0400614int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400615 struct nfs4_sequence_args *args,
616 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400617 struct rpc_task *task)
618{
Trond Myklebust035168a2010-06-16 09:52:26 -0400619 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400620 int ret = 0;
621
Trond Myklebust9d12b212012-01-17 22:04:25 -0500622 if (session == NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400623 goto out;
Trond Myklebust035168a2010-06-16 09:52:26 -0400624
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200625 dprintk("--> %s clp %p session %p sr_slot %td\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400626 __func__, session->clp, session, res->sr_slot ?
627 res->sr_slot - session->fc_slot_table.slots : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400628
Trond Myklebust9d12b212012-01-17 22:04:25 -0500629 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400630out:
631 dprintk("<-- %s status=%d\n", __func__, ret);
632 return ret;
633}
634
635struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400636 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400637 struct nfs4_sequence_args *seq_args;
638 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400639};
640
641static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
642{
643 struct nfs41_call_sync_data *data = calldata;
644
Trond Myklebust035168a2010-06-16 09:52:26 -0400645 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
646
647 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -0500648 data->seq_res, task))
Andy Adamsonce5039c2009-04-01 09:22:13 -0400649 return;
650 rpc_call_start(task);
651}
652
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800653static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
654{
655 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
656 nfs41_call_sync_prepare(task, calldata);
657}
658
Andy Adamson69ab40c2009-04-01 09:22:19 -0400659static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
660{
661 struct nfs41_call_sync_data *data = calldata;
662
Trond Myklebust14516c32010-07-31 14:29:06 -0400663 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400664}
665
Andy Adamsonce5039c2009-04-01 09:22:13 -0400666struct rpc_call_ops nfs41_call_sync_ops = {
667 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400668 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400669};
670
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800671struct rpc_call_ops nfs41_call_priv_sync_ops = {
672 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
673 .rpc_call_done = nfs41_call_sync_done,
674};
675
Bryan Schumaker7c513052011-03-24 17:12:24 +0000676static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
677 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400678 struct rpc_message *msg,
679 struct nfs4_sequence_args *args,
680 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800681 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400682{
683 int ret;
684 struct rpc_task *task;
685 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400686 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400687 .seq_args = args,
688 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400689 };
690 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000691 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400692 .rpc_message = msg,
693 .callback_ops = &nfs41_call_sync_ops,
694 .callback_data = &data
695 };
696
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800697 if (privileged)
698 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400699 task = rpc_run_task(&task_setup);
700 if (IS_ERR(task))
701 ret = PTR_ERR(task);
702 else {
703 ret = task->tk_status;
704 rpc_put_task(task);
705 }
706 return ret;
707}
708
Bryan Schumaker7c513052011-03-24 17:12:24 +0000709int _nfs4_call_sync_session(struct rpc_clnt *clnt,
710 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400711 struct rpc_message *msg,
712 struct nfs4_sequence_args *args,
713 struct nfs4_sequence_res *res,
714 int cache_reply)
715{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500716 nfs41_init_sequence(args, res, cache_reply);
717 return nfs4_call_sync_sequence(clnt, server, msg, args, res, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400718}
719
Trond Myklebustdf896452010-06-16 09:52:26 -0400720#else
Trond Myklebust9d12b212012-01-17 22:04:25 -0500721static inline
722void nfs41_init_sequence(struct nfs4_sequence_args *args,
723 struct nfs4_sequence_res *res, int cache_reply)
724{
725}
726
Trond Myklebust14516c32010-07-31 14:29:06 -0400727static int nfs4_sequence_done(struct rpc_task *task,
728 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400729{
Trond Myklebust14516c32010-07-31 14:29:06 -0400730 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400731}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400732#endif /* CONFIG_NFS_V4_1 */
733
Bryan Schumaker7c513052011-03-24 17:12:24 +0000734int _nfs4_call_sync(struct rpc_clnt *clnt,
735 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400736 struct rpc_message *msg,
737 struct nfs4_sequence_args *args,
738 struct nfs4_sequence_res *res,
739 int cache_reply)
740{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500741 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000742 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400743}
744
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000745static inline
Bryan Schumaker7c513052011-03-24 17:12:24 +0000746int nfs4_call_sync(struct rpc_clnt *clnt,
747 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000748 struct rpc_message *msg,
749 struct nfs4_sequence_args *args,
750 struct nfs4_sequence_res *res,
751 int cache_reply)
752{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000753 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
754 args, res, cache_reply);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000755}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400756
Trond Myklebust38478b22006-05-25 01:40:57 -0400757static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
Trond Myklebust38478b22006-05-25 01:40:57 -0400759 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Trond Myklebust38478b22006-05-25 01:40:57 -0400761 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400762 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700763 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400764 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700765 dir->i_version = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400766 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767}
768
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100769struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400770 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100771 struct nfs_openargs o_arg;
772 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100773 struct nfs_open_confirmargs c_arg;
774 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500775 struct nfs4_string owner_name;
776 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100777 struct nfs_fattr f_attr;
778 struct nfs_fattr dir_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100779 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400780 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100781 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400782 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100783 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100784 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400785 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100786 int rpc_status;
787 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100788};
789
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400790
791static void nfs4_init_opendata_res(struct nfs4_opendata *p)
792{
793 p->o_res.f_attr = &p->f_attr;
794 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400795 p->o_res.seqid = p->o_arg.seqid;
796 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400797 p->o_res.server = p->o_arg.server;
798 nfs_fattr_init(&p->f_attr);
799 nfs_fattr_init(&p->dir_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500800 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400801}
802
Al Viro82a2c1b2011-06-22 18:30:55 -0400803static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500804 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400805 const struct iattr *attrs,
806 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100807{
Al Viro82a2c1b2011-06-22 18:30:55 -0400808 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100809 struct inode *dir = parent->d_inode;
810 struct nfs_server *server = NFS_SERVER(dir);
811 struct nfs4_opendata *p;
812
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400813 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100814 if (p == NULL)
815 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400816 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100817 if (p->o_arg.seqid == NULL)
818 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400819 nfs_sb_active(dentry->d_sb);
820 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100821 p->dir = parent;
822 p->owner = sp;
823 atomic_inc(&sp->so_count);
824 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500825 p->o_arg.open_flags = flags;
826 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400827 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -0500828 p->o_arg.id = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400829 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100830 p->o_arg.server = server;
831 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500832 p->o_arg.dir_bitmask = server->cache_consistency_bitmask;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100833 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust536e43d2012-01-17 22:04:26 -0500834 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400835 u32 *s;
836
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100837 p->o_arg.u.attrs = &p->attrs;
838 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400839 s = (u32 *) p->o_arg.u.verifier.data;
840 s[0] = jiffies;
841 s[1] = current->pid;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100842 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100843 p->c_arg.fh = &p->o_res.fh;
844 p->c_arg.stateid = &p->o_res.stateid;
845 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400846 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400847 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100848 return p;
849err_free:
850 kfree(p);
851err:
852 dput(parent);
853 return NULL;
854}
855
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400856static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100857{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400858 struct nfs4_opendata *p = container_of(kref,
859 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400860 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400861
862 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400863 if (p->state != NULL)
864 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400865 nfs4_put_state_owner(p->owner);
866 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400867 dput(p->dentry);
868 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500869 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400870 kfree(p);
871}
872
873static void nfs4_opendata_put(struct nfs4_opendata *p)
874{
875 if (p != NULL)
876 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100877}
878
Trond Myklebust06f814a2006-01-03 09:55:07 +0100879static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
880{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100881 int ret;
882
Trond Myklebust06f814a2006-01-03 09:55:07 +0100883 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100884 return ret;
885}
886
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500887static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400888{
889 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500890
Trond Myklebust536e43d2012-01-17 22:04:26 -0500891 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500892 goto out;
893 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400894 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500895 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
896 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400897 break;
898 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500899 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
900 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400901 break;
902 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500903 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
904 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400905 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500906out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400907 return ret;
908}
909
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500910static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400911{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500912 if (delegation == NULL)
913 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500914 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400915 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500916 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400917 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500918 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400919 return 1;
920}
921
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500922static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100923{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500924 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100925 case FMODE_WRITE:
926 state->n_wronly++;
927 break;
928 case FMODE_READ:
929 state->n_rdonly++;
930 break;
931 case FMODE_READ|FMODE_WRITE:
932 state->n_rdwr++;
933 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500934 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100935}
936
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500937static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400938{
939 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
940 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
941 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500942 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400943 case FMODE_READ:
944 set_bit(NFS_O_RDONLY_STATE, &state->flags);
945 break;
946 case FMODE_WRITE:
947 set_bit(NFS_O_WRONLY_STATE, &state->flags);
948 break;
949 case FMODE_READ|FMODE_WRITE:
950 set_bit(NFS_O_RDWR_STATE, &state->flags);
951 }
952}
953
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500954static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400955{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400956 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500957 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400958 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400959}
960
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500961static 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 -0700962{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400963 /*
964 * Protect the call to nfs4_state_set_mode_locked and
965 * serialise the stateid update
966 */
967 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400968 if (deleg_stateid != NULL) {
969 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
970 set_bit(NFS_DELEGATED_STATE, &state->flags);
971 }
972 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500973 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400974 write_sequnlock(&state->seqlock);
975 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500976 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700977 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978}
979
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500980static 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 -0500981{
982 struct nfs_inode *nfsi = NFS_I(state->inode);
983 struct nfs_delegation *deleg_cur;
984 int ret = 0;
985
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500986 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500987
988 rcu_read_lock();
989 deleg_cur = rcu_dereference(nfsi->delegation);
990 if (deleg_cur == NULL)
991 goto no_delegation;
992
993 spin_lock(&deleg_cur->lock);
994 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500995 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500996 goto no_delegation_unlock;
997
998 if (delegation == NULL)
999 delegation = &deleg_cur->stateid;
1000 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
1001 goto no_delegation_unlock;
1002
Trond Myklebustb7391f42008-12-23 15:21:52 -05001003 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001004 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001005 ret = 1;
1006no_delegation_unlock:
1007 spin_unlock(&deleg_cur->lock);
1008no_delegation:
1009 rcu_read_unlock();
1010
1011 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001012 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001013 ret = 1;
1014 }
1015
1016 return ret;
1017}
1018
1019
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001020static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001021{
1022 struct nfs_delegation *delegation;
1023
1024 rcu_read_lock();
1025 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001026 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001027 rcu_read_unlock();
1028 return;
1029 }
1030 rcu_read_unlock();
1031 nfs_inode_return_delegation(inode);
1032}
1033
Trond Myklebust6ee41262007-07-08 14:11:36 -04001034static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001035{
1036 struct nfs4_state *state = opendata->state;
1037 struct nfs_inode *nfsi = NFS_I(state->inode);
1038 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -05001039 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001040 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001041 nfs4_stateid stateid;
1042 int ret = -EAGAIN;
1043
Trond Myklebustaac00a82007-07-05 19:02:21 -04001044 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001045 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001046 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001047 if (can_open_cached(state, fmode, open_mode)) {
1048 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001049 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001050 goto out_return_state;
1051 }
1052 spin_unlock(&state->owner->so_lock);
1053 }
Trond Myklebust34310432008-12-23 15:21:38 -05001054 rcu_read_lock();
1055 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001056 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001057 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001058 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001059 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001060 /* Save the delegation */
1061 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
1062 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001063 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001064 if (ret != 0)
1065 goto out;
1066 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001067
1068 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001069 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001070 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001071 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001072out:
1073 return ERR_PTR(ret);
1074out_return_state:
1075 atomic_inc(&state->count);
1076 return state;
1077}
1078
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001079static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1080{
1081 struct inode *inode;
1082 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -04001083 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001084 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001085
Trond Myklebustaac00a82007-07-05 19:02:21 -04001086 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001087 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001088 goto out;
1089 }
1090
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001091 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001092 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001093 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001094 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001095 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001096 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001097 goto err;
1098 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001099 state = nfs4_get_open_state(inode, data->owner);
1100 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001101 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001102 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001103 int delegation_flags = 0;
1104
Trond Myklebust003707c2007-07-05 18:07:55 -04001105 rcu_read_lock();
1106 delegation = rcu_dereference(NFS_I(inode)->delegation);
1107 if (delegation)
1108 delegation_flags = delegation->flags;
1109 rcu_read_unlock();
Trond Myklebust652f89f2011-12-09 19:05:58 -05001110 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1111 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1112 "returning a delegation for "
1113 "OPEN(CLAIM_DELEGATE_CUR)\n",
1114 NFS_CLIENT(inode)->cl_server);
1115 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001116 nfs_inode_set_delegation(state->inode,
1117 data->owner->so_cred,
1118 &data->o_res);
1119 else
1120 nfs_inode_reclaim_delegation(state->inode,
1121 data->owner->so_cred,
1122 &data->o_res);
1123 }
Trond Myklebust34310432008-12-23 15:21:38 -05001124
1125 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001126 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001127 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001128out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001129 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001130err_put_inode:
1131 iput(inode);
1132err:
1133 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001134}
1135
Trond Myklebust864472e2006-01-03 09:55:15 +01001136static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1137{
1138 struct nfs_inode *nfsi = NFS_I(state->inode);
1139 struct nfs_open_context *ctx;
1140
1141 spin_lock(&state->inode->i_lock);
1142 list_for_each_entry(ctx, &nfsi->open_files, list) {
1143 if (ctx->state != state)
1144 continue;
1145 get_nfs_open_context(ctx);
1146 spin_unlock(&state->inode->i_lock);
1147 return ctx;
1148 }
1149 spin_unlock(&state->inode->i_lock);
1150 return ERR_PTR(-ENOENT);
1151}
1152
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001153static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1154{
1155 struct nfs4_opendata *opendata;
1156
Al Viro3d4ff432011-06-22 18:40:12 -04001157 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001158 if (opendata == NULL)
1159 return ERR_PTR(-ENOMEM);
1160 opendata->state = state;
1161 atomic_inc(&state->count);
1162 return opendata;
1163}
1164
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001165static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001166{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001167 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001168 int ret;
1169
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001170 opendata->o_arg.open_flags = 0;
1171 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001172 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1173 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1174 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001175 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001176 if (ret != 0)
1177 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001178 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001179 if (IS_ERR(newstate))
1180 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001181 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001182 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001183 return 0;
1184}
1185
1186static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1187{
Trond Myklebust864472e2006-01-03 09:55:15 +01001188 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001189 int ret;
1190
1191 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001192 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001193 smp_rmb();
1194 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001195 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001196 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001197 if (ret != 0)
1198 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001199 if (newstate != state)
1200 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001201 }
1202 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001203 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001204 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001205 if (ret != 0)
1206 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001207 if (newstate != state)
1208 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001209 }
1210 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001211 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001212 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001213 if (ret != 0)
1214 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001215 if (newstate != state)
1216 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001217 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001218 /*
1219 * We may have performed cached opens for all three recoveries.
1220 * Check if we need to update the current stateid.
1221 */
1222 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1223 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001224 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001225 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1226 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001227 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001228 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001229 return 0;
1230}
1231
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232/*
1233 * OPEN_RECLAIM:
1234 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001236static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001238 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001239 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001240 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 int status;
1242
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001243 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1244 if (IS_ERR(opendata))
1245 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001246 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1247 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001248 rcu_read_lock();
1249 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001250 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001251 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001252 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001253 opendata->o_arg.u.delegation_type = delegation_type;
1254 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001255 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 return status;
1257}
1258
Trond Myklebust539cd032007-06-05 11:46:42 -04001259static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260{
1261 struct nfs_server *server = NFS_SERVER(state->inode);
1262 struct nfs4_exception exception = { };
1263 int err;
1264 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001265 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001266 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001267 break;
1268 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 } while (exception.retry);
1270 return err;
1271}
1272
Trond Myklebust864472e2006-01-03 09:55:15 +01001273static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1274{
1275 struct nfs_open_context *ctx;
1276 int ret;
1277
1278 ctx = nfs4_state_find_open_context(state);
1279 if (IS_ERR(ctx))
1280 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001281 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001282 put_nfs_open_context(ctx);
1283 return ret;
1284}
1285
Trond Myklebust13437e12007-07-06 15:10:43 -04001286static 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 -07001287{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001288 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001289 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001291 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1292 if (IS_ERR(opendata))
1293 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001294 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -04001295 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001296 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +01001297 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001298 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001299 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300}
1301
Trond Myklebust13437e12007-07-06 15:10:43 -04001302int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303{
1304 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001305 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 int err;
1307 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001308 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 switch (err) {
1310 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001311 case -ENOENT:
1312 case -ESTALE:
1313 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001314 case -NFS4ERR_BADSESSION:
1315 case -NFS4ERR_BADSLOT:
1316 case -NFS4ERR_BAD_HIGH_SLOT:
1317 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1318 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001319 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001320 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 case -NFS4ERR_STALE_CLIENTID:
1322 case -NFS4ERR_STALE_STATEID:
1323 case -NFS4ERR_EXPIRED:
1324 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001325 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001326 goto out;
1327 case -ERESTARTSYS:
1328 /*
1329 * The show must go on: exit, but mark the
1330 * stateid as needing recovery.
1331 */
1332 case -NFS4ERR_ADMIN_REVOKED:
1333 case -NFS4ERR_BAD_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001334 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001335 case -EKEYEXPIRED:
1336 /*
1337 * User RPCSEC_GSS context has expired.
1338 * We cannot recover this stateid now, so
1339 * skip it and allow recovery thread to
1340 * proceed.
1341 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001342 case -ENOMEM:
1343 err = 0;
1344 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 }
1346 err = nfs4_handle_exception(server, err, &exception);
1347 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001348out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 return err;
1350}
1351
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001352static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1353{
1354 struct nfs4_opendata *data = calldata;
1355
1356 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001357 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001358 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1359 sizeof(data->o_res.stateid.data));
Trond Myklebustbb226292008-01-02 15:19:18 -05001360 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001361 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001362 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001363 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001364}
1365
1366static void nfs4_open_confirm_release(void *calldata)
1367{
1368 struct nfs4_opendata *data = calldata;
1369 struct nfs4_state *state = NULL;
1370
1371 /* If this request hasn't been cancelled, do nothing */
1372 if (data->cancelled == 0)
1373 goto out_free;
1374 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001375 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001376 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001377 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001378 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001379 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001380out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001381 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001382}
1383
1384static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001385 .rpc_call_done = nfs4_open_confirm_done,
1386 .rpc_release = nfs4_open_confirm_release,
1387};
1388
1389/*
1390 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1391 */
1392static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1393{
1394 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1395 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001396 struct rpc_message msg = {
1397 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1398 .rpc_argp = &data->c_arg,
1399 .rpc_resp = &data->c_res,
1400 .rpc_cred = data->owner->so_cred,
1401 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001402 struct rpc_task_setup task_setup_data = {
1403 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001404 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001405 .callback_ops = &nfs4_open_confirm_ops,
1406 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001407 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001408 .flags = RPC_TASK_ASYNC,
1409 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 int status;
1411
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001412 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001413 data->rpc_done = 0;
1414 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001415 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001416 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001417 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001418 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001419 status = nfs4_wait_for_completion_rpc_task(task);
1420 if (status != 0) {
1421 data->cancelled = 1;
1422 smp_wmb();
1423 } else
1424 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001425 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 return status;
1427}
1428
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001429static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001431 struct nfs4_opendata *data = calldata;
1432 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001433
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001434 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1435 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001436 /*
1437 * Check if we still need to send an OPEN call, or if we can use
1438 * a delegation instead.
1439 */
1440 if (data->state != NULL) {
1441 struct nfs_delegation *delegation;
1442
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001443 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001444 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001445 rcu_read_lock();
1446 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001447 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1448 can_open_delegated(delegation, data->o_arg.fmode))
1449 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001450 rcu_read_unlock();
1451 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001452 /* Update sequence id. */
Trond Myklebust48c22eb2012-01-17 22:04:25 -05001453 data->o_arg.id = sp->so_seqid.owner_id;
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001454 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001455 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001456 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001457 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1458 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001459 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001460 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001461 &data->o_arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05001462 &data->o_res.seq_res, task))
Andy Adamsond8985282009-04-01 09:22:21 -04001463 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001464 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001465 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001466unlock_no_action:
1467 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001468out_no_action:
1469 task->tk_action = NULL;
1470
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001471}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001473static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1474{
1475 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1476 nfs4_open_prepare(task, calldata);
1477}
1478
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001479static void nfs4_open_done(struct rpc_task *task, void *calldata)
1480{
1481 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001483 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001484
Trond Myklebust14516c32010-07-31 14:29:06 -04001485 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1486 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001487
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001488 if (task->tk_status == 0) {
1489 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001490 case S_IFREG:
1491 break;
1492 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001493 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001494 break;
1495 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001496 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001497 break;
1498 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001499 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001500 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001501 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001502 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1503 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001504 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001505 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001506}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001507
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001508static void nfs4_open_release(void *calldata)
1509{
1510 struct nfs4_opendata *data = calldata;
1511 struct nfs4_state *state = NULL;
1512
1513 /* If this request hasn't been cancelled, do nothing */
1514 if (data->cancelled == 0)
1515 goto out_free;
1516 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001517 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001518 goto out_free;
1519 /* In case we need an open_confirm, no cleanup! */
1520 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1521 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001522 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001523 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001524 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001525out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001526 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001527}
1528
1529static const struct rpc_call_ops nfs4_open_ops = {
1530 .rpc_call_prepare = nfs4_open_prepare,
1531 .rpc_call_done = nfs4_open_done,
1532 .rpc_release = nfs4_open_release,
1533};
1534
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001535static const struct rpc_call_ops nfs4_recover_open_ops = {
1536 .rpc_call_prepare = nfs4_recover_open_prepare,
1537 .rpc_call_done = nfs4_open_done,
1538 .rpc_release = nfs4_open_release,
1539};
1540
1541static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001542{
1543 struct inode *dir = data->dir->d_inode;
1544 struct nfs_server *server = NFS_SERVER(dir);
1545 struct nfs_openargs *o_arg = &data->o_arg;
1546 struct nfs_openres *o_res = &data->o_res;
1547 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001548 struct rpc_message msg = {
1549 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1550 .rpc_argp = o_arg,
1551 .rpc_resp = o_res,
1552 .rpc_cred = data->owner->so_cred,
1553 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001554 struct rpc_task_setup task_setup_data = {
1555 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001556 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001557 .callback_ops = &nfs4_open_ops,
1558 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001559 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001560 .flags = RPC_TASK_ASYNC,
1561 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001562 int status;
1563
Trond Myklebust9d12b212012-01-17 22:04:25 -05001564 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001565 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001566 data->rpc_done = 0;
1567 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001568 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001569 if (isrecover)
1570 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001571 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001572 if (IS_ERR(task))
1573 return PTR_ERR(task);
1574 status = nfs4_wait_for_completion_rpc_task(task);
1575 if (status != 0) {
1576 data->cancelled = 1;
1577 smp_wmb();
1578 } else
1579 status = data->rpc_status;
1580 rpc_put_task(task);
1581
1582 return status;
1583}
1584
1585static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1586{
1587 struct inode *dir = data->dir->d_inode;
1588 struct nfs_openres *o_res = &data->o_res;
1589 int status;
1590
1591 status = nfs4_run_open_task(data, 1);
1592 if (status != 0 || !data->rpc_done)
1593 return status;
1594
Trond Myklebust6926afd2012-01-07 13:22:46 -05001595 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1596
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001597 nfs_refresh_inode(dir, o_res->dir_attr);
1598
1599 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1600 status = _nfs4_proc_open_confirm(data);
1601 if (status != 0)
1602 return status;
1603 }
1604
1605 return status;
1606}
1607
1608/*
1609 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1610 */
1611static int _nfs4_proc_open(struct nfs4_opendata *data)
1612{
1613 struct inode *dir = data->dir->d_inode;
1614 struct nfs_server *server = NFS_SERVER(dir);
1615 struct nfs_openargs *o_arg = &data->o_arg;
1616 struct nfs_openres *o_res = &data->o_res;
1617 int status;
1618
1619 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001620 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001621 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001622 if (status != 0) {
1623 if (status == -NFS4ERR_BADNAME &&
1624 !(o_arg->open_flags & O_CREAT))
1625 return -ENOENT;
1626 return status;
1627 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001628
Trond Myklebust6926afd2012-01-07 13:22:46 -05001629 nfs_fattr_map_and_free_names(server, &data->f_attr);
1630
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001631 if (o_arg->open_flags & O_CREAT) {
1632 update_changeattr(dir, &o_res->cinfo);
1633 nfs_post_op_update_inode(dir, o_res->dir_attr);
1634 } else
1635 nfs_refresh_inode(dir, o_res->dir_attr);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001636 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1637 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001639 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001641 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 }
1643 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001644 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001645 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646}
1647
Andy Adamsond83217c2011-03-01 01:34:17 +00001648static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +01001649{
Trond Myklebusta78cb572009-08-09 15:06:19 -04001650 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001651 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001652
Trond Myklebusta78cb572009-08-09 15:06:19 -04001653 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001654 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001655 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001656 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001657 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1658 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001659 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001660 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001661 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001662 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001663 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001664}
1665
Andy Adamsond83217c2011-03-01 01:34:17 +00001666static int nfs4_recover_expired_lease(struct nfs_server *server)
1667{
1668 return nfs4_client_recover_expired_lease(server->nfs_client);
1669}
1670
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671/*
1672 * OPEN_EXPIRED:
1673 * reclaim state on the server after a network partition.
1674 * Assumes caller holds the appropriate lock
1675 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001676static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001678 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001679 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001681 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1682 if (IS_ERR(opendata))
1683 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001684 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001685 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001686 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001687 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001688 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689}
1690
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001691static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001692{
Trond Myklebust539cd032007-06-05 11:46:42 -04001693 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001694 struct nfs4_exception exception = { };
1695 int err;
1696
1697 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001698 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001699 switch (err) {
1700 default:
1701 goto out;
1702 case -NFS4ERR_GRACE:
1703 case -NFS4ERR_DELAY:
1704 nfs4_handle_exception(server, err, &exception);
1705 err = 0;
1706 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001707 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001708out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001709 return err;
1710}
1711
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1713{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001715 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
Trond Myklebust864472e2006-01-03 09:55:15 +01001717 ctx = nfs4_state_find_open_context(state);
1718 if (IS_ERR(ctx))
1719 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001720 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001721 put_nfs_open_context(ctx);
1722 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723}
1724
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001725#if defined(CONFIG_NFS_V4_1)
1726static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1727{
1728 int status;
1729 struct nfs_server *server = NFS_SERVER(state->inode);
1730
1731 status = nfs41_test_stateid(server, state);
1732 if (status == NFS_OK)
1733 return 0;
1734 nfs41_free_stateid(server, state);
1735 return nfs4_open_expired(sp, state);
1736}
1737#endif
1738
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001740 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1741 * fields corresponding to attributes that were used to store the verifier.
1742 * Make sure we clobber those fields in the later setattr call
1743 */
1744static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1745{
1746 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1747 !(sattr->ia_valid & ATTR_ATIME_SET))
1748 sattr->ia_valid |= ATTR_ATIME;
1749
1750 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1751 !(sattr->ia_valid & ATTR_MTIME_SET))
1752 sattr->ia_valid |= ATTR_MTIME;
1753}
1754
1755/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001756 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 */
Al Viro82a2c1b2011-06-22 18:30:55 -04001758static 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 -07001759{
1760 struct nfs4_state_owner *sp;
1761 struct nfs4_state *state = NULL;
1762 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001763 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001764 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
1766 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001768 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1769 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1771 goto out_err;
1772 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001773 status = nfs4_recover_expired_lease(server);
1774 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001775 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001776 if (dentry->d_inode != NULL)
1777 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001778 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001779 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001780 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001781 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
Al Viro82a2c1b2011-06-22 18:30:55 -04001783 if (dentry->d_inode != NULL)
1784 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001785
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001786 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001788 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001790 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001791 status = PTR_ERR(state);
1792 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001793 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001794 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001795 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001796
1797 if (opendata->o_arg.open_flags & O_EXCL) {
1798 nfs4_exclusive_attrset(opendata, sattr);
1799
1800 nfs_fattr_init(opendata->o_res.f_attr);
1801 status = nfs4_do_setattr(state->inode, cred,
1802 opendata->o_res.f_attr, sattr,
1803 state);
1804 if (status == 0)
1805 nfs_setattr_update_inode(state->inode, sattr);
1806 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1807 }
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001808 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 *res = state;
1811 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001812err_opendata_put:
1813 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001814err_put_state_owner:
1815 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 *res = NULL;
1818 return status;
1819}
1820
1821
Al Viro82a2c1b2011-06-22 18:30:55 -04001822static 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 -07001823{
1824 struct nfs4_exception exception = { };
1825 struct nfs4_state *res;
1826 int status;
1827
1828 do {
Al Viro82a2c1b2011-06-22 18:30:55 -04001829 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 if (status == 0)
1831 break;
1832 /* NOTE: BAD_SEQID means the server and client disagree about the
1833 * book-keeping w.r.t. state-changing operations
1834 * (OPEN/CLOSE/LOCK/LOCKU...)
1835 * It is actually a sign of a bug on the client or on the server.
1836 *
1837 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001838 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 * have unhashed the old state_owner for us, and that we can
1840 * therefore safely retry using a new one. We should still warn
1841 * the user though...
1842 */
1843 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001844 printk(KERN_WARNING "NFS: v4 server %s "
1845 " returned a bad sequence-id error!\n",
1846 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 exception.retry = 1;
1848 continue;
1849 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001850 /*
1851 * BAD_STATEID on OPEN means that the server cancelled our
1852 * state before it received the OPEN_CONFIRM.
1853 * Recover by retrying the request as per the discussion
1854 * on Page 181 of RFC3530.
1855 */
1856 if (status == -NFS4ERR_BAD_STATEID) {
1857 exception.retry = 1;
1858 continue;
1859 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001860 if (status == -EAGAIN) {
1861 /* We must have found a delegation */
1862 exception.retry = 1;
1863 continue;
1864 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1866 status, &exception));
1867 } while (exception.retry);
1868 return res;
1869}
1870
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001871static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1872 struct nfs_fattr *fattr, struct iattr *sattr,
1873 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001875 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001877 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 .iap = sattr,
1879 .server = server,
1880 .bitmask = server->attr_bitmask,
1881 };
1882 struct nfs_setattrres res = {
1883 .fattr = fattr,
1884 .server = server,
1885 };
1886 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001887 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1888 .rpc_argp = &arg,
1889 .rpc_resp = &res,
1890 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001892 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001893 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
Trond Myklebust0e574af2005-10-27 22:12:38 -04001895 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001897 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1898 /* Use that stateid */
1899 } else if (state != NULL) {
Trond Myklebust77041ed2010-07-01 12:49:11 -04001900 nfs4_copy_stateid(&arg.stateid, state, current->files, current->tgid);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001901 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1903
Bryan Schumaker7c513052011-03-24 17:12:24 +00001904 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001905 if (status == 0 && state != NULL)
1906 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001907 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908}
1909
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001910static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1911 struct nfs_fattr *fattr, struct iattr *sattr,
1912 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001914 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 struct nfs4_exception exception = { };
1916 int err;
1917 do {
1918 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001919 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 &exception);
1921 } while (exception.retry);
1922 return err;
1923}
1924
1925struct nfs4_closedata {
1926 struct inode *inode;
1927 struct nfs4_state *state;
1928 struct nfs_closeargs arg;
1929 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001930 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001931 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00001932 bool roc;
1933 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934};
1935
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001936static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001937{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001938 struct nfs4_closedata *calldata = data;
1939 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04001940 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07001941
Fred Isamanf7e89172011-01-06 11:36:32 +00001942 if (calldata->roc)
1943 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07001944 nfs4_put_open_state(calldata->state);
1945 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001946 nfs4_put_state_owner(sp);
Al Viro643168c2011-06-22 18:20:23 -04001947 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07001948 kfree(calldata);
1949}
1950
Trond Myklebust88069f72009-12-08 08:33:16 -05001951static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1952 fmode_t fmode)
1953{
1954 spin_lock(&state->owner->so_lock);
1955 if (!(fmode & FMODE_READ))
1956 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1957 if (!(fmode & FMODE_WRITE))
1958 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1959 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1960 spin_unlock(&state->owner->so_lock);
1961}
1962
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001963static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001965 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 struct nfs_server *server = NFS_SERVER(calldata->inode);
1968
Trond Myklebust14516c32010-07-31 14:29:06 -04001969 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
1970 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 /* hmm. we are done with the inode, and in the process of freeing
1972 * the state_owner. we keep this around to process errors
1973 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 switch (task->tk_status) {
1975 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00001976 if (calldata->roc)
1977 pnfs_roc_set_barrier(state->inode,
1978 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04001979 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001980 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05001981 nfs4_close_clear_stateid_flags(state,
1982 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 break;
1984 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001985 case -NFS4ERR_OLD_STATEID:
1986 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001988 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001989 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05001991 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1992 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 }
Trond Myklebust72211db2009-12-15 14:47:36 -05001994 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04001995 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996}
1997
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001998static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002000 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002001 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05002002 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002003
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002004 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07002005 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04002006
Trond Myklebust88069f72009-12-08 08:33:16 -05002007 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2008 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002009 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002010 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002011 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002012 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002013 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2014 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2015 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002016 }
2017 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002018 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2019 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2020 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002021 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002022 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002023 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002024
2025 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002026 /* Note: exit _without_ calling nfs4_close_done */
2027 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07002028 return;
2029 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002030
Fred Isamanf7e89172011-01-06 11:36:32 +00002031 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002032 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002033 if (calldata->roc &&
2034 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
2035 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
2036 task, NULL);
2037 return;
2038 }
2039 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002040
Trond Myklebust516a6af2005-10-27 22:12:41 -04002041 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002042 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04002043 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002044 &calldata->arg.seq_args,
2045 &calldata->res.seq_res,
2046 task))
Andy Adamson19ddab02009-04-01 09:22:20 -04002047 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002048 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049}
2050
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002051static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002052 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002053 .rpc_call_done = nfs4_close_done,
2054 .rpc_release = nfs4_free_closedata,
2055};
2056
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057/*
2058 * It is possible for data to be read/written from a mem-mapped file
2059 * after the sys_close call (which hits the vfs layer as a flush).
2060 * This means that we can't safely call nfsv4 close on a file until
2061 * the inode is cleared. This in turn means that we are not good
2062 * NFSv4 citizens - we do not indicate to the server to update the file's
2063 * share state even when we are done with one of the three share
2064 * stateid's in the inode.
2065 *
2066 * NOTE: Caller must be holding the sp->so_owner semaphore!
2067 */
Al Viro643168c2011-06-22 18:20:23 -04002068int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002070 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002072 struct nfs4_state_owner *sp = state->owner;
2073 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002074 struct rpc_message msg = {
2075 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2076 .rpc_cred = state->owner->so_cred,
2077 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002078 struct rpc_task_setup task_setup_data = {
2079 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002080 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002081 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002082 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002083 .flags = RPC_TASK_ASYNC,
2084 };
Trond Myklebust95121352005-10-18 14:20:12 -07002085 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002087 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002089 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002090 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002091 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002093 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002094 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002096 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002097 if (calldata->arg.seqid == NULL)
2098 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002099 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002100 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002101 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002102 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002103 calldata->res.server = server;
Fred Isamanf7e89172011-01-06 11:36:32 +00002104 calldata->roc = roc;
Al Viro643168c2011-06-22 18:20:23 -04002105 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002106
Trond Myklebust1174dd12010-12-21 10:52:24 -05002107 msg.rpc_argp = &calldata->arg;
2108 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002109 task_setup_data.callback_data = calldata;
2110 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002111 if (IS_ERR(task))
2112 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002113 status = 0;
2114 if (wait)
2115 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002116 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002117 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002118out_free_calldata:
2119 kfree(calldata);
2120out:
Fred Isamanf7e89172011-01-06 11:36:32 +00002121 if (roc)
2122 pnfs_roc_release(state->inode);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002123 nfs4_put_open_state(state);
2124 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002125 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126}
2127
Trond Myklebust2b484292010-09-17 10:56:51 -04002128static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002129nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 struct nfs4_state *state;
2132
Trond Myklebust565277f2007-10-15 18:17:53 -04002133 /* Protect against concurrent sillydeletes */
Al Viro3d4ff432011-06-22 18:40:12 -04002134 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr, ctx->cred);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002135 if (IS_ERR(state))
2136 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002137 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002138 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139}
2140
Trond Myklebust1185a552009-12-03 15:54:02 -05002141static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002142{
2143 if (ctx->state == NULL)
2144 return;
2145 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002146 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002147 else
Al Viro643168c2011-06-22 18:20:23 -04002148 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002149}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150
2151static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2152{
Benny Halevy43652ad2009-04-01 09:21:54 -04002153 struct nfs4_server_caps_arg args = {
2154 .fhandle = fhandle,
2155 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 struct nfs4_server_caps_res res = {};
2157 struct rpc_message msg = {
2158 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002159 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 .rpc_resp = &res,
2161 };
2162 int status;
2163
Bryan Schumaker7c513052011-03-24 17:12:24 +00002164 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 if (status == 0) {
2166 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04002167 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2168 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2169 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2170 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2171 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2173 server->caps |= NFS_CAP_ACLS;
2174 if (res.has_links != 0)
2175 server->caps |= NFS_CAP_HARDLINKS;
2176 if (res.has_symlinks != 0)
2177 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04002178 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2179 server->caps |= NFS_CAP_FILEID;
2180 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2181 server->caps |= NFS_CAP_MODE;
2182 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2183 server->caps |= NFS_CAP_NLINK;
2184 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2185 server->caps |= NFS_CAP_OWNER;
2186 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2187 server->caps |= NFS_CAP_OWNER_GROUP;
2188 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2189 server->caps |= NFS_CAP_ATIME;
2190 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2191 server->caps |= NFS_CAP_CTIME;
2192 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2193 server->caps |= NFS_CAP_MTIME;
2194
Trond Myklebusta65318b2009-03-11 14:10:28 -04002195 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2196 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2197 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 server->acl_bitmask = res.acl_bitmask;
2199 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002200
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 return status;
2202}
2203
Trond Myklebust55a97592006-06-09 09:34:19 -04002204int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205{
2206 struct nfs4_exception exception = { };
2207 int err;
2208 do {
2209 err = nfs4_handle_exception(server,
2210 _nfs4_server_capabilities(server, fhandle),
2211 &exception);
2212 } while (exception.retry);
2213 return err;
2214}
2215
2216static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2217 struct nfs_fsinfo *info)
2218{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 struct nfs4_lookup_root_arg args = {
2220 .bitmask = nfs4_fattr_bitmap,
2221 };
2222 struct nfs4_lookup_res res = {
2223 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002224 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 .fh = fhandle,
2226 };
2227 struct rpc_message msg = {
2228 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2229 .rpc_argp = &args,
2230 .rpc_resp = &res,
2231 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002232
Trond Myklebust0e574af2005-10-27 22:12:38 -04002233 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002234 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235}
2236
2237static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2238 struct nfs_fsinfo *info)
2239{
2240 struct nfs4_exception exception = { };
2241 int err;
2242 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002243 err = _nfs4_lookup_root(server, fhandle, info);
2244 switch (err) {
2245 case 0:
2246 case -NFS4ERR_WRONGSEC:
2247 break;
2248 default:
2249 err = nfs4_handle_exception(server, err, &exception);
2250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 } while (exception.retry);
2252 return err;
2253}
2254
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002255static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2256 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2257{
2258 struct rpc_auth *auth;
2259 int ret;
2260
2261 auth = rpcauth_create(flavor, server->client);
2262 if (!auth) {
2263 ret = -EIO;
2264 goto out;
2265 }
2266 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002267out:
2268 return ret;
2269}
2270
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002271static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002272 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002274 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002275 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002277 len = gss_mech_list_pseudoflavors(&flav_array[0]);
2278 flav_array[len] = RPC_AUTH_NULL;
2279 len += 1;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002280
2281 for (i = 0; i < len; i++) {
2282 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002283 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002284 continue;
2285 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002286 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002287 /*
2288 * -EACCESS could mean that the user doesn't have correct permissions
2289 * to access the mount. It could also mean that we tried to mount
2290 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2291 * existing mount programs don't handle -EACCES very well so it should
2292 * be mapped to -EPERM instead.
2293 */
2294 if (status == -EACCES)
2295 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002296 return status;
2297}
2298
2299/*
2300 * get the file handle for the "/" directory on the server
2301 */
2302static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2303 struct nfs_fsinfo *info)
2304{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002305 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002306 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002307 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2308 /*
2309 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2310 * by nfs4_map_errors() as this function exits.
2311 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002312 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 if (status == 0)
2314 status = nfs4_server_capabilities(server, fhandle);
2315 if (status == 0)
2316 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002317 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318}
2319
Andy Adamson533eb462011-06-13 18:25:56 -04002320static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002321/*
2322 * Get locations and (maybe) other attributes of a referral.
2323 * Note that we'll actually follow the referral later when
2324 * we detect fsid mismatch in inode revalidation
2325 */
Andy Adamson533eb462011-06-13 18:25:56 -04002326static int nfs4_get_referral(struct inode *dir, const struct qstr *name,
2327 struct nfs_fattr *fattr, struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002328{
2329 int status = -ENOMEM;
2330 struct page *page = NULL;
2331 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002332
2333 page = alloc_page(GFP_KERNEL);
2334 if (page == NULL)
2335 goto out;
2336 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2337 if (locations == NULL)
2338 goto out;
2339
Trond Myklebustc228fd32007-01-13 02:28:11 -05002340 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002341 if (status != 0)
2342 goto out;
2343 /* Make sure server returned a different fsid for the referral */
2344 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002345 dprintk("%s: server did not return a different fsid for"
2346 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002347 status = -EIO;
2348 goto out;
2349 }
Andy Adamson533eb462011-06-13 18:25:56 -04002350 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2351 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002352
Andy Adamson533eb462011-06-13 18:25:56 -04002353 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002354 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002355 memset(fhandle, 0, sizeof(struct nfs_fh));
2356out:
2357 if (page)
2358 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002359 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002360 return status;
2361}
2362
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2364{
2365 struct nfs4_getattr_arg args = {
2366 .fh = fhandle,
2367 .bitmask = server->attr_bitmask,
2368 };
2369 struct nfs4_getattr_res res = {
2370 .fattr = fattr,
2371 .server = server,
2372 };
2373 struct rpc_message msg = {
2374 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2375 .rpc_argp = &args,
2376 .rpc_resp = &res,
2377 };
2378
Trond Myklebust0e574af2005-10-27 22:12:38 -04002379 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002380 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381}
2382
2383static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2384{
2385 struct nfs4_exception exception = { };
2386 int err;
2387 do {
2388 err = nfs4_handle_exception(server,
2389 _nfs4_proc_getattr(server, fhandle, fattr),
2390 &exception);
2391 } while (exception.retry);
2392 return err;
2393}
2394
2395/*
2396 * The file is not closed if it is opened due to the a request to change
2397 * the size of the file. The open call will not be needed once the
2398 * VFS layer lookup-intents are implemented.
2399 *
2400 * Close is called when the inode is destroyed.
2401 * If we haven't opened the file for O_WRONLY, we
2402 * need to in the size_change case to obtain a stateid.
2403 *
2404 * Got race?
2405 * Because OPEN is always done by name in nfsv4, it is
2406 * possible that we opened a different file by the same
2407 * name. We can recognize this race condition, but we
2408 * can't do anything about it besides returning an error.
2409 *
2410 * This will be fixed with VFS changes (lookup-intent).
2411 */
2412static int
2413nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2414 struct iattr *sattr)
2415{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002416 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002417 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002418 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 int status;
2420
Benny Halevy8a1636c2010-07-14 15:43:57 -04002421 if (pnfs_ld_layoutret_on_setattr(inode))
2422 pnfs_return_layout(inode);
2423
Trond Myklebust0e574af2005-10-27 22:12:38 -04002424 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425
Trond Myklebustd5308382005-11-04 15:33:38 -05002426 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002427 if (sattr->ia_valid & ATTR_FILE) {
2428 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002429
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002430 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002431 if (ctx) {
2432 cred = ctx->cred;
2433 state = ctx->state;
2434 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002435 }
2436
Trond Myklebust536e43d2012-01-17 22:04:26 -05002437 /* Deal with open(O_TRUNC) */
2438 if (sattr->ia_valid & ATTR_OPEN)
2439 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2440
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002441 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002442 if (status == 0)
2443 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 return status;
2445}
2446
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002447static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2448 const struct qstr *name, struct nfs_fh *fhandle,
2449 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002450{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002451 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002452 int status;
2453 struct nfs4_lookup_arg args = {
2454 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002455 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002456 .name = name,
2457 };
2458 struct nfs4_lookup_res res = {
2459 .server = server,
2460 .fattr = fattr,
2461 .fh = fhandle,
2462 };
2463 struct rpc_message msg = {
2464 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2465 .rpc_argp = &args,
2466 .rpc_resp = &res,
2467 };
2468
2469 nfs_fattr_init(fattr);
2470
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002472 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 dprintk("NFS reply lookup: %d\n", status);
2474 return status;
2475}
2476
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002477void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr, struct nfs_fh *fh)
2478{
2479 memset(fh, 0, sizeof(struct nfs_fh));
2480 fattr->fsid.major = 1;
2481 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2482 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_FSID | NFS_ATTR_FATTR_MOUNTPOINT;
2483 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2484 fattr->nlink = 2;
2485}
2486
Bryan Schumaker7c513052011-03-24 17:12:24 +00002487static int nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir, struct qstr *name,
2488 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489{
2490 struct nfs4_exception exception = { };
2491 int err;
2492 do {
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002493 int status;
2494
2495 status = _nfs4_proc_lookup(clnt, dir, name, fhandle, fattr);
2496 switch (status) {
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002497 case -NFS4ERR_BADNAME:
2498 return -ENOENT;
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002499 case -NFS4ERR_MOVED:
Trond Myklebusta6f498a2011-11-07 15:50:32 -05002500 return nfs4_get_referral(dir, name, fattr, fhandle);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002501 case -NFS4ERR_WRONGSEC:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002502 nfs_fixup_secinfo_attributes(fattr, fhandle);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002503 }
2504 err = nfs4_handle_exception(NFS_SERVER(dir),
2505 status, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 } while (exception.retry);
2507 return err;
2508}
2509
2510static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2511{
Trond Myklebust76b32992007-08-10 17:45:11 -04002512 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 struct nfs4_accessargs args = {
2514 .fh = NFS_FH(inode),
Trond Myklebust76b32992007-08-10 17:45:11 -04002515 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002517 struct nfs4_accessres res = {
2518 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002519 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 struct rpc_message msg = {
2521 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2522 .rpc_argp = &args,
2523 .rpc_resp = &res,
2524 .rpc_cred = entry->cred,
2525 };
2526 int mode = entry->mask;
2527 int status;
2528
2529 /*
2530 * Determine which access bits we want to ask for...
2531 */
2532 if (mode & MAY_READ)
2533 args.access |= NFS4_ACCESS_READ;
2534 if (S_ISDIR(inode->i_mode)) {
2535 if (mode & MAY_WRITE)
2536 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2537 if (mode & MAY_EXEC)
2538 args.access |= NFS4_ACCESS_LOOKUP;
2539 } else {
2540 if (mode & MAY_WRITE)
2541 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2542 if (mode & MAY_EXEC)
2543 args.access |= NFS4_ACCESS_EXECUTE;
2544 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002545
2546 res.fattr = nfs_alloc_fattr();
2547 if (res.fattr == NULL)
2548 return -ENOMEM;
2549
Bryan Schumaker7c513052011-03-24 17:12:24 +00002550 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 if (!status) {
2552 entry->mask = 0;
2553 if (res.access & NFS4_ACCESS_READ)
2554 entry->mask |= MAY_READ;
2555 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2556 entry->mask |= MAY_WRITE;
2557 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2558 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002559 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002561 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 return status;
2563}
2564
2565static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2566{
2567 struct nfs4_exception exception = { };
2568 int err;
2569 do {
2570 err = nfs4_handle_exception(NFS_SERVER(inode),
2571 _nfs4_proc_access(inode, entry),
2572 &exception);
2573 } while (exception.retry);
2574 return err;
2575}
2576
2577/*
2578 * TODO: For the time being, we don't try to get any attributes
2579 * along with any of the zero-copy operations READ, READDIR,
2580 * READLINK, WRITE.
2581 *
2582 * In the case of the first three, we want to put the GETATTR
2583 * after the read-type operation -- this is because it is hard
2584 * to predict the length of a GETATTR response in v4, and thus
2585 * align the READ data correctly. This means that the GETATTR
2586 * may end up partially falling into the page cache, and we should
2587 * shift it into the 'tail' of the xdr_buf before processing.
2588 * To do this efficiently, we need to know the total length
2589 * of data received, which doesn't seem to be available outside
2590 * of the RPC layer.
2591 *
2592 * In the case of WRITE, we also want to put the GETATTR after
2593 * the operation -- in this case because we want to make sure
2594 * we get the post-operation mtime and size. This means that
2595 * we can't use xdr_encode_pages() as written: we need a variant
2596 * of it which would leave room in the 'tail' iovec.
2597 *
2598 * Both of these changes to the XDR layer would in fact be quite
2599 * minor, but I decided to leave them for a subsequent patch.
2600 */
2601static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2602 unsigned int pgbase, unsigned int pglen)
2603{
2604 struct nfs4_readlink args = {
2605 .fh = NFS_FH(inode),
2606 .pgbase = pgbase,
2607 .pglen = pglen,
2608 .pages = &page,
2609 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002610 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 struct rpc_message msg = {
2612 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2613 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002614 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 };
2616
Bryan Schumaker7c513052011-03-24 17:12:24 +00002617 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 -07002618}
2619
2620static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2621 unsigned int pgbase, unsigned int pglen)
2622{
2623 struct nfs4_exception exception = { };
2624 int err;
2625 do {
2626 err = nfs4_handle_exception(NFS_SERVER(inode),
2627 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2628 &exception);
2629 } while (exception.retry);
2630 return err;
2631}
2632
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633/*
2634 * Got race?
2635 * We will need to arrange for the VFS layer to provide an atomic open.
2636 * Until then, this create/open method is prone to inefficiency and race
2637 * conditions due to the lookup, create, and open VFS calls from sys_open()
2638 * placed on the wire.
2639 *
2640 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2641 * The file will be opened again in the subsequent VFS open call
2642 * (nfs4_proc_file_open).
2643 *
2644 * The open for read will just hang around to be used by any process that
2645 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2646 */
2647
2648static int
2649nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002650 int flags, struct nfs_open_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651{
Al Viro82a2c1b2011-06-22 18:30:55 -04002652 struct dentry *de = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 struct nfs4_state *state;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002654 struct rpc_cred *cred = NULL;
2655 fmode_t fmode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 int status = 0;
2657
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002658 if (ctx != NULL) {
2659 cred = ctx->cred;
Al Viro3d4ff432011-06-22 18:40:12 -04002660 de = ctx->dentry;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002661 fmode = ctx->mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 }
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002663 sattr->ia_mode &= ~current_umask();
Al Viro82a2c1b2011-06-22 18:30:55 -04002664 state = nfs4_do_open(dir, de, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002665 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 if (IS_ERR(state)) {
2667 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002668 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002670 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002671 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002672 if (ctx != NULL)
2673 ctx->state = state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002674 else
Al Viro643168c2011-06-22 18:20:23 -04002675 nfs4_close_sync(state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676out:
2677 return status;
2678}
2679
2680static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2681{
Trond Myklebust16e42952005-10-27 22:12:44 -04002682 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002683 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002685 .name.len = name->len,
2686 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002687 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002689 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002690 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002691 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002693 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2694 .rpc_argp = &args,
2695 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 };
Trond Myklebustd3468902010-04-16 16:22:50 -04002697 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698
Trond Myklebustd3468902010-04-16 16:22:50 -04002699 res.dir_attr = nfs_alloc_fattr();
2700 if (res.dir_attr == NULL)
2701 goto out;
2702
Bryan Schumaker7c513052011-03-24 17:12:24 +00002703 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002704 if (status == 0) {
2705 update_changeattr(dir, &res.cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002706 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002707 }
Trond Myklebustd3468902010-04-16 16:22:50 -04002708 nfs_free_fattr(res.dir_attr);
2709out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 return status;
2711}
2712
2713static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2714{
2715 struct nfs4_exception exception = { };
2716 int err;
2717 do {
2718 err = nfs4_handle_exception(NFS_SERVER(dir),
2719 _nfs4_proc_remove(dir, name),
2720 &exception);
2721 } while (exception.retry);
2722 return err;
2723}
2724
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002725static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002727 struct nfs_server *server = NFS_SERVER(dir);
2728 struct nfs_removeargs *args = msg->rpc_argp;
2729 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730
Trond Myklebusta65318b2009-03-11 14:10:28 -04002731 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002732 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05002734 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735}
2736
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002737static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002739 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2740
Trond Myklebust14516c32010-07-31 14:29:06 -04002741 if (!nfs4_sequence_done(task, &res->seq_res))
2742 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002743 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002744 return 0;
2745 update_changeattr(dir, &res->cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002746 nfs_post_op_update_inode(dir, res->dir_attr);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002747 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748}
2749
Jeff Laytond3d41522010-09-17 17:31:57 -04002750static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2751{
2752 struct nfs_server *server = NFS_SERVER(dir);
2753 struct nfs_renameargs *arg = msg->rpc_argp;
2754 struct nfs_renameres *res = msg->rpc_resp;
2755
2756 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2757 arg->bitmask = server->attr_bitmask;
2758 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002759 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04002760}
2761
2762static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2763 struct inode *new_dir)
2764{
2765 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2766
2767 if (!nfs4_sequence_done(task, &res->seq_res))
2768 return 0;
2769 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2770 return 0;
2771
2772 update_changeattr(old_dir, &res->old_cinfo);
2773 nfs_post_op_update_inode(old_dir, res->old_fattr);
2774 update_changeattr(new_dir, &res->new_cinfo);
2775 nfs_post_op_update_inode(new_dir, res->new_fattr);
2776 return 1;
2777}
2778
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2780 struct inode *new_dir, struct qstr *new_name)
2781{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002782 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002783 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 .old_dir = NFS_FH(old_dir),
2785 .new_dir = NFS_FH(new_dir),
2786 .old_name = old_name,
2787 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002788 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002790 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002791 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002792 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 struct rpc_message msg = {
2794 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2795 .rpc_argp = &arg,
2796 .rpc_resp = &res,
2797 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002798 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799
Trond Myklebust011fff72010-04-16 16:22:49 -04002800 res.old_fattr = nfs_alloc_fattr();
2801 res.new_fattr = nfs_alloc_fattr();
2802 if (res.old_fattr == NULL || res.new_fattr == NULL)
2803 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804
Bryan Schumaker7c513052011-03-24 17:12:24 +00002805 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 if (!status) {
2807 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002808 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002810 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 }
Trond Myklebust011fff72010-04-16 16:22:49 -04002812out:
2813 nfs_free_fattr(res.new_fattr);
2814 nfs_free_fattr(res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 return status;
2816}
2817
2818static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2819 struct inode *new_dir, struct qstr *new_name)
2820{
2821 struct nfs4_exception exception = { };
2822 int err;
2823 do {
2824 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2825 _nfs4_proc_rename(old_dir, old_name,
2826 new_dir, new_name),
2827 &exception);
2828 } while (exception.retry);
2829 return err;
2830}
2831
2832static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2833{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002834 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 struct nfs4_link_arg arg = {
2836 .fh = NFS_FH(inode),
2837 .dir_fh = NFS_FH(dir),
2838 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002839 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002841 struct nfs4_link_res res = {
2842 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002843 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 struct rpc_message msg = {
2845 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2846 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002847 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 };
Trond Myklebust136f2622010-04-16 16:22:49 -04002849 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850
Trond Myklebust136f2622010-04-16 16:22:49 -04002851 res.fattr = nfs_alloc_fattr();
2852 res.dir_attr = nfs_alloc_fattr();
2853 if (res.fattr == NULL || res.dir_attr == NULL)
2854 goto out;
2855
Bryan Schumaker7c513052011-03-24 17:12:24 +00002856 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002857 if (!status) {
2858 update_changeattr(dir, &res.cinfo);
2859 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002860 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002861 }
Trond Myklebust136f2622010-04-16 16:22:49 -04002862out:
2863 nfs_free_fattr(res.dir_attr);
2864 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 return status;
2866}
2867
2868static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2869{
2870 struct nfs4_exception exception = { };
2871 int err;
2872 do {
2873 err = nfs4_handle_exception(NFS_SERVER(inode),
2874 _nfs4_proc_link(inode, dir, name),
2875 &exception);
2876 } while (exception.retry);
2877 return err;
2878}
2879
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002880struct nfs4_createdata {
2881 struct rpc_message msg;
2882 struct nfs4_create_arg arg;
2883 struct nfs4_create_res res;
2884 struct nfs_fh fh;
2885 struct nfs_fattr fattr;
2886 struct nfs_fattr dir_fattr;
2887};
2888
2889static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2890 struct qstr *name, struct iattr *sattr, u32 ftype)
2891{
2892 struct nfs4_createdata *data;
2893
2894 data = kzalloc(sizeof(*data), GFP_KERNEL);
2895 if (data != NULL) {
2896 struct nfs_server *server = NFS_SERVER(dir);
2897
2898 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2899 data->msg.rpc_argp = &data->arg;
2900 data->msg.rpc_resp = &data->res;
2901 data->arg.dir_fh = NFS_FH(dir);
2902 data->arg.server = server;
2903 data->arg.name = name;
2904 data->arg.attrs = sattr;
2905 data->arg.ftype = ftype;
2906 data->arg.bitmask = server->attr_bitmask;
2907 data->res.server = server;
2908 data->res.fh = &data->fh;
2909 data->res.fattr = &data->fattr;
2910 data->res.dir_fattr = &data->dir_fattr;
2911 nfs_fattr_init(data->res.fattr);
2912 nfs_fattr_init(data->res.dir_fattr);
2913 }
2914 return data;
2915}
2916
2917static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2918{
Bryan Schumaker7c513052011-03-24 17:12:24 +00002919 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00002920 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002921 if (status == 0) {
2922 update_changeattr(dir, &data->res.dir_cinfo);
2923 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2924 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2925 }
2926 return status;
2927}
2928
2929static void nfs4_free_createdata(struct nfs4_createdata *data)
2930{
2931 kfree(data);
2932}
2933
Chuck Lever4f390c12006-08-22 20:06:22 -04002934static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002935 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002937 struct nfs4_createdata *data;
2938 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939
Chuck Lever94a6d752006-08-22 20:06:23 -04002940 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002941 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002942
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002943 status = -ENOMEM;
2944 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2945 if (data == NULL)
2946 goto out;
2947
2948 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2949 data->arg.u.symlink.pages = &page;
2950 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002952 status = nfs4_do_create(dir, dentry, data);
2953
2954 nfs4_free_createdata(data);
2955out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 return status;
2957}
2958
Chuck Lever4f390c12006-08-22 20:06:22 -04002959static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002960 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961{
2962 struct nfs4_exception exception = { };
2963 int err;
2964 do {
2965 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002966 _nfs4_proc_symlink(dir, dentry, page,
2967 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 &exception);
2969 } while (exception.retry);
2970 return err;
2971}
2972
2973static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2974 struct iattr *sattr)
2975{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002976 struct nfs4_createdata *data;
2977 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002979 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2980 if (data == NULL)
2981 goto out;
2982
2983 status = nfs4_do_create(dir, dentry, data);
2984
2985 nfs4_free_createdata(data);
2986out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 return status;
2988}
2989
2990static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2991 struct iattr *sattr)
2992{
2993 struct nfs4_exception exception = { };
2994 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002995
2996 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 do {
2998 err = nfs4_handle_exception(NFS_SERVER(dir),
2999 _nfs4_proc_mkdir(dir, dentry, sattr),
3000 &exception);
3001 } while (exception.retry);
3002 return err;
3003}
3004
3005static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003006 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007{
3008 struct inode *dir = dentry->d_inode;
3009 struct nfs4_readdir_arg args = {
3010 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003011 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 .pgbase = 0,
3013 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003014 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003015 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 };
3017 struct nfs4_readdir_res res;
3018 struct rpc_message msg = {
3019 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3020 .rpc_argp = &args,
3021 .rpc_resp = &res,
3022 .rpc_cred = cred,
3023 };
3024 int status;
3025
Harvey Harrison3110ff82008-05-02 13:42:44 -07003026 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003027 dentry->d_parent->d_name.name,
3028 dentry->d_name.name,
3029 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
3031 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003032 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 -05003033 if (status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003035 status += args.pgbase;
3036 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003037
3038 nfs_invalidate_atime(dir);
3039
Harvey Harrison3110ff82008-05-02 13:42:44 -07003040 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 return status;
3042}
3043
3044static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003045 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046{
3047 struct nfs4_exception exception = { };
3048 int err;
3049 do {
3050 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3051 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003052 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 &exception);
3054 } while (exception.retry);
3055 return err;
3056}
3057
3058static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3059 struct iattr *sattr, dev_t rdev)
3060{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003061 struct nfs4_createdata *data;
3062 int mode = sattr->ia_mode;
3063 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064
3065 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3066 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003067
3068 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3069 if (data == NULL)
3070 goto out;
3071
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003073 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003075 data->arg.ftype = NF4BLK;
3076 data->arg.u.device.specdata1 = MAJOR(rdev);
3077 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 }
3079 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003080 data->arg.ftype = NF4CHR;
3081 data->arg.u.device.specdata1 = MAJOR(rdev);
3082 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003085 status = nfs4_do_create(dir, dentry, data);
3086
3087 nfs4_free_createdata(data);
3088out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 return status;
3090}
3091
3092static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3093 struct iattr *sattr, dev_t rdev)
3094{
3095 struct nfs4_exception exception = { };
3096 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003097
3098 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 do {
3100 err = nfs4_handle_exception(NFS_SERVER(dir),
3101 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3102 &exception);
3103 } while (exception.retry);
3104 return err;
3105}
3106
3107static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3108 struct nfs_fsstat *fsstat)
3109{
3110 struct nfs4_statfs_arg args = {
3111 .fh = fhandle,
3112 .bitmask = server->attr_bitmask,
3113 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003114 struct nfs4_statfs_res res = {
3115 .fsstat = fsstat,
3116 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 struct rpc_message msg = {
3118 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3119 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003120 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 };
3122
Trond Myklebust0e574af2005-10-27 22:12:38 -04003123 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003124 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125}
3126
3127static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3128{
3129 struct nfs4_exception exception = { };
3130 int err;
3131 do {
3132 err = nfs4_handle_exception(server,
3133 _nfs4_proc_statfs(server, fhandle, fsstat),
3134 &exception);
3135 } while (exception.retry);
3136 return err;
3137}
3138
3139static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3140 struct nfs_fsinfo *fsinfo)
3141{
3142 struct nfs4_fsinfo_arg args = {
3143 .fh = fhandle,
3144 .bitmask = server->attr_bitmask,
3145 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003146 struct nfs4_fsinfo_res res = {
3147 .fsinfo = fsinfo,
3148 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 struct rpc_message msg = {
3150 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3151 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003152 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 };
3154
Bryan Schumaker7c513052011-03-24 17:12:24 +00003155 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156}
3157
3158static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3159{
3160 struct nfs4_exception exception = { };
3161 int err;
3162
3163 do {
3164 err = nfs4_handle_exception(server,
3165 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3166 &exception);
3167 } while (exception.retry);
3168 return err;
3169}
3170
3171static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3172{
Trond Myklebust0e574af2005-10-27 22:12:38 -04003173 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3175}
3176
3177static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3178 struct nfs_pathconf *pathconf)
3179{
3180 struct nfs4_pathconf_arg args = {
3181 .fh = fhandle,
3182 .bitmask = server->attr_bitmask,
3183 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003184 struct nfs4_pathconf_res res = {
3185 .pathconf = pathconf,
3186 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 struct rpc_message msg = {
3188 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3189 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003190 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 };
3192
3193 /* None of the pathconf attributes are mandatory to implement */
3194 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3195 memset(pathconf, 0, sizeof(*pathconf));
3196 return 0;
3197 }
3198
Trond Myklebust0e574af2005-10-27 22:12:38 -04003199 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003200 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201}
3202
3203static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3204 struct nfs_pathconf *pathconf)
3205{
3206 struct nfs4_exception exception = { };
3207 int err;
3208
3209 do {
3210 err = nfs4_handle_exception(server,
3211 _nfs4_proc_pathconf(server, fhandle, pathconf),
3212 &exception);
3213 } while (exception.retry);
3214 return err;
3215}
3216
Benny Halevyd20581a2011-05-22 19:52:03 +03003217void __nfs4_read_done_cb(struct nfs_read_data *data)
3218{
3219 nfs_invalidate_atime(data->inode);
3220}
3221
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003222static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223{
Trond Myklebustec06c092006-03-20 13:44:27 -05003224 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003226 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003227 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003228 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003230
Benny Halevyd20581a2011-05-22 19:52:03 +03003231 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003233 renew_lease(server, data->timestamp);
3234 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235}
3236
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003237static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3238{
3239
3240 dprintk("--> %s\n", __func__);
3241
3242 if (!nfs4_sequence_done(task, &data->res.seq_res))
3243 return -EAGAIN;
3244
Benny Halevyd20581a2011-05-22 19:52:03 +03003245 return data->read_done_cb ? data->read_done_cb(task, data) :
3246 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003247}
3248
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003249static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 data->timestamp = jiffies;
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003252 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003253 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003254 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255}
3256
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003257/* Reset the the nfs_read_data to send the read to the MDS. */
3258void nfs4_reset_read(struct rpc_task *task, struct nfs_read_data *data)
3259{
3260 dprintk("%s Reset task for i/o through\n", __func__);
3261 put_lseg(data->lseg);
3262 data->lseg = NULL;
3263 /* offsets will differ in the dense stripe case */
3264 data->args.offset = data->mds_offset;
3265 data->ds_clp = NULL;
3266 data->args.fh = NFS_FH(data->inode);
3267 data->read_done_cb = nfs4_read_done_cb;
3268 task->tk_ops = data->mds_ops;
3269 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3270}
3271EXPORT_SYMBOL_GPL(nfs4_reset_read);
3272
Fred Isamanb029bc92011-03-03 15:13:42 +00003273static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 struct inode *inode = data->inode;
3276
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003277 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003278 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003279 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003281 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003283 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003284 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003285 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286}
3287
Fred Isamanb029bc92011-03-03 15:13:42 +00003288static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3289{
3290 if (!nfs4_sequence_done(task, &data->res.seq_res))
3291 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003292 return data->write_done_cb ? data->write_done_cb(task, data) :
3293 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003294}
3295
Fred Isamana69aef12011-03-03 15:13:47 +00003296/* Reset the the nfs_write_data to send the write to the MDS. */
3297void nfs4_reset_write(struct rpc_task *task, struct nfs_write_data *data)
3298{
3299 dprintk("%s Reset task for i/o through\n", __func__);
3300 put_lseg(data->lseg);
3301 data->lseg = NULL;
3302 data->ds_clp = NULL;
3303 data->write_done_cb = nfs4_write_done_cb;
3304 data->args.fh = NFS_FH(data->inode);
3305 data->args.bitmask = data->res.server->cache_consistency_bitmask;
3306 data->args.offset = data->mds_offset;
3307 data->res.fattr = &data->fattr;
3308 task->tk_ops = data->mds_ops;
3309 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3310}
3311EXPORT_SYMBOL_GPL(nfs4_reset_write);
3312
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003313static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003315 struct nfs_server *server = NFS_SERVER(data->inode);
3316
Fred Isaman7ffd1062011-03-03 15:13:46 +00003317 if (data->lseg) {
3318 data->args.bitmask = NULL;
3319 data->res.fattr = NULL;
3320 } else
3321 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isamanb029bc92011-03-03 15:13:42 +00003322 if (!data->write_done_cb)
3323 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003324 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 data->timestamp = jiffies;
3326
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003327 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003328 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329}
3330
Fred Isaman5f452432011-03-23 13:27:46 +00003331static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003334
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003335 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003336 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003337 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003339 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003340 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341}
3342
Fred Isaman5f452432011-03-23 13:27:46 +00003343static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3344{
3345 if (!nfs4_sequence_done(task, &data->res.seq_res))
3346 return -EAGAIN;
3347 return data->write_done_cb(task, data);
3348}
3349
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003350static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003352 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003353
3354 if (data->lseg) {
3355 data->args.bitmask = NULL;
3356 data->res.fattr = NULL;
3357 } else
3358 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isaman5f452432011-03-23 13:27:46 +00003359 if (!data->write_done_cb)
3360 data->write_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003361 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003362 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003363 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364}
3365
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003366struct nfs4_renewdata {
3367 struct nfs_client *client;
3368 unsigned long timestamp;
3369};
3370
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371/*
3372 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3373 * standalone procedure for queueing an asynchronous RENEW.
3374 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003375static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003376{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003377 struct nfs4_renewdata *data = calldata;
3378 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003379
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003380 if (atomic_read(&clp->cl_count) > 1)
3381 nfs4_schedule_state_renewal(clp);
3382 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003383 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003384}
3385
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003386static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003388 struct nfs4_renewdata *data = calldata;
3389 struct nfs_client *clp = data->client;
3390 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391
3392 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003393 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003394 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3395 return;
3396 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003397 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003398 return;
3399 }
3400 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003402 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403}
3404
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003405static const struct rpc_call_ops nfs4_renew_ops = {
3406 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003407 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003408};
3409
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003410static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411{
3412 struct rpc_message msg = {
3413 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3414 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003415 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003417 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003419 if (renew_flags == 0)
3420 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003421 if (!atomic_inc_not_zero(&clp->cl_count))
3422 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003423 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003424 if (data == NULL)
3425 return -ENOMEM;
3426 data->client = clp;
3427 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003429 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430}
3431
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003432static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433{
3434 struct rpc_message msg = {
3435 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3436 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003437 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438 };
3439 unsigned long now = jiffies;
3440 int status;
3441
3442 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3443 if (status < 0)
3444 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003445 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 return 0;
3447}
3448
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003449static inline int nfs4_server_supports_acls(struct nfs_server *server)
3450{
3451 return (server->caps & NFS_CAP_ACLS)
3452 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3453 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3454}
3455
3456/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3457 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3458 * the stack.
3459 */
3460#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3461
Neil Hormane9e3d722011-03-04 19:26:03 -05003462static int buf_to_pages_noslab(const void *buf, size_t buflen,
3463 struct page **pages, unsigned int *pgbase)
3464{
3465 struct page *newpage, **spages;
3466 int rc = 0;
3467 size_t len;
3468 spages = pages;
3469
3470 do {
Jovi Zhang43b7c3f2011-03-02 23:19:37 +00003471 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003472 newpage = alloc_page(GFP_KERNEL);
3473
3474 if (newpage == NULL)
3475 goto unwind;
3476 memcpy(page_address(newpage), buf, len);
3477 buf += len;
3478 buflen -= len;
3479 *pages++ = newpage;
3480 rc++;
3481 } while (buflen != 0);
3482
3483 return rc;
3484
3485unwind:
3486 for(; rc > 0; rc--)
3487 __free_page(spages[rc-1]);
3488 return -ENOMEM;
3489}
3490
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003491struct nfs4_cached_acl {
3492 int cached;
3493 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003494 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003495};
3496
3497static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003498{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003499 struct nfs_inode *nfsi = NFS_I(inode);
3500
3501 spin_lock(&inode->i_lock);
3502 kfree(nfsi->nfs4_acl);
3503 nfsi->nfs4_acl = acl;
3504 spin_unlock(&inode->i_lock);
3505}
3506
3507static void nfs4_zap_acl_attr(struct inode *inode)
3508{
3509 nfs4_set_cached_acl(inode, NULL);
3510}
3511
3512static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3513{
3514 struct nfs_inode *nfsi = NFS_I(inode);
3515 struct nfs4_cached_acl *acl;
3516 int ret = -ENOENT;
3517
3518 spin_lock(&inode->i_lock);
3519 acl = nfsi->nfs4_acl;
3520 if (acl == NULL)
3521 goto out;
3522 if (buf == NULL) /* user is just asking for length */
3523 goto out_len;
3524 if (acl->cached == 0)
3525 goto out;
3526 ret = -ERANGE; /* see getxattr(2) man page */
3527 if (acl->len > buflen)
3528 goto out;
3529 memcpy(buf, acl->data, acl->len);
3530out_len:
3531 ret = acl->len;
3532out:
3533 spin_unlock(&inode->i_lock);
3534 return ret;
3535}
3536
3537static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3538{
3539 struct nfs4_cached_acl *acl;
3540
3541 if (buf && acl_len <= PAGE_SIZE) {
3542 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3543 if (acl == NULL)
3544 goto out;
3545 acl->cached = 1;
3546 memcpy(acl->data, buf, acl_len);
3547 } else {
3548 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3549 if (acl == NULL)
3550 goto out;
3551 acl->cached = 0;
3552 }
3553 acl->len = acl_len;
3554out:
3555 nfs4_set_cached_acl(inode, acl);
3556}
3557
Andy Adamsonbf118a32011-12-07 11:55:27 -05003558/*
3559 * The getxattr API returns the required buffer length when called with a
3560 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3561 * the required buf. On a NULL buf, we send a page of data to the server
3562 * guessing that the ACL request can be serviced by a page. If so, we cache
3563 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3564 * the cache. If not so, we throw away the page, and cache the required
3565 * length. The next getxattr call will then produce another round trip to
3566 * the server, this time with the input buf of the required size.
3567 */
Trond Myklebust16b42892006-08-24 12:27:15 -04003568static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003569{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003570 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003571 struct nfs_getaclargs args = {
3572 .fh = NFS_FH(inode),
3573 .acl_pages = pages,
3574 .acl_len = buflen,
3575 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003576 struct nfs_getaclres res = {
3577 .acl_len = buflen,
3578 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003579 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003580 struct rpc_message msg = {
3581 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3582 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003583 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003584 };
Andy Adamsonbf118a32011-12-07 11:55:27 -05003585 int ret = -ENOMEM, npages, i, acl_len = 0;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003586
Andy Adamsonbf118a32011-12-07 11:55:27 -05003587 npages = (buflen + PAGE_SIZE - 1) >> PAGE_SHIFT;
3588 /* As long as we're doing a round trip to the server anyway,
3589 * let's be prepared for a page of acl data. */
3590 if (npages == 0)
3591 npages = 1;
3592
3593 for (i = 0; i < npages; i++) {
3594 pages[i] = alloc_page(GFP_KERNEL);
3595 if (!pages[i])
3596 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003597 }
Andy Adamsonbf118a32011-12-07 11:55:27 -05003598 if (npages > 1) {
3599 /* for decoding across pages */
3600 args.acl_scratch = alloc_page(GFP_KERNEL);
3601 if (!args.acl_scratch)
3602 goto out_free;
3603 }
3604 args.acl_len = npages * PAGE_SIZE;
3605 args.acl_pgbase = 0;
3606 /* Let decode_getfacl know not to fail if the ACL data is larger than
3607 * the page we send as a guess */
3608 if (buf == NULL)
3609 res.acl_flags |= NFS4_ACL_LEN_REQUEST;
3610 resp_buf = page_address(pages[0]);
3611
Peng Taode040be2012-01-10 22:42:47 +08003612 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003613 __func__, buf, buflen, npages, args.acl_len);
3614 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3615 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003616 if (ret)
3617 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003618
3619 acl_len = res.acl_len - res.acl_data_offset;
3620 if (acl_len > args.acl_len)
3621 nfs4_write_cached_acl(inode, NULL, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003622 else
Andy Adamsonbf118a32011-12-07 11:55:27 -05003623 nfs4_write_cached_acl(inode, resp_buf + res.acl_data_offset,
3624 acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003625 if (buf) {
3626 ret = -ERANGE;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003627 if (acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003628 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003629 _copy_from_pages(buf, pages, res.acl_data_offset,
3630 res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003631 }
Andy Adamsonbf118a32011-12-07 11:55:27 -05003632 ret = acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003633out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003634 for (i = 0; i < npages; i++)
3635 if (pages[i])
3636 __free_page(pages[i]);
3637 if (args.acl_scratch)
3638 __free_page(args.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003639 return ret;
3640}
3641
Trond Myklebust16b42892006-08-24 12:27:15 -04003642static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3643{
3644 struct nfs4_exception exception = { };
3645 ssize_t ret;
3646 do {
3647 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3648 if (ret >= 0)
3649 break;
3650 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3651 } while (exception.retry);
3652 return ret;
3653}
3654
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003655static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3656{
3657 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003658 int ret;
3659
3660 if (!nfs4_server_supports_acls(server))
3661 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003662 ret = nfs_revalidate_inode(server, inode);
3663 if (ret < 0)
3664 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003665 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3666 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003667 ret = nfs4_read_cached_acl(inode, buf, buflen);
3668 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003669 /* -ENOENT is returned if there is no ACL or if there is an ACL
3670 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003671 return ret;
3672 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003673}
3674
Trond Myklebust16b42892006-08-24 12:27:15 -04003675static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003676{
3677 struct nfs_server *server = NFS_SERVER(inode);
3678 struct page *pages[NFS4ACL_MAXPAGES];
3679 struct nfs_setaclargs arg = {
3680 .fh = NFS_FH(inode),
3681 .acl_pages = pages,
3682 .acl_len = buflen,
3683 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003684 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003685 struct rpc_message msg = {
3686 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3687 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003688 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003689 };
Neil Hormane9e3d722011-03-04 19:26:03 -05003690 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003691
3692 if (!nfs4_server_supports_acls(server))
3693 return -EOPNOTSUPP;
Neil Hormane9e3d722011-03-04 19:26:03 -05003694 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3695 if (i < 0)
3696 return i;
Trond Myklebust642ac542005-10-18 14:20:19 -07003697 nfs_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003698 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003699
3700 /*
3701 * Free each page after tx, so the only ref left is
3702 * held by the network stack
3703 */
3704 for (; i > 0; i--)
3705 put_page(pages[i-1]);
3706
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003707 /*
3708 * Acl update can result in inode attribute update.
3709 * so mark the attribute cache invalid.
3710 */
3711 spin_lock(&inode->i_lock);
3712 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3713 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003714 nfs_access_zap_cache(inode);
3715 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003716 return ret;
3717}
3718
Trond Myklebust16b42892006-08-24 12:27:15 -04003719static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3720{
3721 struct nfs4_exception exception = { };
3722 int err;
3723 do {
3724 err = nfs4_handle_exception(NFS_SERVER(inode),
3725 __nfs4_proc_set_acl(inode, buf, buflen),
3726 &exception);
3727 } while (exception.retry);
3728 return err;
3729}
3730
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003732nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003734 struct nfs_client *clp = server->nfs_client;
3735
3736 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737 return 0;
3738 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003739 case -NFS4ERR_ADMIN_REVOKED:
3740 case -NFS4ERR_BAD_STATEID:
3741 case -NFS4ERR_OPENMODE:
3742 if (state == NULL)
3743 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003744 nfs4_schedule_stateid_recovery(server, state);
3745 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003746 case -NFS4ERR_EXPIRED:
3747 if (state != NULL)
3748 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003750 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003751 nfs4_schedule_lease_recovery(clp);
3752 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003753#if defined(CONFIG_NFS_V4_1)
3754 case -NFS4ERR_BADSESSION:
3755 case -NFS4ERR_BADSLOT:
3756 case -NFS4ERR_BAD_HIGH_SLOT:
3757 case -NFS4ERR_DEADSESSION:
3758 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3759 case -NFS4ERR_SEQ_FALSE_RETRY:
3760 case -NFS4ERR_SEQ_MISORDERED:
3761 dprintk("%s ERROR %d, Reset session\n", __func__,
3762 task->tk_status);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003763 nfs4_schedule_session_recovery(clp->cl_session);
Andy Adamson4745e312009-04-01 09:22:42 -04003764 task->tk_status = 0;
3765 return -EAGAIN;
3766#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003768 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003769 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003770 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3772 task->tk_status = 0;
3773 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04003774 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 case -NFS4ERR_OLD_STATEID:
3776 task->tk_status = 0;
3777 return -EAGAIN;
3778 }
3779 task->tk_status = nfs4_map_errors(task->tk_status);
3780 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003781wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003782 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003783 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3784 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3785 task->tk_status = 0;
3786 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787}
3788
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003789int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3790 unsigned short port, struct rpc_cred *cred,
3791 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792{
3793 nfs4_verifier sc_verifier;
3794 struct nfs4_setclientid setclientid = {
3795 .sc_verifier = &sc_verifier,
3796 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00003797 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 };
3799 struct rpc_message msg = {
3800 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3801 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003802 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003803 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804 };
Al Virobc4785c2006-10-19 23:28:51 -07003805 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806 int loop = 0;
3807 int status;
3808
Al Virobc4785c2006-10-19 23:28:51 -07003809 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3811 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3812
3813 for(;;) {
3814 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003815 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003816 clp->cl_ipaddr,
3817 rpc_peeraddr2str(clp->cl_rpcclient,
3818 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003819 rpc_peeraddr2str(clp->cl_rpcclient,
3820 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003821 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822 clp->cl_id_uniquifier);
3823 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003824 sizeof(setclientid.sc_netid),
3825 rpc_peeraddr2str(clp->cl_rpcclient,
3826 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003828 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829 clp->cl_ipaddr, port >> 8, port & 255);
3830
Trond Myklebust1bd714f2011-04-24 14:29:33 -04003831 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832 if (status != -NFS4ERR_CLID_INUSE)
3833 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003834 if (loop != 0) {
3835 ++clp->cl_id_uniquifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003837 }
3838 ++loop;
3839 ssleep(clp->cl_lease_time / HZ + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840 }
3841 return status;
3842}
3843
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003844int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003845 struct nfs4_setclientid_res *arg,
3846 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847{
3848 struct nfs_fsinfo fsinfo;
3849 struct rpc_message msg = {
3850 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003851 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003853 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854 };
3855 unsigned long now;
3856 int status;
3857
3858 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04003859 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860 if (status == 0) {
3861 spin_lock(&clp->cl_lock);
3862 clp->cl_lease_time = fsinfo.lease_time * HZ;
3863 clp->cl_last_renewal = now;
3864 spin_unlock(&clp->cl_lock);
3865 }
3866 return status;
3867}
3868
Trond Myklebustfe650402006-01-03 09:55:18 +01003869struct nfs4_delegreturndata {
3870 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003871 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003872 struct nfs_fh fh;
3873 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003874 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003875 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003876 int rpc_status;
3877};
3878
Trond Myklebustfe650402006-01-03 09:55:18 +01003879static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3880{
3881 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003882
Trond Myklebust14516c32010-07-31 14:29:06 -04003883 if (!nfs4_sequence_done(task, &data->res.seq_res))
3884 return;
Andy Adamson938e1012009-04-01 09:22:28 -04003885
Ricardo Labiaga79708862009-12-07 09:23:21 -05003886 switch (task->tk_status) {
3887 case -NFS4ERR_STALE_STATEID:
3888 case -NFS4ERR_EXPIRED:
3889 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01003890 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003891 break;
3892 default:
3893 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3894 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003895 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003896 return;
3897 }
3898 }
3899 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01003900}
3901
3902static void nfs4_delegreturn_release(void *calldata)
3903{
Trond Myklebustfe650402006-01-03 09:55:18 +01003904 kfree(calldata);
3905}
3906
Andy Adamson938e1012009-04-01 09:22:28 -04003907#if defined(CONFIG_NFS_V4_1)
3908static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3909{
3910 struct nfs4_delegreturndata *d_data;
3911
3912 d_data = (struct nfs4_delegreturndata *)data;
3913
Trond Myklebust035168a2010-06-16 09:52:26 -04003914 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04003915 &d_data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003916 &d_data->res.seq_res, task))
Andy Adamson938e1012009-04-01 09:22:28 -04003917 return;
3918 rpc_call_start(task);
3919}
3920#endif /* CONFIG_NFS_V4_1 */
3921
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003922static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04003923#if defined(CONFIG_NFS_V4_1)
3924 .rpc_call_prepare = nfs4_delegreturn_prepare,
3925#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01003926 .rpc_call_done = nfs4_delegreturn_done,
3927 .rpc_release = nfs4_delegreturn_release,
3928};
3929
Trond Myklebuste6f81072008-01-24 18:14:34 -05003930static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003931{
3932 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003933 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003934 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003935 struct rpc_message msg = {
3936 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3937 .rpc_cred = cred,
3938 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003939 struct rpc_task_setup task_setup_data = {
3940 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003941 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003942 .callback_ops = &nfs4_delegreturn_ops,
3943 .flags = RPC_TASK_ASYNC,
3944 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003945 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003946
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003947 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01003948 if (data == NULL)
3949 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05003950 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01003951 data->args.fhandle = &data->fh;
3952 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003953 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003954 nfs_copy_fh(&data->fh, NFS_FH(inode));
3955 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003956 data->res.fattr = &data->fattr;
3957 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003958 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003959 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003960 data->rpc_status = 0;
3961
Trond Myklebustc970aa82007-07-14 15:39:59 -04003962 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05003963 msg.rpc_argp = &data->args;
3964 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003965 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003966 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003967 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003968 if (!issync)
3969 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003970 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003971 if (status != 0)
3972 goto out;
3973 status = data->rpc_status;
3974 if (status != 0)
3975 goto out;
3976 nfs_refresh_inode(inode, &data->fattr);
3977out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003978 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003979 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980}
3981
Trond Myklebuste6f81072008-01-24 18:14:34 -05003982int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983{
3984 struct nfs_server *server = NFS_SERVER(inode);
3985 struct nfs4_exception exception = { };
3986 int err;
3987 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003988 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 switch (err) {
3990 case -NFS4ERR_STALE_STATEID:
3991 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992 case 0:
3993 return 0;
3994 }
3995 err = nfs4_handle_exception(server, err, &exception);
3996 } while (exception.retry);
3997 return err;
3998}
3999
4000#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4001#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4002
4003/*
4004 * sleep, with exponential backoff, and retry the LOCK operation.
4005 */
4006static unsigned long
4007nfs4_set_lock_task_retry(unsigned long timeout)
4008{
Jeff Laytond3103102011-12-01 22:44:39 +01004009 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 timeout <<= 1;
4011 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4012 return NFS4_LOCK_MAXTIMEOUT;
4013 return timeout;
4014}
4015
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4017{
4018 struct inode *inode = state->inode;
4019 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004020 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004021 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004023 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004025 struct nfs_lockt_res res = {
4026 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 };
4028 struct rpc_message msg = {
4029 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4030 .rpc_argp = &arg,
4031 .rpc_resp = &res,
4032 .rpc_cred = state->owner->so_cred,
4033 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034 struct nfs4_lock_state *lsp;
4035 int status;
4036
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004037 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004038 status = nfs4_set_lock_state(state, request);
4039 if (status != 0)
4040 goto out;
4041 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004042 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004043 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004044 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004045 switch (status) {
4046 case 0:
4047 request->fl_type = F_UNLCK;
4048 break;
4049 case -NFS4ERR_DENIED:
4050 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004052 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004053out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 return status;
4055}
4056
4057static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4058{
4059 struct nfs4_exception exception = { };
4060 int err;
4061
4062 do {
4063 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4064 _nfs4_proc_getlk(state, cmd, request),
4065 &exception);
4066 } while (exception.retry);
4067 return err;
4068}
4069
4070static int do_vfs_lock(struct file *file, struct file_lock *fl)
4071{
4072 int res = 0;
4073 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4074 case FL_POSIX:
4075 res = posix_lock_file_wait(file, fl);
4076 break;
4077 case FL_FLOCK:
4078 res = flock_lock_file_wait(file, fl);
4079 break;
4080 default:
4081 BUG();
4082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083 return res;
4084}
4085
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004086struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004087 struct nfs_locku_args arg;
4088 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004089 struct nfs4_lock_state *lsp;
4090 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004091 struct file_lock fl;
4092 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004093 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004094};
4095
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004096static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4097 struct nfs_open_context *ctx,
4098 struct nfs4_lock_state *lsp,
4099 struct nfs_seqid *seqid)
4100{
4101 struct nfs4_unlockdata *p;
4102 struct inode *inode = lsp->ls_state->inode;
4103
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004104 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004105 if (p == NULL)
4106 return NULL;
4107 p->arg.fh = NFS_FH(inode);
4108 p->arg.fl = &p->fl;
4109 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004110 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004111 p->arg.stateid = &lsp->ls_stateid;
4112 p->lsp = lsp;
4113 atomic_inc(&lsp->ls_count);
4114 /* Ensure we don't close file until we're done freeing locks! */
4115 p->ctx = get_nfs_open_context(ctx);
4116 memcpy(&p->fl, fl, sizeof(p->fl));
4117 p->server = NFS_SERVER(inode);
4118 return p;
4119}
4120
Trond Myklebust06f814a2006-01-03 09:55:07 +01004121static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004122{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004123 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004124 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004125 nfs4_put_lock_state(calldata->lsp);
4126 put_nfs_open_context(calldata->ctx);
4127 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004128}
4129
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004130static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004131{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004132 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004133
Trond Myklebust14516c32010-07-31 14:29:06 -04004134 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4135 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004136 switch (task->tk_status) {
4137 case 0:
4138 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004139 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004140 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01004141 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004142 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004143 case -NFS4ERR_BAD_STATEID:
4144 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004145 case -NFS4ERR_STALE_STATEID:
4146 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004147 break;
4148 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004149 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004150 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004151 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004152}
4153
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004154static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004155{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004156 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004158 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004159 return;
4160 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004161 /* Note: exit _without_ running nfs4_locku_done */
4162 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004163 return;
4164 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004165 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004166 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004167 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004168 &calldata->res.seq_res, task))
Andy Adamsona8936932009-04-01 09:22:23 -04004169 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004170 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171}
4172
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004173static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004174 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004175 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004176 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004177};
4178
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004179static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4180 struct nfs_open_context *ctx,
4181 struct nfs4_lock_state *lsp,
4182 struct nfs_seqid *seqid)
4183{
4184 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004185 struct rpc_message msg = {
4186 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4187 .rpc_cred = ctx->cred,
4188 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004189 struct rpc_task_setup task_setup_data = {
4190 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004191 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004192 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004193 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004194 .flags = RPC_TASK_ASYNC,
4195 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004196
Frank Filz137d6ac2007-07-09 15:32:29 -07004197 /* Ensure this is an unlock - when canceling a lock, the
4198 * canceled lock is passed in, and it won't be an unlock.
4199 */
4200 fl->fl_type = F_UNLCK;
4201
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004202 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4203 if (data == NULL) {
4204 nfs_free_seqid(seqid);
4205 return ERR_PTR(-ENOMEM);
4206 }
4207
Trond Myklebust9d12b212012-01-17 22:04:25 -05004208 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004209 msg.rpc_argp = &data->arg;
4210 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004211 task_setup_data.callback_data = data;
4212 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004213}
4214
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4216{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004217 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004218 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004219 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004220 struct rpc_task *task;
4221 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004222 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223
Trond Myklebust9b073572006-06-29 16:38:34 -04004224 status = nfs4_set_lock_state(state, request);
4225 /* Unlock _before_ we do the RPC call */
4226 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004227 down_read(&nfsi->rwsem);
4228 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4229 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004230 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004231 }
4232 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004233 if (status != 0)
4234 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004235 /* Is this a delegated lock? */
4236 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004237 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004238 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004239 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004240 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004241 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004242 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004243 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004244 status = PTR_ERR(task);
4245 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004246 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004247 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004248 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004249out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004250 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004251 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252}
4253
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004254struct nfs4_lockdata {
4255 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004256 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004257 struct nfs4_lock_state *lsp;
4258 struct nfs_open_context *ctx;
4259 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004260 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004261 int rpc_status;
4262 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004263 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004264};
4265
4266static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004267 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4268 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004269{
4270 struct nfs4_lockdata *p;
4271 struct inode *inode = lsp->ls_state->inode;
4272 struct nfs_server *server = NFS_SERVER(inode);
4273
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004274 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004275 if (p == NULL)
4276 return NULL;
4277
4278 p->arg.fh = NFS_FH(inode);
4279 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004280 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004281 if (p->arg.open_seqid == NULL)
4282 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004283 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004284 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004285 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004286 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004287 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004288 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004289 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004290 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004291 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004292 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004293 atomic_inc(&lsp->ls_count);
4294 p->ctx = get_nfs_open_context(ctx);
4295 memcpy(&p->fl, fl, sizeof(p->fl));
4296 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004297out_free_seqid:
4298 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004299out_free:
4300 kfree(p);
4301 return NULL;
4302}
4303
4304static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4305{
4306 struct nfs4_lockdata *data = calldata;
4307 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308
Harvey Harrison3110ff82008-05-02 13:42:44 -07004309 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004310 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4311 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004312 /* Do we need to do an open_to_lock_owner? */
4313 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004314 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4315 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004316 data->arg.open_stateid = &state->stateid;
4317 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004318 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004319 } else
4320 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004321 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004322 if (nfs4_setup_sequence(data->server,
4323 &data->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004324 &data->res.seq_res, task))
Andy Adamson66179ef2009-04-01 09:22:22 -04004325 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004326 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004327 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004328}
4329
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004330static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4331{
4332 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4333 nfs4_lock_prepare(task, calldata);
4334}
4335
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004336static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4337{
4338 struct nfs4_lockdata *data = calldata;
4339
Harvey Harrison3110ff82008-05-02 13:42:44 -07004340 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004341
Trond Myklebust14516c32010-07-31 14:29:06 -04004342 if (!nfs4_sequence_done(task, &data->res.seq_res))
4343 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004344
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004345 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004346 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004347 if (data->rpc_status == 0)
4348 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4349 else
4350 goto out;
4351 }
4352 if (data->rpc_status == 0) {
4353 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4354 sizeof(data->lsp->ls_stateid.data));
4355 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Al Viro3d4ff432011-06-22 18:40:12 -04004356 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004357 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004358out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004359 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004360}
4361
4362static void nfs4_lock_release(void *calldata)
4363{
4364 struct nfs4_lockdata *data = calldata;
4365
Harvey Harrison3110ff82008-05-02 13:42:44 -07004366 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004367 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004368 if (data->cancelled != 0) {
4369 struct rpc_task *task;
4370 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4371 data->arg.lock_seqid);
4372 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004373 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004374 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004375 } else
4376 nfs_free_seqid(data->arg.lock_seqid);
4377 nfs4_put_lock_state(data->lsp);
4378 put_nfs_open_context(data->ctx);
4379 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004380 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004381}
4382
4383static const struct rpc_call_ops nfs4_lock_ops = {
4384 .rpc_call_prepare = nfs4_lock_prepare,
4385 .rpc_call_done = nfs4_lock_done,
4386 .rpc_release = nfs4_lock_release,
4387};
4388
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004389static const struct rpc_call_ops nfs4_recover_lock_ops = {
4390 .rpc_call_prepare = nfs4_recover_lock_prepare,
4391 .rpc_call_done = nfs4_lock_done,
4392 .rpc_release = nfs4_lock_release,
4393};
4394
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004395static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4396{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004397 switch (error) {
4398 case -NFS4ERR_ADMIN_REVOKED:
4399 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004400 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004401 if (new_lock_owner != 0 ||
4402 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004403 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004404 break;
4405 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004406 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004407 case -NFS4ERR_EXPIRED:
4408 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004409 };
4410}
4411
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004412static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004413{
4414 struct nfs4_lockdata *data;
4415 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004416 struct rpc_message msg = {
4417 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4418 .rpc_cred = state->owner->so_cred,
4419 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004420 struct rpc_task_setup task_setup_data = {
4421 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004422 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004423 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004424 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004425 .flags = RPC_TASK_ASYNC,
4426 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004427 int ret;
4428
Harvey Harrison3110ff82008-05-02 13:42:44 -07004429 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004430 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004431 fl->fl_u.nfs4_fl.owner,
4432 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004433 if (data == NULL)
4434 return -ENOMEM;
4435 if (IS_SETLKW(cmd))
4436 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004437 if (recovery_type > NFS_LOCK_NEW) {
4438 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004439 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004440 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4441 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05004442 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004443 msg.rpc_argp = &data->arg;
4444 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004445 task_setup_data.callback_data = data;
4446 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004447 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004448 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004449 ret = nfs4_wait_for_completion_rpc_task(task);
4450 if (ret == 0) {
4451 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004452 if (ret)
4453 nfs4_handle_setlk_error(data->server, data->lsp,
4454 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004455 } else
4456 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004457 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004458 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004459 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460}
4461
4462static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4463{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004464 struct nfs_server *server = NFS_SERVER(state->inode);
4465 struct nfs4_exception exception = { };
4466 int err;
4467
4468 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004469 /* Cache the lock if possible... */
4470 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4471 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004472 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004473 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004474 break;
4475 nfs4_handle_exception(server, err, &exception);
4476 } while (exception.retry);
4477 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478}
4479
4480static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4481{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004482 struct nfs_server *server = NFS_SERVER(state->inode);
4483 struct nfs4_exception exception = { };
4484 int err;
4485
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004486 err = nfs4_set_lock_state(state, request);
4487 if (err != 0)
4488 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004489 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004490 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4491 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004492 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004493 switch (err) {
4494 default:
4495 goto out;
4496 case -NFS4ERR_GRACE:
4497 case -NFS4ERR_DELAY:
4498 nfs4_handle_exception(server, err, &exception);
4499 err = 0;
4500 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004501 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004502out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004503 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504}
4505
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004506#if defined(CONFIG_NFS_V4_1)
4507static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4508{
4509 int status;
4510 struct nfs_server *server = NFS_SERVER(state->inode);
4511
4512 status = nfs41_test_stateid(server, state);
4513 if (status == NFS_OK)
4514 return 0;
4515 nfs41_free_stateid(server, state);
4516 return nfs4_lock_expired(state, request);
4517}
4518#endif
4519
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4521{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004522 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004523 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004524 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004526 if ((fl_flags & FL_POSIX) &&
4527 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4528 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004529 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004530 status = nfs4_set_lock_state(state, request);
4531 if (status != 0)
4532 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004533 request->fl_flags |= FL_ACCESS;
4534 status = do_vfs_lock(request->fl_file, request);
4535 if (status < 0)
4536 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004537 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004538 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004539 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004540 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004541 request->fl_flags = fl_flags & ~FL_SLEEP;
4542 status = do_vfs_lock(request->fl_file, request);
4543 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004544 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004545 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004546 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004547 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004548 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004549 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004550 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07004551 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004552out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004553 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004554out:
4555 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556 return status;
4557}
4558
4559static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4560{
4561 struct nfs4_exception exception = { };
4562 int err;
4563
4564 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004565 err = _nfs4_proc_setlk(state, cmd, request);
4566 if (err == -NFS4ERR_DENIED)
4567 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004569 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570 } while (exception.retry);
4571 return err;
4572}
4573
4574static int
4575nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4576{
4577 struct nfs_open_context *ctx;
4578 struct nfs4_state *state;
4579 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4580 int status;
4581
4582 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004583 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584 state = ctx->state;
4585
4586 if (request->fl_start < 0 || request->fl_end < 0)
4587 return -EINVAL;
4588
Trond Myklebustd9531262009-07-21 19:22:38 -04004589 if (IS_GETLK(cmd)) {
4590 if (state != NULL)
4591 return nfs4_proc_getlk(state, F_GETLK, request);
4592 return 0;
4593 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594
4595 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4596 return -EINVAL;
4597
Trond Myklebustd9531262009-07-21 19:22:38 -04004598 if (request->fl_type == F_UNLCK) {
4599 if (state != NULL)
4600 return nfs4_proc_unlck(state, cmd, request);
4601 return 0;
4602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603
Trond Myklebustd9531262009-07-21 19:22:38 -04004604 if (state == NULL)
4605 return -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606 do {
4607 status = nfs4_proc_setlk(state, cmd, request);
4608 if ((status != -EAGAIN) || IS_SETLK(cmd))
4609 break;
4610 timeout = nfs4_set_lock_task_retry(timeout);
4611 status = -ERESTARTSYS;
4612 if (signalled())
4613 break;
4614 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615 return status;
4616}
4617
Trond Myklebust888e6942005-11-04 15:38:11 -05004618int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4619{
4620 struct nfs_server *server = NFS_SERVER(state->inode);
4621 struct nfs4_exception exception = { };
4622 int err;
4623
4624 err = nfs4_set_lock_state(state, fl);
4625 if (err != 0)
4626 goto out;
4627 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004628 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004629 switch (err) {
4630 default:
4631 printk(KERN_ERR "%s: unhandled error %d.\n",
4632 __func__, err);
4633 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004634 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004635 goto out;
4636 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004637 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07004638 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004639 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004640 nfs4_schedule_lease_recovery(server->nfs_client);
4641 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004642 case -NFS4ERR_BADSESSION:
4643 case -NFS4ERR_BADSLOT:
4644 case -NFS4ERR_BAD_HIGH_SLOT:
4645 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4646 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004647 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Trond Myklebustd5122202009-06-17 13:22:58 -07004648 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004649 case -ERESTARTSYS:
4650 /*
4651 * The show must go on: exit, but mark the
4652 * stateid as needing recovery.
4653 */
4654 case -NFS4ERR_ADMIN_REVOKED:
4655 case -NFS4ERR_BAD_STATEID:
4656 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004657 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004658 err = 0;
4659 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004660 case -EKEYEXPIRED:
4661 /*
4662 * User RPCSEC_GSS context has expired.
4663 * We cannot recover this stateid now, so
4664 * skip it and allow recovery thread to
4665 * proceed.
4666 */
4667 err = 0;
4668 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004669 case -ENOMEM:
4670 case -NFS4ERR_DENIED:
4671 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4672 err = 0;
4673 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004674 case -NFS4ERR_DELAY:
4675 break;
4676 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004677 err = nfs4_handle_exception(server, err, &exception);
4678 } while (exception.retry);
4679out:
4680 return err;
4681}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004682
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004683static void nfs4_release_lockowner_release(void *calldata)
4684{
4685 kfree(calldata);
4686}
4687
4688const struct rpc_call_ops nfs4_release_lockowner_ops = {
4689 .rpc_release = nfs4_release_lockowner_release,
4690};
4691
4692void nfs4_release_lockowner(const struct nfs4_lock_state *lsp)
4693{
4694 struct nfs_server *server = lsp->ls_state->owner->so_server;
4695 struct nfs_release_lockowner_args *args;
4696 struct rpc_message msg = {
4697 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4698 };
4699
4700 if (server->nfs_client->cl_mvops->minor_version != 0)
4701 return;
4702 args = kmalloc(sizeof(*args), GFP_NOFS);
4703 if (!args)
4704 return;
4705 args->lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004706 args->lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004707 args->lock_owner.s_dev = server->s_dev;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004708 msg.rpc_argp = args;
4709 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, args);
4710}
4711
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004712#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4713
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004714static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4715 const void *buf, size_t buflen,
4716 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004717{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004718 if (strcmp(key, "") != 0)
4719 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004720
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004721 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004722}
4723
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004724static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4725 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004726{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004727 if (strcmp(key, "") != 0)
4728 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004729
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004730 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004731}
4732
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004733static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4734 size_t list_len, const char *name,
4735 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004736{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004737 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004738
J. Bruce Fields096455a2006-03-20 23:23:42 -05004739 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4740 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004741
4742 if (list && len <= list_len)
4743 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004744 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004745}
4746
Andy Adamson533eb462011-06-13 18:25:56 -04004747/*
4748 * nfs_fhget will use either the mounted_on_fileid or the fileid
4749 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004750static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4751{
Andy Adamson533eb462011-06-13 18:25:56 -04004752 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
4753 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
4754 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4755 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004756 return;
4757
4758 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4759 NFS_ATTR_FATTR_NLINK;
4760 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4761 fattr->nlink = 2;
4762}
4763
Trond Myklebust56659e92007-07-17 21:52:39 -04004764int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004765 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004766{
4767 struct nfs_server *server = NFS_SERVER(dir);
4768 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004769 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004770 };
4771 struct nfs4_fs_locations_arg args = {
4772 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004773 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004774 .page = page,
4775 .bitmask = bitmask,
4776 };
Benny Halevy22958462009-04-01 09:22:02 -04004777 struct nfs4_fs_locations_res res = {
4778 .fs_locations = fs_locations,
4779 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004780 struct rpc_message msg = {
4781 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4782 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004783 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004784 };
4785 int status;
4786
Harvey Harrison3110ff82008-05-02 13:42:44 -07004787 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04004788
4789 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
4790 * is not supported */
4791 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
4792 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
4793 else
4794 bitmask[0] |= FATTR4_WORD0_FILEID;
4795
Trond Myklebustc228fd32007-01-13 02:28:11 -05004796 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004797 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004798 fs_locations->nlocations = 0;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004799 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004800 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004801 return status;
4802}
4803
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00004804static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4805{
4806 int status;
4807 struct nfs4_secinfo_arg args = {
4808 .dir_fh = NFS_FH(dir),
4809 .name = name,
4810 };
4811 struct nfs4_secinfo_res res = {
4812 .flavors = flavors,
4813 };
4814 struct rpc_message msg = {
4815 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
4816 .rpc_argp = &args,
4817 .rpc_resp = &res,
4818 };
4819
4820 dprintk("NFS call secinfo %s\n", name->name);
4821 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4822 dprintk("NFS reply secinfo: %d\n", status);
4823 return status;
4824}
4825
4826int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4827{
4828 struct nfs4_exception exception = { };
4829 int err;
4830 do {
4831 err = nfs4_handle_exception(NFS_SERVER(dir),
4832 _nfs4_proc_secinfo(dir, name, flavors),
4833 &exception);
4834 } while (exception.retry);
4835 return err;
4836}
4837
Andy Adamson557134a2009-04-01 09:21:53 -04004838#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04004839/*
Andy Adamson357f54d2010-12-14 10:11:57 -05004840 * Check the exchange flags returned by the server for invalid flags, having
4841 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
4842 * DS flags set.
4843 */
4844static int nfs4_check_cl_exchange_flags(u32 flags)
4845{
4846 if (flags & ~EXCHGID4_FLAG_MASK_R)
4847 goto out_inval;
4848 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
4849 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
4850 goto out_inval;
4851 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
4852 goto out_inval;
4853 return NFS_OK;
4854out_inval:
4855 return -NFS4ERR_INVAL;
4856}
4857
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004858static bool
4859nfs41_same_server_scope(struct server_scope *a, struct server_scope *b)
4860{
4861 if (a->server_scope_sz == b->server_scope_sz &&
4862 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
4863 return true;
4864
4865 return false;
4866}
4867
Andy Adamson357f54d2010-12-14 10:11:57 -05004868/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04004869 * nfs4_proc_exchange_id()
4870 *
4871 * Since the clientid has expired, all compounds using sessions
4872 * associated with the stale clientid will be returning
4873 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4874 * be in some phase of session reset.
4875 */
Andy Adamson4d643d12009-12-04 15:52:24 -05004876int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004877{
4878 nfs4_verifier verifier;
4879 struct nfs41_exchange_id_args args = {
4880 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05004881 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04004882 };
4883 struct nfs41_exchange_id_res res = {
4884 .client = clp,
4885 };
4886 int status;
4887 struct rpc_message msg = {
4888 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4889 .rpc_argp = &args,
4890 .rpc_resp = &res,
4891 .rpc_cred = cred,
4892 };
4893 __be32 *p;
4894
4895 dprintk("--> %s\n", __func__);
4896 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04004897
Benny Halevy99fe60d2009-04-01 09:22:29 -04004898 p = (u32 *)verifier.data;
4899 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4900 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4901 args.verifier = &verifier;
4902
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004903 args.id_len = scnprintf(args.id, sizeof(args.id),
4904 "%s/%s.%s/%u",
4905 clp->cl_ipaddr,
4906 init_utsname()->nodename,
4907 init_utsname()->domainname,
4908 clp->cl_rpcclient->cl_auth->au_flavor);
Benny Halevy99fe60d2009-04-01 09:22:29 -04004909
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004910 res.server_scope = kzalloc(sizeof(struct server_scope), GFP_KERNEL);
4911 if (unlikely(!res.server_scope))
4912 return -ENOMEM;
4913
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004914 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004915 if (!status)
4916 status = nfs4_check_cl_exchange_flags(clp->cl_exchange_flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004917
4918 if (!status) {
4919 if (clp->server_scope &&
4920 !nfs41_same_server_scope(clp->server_scope,
4921 res.server_scope)) {
4922 dprintk("%s: server_scope mismatch detected\n",
4923 __func__);
4924 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
4925 kfree(clp->server_scope);
4926 clp->server_scope = NULL;
4927 }
4928
4929 if (!clp->server_scope)
4930 clp->server_scope = res.server_scope;
4931 else
4932 kfree(res.server_scope);
4933 }
4934
Benny Halevy99fe60d2009-04-01 09:22:29 -04004935 dprintk("<-- %s status= %d\n", __func__, status);
4936 return status;
4937}
4938
Andy Adamson2050f0c2009-04-01 09:22:30 -04004939struct nfs4_get_lease_time_data {
4940 struct nfs4_get_lease_time_args *args;
4941 struct nfs4_get_lease_time_res *res;
4942 struct nfs_client *clp;
4943};
4944
4945static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4946 void *calldata)
4947{
4948 int ret;
4949 struct nfs4_get_lease_time_data *data =
4950 (struct nfs4_get_lease_time_data *)calldata;
4951
4952 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004953 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004954 /* just setup sequence, do not trigger session recovery
4955 since we're invoked within one */
4956 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004957 &data->args->la_seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004958 &data->res->lr_seq_res, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004959
4960 BUG_ON(ret == -EAGAIN);
4961 rpc_call_start(task);
4962 dprintk("<-- %s\n", __func__);
4963}
4964
4965/*
4966 * Called from nfs4_state_manager thread for session setup, so don't recover
4967 * from sequence operation or clientid errors.
4968 */
4969static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4970{
4971 struct nfs4_get_lease_time_data *data =
4972 (struct nfs4_get_lease_time_data *)calldata;
4973
4974 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04004975 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
4976 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04004977 switch (task->tk_status) {
4978 case -NFS4ERR_DELAY:
4979 case -NFS4ERR_GRACE:
4980 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4981 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4982 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004983 /* fall through */
4984 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004985 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004986 return;
4987 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04004988 dprintk("<-- %s\n", __func__);
4989}
4990
4991struct rpc_call_ops nfs4_get_lease_time_ops = {
4992 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4993 .rpc_call_done = nfs4_get_lease_time_done,
4994};
4995
4996int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4997{
4998 struct rpc_task *task;
4999 struct nfs4_get_lease_time_args args;
5000 struct nfs4_get_lease_time_res res = {
5001 .lr_fsinfo = fsinfo,
5002 };
5003 struct nfs4_get_lease_time_data data = {
5004 .args = &args,
5005 .res = &res,
5006 .clp = clp,
5007 };
5008 struct rpc_message msg = {
5009 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5010 .rpc_argp = &args,
5011 .rpc_resp = &res,
5012 };
5013 struct rpc_task_setup task_setup = {
5014 .rpc_client = clp->cl_rpcclient,
5015 .rpc_message = &msg,
5016 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005017 .callback_data = &data,
5018 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005019 };
5020 int status;
5021
Trond Myklebust9d12b212012-01-17 22:04:25 -05005022 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005023 dprintk("--> %s\n", __func__);
5024 task = rpc_run_task(&task_setup);
5025
5026 if (IS_ERR(task))
5027 status = PTR_ERR(task);
5028 else {
5029 status = task->tk_status;
5030 rpc_put_task(task);
5031 }
5032 dprintk("<-- %s return %d\n", __func__, status);
5033
5034 return status;
5035}
5036
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005037/*
5038 * Reset a slot table
5039 */
Andy Adamson104aeba2010-01-14 17:45:10 -05005040static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
5041 int ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005042{
Andy Adamson104aeba2010-01-14 17:45:10 -05005043 struct nfs4_slot *new = NULL;
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005044 int i;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005045 int ret = 0;
5046
Andy Adamson104aeba2010-01-14 17:45:10 -05005047 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5048 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005049
Andy Adamson104aeba2010-01-14 17:45:10 -05005050 /* Does the newly negotiated max_reqs match the existing slot table? */
5051 if (max_reqs != tbl->max_slots) {
5052 ret = -ENOMEM;
5053 new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005054 GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05005055 if (!new)
5056 goto out;
5057 ret = 0;
5058 kfree(tbl->slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005059 }
5060 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson104aeba2010-01-14 17:45:10 -05005061 if (new) {
5062 tbl->slots = new;
5063 tbl->max_slots = max_reqs;
5064 }
5065 for (i = 0; i < tbl->max_slots; ++i)
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005066 tbl->slots[i].seq_nr = ivalue;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005067 spin_unlock(&tbl->slot_tbl_lock);
5068 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5069 tbl, tbl->slots, tbl->max_slots);
5070out:
5071 dprintk("<-- %s: return %d\n", __func__, ret);
5072 return ret;
5073}
5074
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005075/* Destroy the slot table */
5076static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5077{
5078 if (session->fc_slot_table.slots != NULL) {
5079 kfree(session->fc_slot_table.slots);
5080 session->fc_slot_table.slots = NULL;
5081 }
5082 if (session->bc_slot_table.slots != NULL) {
5083 kfree(session->bc_slot_table.slots);
5084 session->bc_slot_table.slots = NULL;
5085 }
5086 return;
5087}
5088
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005089/*
Andy Adamsonfc931582009-04-01 09:22:31 -04005090 * Initialize slot table
5091 */
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005092static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
5093 int max_slots, int ivalue)
Andy Adamsonfc931582009-04-01 09:22:31 -04005094{
Andy Adamsonfc931582009-04-01 09:22:31 -04005095 struct nfs4_slot *slot;
5096 int ret = -ENOMEM;
5097
5098 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
5099
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005100 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
Andy Adamsonfc931582009-04-01 09:22:31 -04005101
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005102 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_NOFS);
Andy Adamsonfc931582009-04-01 09:22:31 -04005103 if (!slot)
5104 goto out;
Andy Adamsonfc931582009-04-01 09:22:31 -04005105 ret = 0;
5106
5107 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonfc931582009-04-01 09:22:31 -04005108 tbl->max_slots = max_slots;
5109 tbl->slots = slot;
5110 tbl->highest_used_slotid = -1; /* no slot is currently used */
5111 spin_unlock(&tbl->slot_tbl_lock);
5112 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5113 tbl, tbl->slots, tbl->max_slots);
5114out:
5115 dprintk("<-- %s: return %d\n", __func__, ret);
5116 return ret;
Andy Adamsonfc931582009-04-01 09:22:31 -04005117}
5118
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005119/*
Andy Adamsonaacd5532011-11-09 13:58:21 -05005120 * Initialize or reset the forechannel and backchannel tables
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005121 */
Andy Adamsonaacd5532011-11-09 13:58:21 -05005122static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005123{
Trond Myklebustf26468f2009-12-05 19:32:11 -05005124 struct nfs4_slot_table *tbl;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005125 int status;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005126
Andy Adamsonaacd5532011-11-09 13:58:21 -05005127 dprintk("--> %s\n", __func__);
5128 /* Fore channel */
5129 tbl = &ses->fc_slot_table;
Trond Myklebustf26468f2009-12-05 19:32:11 -05005130 if (tbl->slots == NULL) {
Andy Adamsonaacd5532011-11-09 13:58:21 -05005131 status = nfs4_init_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5132 if (status) /* -ENOMEM */
5133 return status;
5134 } else {
5135 status = nfs4_reset_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
Trond Myklebustf26468f2009-12-05 19:32:11 -05005136 if (status)
5137 return status;
5138 }
Andy Adamsonaacd5532011-11-09 13:58:21 -05005139 /* Back channel */
5140 tbl = &ses->bc_slot_table;
Trond Myklebustf26468f2009-12-05 19:32:11 -05005141 if (tbl->slots == NULL) {
Andy Adamsonaacd5532011-11-09 13:58:21 -05005142 status = nfs4_init_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
Trond Myklebustf26468f2009-12-05 19:32:11 -05005143 if (status)
Andy Adamsonaacd5532011-11-09 13:58:21 -05005144 /* Fore and back channel share a connection so get
5145 * both slot tables or neither */
5146 nfs4_destroy_slot_tables(ses);
5147 } else
5148 status = nfs4_reset_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005149 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04005150}
5151
5152struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5153{
5154 struct nfs4_session *session;
5155 struct nfs4_slot_table *tbl;
5156
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005157 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04005158 if (!session)
5159 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04005160
Andy Adamson557134a2009-04-01 09:21:53 -04005161 tbl = &session->fc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05005162 tbl->highest_used_slotid = -1;
Andy Adamson557134a2009-04-01 09:21:53 -04005163 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005164 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005165 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005166
5167 tbl = &session->bc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05005168 tbl->highest_used_slotid = -1;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005169 spin_lock_init(&tbl->slot_tbl_lock);
5170 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005171 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005172
Trond Myklebust1055d762010-06-16 09:52:27 -04005173 session->session_state = 1<<NFS4_SESSION_INITING;
5174
Andy Adamson557134a2009-04-01 09:21:53 -04005175 session->clp = clp;
5176 return session;
5177}
5178
5179void nfs4_destroy_session(struct nfs4_session *session)
5180{
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005181 nfs4_proc_destroy_session(session);
5182 dprintk("%s Destroy backchannel for xprt %p\n",
5183 __func__, session->clp->cl_rpcclient->cl_xprt);
5184 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
5185 NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005186 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04005187 kfree(session);
5188}
5189
Andy Adamsonfc931582009-04-01 09:22:31 -04005190/*
5191 * Initialize the values to be used by the client in CREATE_SESSION
5192 * If nfs4_init_session set the fore channel request and response sizes,
5193 * use them.
5194 *
5195 * Set the back channel max_resp_sz_cached to zero to force the client to
5196 * always set csa_cachethis to FALSE because the current implementation
5197 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5198 */
5199static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5200{
5201 struct nfs4_session *session = args->client->cl_session;
5202 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5203 mxresp_sz = session->fc_attrs.max_resp_sz;
5204
5205 if (mxrqst_sz == 0)
5206 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5207 if (mxresp_sz == 0)
5208 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5209 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005210 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5211 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005212 args->fc_attrs.max_ops = NFS4_MAX_OPS;
5213 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
5214
5215 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005216 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005217 __func__,
5218 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005219 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005220
5221 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005222 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5223 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5224 args->bc_attrs.max_resp_sz_cached = 0;
5225 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5226 args->bc_attrs.max_reqs = 1;
5227
5228 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5229 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5230 __func__,
5231 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5232 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5233 args->bc_attrs.max_reqs);
5234}
5235
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005236static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005237{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005238 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5239 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5240
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005241 if (rcvd->max_resp_sz > sent->max_resp_sz)
5242 return -EINVAL;
5243 /*
5244 * Our requested max_ops is the minimum we need; we're not
5245 * prepared to break up compounds into smaller pieces than that.
5246 * So, no point even trying to continue if the server won't
5247 * cooperate:
5248 */
5249 if (rcvd->max_ops < sent->max_ops)
5250 return -EINVAL;
5251 if (rcvd->max_reqs == 0)
5252 return -EINVAL;
5253 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005254}
5255
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005256static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5257{
5258 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5259 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005260
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005261 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5262 return -EINVAL;
5263 if (rcvd->max_resp_sz < sent->max_resp_sz)
5264 return -EINVAL;
5265 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5266 return -EINVAL;
5267 /* These would render the backchannel useless: */
5268 if (rcvd->max_ops == 0)
5269 return -EINVAL;
5270 if (rcvd->max_reqs == 0)
5271 return -EINVAL;
5272 return 0;
5273}
Andy Adamson8d353012009-04-01 09:22:32 -04005274
Andy Adamson8d353012009-04-01 09:22:32 -04005275static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5276 struct nfs4_session *session)
5277{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005278 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005279
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005280 ret = nfs4_verify_fore_channel_attrs(args, session);
5281 if (ret)
5282 return ret;
5283 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005284}
5285
Andy Adamsonfc931582009-04-01 09:22:31 -04005286static int _nfs4_proc_create_session(struct nfs_client *clp)
5287{
5288 struct nfs4_session *session = clp->cl_session;
5289 struct nfs41_create_session_args args = {
5290 .client = clp,
5291 .cb_program = NFS4_CALLBACK,
5292 };
5293 struct nfs41_create_session_res res = {
5294 .client = clp,
5295 };
5296 struct rpc_message msg = {
5297 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5298 .rpc_argp = &args,
5299 .rpc_resp = &res,
5300 };
5301 int status;
5302
5303 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005304 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005305
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005306 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005307
Andy Adamson8d353012009-04-01 09:22:32 -04005308 if (!status)
5309 /* Verify the session's negotiated channel_attrs values */
5310 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005311 if (!status) {
5312 /* Increment the clientid slot sequence id */
5313 clp->cl_seqid++;
5314 }
5315
5316 return status;
5317}
5318
5319/*
5320 * Issues a CREATE_SESSION operation to the server.
5321 * It is the responsibility of the caller to verify the session is
5322 * expired before calling this routine.
5323 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05005324int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04005325{
5326 int status;
5327 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005328 struct nfs4_session *session = clp->cl_session;
5329
5330 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5331
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005332 status = _nfs4_proc_create_session(clp);
Andy Adamsonfc931582009-04-01 09:22:31 -04005333 if (status)
5334 goto out;
5335
Andy Adamsonaacd5532011-11-09 13:58:21 -05005336 /* Init or reset the session slot tables */
5337 status = nfs4_setup_session_slot_tables(session);
5338 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005339 if (status)
5340 goto out;
5341
5342 ptr = (unsigned *)&session->sess_id.data[0];
5343 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5344 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005345out:
5346 dprintk("<-- %s\n", __func__);
5347 return status;
5348}
5349
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005350/*
5351 * Issue the over-the-wire RPC DESTROY_SESSION.
5352 * The caller must serialize access to this routine.
5353 */
5354int nfs4_proc_destroy_session(struct nfs4_session *session)
5355{
5356 int status = 0;
5357 struct rpc_message msg;
5358
5359 dprintk("--> nfs4_proc_destroy_session\n");
5360
5361 /* session is still being setup */
5362 if (session->clp->cl_cons_state != NFS_CS_READY)
5363 return status;
5364
5365 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5366 msg.rpc_argp = session;
5367 msg.rpc_resp = NULL;
5368 msg.rpc_cred = NULL;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005369 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005370
5371 if (status)
5372 printk(KERN_WARNING
5373 "Got error %d from the server on DESTROY_SESSION. "
5374 "Session has been destroyed regardless...\n", status);
5375
5376 dprintk("<-- nfs4_proc_destroy_session\n");
5377 return status;
5378}
5379
Trond Myklebustfccba802009-07-21 16:48:07 -04005380int nfs4_init_session(struct nfs_server *server)
5381{
5382 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005383 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005384 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005385 int ret;
5386
5387 if (!nfs4_has_session(clp))
5388 return 0;
5389
Trond Myklebust1055d762010-06-16 09:52:27 -04005390 session = clp->cl_session;
5391 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5392 return 0;
5393
Andy Adamson68bf05e2009-12-15 12:55:02 -05005394 rsize = server->rsize;
5395 if (rsize == 0)
5396 rsize = NFS_MAX_FILE_IO_SIZE;
5397 wsize = server->wsize;
5398 if (wsize == 0)
5399 wsize = NFS_MAX_FILE_IO_SIZE;
5400
Andy Adamson68bf05e2009-12-15 12:55:02 -05005401 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5402 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005403
Trond Myklebustfccba802009-07-21 16:48:07 -04005404 ret = nfs4_recover_expired_lease(server);
5405 if (!ret)
5406 ret = nfs4_check_client_ready(clp);
5407 return ret;
5408}
5409
Andy Adamsond83217c2011-03-01 01:34:17 +00005410int nfs4_init_ds_session(struct nfs_client *clp)
5411{
5412 struct nfs4_session *session = clp->cl_session;
5413 int ret;
5414
5415 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5416 return 0;
5417
5418 ret = nfs4_client_recover_expired_lease(clp);
5419 if (!ret)
5420 /* Test for the DS role */
5421 if (!is_ds_client(clp))
5422 ret = -ENODEV;
5423 if (!ret)
5424 ret = nfs4_check_client_ready(clp);
5425 return ret;
5426
5427}
5428EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5429
5430
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005431/*
5432 * Renew the cl_session lease.
5433 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005434struct nfs4_sequence_data {
5435 struct nfs_client *clp;
5436 struct nfs4_sequence_args args;
5437 struct nfs4_sequence_res res;
5438};
5439
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005440static void nfs41_sequence_release(void *data)
5441{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005442 struct nfs4_sequence_data *calldata = data;
5443 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005444
Alexandros Batsakis71358402010-02-05 03:45:05 -08005445 if (atomic_read(&clp->cl_count) > 1)
5446 nfs4_schedule_state_renewal(clp);
5447 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005448 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005449}
5450
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005451static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5452{
5453 switch(task->tk_status) {
5454 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005455 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5456 return -EAGAIN;
5457 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005458 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005459 }
5460 return 0;
5461}
5462
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005463static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005464{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005465 struct nfs4_sequence_data *calldata = data;
5466 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005467
Trond Myklebust14516c32010-07-31 14:29:06 -04005468 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5469 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005470
5471 if (task->tk_status < 0) {
5472 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005473 if (atomic_read(&clp->cl_count) == 1)
5474 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005475
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005476 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5477 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005478 return;
5479 }
5480 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005481 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005482out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005483 dprintk("<-- %s\n", __func__);
5484}
5485
5486static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5487{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005488 struct nfs4_sequence_data *calldata = data;
5489 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005490 struct nfs4_sequence_args *args;
5491 struct nfs4_sequence_res *res;
5492
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005493 args = task->tk_msg.rpc_argp;
5494 res = task->tk_msg.rpc_resp;
5495
Trond Myklebust9d12b212012-01-17 22:04:25 -05005496 if (nfs41_setup_sequence(clp->cl_session, args, res, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005497 return;
5498 rpc_call_start(task);
5499}
5500
5501static const struct rpc_call_ops nfs41_sequence_ops = {
5502 .rpc_call_done = nfs41_sequence_call_done,
5503 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005504 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005505};
5506
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005507static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005508{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005509 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005510 struct rpc_message msg = {
5511 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5512 .rpc_cred = cred,
5513 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005514 struct rpc_task_setup task_setup_data = {
5515 .rpc_client = clp->cl_rpcclient,
5516 .rpc_message = &msg,
5517 .callback_ops = &nfs41_sequence_ops,
5518 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5519 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005520
Alexandros Batsakis71358402010-02-05 03:45:05 -08005521 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005522 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005523 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005524 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005525 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005526 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005527 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05005528 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005529 msg.rpc_argp = &calldata->args;
5530 msg.rpc_resp = &calldata->res;
5531 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005532 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005533
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005534 return rpc_run_task(&task_setup_data);
5535}
5536
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005537static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005538{
5539 struct rpc_task *task;
5540 int ret = 0;
5541
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005542 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5543 return 0;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005544 task = _nfs41_proc_sequence(clp, cred);
5545 if (IS_ERR(task))
5546 ret = PTR_ERR(task);
5547 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005548 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005549 dprintk("<-- %s status=%d\n", __func__, ret);
5550 return ret;
5551}
5552
5553static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5554{
5555 struct rpc_task *task;
5556 int ret;
5557
5558 task = _nfs41_proc_sequence(clp, cred);
5559 if (IS_ERR(task)) {
5560 ret = PTR_ERR(task);
5561 goto out;
5562 }
5563 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005564 if (!ret) {
5565 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5566
5567 if (task->tk_status == 0)
5568 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005569 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005570 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005571 rpc_put_task(task);
5572out:
5573 dprintk("<-- %s status=%d\n", __func__, ret);
5574 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005575}
5576
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005577struct nfs4_reclaim_complete_data {
5578 struct nfs_client *clp;
5579 struct nfs41_reclaim_complete_args arg;
5580 struct nfs41_reclaim_complete_res res;
5581};
5582
5583static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5584{
5585 struct nfs4_reclaim_complete_data *calldata = data;
5586
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005587 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04005588 if (nfs41_setup_sequence(calldata->clp->cl_session,
5589 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005590 &calldata->res.seq_res, task))
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005591 return;
5592
5593 rpc_call_start(task);
5594}
5595
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005596static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5597{
5598 switch(task->tk_status) {
5599 case 0:
5600 case -NFS4ERR_COMPLETE_ALREADY:
5601 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5602 break;
5603 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005604 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005605 /* fall through */
5606 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005607 return -EAGAIN;
5608 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005609 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005610 }
5611 return 0;
5612}
5613
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005614static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5615{
5616 struct nfs4_reclaim_complete_data *calldata = data;
5617 struct nfs_client *clp = calldata->clp;
5618 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5619
5620 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005621 if (!nfs41_sequence_done(task, res))
5622 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005623
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005624 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5625 rpc_restart_call_prepare(task);
5626 return;
5627 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005628 dprintk("<-- %s\n", __func__);
5629}
5630
5631static void nfs4_free_reclaim_complete_data(void *data)
5632{
5633 struct nfs4_reclaim_complete_data *calldata = data;
5634
5635 kfree(calldata);
5636}
5637
5638static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5639 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5640 .rpc_call_done = nfs4_reclaim_complete_done,
5641 .rpc_release = nfs4_free_reclaim_complete_data,
5642};
5643
5644/*
5645 * Issue a global reclaim complete.
5646 */
5647static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5648{
5649 struct nfs4_reclaim_complete_data *calldata;
5650 struct rpc_task *task;
5651 struct rpc_message msg = {
5652 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5653 };
5654 struct rpc_task_setup task_setup_data = {
5655 .rpc_client = clp->cl_rpcclient,
5656 .rpc_message = &msg,
5657 .callback_ops = &nfs4_reclaim_complete_call_ops,
5658 .flags = RPC_TASK_ASYNC,
5659 };
5660 int status = -ENOMEM;
5661
5662 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005663 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005664 if (calldata == NULL)
5665 goto out;
5666 calldata->clp = clp;
5667 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005668
Trond Myklebust9d12b212012-01-17 22:04:25 -05005669 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005670 msg.rpc_argp = &calldata->arg;
5671 msg.rpc_resp = &calldata->res;
5672 task_setup_data.callback_data = calldata;
5673 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005674 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005675 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005676 goto out;
5677 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05005678 status = nfs4_wait_for_completion_rpc_task(task);
5679 if (status == 0)
5680 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005681 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005682 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005683out:
5684 dprintk("<-- %s status=%d\n", __func__, status);
5685 return status;
5686}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005687
5688static void
5689nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5690{
5691 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00005692 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005693
5694 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00005695 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5696 * right now covering the LAYOUTGET we are about to send.
5697 * However, that is not so catastrophic, and there seems
5698 * to be no way to prevent it completely.
5699 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005700 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005701 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005702 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00005703 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5704 NFS_I(lgp->args.inode)->layout,
5705 lgp->args.ctx->state)) {
5706 rpc_exit(task, NFS4_OK);
5707 return;
5708 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005709 rpc_call_start(task);
5710}
5711
5712static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5713{
5714 struct nfs4_layoutget *lgp = calldata;
5715 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5716
5717 dprintk("--> %s\n", __func__);
5718
5719 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
5720 return;
5721
5722 switch (task->tk_status) {
5723 case 0:
5724 break;
5725 case -NFS4ERR_LAYOUTTRYLATER:
5726 case -NFS4ERR_RECALLCONFLICT:
5727 task->tk_status = -NFS4ERR_DELAY;
5728 /* Fall through */
5729 default:
5730 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5731 rpc_restart_call_prepare(task);
5732 return;
5733 }
5734 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005735 dprintk("<-- %s\n", __func__);
5736}
5737
5738static void nfs4_layoutget_release(void *calldata)
5739{
5740 struct nfs4_layoutget *lgp = calldata;
5741
5742 dprintk("--> %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005743 put_nfs_open_context(lgp->args.ctx);
5744 kfree(calldata);
5745 dprintk("<-- %s\n", __func__);
5746}
5747
5748static const struct rpc_call_ops nfs4_layoutget_call_ops = {
5749 .rpc_call_prepare = nfs4_layoutget_prepare,
5750 .rpc_call_done = nfs4_layoutget_done,
5751 .rpc_release = nfs4_layoutget_release,
5752};
5753
5754int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
5755{
5756 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5757 struct rpc_task *task;
5758 struct rpc_message msg = {
5759 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
5760 .rpc_argp = &lgp->args,
5761 .rpc_resp = &lgp->res,
5762 };
5763 struct rpc_task_setup task_setup_data = {
5764 .rpc_client = server->client,
5765 .rpc_message = &msg,
5766 .callback_ops = &nfs4_layoutget_call_ops,
5767 .callback_data = lgp,
5768 .flags = RPC_TASK_ASYNC,
5769 };
5770 int status = 0;
5771
5772 dprintk("--> %s\n", __func__);
5773
Weston Andros Adamson35124a02011-03-24 16:48:21 -04005774 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005775 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05005776 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005777 task = rpc_run_task(&task_setup_data);
5778 if (IS_ERR(task))
5779 return PTR_ERR(task);
5780 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00005781 if (status == 0)
5782 status = task->tk_status;
5783 if (status == 0)
5784 status = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005785 rpc_put_task(task);
5786 dprintk("<-- %s status=%d\n", __func__, status);
5787 return status;
5788}
5789
Benny Halevycbe82602011-05-22 19:52:37 +03005790static void
5791nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
5792{
5793 struct nfs4_layoutreturn *lrp = calldata;
5794
5795 dprintk("--> %s\n", __func__);
5796 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005797 &lrp->res.seq_res, task))
Benny Halevycbe82602011-05-22 19:52:37 +03005798 return;
5799 rpc_call_start(task);
5800}
5801
5802static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
5803{
5804 struct nfs4_layoutreturn *lrp = calldata;
5805 struct nfs_server *server;
Trond Myklebusta56aaa02011-06-15 11:59:10 -04005806 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03005807
5808 dprintk("--> %s\n", __func__);
5809
5810 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
5811 return;
5812
5813 server = NFS_SERVER(lrp->args.inode);
5814 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005815 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03005816 return;
5817 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04005818 spin_lock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03005819 if (task->tk_status == 0) {
Benny Halevycbe82602011-05-22 19:52:37 +03005820 if (lrp->res.lrs_present) {
Benny Halevycbe82602011-05-22 19:52:37 +03005821 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Benny Halevycbe82602011-05-22 19:52:37 +03005822 } else
5823 BUG_ON(!list_empty(&lo->plh_segs));
5824 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04005825 lo->plh_block_lgets--;
5826 spin_unlock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03005827 dprintk("<-- %s\n", __func__);
5828}
5829
5830static void nfs4_layoutreturn_release(void *calldata)
5831{
5832 struct nfs4_layoutreturn *lrp = calldata;
5833
5834 dprintk("--> %s\n", __func__);
Trond Myklebusta56aaa02011-06-15 11:59:10 -04005835 put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03005836 kfree(calldata);
5837 dprintk("<-- %s\n", __func__);
5838}
5839
5840static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
5841 .rpc_call_prepare = nfs4_layoutreturn_prepare,
5842 .rpc_call_done = nfs4_layoutreturn_done,
5843 .rpc_release = nfs4_layoutreturn_release,
5844};
5845
5846int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
5847{
5848 struct rpc_task *task;
5849 struct rpc_message msg = {
5850 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
5851 .rpc_argp = &lrp->args,
5852 .rpc_resp = &lrp->res,
5853 };
5854 struct rpc_task_setup task_setup_data = {
5855 .rpc_client = lrp->clp->cl_rpcclient,
5856 .rpc_message = &msg,
5857 .callback_ops = &nfs4_layoutreturn_call_ops,
5858 .callback_data = lrp,
5859 };
5860 int status;
5861
5862 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05005863 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03005864 task = rpc_run_task(&task_setup_data);
5865 if (IS_ERR(task))
5866 return PTR_ERR(task);
5867 status = task->tk_status;
5868 dprintk("<-- %s status=%d\n", __func__, status);
5869 rpc_put_task(task);
5870 return status;
5871}
5872
Andy Adamson7f11d8d2011-07-30 20:52:35 -04005873/*
5874 * Retrieve the list of Data Server devices from the MDS.
5875 */
5876static int _nfs4_getdevicelist(struct nfs_server *server,
5877 const struct nfs_fh *fh,
5878 struct pnfs_devicelist *devlist)
5879{
5880 struct nfs4_getdevicelist_args args = {
5881 .fh = fh,
5882 .layoutclass = server->pnfs_curr_ld->id,
5883 };
5884 struct nfs4_getdevicelist_res res = {
5885 .devlist = devlist,
5886 };
5887 struct rpc_message msg = {
5888 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
5889 .rpc_argp = &args,
5890 .rpc_resp = &res,
5891 };
5892 int status;
5893
5894 dprintk("--> %s\n", __func__);
5895 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
5896 &res.seq_res, 0);
5897 dprintk("<-- %s status=%d\n", __func__, status);
5898 return status;
5899}
5900
5901int nfs4_proc_getdevicelist(struct nfs_server *server,
5902 const struct nfs_fh *fh,
5903 struct pnfs_devicelist *devlist)
5904{
5905 struct nfs4_exception exception = { };
5906 int err;
5907
5908 do {
5909 err = nfs4_handle_exception(server,
5910 _nfs4_getdevicelist(server, fh, devlist),
5911 &exception);
5912 } while (exception.retry);
5913
5914 dprintk("%s: err=%d, num_devs=%u\n", __func__,
5915 err, devlist->num_devs);
5916
5917 return err;
5918}
5919EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
5920
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005921static int
5922_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5923{
5924 struct nfs4_getdeviceinfo_args args = {
5925 .pdev = pdev,
5926 };
5927 struct nfs4_getdeviceinfo_res res = {
5928 .pdev = pdev,
5929 };
5930 struct rpc_message msg = {
5931 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
5932 .rpc_argp = &args,
5933 .rpc_resp = &res,
5934 };
5935 int status;
5936
5937 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005938 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005939 dprintk("<-- %s status=%d\n", __func__, status);
5940
5941 return status;
5942}
5943
5944int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5945{
5946 struct nfs4_exception exception = { };
5947 int err;
5948
5949 do {
5950 err = nfs4_handle_exception(server,
5951 _nfs4_proc_getdeviceinfo(server, pdev),
5952 &exception);
5953 } while (exception.retry);
5954 return err;
5955}
5956EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
5957
Andy Adamson863a3c62011-03-23 13:27:54 +00005958static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
5959{
5960 struct nfs4_layoutcommit_data *data = calldata;
5961 struct nfs_server *server = NFS_SERVER(data->args.inode);
5962
5963 if (nfs4_setup_sequence(server, &data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005964 &data->res.seq_res, task))
Andy Adamson863a3c62011-03-23 13:27:54 +00005965 return;
5966 rpc_call_start(task);
5967}
5968
5969static void
5970nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
5971{
5972 struct nfs4_layoutcommit_data *data = calldata;
5973 struct nfs_server *server = NFS_SERVER(data->args.inode);
5974
5975 if (!nfs4_sequence_done(task, &data->res.seq_res))
5976 return;
5977
5978 switch (task->tk_status) { /* Just ignore these failures */
5979 case NFS4ERR_DELEG_REVOKED: /* layout was recalled */
5980 case NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
5981 case NFS4ERR_BADLAYOUT: /* no layout */
5982 case NFS4ERR_GRACE: /* loca_recalim always false */
5983 task->tk_status = 0;
5984 }
5985
5986 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005987 rpc_restart_call_prepare(task);
Andy Adamson863a3c62011-03-23 13:27:54 +00005988 return;
5989 }
5990
5991 if (task->tk_status == 0)
5992 nfs_post_op_update_inode_force_wcc(data->args.inode,
5993 data->res.fattr);
5994}
5995
5996static void nfs4_layoutcommit_release(void *calldata)
5997{
5998 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04005999 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07006000 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00006001
Andy Adamsondb29c082011-07-30 20:52:38 -04006002 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006003 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04006004 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6005 list_del_init(&lseg->pls_lc_list);
6006 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6007 &lseg->pls_flags))
6008 put_lseg(lseg);
6009 }
Peng Tao92407e72011-10-23 20:21:17 -07006010
6011 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6012 smp_mb__after_clear_bit();
6013 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6014
Andy Adamson863a3c62011-03-23 13:27:54 +00006015 put_rpccred(data->cred);
6016 kfree(data);
6017}
6018
6019static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6020 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6021 .rpc_call_done = nfs4_layoutcommit_done,
6022 .rpc_release = nfs4_layoutcommit_release,
6023};
6024
6025int
Andy Adamsonef311532011-03-12 02:58:10 -05006026nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006027{
6028 struct rpc_message msg = {
6029 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6030 .rpc_argp = &data->args,
6031 .rpc_resp = &data->res,
6032 .rpc_cred = data->cred,
6033 };
6034 struct rpc_task_setup task_setup_data = {
6035 .task = &data->task,
6036 .rpc_client = NFS_CLIENT(data->args.inode),
6037 .rpc_message = &msg,
6038 .callback_ops = &nfs4_layoutcommit_ops,
6039 .callback_data = data,
6040 .flags = RPC_TASK_ASYNC,
6041 };
6042 struct rpc_task *task;
6043 int status = 0;
6044
6045 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6046 "lbw: %llu inode %lu\n",
6047 data->task.tk_pid, sync,
6048 data->args.lastbytewritten,
6049 data->args.inode->i_ino);
6050
Trond Myklebust9d12b212012-01-17 22:04:25 -05006051 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006052 task = rpc_run_task(&task_setup_data);
6053 if (IS_ERR(task))
6054 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006055 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006056 goto out;
6057 status = nfs4_wait_for_completion_rpc_task(task);
6058 if (status != 0)
6059 goto out;
6060 status = task->tk_status;
6061out:
6062 dprintk("%s: status %d\n", __func__, status);
6063 rpc_put_task(task);
6064 return status;
6065}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006066
6067static int
6068_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6069 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6070{
6071 struct nfs41_secinfo_no_name_args args = {
6072 .style = SECINFO_STYLE_CURRENT_FH,
6073 };
6074 struct nfs4_secinfo_res res = {
6075 .flavors = flavors,
6076 };
6077 struct rpc_message msg = {
6078 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6079 .rpc_argp = &args,
6080 .rpc_resp = &res,
6081 };
6082 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6083}
6084
6085static int
6086nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6087 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6088{
6089 struct nfs4_exception exception = { };
6090 int err;
6091 do {
6092 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6093 switch (err) {
6094 case 0:
6095 case -NFS4ERR_WRONGSEC:
6096 case -NFS4ERR_NOTSUPP:
6097 break;
6098 default:
6099 err = nfs4_handle_exception(server, err, &exception);
6100 }
6101 } while (exception.retry);
6102 return err;
6103}
6104
6105static int
6106nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6107 struct nfs_fsinfo *info)
6108{
6109 int err;
6110 struct page *page;
6111 rpc_authflavor_t flavor;
6112 struct nfs4_secinfo_flavors *flavors;
6113
6114 page = alloc_page(GFP_KERNEL);
6115 if (!page) {
6116 err = -ENOMEM;
6117 goto out;
6118 }
6119
6120 flavors = page_address(page);
6121 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6122
6123 /*
6124 * Fall back on "guess and check" method if
6125 * the server doesn't support SECINFO_NO_NAME
6126 */
6127 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6128 err = nfs4_find_root_sec(server, fhandle, info);
6129 goto out_freepage;
6130 }
6131 if (err)
6132 goto out_freepage;
6133
6134 flavor = nfs_find_best_sec(flavors);
6135 if (err == 0)
6136 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6137
6138out_freepage:
6139 put_page(page);
6140 if (err == -EACCES)
6141 return -EPERM;
6142out:
6143 return err;
6144}
Bryan Schumaker7d974792011-06-02 14:59:08 -04006145static int _nfs41_test_stateid(struct nfs_server *server, struct nfs4_state *state)
6146{
Bryan Schumaker7d974792011-06-02 14:59:08 -04006147 struct nfs41_test_stateid_args args = {
6148 .stateid = &state->stateid,
6149 };
6150 struct nfs41_test_stateid_res res;
6151 struct rpc_message msg = {
6152 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6153 .rpc_argp = &args,
6154 .rpc_resp = &res,
6155 };
Trond Myklebust9d12b212012-01-17 22:04:25 -05006156 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6157 return nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Bryan Schumaker7d974792011-06-02 14:59:08 -04006158}
6159
6160static int nfs41_test_stateid(struct nfs_server *server, struct nfs4_state *state)
6161{
6162 struct nfs4_exception exception = { };
6163 int err;
6164 do {
6165 err = nfs4_handle_exception(server,
6166 _nfs41_test_stateid(server, state),
6167 &exception);
6168 } while (exception.retry);
6169 return err;
6170}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006171
6172static int _nfs4_free_stateid(struct nfs_server *server, struct nfs4_state *state)
6173{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006174 struct nfs41_free_stateid_args args = {
6175 .stateid = &state->stateid,
6176 };
6177 struct nfs41_free_stateid_res res;
6178 struct rpc_message msg = {
6179 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6180 .rpc_argp = &args,
6181 .rpc_resp = &res,
6182 };
6183
Trond Myklebust9d12b212012-01-17 22:04:25 -05006184 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6185 return nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006186}
6187
6188static int nfs41_free_stateid(struct nfs_server *server, struct nfs4_state *state)
6189{
6190 struct nfs4_exception exception = { };
6191 int err;
6192 do {
6193 err = nfs4_handle_exception(server,
6194 _nfs4_free_stateid(server, state),
6195 &exception);
6196 } while (exception.retry);
6197 return err;
6198}
Andy Adamson557134a2009-04-01 09:21:53 -04006199#endif /* CONFIG_NFS_V4_1 */
6200
Andy Adamson591d71c2009-04-01 09:22:47 -04006201struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006202 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006203 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006204 .recover_open = nfs4_open_reclaim,
6205 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006206 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006207 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006208};
6209
Andy Adamson591d71c2009-04-01 09:22:47 -04006210#if defined(CONFIG_NFS_V4_1)
6211struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
6212 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6213 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6214 .recover_open = nfs4_open_reclaim,
6215 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006216 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006217 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006218 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04006219};
6220#endif /* CONFIG_NFS_V4_1 */
6221
6222struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006223 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006224 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006225 .recover_open = nfs4_open_expired,
6226 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006227 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006228 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006229};
6230
Andy Adamson591d71c2009-04-01 09:22:47 -04006231#if defined(CONFIG_NFS_V4_1)
6232struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
6233 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6234 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006235 .recover_open = nfs41_open_expired,
6236 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006237 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006238 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006239};
6240#endif /* CONFIG_NFS_V4_1 */
6241
Benny Halevy29fba382009-04-01 09:22:44 -04006242struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
6243 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006244 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006245 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006246};
6247
6248#if defined(CONFIG_NFS_V4_1)
6249struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
6250 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006251 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006252 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006253};
6254#endif
6255
Trond Myklebust97dc1352010-06-16 09:52:26 -04006256static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6257 .minor_version = 0,
6258 .call_sync = _nfs4_call_sync,
Trond Myklebuste047a102010-06-16 09:52:27 -04006259 .validate_stateid = nfs4_validate_delegation_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006260 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006261 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6262 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6263 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006264};
6265
6266#if defined(CONFIG_NFS_V4_1)
6267static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6268 .minor_version = 1,
6269 .call_sync = _nfs4_call_sync_session,
Trond Myklebuste047a102010-06-16 09:52:27 -04006270 .validate_stateid = nfs41_validate_delegation_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006271 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006272 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6273 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6274 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006275};
6276#endif
6277
Trond Myklebust97dc1352010-06-16 09:52:26 -04006278const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6279 [0] = &nfs_v4_0_minor_ops,
6280#if defined(CONFIG_NFS_V4_1)
6281 [1] = &nfs_v4_1_minor_ops,
6282#endif
6283};
6284
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006285static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006286 .permission = nfs_permission,
6287 .getattr = nfs_getattr,
6288 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006289 .getxattr = generic_getxattr,
6290 .setxattr = generic_setxattr,
6291 .listxattr = generic_listxattr,
6292 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006293};
6294
David Howells509de812006-08-22 20:06:11 -04006295const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006296 .version = 4, /* protocol version */
6297 .dentry_ops = &nfs4_dentry_operations,
6298 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006299 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006300 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006301 .getroot = nfs4_proc_get_root,
6302 .getattr = nfs4_proc_getattr,
6303 .setattr = nfs4_proc_setattr,
6304 .lookup = nfs4_proc_lookup,
6305 .access = nfs4_proc_access,
6306 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006307 .create = nfs4_proc_create,
6308 .remove = nfs4_proc_remove,
6309 .unlink_setup = nfs4_proc_unlink_setup,
6310 .unlink_done = nfs4_proc_unlink_done,
6311 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006312 .rename_setup = nfs4_proc_rename_setup,
6313 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006314 .link = nfs4_proc_link,
6315 .symlink = nfs4_proc_symlink,
6316 .mkdir = nfs4_proc_mkdir,
6317 .rmdir = nfs4_proc_remove,
6318 .readdir = nfs4_proc_readdir,
6319 .mknod = nfs4_proc_mknod,
6320 .statfs = nfs4_proc_statfs,
6321 .fsinfo = nfs4_proc_fsinfo,
6322 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006323 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006324 .decode_dirent = nfs4_decode_dirent,
6325 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05006326 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006327 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006328 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006329 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006330 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006331 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006332 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006333 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006334 .open_context = nfs4_atomic_open,
Andy Adamson45a52a02011-03-01 01:34:08 +00006335 .init_client = nfs4_init_client,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006336 .secinfo = nfs4_proc_secinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006337};
6338
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006339static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6340 .prefix = XATTR_NAME_NFSV4_ACL,
6341 .list = nfs4_xattr_list_nfs4_acl,
6342 .get = nfs4_xattr_get_nfs4_acl,
6343 .set = nfs4_xattr_set_nfs4_acl,
6344};
6345
6346const struct xattr_handler *nfs4_xattr_handlers[] = {
6347 &nfs4_xattr_nfs4_acl_handler,
6348 NULL
6349};
6350
Linus Torvalds1da177e2005-04-16 15:20:36 -07006351/*
6352 * Local variables:
6353 * c-basic-offset: 8
6354 * End:
6355 */