blob: 360240cc1e9b39ecfd6262a6fa00befef67cf5d8 [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
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800388/*
Andy Adamson42acd022011-01-06 02:04:34 +0000389 * Signal state manager thread if session fore channel is drained
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800390 */
Andy Adamson42acd022011-01-06 02:04:34 +0000391static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800392{
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800393 struct rpc_task *task;
394
Trond Myklebusta2118c32010-06-16 09:52:26 -0400395 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800396 task = rpc_wake_up_next(&ses->fc_slot_table.slot_tbl_waitq);
397 if (task)
398 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800399 return;
400 }
401
402 if (ses->fc_slot_table.highest_used_slotid != -1)
403 return;
404
Andy Adamson42acd022011-01-06 02:04:34 +0000405 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
406 complete(&ses->fc_slot_table.complete);
407}
408
409/*
410 * Signal state manager thread if session back channel is drained
411 */
412void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
413{
414 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
415 ses->bc_slot_table.highest_used_slotid != -1)
416 return;
417 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
418 complete(&ses->bc_slot_table.complete);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800419}
420
Trond Myklebustd185a332010-06-16 09:52:25 -0400421static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400422{
423 struct nfs4_slot_table *tbl;
424
Trond Myklebustd185a332010-06-16 09:52:25 -0400425 tbl = &res->sr_session->fc_slot_table;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400426 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400427 /* just wake up the next guy waiting since
428 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500429 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500430 return;
Andy Adamson13615872009-04-01 09:22:17 -0400431 }
Andy Adamsonea028ac2009-12-04 15:55:38 -0500432
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500433 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonaabd0b42011-11-09 13:58:22 -0500434 nfs4_free_slot(tbl, res->sr_slot - tbl->slots);
Andy Adamson42acd022011-01-06 02:04:34 +0000435 nfs4_check_drain_fc_complete(res->sr_session);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500436 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400437 res->sr_slot = NULL;
Andy Adamson13615872009-04-01 09:22:17 -0400438}
439
Trond Myklebust14516c32010-07-31 14:29:06 -0400440static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400441{
442 unsigned long timestamp;
Trond Myklebust14516c32010-07-31 14:29:06 -0400443 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400444
445 /*
446 * sr_status remains 1 if an RPC level error occurred. The server
447 * may or may not have processed the sequence operation..
448 * Proceed as if the server received and processed the sequence
449 * operation.
450 */
451 if (res->sr_status == 1)
452 res->sr_status = NFS_OK;
453
Bryan Schumaker468f8612011-04-18 15:57:32 -0400454 /* don't increment the sequence number if the task wasn't sent */
455 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400456 goto out;
457
Andy Adamson691daf32009-12-04 15:55:39 -0500458 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400459 switch (res->sr_status) {
460 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400461 /* Update the slot's sequence and clientid lease timer */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400462 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400463 timestamp = res->sr_renewal_time;
Trond Myklebust14516c32010-07-31 14:29:06 -0400464 clp = res->sr_session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400465 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500466 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500467 if (res->sr_status_flags != 0)
468 nfs4_schedule_lease_recovery(clp);
Trond Myklebust14516c32010-07-31 14:29:06 -0400469 break;
470 case -NFS4ERR_DELAY:
471 /* The server detected a resend of the RPC call and
472 * returned NFS4ERR_DELAY as per Section 2.10.6.2
473 * of RFC5661.
474 */
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200475 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400476 __func__,
477 res->sr_slot - res->sr_session->fc_slot_table.slots,
478 res->sr_slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400479 goto out_retry;
480 default:
481 /* Just update the slot sequence no. */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400482 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400483 }
484out:
485 /* The session may be reset by one of the error handlers. */
486 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400487 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400488 return 1;
489out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400490 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400491 goto out;
492 rpc_delay(task, NFS4_POLL_RETRY_MAX);
493 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400494}
495
Trond Myklebust14516c32010-07-31 14:29:06 -0400496static int nfs4_sequence_done(struct rpc_task *task,
497 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400498{
Trond Myklebust14516c32010-07-31 14:29:06 -0400499 if (res->sr_session == NULL)
500 return 1;
501 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400502}
503
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400504/*
Benny Halevy510b8172009-04-01 09:22:14 -0400505 * nfs4_find_slot - efficiently look for a free slot
506 *
507 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
508 * If found, we mark the slot as used, update the highest_used_slotid,
509 * and respectively set up the sequence operation args.
510 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400511 *
512 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400513 */
514static u8
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800515nfs4_find_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400516{
517 int slotid;
518 u8 ret_id = NFS4_MAX_SLOT_TABLE;
519 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
520
Benny Halevy510b8172009-04-01 09:22:14 -0400521 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
522 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
523 tbl->max_slots);
524 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
525 if (slotid >= tbl->max_slots)
526 goto out;
527 __set_bit(slotid, tbl->used_slots);
528 if (slotid > tbl->highest_used_slotid)
529 tbl->highest_used_slotid = slotid;
530 ret_id = slotid;
531out:
532 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
533 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400534 return ret_id;
535}
536
Trond Myklebust9d12b212012-01-17 22:04:25 -0500537static void nfs41_init_sequence(struct nfs4_sequence_args *args,
538 struct nfs4_sequence_res *res, int cache_reply)
539{
540 args->sa_session = NULL;
541 args->sa_cache_this = 0;
542 if (cache_reply)
543 args->sa_cache_this = 1;
544 res->sr_session = NULL;
545 res->sr_slot = NULL;
546}
547
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000548int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400549 struct nfs4_sequence_args *args,
550 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400551 struct rpc_task *task)
552{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400553 struct nfs4_slot *slot;
554 struct nfs4_slot_table *tbl;
555 u8 slotid;
556
557 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400558 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400559 if (res->sr_slot != NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400560 return 0;
561
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400562 tbl = &session->fc_slot_table;
563
564 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400565 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800566 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500567 /* The state manager will wait until the slot table is empty */
Andy Adamson05f0d232009-12-04 15:55:37 -0500568 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400569 spin_unlock(&tbl->slot_tbl_lock);
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500570 dprintk("%s session is draining\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500571 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400572 }
573
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800574 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
575 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
576 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
577 spin_unlock(&tbl->slot_tbl_lock);
578 dprintk("%s enforce FIFO order\n", __func__);
579 return -EAGAIN;
580 }
581
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800582 slotid = nfs4_find_slot(tbl);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400583 if (slotid == NFS4_MAX_SLOT_TABLE) {
584 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
585 spin_unlock(&tbl->slot_tbl_lock);
586 dprintk("<-- %s: no free slots\n", __func__);
587 return -EAGAIN;
588 }
589 spin_unlock(&tbl->slot_tbl_lock);
590
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800591 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400592 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400593 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400594 args->sa_slotid = slotid;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400595
596 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
597
Benny Halevy99fe60d2009-04-01 09:22:29 -0400598 res->sr_session = session;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400599 res->sr_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400600 res->sr_renewal_time = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400601 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400602 /*
603 * sr_status is only set in decode_sequence, and so will remain
604 * set to 1 if an rpc level failure occurs.
605 */
606 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400607 return 0;
608}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000609EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400610
Trond Myklebust035168a2010-06-16 09:52:26 -0400611int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400612 struct nfs4_sequence_args *args,
613 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400614 struct rpc_task *task)
615{
Trond Myklebust035168a2010-06-16 09:52:26 -0400616 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400617 int ret = 0;
618
Trond Myklebust9d12b212012-01-17 22:04:25 -0500619 if (session == NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400620 goto out;
Trond Myklebust035168a2010-06-16 09:52:26 -0400621
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200622 dprintk("--> %s clp %p session %p sr_slot %td\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400623 __func__, session->clp, session, res->sr_slot ?
624 res->sr_slot - session->fc_slot_table.slots : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400625
Trond Myklebust9d12b212012-01-17 22:04:25 -0500626 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400627out:
628 dprintk("<-- %s status=%d\n", __func__, ret);
629 return ret;
630}
631
632struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400633 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400634 struct nfs4_sequence_args *seq_args;
635 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400636};
637
638static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
639{
640 struct nfs41_call_sync_data *data = calldata;
641
Trond Myklebust035168a2010-06-16 09:52:26 -0400642 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
643
644 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -0500645 data->seq_res, task))
Andy Adamsonce5039c2009-04-01 09:22:13 -0400646 return;
647 rpc_call_start(task);
648}
649
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800650static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
651{
652 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
653 nfs41_call_sync_prepare(task, calldata);
654}
655
Andy Adamson69ab40c2009-04-01 09:22:19 -0400656static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
657{
658 struct nfs41_call_sync_data *data = calldata;
659
Trond Myklebust14516c32010-07-31 14:29:06 -0400660 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400661}
662
Andy Adamsonce5039c2009-04-01 09:22:13 -0400663struct rpc_call_ops nfs41_call_sync_ops = {
664 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400665 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400666};
667
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800668struct rpc_call_ops nfs41_call_priv_sync_ops = {
669 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
670 .rpc_call_done = nfs41_call_sync_done,
671};
672
Bryan Schumaker7c513052011-03-24 17:12:24 +0000673static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
674 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400675 struct rpc_message *msg,
676 struct nfs4_sequence_args *args,
677 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800678 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400679{
680 int ret;
681 struct rpc_task *task;
682 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400683 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400684 .seq_args = args,
685 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400686 };
687 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000688 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400689 .rpc_message = msg,
690 .callback_ops = &nfs41_call_sync_ops,
691 .callback_data = &data
692 };
693
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800694 if (privileged)
695 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400696 task = rpc_run_task(&task_setup);
697 if (IS_ERR(task))
698 ret = PTR_ERR(task);
699 else {
700 ret = task->tk_status;
701 rpc_put_task(task);
702 }
703 return ret;
704}
705
Bryan Schumaker7c513052011-03-24 17:12:24 +0000706int _nfs4_call_sync_session(struct rpc_clnt *clnt,
707 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400708 struct rpc_message *msg,
709 struct nfs4_sequence_args *args,
710 struct nfs4_sequence_res *res,
711 int cache_reply)
712{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500713 nfs41_init_sequence(args, res, cache_reply);
714 return nfs4_call_sync_sequence(clnt, server, msg, args, res, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400715}
716
Trond Myklebustdf896452010-06-16 09:52:26 -0400717#else
Trond Myklebust9d12b212012-01-17 22:04:25 -0500718static inline
719void nfs41_init_sequence(struct nfs4_sequence_args *args,
720 struct nfs4_sequence_res *res, int cache_reply)
721{
722}
723
Trond Myklebust14516c32010-07-31 14:29:06 -0400724static int nfs4_sequence_done(struct rpc_task *task,
725 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400726{
Trond Myklebust14516c32010-07-31 14:29:06 -0400727 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400728}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400729#endif /* CONFIG_NFS_V4_1 */
730
Bryan Schumaker7c513052011-03-24 17:12:24 +0000731int _nfs4_call_sync(struct rpc_clnt *clnt,
732 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400733 struct rpc_message *msg,
734 struct nfs4_sequence_args *args,
735 struct nfs4_sequence_res *res,
736 int cache_reply)
737{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500738 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000739 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400740}
741
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000742static inline
Bryan Schumaker7c513052011-03-24 17:12:24 +0000743int nfs4_call_sync(struct rpc_clnt *clnt,
744 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000745 struct rpc_message *msg,
746 struct nfs4_sequence_args *args,
747 struct nfs4_sequence_res *res,
748 int cache_reply)
749{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000750 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
751 args, res, cache_reply);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000752}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400753
Trond Myklebust38478b22006-05-25 01:40:57 -0400754static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755{
Trond Myklebust38478b22006-05-25 01:40:57 -0400756 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Trond Myklebust38478b22006-05-25 01:40:57 -0400758 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400759 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700760 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400761 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700762 dir->i_version = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400763 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764}
765
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100766struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400767 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100768 struct nfs_openargs o_arg;
769 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100770 struct nfs_open_confirmargs c_arg;
771 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500772 struct nfs4_string owner_name;
773 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100774 struct nfs_fattr f_attr;
775 struct nfs_fattr dir_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100776 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400777 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100778 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400779 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100780 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100781 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400782 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100783 int rpc_status;
784 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100785};
786
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400787
788static void nfs4_init_opendata_res(struct nfs4_opendata *p)
789{
790 p->o_res.f_attr = &p->f_attr;
791 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400792 p->o_res.seqid = p->o_arg.seqid;
793 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400794 p->o_res.server = p->o_arg.server;
795 nfs_fattr_init(&p->f_attr);
796 nfs_fattr_init(&p->dir_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500797 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400798}
799
Al Viro82a2c1b2011-06-22 18:30:55 -0400800static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500801 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400802 const struct iattr *attrs,
803 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100804{
Al Viro82a2c1b2011-06-22 18:30:55 -0400805 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100806 struct inode *dir = parent->d_inode;
807 struct nfs_server *server = NFS_SERVER(dir);
808 struct nfs4_opendata *p;
809
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400810 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100811 if (p == NULL)
812 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400813 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100814 if (p->o_arg.seqid == NULL)
815 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400816 nfs_sb_active(dentry->d_sb);
817 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100818 p->dir = parent;
819 p->owner = sp;
820 atomic_inc(&sp->so_count);
821 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500822 p->o_arg.open_flags = flags;
823 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400824 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -0500825 p->o_arg.id = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400826 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100827 p->o_arg.server = server;
828 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500829 p->o_arg.dir_bitmask = server->cache_consistency_bitmask;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100830 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust536e43d2012-01-17 22:04:26 -0500831 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400832 u32 *s;
833
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100834 p->o_arg.u.attrs = &p->attrs;
835 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400836 s = (u32 *) p->o_arg.u.verifier.data;
837 s[0] = jiffies;
838 s[1] = current->pid;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100839 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100840 p->c_arg.fh = &p->o_res.fh;
841 p->c_arg.stateid = &p->o_res.stateid;
842 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400843 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400844 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100845 return p;
846err_free:
847 kfree(p);
848err:
849 dput(parent);
850 return NULL;
851}
852
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400853static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100854{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400855 struct nfs4_opendata *p = container_of(kref,
856 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400857 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400858
859 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400860 if (p->state != NULL)
861 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400862 nfs4_put_state_owner(p->owner);
863 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400864 dput(p->dentry);
865 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500866 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400867 kfree(p);
868}
869
870static void nfs4_opendata_put(struct nfs4_opendata *p)
871{
872 if (p != NULL)
873 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100874}
875
Trond Myklebust06f814a2006-01-03 09:55:07 +0100876static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
877{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100878 int ret;
879
Trond Myklebust06f814a2006-01-03 09:55:07 +0100880 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100881 return ret;
882}
883
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500884static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400885{
886 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500887
Trond Myklebust536e43d2012-01-17 22:04:26 -0500888 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500889 goto out;
890 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400891 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500892 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
893 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400894 break;
895 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500896 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
897 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400898 break;
899 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500900 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
901 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400902 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500903out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400904 return ret;
905}
906
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500907static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400908{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500909 if (delegation == NULL)
910 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500911 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400912 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500913 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400914 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500915 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400916 return 1;
917}
918
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500919static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100920{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500921 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100922 case FMODE_WRITE:
923 state->n_wronly++;
924 break;
925 case FMODE_READ:
926 state->n_rdonly++;
927 break;
928 case FMODE_READ|FMODE_WRITE:
929 state->n_rdwr++;
930 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500931 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100932}
933
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500934static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400935{
936 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
937 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
938 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500939 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400940 case FMODE_READ:
941 set_bit(NFS_O_RDONLY_STATE, &state->flags);
942 break;
943 case FMODE_WRITE:
944 set_bit(NFS_O_WRONLY_STATE, &state->flags);
945 break;
946 case FMODE_READ|FMODE_WRITE:
947 set_bit(NFS_O_RDWR_STATE, &state->flags);
948 }
949}
950
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500951static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400952{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400953 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500954 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400955 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400956}
957
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500958static 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 -0700959{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400960 /*
961 * Protect the call to nfs4_state_set_mode_locked and
962 * serialise the stateid update
963 */
964 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400965 if (deleg_stateid != NULL) {
966 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
967 set_bit(NFS_DELEGATED_STATE, &state->flags);
968 }
969 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500970 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400971 write_sequnlock(&state->seqlock);
972 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500973 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700974 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975}
976
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500977static 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 -0500978{
979 struct nfs_inode *nfsi = NFS_I(state->inode);
980 struct nfs_delegation *deleg_cur;
981 int ret = 0;
982
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500983 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500984
985 rcu_read_lock();
986 deleg_cur = rcu_dereference(nfsi->delegation);
987 if (deleg_cur == NULL)
988 goto no_delegation;
989
990 spin_lock(&deleg_cur->lock);
991 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500992 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500993 goto no_delegation_unlock;
994
995 if (delegation == NULL)
996 delegation = &deleg_cur->stateid;
997 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
998 goto no_delegation_unlock;
999
Trond Myklebustb7391f42008-12-23 15:21:52 -05001000 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001001 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001002 ret = 1;
1003no_delegation_unlock:
1004 spin_unlock(&deleg_cur->lock);
1005no_delegation:
1006 rcu_read_unlock();
1007
1008 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001009 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001010 ret = 1;
1011 }
1012
1013 return ret;
1014}
1015
1016
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001017static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001018{
1019 struct nfs_delegation *delegation;
1020
1021 rcu_read_lock();
1022 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001023 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001024 rcu_read_unlock();
1025 return;
1026 }
1027 rcu_read_unlock();
1028 nfs_inode_return_delegation(inode);
1029}
1030
Trond Myklebust6ee41262007-07-08 14:11:36 -04001031static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001032{
1033 struct nfs4_state *state = opendata->state;
1034 struct nfs_inode *nfsi = NFS_I(state->inode);
1035 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -05001036 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001037 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001038 nfs4_stateid stateid;
1039 int ret = -EAGAIN;
1040
Trond Myklebustaac00a82007-07-05 19:02:21 -04001041 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001042 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001043 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001044 if (can_open_cached(state, fmode, open_mode)) {
1045 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001046 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001047 goto out_return_state;
1048 }
1049 spin_unlock(&state->owner->so_lock);
1050 }
Trond Myklebust34310432008-12-23 15:21:38 -05001051 rcu_read_lock();
1052 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001053 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001054 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001055 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001056 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001057 /* Save the delegation */
1058 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
1059 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001060 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001061 if (ret != 0)
1062 goto out;
1063 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001064
1065 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001066 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001067 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001068 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001069out:
1070 return ERR_PTR(ret);
1071out_return_state:
1072 atomic_inc(&state->count);
1073 return state;
1074}
1075
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001076static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1077{
1078 struct inode *inode;
1079 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -04001080 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001081 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001082
Trond Myklebustaac00a82007-07-05 19:02:21 -04001083 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001084 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001085 goto out;
1086 }
1087
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001088 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001089 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001090 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001091 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001092 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001093 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001094 goto err;
1095 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001096 state = nfs4_get_open_state(inode, data->owner);
1097 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001098 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001099 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001100 int delegation_flags = 0;
1101
Trond Myklebust003707c2007-07-05 18:07:55 -04001102 rcu_read_lock();
1103 delegation = rcu_dereference(NFS_I(inode)->delegation);
1104 if (delegation)
1105 delegation_flags = delegation->flags;
1106 rcu_read_unlock();
Trond Myklebust652f89f2011-12-09 19:05:58 -05001107 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1108 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1109 "returning a delegation for "
1110 "OPEN(CLAIM_DELEGATE_CUR)\n",
1111 NFS_CLIENT(inode)->cl_server);
1112 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001113 nfs_inode_set_delegation(state->inode,
1114 data->owner->so_cred,
1115 &data->o_res);
1116 else
1117 nfs_inode_reclaim_delegation(state->inode,
1118 data->owner->so_cred,
1119 &data->o_res);
1120 }
Trond Myklebust34310432008-12-23 15:21:38 -05001121
1122 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001123 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001124 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001125out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001126 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001127err_put_inode:
1128 iput(inode);
1129err:
1130 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001131}
1132
Trond Myklebust864472e2006-01-03 09:55:15 +01001133static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1134{
1135 struct nfs_inode *nfsi = NFS_I(state->inode);
1136 struct nfs_open_context *ctx;
1137
1138 spin_lock(&state->inode->i_lock);
1139 list_for_each_entry(ctx, &nfsi->open_files, list) {
1140 if (ctx->state != state)
1141 continue;
1142 get_nfs_open_context(ctx);
1143 spin_unlock(&state->inode->i_lock);
1144 return ctx;
1145 }
1146 spin_unlock(&state->inode->i_lock);
1147 return ERR_PTR(-ENOENT);
1148}
1149
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001150static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1151{
1152 struct nfs4_opendata *opendata;
1153
Al Viro3d4ff432011-06-22 18:40:12 -04001154 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001155 if (opendata == NULL)
1156 return ERR_PTR(-ENOMEM);
1157 opendata->state = state;
1158 atomic_inc(&state->count);
1159 return opendata;
1160}
1161
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001162static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001163{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001164 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001165 int ret;
1166
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001167 opendata->o_arg.open_flags = 0;
1168 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001169 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1170 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1171 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001172 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001173 if (ret != 0)
1174 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001175 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001176 if (IS_ERR(newstate))
1177 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001178 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001179 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001180 return 0;
1181}
1182
1183static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1184{
Trond Myklebust864472e2006-01-03 09:55:15 +01001185 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001186 int ret;
1187
1188 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001189 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001190 smp_rmb();
1191 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001192 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001193 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001194 if (ret != 0)
1195 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001196 if (newstate != state)
1197 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001198 }
1199 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001200 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001201 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001202 if (ret != 0)
1203 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001204 if (newstate != state)
1205 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001206 }
1207 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001208 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001209 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001210 if (ret != 0)
1211 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001212 if (newstate != state)
1213 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001214 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001215 /*
1216 * We may have performed cached opens for all three recoveries.
1217 * Check if we need to update the current stateid.
1218 */
1219 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1220 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001221 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001222 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1223 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001224 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001225 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001226 return 0;
1227}
1228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229/*
1230 * OPEN_RECLAIM:
1231 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001233static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001235 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001236 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001237 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 int status;
1239
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001240 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1241 if (IS_ERR(opendata))
1242 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001243 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1244 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001245 rcu_read_lock();
1246 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001247 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001248 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001249 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001250 opendata->o_arg.u.delegation_type = delegation_type;
1251 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001252 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 return status;
1254}
1255
Trond Myklebust539cd032007-06-05 11:46:42 -04001256static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257{
1258 struct nfs_server *server = NFS_SERVER(state->inode);
1259 struct nfs4_exception exception = { };
1260 int err;
1261 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001262 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001263 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001264 break;
1265 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 } while (exception.retry);
1267 return err;
1268}
1269
Trond Myklebust864472e2006-01-03 09:55:15 +01001270static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1271{
1272 struct nfs_open_context *ctx;
1273 int ret;
1274
1275 ctx = nfs4_state_find_open_context(state);
1276 if (IS_ERR(ctx))
1277 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001278 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001279 put_nfs_open_context(ctx);
1280 return ret;
1281}
1282
Trond Myklebust13437e12007-07-06 15:10:43 -04001283static 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 -07001284{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001285 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001286 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001288 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1289 if (IS_ERR(opendata))
1290 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001291 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -04001292 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001293 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +01001294 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001295 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001296 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297}
1298
Trond Myklebust13437e12007-07-06 15:10:43 -04001299int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300{
1301 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001302 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 int err;
1304 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001305 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 switch (err) {
1307 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001308 case -ENOENT:
1309 case -ESTALE:
1310 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001311 case -NFS4ERR_BADSESSION:
1312 case -NFS4ERR_BADSLOT:
1313 case -NFS4ERR_BAD_HIGH_SLOT:
1314 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1315 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001316 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001317 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 case -NFS4ERR_STALE_CLIENTID:
1319 case -NFS4ERR_STALE_STATEID:
1320 case -NFS4ERR_EXPIRED:
1321 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001322 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001323 goto out;
1324 case -ERESTARTSYS:
1325 /*
1326 * The show must go on: exit, but mark the
1327 * stateid as needing recovery.
1328 */
1329 case -NFS4ERR_ADMIN_REVOKED:
1330 case -NFS4ERR_BAD_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001331 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001332 case -EKEYEXPIRED:
1333 /*
1334 * User RPCSEC_GSS context has expired.
1335 * We cannot recover this stateid now, so
1336 * skip it and allow recovery thread to
1337 * proceed.
1338 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001339 case -ENOMEM:
1340 err = 0;
1341 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 }
1343 err = nfs4_handle_exception(server, err, &exception);
1344 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001345out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 return err;
1347}
1348
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001349static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1350{
1351 struct nfs4_opendata *data = calldata;
1352
1353 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001354 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001355 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1356 sizeof(data->o_res.stateid.data));
Trond Myklebustbb226292008-01-02 15:19:18 -05001357 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001358 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001359 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001360 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001361}
1362
1363static void nfs4_open_confirm_release(void *calldata)
1364{
1365 struct nfs4_opendata *data = calldata;
1366 struct nfs4_state *state = NULL;
1367
1368 /* If this request hasn't been cancelled, do nothing */
1369 if (data->cancelled == 0)
1370 goto out_free;
1371 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001372 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001373 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001374 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001375 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001376 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001377out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001378 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001379}
1380
1381static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001382 .rpc_call_done = nfs4_open_confirm_done,
1383 .rpc_release = nfs4_open_confirm_release,
1384};
1385
1386/*
1387 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1388 */
1389static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1390{
1391 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1392 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001393 struct rpc_message msg = {
1394 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1395 .rpc_argp = &data->c_arg,
1396 .rpc_resp = &data->c_res,
1397 .rpc_cred = data->owner->so_cred,
1398 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001399 struct rpc_task_setup task_setup_data = {
1400 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001401 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001402 .callback_ops = &nfs4_open_confirm_ops,
1403 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001404 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001405 .flags = RPC_TASK_ASYNC,
1406 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 int status;
1408
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001409 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001410 data->rpc_done = 0;
1411 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001412 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001413 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001414 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001415 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001416 status = nfs4_wait_for_completion_rpc_task(task);
1417 if (status != 0) {
1418 data->cancelled = 1;
1419 smp_wmb();
1420 } else
1421 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001422 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 return status;
1424}
1425
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001426static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001428 struct nfs4_opendata *data = calldata;
1429 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001430
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001431 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1432 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001433 /*
1434 * Check if we still need to send an OPEN call, or if we can use
1435 * a delegation instead.
1436 */
1437 if (data->state != NULL) {
1438 struct nfs_delegation *delegation;
1439
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001440 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001441 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001442 rcu_read_lock();
1443 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001444 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1445 can_open_delegated(delegation, data->o_arg.fmode))
1446 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001447 rcu_read_unlock();
1448 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001449 /* Update sequence id. */
Trond Myklebust48c22eb2012-01-17 22:04:25 -05001450 data->o_arg.id = sp->so_seqid.owner_id;
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001451 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001452 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001453 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001454 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1455 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001456 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001457 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001458 &data->o_arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05001459 &data->o_res.seq_res, task))
Andy Adamsond8985282009-04-01 09:22:21 -04001460 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001461 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001462 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001463unlock_no_action:
1464 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001465out_no_action:
1466 task->tk_action = NULL;
1467
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001468}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001470static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1471{
1472 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1473 nfs4_open_prepare(task, calldata);
1474}
1475
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001476static void nfs4_open_done(struct rpc_task *task, void *calldata)
1477{
1478 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001480 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001481
Trond Myklebust14516c32010-07-31 14:29:06 -04001482 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1483 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001484
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001485 if (task->tk_status == 0) {
1486 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001487 case S_IFREG:
1488 break;
1489 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001490 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001491 break;
1492 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001493 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001494 break;
1495 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001496 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001497 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001498 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001499 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1500 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001501 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001502 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001503}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001504
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001505static void nfs4_open_release(void *calldata)
1506{
1507 struct nfs4_opendata *data = calldata;
1508 struct nfs4_state *state = NULL;
1509
1510 /* If this request hasn't been cancelled, do nothing */
1511 if (data->cancelled == 0)
1512 goto out_free;
1513 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001514 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001515 goto out_free;
1516 /* In case we need an open_confirm, no cleanup! */
1517 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1518 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001519 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001520 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001521 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001522out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001523 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001524}
1525
1526static const struct rpc_call_ops nfs4_open_ops = {
1527 .rpc_call_prepare = nfs4_open_prepare,
1528 .rpc_call_done = nfs4_open_done,
1529 .rpc_release = nfs4_open_release,
1530};
1531
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001532static const struct rpc_call_ops nfs4_recover_open_ops = {
1533 .rpc_call_prepare = nfs4_recover_open_prepare,
1534 .rpc_call_done = nfs4_open_done,
1535 .rpc_release = nfs4_open_release,
1536};
1537
1538static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001539{
1540 struct inode *dir = data->dir->d_inode;
1541 struct nfs_server *server = NFS_SERVER(dir);
1542 struct nfs_openargs *o_arg = &data->o_arg;
1543 struct nfs_openres *o_res = &data->o_res;
1544 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001545 struct rpc_message msg = {
1546 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1547 .rpc_argp = o_arg,
1548 .rpc_resp = o_res,
1549 .rpc_cred = data->owner->so_cred,
1550 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001551 struct rpc_task_setup task_setup_data = {
1552 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001553 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001554 .callback_ops = &nfs4_open_ops,
1555 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001556 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001557 .flags = RPC_TASK_ASYNC,
1558 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001559 int status;
1560
Trond Myklebust9d12b212012-01-17 22:04:25 -05001561 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001562 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001563 data->rpc_done = 0;
1564 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001565 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001566 if (isrecover)
1567 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001568 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001569 if (IS_ERR(task))
1570 return PTR_ERR(task);
1571 status = nfs4_wait_for_completion_rpc_task(task);
1572 if (status != 0) {
1573 data->cancelled = 1;
1574 smp_wmb();
1575 } else
1576 status = data->rpc_status;
1577 rpc_put_task(task);
1578
1579 return status;
1580}
1581
1582static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1583{
1584 struct inode *dir = data->dir->d_inode;
1585 struct nfs_openres *o_res = &data->o_res;
1586 int status;
1587
1588 status = nfs4_run_open_task(data, 1);
1589 if (status != 0 || !data->rpc_done)
1590 return status;
1591
Trond Myklebust6926afd2012-01-07 13:22:46 -05001592 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1593
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001594 nfs_refresh_inode(dir, o_res->dir_attr);
1595
1596 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1597 status = _nfs4_proc_open_confirm(data);
1598 if (status != 0)
1599 return status;
1600 }
1601
1602 return status;
1603}
1604
1605/*
1606 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1607 */
1608static int _nfs4_proc_open(struct nfs4_opendata *data)
1609{
1610 struct inode *dir = data->dir->d_inode;
1611 struct nfs_server *server = NFS_SERVER(dir);
1612 struct nfs_openargs *o_arg = &data->o_arg;
1613 struct nfs_openres *o_res = &data->o_res;
1614 int status;
1615
1616 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001617 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001618 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001619 if (status != 0) {
1620 if (status == -NFS4ERR_BADNAME &&
1621 !(o_arg->open_flags & O_CREAT))
1622 return -ENOENT;
1623 return status;
1624 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001625
Trond Myklebust6926afd2012-01-07 13:22:46 -05001626 nfs_fattr_map_and_free_names(server, &data->f_attr);
1627
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001628 if (o_arg->open_flags & O_CREAT) {
1629 update_changeattr(dir, &o_res->cinfo);
1630 nfs_post_op_update_inode(dir, o_res->dir_attr);
1631 } else
1632 nfs_refresh_inode(dir, o_res->dir_attr);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001633 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1634 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001636 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001638 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 }
1640 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001641 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001642 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643}
1644
Andy Adamsond83217c2011-03-01 01:34:17 +00001645static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +01001646{
Trond Myklebusta78cb572009-08-09 15:06:19 -04001647 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001648 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001649
Trond Myklebusta78cb572009-08-09 15:06:19 -04001650 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001651 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001652 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001653 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001654 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1655 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001656 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001657 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001658 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001659 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001660 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001661}
1662
Andy Adamsond83217c2011-03-01 01:34:17 +00001663static int nfs4_recover_expired_lease(struct nfs_server *server)
1664{
1665 return nfs4_client_recover_expired_lease(server->nfs_client);
1666}
1667
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668/*
1669 * OPEN_EXPIRED:
1670 * reclaim state on the server after a network partition.
1671 * Assumes caller holds the appropriate lock
1672 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001673static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001675 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001676 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001678 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1679 if (IS_ERR(opendata))
1680 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001681 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001682 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001683 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001684 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001685 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686}
1687
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001688static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001689{
Trond Myklebust539cd032007-06-05 11:46:42 -04001690 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001691 struct nfs4_exception exception = { };
1692 int err;
1693
1694 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001695 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001696 switch (err) {
1697 default:
1698 goto out;
1699 case -NFS4ERR_GRACE:
1700 case -NFS4ERR_DELAY:
1701 nfs4_handle_exception(server, err, &exception);
1702 err = 0;
1703 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001704 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001705out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001706 return err;
1707}
1708
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1710{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001712 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
Trond Myklebust864472e2006-01-03 09:55:15 +01001714 ctx = nfs4_state_find_open_context(state);
1715 if (IS_ERR(ctx))
1716 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001717 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001718 put_nfs_open_context(ctx);
1719 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720}
1721
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001722#if defined(CONFIG_NFS_V4_1)
1723static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1724{
1725 int status;
1726 struct nfs_server *server = NFS_SERVER(state->inode);
1727
1728 status = nfs41_test_stateid(server, state);
1729 if (status == NFS_OK)
1730 return 0;
1731 nfs41_free_stateid(server, state);
1732 return nfs4_open_expired(sp, state);
1733}
1734#endif
1735
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001737 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1738 * fields corresponding to attributes that were used to store the verifier.
1739 * Make sure we clobber those fields in the later setattr call
1740 */
1741static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1742{
1743 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1744 !(sattr->ia_valid & ATTR_ATIME_SET))
1745 sattr->ia_valid |= ATTR_ATIME;
1746
1747 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1748 !(sattr->ia_valid & ATTR_MTIME_SET))
1749 sattr->ia_valid |= ATTR_MTIME;
1750}
1751
1752/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001753 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 */
Al Viro82a2c1b2011-06-22 18:30:55 -04001755static 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 -07001756{
1757 struct nfs4_state_owner *sp;
1758 struct nfs4_state *state = NULL;
1759 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001760 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001761 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
1763 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001765 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1766 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1768 goto out_err;
1769 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001770 status = nfs4_recover_expired_lease(server);
1771 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001772 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001773 if (dentry->d_inode != NULL)
1774 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001775 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001776 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001777 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001778 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
Al Viro82a2c1b2011-06-22 18:30:55 -04001780 if (dentry->d_inode != NULL)
1781 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001782
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001783 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001785 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001787 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001788 status = PTR_ERR(state);
1789 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001790 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001791 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001792 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001793
1794 if (opendata->o_arg.open_flags & O_EXCL) {
1795 nfs4_exclusive_attrset(opendata, sattr);
1796
1797 nfs_fattr_init(opendata->o_res.f_attr);
1798 status = nfs4_do_setattr(state->inode, cred,
1799 opendata->o_res.f_attr, sattr,
1800 state);
1801 if (status == 0)
1802 nfs_setattr_update_inode(state->inode, sattr);
1803 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1804 }
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001805 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 *res = state;
1808 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001809err_opendata_put:
1810 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001811err_put_state_owner:
1812 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 *res = NULL;
1815 return status;
1816}
1817
1818
Al Viro82a2c1b2011-06-22 18:30:55 -04001819static 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 -07001820{
1821 struct nfs4_exception exception = { };
1822 struct nfs4_state *res;
1823 int status;
1824
1825 do {
Al Viro82a2c1b2011-06-22 18:30:55 -04001826 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 if (status == 0)
1828 break;
1829 /* NOTE: BAD_SEQID means the server and client disagree about the
1830 * book-keeping w.r.t. state-changing operations
1831 * (OPEN/CLOSE/LOCK/LOCKU...)
1832 * It is actually a sign of a bug on the client or on the server.
1833 *
1834 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001835 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 * have unhashed the old state_owner for us, and that we can
1837 * therefore safely retry using a new one. We should still warn
1838 * the user though...
1839 */
1840 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001841 printk(KERN_WARNING "NFS: v4 server %s "
1842 " returned a bad sequence-id error!\n",
1843 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 exception.retry = 1;
1845 continue;
1846 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001847 /*
1848 * BAD_STATEID on OPEN means that the server cancelled our
1849 * state before it received the OPEN_CONFIRM.
1850 * Recover by retrying the request as per the discussion
1851 * on Page 181 of RFC3530.
1852 */
1853 if (status == -NFS4ERR_BAD_STATEID) {
1854 exception.retry = 1;
1855 continue;
1856 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001857 if (status == -EAGAIN) {
1858 /* We must have found a delegation */
1859 exception.retry = 1;
1860 continue;
1861 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1863 status, &exception));
1864 } while (exception.retry);
1865 return res;
1866}
1867
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001868static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1869 struct nfs_fattr *fattr, struct iattr *sattr,
1870 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001872 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001874 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 .iap = sattr,
1876 .server = server,
1877 .bitmask = server->attr_bitmask,
1878 };
1879 struct nfs_setattrres res = {
1880 .fattr = fattr,
1881 .server = server,
1882 };
1883 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001884 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1885 .rpc_argp = &arg,
1886 .rpc_resp = &res,
1887 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001889 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001890 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
Trond Myklebust0e574af2005-10-27 22:12:38 -04001892 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001894 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1895 /* Use that stateid */
1896 } else if (state != NULL) {
Trond Myklebust77041ed2010-07-01 12:49:11 -04001897 nfs4_copy_stateid(&arg.stateid, state, current->files, current->tgid);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001898 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1900
Bryan Schumaker7c513052011-03-24 17:12:24 +00001901 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001902 if (status == 0 && state != NULL)
1903 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001904 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905}
1906
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001907static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1908 struct nfs_fattr *fattr, struct iattr *sattr,
1909 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001911 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 struct nfs4_exception exception = { };
1913 int err;
1914 do {
1915 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001916 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 &exception);
1918 } while (exception.retry);
1919 return err;
1920}
1921
1922struct nfs4_closedata {
1923 struct inode *inode;
1924 struct nfs4_state *state;
1925 struct nfs_closeargs arg;
1926 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001927 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001928 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00001929 bool roc;
1930 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931};
1932
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001933static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001934{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001935 struct nfs4_closedata *calldata = data;
1936 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04001937 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07001938
Fred Isamanf7e89172011-01-06 11:36:32 +00001939 if (calldata->roc)
1940 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07001941 nfs4_put_open_state(calldata->state);
1942 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001943 nfs4_put_state_owner(sp);
Al Viro643168c2011-06-22 18:20:23 -04001944 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07001945 kfree(calldata);
1946}
1947
Trond Myklebust88069f72009-12-08 08:33:16 -05001948static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1949 fmode_t fmode)
1950{
1951 spin_lock(&state->owner->so_lock);
1952 if (!(fmode & FMODE_READ))
1953 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1954 if (!(fmode & FMODE_WRITE))
1955 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1956 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1957 spin_unlock(&state->owner->so_lock);
1958}
1959
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001960static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001962 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 struct nfs_server *server = NFS_SERVER(calldata->inode);
1965
Trond Myklebust14516c32010-07-31 14:29:06 -04001966 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
1967 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 /* hmm. we are done with the inode, and in the process of freeing
1969 * the state_owner. we keep this around to process errors
1970 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 switch (task->tk_status) {
1972 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00001973 if (calldata->roc)
1974 pnfs_roc_set_barrier(state->inode,
1975 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04001976 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001977 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05001978 nfs4_close_clear_stateid_flags(state,
1979 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 break;
1981 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001982 case -NFS4ERR_OLD_STATEID:
1983 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001985 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001986 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05001988 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1989 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 }
Trond Myklebust72211db2009-12-15 14:47:36 -05001991 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04001992 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993}
1994
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001995static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001997 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001998 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05001999 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002000
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002001 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07002002 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04002003
Trond Myklebust88069f72009-12-08 08:33:16 -05002004 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2005 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002006 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002007 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002008 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002009 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002010 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2011 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2012 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002013 }
2014 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002015 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2016 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2017 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002018 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002019 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002020 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002021
2022 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002023 /* Note: exit _without_ calling nfs4_close_done */
2024 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07002025 return;
2026 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002027
Fred Isamanf7e89172011-01-06 11:36:32 +00002028 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002029 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002030 if (calldata->roc &&
2031 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
2032 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
2033 task, NULL);
2034 return;
2035 }
2036 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002037
Trond Myklebust516a6af2005-10-27 22:12:41 -04002038 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002039 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04002040 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002041 &calldata->arg.seq_args,
2042 &calldata->res.seq_res,
2043 task))
Andy Adamson19ddab02009-04-01 09:22:20 -04002044 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002045 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046}
2047
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002048static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002049 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002050 .rpc_call_done = nfs4_close_done,
2051 .rpc_release = nfs4_free_closedata,
2052};
2053
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054/*
2055 * It is possible for data to be read/written from a mem-mapped file
2056 * after the sys_close call (which hits the vfs layer as a flush).
2057 * This means that we can't safely call nfsv4 close on a file until
2058 * the inode is cleared. This in turn means that we are not good
2059 * NFSv4 citizens - we do not indicate to the server to update the file's
2060 * share state even when we are done with one of the three share
2061 * stateid's in the inode.
2062 *
2063 * NOTE: Caller must be holding the sp->so_owner semaphore!
2064 */
Al Viro643168c2011-06-22 18:20:23 -04002065int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002067 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002069 struct nfs4_state_owner *sp = state->owner;
2070 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002071 struct rpc_message msg = {
2072 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2073 .rpc_cred = state->owner->so_cred,
2074 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002075 struct rpc_task_setup task_setup_data = {
2076 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002077 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002078 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002079 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002080 .flags = RPC_TASK_ASYNC,
2081 };
Trond Myklebust95121352005-10-18 14:20:12 -07002082 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002084 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002086 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002087 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002088 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002090 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002091 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002093 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002094 if (calldata->arg.seqid == NULL)
2095 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002096 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002097 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002098 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002099 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002100 calldata->res.server = server;
Fred Isamanf7e89172011-01-06 11:36:32 +00002101 calldata->roc = roc;
Al Viro643168c2011-06-22 18:20:23 -04002102 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002103
Trond Myklebust1174dd12010-12-21 10:52:24 -05002104 msg.rpc_argp = &calldata->arg;
2105 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002106 task_setup_data.callback_data = calldata;
2107 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002108 if (IS_ERR(task))
2109 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002110 status = 0;
2111 if (wait)
2112 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002113 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002114 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002115out_free_calldata:
2116 kfree(calldata);
2117out:
Fred Isamanf7e89172011-01-06 11:36:32 +00002118 if (roc)
2119 pnfs_roc_release(state->inode);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002120 nfs4_put_open_state(state);
2121 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002122 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123}
2124
Trond Myklebust2b484292010-09-17 10:56:51 -04002125static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002126nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 struct nfs4_state *state;
2129
Trond Myklebust565277f2007-10-15 18:17:53 -04002130 /* Protect against concurrent sillydeletes */
Al Viro3d4ff432011-06-22 18:40:12 -04002131 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr, ctx->cred);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002132 if (IS_ERR(state))
2133 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002134 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002135 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136}
2137
Trond Myklebust1185a552009-12-03 15:54:02 -05002138static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002139{
2140 if (ctx->state == NULL)
2141 return;
2142 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002143 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002144 else
Al Viro643168c2011-06-22 18:20:23 -04002145 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002146}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
2148static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2149{
Benny Halevy43652ad2009-04-01 09:21:54 -04002150 struct nfs4_server_caps_arg args = {
2151 .fhandle = fhandle,
2152 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 struct nfs4_server_caps_res res = {};
2154 struct rpc_message msg = {
2155 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002156 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 .rpc_resp = &res,
2158 };
2159 int status;
2160
Bryan Schumaker7c513052011-03-24 17:12:24 +00002161 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 if (status == 0) {
2163 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002164 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2165 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2166 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2167 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2168 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2170 server->caps |= NFS_CAP_ACLS;
2171 if (res.has_links != 0)
2172 server->caps |= NFS_CAP_HARDLINKS;
2173 if (res.has_symlinks != 0)
2174 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002175 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2176 server->caps |= NFS_CAP_FILEID;
2177 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2178 server->caps |= NFS_CAP_MODE;
2179 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2180 server->caps |= NFS_CAP_NLINK;
2181 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2182 server->caps |= NFS_CAP_OWNER;
2183 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2184 server->caps |= NFS_CAP_OWNER_GROUP;
2185 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2186 server->caps |= NFS_CAP_ATIME;
2187 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2188 server->caps |= NFS_CAP_CTIME;
2189 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2190 server->caps |= NFS_CAP_MTIME;
2191
Trond Myklebusta65318b2009-03-11 14:10:28 -04002192 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2193 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2194 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 server->acl_bitmask = res.acl_bitmask;
2196 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002197
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 return status;
2199}
2200
Trond Myklebust55a97592006-06-09 09:34:19 -04002201int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202{
2203 struct nfs4_exception exception = { };
2204 int err;
2205 do {
2206 err = nfs4_handle_exception(server,
2207 _nfs4_server_capabilities(server, fhandle),
2208 &exception);
2209 } while (exception.retry);
2210 return err;
2211}
2212
2213static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2214 struct nfs_fsinfo *info)
2215{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 struct nfs4_lookup_root_arg args = {
2217 .bitmask = nfs4_fattr_bitmap,
2218 };
2219 struct nfs4_lookup_res res = {
2220 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002221 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 .fh = fhandle,
2223 };
2224 struct rpc_message msg = {
2225 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2226 .rpc_argp = &args,
2227 .rpc_resp = &res,
2228 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002229
Trond Myklebust0e574af2005-10-27 22:12:38 -04002230 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002231 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232}
2233
2234static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2235 struct nfs_fsinfo *info)
2236{
2237 struct nfs4_exception exception = { };
2238 int err;
2239 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002240 err = _nfs4_lookup_root(server, fhandle, info);
2241 switch (err) {
2242 case 0:
2243 case -NFS4ERR_WRONGSEC:
2244 break;
2245 default:
2246 err = nfs4_handle_exception(server, err, &exception);
2247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 } while (exception.retry);
2249 return err;
2250}
2251
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002252static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2253 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2254{
2255 struct rpc_auth *auth;
2256 int ret;
2257
2258 auth = rpcauth_create(flavor, server->client);
2259 if (!auth) {
2260 ret = -EIO;
2261 goto out;
2262 }
2263 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002264out:
2265 return ret;
2266}
2267
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002268static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002269 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002271 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002272 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002274 len = gss_mech_list_pseudoflavors(&flav_array[0]);
2275 flav_array[len] = RPC_AUTH_NULL;
2276 len += 1;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002277
2278 for (i = 0; i < len; i++) {
2279 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002280 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002281 continue;
2282 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002283 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002284 /*
2285 * -EACCESS could mean that the user doesn't have correct permissions
2286 * to access the mount. It could also mean that we tried to mount
2287 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2288 * existing mount programs don't handle -EACCES very well so it should
2289 * be mapped to -EPERM instead.
2290 */
2291 if (status == -EACCES)
2292 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002293 return status;
2294}
2295
2296/*
2297 * get the file handle for the "/" directory on the server
2298 */
2299static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2300 struct nfs_fsinfo *info)
2301{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002302 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002303 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002304 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2305 /*
2306 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2307 * by nfs4_map_errors() as this function exits.
2308 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002309 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 if (status == 0)
2311 status = nfs4_server_capabilities(server, fhandle);
2312 if (status == 0)
2313 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002314 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315}
2316
Andy Adamson533eb462011-06-13 18:25:56 -04002317static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002318/*
2319 * Get locations and (maybe) other attributes of a referral.
2320 * Note that we'll actually follow the referral later when
2321 * we detect fsid mismatch in inode revalidation
2322 */
Andy Adamson533eb462011-06-13 18:25:56 -04002323static int nfs4_get_referral(struct inode *dir, const struct qstr *name,
2324 struct nfs_fattr *fattr, struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002325{
2326 int status = -ENOMEM;
2327 struct page *page = NULL;
2328 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002329
2330 page = alloc_page(GFP_KERNEL);
2331 if (page == NULL)
2332 goto out;
2333 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2334 if (locations == NULL)
2335 goto out;
2336
Trond Myklebustc228fd32007-01-13 02:28:11 -05002337 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002338 if (status != 0)
2339 goto out;
2340 /* Make sure server returned a different fsid for the referral */
2341 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002342 dprintk("%s: server did not return a different fsid for"
2343 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002344 status = -EIO;
2345 goto out;
2346 }
Andy Adamson533eb462011-06-13 18:25:56 -04002347 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2348 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002349
Andy Adamson533eb462011-06-13 18:25:56 -04002350 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002351 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002352 memset(fhandle, 0, sizeof(struct nfs_fh));
2353out:
2354 if (page)
2355 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002356 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002357 return status;
2358}
2359
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2361{
2362 struct nfs4_getattr_arg args = {
2363 .fh = fhandle,
2364 .bitmask = server->attr_bitmask,
2365 };
2366 struct nfs4_getattr_res res = {
2367 .fattr = fattr,
2368 .server = server,
2369 };
2370 struct rpc_message msg = {
2371 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2372 .rpc_argp = &args,
2373 .rpc_resp = &res,
2374 };
2375
Trond Myklebust0e574af2005-10-27 22:12:38 -04002376 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002377 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378}
2379
2380static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2381{
2382 struct nfs4_exception exception = { };
2383 int err;
2384 do {
2385 err = nfs4_handle_exception(server,
2386 _nfs4_proc_getattr(server, fhandle, fattr),
2387 &exception);
2388 } while (exception.retry);
2389 return err;
2390}
2391
2392/*
2393 * The file is not closed if it is opened due to the a request to change
2394 * the size of the file. The open call will not be needed once the
2395 * VFS layer lookup-intents are implemented.
2396 *
2397 * Close is called when the inode is destroyed.
2398 * If we haven't opened the file for O_WRONLY, we
2399 * need to in the size_change case to obtain a stateid.
2400 *
2401 * Got race?
2402 * Because OPEN is always done by name in nfsv4, it is
2403 * possible that we opened a different file by the same
2404 * name. We can recognize this race condition, but we
2405 * can't do anything about it besides returning an error.
2406 *
2407 * This will be fixed with VFS changes (lookup-intent).
2408 */
2409static int
2410nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2411 struct iattr *sattr)
2412{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002413 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002414 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002415 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 int status;
2417
Benny Halevy8a1636c2010-07-14 15:43:57 -04002418 if (pnfs_ld_layoutret_on_setattr(inode))
2419 pnfs_return_layout(inode);
2420
Trond Myklebust0e574af2005-10-27 22:12:38 -04002421 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422
Trond Myklebustd5308382005-11-04 15:33:38 -05002423 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002424 if (sattr->ia_valid & ATTR_FILE) {
2425 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002426
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002427 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002428 if (ctx) {
2429 cred = ctx->cred;
2430 state = ctx->state;
2431 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002432 }
2433
Trond Myklebust536e43d2012-01-17 22:04:26 -05002434 /* Deal with open(O_TRUNC) */
2435 if (sattr->ia_valid & ATTR_OPEN)
2436 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2437
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002438 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002439 if (status == 0)
2440 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 return status;
2442}
2443
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002444static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2445 const struct qstr *name, struct nfs_fh *fhandle,
2446 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002447{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002448 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002449 int status;
2450 struct nfs4_lookup_arg args = {
2451 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002452 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002453 .name = name,
2454 };
2455 struct nfs4_lookup_res res = {
2456 .server = server,
2457 .fattr = fattr,
2458 .fh = fhandle,
2459 };
2460 struct rpc_message msg = {
2461 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2462 .rpc_argp = &args,
2463 .rpc_resp = &res,
2464 };
2465
2466 nfs_fattr_init(fattr);
2467
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002469 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 dprintk("NFS reply lookup: %d\n", status);
2471 return status;
2472}
2473
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002474void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr, struct nfs_fh *fh)
2475{
2476 memset(fh, 0, sizeof(struct nfs_fh));
2477 fattr->fsid.major = 1;
2478 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2479 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_FSID | NFS_ATTR_FATTR_MOUNTPOINT;
2480 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2481 fattr->nlink = 2;
2482}
2483
Bryan Schumaker7c513052011-03-24 17:12:24 +00002484static int nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir, struct qstr *name,
2485 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486{
2487 struct nfs4_exception exception = { };
2488 int err;
2489 do {
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002490 int status;
2491
2492 status = _nfs4_proc_lookup(clnt, dir, name, fhandle, fattr);
2493 switch (status) {
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002494 case -NFS4ERR_BADNAME:
2495 return -ENOENT;
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002496 case -NFS4ERR_MOVED:
Trond Myklebusta6f498a2011-11-07 15:50:32 -05002497 return nfs4_get_referral(dir, name, fattr, fhandle);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002498 case -NFS4ERR_WRONGSEC:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002499 nfs_fixup_secinfo_attributes(fattr, fhandle);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002500 }
2501 err = nfs4_handle_exception(NFS_SERVER(dir),
2502 status, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 } while (exception.retry);
2504 return err;
2505}
2506
2507static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2508{
Trond Myklebust76b32992007-08-10 17:45:11 -04002509 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 struct nfs4_accessargs args = {
2511 .fh = NFS_FH(inode),
Trond Myklebust76b32992007-08-10 17:45:11 -04002512 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002514 struct nfs4_accessres res = {
2515 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002516 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 struct rpc_message msg = {
2518 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2519 .rpc_argp = &args,
2520 .rpc_resp = &res,
2521 .rpc_cred = entry->cred,
2522 };
2523 int mode = entry->mask;
2524 int status;
2525
2526 /*
2527 * Determine which access bits we want to ask for...
2528 */
2529 if (mode & MAY_READ)
2530 args.access |= NFS4_ACCESS_READ;
2531 if (S_ISDIR(inode->i_mode)) {
2532 if (mode & MAY_WRITE)
2533 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2534 if (mode & MAY_EXEC)
2535 args.access |= NFS4_ACCESS_LOOKUP;
2536 } else {
2537 if (mode & MAY_WRITE)
2538 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2539 if (mode & MAY_EXEC)
2540 args.access |= NFS4_ACCESS_EXECUTE;
2541 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002542
2543 res.fattr = nfs_alloc_fattr();
2544 if (res.fattr == NULL)
2545 return -ENOMEM;
2546
Bryan Schumaker7c513052011-03-24 17:12:24 +00002547 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 if (!status) {
2549 entry->mask = 0;
2550 if (res.access & NFS4_ACCESS_READ)
2551 entry->mask |= MAY_READ;
2552 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2553 entry->mask |= MAY_WRITE;
2554 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2555 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002556 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002558 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 return status;
2560}
2561
2562static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2563{
2564 struct nfs4_exception exception = { };
2565 int err;
2566 do {
2567 err = nfs4_handle_exception(NFS_SERVER(inode),
2568 _nfs4_proc_access(inode, entry),
2569 &exception);
2570 } while (exception.retry);
2571 return err;
2572}
2573
2574/*
2575 * TODO: For the time being, we don't try to get any attributes
2576 * along with any of the zero-copy operations READ, READDIR,
2577 * READLINK, WRITE.
2578 *
2579 * In the case of the first three, we want to put the GETATTR
2580 * after the read-type operation -- this is because it is hard
2581 * to predict the length of a GETATTR response in v4, and thus
2582 * align the READ data correctly. This means that the GETATTR
2583 * may end up partially falling into the page cache, and we should
2584 * shift it into the 'tail' of the xdr_buf before processing.
2585 * To do this efficiently, we need to know the total length
2586 * of data received, which doesn't seem to be available outside
2587 * of the RPC layer.
2588 *
2589 * In the case of WRITE, we also want to put the GETATTR after
2590 * the operation -- in this case because we want to make sure
2591 * we get the post-operation mtime and size. This means that
2592 * we can't use xdr_encode_pages() as written: we need a variant
2593 * of it which would leave room in the 'tail' iovec.
2594 *
2595 * Both of these changes to the XDR layer would in fact be quite
2596 * minor, but I decided to leave them for a subsequent patch.
2597 */
2598static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2599 unsigned int pgbase, unsigned int pglen)
2600{
2601 struct nfs4_readlink args = {
2602 .fh = NFS_FH(inode),
2603 .pgbase = pgbase,
2604 .pglen = pglen,
2605 .pages = &page,
2606 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002607 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 struct rpc_message msg = {
2609 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2610 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002611 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 };
2613
Bryan Schumaker7c513052011-03-24 17:12:24 +00002614 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 -07002615}
2616
2617static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2618 unsigned int pgbase, unsigned int pglen)
2619{
2620 struct nfs4_exception exception = { };
2621 int err;
2622 do {
2623 err = nfs4_handle_exception(NFS_SERVER(inode),
2624 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2625 &exception);
2626 } while (exception.retry);
2627 return err;
2628}
2629
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630/*
2631 * Got race?
2632 * We will need to arrange for the VFS layer to provide an atomic open.
2633 * Until then, this create/open method is prone to inefficiency and race
2634 * conditions due to the lookup, create, and open VFS calls from sys_open()
2635 * placed on the wire.
2636 *
2637 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2638 * The file will be opened again in the subsequent VFS open call
2639 * (nfs4_proc_file_open).
2640 *
2641 * The open for read will just hang around to be used by any process that
2642 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2643 */
2644
2645static int
2646nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002647 int flags, struct nfs_open_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648{
Al Viro82a2c1b2011-06-22 18:30:55 -04002649 struct dentry *de = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 struct nfs4_state *state;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002651 struct rpc_cred *cred = NULL;
2652 fmode_t fmode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 int status = 0;
2654
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002655 if (ctx != NULL) {
2656 cred = ctx->cred;
Al Viro3d4ff432011-06-22 18:40:12 -04002657 de = ctx->dentry;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002658 fmode = ctx->mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 }
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002660 sattr->ia_mode &= ~current_umask();
Al Viro82a2c1b2011-06-22 18:30:55 -04002661 state = nfs4_do_open(dir, de, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002662 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 if (IS_ERR(state)) {
2664 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002665 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002667 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002668 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002669 if (ctx != NULL)
2670 ctx->state = state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002671 else
Al Viro643168c2011-06-22 18:20:23 -04002672 nfs4_close_sync(state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673out:
2674 return status;
2675}
2676
2677static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2678{
Trond Myklebust16e42952005-10-27 22:12:44 -04002679 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002680 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002682 .name.len = name->len,
2683 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002684 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002686 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002687 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002688 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002690 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2691 .rpc_argp = &args,
2692 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 };
Trond Myklebustd3468902010-04-16 16:22:50 -04002694 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695
Trond Myklebustd3468902010-04-16 16:22:50 -04002696 res.dir_attr = nfs_alloc_fattr();
2697 if (res.dir_attr == NULL)
2698 goto out;
2699
Bryan Schumaker7c513052011-03-24 17:12:24 +00002700 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002701 if (status == 0) {
2702 update_changeattr(dir, &res.cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002703 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002704 }
Trond Myklebustd3468902010-04-16 16:22:50 -04002705 nfs_free_fattr(res.dir_attr);
2706out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 return status;
2708}
2709
2710static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2711{
2712 struct nfs4_exception exception = { };
2713 int err;
2714 do {
2715 err = nfs4_handle_exception(NFS_SERVER(dir),
2716 _nfs4_proc_remove(dir, name),
2717 &exception);
2718 } while (exception.retry);
2719 return err;
2720}
2721
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002722static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002724 struct nfs_server *server = NFS_SERVER(dir);
2725 struct nfs_removeargs *args = msg->rpc_argp;
2726 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727
Trond Myklebusta65318b2009-03-11 14:10:28 -04002728 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002729 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05002731 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732}
2733
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002734static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002736 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2737
Trond Myklebust14516c32010-07-31 14:29:06 -04002738 if (!nfs4_sequence_done(task, &res->seq_res))
2739 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002740 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002741 return 0;
2742 update_changeattr(dir, &res->cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002743 nfs_post_op_update_inode(dir, res->dir_attr);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002744 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745}
2746
Jeff Laytond3d41522010-09-17 17:31:57 -04002747static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2748{
2749 struct nfs_server *server = NFS_SERVER(dir);
2750 struct nfs_renameargs *arg = msg->rpc_argp;
2751 struct nfs_renameres *res = msg->rpc_resp;
2752
2753 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2754 arg->bitmask = server->attr_bitmask;
2755 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002756 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04002757}
2758
2759static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2760 struct inode *new_dir)
2761{
2762 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2763
2764 if (!nfs4_sequence_done(task, &res->seq_res))
2765 return 0;
2766 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2767 return 0;
2768
2769 update_changeattr(old_dir, &res->old_cinfo);
2770 nfs_post_op_update_inode(old_dir, res->old_fattr);
2771 update_changeattr(new_dir, &res->new_cinfo);
2772 nfs_post_op_update_inode(new_dir, res->new_fattr);
2773 return 1;
2774}
2775
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2777 struct inode *new_dir, struct qstr *new_name)
2778{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002779 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002780 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 .old_dir = NFS_FH(old_dir),
2782 .new_dir = NFS_FH(new_dir),
2783 .old_name = old_name,
2784 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002785 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002787 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002788 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002789 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 struct rpc_message msg = {
2791 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2792 .rpc_argp = &arg,
2793 .rpc_resp = &res,
2794 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002795 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796
Trond Myklebust011fff72010-04-16 16:22:49 -04002797 res.old_fattr = nfs_alloc_fattr();
2798 res.new_fattr = nfs_alloc_fattr();
2799 if (res.old_fattr == NULL || res.new_fattr == NULL)
2800 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801
Bryan Schumaker7c513052011-03-24 17:12:24 +00002802 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 if (!status) {
2804 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002805 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002807 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 }
Trond Myklebust011fff72010-04-16 16:22:49 -04002809out:
2810 nfs_free_fattr(res.new_fattr);
2811 nfs_free_fattr(res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 return status;
2813}
2814
2815static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2816 struct inode *new_dir, struct qstr *new_name)
2817{
2818 struct nfs4_exception exception = { };
2819 int err;
2820 do {
2821 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2822 _nfs4_proc_rename(old_dir, old_name,
2823 new_dir, new_name),
2824 &exception);
2825 } while (exception.retry);
2826 return err;
2827}
2828
2829static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2830{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002831 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 struct nfs4_link_arg arg = {
2833 .fh = NFS_FH(inode),
2834 .dir_fh = NFS_FH(dir),
2835 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002836 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002838 struct nfs4_link_res res = {
2839 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002840 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 struct rpc_message msg = {
2842 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2843 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002844 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 };
Trond Myklebust136f2622010-04-16 16:22:49 -04002846 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847
Trond Myklebust136f2622010-04-16 16:22:49 -04002848 res.fattr = nfs_alloc_fattr();
2849 res.dir_attr = nfs_alloc_fattr();
2850 if (res.fattr == NULL || res.dir_attr == NULL)
2851 goto out;
2852
Bryan Schumaker7c513052011-03-24 17:12:24 +00002853 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002854 if (!status) {
2855 update_changeattr(dir, &res.cinfo);
2856 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002857 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002858 }
Trond Myklebust136f2622010-04-16 16:22:49 -04002859out:
2860 nfs_free_fattr(res.dir_attr);
2861 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 return status;
2863}
2864
2865static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2866{
2867 struct nfs4_exception exception = { };
2868 int err;
2869 do {
2870 err = nfs4_handle_exception(NFS_SERVER(inode),
2871 _nfs4_proc_link(inode, dir, name),
2872 &exception);
2873 } while (exception.retry);
2874 return err;
2875}
2876
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002877struct nfs4_createdata {
2878 struct rpc_message msg;
2879 struct nfs4_create_arg arg;
2880 struct nfs4_create_res res;
2881 struct nfs_fh fh;
2882 struct nfs_fattr fattr;
2883 struct nfs_fattr dir_fattr;
2884};
2885
2886static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2887 struct qstr *name, struct iattr *sattr, u32 ftype)
2888{
2889 struct nfs4_createdata *data;
2890
2891 data = kzalloc(sizeof(*data), GFP_KERNEL);
2892 if (data != NULL) {
2893 struct nfs_server *server = NFS_SERVER(dir);
2894
2895 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2896 data->msg.rpc_argp = &data->arg;
2897 data->msg.rpc_resp = &data->res;
2898 data->arg.dir_fh = NFS_FH(dir);
2899 data->arg.server = server;
2900 data->arg.name = name;
2901 data->arg.attrs = sattr;
2902 data->arg.ftype = ftype;
2903 data->arg.bitmask = server->attr_bitmask;
2904 data->res.server = server;
2905 data->res.fh = &data->fh;
2906 data->res.fattr = &data->fattr;
2907 data->res.dir_fattr = &data->dir_fattr;
2908 nfs_fattr_init(data->res.fattr);
2909 nfs_fattr_init(data->res.dir_fattr);
2910 }
2911 return data;
2912}
2913
2914static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2915{
Bryan Schumaker7c513052011-03-24 17:12:24 +00002916 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00002917 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002918 if (status == 0) {
2919 update_changeattr(dir, &data->res.dir_cinfo);
2920 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2921 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2922 }
2923 return status;
2924}
2925
2926static void nfs4_free_createdata(struct nfs4_createdata *data)
2927{
2928 kfree(data);
2929}
2930
Chuck Lever4f390c12006-08-22 20:06:22 -04002931static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002932 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002934 struct nfs4_createdata *data;
2935 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936
Chuck Lever94a6d752006-08-22 20:06:23 -04002937 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002938 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002939
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002940 status = -ENOMEM;
2941 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2942 if (data == NULL)
2943 goto out;
2944
2945 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2946 data->arg.u.symlink.pages = &page;
2947 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002949 status = nfs4_do_create(dir, dentry, data);
2950
2951 nfs4_free_createdata(data);
2952out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 return status;
2954}
2955
Chuck Lever4f390c12006-08-22 20:06:22 -04002956static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002957 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958{
2959 struct nfs4_exception exception = { };
2960 int err;
2961 do {
2962 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002963 _nfs4_proc_symlink(dir, dentry, page,
2964 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 &exception);
2966 } while (exception.retry);
2967 return err;
2968}
2969
2970static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2971 struct iattr *sattr)
2972{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002973 struct nfs4_createdata *data;
2974 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002976 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2977 if (data == NULL)
2978 goto out;
2979
2980 status = nfs4_do_create(dir, dentry, data);
2981
2982 nfs4_free_createdata(data);
2983out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 return status;
2985}
2986
2987static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2988 struct iattr *sattr)
2989{
2990 struct nfs4_exception exception = { };
2991 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002992
2993 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 do {
2995 err = nfs4_handle_exception(NFS_SERVER(dir),
2996 _nfs4_proc_mkdir(dir, dentry, sattr),
2997 &exception);
2998 } while (exception.retry);
2999 return err;
3000}
3001
3002static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003003 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004{
3005 struct inode *dir = dentry->d_inode;
3006 struct nfs4_readdir_arg args = {
3007 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003008 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 .pgbase = 0,
3010 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003011 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003012 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 };
3014 struct nfs4_readdir_res res;
3015 struct rpc_message msg = {
3016 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3017 .rpc_argp = &args,
3018 .rpc_resp = &res,
3019 .rpc_cred = cred,
3020 };
3021 int status;
3022
Harvey Harrison3110ff82008-05-02 13:42:44 -07003023 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003024 dentry->d_parent->d_name.name,
3025 dentry->d_name.name,
3026 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
3028 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003029 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 -05003030 if (status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003032 status += args.pgbase;
3033 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003034
3035 nfs_invalidate_atime(dir);
3036
Harvey Harrison3110ff82008-05-02 13:42:44 -07003037 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 return status;
3039}
3040
3041static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003042 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043{
3044 struct nfs4_exception exception = { };
3045 int err;
3046 do {
3047 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3048 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003049 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 &exception);
3051 } while (exception.retry);
3052 return err;
3053}
3054
3055static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3056 struct iattr *sattr, dev_t rdev)
3057{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003058 struct nfs4_createdata *data;
3059 int mode = sattr->ia_mode;
3060 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061
3062 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3063 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003064
3065 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3066 if (data == NULL)
3067 goto out;
3068
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003070 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003072 data->arg.ftype = NF4BLK;
3073 data->arg.u.device.specdata1 = MAJOR(rdev);
3074 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 }
3076 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003077 data->arg.ftype = NF4CHR;
3078 data->arg.u.device.specdata1 = MAJOR(rdev);
3079 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003082 status = nfs4_do_create(dir, dentry, data);
3083
3084 nfs4_free_createdata(data);
3085out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 return status;
3087}
3088
3089static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3090 struct iattr *sattr, dev_t rdev)
3091{
3092 struct nfs4_exception exception = { };
3093 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003094
3095 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 do {
3097 err = nfs4_handle_exception(NFS_SERVER(dir),
3098 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3099 &exception);
3100 } while (exception.retry);
3101 return err;
3102}
3103
3104static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3105 struct nfs_fsstat *fsstat)
3106{
3107 struct nfs4_statfs_arg args = {
3108 .fh = fhandle,
3109 .bitmask = server->attr_bitmask,
3110 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003111 struct nfs4_statfs_res res = {
3112 .fsstat = fsstat,
3113 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 struct rpc_message msg = {
3115 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3116 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003117 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 };
3119
Trond Myklebust0e574af2005-10-27 22:12:38 -04003120 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003121 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122}
3123
3124static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3125{
3126 struct nfs4_exception exception = { };
3127 int err;
3128 do {
3129 err = nfs4_handle_exception(server,
3130 _nfs4_proc_statfs(server, fhandle, fsstat),
3131 &exception);
3132 } while (exception.retry);
3133 return err;
3134}
3135
3136static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3137 struct nfs_fsinfo *fsinfo)
3138{
3139 struct nfs4_fsinfo_arg args = {
3140 .fh = fhandle,
3141 .bitmask = server->attr_bitmask,
3142 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003143 struct nfs4_fsinfo_res res = {
3144 .fsinfo = fsinfo,
3145 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 struct rpc_message msg = {
3147 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3148 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003149 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 };
3151
Bryan Schumaker7c513052011-03-24 17:12:24 +00003152 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153}
3154
3155static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3156{
3157 struct nfs4_exception exception = { };
3158 int err;
3159
3160 do {
3161 err = nfs4_handle_exception(server,
3162 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3163 &exception);
3164 } while (exception.retry);
3165 return err;
3166}
3167
3168static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3169{
Trond Myklebust0e574af2005-10-27 22:12:38 -04003170 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3172}
3173
3174static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3175 struct nfs_pathconf *pathconf)
3176{
3177 struct nfs4_pathconf_arg args = {
3178 .fh = fhandle,
3179 .bitmask = server->attr_bitmask,
3180 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003181 struct nfs4_pathconf_res res = {
3182 .pathconf = pathconf,
3183 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 struct rpc_message msg = {
3185 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3186 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003187 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 };
3189
3190 /* None of the pathconf attributes are mandatory to implement */
3191 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3192 memset(pathconf, 0, sizeof(*pathconf));
3193 return 0;
3194 }
3195
Trond Myklebust0e574af2005-10-27 22:12:38 -04003196 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003197 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198}
3199
3200static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3201 struct nfs_pathconf *pathconf)
3202{
3203 struct nfs4_exception exception = { };
3204 int err;
3205
3206 do {
3207 err = nfs4_handle_exception(server,
3208 _nfs4_proc_pathconf(server, fhandle, pathconf),
3209 &exception);
3210 } while (exception.retry);
3211 return err;
3212}
3213
Benny Halevyd20581a2011-05-22 19:52:03 +03003214void __nfs4_read_done_cb(struct nfs_read_data *data)
3215{
3216 nfs_invalidate_atime(data->inode);
3217}
3218
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003219static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220{
Trond Myklebustec06c092006-03-20 13:44:27 -05003221 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003223 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003224 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003225 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003227
Benny Halevyd20581a2011-05-22 19:52:03 +03003228 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003230 renew_lease(server, data->timestamp);
3231 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232}
3233
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003234static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3235{
3236
3237 dprintk("--> %s\n", __func__);
3238
3239 if (!nfs4_sequence_done(task, &data->res.seq_res))
3240 return -EAGAIN;
3241
Benny Halevyd20581a2011-05-22 19:52:03 +03003242 return data->read_done_cb ? data->read_done_cb(task, data) :
3243 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003244}
3245
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003246static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003249 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003250 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003251 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252}
3253
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003254/* Reset the the nfs_read_data to send the read to the MDS. */
3255void nfs4_reset_read(struct rpc_task *task, struct nfs_read_data *data)
3256{
3257 dprintk("%s Reset task for i/o through\n", __func__);
3258 put_lseg(data->lseg);
3259 data->lseg = NULL;
3260 /* offsets will differ in the dense stripe case */
3261 data->args.offset = data->mds_offset;
3262 data->ds_clp = NULL;
3263 data->args.fh = NFS_FH(data->inode);
3264 data->read_done_cb = nfs4_read_done_cb;
3265 task->tk_ops = data->mds_ops;
3266 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3267}
3268EXPORT_SYMBOL_GPL(nfs4_reset_read);
3269
Fred Isamanb029bc92011-03-03 15:13:42 +00003270static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 struct inode *inode = data->inode;
3273
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003274 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003275 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003276 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003278 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003280 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003281 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003282 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283}
3284
Fred Isamanb029bc92011-03-03 15:13:42 +00003285static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3286{
3287 if (!nfs4_sequence_done(task, &data->res.seq_res))
3288 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003289 return data->write_done_cb ? data->write_done_cb(task, data) :
3290 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003291}
3292
Fred Isamana69aef12011-03-03 15:13:47 +00003293/* Reset the the nfs_write_data to send the write to the MDS. */
3294void nfs4_reset_write(struct rpc_task *task, struct nfs_write_data *data)
3295{
3296 dprintk("%s Reset task for i/o through\n", __func__);
3297 put_lseg(data->lseg);
3298 data->lseg = NULL;
3299 data->ds_clp = NULL;
3300 data->write_done_cb = nfs4_write_done_cb;
3301 data->args.fh = NFS_FH(data->inode);
3302 data->args.bitmask = data->res.server->cache_consistency_bitmask;
3303 data->args.offset = data->mds_offset;
3304 data->res.fattr = &data->fattr;
3305 task->tk_ops = data->mds_ops;
3306 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3307}
3308EXPORT_SYMBOL_GPL(nfs4_reset_write);
3309
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003310static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003312 struct nfs_server *server = NFS_SERVER(data->inode);
3313
Fred Isaman7ffd1062011-03-03 15:13:46 +00003314 if (data->lseg) {
3315 data->args.bitmask = NULL;
3316 data->res.fattr = NULL;
3317 } else
3318 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isamanb029bc92011-03-03 15:13:42 +00003319 if (!data->write_done_cb)
3320 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003321 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 data->timestamp = jiffies;
3323
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003324 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003325 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326}
3327
Fred Isaman5f452432011-03-23 13:27:46 +00003328static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003331
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003332 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003333 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003334 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003336 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003337 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338}
3339
Fred Isaman5f452432011-03-23 13:27:46 +00003340static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3341{
3342 if (!nfs4_sequence_done(task, &data->res.seq_res))
3343 return -EAGAIN;
3344 return data->write_done_cb(task, data);
3345}
3346
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003347static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003349 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003350
3351 if (data->lseg) {
3352 data->args.bitmask = NULL;
3353 data->res.fattr = NULL;
3354 } else
3355 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isaman5f452432011-03-23 13:27:46 +00003356 if (!data->write_done_cb)
3357 data->write_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003358 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003359 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003360 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361}
3362
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003363struct nfs4_renewdata {
3364 struct nfs_client *client;
3365 unsigned long timestamp;
3366};
3367
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368/*
3369 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3370 * standalone procedure for queueing an asynchronous RENEW.
3371 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003372static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003373{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003374 struct nfs4_renewdata *data = calldata;
3375 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003376
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003377 if (atomic_read(&clp->cl_count) > 1)
3378 nfs4_schedule_state_renewal(clp);
3379 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003380 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003381}
3382
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003383static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003385 struct nfs4_renewdata *data = calldata;
3386 struct nfs_client *clp = data->client;
3387 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388
3389 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003390 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003391 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3392 return;
3393 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003394 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003395 return;
3396 }
3397 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003399 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400}
3401
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003402static const struct rpc_call_ops nfs4_renew_ops = {
3403 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003404 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003405};
3406
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003407static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408{
3409 struct rpc_message msg = {
3410 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3411 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003412 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003414 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003416 if (renew_flags == 0)
3417 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003418 if (!atomic_inc_not_zero(&clp->cl_count))
3419 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003420 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003421 if (data == NULL)
3422 return -ENOMEM;
3423 data->client = clp;
3424 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003426 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427}
3428
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003429static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430{
3431 struct rpc_message msg = {
3432 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3433 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003434 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 };
3436 unsigned long now = jiffies;
3437 int status;
3438
3439 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3440 if (status < 0)
3441 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003442 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 return 0;
3444}
3445
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003446static inline int nfs4_server_supports_acls(struct nfs_server *server)
3447{
3448 return (server->caps & NFS_CAP_ACLS)
3449 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3450 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3451}
3452
3453/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3454 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3455 * the stack.
3456 */
3457#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3458
Neil Hormane9e3d722011-03-04 19:26:03 -05003459static int buf_to_pages_noslab(const void *buf, size_t buflen,
3460 struct page **pages, unsigned int *pgbase)
3461{
3462 struct page *newpage, **spages;
3463 int rc = 0;
3464 size_t len;
3465 spages = pages;
3466
3467 do {
Jovi Zhang43b7c3f2011-03-02 23:19:37 +00003468 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003469 newpage = alloc_page(GFP_KERNEL);
3470
3471 if (newpage == NULL)
3472 goto unwind;
3473 memcpy(page_address(newpage), buf, len);
3474 buf += len;
3475 buflen -= len;
3476 *pages++ = newpage;
3477 rc++;
3478 } while (buflen != 0);
3479
3480 return rc;
3481
3482unwind:
3483 for(; rc > 0; rc--)
3484 __free_page(spages[rc-1]);
3485 return -ENOMEM;
3486}
3487
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003488struct nfs4_cached_acl {
3489 int cached;
3490 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003491 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003492};
3493
3494static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003495{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003496 struct nfs_inode *nfsi = NFS_I(inode);
3497
3498 spin_lock(&inode->i_lock);
3499 kfree(nfsi->nfs4_acl);
3500 nfsi->nfs4_acl = acl;
3501 spin_unlock(&inode->i_lock);
3502}
3503
3504static void nfs4_zap_acl_attr(struct inode *inode)
3505{
3506 nfs4_set_cached_acl(inode, NULL);
3507}
3508
3509static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3510{
3511 struct nfs_inode *nfsi = NFS_I(inode);
3512 struct nfs4_cached_acl *acl;
3513 int ret = -ENOENT;
3514
3515 spin_lock(&inode->i_lock);
3516 acl = nfsi->nfs4_acl;
3517 if (acl == NULL)
3518 goto out;
3519 if (buf == NULL) /* user is just asking for length */
3520 goto out_len;
3521 if (acl->cached == 0)
3522 goto out;
3523 ret = -ERANGE; /* see getxattr(2) man page */
3524 if (acl->len > buflen)
3525 goto out;
3526 memcpy(buf, acl->data, acl->len);
3527out_len:
3528 ret = acl->len;
3529out:
3530 spin_unlock(&inode->i_lock);
3531 return ret;
3532}
3533
3534static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3535{
3536 struct nfs4_cached_acl *acl;
3537
3538 if (buf && acl_len <= PAGE_SIZE) {
3539 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3540 if (acl == NULL)
3541 goto out;
3542 acl->cached = 1;
3543 memcpy(acl->data, buf, acl_len);
3544 } else {
3545 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3546 if (acl == NULL)
3547 goto out;
3548 acl->cached = 0;
3549 }
3550 acl->len = acl_len;
3551out:
3552 nfs4_set_cached_acl(inode, acl);
3553}
3554
Andy Adamsonbf118a32011-12-07 11:55:27 -05003555/*
3556 * The getxattr API returns the required buffer length when called with a
3557 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3558 * the required buf. On a NULL buf, we send a page of data to the server
3559 * guessing that the ACL request can be serviced by a page. If so, we cache
3560 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3561 * the cache. If not so, we throw away the page, and cache the required
3562 * length. The next getxattr call will then produce another round trip to
3563 * the server, this time with the input buf of the required size.
3564 */
Trond Myklebust16b42892006-08-24 12:27:15 -04003565static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003566{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003567 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003568 struct nfs_getaclargs args = {
3569 .fh = NFS_FH(inode),
3570 .acl_pages = pages,
3571 .acl_len = buflen,
3572 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003573 struct nfs_getaclres res = {
3574 .acl_len = buflen,
3575 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003576 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003577 struct rpc_message msg = {
3578 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3579 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003580 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003581 };
Andy Adamsonbf118a32011-12-07 11:55:27 -05003582 int ret = -ENOMEM, npages, i, acl_len = 0;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003583
Andy Adamsonbf118a32011-12-07 11:55:27 -05003584 npages = (buflen + PAGE_SIZE - 1) >> PAGE_SHIFT;
3585 /* As long as we're doing a round trip to the server anyway,
3586 * let's be prepared for a page of acl data. */
3587 if (npages == 0)
3588 npages = 1;
3589
3590 for (i = 0; i < npages; i++) {
3591 pages[i] = alloc_page(GFP_KERNEL);
3592 if (!pages[i])
3593 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003594 }
Andy Adamsonbf118a32011-12-07 11:55:27 -05003595 if (npages > 1) {
3596 /* for decoding across pages */
3597 args.acl_scratch = alloc_page(GFP_KERNEL);
3598 if (!args.acl_scratch)
3599 goto out_free;
3600 }
3601 args.acl_len = npages * PAGE_SIZE;
3602 args.acl_pgbase = 0;
3603 /* Let decode_getfacl know not to fail if the ACL data is larger than
3604 * the page we send as a guess */
3605 if (buf == NULL)
3606 res.acl_flags |= NFS4_ACL_LEN_REQUEST;
3607 resp_buf = page_address(pages[0]);
3608
Peng Taode040be2012-01-10 22:42:47 +08003609 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003610 __func__, buf, buflen, npages, args.acl_len);
3611 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3612 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003613 if (ret)
3614 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003615
3616 acl_len = res.acl_len - res.acl_data_offset;
3617 if (acl_len > args.acl_len)
3618 nfs4_write_cached_acl(inode, NULL, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003619 else
Andy Adamsonbf118a32011-12-07 11:55:27 -05003620 nfs4_write_cached_acl(inode, resp_buf + res.acl_data_offset,
3621 acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003622 if (buf) {
3623 ret = -ERANGE;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003624 if (acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003625 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003626 _copy_from_pages(buf, pages, res.acl_data_offset,
3627 res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003628 }
Andy Adamsonbf118a32011-12-07 11:55:27 -05003629 ret = acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003630out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003631 for (i = 0; i < npages; i++)
3632 if (pages[i])
3633 __free_page(pages[i]);
3634 if (args.acl_scratch)
3635 __free_page(args.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003636 return ret;
3637}
3638
Trond Myklebust16b42892006-08-24 12:27:15 -04003639static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3640{
3641 struct nfs4_exception exception = { };
3642 ssize_t ret;
3643 do {
3644 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3645 if (ret >= 0)
3646 break;
3647 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3648 } while (exception.retry);
3649 return ret;
3650}
3651
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003652static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3653{
3654 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003655 int ret;
3656
3657 if (!nfs4_server_supports_acls(server))
3658 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003659 ret = nfs_revalidate_inode(server, inode);
3660 if (ret < 0)
3661 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003662 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3663 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003664 ret = nfs4_read_cached_acl(inode, buf, buflen);
3665 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003666 /* -ENOENT is returned if there is no ACL or if there is an ACL
3667 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003668 return ret;
3669 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003670}
3671
Trond Myklebust16b42892006-08-24 12:27:15 -04003672static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003673{
3674 struct nfs_server *server = NFS_SERVER(inode);
3675 struct page *pages[NFS4ACL_MAXPAGES];
3676 struct nfs_setaclargs arg = {
3677 .fh = NFS_FH(inode),
3678 .acl_pages = pages,
3679 .acl_len = buflen,
3680 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003681 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003682 struct rpc_message msg = {
3683 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3684 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003685 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003686 };
Neil Hormane9e3d722011-03-04 19:26:03 -05003687 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003688
3689 if (!nfs4_server_supports_acls(server))
3690 return -EOPNOTSUPP;
Neil Hormane9e3d722011-03-04 19:26:03 -05003691 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3692 if (i < 0)
3693 return i;
Trond Myklebust642ac542005-10-18 14:20:19 -07003694 nfs_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003695 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003696
3697 /*
3698 * Free each page after tx, so the only ref left is
3699 * held by the network stack
3700 */
3701 for (; i > 0; i--)
3702 put_page(pages[i-1]);
3703
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003704 /*
3705 * Acl update can result in inode attribute update.
3706 * so mark the attribute cache invalid.
3707 */
3708 spin_lock(&inode->i_lock);
3709 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3710 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003711 nfs_access_zap_cache(inode);
3712 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003713 return ret;
3714}
3715
Trond Myklebust16b42892006-08-24 12:27:15 -04003716static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3717{
3718 struct nfs4_exception exception = { };
3719 int err;
3720 do {
3721 err = nfs4_handle_exception(NFS_SERVER(inode),
3722 __nfs4_proc_set_acl(inode, buf, buflen),
3723 &exception);
3724 } while (exception.retry);
3725 return err;
3726}
3727
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003729nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003731 struct nfs_client *clp = server->nfs_client;
3732
3733 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734 return 0;
3735 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003736 case -NFS4ERR_ADMIN_REVOKED:
3737 case -NFS4ERR_BAD_STATEID:
3738 case -NFS4ERR_OPENMODE:
3739 if (state == NULL)
3740 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003741 nfs4_schedule_stateid_recovery(server, state);
3742 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003743 case -NFS4ERR_EXPIRED:
3744 if (state != NULL)
3745 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003747 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003748 nfs4_schedule_lease_recovery(clp);
3749 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003750#if defined(CONFIG_NFS_V4_1)
3751 case -NFS4ERR_BADSESSION:
3752 case -NFS4ERR_BADSLOT:
3753 case -NFS4ERR_BAD_HIGH_SLOT:
3754 case -NFS4ERR_DEADSESSION:
3755 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3756 case -NFS4ERR_SEQ_FALSE_RETRY:
3757 case -NFS4ERR_SEQ_MISORDERED:
3758 dprintk("%s ERROR %d, Reset session\n", __func__,
3759 task->tk_status);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003760 nfs4_schedule_session_recovery(clp->cl_session);
Andy Adamson4745e312009-04-01 09:22:42 -04003761 task->tk_status = 0;
3762 return -EAGAIN;
3763#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003765 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003766 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003767 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3769 task->tk_status = 0;
3770 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04003771 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772 case -NFS4ERR_OLD_STATEID:
3773 task->tk_status = 0;
3774 return -EAGAIN;
3775 }
3776 task->tk_status = nfs4_map_errors(task->tk_status);
3777 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003778wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003779 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003780 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3781 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3782 task->tk_status = 0;
3783 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784}
3785
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003786int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3787 unsigned short port, struct rpc_cred *cred,
3788 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789{
3790 nfs4_verifier sc_verifier;
3791 struct nfs4_setclientid setclientid = {
3792 .sc_verifier = &sc_verifier,
3793 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00003794 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795 };
3796 struct rpc_message msg = {
3797 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3798 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003799 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003800 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801 };
Al Virobc4785c2006-10-19 23:28:51 -07003802 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 int loop = 0;
3804 int status;
3805
Al Virobc4785c2006-10-19 23:28:51 -07003806 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3808 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3809
3810 for(;;) {
3811 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003812 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003813 clp->cl_ipaddr,
3814 rpc_peeraddr2str(clp->cl_rpcclient,
3815 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003816 rpc_peeraddr2str(clp->cl_rpcclient,
3817 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003818 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 clp->cl_id_uniquifier);
3820 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003821 sizeof(setclientid.sc_netid),
3822 rpc_peeraddr2str(clp->cl_rpcclient,
3823 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003825 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826 clp->cl_ipaddr, port >> 8, port & 255);
3827
Trond Myklebust1bd714f2011-04-24 14:29:33 -04003828 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829 if (status != -NFS4ERR_CLID_INUSE)
3830 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003831 if (loop != 0) {
3832 ++clp->cl_id_uniquifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003834 }
3835 ++loop;
3836 ssleep(clp->cl_lease_time / HZ + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837 }
3838 return status;
3839}
3840
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003841int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003842 struct nfs4_setclientid_res *arg,
3843 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844{
3845 struct nfs_fsinfo fsinfo;
3846 struct rpc_message msg = {
3847 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003848 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003850 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851 };
3852 unsigned long now;
3853 int status;
3854
3855 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04003856 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857 if (status == 0) {
3858 spin_lock(&clp->cl_lock);
3859 clp->cl_lease_time = fsinfo.lease_time * HZ;
3860 clp->cl_last_renewal = now;
3861 spin_unlock(&clp->cl_lock);
3862 }
3863 return status;
3864}
3865
Trond Myklebustfe650402006-01-03 09:55:18 +01003866struct nfs4_delegreturndata {
3867 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003868 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003869 struct nfs_fh fh;
3870 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003871 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003872 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003873 int rpc_status;
3874};
3875
Trond Myklebustfe650402006-01-03 09:55:18 +01003876static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3877{
3878 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003879
Trond Myklebust14516c32010-07-31 14:29:06 -04003880 if (!nfs4_sequence_done(task, &data->res.seq_res))
3881 return;
Andy Adamson938e1012009-04-01 09:22:28 -04003882
Ricardo Labiaga79708862009-12-07 09:23:21 -05003883 switch (task->tk_status) {
3884 case -NFS4ERR_STALE_STATEID:
3885 case -NFS4ERR_EXPIRED:
3886 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01003887 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003888 break;
3889 default:
3890 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3891 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003892 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003893 return;
3894 }
3895 }
3896 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01003897}
3898
3899static void nfs4_delegreturn_release(void *calldata)
3900{
Trond Myklebustfe650402006-01-03 09:55:18 +01003901 kfree(calldata);
3902}
3903
Andy Adamson938e1012009-04-01 09:22:28 -04003904#if defined(CONFIG_NFS_V4_1)
3905static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3906{
3907 struct nfs4_delegreturndata *d_data;
3908
3909 d_data = (struct nfs4_delegreturndata *)data;
3910
Trond Myklebust035168a2010-06-16 09:52:26 -04003911 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04003912 &d_data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003913 &d_data->res.seq_res, task))
Andy Adamson938e1012009-04-01 09:22:28 -04003914 return;
3915 rpc_call_start(task);
3916}
3917#endif /* CONFIG_NFS_V4_1 */
3918
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003919static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04003920#if defined(CONFIG_NFS_V4_1)
3921 .rpc_call_prepare = nfs4_delegreturn_prepare,
3922#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01003923 .rpc_call_done = nfs4_delegreturn_done,
3924 .rpc_release = nfs4_delegreturn_release,
3925};
3926
Trond Myklebuste6f81072008-01-24 18:14:34 -05003927static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003928{
3929 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003930 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003931 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003932 struct rpc_message msg = {
3933 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3934 .rpc_cred = cred,
3935 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003936 struct rpc_task_setup task_setup_data = {
3937 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003938 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003939 .callback_ops = &nfs4_delegreturn_ops,
3940 .flags = RPC_TASK_ASYNC,
3941 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003942 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003943
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003944 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01003945 if (data == NULL)
3946 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05003947 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01003948 data->args.fhandle = &data->fh;
3949 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003950 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003951 nfs_copy_fh(&data->fh, NFS_FH(inode));
3952 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003953 data->res.fattr = &data->fattr;
3954 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003955 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003956 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003957 data->rpc_status = 0;
3958
Trond Myklebustc970aa82007-07-14 15:39:59 -04003959 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05003960 msg.rpc_argp = &data->args;
3961 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003962 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003963 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003964 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003965 if (!issync)
3966 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003967 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003968 if (status != 0)
3969 goto out;
3970 status = data->rpc_status;
3971 if (status != 0)
3972 goto out;
3973 nfs_refresh_inode(inode, &data->fattr);
3974out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003975 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003976 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977}
3978
Trond Myklebuste6f81072008-01-24 18:14:34 -05003979int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980{
3981 struct nfs_server *server = NFS_SERVER(inode);
3982 struct nfs4_exception exception = { };
3983 int err;
3984 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003985 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986 switch (err) {
3987 case -NFS4ERR_STALE_STATEID:
3988 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 case 0:
3990 return 0;
3991 }
3992 err = nfs4_handle_exception(server, err, &exception);
3993 } while (exception.retry);
3994 return err;
3995}
3996
3997#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3998#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3999
4000/*
4001 * sleep, with exponential backoff, and retry the LOCK operation.
4002 */
4003static unsigned long
4004nfs4_set_lock_task_retry(unsigned long timeout)
4005{
Jeff Laytond3103102011-12-01 22:44:39 +01004006 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007 timeout <<= 1;
4008 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4009 return NFS4_LOCK_MAXTIMEOUT;
4010 return timeout;
4011}
4012
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4014{
4015 struct inode *inode = state->inode;
4016 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004017 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004018 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004020 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004022 struct nfs_lockt_res res = {
4023 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 };
4025 struct rpc_message msg = {
4026 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4027 .rpc_argp = &arg,
4028 .rpc_resp = &res,
4029 .rpc_cred = state->owner->so_cred,
4030 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 struct nfs4_lock_state *lsp;
4032 int status;
4033
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004034 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004035 status = nfs4_set_lock_state(state, request);
4036 if (status != 0)
4037 goto out;
4038 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004039 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004040 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004041 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004042 switch (status) {
4043 case 0:
4044 request->fl_type = F_UNLCK;
4045 break;
4046 case -NFS4ERR_DENIED:
4047 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004049 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004050out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051 return status;
4052}
4053
4054static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4055{
4056 struct nfs4_exception exception = { };
4057 int err;
4058
4059 do {
4060 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4061 _nfs4_proc_getlk(state, cmd, request),
4062 &exception);
4063 } while (exception.retry);
4064 return err;
4065}
4066
4067static int do_vfs_lock(struct file *file, struct file_lock *fl)
4068{
4069 int res = 0;
4070 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4071 case FL_POSIX:
4072 res = posix_lock_file_wait(file, fl);
4073 break;
4074 case FL_FLOCK:
4075 res = flock_lock_file_wait(file, fl);
4076 break;
4077 default:
4078 BUG();
4079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080 return res;
4081}
4082
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004083struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004084 struct nfs_locku_args arg;
4085 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004086 struct nfs4_lock_state *lsp;
4087 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004088 struct file_lock fl;
4089 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004090 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004091};
4092
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004093static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4094 struct nfs_open_context *ctx,
4095 struct nfs4_lock_state *lsp,
4096 struct nfs_seqid *seqid)
4097{
4098 struct nfs4_unlockdata *p;
4099 struct inode *inode = lsp->ls_state->inode;
4100
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004101 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004102 if (p == NULL)
4103 return NULL;
4104 p->arg.fh = NFS_FH(inode);
4105 p->arg.fl = &p->fl;
4106 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004107 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004108 p->arg.stateid = &lsp->ls_stateid;
4109 p->lsp = lsp;
4110 atomic_inc(&lsp->ls_count);
4111 /* Ensure we don't close file until we're done freeing locks! */
4112 p->ctx = get_nfs_open_context(ctx);
4113 memcpy(&p->fl, fl, sizeof(p->fl));
4114 p->server = NFS_SERVER(inode);
4115 return p;
4116}
4117
Trond Myklebust06f814a2006-01-03 09:55:07 +01004118static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004119{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004120 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004121 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004122 nfs4_put_lock_state(calldata->lsp);
4123 put_nfs_open_context(calldata->ctx);
4124 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004125}
4126
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004127static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004128{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004129 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004130
Trond Myklebust14516c32010-07-31 14:29:06 -04004131 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4132 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004133 switch (task->tk_status) {
4134 case 0:
4135 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004136 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004137 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01004138 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004139 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004140 case -NFS4ERR_BAD_STATEID:
4141 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004142 case -NFS4ERR_STALE_STATEID:
4143 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004144 break;
4145 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004146 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004147 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004148 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004149}
4150
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004151static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004152{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004153 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004155 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004156 return;
4157 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004158 /* Note: exit _without_ running nfs4_locku_done */
4159 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004160 return;
4161 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004162 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004163 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004164 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004165 &calldata->res.seq_res, task))
Andy Adamsona8936932009-04-01 09:22:23 -04004166 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004167 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168}
4169
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004170static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004171 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004172 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004173 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004174};
4175
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004176static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4177 struct nfs_open_context *ctx,
4178 struct nfs4_lock_state *lsp,
4179 struct nfs_seqid *seqid)
4180{
4181 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004182 struct rpc_message msg = {
4183 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4184 .rpc_cred = ctx->cred,
4185 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004186 struct rpc_task_setup task_setup_data = {
4187 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004188 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004189 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004190 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004191 .flags = RPC_TASK_ASYNC,
4192 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004193
Frank Filz137d6ac2007-07-09 15:32:29 -07004194 /* Ensure this is an unlock - when canceling a lock, the
4195 * canceled lock is passed in, and it won't be an unlock.
4196 */
4197 fl->fl_type = F_UNLCK;
4198
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004199 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4200 if (data == NULL) {
4201 nfs_free_seqid(seqid);
4202 return ERR_PTR(-ENOMEM);
4203 }
4204
Trond Myklebust9d12b212012-01-17 22:04:25 -05004205 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004206 msg.rpc_argp = &data->arg;
4207 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004208 task_setup_data.callback_data = data;
4209 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004210}
4211
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4213{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004214 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004215 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004216 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004217 struct rpc_task *task;
4218 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004219 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220
Trond Myklebust9b073572006-06-29 16:38:34 -04004221 status = nfs4_set_lock_state(state, request);
4222 /* Unlock _before_ we do the RPC call */
4223 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004224 down_read(&nfsi->rwsem);
4225 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4226 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004227 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004228 }
4229 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004230 if (status != 0)
4231 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004232 /* Is this a delegated lock? */
4233 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004234 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004235 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004236 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004237 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004238 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004239 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004240 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004241 status = PTR_ERR(task);
4242 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004243 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004244 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004245 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004246out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004247 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004248 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249}
4250
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004251struct nfs4_lockdata {
4252 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004253 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004254 struct nfs4_lock_state *lsp;
4255 struct nfs_open_context *ctx;
4256 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004257 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004258 int rpc_status;
4259 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004260 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004261};
4262
4263static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004264 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4265 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004266{
4267 struct nfs4_lockdata *p;
4268 struct inode *inode = lsp->ls_state->inode;
4269 struct nfs_server *server = NFS_SERVER(inode);
4270
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004271 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004272 if (p == NULL)
4273 return NULL;
4274
4275 p->arg.fh = NFS_FH(inode);
4276 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004277 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004278 if (p->arg.open_seqid == NULL)
4279 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004280 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004281 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004282 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004283 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004284 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004285 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004286 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004287 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004288 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004289 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004290 atomic_inc(&lsp->ls_count);
4291 p->ctx = get_nfs_open_context(ctx);
4292 memcpy(&p->fl, fl, sizeof(p->fl));
4293 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004294out_free_seqid:
4295 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004296out_free:
4297 kfree(p);
4298 return NULL;
4299}
4300
4301static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4302{
4303 struct nfs4_lockdata *data = calldata;
4304 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305
Harvey Harrison3110ff82008-05-02 13:42:44 -07004306 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004307 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4308 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004309 /* Do we need to do an open_to_lock_owner? */
4310 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004311 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4312 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004313 data->arg.open_stateid = &state->stateid;
4314 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004315 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004316 } else
4317 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004318 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004319 if (nfs4_setup_sequence(data->server,
4320 &data->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004321 &data->res.seq_res, task))
Andy Adamson66179ef2009-04-01 09:22:22 -04004322 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004323 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004324 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004325}
4326
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004327static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4328{
4329 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4330 nfs4_lock_prepare(task, calldata);
4331}
4332
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004333static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4334{
4335 struct nfs4_lockdata *data = calldata;
4336
Harvey Harrison3110ff82008-05-02 13:42:44 -07004337 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004338
Trond Myklebust14516c32010-07-31 14:29:06 -04004339 if (!nfs4_sequence_done(task, &data->res.seq_res))
4340 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004341
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004342 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004343 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004344 if (data->rpc_status == 0)
4345 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4346 else
4347 goto out;
4348 }
4349 if (data->rpc_status == 0) {
4350 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4351 sizeof(data->lsp->ls_stateid.data));
4352 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Al Viro3d4ff432011-06-22 18:40:12 -04004353 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004354 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004355out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004356 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004357}
4358
4359static void nfs4_lock_release(void *calldata)
4360{
4361 struct nfs4_lockdata *data = calldata;
4362
Harvey Harrison3110ff82008-05-02 13:42:44 -07004363 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004364 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004365 if (data->cancelled != 0) {
4366 struct rpc_task *task;
4367 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4368 data->arg.lock_seqid);
4369 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004370 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004371 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004372 } else
4373 nfs_free_seqid(data->arg.lock_seqid);
4374 nfs4_put_lock_state(data->lsp);
4375 put_nfs_open_context(data->ctx);
4376 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004377 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004378}
4379
4380static const struct rpc_call_ops nfs4_lock_ops = {
4381 .rpc_call_prepare = nfs4_lock_prepare,
4382 .rpc_call_done = nfs4_lock_done,
4383 .rpc_release = nfs4_lock_release,
4384};
4385
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004386static const struct rpc_call_ops nfs4_recover_lock_ops = {
4387 .rpc_call_prepare = nfs4_recover_lock_prepare,
4388 .rpc_call_done = nfs4_lock_done,
4389 .rpc_release = nfs4_lock_release,
4390};
4391
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004392static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4393{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004394 switch (error) {
4395 case -NFS4ERR_ADMIN_REVOKED:
4396 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004397 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004398 if (new_lock_owner != 0 ||
4399 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004400 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004401 break;
4402 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004403 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004404 case -NFS4ERR_EXPIRED:
4405 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004406 };
4407}
4408
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004409static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004410{
4411 struct nfs4_lockdata *data;
4412 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004413 struct rpc_message msg = {
4414 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4415 .rpc_cred = state->owner->so_cred,
4416 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004417 struct rpc_task_setup task_setup_data = {
4418 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004419 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004420 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004421 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004422 .flags = RPC_TASK_ASYNC,
4423 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004424 int ret;
4425
Harvey Harrison3110ff82008-05-02 13:42:44 -07004426 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004427 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004428 fl->fl_u.nfs4_fl.owner,
4429 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004430 if (data == NULL)
4431 return -ENOMEM;
4432 if (IS_SETLKW(cmd))
4433 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004434 if (recovery_type > NFS_LOCK_NEW) {
4435 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004436 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004437 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4438 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05004439 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004440 msg.rpc_argp = &data->arg;
4441 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004442 task_setup_data.callback_data = data;
4443 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004444 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004445 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004446 ret = nfs4_wait_for_completion_rpc_task(task);
4447 if (ret == 0) {
4448 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004449 if (ret)
4450 nfs4_handle_setlk_error(data->server, data->lsp,
4451 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004452 } else
4453 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004454 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004455 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004456 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457}
4458
4459static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4460{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004461 struct nfs_server *server = NFS_SERVER(state->inode);
4462 struct nfs4_exception exception = { };
4463 int err;
4464
4465 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004466 /* Cache the lock if possible... */
4467 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4468 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004469 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004470 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004471 break;
4472 nfs4_handle_exception(server, err, &exception);
4473 } while (exception.retry);
4474 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475}
4476
4477static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4478{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004479 struct nfs_server *server = NFS_SERVER(state->inode);
4480 struct nfs4_exception exception = { };
4481 int err;
4482
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004483 err = nfs4_set_lock_state(state, request);
4484 if (err != 0)
4485 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004486 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004487 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4488 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004489 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004490 switch (err) {
4491 default:
4492 goto out;
4493 case -NFS4ERR_GRACE:
4494 case -NFS4ERR_DELAY:
4495 nfs4_handle_exception(server, err, &exception);
4496 err = 0;
4497 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004498 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004499out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004500 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501}
4502
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004503#if defined(CONFIG_NFS_V4_1)
4504static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4505{
4506 int status;
4507 struct nfs_server *server = NFS_SERVER(state->inode);
4508
4509 status = nfs41_test_stateid(server, state);
4510 if (status == NFS_OK)
4511 return 0;
4512 nfs41_free_stateid(server, state);
4513 return nfs4_lock_expired(state, request);
4514}
4515#endif
4516
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4518{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004519 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004520 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004521 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004523 if ((fl_flags & FL_POSIX) &&
4524 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4525 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004526 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004527 status = nfs4_set_lock_state(state, request);
4528 if (status != 0)
4529 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004530 request->fl_flags |= FL_ACCESS;
4531 status = do_vfs_lock(request->fl_file, request);
4532 if (status < 0)
4533 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004534 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004535 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004536 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004537 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004538 request->fl_flags = fl_flags & ~FL_SLEEP;
4539 status = do_vfs_lock(request->fl_file, request);
4540 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004541 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004542 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004543 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004544 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004545 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004546 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004547 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07004548 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004549out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004550 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004551out:
4552 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553 return status;
4554}
4555
4556static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4557{
4558 struct nfs4_exception exception = { };
4559 int err;
4560
4561 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004562 err = _nfs4_proc_setlk(state, cmd, request);
4563 if (err == -NFS4ERR_DENIED)
4564 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004566 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567 } while (exception.retry);
4568 return err;
4569}
4570
4571static int
4572nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4573{
4574 struct nfs_open_context *ctx;
4575 struct nfs4_state *state;
4576 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4577 int status;
4578
4579 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004580 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581 state = ctx->state;
4582
4583 if (request->fl_start < 0 || request->fl_end < 0)
4584 return -EINVAL;
4585
Trond Myklebustd9531262009-07-21 19:22:38 -04004586 if (IS_GETLK(cmd)) {
4587 if (state != NULL)
4588 return nfs4_proc_getlk(state, F_GETLK, request);
4589 return 0;
4590 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591
4592 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4593 return -EINVAL;
4594
Trond Myklebustd9531262009-07-21 19:22:38 -04004595 if (request->fl_type == F_UNLCK) {
4596 if (state != NULL)
4597 return nfs4_proc_unlck(state, cmd, request);
4598 return 0;
4599 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600
Trond Myklebustd9531262009-07-21 19:22:38 -04004601 if (state == NULL)
4602 return -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603 do {
4604 status = nfs4_proc_setlk(state, cmd, request);
4605 if ((status != -EAGAIN) || IS_SETLK(cmd))
4606 break;
4607 timeout = nfs4_set_lock_task_retry(timeout);
4608 status = -ERESTARTSYS;
4609 if (signalled())
4610 break;
4611 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612 return status;
4613}
4614
Trond Myklebust888e6942005-11-04 15:38:11 -05004615int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4616{
4617 struct nfs_server *server = NFS_SERVER(state->inode);
4618 struct nfs4_exception exception = { };
4619 int err;
4620
4621 err = nfs4_set_lock_state(state, fl);
4622 if (err != 0)
4623 goto out;
4624 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004625 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004626 switch (err) {
4627 default:
4628 printk(KERN_ERR "%s: unhandled error %d.\n",
4629 __func__, err);
4630 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004631 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004632 goto out;
4633 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004634 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07004635 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004636 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004637 nfs4_schedule_lease_recovery(server->nfs_client);
4638 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004639 case -NFS4ERR_BADSESSION:
4640 case -NFS4ERR_BADSLOT:
4641 case -NFS4ERR_BAD_HIGH_SLOT:
4642 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4643 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004644 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Trond Myklebustd5122202009-06-17 13:22:58 -07004645 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004646 case -ERESTARTSYS:
4647 /*
4648 * The show must go on: exit, but mark the
4649 * stateid as needing recovery.
4650 */
4651 case -NFS4ERR_ADMIN_REVOKED:
4652 case -NFS4ERR_BAD_STATEID:
4653 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004654 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004655 err = 0;
4656 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004657 case -EKEYEXPIRED:
4658 /*
4659 * User RPCSEC_GSS context has expired.
4660 * We cannot recover this stateid now, so
4661 * skip it and allow recovery thread to
4662 * proceed.
4663 */
4664 err = 0;
4665 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004666 case -ENOMEM:
4667 case -NFS4ERR_DENIED:
4668 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4669 err = 0;
4670 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004671 case -NFS4ERR_DELAY:
4672 break;
4673 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004674 err = nfs4_handle_exception(server, err, &exception);
4675 } while (exception.retry);
4676out:
4677 return err;
4678}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004679
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004680static void nfs4_release_lockowner_release(void *calldata)
4681{
4682 kfree(calldata);
4683}
4684
4685const struct rpc_call_ops nfs4_release_lockowner_ops = {
4686 .rpc_release = nfs4_release_lockowner_release,
4687};
4688
4689void nfs4_release_lockowner(const struct nfs4_lock_state *lsp)
4690{
4691 struct nfs_server *server = lsp->ls_state->owner->so_server;
4692 struct nfs_release_lockowner_args *args;
4693 struct rpc_message msg = {
4694 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4695 };
4696
4697 if (server->nfs_client->cl_mvops->minor_version != 0)
4698 return;
4699 args = kmalloc(sizeof(*args), GFP_NOFS);
4700 if (!args)
4701 return;
4702 args->lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004703 args->lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004704 args->lock_owner.s_dev = server->s_dev;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004705 msg.rpc_argp = args;
4706 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, args);
4707}
4708
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004709#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4710
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004711static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4712 const void *buf, size_t buflen,
4713 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004714{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004715 if (strcmp(key, "") != 0)
4716 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004717
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004718 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004719}
4720
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004721static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4722 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004723{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004724 if (strcmp(key, "") != 0)
4725 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004726
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004727 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004728}
4729
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004730static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4731 size_t list_len, const char *name,
4732 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004733{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004734 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004735
J. Bruce Fields096455a2006-03-20 23:23:42 -05004736 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4737 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004738
4739 if (list && len <= list_len)
4740 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004741 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004742}
4743
Andy Adamson533eb462011-06-13 18:25:56 -04004744/*
4745 * nfs_fhget will use either the mounted_on_fileid or the fileid
4746 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004747static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4748{
Andy Adamson533eb462011-06-13 18:25:56 -04004749 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
4750 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
4751 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4752 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004753 return;
4754
4755 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4756 NFS_ATTR_FATTR_NLINK;
4757 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4758 fattr->nlink = 2;
4759}
4760
Trond Myklebust56659e92007-07-17 21:52:39 -04004761int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004762 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004763{
4764 struct nfs_server *server = NFS_SERVER(dir);
4765 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004766 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004767 };
4768 struct nfs4_fs_locations_arg args = {
4769 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004770 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004771 .page = page,
4772 .bitmask = bitmask,
4773 };
Benny Halevy22958462009-04-01 09:22:02 -04004774 struct nfs4_fs_locations_res res = {
4775 .fs_locations = fs_locations,
4776 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004777 struct rpc_message msg = {
4778 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4779 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004780 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004781 };
4782 int status;
4783
Harvey Harrison3110ff82008-05-02 13:42:44 -07004784 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04004785
4786 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
4787 * is not supported */
4788 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
4789 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
4790 else
4791 bitmask[0] |= FATTR4_WORD0_FILEID;
4792
Trond Myklebustc228fd32007-01-13 02:28:11 -05004793 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004794 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004795 fs_locations->nlocations = 0;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004796 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004797 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004798 return status;
4799}
4800
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00004801static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4802{
4803 int status;
4804 struct nfs4_secinfo_arg args = {
4805 .dir_fh = NFS_FH(dir),
4806 .name = name,
4807 };
4808 struct nfs4_secinfo_res res = {
4809 .flavors = flavors,
4810 };
4811 struct rpc_message msg = {
4812 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
4813 .rpc_argp = &args,
4814 .rpc_resp = &res,
4815 };
4816
4817 dprintk("NFS call secinfo %s\n", name->name);
4818 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4819 dprintk("NFS reply secinfo: %d\n", status);
4820 return status;
4821}
4822
4823int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4824{
4825 struct nfs4_exception exception = { };
4826 int err;
4827 do {
4828 err = nfs4_handle_exception(NFS_SERVER(dir),
4829 _nfs4_proc_secinfo(dir, name, flavors),
4830 &exception);
4831 } while (exception.retry);
4832 return err;
4833}
4834
Andy Adamson557134a2009-04-01 09:21:53 -04004835#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04004836/*
Andy Adamson357f54d2010-12-14 10:11:57 -05004837 * Check the exchange flags returned by the server for invalid flags, having
4838 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
4839 * DS flags set.
4840 */
4841static int nfs4_check_cl_exchange_flags(u32 flags)
4842{
4843 if (flags & ~EXCHGID4_FLAG_MASK_R)
4844 goto out_inval;
4845 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
4846 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
4847 goto out_inval;
4848 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
4849 goto out_inval;
4850 return NFS_OK;
4851out_inval:
4852 return -NFS4ERR_INVAL;
4853}
4854
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004855static bool
4856nfs41_same_server_scope(struct server_scope *a, struct server_scope *b)
4857{
4858 if (a->server_scope_sz == b->server_scope_sz &&
4859 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
4860 return true;
4861
4862 return false;
4863}
4864
Andy Adamson357f54d2010-12-14 10:11:57 -05004865/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04004866 * nfs4_proc_exchange_id()
4867 *
4868 * Since the clientid has expired, all compounds using sessions
4869 * associated with the stale clientid will be returning
4870 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4871 * be in some phase of session reset.
4872 */
Andy Adamson4d643d12009-12-04 15:52:24 -05004873int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004874{
4875 nfs4_verifier verifier;
4876 struct nfs41_exchange_id_args args = {
4877 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05004878 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04004879 };
4880 struct nfs41_exchange_id_res res = {
4881 .client = clp,
4882 };
4883 int status;
4884 struct rpc_message msg = {
4885 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4886 .rpc_argp = &args,
4887 .rpc_resp = &res,
4888 .rpc_cred = cred,
4889 };
4890 __be32 *p;
4891
4892 dprintk("--> %s\n", __func__);
4893 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04004894
Benny Halevy99fe60d2009-04-01 09:22:29 -04004895 p = (u32 *)verifier.data;
4896 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4897 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4898 args.verifier = &verifier;
4899
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004900 args.id_len = scnprintf(args.id, sizeof(args.id),
4901 "%s/%s.%s/%u",
4902 clp->cl_ipaddr,
4903 init_utsname()->nodename,
4904 init_utsname()->domainname,
4905 clp->cl_rpcclient->cl_auth->au_flavor);
Benny Halevy99fe60d2009-04-01 09:22:29 -04004906
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004907 res.server_scope = kzalloc(sizeof(struct server_scope), GFP_KERNEL);
4908 if (unlikely(!res.server_scope))
4909 return -ENOMEM;
4910
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004911 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004912 if (!status)
4913 status = nfs4_check_cl_exchange_flags(clp->cl_exchange_flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004914
4915 if (!status) {
4916 if (clp->server_scope &&
4917 !nfs41_same_server_scope(clp->server_scope,
4918 res.server_scope)) {
4919 dprintk("%s: server_scope mismatch detected\n",
4920 __func__);
4921 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
4922 kfree(clp->server_scope);
4923 clp->server_scope = NULL;
4924 }
4925
4926 if (!clp->server_scope)
4927 clp->server_scope = res.server_scope;
4928 else
4929 kfree(res.server_scope);
4930 }
4931
Benny Halevy99fe60d2009-04-01 09:22:29 -04004932 dprintk("<-- %s status= %d\n", __func__, status);
4933 return status;
4934}
4935
Andy Adamson2050f0c2009-04-01 09:22:30 -04004936struct nfs4_get_lease_time_data {
4937 struct nfs4_get_lease_time_args *args;
4938 struct nfs4_get_lease_time_res *res;
4939 struct nfs_client *clp;
4940};
4941
4942static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4943 void *calldata)
4944{
4945 int ret;
4946 struct nfs4_get_lease_time_data *data =
4947 (struct nfs4_get_lease_time_data *)calldata;
4948
4949 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004950 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004951 /* just setup sequence, do not trigger session recovery
4952 since we're invoked within one */
4953 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004954 &data->args->la_seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004955 &data->res->lr_seq_res, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004956
4957 BUG_ON(ret == -EAGAIN);
4958 rpc_call_start(task);
4959 dprintk("<-- %s\n", __func__);
4960}
4961
4962/*
4963 * Called from nfs4_state_manager thread for session setup, so don't recover
4964 * from sequence operation or clientid errors.
4965 */
4966static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4967{
4968 struct nfs4_get_lease_time_data *data =
4969 (struct nfs4_get_lease_time_data *)calldata;
4970
4971 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04004972 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
4973 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04004974 switch (task->tk_status) {
4975 case -NFS4ERR_DELAY:
4976 case -NFS4ERR_GRACE:
4977 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4978 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4979 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004980 /* fall through */
4981 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004982 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004983 return;
4984 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04004985 dprintk("<-- %s\n", __func__);
4986}
4987
4988struct rpc_call_ops nfs4_get_lease_time_ops = {
4989 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4990 .rpc_call_done = nfs4_get_lease_time_done,
4991};
4992
4993int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4994{
4995 struct rpc_task *task;
4996 struct nfs4_get_lease_time_args args;
4997 struct nfs4_get_lease_time_res res = {
4998 .lr_fsinfo = fsinfo,
4999 };
5000 struct nfs4_get_lease_time_data data = {
5001 .args = &args,
5002 .res = &res,
5003 .clp = clp,
5004 };
5005 struct rpc_message msg = {
5006 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5007 .rpc_argp = &args,
5008 .rpc_resp = &res,
5009 };
5010 struct rpc_task_setup task_setup = {
5011 .rpc_client = clp->cl_rpcclient,
5012 .rpc_message = &msg,
5013 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005014 .callback_data = &data,
5015 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005016 };
5017 int status;
5018
Trond Myklebust9d12b212012-01-17 22:04:25 -05005019 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005020 dprintk("--> %s\n", __func__);
5021 task = rpc_run_task(&task_setup);
5022
5023 if (IS_ERR(task))
5024 status = PTR_ERR(task);
5025 else {
5026 status = task->tk_status;
5027 rpc_put_task(task);
5028 }
5029 dprintk("<-- %s return %d\n", __func__, status);
5030
5031 return status;
5032}
5033
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005034/*
5035 * Reset a slot table
5036 */
Andy Adamson104aeba2010-01-14 17:45:10 -05005037static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
5038 int ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005039{
Andy Adamson104aeba2010-01-14 17:45:10 -05005040 struct nfs4_slot *new = NULL;
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005041 int i;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005042 int ret = 0;
5043
Andy Adamson104aeba2010-01-14 17:45:10 -05005044 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5045 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005046
Andy Adamson104aeba2010-01-14 17:45:10 -05005047 /* Does the newly negotiated max_reqs match the existing slot table? */
5048 if (max_reqs != tbl->max_slots) {
5049 ret = -ENOMEM;
5050 new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005051 GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05005052 if (!new)
5053 goto out;
5054 ret = 0;
5055 kfree(tbl->slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005056 }
5057 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson104aeba2010-01-14 17:45:10 -05005058 if (new) {
5059 tbl->slots = new;
5060 tbl->max_slots = max_reqs;
5061 }
5062 for (i = 0; i < tbl->max_slots; ++i)
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005063 tbl->slots[i].seq_nr = ivalue;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005064 spin_unlock(&tbl->slot_tbl_lock);
5065 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5066 tbl, tbl->slots, tbl->max_slots);
5067out:
5068 dprintk("<-- %s: return %d\n", __func__, ret);
5069 return ret;
5070}
5071
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005072/* Destroy the slot table */
5073static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5074{
5075 if (session->fc_slot_table.slots != NULL) {
5076 kfree(session->fc_slot_table.slots);
5077 session->fc_slot_table.slots = NULL;
5078 }
5079 if (session->bc_slot_table.slots != NULL) {
5080 kfree(session->bc_slot_table.slots);
5081 session->bc_slot_table.slots = NULL;
5082 }
5083 return;
5084}
5085
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005086/*
Andy Adamsonfc931582009-04-01 09:22:31 -04005087 * Initialize slot table
5088 */
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005089static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
5090 int max_slots, int ivalue)
Andy Adamsonfc931582009-04-01 09:22:31 -04005091{
Andy Adamsonfc931582009-04-01 09:22:31 -04005092 struct nfs4_slot *slot;
5093 int ret = -ENOMEM;
5094
5095 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
5096
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005097 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
Andy Adamsonfc931582009-04-01 09:22:31 -04005098
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005099 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_NOFS);
Andy Adamsonfc931582009-04-01 09:22:31 -04005100 if (!slot)
5101 goto out;
Andy Adamsonfc931582009-04-01 09:22:31 -04005102 ret = 0;
5103
5104 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonfc931582009-04-01 09:22:31 -04005105 tbl->max_slots = max_slots;
5106 tbl->slots = slot;
5107 tbl->highest_used_slotid = -1; /* no slot is currently used */
5108 spin_unlock(&tbl->slot_tbl_lock);
5109 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5110 tbl, tbl->slots, tbl->max_slots);
5111out:
5112 dprintk("<-- %s: return %d\n", __func__, ret);
5113 return ret;
Andy Adamsonfc931582009-04-01 09:22:31 -04005114}
5115
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005116/*
Andy Adamsonaacd5532011-11-09 13:58:21 -05005117 * Initialize or reset the forechannel and backchannel tables
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005118 */
Andy Adamsonaacd5532011-11-09 13:58:21 -05005119static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005120{
Trond Myklebustf26468f2009-12-05 19:32:11 -05005121 struct nfs4_slot_table *tbl;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005122 int status;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005123
Andy Adamsonaacd5532011-11-09 13:58:21 -05005124 dprintk("--> %s\n", __func__);
5125 /* Fore channel */
5126 tbl = &ses->fc_slot_table;
Trond Myklebustf26468f2009-12-05 19:32:11 -05005127 if (tbl->slots == NULL) {
Andy Adamsonaacd5532011-11-09 13:58:21 -05005128 status = nfs4_init_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5129 if (status) /* -ENOMEM */
5130 return status;
5131 } else {
5132 status = nfs4_reset_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
Trond Myklebustf26468f2009-12-05 19:32:11 -05005133 if (status)
5134 return status;
5135 }
Andy Adamsonaacd5532011-11-09 13:58:21 -05005136 /* Back channel */
5137 tbl = &ses->bc_slot_table;
Trond Myklebustf26468f2009-12-05 19:32:11 -05005138 if (tbl->slots == NULL) {
Andy Adamsonaacd5532011-11-09 13:58:21 -05005139 status = nfs4_init_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
Trond Myklebustf26468f2009-12-05 19:32:11 -05005140 if (status)
Andy Adamsonaacd5532011-11-09 13:58:21 -05005141 /* Fore and back channel share a connection so get
5142 * both slot tables or neither */
5143 nfs4_destroy_slot_tables(ses);
5144 } else
5145 status = nfs4_reset_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005146 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04005147}
5148
5149struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5150{
5151 struct nfs4_session *session;
5152 struct nfs4_slot_table *tbl;
5153
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005154 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04005155 if (!session)
5156 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04005157
Andy Adamson557134a2009-04-01 09:21:53 -04005158 tbl = &session->fc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05005159 tbl->highest_used_slotid = -1;
Andy Adamson557134a2009-04-01 09:21:53 -04005160 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005161 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005162 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005163
5164 tbl = &session->bc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05005165 tbl->highest_used_slotid = -1;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005166 spin_lock_init(&tbl->slot_tbl_lock);
5167 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005168 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005169
Trond Myklebust1055d762010-06-16 09:52:27 -04005170 session->session_state = 1<<NFS4_SESSION_INITING;
5171
Andy Adamson557134a2009-04-01 09:21:53 -04005172 session->clp = clp;
5173 return session;
5174}
5175
5176void nfs4_destroy_session(struct nfs4_session *session)
5177{
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005178 nfs4_proc_destroy_session(session);
5179 dprintk("%s Destroy backchannel for xprt %p\n",
5180 __func__, session->clp->cl_rpcclient->cl_xprt);
5181 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
5182 NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005183 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04005184 kfree(session);
5185}
5186
Andy Adamsonfc931582009-04-01 09:22:31 -04005187/*
5188 * Initialize the values to be used by the client in CREATE_SESSION
5189 * If nfs4_init_session set the fore channel request and response sizes,
5190 * use them.
5191 *
5192 * Set the back channel max_resp_sz_cached to zero to force the client to
5193 * always set csa_cachethis to FALSE because the current implementation
5194 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5195 */
5196static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5197{
5198 struct nfs4_session *session = args->client->cl_session;
5199 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5200 mxresp_sz = session->fc_attrs.max_resp_sz;
5201
5202 if (mxrqst_sz == 0)
5203 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5204 if (mxresp_sz == 0)
5205 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5206 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005207 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5208 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005209 args->fc_attrs.max_ops = NFS4_MAX_OPS;
5210 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
5211
5212 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005213 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005214 __func__,
5215 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005216 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005217
5218 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005219 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5220 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5221 args->bc_attrs.max_resp_sz_cached = 0;
5222 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5223 args->bc_attrs.max_reqs = 1;
5224
5225 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5226 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5227 __func__,
5228 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5229 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5230 args->bc_attrs.max_reqs);
5231}
5232
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005233static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005234{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005235 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5236 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5237
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005238 if (rcvd->max_resp_sz > sent->max_resp_sz)
5239 return -EINVAL;
5240 /*
5241 * Our requested max_ops is the minimum we need; we're not
5242 * prepared to break up compounds into smaller pieces than that.
5243 * So, no point even trying to continue if the server won't
5244 * cooperate:
5245 */
5246 if (rcvd->max_ops < sent->max_ops)
5247 return -EINVAL;
5248 if (rcvd->max_reqs == 0)
5249 return -EINVAL;
5250 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005251}
5252
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005253static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5254{
5255 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5256 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005257
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005258 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5259 return -EINVAL;
5260 if (rcvd->max_resp_sz < sent->max_resp_sz)
5261 return -EINVAL;
5262 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5263 return -EINVAL;
5264 /* These would render the backchannel useless: */
5265 if (rcvd->max_ops == 0)
5266 return -EINVAL;
5267 if (rcvd->max_reqs == 0)
5268 return -EINVAL;
5269 return 0;
5270}
Andy Adamson8d353012009-04-01 09:22:32 -04005271
Andy Adamson8d353012009-04-01 09:22:32 -04005272static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5273 struct nfs4_session *session)
5274{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005275 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005276
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005277 ret = nfs4_verify_fore_channel_attrs(args, session);
5278 if (ret)
5279 return ret;
5280 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005281}
5282
Andy Adamsonfc931582009-04-01 09:22:31 -04005283static int _nfs4_proc_create_session(struct nfs_client *clp)
5284{
5285 struct nfs4_session *session = clp->cl_session;
5286 struct nfs41_create_session_args args = {
5287 .client = clp,
5288 .cb_program = NFS4_CALLBACK,
5289 };
5290 struct nfs41_create_session_res res = {
5291 .client = clp,
5292 };
5293 struct rpc_message msg = {
5294 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5295 .rpc_argp = &args,
5296 .rpc_resp = &res,
5297 };
5298 int status;
5299
5300 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005301 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005302
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005303 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005304
Andy Adamson8d353012009-04-01 09:22:32 -04005305 if (!status)
5306 /* Verify the session's negotiated channel_attrs values */
5307 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005308 if (!status) {
5309 /* Increment the clientid slot sequence id */
5310 clp->cl_seqid++;
5311 }
5312
5313 return status;
5314}
5315
5316/*
5317 * Issues a CREATE_SESSION operation to the server.
5318 * It is the responsibility of the caller to verify the session is
5319 * expired before calling this routine.
5320 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05005321int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04005322{
5323 int status;
5324 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005325 struct nfs4_session *session = clp->cl_session;
5326
5327 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5328
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005329 status = _nfs4_proc_create_session(clp);
Andy Adamsonfc931582009-04-01 09:22:31 -04005330 if (status)
5331 goto out;
5332
Andy Adamsonaacd5532011-11-09 13:58:21 -05005333 /* Init or reset the session slot tables */
5334 status = nfs4_setup_session_slot_tables(session);
5335 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005336 if (status)
5337 goto out;
5338
5339 ptr = (unsigned *)&session->sess_id.data[0];
5340 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5341 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005342out:
5343 dprintk("<-- %s\n", __func__);
5344 return status;
5345}
5346
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005347/*
5348 * Issue the over-the-wire RPC DESTROY_SESSION.
5349 * The caller must serialize access to this routine.
5350 */
5351int nfs4_proc_destroy_session(struct nfs4_session *session)
5352{
5353 int status = 0;
5354 struct rpc_message msg;
5355
5356 dprintk("--> nfs4_proc_destroy_session\n");
5357
5358 /* session is still being setup */
5359 if (session->clp->cl_cons_state != NFS_CS_READY)
5360 return status;
5361
5362 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5363 msg.rpc_argp = session;
5364 msg.rpc_resp = NULL;
5365 msg.rpc_cred = NULL;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005366 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005367
5368 if (status)
5369 printk(KERN_WARNING
5370 "Got error %d from the server on DESTROY_SESSION. "
5371 "Session has been destroyed regardless...\n", status);
5372
5373 dprintk("<-- nfs4_proc_destroy_session\n");
5374 return status;
5375}
5376
Trond Myklebustfccba802009-07-21 16:48:07 -04005377int nfs4_init_session(struct nfs_server *server)
5378{
5379 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005380 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005381 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005382 int ret;
5383
5384 if (!nfs4_has_session(clp))
5385 return 0;
5386
Trond Myklebust1055d762010-06-16 09:52:27 -04005387 session = clp->cl_session;
5388 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5389 return 0;
5390
Andy Adamson68bf05e2009-12-15 12:55:02 -05005391 rsize = server->rsize;
5392 if (rsize == 0)
5393 rsize = NFS_MAX_FILE_IO_SIZE;
5394 wsize = server->wsize;
5395 if (wsize == 0)
5396 wsize = NFS_MAX_FILE_IO_SIZE;
5397
Andy Adamson68bf05e2009-12-15 12:55:02 -05005398 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5399 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005400
Trond Myklebustfccba802009-07-21 16:48:07 -04005401 ret = nfs4_recover_expired_lease(server);
5402 if (!ret)
5403 ret = nfs4_check_client_ready(clp);
5404 return ret;
5405}
5406
Andy Adamsond83217c2011-03-01 01:34:17 +00005407int nfs4_init_ds_session(struct nfs_client *clp)
5408{
5409 struct nfs4_session *session = clp->cl_session;
5410 int ret;
5411
5412 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5413 return 0;
5414
5415 ret = nfs4_client_recover_expired_lease(clp);
5416 if (!ret)
5417 /* Test for the DS role */
5418 if (!is_ds_client(clp))
5419 ret = -ENODEV;
5420 if (!ret)
5421 ret = nfs4_check_client_ready(clp);
5422 return ret;
5423
5424}
5425EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5426
5427
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005428/*
5429 * Renew the cl_session lease.
5430 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005431struct nfs4_sequence_data {
5432 struct nfs_client *clp;
5433 struct nfs4_sequence_args args;
5434 struct nfs4_sequence_res res;
5435};
5436
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005437static void nfs41_sequence_release(void *data)
5438{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005439 struct nfs4_sequence_data *calldata = data;
5440 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005441
Alexandros Batsakis71358402010-02-05 03:45:05 -08005442 if (atomic_read(&clp->cl_count) > 1)
5443 nfs4_schedule_state_renewal(clp);
5444 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005445 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005446}
5447
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005448static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5449{
5450 switch(task->tk_status) {
5451 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005452 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5453 return -EAGAIN;
5454 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005455 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005456 }
5457 return 0;
5458}
5459
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005460static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005461{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005462 struct nfs4_sequence_data *calldata = data;
5463 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005464
Trond Myklebust14516c32010-07-31 14:29:06 -04005465 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5466 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005467
5468 if (task->tk_status < 0) {
5469 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005470 if (atomic_read(&clp->cl_count) == 1)
5471 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005472
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005473 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5474 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005475 return;
5476 }
5477 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005478 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005479out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005480 dprintk("<-- %s\n", __func__);
5481}
5482
5483static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5484{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005485 struct nfs4_sequence_data *calldata = data;
5486 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005487 struct nfs4_sequence_args *args;
5488 struct nfs4_sequence_res *res;
5489
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005490 args = task->tk_msg.rpc_argp;
5491 res = task->tk_msg.rpc_resp;
5492
Trond Myklebust9d12b212012-01-17 22:04:25 -05005493 if (nfs41_setup_sequence(clp->cl_session, args, res, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005494 return;
5495 rpc_call_start(task);
5496}
5497
5498static const struct rpc_call_ops nfs41_sequence_ops = {
5499 .rpc_call_done = nfs41_sequence_call_done,
5500 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005501 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005502};
5503
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005504static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005505{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005506 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005507 struct rpc_message msg = {
5508 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5509 .rpc_cred = cred,
5510 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005511 struct rpc_task_setup task_setup_data = {
5512 .rpc_client = clp->cl_rpcclient,
5513 .rpc_message = &msg,
5514 .callback_ops = &nfs41_sequence_ops,
5515 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5516 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005517
Alexandros Batsakis71358402010-02-05 03:45:05 -08005518 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005519 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005520 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005521 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005522 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005523 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005524 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05005525 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005526 msg.rpc_argp = &calldata->args;
5527 msg.rpc_resp = &calldata->res;
5528 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005529 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005530
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005531 return rpc_run_task(&task_setup_data);
5532}
5533
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005534static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005535{
5536 struct rpc_task *task;
5537 int ret = 0;
5538
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005539 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5540 return 0;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005541 task = _nfs41_proc_sequence(clp, cred);
5542 if (IS_ERR(task))
5543 ret = PTR_ERR(task);
5544 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005545 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005546 dprintk("<-- %s status=%d\n", __func__, ret);
5547 return ret;
5548}
5549
5550static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5551{
5552 struct rpc_task *task;
5553 int ret;
5554
5555 task = _nfs41_proc_sequence(clp, cred);
5556 if (IS_ERR(task)) {
5557 ret = PTR_ERR(task);
5558 goto out;
5559 }
5560 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005561 if (!ret) {
5562 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5563
5564 if (task->tk_status == 0)
5565 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005566 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005567 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005568 rpc_put_task(task);
5569out:
5570 dprintk("<-- %s status=%d\n", __func__, ret);
5571 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005572}
5573
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005574struct nfs4_reclaim_complete_data {
5575 struct nfs_client *clp;
5576 struct nfs41_reclaim_complete_args arg;
5577 struct nfs41_reclaim_complete_res res;
5578};
5579
5580static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5581{
5582 struct nfs4_reclaim_complete_data *calldata = data;
5583
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005584 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04005585 if (nfs41_setup_sequence(calldata->clp->cl_session,
5586 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005587 &calldata->res.seq_res, task))
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005588 return;
5589
5590 rpc_call_start(task);
5591}
5592
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005593static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5594{
5595 switch(task->tk_status) {
5596 case 0:
5597 case -NFS4ERR_COMPLETE_ALREADY:
5598 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5599 break;
5600 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005601 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005602 /* fall through */
5603 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005604 return -EAGAIN;
5605 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005606 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005607 }
5608 return 0;
5609}
5610
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005611static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5612{
5613 struct nfs4_reclaim_complete_data *calldata = data;
5614 struct nfs_client *clp = calldata->clp;
5615 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5616
5617 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005618 if (!nfs41_sequence_done(task, res))
5619 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005620
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005621 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5622 rpc_restart_call_prepare(task);
5623 return;
5624 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005625 dprintk("<-- %s\n", __func__);
5626}
5627
5628static void nfs4_free_reclaim_complete_data(void *data)
5629{
5630 struct nfs4_reclaim_complete_data *calldata = data;
5631
5632 kfree(calldata);
5633}
5634
5635static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5636 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5637 .rpc_call_done = nfs4_reclaim_complete_done,
5638 .rpc_release = nfs4_free_reclaim_complete_data,
5639};
5640
5641/*
5642 * Issue a global reclaim complete.
5643 */
5644static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5645{
5646 struct nfs4_reclaim_complete_data *calldata;
5647 struct rpc_task *task;
5648 struct rpc_message msg = {
5649 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5650 };
5651 struct rpc_task_setup task_setup_data = {
5652 .rpc_client = clp->cl_rpcclient,
5653 .rpc_message = &msg,
5654 .callback_ops = &nfs4_reclaim_complete_call_ops,
5655 .flags = RPC_TASK_ASYNC,
5656 };
5657 int status = -ENOMEM;
5658
5659 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005660 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005661 if (calldata == NULL)
5662 goto out;
5663 calldata->clp = clp;
5664 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005665
Trond Myklebust9d12b212012-01-17 22:04:25 -05005666 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005667 msg.rpc_argp = &calldata->arg;
5668 msg.rpc_resp = &calldata->res;
5669 task_setup_data.callback_data = calldata;
5670 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005671 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005672 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005673 goto out;
5674 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05005675 status = nfs4_wait_for_completion_rpc_task(task);
5676 if (status == 0)
5677 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005678 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005679 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005680out:
5681 dprintk("<-- %s status=%d\n", __func__, status);
5682 return status;
5683}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005684
5685static void
5686nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5687{
5688 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00005689 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005690
5691 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00005692 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5693 * right now covering the LAYOUTGET we are about to send.
5694 * However, that is not so catastrophic, and there seems
5695 * to be no way to prevent it completely.
5696 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005697 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005698 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005699 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00005700 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5701 NFS_I(lgp->args.inode)->layout,
5702 lgp->args.ctx->state)) {
5703 rpc_exit(task, NFS4_OK);
5704 return;
5705 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005706 rpc_call_start(task);
5707}
5708
5709static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5710{
5711 struct nfs4_layoutget *lgp = calldata;
5712 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5713
5714 dprintk("--> %s\n", __func__);
5715
5716 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
5717 return;
5718
5719 switch (task->tk_status) {
5720 case 0:
5721 break;
5722 case -NFS4ERR_LAYOUTTRYLATER:
5723 case -NFS4ERR_RECALLCONFLICT:
5724 task->tk_status = -NFS4ERR_DELAY;
5725 /* Fall through */
5726 default:
5727 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5728 rpc_restart_call_prepare(task);
5729 return;
5730 }
5731 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005732 dprintk("<-- %s\n", __func__);
5733}
5734
5735static void nfs4_layoutget_release(void *calldata)
5736{
5737 struct nfs4_layoutget *lgp = calldata;
5738
5739 dprintk("--> %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005740 put_nfs_open_context(lgp->args.ctx);
5741 kfree(calldata);
5742 dprintk("<-- %s\n", __func__);
5743}
5744
5745static const struct rpc_call_ops nfs4_layoutget_call_ops = {
5746 .rpc_call_prepare = nfs4_layoutget_prepare,
5747 .rpc_call_done = nfs4_layoutget_done,
5748 .rpc_release = nfs4_layoutget_release,
5749};
5750
5751int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
5752{
5753 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5754 struct rpc_task *task;
5755 struct rpc_message msg = {
5756 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
5757 .rpc_argp = &lgp->args,
5758 .rpc_resp = &lgp->res,
5759 };
5760 struct rpc_task_setup task_setup_data = {
5761 .rpc_client = server->client,
5762 .rpc_message = &msg,
5763 .callback_ops = &nfs4_layoutget_call_ops,
5764 .callback_data = lgp,
5765 .flags = RPC_TASK_ASYNC,
5766 };
5767 int status = 0;
5768
5769 dprintk("--> %s\n", __func__);
5770
Weston Andros Adamson35124a02011-03-24 16:48:21 -04005771 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005772 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05005773 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005774 task = rpc_run_task(&task_setup_data);
5775 if (IS_ERR(task))
5776 return PTR_ERR(task);
5777 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00005778 if (status == 0)
5779 status = task->tk_status;
5780 if (status == 0)
5781 status = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005782 rpc_put_task(task);
5783 dprintk("<-- %s status=%d\n", __func__, status);
5784 return status;
5785}
5786
Benny Halevycbe82602011-05-22 19:52:37 +03005787static void
5788nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
5789{
5790 struct nfs4_layoutreturn *lrp = calldata;
5791
5792 dprintk("--> %s\n", __func__);
5793 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005794 &lrp->res.seq_res, task))
Benny Halevycbe82602011-05-22 19:52:37 +03005795 return;
5796 rpc_call_start(task);
5797}
5798
5799static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
5800{
5801 struct nfs4_layoutreturn *lrp = calldata;
5802 struct nfs_server *server;
Trond Myklebusta56aaa02011-06-15 11:59:10 -04005803 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03005804
5805 dprintk("--> %s\n", __func__);
5806
5807 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
5808 return;
5809
5810 server = NFS_SERVER(lrp->args.inode);
5811 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005812 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03005813 return;
5814 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04005815 spin_lock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03005816 if (task->tk_status == 0) {
Benny Halevycbe82602011-05-22 19:52:37 +03005817 if (lrp->res.lrs_present) {
Benny Halevycbe82602011-05-22 19:52:37 +03005818 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Benny Halevycbe82602011-05-22 19:52:37 +03005819 } else
5820 BUG_ON(!list_empty(&lo->plh_segs));
5821 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04005822 lo->plh_block_lgets--;
5823 spin_unlock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03005824 dprintk("<-- %s\n", __func__);
5825}
5826
5827static void nfs4_layoutreturn_release(void *calldata)
5828{
5829 struct nfs4_layoutreturn *lrp = calldata;
5830
5831 dprintk("--> %s\n", __func__);
Trond Myklebusta56aaa02011-06-15 11:59:10 -04005832 put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03005833 kfree(calldata);
5834 dprintk("<-- %s\n", __func__);
5835}
5836
5837static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
5838 .rpc_call_prepare = nfs4_layoutreturn_prepare,
5839 .rpc_call_done = nfs4_layoutreturn_done,
5840 .rpc_release = nfs4_layoutreturn_release,
5841};
5842
5843int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
5844{
5845 struct rpc_task *task;
5846 struct rpc_message msg = {
5847 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
5848 .rpc_argp = &lrp->args,
5849 .rpc_resp = &lrp->res,
5850 };
5851 struct rpc_task_setup task_setup_data = {
5852 .rpc_client = lrp->clp->cl_rpcclient,
5853 .rpc_message = &msg,
5854 .callback_ops = &nfs4_layoutreturn_call_ops,
5855 .callback_data = lrp,
5856 };
5857 int status;
5858
5859 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05005860 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03005861 task = rpc_run_task(&task_setup_data);
5862 if (IS_ERR(task))
5863 return PTR_ERR(task);
5864 status = task->tk_status;
5865 dprintk("<-- %s status=%d\n", __func__, status);
5866 rpc_put_task(task);
5867 return status;
5868}
5869
Andy Adamson7f11d8d2011-07-30 20:52:35 -04005870/*
5871 * Retrieve the list of Data Server devices from the MDS.
5872 */
5873static int _nfs4_getdevicelist(struct nfs_server *server,
5874 const struct nfs_fh *fh,
5875 struct pnfs_devicelist *devlist)
5876{
5877 struct nfs4_getdevicelist_args args = {
5878 .fh = fh,
5879 .layoutclass = server->pnfs_curr_ld->id,
5880 };
5881 struct nfs4_getdevicelist_res res = {
5882 .devlist = devlist,
5883 };
5884 struct rpc_message msg = {
5885 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
5886 .rpc_argp = &args,
5887 .rpc_resp = &res,
5888 };
5889 int status;
5890
5891 dprintk("--> %s\n", __func__);
5892 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
5893 &res.seq_res, 0);
5894 dprintk("<-- %s status=%d\n", __func__, status);
5895 return status;
5896}
5897
5898int nfs4_proc_getdevicelist(struct nfs_server *server,
5899 const struct nfs_fh *fh,
5900 struct pnfs_devicelist *devlist)
5901{
5902 struct nfs4_exception exception = { };
5903 int err;
5904
5905 do {
5906 err = nfs4_handle_exception(server,
5907 _nfs4_getdevicelist(server, fh, devlist),
5908 &exception);
5909 } while (exception.retry);
5910
5911 dprintk("%s: err=%d, num_devs=%u\n", __func__,
5912 err, devlist->num_devs);
5913
5914 return err;
5915}
5916EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
5917
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005918static int
5919_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5920{
5921 struct nfs4_getdeviceinfo_args args = {
5922 .pdev = pdev,
5923 };
5924 struct nfs4_getdeviceinfo_res res = {
5925 .pdev = pdev,
5926 };
5927 struct rpc_message msg = {
5928 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
5929 .rpc_argp = &args,
5930 .rpc_resp = &res,
5931 };
5932 int status;
5933
5934 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005935 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005936 dprintk("<-- %s status=%d\n", __func__, status);
5937
5938 return status;
5939}
5940
5941int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5942{
5943 struct nfs4_exception exception = { };
5944 int err;
5945
5946 do {
5947 err = nfs4_handle_exception(server,
5948 _nfs4_proc_getdeviceinfo(server, pdev),
5949 &exception);
5950 } while (exception.retry);
5951 return err;
5952}
5953EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
5954
Andy Adamson863a3c62011-03-23 13:27:54 +00005955static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
5956{
5957 struct nfs4_layoutcommit_data *data = calldata;
5958 struct nfs_server *server = NFS_SERVER(data->args.inode);
5959
5960 if (nfs4_setup_sequence(server, &data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005961 &data->res.seq_res, task))
Andy Adamson863a3c62011-03-23 13:27:54 +00005962 return;
5963 rpc_call_start(task);
5964}
5965
5966static void
5967nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
5968{
5969 struct nfs4_layoutcommit_data *data = calldata;
5970 struct nfs_server *server = NFS_SERVER(data->args.inode);
5971
5972 if (!nfs4_sequence_done(task, &data->res.seq_res))
5973 return;
5974
5975 switch (task->tk_status) { /* Just ignore these failures */
5976 case NFS4ERR_DELEG_REVOKED: /* layout was recalled */
5977 case NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
5978 case NFS4ERR_BADLAYOUT: /* no layout */
5979 case NFS4ERR_GRACE: /* loca_recalim always false */
5980 task->tk_status = 0;
5981 }
5982
5983 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005984 rpc_restart_call_prepare(task);
Andy Adamson863a3c62011-03-23 13:27:54 +00005985 return;
5986 }
5987
5988 if (task->tk_status == 0)
5989 nfs_post_op_update_inode_force_wcc(data->args.inode,
5990 data->res.fattr);
5991}
5992
5993static void nfs4_layoutcommit_release(void *calldata)
5994{
5995 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04005996 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07005997 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00005998
Andy Adamsondb29c082011-07-30 20:52:38 -04005999 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006000 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04006001 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6002 list_del_init(&lseg->pls_lc_list);
6003 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6004 &lseg->pls_flags))
6005 put_lseg(lseg);
6006 }
Peng Tao92407e72011-10-23 20:21:17 -07006007
6008 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6009 smp_mb__after_clear_bit();
6010 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6011
Andy Adamson863a3c62011-03-23 13:27:54 +00006012 put_rpccred(data->cred);
6013 kfree(data);
6014}
6015
6016static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6017 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6018 .rpc_call_done = nfs4_layoutcommit_done,
6019 .rpc_release = nfs4_layoutcommit_release,
6020};
6021
6022int
Andy Adamsonef311532011-03-12 02:58:10 -05006023nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006024{
6025 struct rpc_message msg = {
6026 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6027 .rpc_argp = &data->args,
6028 .rpc_resp = &data->res,
6029 .rpc_cred = data->cred,
6030 };
6031 struct rpc_task_setup task_setup_data = {
6032 .task = &data->task,
6033 .rpc_client = NFS_CLIENT(data->args.inode),
6034 .rpc_message = &msg,
6035 .callback_ops = &nfs4_layoutcommit_ops,
6036 .callback_data = data,
6037 .flags = RPC_TASK_ASYNC,
6038 };
6039 struct rpc_task *task;
6040 int status = 0;
6041
6042 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6043 "lbw: %llu inode %lu\n",
6044 data->task.tk_pid, sync,
6045 data->args.lastbytewritten,
6046 data->args.inode->i_ino);
6047
Trond Myklebust9d12b212012-01-17 22:04:25 -05006048 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006049 task = rpc_run_task(&task_setup_data);
6050 if (IS_ERR(task))
6051 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006052 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006053 goto out;
6054 status = nfs4_wait_for_completion_rpc_task(task);
6055 if (status != 0)
6056 goto out;
6057 status = task->tk_status;
6058out:
6059 dprintk("%s: status %d\n", __func__, status);
6060 rpc_put_task(task);
6061 return status;
6062}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006063
6064static int
6065_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6066 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6067{
6068 struct nfs41_secinfo_no_name_args args = {
6069 .style = SECINFO_STYLE_CURRENT_FH,
6070 };
6071 struct nfs4_secinfo_res res = {
6072 .flavors = flavors,
6073 };
6074 struct rpc_message msg = {
6075 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6076 .rpc_argp = &args,
6077 .rpc_resp = &res,
6078 };
6079 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6080}
6081
6082static int
6083nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6084 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6085{
6086 struct nfs4_exception exception = { };
6087 int err;
6088 do {
6089 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6090 switch (err) {
6091 case 0:
6092 case -NFS4ERR_WRONGSEC:
6093 case -NFS4ERR_NOTSUPP:
6094 break;
6095 default:
6096 err = nfs4_handle_exception(server, err, &exception);
6097 }
6098 } while (exception.retry);
6099 return err;
6100}
6101
6102static int
6103nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6104 struct nfs_fsinfo *info)
6105{
6106 int err;
6107 struct page *page;
6108 rpc_authflavor_t flavor;
6109 struct nfs4_secinfo_flavors *flavors;
6110
6111 page = alloc_page(GFP_KERNEL);
6112 if (!page) {
6113 err = -ENOMEM;
6114 goto out;
6115 }
6116
6117 flavors = page_address(page);
6118 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6119
6120 /*
6121 * Fall back on "guess and check" method if
6122 * the server doesn't support SECINFO_NO_NAME
6123 */
6124 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6125 err = nfs4_find_root_sec(server, fhandle, info);
6126 goto out_freepage;
6127 }
6128 if (err)
6129 goto out_freepage;
6130
6131 flavor = nfs_find_best_sec(flavors);
6132 if (err == 0)
6133 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6134
6135out_freepage:
6136 put_page(page);
6137 if (err == -EACCES)
6138 return -EPERM;
6139out:
6140 return err;
6141}
Bryan Schumaker7d974792011-06-02 14:59:08 -04006142static int _nfs41_test_stateid(struct nfs_server *server, struct nfs4_state *state)
6143{
Bryan Schumaker7d974792011-06-02 14:59:08 -04006144 struct nfs41_test_stateid_args args = {
6145 .stateid = &state->stateid,
6146 };
6147 struct nfs41_test_stateid_res res;
6148 struct rpc_message msg = {
6149 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6150 .rpc_argp = &args,
6151 .rpc_resp = &res,
6152 };
Trond Myklebust9d12b212012-01-17 22:04:25 -05006153 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6154 return nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Bryan Schumaker7d974792011-06-02 14:59:08 -04006155}
6156
6157static int nfs41_test_stateid(struct nfs_server *server, struct nfs4_state *state)
6158{
6159 struct nfs4_exception exception = { };
6160 int err;
6161 do {
6162 err = nfs4_handle_exception(server,
6163 _nfs41_test_stateid(server, state),
6164 &exception);
6165 } while (exception.retry);
6166 return err;
6167}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006168
6169static int _nfs4_free_stateid(struct nfs_server *server, struct nfs4_state *state)
6170{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006171 struct nfs41_free_stateid_args args = {
6172 .stateid = &state->stateid,
6173 };
6174 struct nfs41_free_stateid_res res;
6175 struct rpc_message msg = {
6176 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6177 .rpc_argp = &args,
6178 .rpc_resp = &res,
6179 };
6180
Trond Myklebust9d12b212012-01-17 22:04:25 -05006181 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6182 return nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006183}
6184
6185static int nfs41_free_stateid(struct nfs_server *server, struct nfs4_state *state)
6186{
6187 struct nfs4_exception exception = { };
6188 int err;
6189 do {
6190 err = nfs4_handle_exception(server,
6191 _nfs4_free_stateid(server, state),
6192 &exception);
6193 } while (exception.retry);
6194 return err;
6195}
Andy Adamson557134a2009-04-01 09:21:53 -04006196#endif /* CONFIG_NFS_V4_1 */
6197
Andy Adamson591d71c2009-04-01 09:22:47 -04006198struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006199 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006200 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006201 .recover_open = nfs4_open_reclaim,
6202 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006203 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006204 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006205};
6206
Andy Adamson591d71c2009-04-01 09:22:47 -04006207#if defined(CONFIG_NFS_V4_1)
6208struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
6209 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6210 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6211 .recover_open = nfs4_open_reclaim,
6212 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006213 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006214 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006215 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04006216};
6217#endif /* CONFIG_NFS_V4_1 */
6218
6219struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006220 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006221 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006222 .recover_open = nfs4_open_expired,
6223 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006224 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006225 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006226};
6227
Andy Adamson591d71c2009-04-01 09:22:47 -04006228#if defined(CONFIG_NFS_V4_1)
6229struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
6230 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6231 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006232 .recover_open = nfs41_open_expired,
6233 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006234 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006235 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006236};
6237#endif /* CONFIG_NFS_V4_1 */
6238
Benny Halevy29fba382009-04-01 09:22:44 -04006239struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
6240 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006241 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006242 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006243};
6244
6245#if defined(CONFIG_NFS_V4_1)
6246struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
6247 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006248 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006249 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006250};
6251#endif
6252
Trond Myklebust97dc1352010-06-16 09:52:26 -04006253static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6254 .minor_version = 0,
6255 .call_sync = _nfs4_call_sync,
Trond Myklebuste047a102010-06-16 09:52:27 -04006256 .validate_stateid = nfs4_validate_delegation_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006257 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006258 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6259 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6260 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006261};
6262
6263#if defined(CONFIG_NFS_V4_1)
6264static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6265 .minor_version = 1,
6266 .call_sync = _nfs4_call_sync_session,
Trond Myklebuste047a102010-06-16 09:52:27 -04006267 .validate_stateid = nfs41_validate_delegation_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006268 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006269 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6270 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6271 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006272};
6273#endif
6274
Trond Myklebust97dc1352010-06-16 09:52:26 -04006275const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6276 [0] = &nfs_v4_0_minor_ops,
6277#if defined(CONFIG_NFS_V4_1)
6278 [1] = &nfs_v4_1_minor_ops,
6279#endif
6280};
6281
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006282static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006283 .permission = nfs_permission,
6284 .getattr = nfs_getattr,
6285 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006286 .getxattr = generic_getxattr,
6287 .setxattr = generic_setxattr,
6288 .listxattr = generic_listxattr,
6289 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006290};
6291
David Howells509de812006-08-22 20:06:11 -04006292const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006293 .version = 4, /* protocol version */
6294 .dentry_ops = &nfs4_dentry_operations,
6295 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006296 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006297 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006298 .getroot = nfs4_proc_get_root,
6299 .getattr = nfs4_proc_getattr,
6300 .setattr = nfs4_proc_setattr,
6301 .lookup = nfs4_proc_lookup,
6302 .access = nfs4_proc_access,
6303 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006304 .create = nfs4_proc_create,
6305 .remove = nfs4_proc_remove,
6306 .unlink_setup = nfs4_proc_unlink_setup,
6307 .unlink_done = nfs4_proc_unlink_done,
6308 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006309 .rename_setup = nfs4_proc_rename_setup,
6310 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006311 .link = nfs4_proc_link,
6312 .symlink = nfs4_proc_symlink,
6313 .mkdir = nfs4_proc_mkdir,
6314 .rmdir = nfs4_proc_remove,
6315 .readdir = nfs4_proc_readdir,
6316 .mknod = nfs4_proc_mknod,
6317 .statfs = nfs4_proc_statfs,
6318 .fsinfo = nfs4_proc_fsinfo,
6319 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006320 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006321 .decode_dirent = nfs4_decode_dirent,
6322 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05006323 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006324 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006325 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006326 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006327 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006328 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006329 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006330 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006331 .open_context = nfs4_atomic_open,
Andy Adamson45a52a02011-03-01 01:34:08 +00006332 .init_client = nfs4_init_client,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006333 .secinfo = nfs4_proc_secinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006334};
6335
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006336static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6337 .prefix = XATTR_NAME_NFSV4_ACL,
6338 .list = nfs4_xattr_list_nfs4_acl,
6339 .get = nfs4_xattr_get_nfs4_acl,
6340 .set = nfs4_xattr_set_nfs4_acl,
6341};
6342
6343const struct xattr_handler *nfs4_xattr_handlers[] = {
6344 &nfs4_xattr_nfs4_acl_handler,
6345 NULL
6346};
6347
Linus Torvalds1da177e2005-04-16 15:20:36 -07006348/*
6349 * Local variables:
6350 * c-basic-offset: 8
6351 * End:
6352 */