blob: 0168007b78d2e9952370e8b33378718796d3e80c [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090042#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/sunrpc/clnt.h>
44#include <linux/nfs.h>
45#include <linux/nfs4.h>
46#include <linux/nfs_fs.h>
47#include <linux/nfs_page.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070049#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040050#include <linux/module.h>
Andy Adamson5a0ffe52009-04-01 09:23:18 -040051#include <linux/sunrpc/bc_xprt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Trond Myklebust4ce79712005-06-22 17:16:21 +000053#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050055#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050056#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040057#include "callback.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59#define NFSDBG_FACILITY NFSDBG_PROC
60
Trond Myklebust2066fe82006-09-15 08:30:46 -040061#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#define NFS4_POLL_RETRY_MAX (15*HZ)
63
Trond Myklebusta78cb572009-08-09 15:06:19 -040064#define NFS4_MAX_LOOP_ON_RECOVER (10)
65
Trond Myklebustcdd4e682006-01-03 09:55:12 +010066struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010067static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080068static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050070static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Trond Myklebust99367812007-07-17 21:52:41 -040071static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
72static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040073static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
74 struct nfs_fattr *fattr, struct iattr *sattr,
75 struct nfs4_state *state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Linus Torvalds1da177e2005-04-16 15:20:36 -070077/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050078static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
Trond Myklebust52567b02009-10-23 14:46:42 -040080 if (err >= -1000)
81 return err;
82 switch (err) {
83 case -NFS4ERR_RESOURCE:
84 return -EREMOTEIO;
85 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -070087 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -040088 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 }
Trond Myklebust52567b02009-10-23 14:46:42 -040090 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
92
93/*
94 * This is our standard bitmap for GETATTR requests.
95 */
96const u32 nfs4_fattr_bitmap[2] = {
97 FATTR4_WORD0_TYPE
98 | FATTR4_WORD0_CHANGE
99 | FATTR4_WORD0_SIZE
100 | FATTR4_WORD0_FSID
101 | FATTR4_WORD0_FILEID,
102 FATTR4_WORD1_MODE
103 | FATTR4_WORD1_NUMLINKS
104 | FATTR4_WORD1_OWNER
105 | FATTR4_WORD1_OWNER_GROUP
106 | FATTR4_WORD1_RAWDEV
107 | FATTR4_WORD1_SPACE_USED
108 | FATTR4_WORD1_TIME_ACCESS
109 | FATTR4_WORD1_TIME_METADATA
110 | FATTR4_WORD1_TIME_MODIFY
111};
112
113const u32 nfs4_statfs_bitmap[2] = {
114 FATTR4_WORD0_FILES_AVAIL
115 | FATTR4_WORD0_FILES_FREE
116 | FATTR4_WORD0_FILES_TOTAL,
117 FATTR4_WORD1_SPACE_AVAIL
118 | FATTR4_WORD1_SPACE_FREE
119 | FATTR4_WORD1_SPACE_TOTAL
120};
121
Trond Myklebust4ce79712005-06-22 17:16:21 +0000122const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 FATTR4_WORD0_MAXLINK
124 | FATTR4_WORD0_MAXNAME,
125 0
126};
127
128const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
129 | FATTR4_WORD0_MAXREAD
130 | FATTR4_WORD0_MAXWRITE
131 | FATTR4_WORD0_LEASE_TIME,
132 0
133};
134
Manoj Naik830b8e32006-06-09 09:34:25 -0400135const u32 nfs4_fs_locations_bitmap[2] = {
136 FATTR4_WORD0_TYPE
137 | FATTR4_WORD0_CHANGE
138 | FATTR4_WORD0_SIZE
139 | FATTR4_WORD0_FSID
140 | FATTR4_WORD0_FILEID
141 | FATTR4_WORD0_FS_LOCATIONS,
142 FATTR4_WORD1_MODE
143 | FATTR4_WORD1_NUMLINKS
144 | FATTR4_WORD1_OWNER
145 | FATTR4_WORD1_OWNER_GROUP
146 | FATTR4_WORD1_RAWDEV
147 | FATTR4_WORD1_SPACE_USED
148 | FATTR4_WORD1_TIME_ACCESS
149 | FATTR4_WORD1_TIME_METADATA
150 | FATTR4_WORD1_TIME_MODIFY
151 | FATTR4_WORD1_MOUNTED_ON_FILEID
152};
153
Al Virobc4785c2006-10-19 23:28:51 -0700154static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 struct nfs4_readdir_arg *readdir)
156{
Al Viro0dbb4c62006-10-19 23:28:49 -0700157 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159 BUG_ON(readdir->count < 80);
160 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000161 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
163 return;
164 }
165
166 readdir->cookie = 0;
167 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
168 if (cookie == 2)
169 return;
170
171 /*
172 * NFSv4 servers do not return entries for '.' and '..'
173 * Therefore, we fake these entries here. We let '.'
174 * have cookie 0 and '..' have cookie 1. Note that
175 * when talking to the server, we always send cookie 0
176 * instead of 1 or 2.
177 */
Al Viro0dbb4c62006-10-19 23:28:49 -0700178 start = p = kmap_atomic(*readdir->pages, KM_USER0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 if (cookie == 0) {
181 *p++ = xdr_one; /* next */
182 *p++ = xdr_zero; /* cookie, first word */
183 *p++ = xdr_one; /* cookie, second word */
184 *p++ = xdr_one; /* entry len */
185 memcpy(p, ".\0\0\0", 4); /* entry */
186 p++;
187 *p++ = xdr_one; /* bitmap length */
188 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
189 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400190 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 }
192
193 *p++ = xdr_one; /* next */
194 *p++ = xdr_zero; /* cookie, first word */
195 *p++ = xdr_two; /* cookie, second word */
196 *p++ = xdr_two; /* entry len */
197 memcpy(p, "..\0\0", 4); /* entry */
198 p++;
199 *p++ = xdr_one; /* bitmap length */
200 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
201 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400202 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204 readdir->pgbase = (char *)p - (char *)start;
205 readdir->count -= readdir->pgbase;
206 kunmap_atomic(start, KM_USER0);
207}
208
Trond Myklebust65de8722008-12-23 15:21:44 -0500209static int nfs4_wait_clnt_recover(struct nfs_client *clp)
210{
211 int res;
212
213 might_sleep();
214
Trond Myklebuste005e802008-12-23 15:21:48 -0500215 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400216 nfs_wait_bit_killable, TASK_KILLABLE);
Trond Myklebust65de8722008-12-23 15:21:44 -0500217 return res;
218}
219
220static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
221{
222 int res = 0;
223
224 might_sleep();
225
226 if (*timeout <= 0)
227 *timeout = NFS4_POLL_RETRY_MIN;
228 if (*timeout > NFS4_POLL_RETRY_MAX)
229 *timeout = NFS4_POLL_RETRY_MAX;
230 schedule_timeout_killable(*timeout);
231 if (fatal_signal_pending(current))
232 res = -ERESTARTSYS;
233 *timeout <<= 1;
234 return res;
235}
236
237/* This is the error handling routine for processes that are allowed
238 * to sleep.
239 */
240static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
241{
242 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500243 struct nfs4_state *state = exception->state;
Trond Myklebust65de8722008-12-23 15:21:44 -0500244 int ret = errorcode;
245
246 exception->retry = 0;
247 switch(errorcode) {
248 case 0:
249 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500250 case -NFS4ERR_ADMIN_REVOKED:
251 case -NFS4ERR_BAD_STATEID:
252 case -NFS4ERR_OPENMODE:
253 if (state == NULL)
254 break;
255 nfs4_state_mark_reclaim_nograce(clp, state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500256 goto do_state_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500257 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500258 if (state == NULL)
259 break;
260 nfs4_state_mark_reclaim_reboot(clp, state);
261 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust65de8722008-12-23 15:21:44 -0500262 case -NFS4ERR_EXPIRED:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500263 goto do_state_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500264#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400265 case -NFS4ERR_BADSESSION:
266 case -NFS4ERR_BADSLOT:
267 case -NFS4ERR_BAD_HIGH_SLOT:
268 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
269 case -NFS4ERR_DEADSESSION:
270 case -NFS4ERR_SEQ_FALSE_RETRY:
271 case -NFS4ERR_SEQ_MISORDERED:
272 dprintk("%s ERROR: %d Reset session\n", __func__,
273 errorcode);
Andy Adamson0b9e2d42009-12-04 16:02:14 -0500274 nfs4_schedule_state_recovery(clp);
Andy Adamson4745e312009-04-01 09:22:42 -0400275 exception->retry = 1;
Andy Adamsonb9179232009-12-04 15:55:32 -0500276 break;
Trond Myklebust03391692010-01-26 15:42:38 -0500277#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500278 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500279 if (exception->timeout > HZ) {
280 /* We have retried a decent amount, time to
281 * fail
282 */
283 ret = -EBUSY;
284 break;
285 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500286 case -NFS4ERR_GRACE:
287 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -0500288 case -EKEYEXPIRED:
Trond Myklebust65de8722008-12-23 15:21:44 -0500289 ret = nfs4_delay(server->client, &exception->timeout);
290 if (ret != 0)
291 break;
292 case -NFS4ERR_OLD_STATEID:
293 exception->retry = 1;
294 }
295 /* We failed to handle the error */
296 return nfs4_map_errors(ret);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500297do_state_recovery:
298 nfs4_schedule_state_recovery(clp);
299 ret = nfs4_wait_clnt_recover(clp);
300 if (ret == 0)
301 exception->retry = 1;
302 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500303}
304
305
Trond Myklebust26e976a2006-01-03 09:55:21 +0100306static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
David Howells7539bba2006-08-22 20:06:09 -0400308 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 spin_lock(&clp->cl_lock);
310 if (time_before(clp->cl_last_renewal,timestamp))
311 clp->cl_last_renewal = timestamp;
312 spin_unlock(&clp->cl_lock);
313}
314
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400315#if defined(CONFIG_NFS_V4_1)
316
Benny Halevy510b8172009-04-01 09:22:14 -0400317/*
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400318 * nfs4_free_slot - free a slot and efficiently update slot table.
319 *
320 * freeing a slot is trivially done by clearing its respective bit
321 * in the bitmap.
322 * If the freed slotid equals highest_used_slotid we want to update it
323 * so that the server would be able to size down the slot table if needed,
324 * otherwise we know that the highest_used_slotid is still in use.
325 * When updating highest_used_slotid there may be "holes" in the bitmap
326 * so we need to scan down from highest_used_slotid to 0 looking for the now
327 * highest slotid in use.
328 * If none found, highest_used_slotid is set to -1.
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500329 *
330 * Must be called while holding tbl->slot_tbl_lock
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400331 */
332static void
333nfs4_free_slot(struct nfs4_slot_table *tbl, u8 free_slotid)
334{
335 int slotid = free_slotid;
336
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400337 /* clear used bit in bitmap */
338 __clear_bit(slotid, tbl->used_slots);
339
340 /* update highest_used_slotid when it is freed */
341 if (slotid == tbl->highest_used_slotid) {
342 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500343 if (slotid < tbl->max_slots)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400344 tbl->highest_used_slotid = slotid;
345 else
346 tbl->highest_used_slotid = -1;
347 }
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400348 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
349 free_slotid, tbl->highest_used_slotid);
350}
351
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800352/*
353 * Signal state manager thread if session is drained
354 */
355static void nfs41_check_drain_session_complete(struct nfs4_session *ses)
356{
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800357 struct rpc_task *task;
358
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800359 if (!test_bit(NFS4CLNT_SESSION_DRAINING, &ses->clp->cl_state)) {
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800360 task = rpc_wake_up_next(&ses->fc_slot_table.slot_tbl_waitq);
361 if (task)
362 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800363 return;
364 }
365
366 if (ses->fc_slot_table.highest_used_slotid != -1)
367 return;
368
369 dprintk("%s COMPLETE: Session Drained\n", __func__);
370 complete(&ses->complete);
371}
372
Trond Myklebustd185a332010-06-16 09:52:25 -0400373static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400374{
375 struct nfs4_slot_table *tbl;
376
Trond Myklebustd185a332010-06-16 09:52:25 -0400377 tbl = &res->sr_session->fc_slot_table;
Andy Adamson13615872009-04-01 09:22:17 -0400378 if (res->sr_slotid == NFS4_MAX_SLOT_TABLE) {
Andy Adamson13615872009-04-01 09:22:17 -0400379 /* just wake up the next guy waiting since
380 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500381 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500382 return;
Andy Adamson13615872009-04-01 09:22:17 -0400383 }
Andy Adamsonea028ac2009-12-04 15:55:38 -0500384
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500385 spin_lock(&tbl->slot_tbl_lock);
386 nfs4_free_slot(tbl, res->sr_slotid);
Trond Myklebustd185a332010-06-16 09:52:25 -0400387 nfs41_check_drain_session_complete(res->sr_session);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500388 spin_unlock(&tbl->slot_tbl_lock);
389 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
Andy Adamson13615872009-04-01 09:22:17 -0400390}
391
Trond Myklebustd185a332010-06-16 09:52:25 -0400392static void nfs41_sequence_done(struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400393{
394 unsigned long timestamp;
395 struct nfs4_slot_table *tbl;
396 struct nfs4_slot *slot;
397
398 /*
399 * sr_status remains 1 if an RPC level error occurred. The server
400 * may or may not have processed the sequence operation..
401 * Proceed as if the server received and processed the sequence
402 * operation.
403 */
404 if (res->sr_status == 1)
405 res->sr_status = NFS_OK;
406
407 /* -ERESTARTSYS can result in skipping nfs41_sequence_setup */
408 if (res->sr_slotid == NFS4_MAX_SLOT_TABLE)
409 goto out;
410
Andy Adamson691daf32009-12-04 15:55:39 -0500411 /* Check the SEQUENCE operation status */
Andy Adamsonb0df8062009-04-01 09:22:18 -0400412 if (res->sr_status == 0) {
Trond Myklebustd185a332010-06-16 09:52:25 -0400413 struct nfs_client *clp = res->sr_session->clp;
414 tbl = &res->sr_session->fc_slot_table;
Andy Adamson691daf32009-12-04 15:55:39 -0500415 slot = tbl->slots + res->sr_slotid;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400416 /* Update the slot's sequence and clientid lease timer */
417 ++slot->seq_nr;
418 timestamp = res->sr_renewal_time;
419 spin_lock(&clp->cl_lock);
420 if (time_before(clp->cl_last_renewal, timestamp))
421 clp->cl_last_renewal = timestamp;
422 spin_unlock(&clp->cl_lock);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500423 /* Check sequence flags */
Alexandros Batsakis71358402010-02-05 03:45:05 -0800424 if (atomic_read(&clp->cl_count) > 1)
425 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400426 }
427out:
428 /* The session may be reset by one of the error handlers. */
429 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400430 nfs41_sequence_free_slot(res);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400431}
432
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400433/*
Benny Halevy510b8172009-04-01 09:22:14 -0400434 * nfs4_find_slot - efficiently look for a free slot
435 *
436 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
437 * If found, we mark the slot as used, update the highest_used_slotid,
438 * and respectively set up the sequence operation args.
439 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400440 *
441 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400442 */
443static u8
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800444nfs4_find_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400445{
446 int slotid;
447 u8 ret_id = NFS4_MAX_SLOT_TABLE;
448 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
449
Benny Halevy510b8172009-04-01 09:22:14 -0400450 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
451 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
452 tbl->max_slots);
453 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
454 if (slotid >= tbl->max_slots)
455 goto out;
456 __set_bit(slotid, tbl->used_slots);
457 if (slotid > tbl->highest_used_slotid)
458 tbl->highest_used_slotid = slotid;
459 ret_id = slotid;
460out:
461 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
462 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400463 return ret_id;
464}
465
Andy Adamsonce5039c2009-04-01 09:22:13 -0400466static int nfs41_setup_sequence(struct nfs4_session *session,
467 struct nfs4_sequence_args *args,
468 struct nfs4_sequence_res *res,
469 int cache_reply,
470 struct rpc_task *task)
471{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400472 struct nfs4_slot *slot;
473 struct nfs4_slot_table *tbl;
474 u8 slotid;
475
476 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400477 /* slot already allocated? */
478 if (res->sr_slotid != NFS4_MAX_SLOT_TABLE)
479 return 0;
480
Andy Adamsonce5039c2009-04-01 09:22:13 -0400481 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400482 tbl = &session->fc_slot_table;
483
484 spin_lock(&tbl->slot_tbl_lock);
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800485 if (test_bit(NFS4CLNT_SESSION_DRAINING, &session->clp->cl_state) &&
486 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamsonea028ac2009-12-04 15:55:38 -0500487 /*
488 * The state manager will wait until the slot table is empty.
489 * Schedule the reset thread
490 */
Andy Adamson05f0d232009-12-04 15:55:37 -0500491 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400492 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500493 dprintk("%s Schedule Session Reset\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500494 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400495 }
496
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800497 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
498 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
499 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
500 spin_unlock(&tbl->slot_tbl_lock);
501 dprintk("%s enforce FIFO order\n", __func__);
502 return -EAGAIN;
503 }
504
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800505 slotid = nfs4_find_slot(tbl);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400506 if (slotid == NFS4_MAX_SLOT_TABLE) {
507 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
508 spin_unlock(&tbl->slot_tbl_lock);
509 dprintk("<-- %s: no free slots\n", __func__);
510 return -EAGAIN;
511 }
512 spin_unlock(&tbl->slot_tbl_lock);
513
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800514 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400515 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400516 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400517 args->sa_slotid = slotid;
518 args->sa_cache_this = cache_reply;
519
520 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
521
Benny Halevy99fe60d2009-04-01 09:22:29 -0400522 res->sr_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400523 res->sr_slotid = slotid;
524 res->sr_renewal_time = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400525 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400526 /*
527 * sr_status is only set in decode_sequence, and so will remain
528 * set to 1 if an rpc level failure occurs.
529 */
530 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400531 return 0;
532}
533
Trond Myklebust035168a2010-06-16 09:52:26 -0400534int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400535 struct nfs4_sequence_args *args,
536 struct nfs4_sequence_res *res,
537 int cache_reply,
538 struct rpc_task *task)
539{
Trond Myklebust035168a2010-06-16 09:52:26 -0400540 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400541 int ret = 0;
542
Trond Myklebust035168a2010-06-16 09:52:26 -0400543 if (session == NULL) {
544 args->sa_session = NULL;
545 res->sr_session = NULL;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400546 goto out;
Trond Myklebust035168a2010-06-16 09:52:26 -0400547 }
548
549 dprintk("--> %s clp %p session %p sr_slotid %d\n",
550 __func__, session->clp, session, res->sr_slotid);
551
552 ret = nfs41_setup_sequence(session, args, res, cache_reply,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400553 task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400554out:
555 dprintk("<-- %s status=%d\n", __func__, ret);
556 return ret;
557}
558
559struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400560 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400561 struct nfs4_sequence_args *seq_args;
562 struct nfs4_sequence_res *seq_res;
563 int cache_reply;
564};
565
566static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
567{
568 struct nfs41_call_sync_data *data = calldata;
569
Trond Myklebust035168a2010-06-16 09:52:26 -0400570 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
571
572 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400573 data->seq_res, data->cache_reply, task))
574 return;
575 rpc_call_start(task);
576}
577
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800578static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
579{
580 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
581 nfs41_call_sync_prepare(task, calldata);
582}
583
Andy Adamson69ab40c2009-04-01 09:22:19 -0400584static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
585{
586 struct nfs41_call_sync_data *data = calldata;
587
Trond Myklebustd185a332010-06-16 09:52:25 -0400588 nfs41_sequence_done(data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400589}
590
Andy Adamsonce5039c2009-04-01 09:22:13 -0400591struct rpc_call_ops nfs41_call_sync_ops = {
592 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400593 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400594};
595
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800596struct rpc_call_ops nfs41_call_priv_sync_ops = {
597 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
598 .rpc_call_done = nfs41_call_sync_done,
599};
600
Trond Myklebust035168a2010-06-16 09:52:26 -0400601static int nfs4_call_sync_sequence(struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400602 struct rpc_message *msg,
603 struct nfs4_sequence_args *args,
604 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800605 int cache_reply,
606 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400607{
608 int ret;
609 struct rpc_task *task;
610 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400611 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400612 .seq_args = args,
613 .seq_res = res,
614 .cache_reply = cache_reply,
615 };
616 struct rpc_task_setup task_setup = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400617 .rpc_client = server->client,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400618 .rpc_message = msg,
619 .callback_ops = &nfs41_call_sync_ops,
620 .callback_data = &data
621 };
622
623 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800624 if (privileged)
625 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400626 task = rpc_run_task(&task_setup);
627 if (IS_ERR(task))
628 ret = PTR_ERR(task);
629 else {
630 ret = task->tk_status;
631 rpc_put_task(task);
632 }
633 return ret;
634}
635
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400636int _nfs4_call_sync_session(struct nfs_server *server,
637 struct rpc_message *msg,
638 struct nfs4_sequence_args *args,
639 struct nfs4_sequence_res *res,
640 int cache_reply)
641{
Trond Myklebust035168a2010-06-16 09:52:26 -0400642 return nfs4_call_sync_sequence(server, msg, args, res, cache_reply, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400643}
644
645#endif /* CONFIG_NFS_V4_1 */
646
647int _nfs4_call_sync(struct nfs_server *server,
648 struct rpc_message *msg,
649 struct nfs4_sequence_args *args,
650 struct nfs4_sequence_res *res,
651 int cache_reply)
652{
Benny Halevyf3752972009-04-01 09:22:04 -0400653 args->sa_session = res->sr_session = NULL;
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400654 return rpc_call_sync(server->client, msg, 0);
655}
656
657#define nfs4_call_sync(server, msg, args, res, cache_reply) \
658 (server)->nfs_client->cl_call_sync((server), (msg), &(args)->seq_args, \
659 &(res)->seq_res, (cache_reply))
660
Andy Adamsonb0df8062009-04-01 09:22:18 -0400661static void nfs4_sequence_done(const struct nfs_server *server,
662 struct nfs4_sequence_res *res, int rpc_status)
663{
664#ifdef CONFIG_NFS_V4_1
665 if (nfs4_has_session(server->nfs_client))
Trond Myklebustd185a332010-06-16 09:52:25 -0400666 nfs41_sequence_done(res);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400667#endif /* CONFIG_NFS_V4_1 */
668}
669
Trond Myklebust38478b22006-05-25 01:40:57 -0400670static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
Trond Myklebust38478b22006-05-25 01:40:57 -0400672 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Trond Myklebust38478b22006-05-25 01:40:57 -0400674 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400675 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
676 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400677 nfs_force_lookup_revalidate(dir);
Trond Myklebust40d24702007-10-08 09:24:22 -0400678 nfsi->change_attr = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400679 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680}
681
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100682struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400683 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100684 struct nfs_openargs o_arg;
685 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100686 struct nfs_open_confirmargs c_arg;
687 struct nfs_open_confirmres c_res;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100688 struct nfs_fattr f_attr;
689 struct nfs_fattr dir_attr;
Trond Myklebustad389da2007-06-05 12:30:00 -0400690 struct path path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100691 struct dentry *dir;
692 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400693 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100694 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100695 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400696 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100697 int rpc_status;
698 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100699};
700
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400701
702static void nfs4_init_opendata_res(struct nfs4_opendata *p)
703{
704 p->o_res.f_attr = &p->f_attr;
705 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400706 p->o_res.seqid = p->o_arg.seqid;
707 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400708 p->o_res.server = p->o_arg.server;
709 nfs_fattr_init(&p->f_attr);
710 nfs_fattr_init(&p->dir_attr);
Benny Halevy578e4582009-06-18 22:01:23 -0400711 p->o_res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400712}
713
Trond Myklebustad389da2007-06-05 12:30:00 -0400714static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500715 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400716 const struct iattr *attrs,
717 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100718{
Trond Myklebustad389da2007-06-05 12:30:00 -0400719 struct dentry *parent = dget_parent(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100720 struct inode *dir = parent->d_inode;
721 struct nfs_server *server = NFS_SERVER(dir);
722 struct nfs4_opendata *p;
723
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400724 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100725 if (p == NULL)
726 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400727 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100728 if (p->o_arg.seqid == NULL)
729 goto err_free;
Al Virof6948692010-01-30 13:51:04 -0500730 path_get(path);
731 p->path = *path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100732 p->dir = parent;
733 p->owner = sp;
734 atomic_inc(&sp->so_count);
735 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500736 p->o_arg.open_flags = flags;
737 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400738 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400739 p->o_arg.id = sp->so_owner_id.id;
Trond Myklebustad389da2007-06-05 12:30:00 -0400740 p->o_arg.name = &p->path.dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100741 p->o_arg.server = server;
742 p->o_arg.bitmask = server->attr_bitmask;
743 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100744 if (flags & O_EXCL) {
Alexandros Batsakis4882ef72009-12-05 13:30:21 -0500745 if (nfs4_has_persistent_session(server->nfs_client)) {
746 /* GUARDED */
747 p->o_arg.u.attrs = &p->attrs;
748 memcpy(&p->attrs, attrs, sizeof(p->attrs));
749 } else { /* EXCLUSIVE4_1 */
750 u32 *s = (u32 *) p->o_arg.u.verifier.data;
751 s[0] = jiffies;
752 s[1] = current->pid;
753 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100754 } else if (flags & O_CREAT) {
755 p->o_arg.u.attrs = &p->attrs;
756 memcpy(&p->attrs, attrs, sizeof(p->attrs));
757 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100758 p->c_arg.fh = &p->o_res.fh;
759 p->c_arg.stateid = &p->o_res.stateid;
760 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400761 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400762 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100763 return p;
764err_free:
765 kfree(p);
766err:
767 dput(parent);
768 return NULL;
769}
770
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400771static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100772{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400773 struct nfs4_opendata *p = container_of(kref,
774 struct nfs4_opendata, kref);
775
776 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400777 if (p->state != NULL)
778 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400779 nfs4_put_state_owner(p->owner);
780 dput(p->dir);
Jan Blunck31f31db12008-05-02 13:42:45 -0700781 path_put(&p->path);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400782 kfree(p);
783}
784
785static void nfs4_opendata_put(struct nfs4_opendata *p)
786{
787 if (p != NULL)
788 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100789}
790
Trond Myklebust06f814a2006-01-03 09:55:07 +0100791static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
792{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100793 int ret;
794
Trond Myklebust06f814a2006-01-03 09:55:07 +0100795 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100796 return ret;
797}
798
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500799static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400800{
801 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500802
803 if (open_mode & O_EXCL)
804 goto out;
805 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400806 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500807 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
808 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400809 break;
810 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500811 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
812 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400813 break;
814 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500815 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
816 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400817 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500818out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400819 return ret;
820}
821
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500822static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400823{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500824 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400825 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500826 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400827 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500828 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400829 return 1;
830}
831
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500832static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100833{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500834 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100835 case FMODE_WRITE:
836 state->n_wronly++;
837 break;
838 case FMODE_READ:
839 state->n_rdonly++;
840 break;
841 case FMODE_READ|FMODE_WRITE:
842 state->n_rdwr++;
843 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500844 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100845}
846
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500847static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400848{
849 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
850 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
851 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500852 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400853 case FMODE_READ:
854 set_bit(NFS_O_RDONLY_STATE, &state->flags);
855 break;
856 case FMODE_WRITE:
857 set_bit(NFS_O_WRONLY_STATE, &state->flags);
858 break;
859 case FMODE_READ|FMODE_WRITE:
860 set_bit(NFS_O_RDWR_STATE, &state->flags);
861 }
862}
863
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500864static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400865{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400866 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500867 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400868 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400869}
870
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500871static 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 -0700872{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400873 /*
874 * Protect the call to nfs4_state_set_mode_locked and
875 * serialise the stateid update
876 */
877 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400878 if (deleg_stateid != NULL) {
879 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
880 set_bit(NFS_DELEGATED_STATE, &state->flags);
881 }
882 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500883 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400884 write_sequnlock(&state->seqlock);
885 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500886 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700887 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888}
889
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500890static 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 -0500891{
892 struct nfs_inode *nfsi = NFS_I(state->inode);
893 struct nfs_delegation *deleg_cur;
894 int ret = 0;
895
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500896 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500897
898 rcu_read_lock();
899 deleg_cur = rcu_dereference(nfsi->delegation);
900 if (deleg_cur == NULL)
901 goto no_delegation;
902
903 spin_lock(&deleg_cur->lock);
904 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500905 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500906 goto no_delegation_unlock;
907
908 if (delegation == NULL)
909 delegation = &deleg_cur->stateid;
910 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
911 goto no_delegation_unlock;
912
Trond Myklebustb7391f42008-12-23 15:21:52 -0500913 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500914 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500915 ret = 1;
916no_delegation_unlock:
917 spin_unlock(&deleg_cur->lock);
918no_delegation:
919 rcu_read_unlock();
920
921 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500922 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500923 ret = 1;
924 }
925
926 return ret;
927}
928
929
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500930static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400931{
932 struct nfs_delegation *delegation;
933
934 rcu_read_lock();
935 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500936 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -0400937 rcu_read_unlock();
938 return;
939 }
940 rcu_read_unlock();
941 nfs_inode_return_delegation(inode);
942}
943
Trond Myklebust6ee41262007-07-08 14:11:36 -0400944static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400945{
946 struct nfs4_state *state = opendata->state;
947 struct nfs_inode *nfsi = NFS_I(state->inode);
948 struct nfs_delegation *delegation;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500949 int open_mode = opendata->o_arg.open_flags & O_EXCL;
950 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400951 nfs4_stateid stateid;
952 int ret = -EAGAIN;
953
Trond Myklebustaac00a82007-07-05 19:02:21 -0400954 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500955 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400956 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500957 if (can_open_cached(state, fmode, open_mode)) {
958 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400959 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400960 goto out_return_state;
961 }
962 spin_unlock(&state->owner->so_lock);
963 }
Trond Myklebust34310432008-12-23 15:21:38 -0500964 rcu_read_lock();
965 delegation = rcu_dereference(nfsi->delegation);
966 if (delegation == NULL ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500967 !can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -0500968 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -0400969 break;
Trond Myklebust34310432008-12-23 15:21:38 -0500970 }
Trond Myklebustaac00a82007-07-05 19:02:21 -0400971 /* Save the delegation */
972 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
973 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -0400974 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400975 if (ret != 0)
976 goto out;
977 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -0500978
979 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500980 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -0500981 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400982 }
Trond Myklebustaac00a82007-07-05 19:02:21 -0400983out:
984 return ERR_PTR(ret);
985out_return_state:
986 atomic_inc(&state->count);
987 return state;
988}
989
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100990static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
991{
992 struct inode *inode;
993 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -0400994 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400995 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100996
Trond Myklebustaac00a82007-07-05 19:02:21 -0400997 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400998 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400999 goto out;
1000 }
1001
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001002 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001003 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001004 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001005 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001006 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001007 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001008 goto err;
1009 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001010 state = nfs4_get_open_state(inode, data->owner);
1011 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001012 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001013 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001014 int delegation_flags = 0;
1015
Trond Myklebust003707c2007-07-05 18:07:55 -04001016 rcu_read_lock();
1017 delegation = rcu_dereference(NFS_I(inode)->delegation);
1018 if (delegation)
1019 delegation_flags = delegation->flags;
1020 rcu_read_unlock();
Trond Myklebust15c831b2008-12-23 15:21:39 -05001021 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001022 nfs_inode_set_delegation(state->inode,
1023 data->owner->so_cred,
1024 &data->o_res);
1025 else
1026 nfs_inode_reclaim_delegation(state->inode,
1027 data->owner->so_cred,
1028 &data->o_res);
1029 }
Trond Myklebust34310432008-12-23 15:21:38 -05001030
1031 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001032 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001033 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001034out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001035 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001036err_put_inode:
1037 iput(inode);
1038err:
1039 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001040}
1041
Trond Myklebust864472e2006-01-03 09:55:15 +01001042static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1043{
1044 struct nfs_inode *nfsi = NFS_I(state->inode);
1045 struct nfs_open_context *ctx;
1046
1047 spin_lock(&state->inode->i_lock);
1048 list_for_each_entry(ctx, &nfsi->open_files, list) {
1049 if (ctx->state != state)
1050 continue;
1051 get_nfs_open_context(ctx);
1052 spin_unlock(&state->inode->i_lock);
1053 return ctx;
1054 }
1055 spin_unlock(&state->inode->i_lock);
1056 return ERR_PTR(-ENOENT);
1057}
1058
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001059static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1060{
1061 struct nfs4_opendata *opendata;
1062
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001063 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001064 if (opendata == NULL)
1065 return ERR_PTR(-ENOMEM);
1066 opendata->state = state;
1067 atomic_inc(&state->count);
1068 return opendata;
1069}
1070
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001071static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001072{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001073 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001074 int ret;
1075
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001076 opendata->o_arg.open_flags = 0;
1077 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001078 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1079 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1080 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001081 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001082 if (ret != 0)
1083 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001084 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001085 if (IS_ERR(newstate))
1086 return PTR_ERR(newstate);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001087 nfs4_close_state(&opendata->path, newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001088 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001089 return 0;
1090}
1091
1092static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1093{
Trond Myklebust864472e2006-01-03 09:55:15 +01001094 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001095 int ret;
1096
1097 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001098 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001099 smp_rmb();
1100 if (state->n_rdwr != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001101 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001102 if (ret != 0)
1103 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001104 if (newstate != state)
1105 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001106 }
1107 if (state->n_wronly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001108 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001109 if (ret != 0)
1110 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001111 if (newstate != state)
1112 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001113 }
1114 if (state->n_rdonly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001115 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001116 if (ret != 0)
1117 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001118 if (newstate != state)
1119 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001120 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001121 /*
1122 * We may have performed cached opens for all three recoveries.
1123 * Check if we need to update the current stateid.
1124 */
1125 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1126 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001127 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001128 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1129 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001130 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001131 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001132 return 0;
1133}
1134
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135/*
1136 * OPEN_RECLAIM:
1137 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001139static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001141 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001142 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001143 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 int status;
1145
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001146 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1147 if (IS_ERR(opendata))
1148 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001149 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1150 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001151 rcu_read_lock();
1152 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001153 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001154 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001155 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001156 opendata->o_arg.u.delegation_type = delegation_type;
1157 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001158 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 return status;
1160}
1161
Trond Myklebust539cd032007-06-05 11:46:42 -04001162static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163{
1164 struct nfs_server *server = NFS_SERVER(state->inode);
1165 struct nfs4_exception exception = { };
1166 int err;
1167 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001168 err = _nfs4_do_open_reclaim(ctx, state);
Jeff Layton2c643482010-01-07 09:42:03 -05001169 if (err != -NFS4ERR_DELAY && err != -EKEYEXPIRED)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001170 break;
1171 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 } while (exception.retry);
1173 return err;
1174}
1175
Trond Myklebust864472e2006-01-03 09:55:15 +01001176static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1177{
1178 struct nfs_open_context *ctx;
1179 int ret;
1180
1181 ctx = nfs4_state_find_open_context(state);
1182 if (IS_ERR(ctx))
1183 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001184 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001185 put_nfs_open_context(ctx);
1186 return ret;
1187}
1188
Trond Myklebust13437e12007-07-06 15:10:43 -04001189static 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 -07001190{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001191 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001192 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001194 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1195 if (IS_ERR(opendata))
1196 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001197 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -04001198 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001199 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +01001200 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001201 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001202 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203}
1204
Trond Myklebust13437e12007-07-06 15:10:43 -04001205int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206{
1207 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001208 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 int err;
1210 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001211 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 switch (err) {
1213 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001214 case -ENOENT:
1215 case -ESTALE:
1216 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001217 case -NFS4ERR_BADSESSION:
1218 case -NFS4ERR_BADSLOT:
1219 case -NFS4ERR_BAD_HIGH_SLOT:
1220 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1221 case -NFS4ERR_DEADSESSION:
1222 nfs4_schedule_state_recovery(
1223 server->nfs_client);
1224 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 case -NFS4ERR_STALE_CLIENTID:
1226 case -NFS4ERR_STALE_STATEID:
1227 case -NFS4ERR_EXPIRED:
1228 /* Don't recall a delegation if it was lost */
David Howells7539bba2006-08-22 20:06:09 -04001229 nfs4_schedule_state_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001230 goto out;
1231 case -ERESTARTSYS:
1232 /*
1233 * The show must go on: exit, but mark the
1234 * stateid as needing recovery.
1235 */
1236 case -NFS4ERR_ADMIN_REVOKED:
1237 case -NFS4ERR_BAD_STATEID:
1238 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
1239 case -ENOMEM:
1240 err = 0;
1241 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 }
1243 err = nfs4_handle_exception(server, err, &exception);
1244 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001245out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 return err;
1247}
1248
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001249static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1250{
1251 struct nfs4_opendata *data = calldata;
1252
1253 data->rpc_status = task->tk_status;
1254 if (RPC_ASSASSINATED(task))
1255 return;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001256 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001257 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1258 sizeof(data->o_res.stateid.data));
Trond Myklebustbb226292008-01-02 15:19:18 -05001259 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001260 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001261 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001262 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001263}
1264
1265static void nfs4_open_confirm_release(void *calldata)
1266{
1267 struct nfs4_opendata *data = calldata;
1268 struct nfs4_state *state = NULL;
1269
1270 /* If this request hasn't been cancelled, do nothing */
1271 if (data->cancelled == 0)
1272 goto out_free;
1273 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001274 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001275 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001276 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001277 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001278 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001279out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001280 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001281}
1282
1283static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001284 .rpc_call_done = nfs4_open_confirm_done,
1285 .rpc_release = nfs4_open_confirm_release,
1286};
1287
1288/*
1289 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1290 */
1291static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1292{
1293 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1294 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001295 struct rpc_message msg = {
1296 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1297 .rpc_argp = &data->c_arg,
1298 .rpc_resp = &data->c_res,
1299 .rpc_cred = data->owner->so_cred,
1300 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001301 struct rpc_task_setup task_setup_data = {
1302 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001303 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001304 .callback_ops = &nfs4_open_confirm_ops,
1305 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001306 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001307 .flags = RPC_TASK_ASYNC,
1308 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 int status;
1310
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001311 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001312 data->rpc_done = 0;
1313 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001314 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001315 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001316 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001317 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001318 status = nfs4_wait_for_completion_rpc_task(task);
1319 if (status != 0) {
1320 data->cancelled = 1;
1321 smp_wmb();
1322 } else
1323 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001324 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 return status;
1326}
1327
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001328static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001330 struct nfs4_opendata *data = calldata;
1331 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001332
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001333 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1334 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001335 /*
1336 * Check if we still need to send an OPEN call, or if we can use
1337 * a delegation instead.
1338 */
1339 if (data->state != NULL) {
1340 struct nfs_delegation *delegation;
1341
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001342 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001343 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001344 rcu_read_lock();
1345 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1346 if (delegation != NULL &&
Trond Myklebust15c831b2008-12-23 15:21:39 -05001347 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001348 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001349 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001350 }
1351 rcu_read_unlock();
1352 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001353 /* Update sequence id. */
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001354 data->o_arg.id = sp->so_owner_id.id;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001355 data->o_arg.clientid = sp->so_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001356 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001357 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001358 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1359 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001360 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001361 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001362 &data->o_arg.seq_args,
1363 &data->o_res.seq_res, 1, task))
1364 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001365 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001366 return;
1367out_no_action:
1368 task->tk_action = NULL;
1369
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001370}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001372static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1373{
1374 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1375 nfs4_open_prepare(task, calldata);
1376}
1377
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001378static void nfs4_open_done(struct rpc_task *task, void *calldata)
1379{
1380 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001382 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001383
Trond Myklebustd61e6122009-12-05 19:32:19 -05001384 nfs4_sequence_done(data->o_arg.server, &data->o_res.seq_res,
1385 task->tk_status);
Andy Adamsond8985282009-04-01 09:22:21 -04001386
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001387 if (RPC_ASSASSINATED(task))
1388 return;
1389 if (task->tk_status == 0) {
1390 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001391 case S_IFREG:
1392 break;
1393 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001394 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001395 break;
1396 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001397 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001398 break;
1399 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001400 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001401 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001402 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001403 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1404 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001405 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001406 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001407}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001408
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001409static void nfs4_open_release(void *calldata)
1410{
1411 struct nfs4_opendata *data = calldata;
1412 struct nfs4_state *state = NULL;
1413
1414 /* If this request hasn't been cancelled, do nothing */
1415 if (data->cancelled == 0)
1416 goto out_free;
1417 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001418 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001419 goto out_free;
1420 /* In case we need an open_confirm, no cleanup! */
1421 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1422 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001423 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001424 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001425 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001426out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001427 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001428}
1429
1430static const struct rpc_call_ops nfs4_open_ops = {
1431 .rpc_call_prepare = nfs4_open_prepare,
1432 .rpc_call_done = nfs4_open_done,
1433 .rpc_release = nfs4_open_release,
1434};
1435
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001436static const struct rpc_call_ops nfs4_recover_open_ops = {
1437 .rpc_call_prepare = nfs4_recover_open_prepare,
1438 .rpc_call_done = nfs4_open_done,
1439 .rpc_release = nfs4_open_release,
1440};
1441
1442static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001443{
1444 struct inode *dir = data->dir->d_inode;
1445 struct nfs_server *server = NFS_SERVER(dir);
1446 struct nfs_openargs *o_arg = &data->o_arg;
1447 struct nfs_openres *o_res = &data->o_res;
1448 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001449 struct rpc_message msg = {
1450 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1451 .rpc_argp = o_arg,
1452 .rpc_resp = o_res,
1453 .rpc_cred = data->owner->so_cred,
1454 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001455 struct rpc_task_setup task_setup_data = {
1456 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001457 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001458 .callback_ops = &nfs4_open_ops,
1459 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001460 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001461 .flags = RPC_TASK_ASYNC,
1462 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001463 int status;
1464
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001465 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001466 data->rpc_done = 0;
1467 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001468 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001469 if (isrecover)
1470 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001471 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001472 if (IS_ERR(task))
1473 return PTR_ERR(task);
1474 status = nfs4_wait_for_completion_rpc_task(task);
1475 if (status != 0) {
1476 data->cancelled = 1;
1477 smp_wmb();
1478 } else
1479 status = data->rpc_status;
1480 rpc_put_task(task);
1481
1482 return status;
1483}
1484
1485static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1486{
1487 struct inode *dir = data->dir->d_inode;
1488 struct nfs_openres *o_res = &data->o_res;
1489 int status;
1490
1491 status = nfs4_run_open_task(data, 1);
1492 if (status != 0 || !data->rpc_done)
1493 return status;
1494
1495 nfs_refresh_inode(dir, o_res->dir_attr);
1496
1497 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1498 status = _nfs4_proc_open_confirm(data);
1499 if (status != 0)
1500 return status;
1501 }
1502
1503 return status;
1504}
1505
1506/*
1507 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1508 */
1509static int _nfs4_proc_open(struct nfs4_opendata *data)
1510{
1511 struct inode *dir = data->dir->d_inode;
1512 struct nfs_server *server = NFS_SERVER(dir);
1513 struct nfs_openargs *o_arg = &data->o_arg;
1514 struct nfs_openres *o_res = &data->o_res;
1515 int status;
1516
1517 status = nfs4_run_open_task(data, 0);
Trond Myklebust3e309912007-07-07 13:19:59 -04001518 if (status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001519 return status;
1520
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001521 if (o_arg->open_flags & O_CREAT) {
1522 update_changeattr(dir, &o_res->cinfo);
1523 nfs_post_op_update_inode(dir, o_res->dir_attr);
1524 } else
1525 nfs_refresh_inode(dir, o_res->dir_attr);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001526 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1527 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001529 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001531 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 }
1533 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001534 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001535 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536}
1537
Trond Myklebust10afec92007-05-14 17:16:04 -04001538static int nfs4_recover_expired_lease(struct nfs_server *server)
Trond Myklebust58d97142006-01-03 09:55:24 +01001539{
David Howells7539bba2006-08-22 20:06:09 -04001540 struct nfs_client *clp = server->nfs_client;
Trond Myklebusta78cb572009-08-09 15:06:19 -04001541 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001542 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001543
Trond Myklebusta78cb572009-08-09 15:06:19 -04001544 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001545 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001546 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001547 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001548 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1549 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001550 break;
Trond Myklebust58d97142006-01-03 09:55:24 +01001551 nfs4_schedule_state_recovery(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001552 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001553 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001554 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001555}
1556
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557/*
1558 * OPEN_EXPIRED:
1559 * reclaim state on the server after a network partition.
1560 * Assumes caller holds the appropriate lock
1561 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001562static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001564 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001565 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001567 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1568 if (IS_ERR(opendata))
1569 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001570 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001571 if (ret == -ESTALE)
Trond Myklebust539cd032007-06-05 11:46:42 -04001572 d_drop(ctx->path.dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001573 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001574 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575}
1576
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001577static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001578{
Trond Myklebust539cd032007-06-05 11:46:42 -04001579 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001580 struct nfs4_exception exception = { };
1581 int err;
1582
1583 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001584 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001585 switch (err) {
1586 default:
1587 goto out;
1588 case -NFS4ERR_GRACE:
1589 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -05001590 case -EKEYEXPIRED:
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001591 nfs4_handle_exception(server, err, &exception);
1592 err = 0;
1593 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001594 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001595out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001596 return err;
1597}
1598
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1600{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001602 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
Trond Myklebust864472e2006-01-03 09:55:15 +01001604 ctx = nfs4_state_find_open_context(state);
1605 if (IS_ERR(ctx))
1606 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001607 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001608 put_nfs_open_context(ctx);
1609 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610}
1611
1612/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001613 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1614 * fields corresponding to attributes that were used to store the verifier.
1615 * Make sure we clobber those fields in the later setattr call
1616 */
1617static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1618{
1619 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1620 !(sattr->ia_valid & ATTR_ATIME_SET))
1621 sattr->ia_valid |= ATTR_ATIME;
1622
1623 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1624 !(sattr->ia_valid & ATTR_MTIME_SET))
1625 sattr->ia_valid |= ATTR_MTIME;
1626}
1627
1628/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001629 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001631static int _nfs4_do_open(struct inode *dir, struct path *path, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632{
1633 struct nfs4_state_owner *sp;
1634 struct nfs4_state *state = NULL;
1635 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001636 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001637 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
1639 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 status = -ENOMEM;
1641 if (!(sp = nfs4_get_state_owner(server, cred))) {
1642 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1643 goto out_err;
1644 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001645 status = nfs4_recover_expired_lease(server);
1646 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001647 goto err_put_state_owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001648 if (path->dentry->d_inode != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001649 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001650 status = -ENOMEM;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001651 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001652 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001653 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
Trond Myklebustaac00a82007-07-05 19:02:21 -04001655 if (path->dentry->d_inode != NULL)
1656 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1657
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001658 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001660 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001662 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001663 status = PTR_ERR(state);
1664 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001665 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001666 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001667 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001668
1669 if (opendata->o_arg.open_flags & O_EXCL) {
1670 nfs4_exclusive_attrset(opendata, sattr);
1671
1672 nfs_fattr_init(opendata->o_res.f_attr);
1673 status = nfs4_do_setattr(state->inode, cred,
1674 opendata->o_res.f_attr, sattr,
1675 state);
1676 if (status == 0)
1677 nfs_setattr_update_inode(state->inode, sattr);
1678 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1679 }
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001680 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 *res = state;
1683 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001684err_opendata_put:
1685 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001686err_put_state_owner:
1687 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 *res = NULL;
1690 return status;
1691}
1692
1693
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001694static struct nfs4_state *nfs4_do_open(struct inode *dir, struct path *path, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695{
1696 struct nfs4_exception exception = { };
1697 struct nfs4_state *res;
1698 int status;
1699
1700 do {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001701 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 if (status == 0)
1703 break;
1704 /* NOTE: BAD_SEQID means the server and client disagree about the
1705 * book-keeping w.r.t. state-changing operations
1706 * (OPEN/CLOSE/LOCK/LOCKU...)
1707 * It is actually a sign of a bug on the client or on the server.
1708 *
1709 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001710 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 * have unhashed the old state_owner for us, and that we can
1712 * therefore safely retry using a new one. We should still warn
1713 * the user though...
1714 */
1715 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001716 printk(KERN_WARNING "NFS: v4 server %s "
1717 " returned a bad sequence-id error!\n",
1718 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 exception.retry = 1;
1720 continue;
1721 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001722 /*
1723 * BAD_STATEID on OPEN means that the server cancelled our
1724 * state before it received the OPEN_CONFIRM.
1725 * Recover by retrying the request as per the discussion
1726 * on Page 181 of RFC3530.
1727 */
1728 if (status == -NFS4ERR_BAD_STATEID) {
1729 exception.retry = 1;
1730 continue;
1731 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001732 if (status == -EAGAIN) {
1733 /* We must have found a delegation */
1734 exception.retry = 1;
1735 continue;
1736 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1738 status, &exception));
1739 } while (exception.retry);
1740 return res;
1741}
1742
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001743static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1744 struct nfs_fattr *fattr, struct iattr *sattr,
1745 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001747 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001749 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 .iap = sattr,
1751 .server = server,
1752 .bitmask = server->attr_bitmask,
1753 };
1754 struct nfs_setattrres res = {
1755 .fattr = fattr,
1756 .server = server,
1757 };
1758 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001759 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1760 .rpc_argp = &arg,
1761 .rpc_resp = &res,
1762 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001764 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001765 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
Trond Myklebust0e574af2005-10-27 22:12:38 -04001767 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001769 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1770 /* Use that stateid */
1771 } else if (state != NULL) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001772 nfs4_copy_stateid(&arg.stateid, state, current->files);
1773 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1775
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001776 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001777 if (status == 0 && state != NULL)
1778 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001779 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780}
1781
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001782static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1783 struct nfs_fattr *fattr, struct iattr *sattr,
1784 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001786 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 struct nfs4_exception exception = { };
1788 int err;
1789 do {
1790 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001791 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 &exception);
1793 } while (exception.retry);
1794 return err;
1795}
1796
1797struct nfs4_closedata {
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001798 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 struct inode *inode;
1800 struct nfs4_state *state;
1801 struct nfs_closeargs arg;
1802 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001803 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001804 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805};
1806
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001807static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001808{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001809 struct nfs4_closedata *calldata = data;
1810 struct nfs4_state_owner *sp = calldata->state->owner;
Trond Myklebust95121352005-10-18 14:20:12 -07001811
1812 nfs4_put_open_state(calldata->state);
1813 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001814 nfs4_put_state_owner(sp);
Jan Blunck31f31db12008-05-02 13:42:45 -07001815 path_put(&calldata->path);
Trond Myklebust95121352005-10-18 14:20:12 -07001816 kfree(calldata);
1817}
1818
Trond Myklebust88069f72009-12-08 08:33:16 -05001819static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1820 fmode_t fmode)
1821{
1822 spin_lock(&state->owner->so_lock);
1823 if (!(fmode & FMODE_READ))
1824 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1825 if (!(fmode & FMODE_WRITE))
1826 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1827 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1828 spin_unlock(&state->owner->so_lock);
1829}
1830
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001831static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001833 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 struct nfs_server *server = NFS_SERVER(calldata->inode);
1836
Andy Adamson19ddab02009-04-01 09:22:20 -04001837 nfs4_sequence_done(server, &calldata->res.seq_res, task->tk_status);
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001838 if (RPC_ASSASSINATED(task))
1839 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 /* hmm. we are done with the inode, and in the process of freeing
1841 * the state_owner. we keep this around to process errors
1842 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 switch (task->tk_status) {
1844 case 0:
Trond Myklebust45328c32007-07-26 17:47:34 -04001845 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001846 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05001847 nfs4_close_clear_stateid_flags(state,
1848 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 break;
1850 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001851 case -NFS4ERR_OLD_STATEID:
1852 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001854 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001855 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05001857 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1858 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 }
Trond Myklebust72211db2009-12-15 14:47:36 -05001860 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04001861 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862}
1863
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001864static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001866 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001867 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05001868 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07001869
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001870 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001871 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001872
Trond Myklebust88069f72009-12-08 08:33:16 -05001873 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1874 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05001875 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001876 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01001877 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001878 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001879 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
1880 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1881 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04001882 }
1883 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001884 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
1885 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1886 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04001887 }
Trond Myklebuste7616922006-01-03 09:55:13 +01001888 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05001889 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05001890
1891 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001892 /* Note: exit _without_ calling nfs4_close_done */
1893 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07001894 return;
1895 }
Trond Myklebust88069f72009-12-08 08:33:16 -05001896
1897 if (calldata->arg.fmode == 0)
1898 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
1899
Trond Myklebust516a6af2005-10-27 22:12:41 -04001900 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001901 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001902 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
Andy Adamson19ddab02009-04-01 09:22:20 -04001903 &calldata->arg.seq_args, &calldata->res.seq_res,
1904 1, task))
1905 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001906 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907}
1908
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001909static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001910 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001911 .rpc_call_done = nfs4_close_done,
1912 .rpc_release = nfs4_free_closedata,
1913};
1914
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915/*
1916 * It is possible for data to be read/written from a mem-mapped file
1917 * after the sys_close call (which hits the vfs layer as a flush).
1918 * This means that we can't safely call nfsv4 close on a file until
1919 * the inode is cleared. This in turn means that we are not good
1920 * NFSv4 citizens - we do not indicate to the server to update the file's
1921 * share state even when we are done with one of the three share
1922 * stateid's in the inode.
1923 *
1924 * NOTE: Caller must be holding the sp->so_owner semaphore!
1925 */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001926int nfs4_do_close(struct path *path, struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001928 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04001930 struct nfs4_state_owner *sp = state->owner;
1931 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001932 struct rpc_message msg = {
1933 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1934 .rpc_cred = state->owner->so_cred,
1935 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001936 struct rpc_task_setup task_setup_data = {
1937 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001938 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001939 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05001940 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001941 .flags = RPC_TASK_ASYNC,
1942 };
Trond Myklebust95121352005-10-18 14:20:12 -07001943 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001945 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07001947 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001948 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001950 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04001951 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001953 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07001954 if (calldata->arg.seqid == NULL)
1955 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001956 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04001957 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001958 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04001959 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001960 calldata->res.server = server;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04001961 calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Al Virof6948692010-01-30 13:51:04 -05001962 path_get(path);
1963 calldata->path = *path;
Trond Myklebust95121352005-10-18 14:20:12 -07001964
Trond Myklebust5138fde2007-07-14 15:40:01 -04001965 msg.rpc_argp = &calldata->arg,
1966 msg.rpc_resp = &calldata->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001967 task_setup_data.callback_data = calldata;
1968 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001969 if (IS_ERR(task))
1970 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001971 status = 0;
1972 if (wait)
1973 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001974 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001975 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07001976out_free_calldata:
1977 kfree(calldata);
1978out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04001979 nfs4_put_open_state(state);
1980 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07001981 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982}
1983
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001984static int nfs4_intent_set_file(struct nameidata *nd, struct path *path, struct nfs4_state *state, fmode_t fmode)
Trond Myklebust02a913a2005-10-18 14:20:17 -07001985{
1986 struct file *filp;
Trond Myklebust1b45c462007-07-03 13:04:56 -04001987 int ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001988
Trond Myklebust1b45c462007-07-03 13:04:56 -04001989 /* If the open_intent is for execute, we have an extra check to make */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001990 if (fmode & FMODE_EXEC) {
Trond Myklebustaf22f942007-08-10 17:45:10 -04001991 ret = nfs_may_open(state->inode,
Trond Myklebust1b45c462007-07-03 13:04:56 -04001992 state->owner->so_cred,
1993 nd->intent.open.flags);
1994 if (ret < 0)
1995 goto out_close;
1996 }
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001997 filp = lookup_instantiate_filp(nd, path->dentry, NULL);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001998 if (!IS_ERR(filp)) {
1999 struct nfs_open_context *ctx;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04002000 ctx = nfs_file_open_context(filp);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002001 ctx->state = state;
Trond Myklebust95cf9592006-04-18 13:14:06 -04002002 return 0;
2003 }
Trond Myklebust1b45c462007-07-03 13:04:56 -04002004 ret = PTR_ERR(filp);
2005out_close:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002006 nfs4_close_sync(path, state, fmode & (FMODE_READ|FMODE_WRITE));
Trond Myklebust1b45c462007-07-03 13:04:56 -04002007 return ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002008}
2009
2010struct dentry *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
2012{
Trond Myklebustad389da2007-06-05 12:30:00 -04002013 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08002014 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04002015 .dentry = dentry,
2016 };
Jan Blunck4ac91372008-02-14 19:34:32 -08002017 struct dentry *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 struct iattr attr;
2019 struct rpc_cred *cred;
2020 struct nfs4_state *state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002021 struct dentry *res;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002022 fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023
2024 if (nd->flags & LOOKUP_CREATE) {
2025 attr.ia_mode = nd->intent.open.create_mode;
2026 attr.ia_valid = ATTR_MODE;
2027 if (!IS_POSIXACL(dir))
Al Viroce3b0f82009-03-29 19:08:22 -04002028 attr.ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 } else {
2030 attr.ia_valid = 0;
2031 BUG_ON(nd->intent.open.flags & O_CREAT);
2032 }
2033
Trond Myklebust98a8e322008-03-12 12:25:28 -04002034 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 if (IS_ERR(cred))
Trond Myklebust02a913a2005-10-18 14:20:17 -07002036 return (struct dentry *)cred;
Trond Myklebust565277f2007-10-15 18:17:53 -04002037 parent = dentry->d_parent;
2038 /* Protect against concurrent sillydeletes */
2039 nfs_block_sillyrename(parent);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002040 state = nfs4_do_open(dir, &path, fmode, nd->intent.open.flags, &attr, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002042 if (IS_ERR(state)) {
Trond Myklebustd75340c2007-10-01 21:42:01 -04002043 if (PTR_ERR(state) == -ENOENT) {
Trond Myklebust02a913a2005-10-18 14:20:17 -07002044 d_add(dentry, NULL);
Trond Myklebustd75340c2007-10-01 21:42:01 -04002045 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2046 }
Trond Myklebust565277f2007-10-15 18:17:53 -04002047 nfs_unblock_sillyrename(parent);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002048 return (struct dentry *)state;
2049 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002050 res = d_add_unique(dentry, igrab(state->inode));
Trond Myklebust02a913a2005-10-18 14:20:17 -07002051 if (res != NULL)
Trond Myklebustdeee9362007-08-27 11:33:00 -04002052 path.dentry = res;
Trond Myklebustd75340c2007-10-01 21:42:01 -04002053 nfs_set_verifier(path.dentry, nfs_save_change_attribute(dir));
Trond Myklebust565277f2007-10-15 18:17:53 -04002054 nfs_unblock_sillyrename(parent);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002055 nfs4_intent_set_file(nd, &path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002056 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057}
2058
2059int
Trond Myklebust02a913a2005-10-18 14:20:17 -07002060nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061{
Trond Myklebustad389da2007-06-05 12:30:00 -04002062 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08002063 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04002064 .dentry = dentry,
2065 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 struct rpc_cred *cred;
2067 struct nfs4_state *state;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002068 fmode_t fmode = openflags & (FMODE_READ | FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
Trond Myklebust98a8e322008-03-12 12:25:28 -04002070 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 if (IS_ERR(cred))
2072 return PTR_ERR(cred);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002073 state = nfs4_do_open(dir, &path, fmode, openflags, NULL, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002075 if (IS_ERR(state)) {
2076 switch (PTR_ERR(state)) {
2077 case -EPERM:
2078 case -EACCES:
2079 case -EDQUOT:
2080 case -ENOSPC:
2081 case -EROFS:
Al Viro04287f92010-04-08 00:06:07 +01002082 return PTR_ERR(state);
Chuck Leverb87c0ad2006-10-19 23:28:42 -07002083 default:
2084 goto out_drop;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002085 }
Trond Myklebust02a913a2005-10-18 14:20:17 -07002086 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002087 if (state->inode == dentry->d_inode) {
Trond Myklebustd75340c2007-10-01 21:42:01 -04002088 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002089 nfs4_intent_set_file(nd, &path, state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 return 1;
2091 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002092 nfs4_close_sync(&path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002093out_drop:
2094 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 return 0;
2096}
2097
Trond Myklebust1185a552009-12-03 15:54:02 -05002098static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002099{
2100 if (ctx->state == NULL)
2101 return;
2102 if (is_sync)
2103 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
2104 else
2105 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
2106}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107
2108static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2109{
Benny Halevy43652ad2009-04-01 09:21:54 -04002110 struct nfs4_server_caps_arg args = {
2111 .fhandle = fhandle,
2112 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 struct nfs4_server_caps_res res = {};
2114 struct rpc_message msg = {
2115 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002116 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 .rpc_resp = &res,
2118 };
2119 int status;
2120
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002121 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 if (status == 0) {
2123 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002124 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2125 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2126 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2127 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2128 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2130 server->caps |= NFS_CAP_ACLS;
2131 if (res.has_links != 0)
2132 server->caps |= NFS_CAP_HARDLINKS;
2133 if (res.has_symlinks != 0)
2134 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002135 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2136 server->caps |= NFS_CAP_FILEID;
2137 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2138 server->caps |= NFS_CAP_MODE;
2139 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2140 server->caps |= NFS_CAP_NLINK;
2141 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2142 server->caps |= NFS_CAP_OWNER;
2143 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2144 server->caps |= NFS_CAP_OWNER_GROUP;
2145 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2146 server->caps |= NFS_CAP_ATIME;
2147 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2148 server->caps |= NFS_CAP_CTIME;
2149 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2150 server->caps |= NFS_CAP_MTIME;
2151
Trond Myklebusta65318b2009-03-11 14:10:28 -04002152 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2153 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2154 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 server->acl_bitmask = res.acl_bitmask;
2156 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002157
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 return status;
2159}
2160
Trond Myklebust55a97592006-06-09 09:34:19 -04002161int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162{
2163 struct nfs4_exception exception = { };
2164 int err;
2165 do {
2166 err = nfs4_handle_exception(server,
2167 _nfs4_server_capabilities(server, fhandle),
2168 &exception);
2169 } while (exception.retry);
2170 return err;
2171}
2172
2173static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2174 struct nfs_fsinfo *info)
2175{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 struct nfs4_lookup_root_arg args = {
2177 .bitmask = nfs4_fattr_bitmap,
2178 };
2179 struct nfs4_lookup_res res = {
2180 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002181 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 .fh = fhandle,
2183 };
2184 struct rpc_message msg = {
2185 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2186 .rpc_argp = &args,
2187 .rpc_resp = &res,
2188 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002189
Trond Myklebust0e574af2005-10-27 22:12:38 -04002190 nfs_fattr_init(info->fattr);
Trond Myklebustfccba802009-07-21 16:48:07 -04002191 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192}
2193
2194static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2195 struct nfs_fsinfo *info)
2196{
2197 struct nfs4_exception exception = { };
2198 int err;
2199 do {
2200 err = nfs4_handle_exception(server,
2201 _nfs4_lookup_root(server, fhandle, info),
2202 &exception);
2203 } while (exception.retry);
2204 return err;
2205}
2206
David Howells54ceac42006-08-22 20:06:13 -04002207/*
2208 * get the file handle for the "/" directory on the server
2209 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002211 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 int status;
2214
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 status = nfs4_lookup_root(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 if (status == 0)
2217 status = nfs4_server_capabilities(server, fhandle);
2218 if (status == 0)
2219 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002220 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221}
2222
Manoj Naik6b97fd32006-06-09 09:34:29 -04002223/*
2224 * Get locations and (maybe) other attributes of a referral.
2225 * Note that we'll actually follow the referral later when
2226 * we detect fsid mismatch in inode revalidation
2227 */
Trond Myklebust56659e92007-07-17 21:52:39 -04002228static int nfs4_get_referral(struct inode *dir, const struct qstr *name, struct nfs_fattr *fattr, struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002229{
2230 int status = -ENOMEM;
2231 struct page *page = NULL;
2232 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002233
2234 page = alloc_page(GFP_KERNEL);
2235 if (page == NULL)
2236 goto out;
2237 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2238 if (locations == NULL)
2239 goto out;
2240
Trond Myklebustc228fd32007-01-13 02:28:11 -05002241 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002242 if (status != 0)
2243 goto out;
2244 /* Make sure server returned a different fsid for the referral */
2245 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -07002246 dprintk("%s: server did not return a different fsid for a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002247 status = -EIO;
2248 goto out;
2249 }
2250
2251 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2252 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
2253 if (!fattr->mode)
2254 fattr->mode = S_IFDIR;
2255 memset(fhandle, 0, sizeof(struct nfs_fh));
2256out:
2257 if (page)
2258 __free_page(page);
2259 if (locations)
2260 kfree(locations);
2261 return status;
2262}
2263
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2265{
2266 struct nfs4_getattr_arg args = {
2267 .fh = fhandle,
2268 .bitmask = server->attr_bitmask,
2269 };
2270 struct nfs4_getattr_res res = {
2271 .fattr = fattr,
2272 .server = server,
2273 };
2274 struct rpc_message msg = {
2275 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2276 .rpc_argp = &args,
2277 .rpc_resp = &res,
2278 };
2279
Trond Myklebust0e574af2005-10-27 22:12:38 -04002280 nfs_fattr_init(fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002281 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282}
2283
2284static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2285{
2286 struct nfs4_exception exception = { };
2287 int err;
2288 do {
2289 err = nfs4_handle_exception(server,
2290 _nfs4_proc_getattr(server, fhandle, fattr),
2291 &exception);
2292 } while (exception.retry);
2293 return err;
2294}
2295
2296/*
2297 * The file is not closed if it is opened due to the a request to change
2298 * the size of the file. The open call will not be needed once the
2299 * VFS layer lookup-intents are implemented.
2300 *
2301 * Close is called when the inode is destroyed.
2302 * If we haven't opened the file for O_WRONLY, we
2303 * need to in the size_change case to obtain a stateid.
2304 *
2305 * Got race?
2306 * Because OPEN is always done by name in nfsv4, it is
2307 * possible that we opened a different file by the same
2308 * name. We can recognize this race condition, but we
2309 * can't do anything about it besides returning an error.
2310 *
2311 * This will be fixed with VFS changes (lookup-intent).
2312 */
2313static int
2314nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2315 struct iattr *sattr)
2316{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002317 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002318 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002319 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 int status;
2321
Trond Myklebust0e574af2005-10-27 22:12:38 -04002322 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323
Trond Myklebustd5308382005-11-04 15:33:38 -05002324 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002325 if (sattr->ia_valid & ATTR_FILE) {
2326 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002327
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002328 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002329 if (ctx) {
2330 cred = ctx->cred;
2331 state = ctx->state;
2332 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002333 }
2334
2335 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002336 if (status == 0)
2337 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 return status;
2339}
2340
Trond Myklebust56659e92007-07-17 21:52:39 -04002341static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
2342 const struct qstr *name, struct nfs_fh *fhandle,
David Howells2b3de442006-08-22 20:06:09 -04002343 struct nfs_fattr *fattr)
2344{
2345 int status;
2346 struct nfs4_lookup_arg args = {
2347 .bitmask = server->attr_bitmask,
2348 .dir_fh = dirfh,
2349 .name = name,
2350 };
2351 struct nfs4_lookup_res res = {
2352 .server = server,
2353 .fattr = fattr,
2354 .fh = fhandle,
2355 };
2356 struct rpc_message msg = {
2357 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2358 .rpc_argp = &args,
2359 .rpc_resp = &res,
2360 };
2361
2362 nfs_fattr_init(fattr);
2363
2364 dprintk("NFS call lookupfh %s\n", name->name);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002365 status = nfs4_call_sync(server, &msg, &args, &res, 0);
David Howells2b3de442006-08-22 20:06:09 -04002366 dprintk("NFS reply lookupfh: %d\n", status);
David Howells2b3de442006-08-22 20:06:09 -04002367 return status;
2368}
2369
2370static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2371 struct qstr *name, struct nfs_fh *fhandle,
2372 struct nfs_fattr *fattr)
2373{
2374 struct nfs4_exception exception = { };
2375 int err;
2376 do {
Trond Myklebust4e56e082007-07-01 18:13:52 -04002377 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
2378 /* FIXME: !!!! */
2379 if (err == -NFS4ERR_MOVED) {
2380 err = -EREMOTE;
2381 break;
2382 }
2383 err = nfs4_handle_exception(server, err, &exception);
David Howells2b3de442006-08-22 20:06:09 -04002384 } while (exception.retry);
2385 return err;
2386}
2387
Trond Myklebust56659e92007-07-17 21:52:39 -04002388static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2390{
Trond Myklebust4e56e082007-07-01 18:13:52 -04002391 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392
2393 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust4e56e082007-07-01 18:13:52 -04002394 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002395 if (status == -NFS4ERR_MOVED)
2396 status = nfs4_get_referral(dir, name, fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 dprintk("NFS reply lookup: %d\n", status);
2398 return status;
2399}
2400
2401static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2402{
2403 struct nfs4_exception exception = { };
2404 int err;
2405 do {
2406 err = nfs4_handle_exception(NFS_SERVER(dir),
2407 _nfs4_proc_lookup(dir, name, fhandle, fattr),
2408 &exception);
2409 } while (exception.retry);
2410 return err;
2411}
2412
2413static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2414{
Trond Myklebust76b32992007-08-10 17:45:11 -04002415 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 struct nfs4_accessargs args = {
2417 .fh = NFS_FH(inode),
Trond Myklebust76b32992007-08-10 17:45:11 -04002418 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002420 struct nfs4_accessres res = {
2421 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002422 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 struct rpc_message msg = {
2424 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2425 .rpc_argp = &args,
2426 .rpc_resp = &res,
2427 .rpc_cred = entry->cred,
2428 };
2429 int mode = entry->mask;
2430 int status;
2431
2432 /*
2433 * Determine which access bits we want to ask for...
2434 */
2435 if (mode & MAY_READ)
2436 args.access |= NFS4_ACCESS_READ;
2437 if (S_ISDIR(inode->i_mode)) {
2438 if (mode & MAY_WRITE)
2439 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2440 if (mode & MAY_EXEC)
2441 args.access |= NFS4_ACCESS_LOOKUP;
2442 } else {
2443 if (mode & MAY_WRITE)
2444 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2445 if (mode & MAY_EXEC)
2446 args.access |= NFS4_ACCESS_EXECUTE;
2447 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002448
2449 res.fattr = nfs_alloc_fattr();
2450 if (res.fattr == NULL)
2451 return -ENOMEM;
2452
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002453 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 if (!status) {
2455 entry->mask = 0;
2456 if (res.access & NFS4_ACCESS_READ)
2457 entry->mask |= MAY_READ;
2458 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2459 entry->mask |= MAY_WRITE;
2460 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2461 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002462 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002464 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 return status;
2466}
2467
2468static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2469{
2470 struct nfs4_exception exception = { };
2471 int err;
2472 do {
2473 err = nfs4_handle_exception(NFS_SERVER(inode),
2474 _nfs4_proc_access(inode, entry),
2475 &exception);
2476 } while (exception.retry);
2477 return err;
2478}
2479
2480/*
2481 * TODO: For the time being, we don't try to get any attributes
2482 * along with any of the zero-copy operations READ, READDIR,
2483 * READLINK, WRITE.
2484 *
2485 * In the case of the first three, we want to put the GETATTR
2486 * after the read-type operation -- this is because it is hard
2487 * to predict the length of a GETATTR response in v4, and thus
2488 * align the READ data correctly. This means that the GETATTR
2489 * may end up partially falling into the page cache, and we should
2490 * shift it into the 'tail' of the xdr_buf before processing.
2491 * To do this efficiently, we need to know the total length
2492 * of data received, which doesn't seem to be available outside
2493 * of the RPC layer.
2494 *
2495 * In the case of WRITE, we also want to put the GETATTR after
2496 * the operation -- in this case because we want to make sure
2497 * we get the post-operation mtime and size. This means that
2498 * we can't use xdr_encode_pages() as written: we need a variant
2499 * of it which would leave room in the 'tail' iovec.
2500 *
2501 * Both of these changes to the XDR layer would in fact be quite
2502 * minor, but I decided to leave them for a subsequent patch.
2503 */
2504static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2505 unsigned int pgbase, unsigned int pglen)
2506{
2507 struct nfs4_readlink args = {
2508 .fh = NFS_FH(inode),
2509 .pgbase = pgbase,
2510 .pglen = pglen,
2511 .pages = &page,
2512 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002513 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 struct rpc_message msg = {
2515 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2516 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002517 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 };
2519
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002520 return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521}
2522
2523static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2524 unsigned int pgbase, unsigned int pglen)
2525{
2526 struct nfs4_exception exception = { };
2527 int err;
2528 do {
2529 err = nfs4_handle_exception(NFS_SERVER(inode),
2530 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2531 &exception);
2532 } while (exception.retry);
2533 return err;
2534}
2535
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536/*
2537 * Got race?
2538 * We will need to arrange for the VFS layer to provide an atomic open.
2539 * Until then, this create/open method is prone to inefficiency and race
2540 * conditions due to the lookup, create, and open VFS calls from sys_open()
2541 * placed on the wire.
2542 *
2543 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2544 * The file will be opened again in the subsequent VFS open call
2545 * (nfs4_proc_file_open).
2546 *
2547 * The open for read will just hang around to be used by any process that
2548 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2549 */
2550
2551static int
2552nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebust02a913a2005-10-18 14:20:17 -07002553 int flags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554{
Trond Myklebustad389da2007-06-05 12:30:00 -04002555 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08002556 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04002557 .dentry = dentry,
2558 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 struct nfs4_state *state;
2560 struct rpc_cred *cred;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002561 fmode_t fmode = flags & (FMODE_READ | FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 int status = 0;
2563
Trond Myklebust98a8e322008-03-12 12:25:28 -04002564 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 if (IS_ERR(cred)) {
2566 status = PTR_ERR(cred);
2567 goto out;
2568 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002569 state = nfs4_do_open(dir, &path, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002570 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 if (IS_ERR(state)) {
2572 status = PTR_ERR(state);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002573 goto out_putcred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002575 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002576 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustad389da2007-06-05 12:30:00 -04002577 if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002578 status = nfs4_intent_set_file(nd, &path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002579 else
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002580 nfs4_close_sync(&path, state, fmode);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002581out_putcred:
2582 put_rpccred(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583out:
2584 return status;
2585}
2586
2587static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2588{
Trond Myklebust16e42952005-10-27 22:12:44 -04002589 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002590 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002592 .name.len = name->len,
2593 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002594 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002596 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002597 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002598 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002600 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2601 .rpc_argp = &args,
2602 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 };
Trond Myklebustd3468902010-04-16 16:22:50 -04002604 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605
Trond Myklebustd3468902010-04-16 16:22:50 -04002606 res.dir_attr = nfs_alloc_fattr();
2607 if (res.dir_attr == NULL)
2608 goto out;
2609
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002610 status = nfs4_call_sync(server, &msg, &args, &res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002611 if (status == 0) {
2612 update_changeattr(dir, &res.cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002613 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002614 }
Trond Myklebustd3468902010-04-16 16:22:50 -04002615 nfs_free_fattr(res.dir_attr);
2616out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 return status;
2618}
2619
2620static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2621{
2622 struct nfs4_exception exception = { };
2623 int err;
2624 do {
2625 err = nfs4_handle_exception(NFS_SERVER(dir),
2626 _nfs4_proc_remove(dir, name),
2627 &exception);
2628 } while (exception.retry);
2629 return err;
2630}
2631
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002632static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002634 struct nfs_server *server = NFS_SERVER(dir);
2635 struct nfs_removeargs *args = msg->rpc_argp;
2636 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637
Trond Myklebusta65318b2009-03-11 14:10:28 -04002638 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002639 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641}
2642
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002643static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002645 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2646
Andy Adamson472cfbd2009-04-01 09:22:24 -04002647 nfs4_sequence_done(res->server, &res->seq_res, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002648 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002649 return 0;
2650 update_changeattr(dir, &res->cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002651 nfs_post_op_update_inode(dir, res->dir_attr);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002652 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653}
2654
2655static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2656 struct inode *new_dir, struct qstr *new_name)
2657{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002658 struct nfs_server *server = NFS_SERVER(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 struct nfs4_rename_arg arg = {
2660 .old_dir = NFS_FH(old_dir),
2661 .new_dir = NFS_FH(new_dir),
2662 .old_name = old_name,
2663 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002664 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 };
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002666 struct nfs4_rename_res res = {
2667 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002668 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 struct rpc_message msg = {
2670 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2671 .rpc_argp = &arg,
2672 .rpc_resp = &res,
2673 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002674 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675
Trond Myklebust011fff72010-04-16 16:22:49 -04002676 res.old_fattr = nfs_alloc_fattr();
2677 res.new_fattr = nfs_alloc_fattr();
2678 if (res.old_fattr == NULL || res.new_fattr == NULL)
2679 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680
Trond Myklebust011fff72010-04-16 16:22:49 -04002681 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 if (!status) {
2683 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002684 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002686 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 }
Trond Myklebust011fff72010-04-16 16:22:49 -04002688out:
2689 nfs_free_fattr(res.new_fattr);
2690 nfs_free_fattr(res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 return status;
2692}
2693
2694static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2695 struct inode *new_dir, struct qstr *new_name)
2696{
2697 struct nfs4_exception exception = { };
2698 int err;
2699 do {
2700 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2701 _nfs4_proc_rename(old_dir, old_name,
2702 new_dir, new_name),
2703 &exception);
2704 } while (exception.retry);
2705 return err;
2706}
2707
2708static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2709{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002710 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 struct nfs4_link_arg arg = {
2712 .fh = NFS_FH(inode),
2713 .dir_fh = NFS_FH(dir),
2714 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002715 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002717 struct nfs4_link_res res = {
2718 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002719 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 struct rpc_message msg = {
2721 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2722 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002723 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 };
Trond Myklebust136f2622010-04-16 16:22:49 -04002725 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726
Trond Myklebust136f2622010-04-16 16:22:49 -04002727 res.fattr = nfs_alloc_fattr();
2728 res.dir_attr = nfs_alloc_fattr();
2729 if (res.fattr == NULL || res.dir_attr == NULL)
2730 goto out;
2731
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002732 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002733 if (!status) {
2734 update_changeattr(dir, &res.cinfo);
2735 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002736 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002737 }
Trond Myklebust136f2622010-04-16 16:22:49 -04002738out:
2739 nfs_free_fattr(res.dir_attr);
2740 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 return status;
2742}
2743
2744static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2745{
2746 struct nfs4_exception exception = { };
2747 int err;
2748 do {
2749 err = nfs4_handle_exception(NFS_SERVER(inode),
2750 _nfs4_proc_link(inode, dir, name),
2751 &exception);
2752 } while (exception.retry);
2753 return err;
2754}
2755
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002756struct nfs4_createdata {
2757 struct rpc_message msg;
2758 struct nfs4_create_arg arg;
2759 struct nfs4_create_res res;
2760 struct nfs_fh fh;
2761 struct nfs_fattr fattr;
2762 struct nfs_fattr dir_fattr;
2763};
2764
2765static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2766 struct qstr *name, struct iattr *sattr, u32 ftype)
2767{
2768 struct nfs4_createdata *data;
2769
2770 data = kzalloc(sizeof(*data), GFP_KERNEL);
2771 if (data != NULL) {
2772 struct nfs_server *server = NFS_SERVER(dir);
2773
2774 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2775 data->msg.rpc_argp = &data->arg;
2776 data->msg.rpc_resp = &data->res;
2777 data->arg.dir_fh = NFS_FH(dir);
2778 data->arg.server = server;
2779 data->arg.name = name;
2780 data->arg.attrs = sattr;
2781 data->arg.ftype = ftype;
2782 data->arg.bitmask = server->attr_bitmask;
2783 data->res.server = server;
2784 data->res.fh = &data->fh;
2785 data->res.fattr = &data->fattr;
2786 data->res.dir_fattr = &data->dir_fattr;
2787 nfs_fattr_init(data->res.fattr);
2788 nfs_fattr_init(data->res.dir_fattr);
2789 }
2790 return data;
2791}
2792
2793static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2794{
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002795 int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
2796 &data->arg, &data->res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002797 if (status == 0) {
2798 update_changeattr(dir, &data->res.dir_cinfo);
2799 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2800 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2801 }
2802 return status;
2803}
2804
2805static void nfs4_free_createdata(struct nfs4_createdata *data)
2806{
2807 kfree(data);
2808}
2809
Chuck Lever4f390c12006-08-22 20:06:22 -04002810static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002811 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002813 struct nfs4_createdata *data;
2814 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815
Chuck Lever94a6d752006-08-22 20:06:23 -04002816 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002817 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002818
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002819 status = -ENOMEM;
2820 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2821 if (data == NULL)
2822 goto out;
2823
2824 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2825 data->arg.u.symlink.pages = &page;
2826 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002828 status = nfs4_do_create(dir, dentry, data);
2829
2830 nfs4_free_createdata(data);
2831out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 return status;
2833}
2834
Chuck Lever4f390c12006-08-22 20:06:22 -04002835static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002836 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837{
2838 struct nfs4_exception exception = { };
2839 int err;
2840 do {
2841 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002842 _nfs4_proc_symlink(dir, dentry, page,
2843 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 &exception);
2845 } while (exception.retry);
2846 return err;
2847}
2848
2849static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2850 struct iattr *sattr)
2851{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002852 struct nfs4_createdata *data;
2853 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002855 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2856 if (data == NULL)
2857 goto out;
2858
2859 status = nfs4_do_create(dir, dentry, data);
2860
2861 nfs4_free_createdata(data);
2862out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 return status;
2864}
2865
2866static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2867 struct iattr *sattr)
2868{
2869 struct nfs4_exception exception = { };
2870 int err;
2871 do {
2872 err = nfs4_handle_exception(NFS_SERVER(dir),
2873 _nfs4_proc_mkdir(dir, dentry, sattr),
2874 &exception);
2875 } while (exception.retry);
2876 return err;
2877}
2878
2879static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2880 u64 cookie, struct page *page, unsigned int count, int plus)
2881{
2882 struct inode *dir = dentry->d_inode;
2883 struct nfs4_readdir_arg args = {
2884 .fh = NFS_FH(dir),
2885 .pages = &page,
2886 .pgbase = 0,
2887 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09002888 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 };
2890 struct nfs4_readdir_res res;
2891 struct rpc_message msg = {
2892 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2893 .rpc_argp = &args,
2894 .rpc_resp = &res,
2895 .rpc_cred = cred,
2896 };
2897 int status;
2898
Harvey Harrison3110ff82008-05-02 13:42:44 -07002899 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00002900 dentry->d_parent->d_name.name,
2901 dentry->d_name.name,
2902 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2904 res.pgbase = args.pgbase;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002905 status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 if (status == 0)
2907 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustc4812992007-09-28 17:11:45 -04002908
2909 nfs_invalidate_atime(dir);
2910
Harvey Harrison3110ff82008-05-02 13:42:44 -07002911 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 return status;
2913}
2914
2915static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2916 u64 cookie, struct page *page, unsigned int count, int plus)
2917{
2918 struct nfs4_exception exception = { };
2919 int err;
2920 do {
2921 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2922 _nfs4_proc_readdir(dentry, cred, cookie,
2923 page, count, plus),
2924 &exception);
2925 } while (exception.retry);
2926 return err;
2927}
2928
2929static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2930 struct iattr *sattr, dev_t rdev)
2931{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002932 struct nfs4_createdata *data;
2933 int mode = sattr->ia_mode;
2934 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935
2936 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2937 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002938
2939 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2940 if (data == NULL)
2941 goto out;
2942
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002944 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002946 data->arg.ftype = NF4BLK;
2947 data->arg.u.device.specdata1 = MAJOR(rdev);
2948 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 }
2950 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002951 data->arg.ftype = NF4CHR;
2952 data->arg.u.device.specdata1 = MAJOR(rdev);
2953 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002956 status = nfs4_do_create(dir, dentry, data);
2957
2958 nfs4_free_createdata(data);
2959out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 return status;
2961}
2962
2963static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2964 struct iattr *sattr, dev_t rdev)
2965{
2966 struct nfs4_exception exception = { };
2967 int err;
2968 do {
2969 err = nfs4_handle_exception(NFS_SERVER(dir),
2970 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2971 &exception);
2972 } while (exception.retry);
2973 return err;
2974}
2975
2976static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2977 struct nfs_fsstat *fsstat)
2978{
2979 struct nfs4_statfs_arg args = {
2980 .fh = fhandle,
2981 .bitmask = server->attr_bitmask,
2982 };
Benny Halevy24ad1482009-04-01 09:21:56 -04002983 struct nfs4_statfs_res res = {
2984 .fsstat = fsstat,
2985 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 struct rpc_message msg = {
2987 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2988 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04002989 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 };
2991
Trond Myklebust0e574af2005-10-27 22:12:38 -04002992 nfs_fattr_init(fsstat->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002993 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994}
2995
2996static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2997{
2998 struct nfs4_exception exception = { };
2999 int err;
3000 do {
3001 err = nfs4_handle_exception(server,
3002 _nfs4_proc_statfs(server, fhandle, fsstat),
3003 &exception);
3004 } while (exception.retry);
3005 return err;
3006}
3007
3008static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3009 struct nfs_fsinfo *fsinfo)
3010{
3011 struct nfs4_fsinfo_arg args = {
3012 .fh = fhandle,
3013 .bitmask = server->attr_bitmask,
3014 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003015 struct nfs4_fsinfo_res res = {
3016 .fsinfo = fsinfo,
3017 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 struct rpc_message msg = {
3019 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3020 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003021 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 };
3023
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003024 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025}
3026
3027static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3028{
3029 struct nfs4_exception exception = { };
3030 int err;
3031
3032 do {
3033 err = nfs4_handle_exception(server,
3034 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3035 &exception);
3036 } while (exception.retry);
3037 return err;
3038}
3039
3040static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3041{
Trond Myklebust0e574af2005-10-27 22:12:38 -04003042 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3044}
3045
3046static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3047 struct nfs_pathconf *pathconf)
3048{
3049 struct nfs4_pathconf_arg args = {
3050 .fh = fhandle,
3051 .bitmask = server->attr_bitmask,
3052 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003053 struct nfs4_pathconf_res res = {
3054 .pathconf = pathconf,
3055 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 struct rpc_message msg = {
3057 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3058 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003059 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 };
3061
3062 /* None of the pathconf attributes are mandatory to implement */
3063 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3064 memset(pathconf, 0, sizeof(*pathconf));
3065 return 0;
3066 }
3067
Trond Myklebust0e574af2005-10-27 22:12:38 -04003068 nfs_fattr_init(pathconf->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003069 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070}
3071
3072static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3073 struct nfs_pathconf *pathconf)
3074{
3075 struct nfs4_exception exception = { };
3076 int err;
3077
3078 do {
3079 err = nfs4_handle_exception(server,
3080 _nfs4_proc_pathconf(server, fhandle, pathconf),
3081 &exception);
3082 } while (exception.retry);
3083 return err;
3084}
3085
Trond Myklebustec06c092006-03-20 13:44:27 -05003086static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087{
Trond Myklebustec06c092006-03-20 13:44:27 -05003088 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089
Andy Adamsonf11c88a2009-04-01 09:22:25 -04003090 dprintk("--> %s\n", __func__);
3091
Andy Adamsonf11c88a2009-04-01 09:22:25 -04003092 nfs4_sequence_done(server, &data->res.seq_res, task->tk_status);
3093
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003094 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003095 nfs_restart_rpc(task, server->nfs_client);
Trond Myklebustec06c092006-03-20 13:44:27 -05003096 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003098
3099 nfs_invalidate_atime(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003101 renew_lease(server, data->timestamp);
3102 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103}
3104
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003105static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 data->timestamp = jiffies;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003108 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109}
3110
Trond Myklebust788e7a82006-03-20 13:44:27 -05003111static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 struct inode *inode = data->inode;
3114
Andy Adamsondef6ed72009-04-01 09:22:26 -04003115 nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
3116 task->tk_status);
3117
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003118 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003119 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003120 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003122 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003124 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003125 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003126 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127}
3128
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003129static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003131 struct nfs_server *server = NFS_SERVER(data->inode);
3132
Trond Myklebusta65318b2009-03-11 14:10:28 -04003133 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003134 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 data->timestamp = jiffies;
3136
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003137 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138}
3139
Trond Myklebust788e7a82006-03-20 13:44:27 -05003140static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 struct inode *inode = data->inode;
3143
Andy Adamson21d9a852009-04-01 09:22:27 -04003144 nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
3145 task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003146 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003147 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003148 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003150 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003151 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152}
3153
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003154static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003156 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157
Trond Myklebusta65318b2009-03-11 14:10:28 -04003158 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003159 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003160 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161}
3162
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003163struct nfs4_renewdata {
3164 struct nfs_client *client;
3165 unsigned long timestamp;
3166};
3167
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168/*
3169 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3170 * standalone procedure for queueing an asynchronous RENEW.
3171 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003172static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003173{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003174 struct nfs4_renewdata *data = calldata;
3175 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003176
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003177 if (atomic_read(&clp->cl_count) > 1)
3178 nfs4_schedule_state_renewal(clp);
3179 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003180 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003181}
3182
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003183static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003185 struct nfs4_renewdata *data = calldata;
3186 struct nfs_client *clp = data->client;
3187 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188
3189 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003190 /* Unless we're shutting down, schedule state recovery! */
3191 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
3192 nfs4_schedule_state_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 return;
3194 }
3195 spin_lock(&clp->cl_lock);
3196 if (time_before(clp->cl_last_renewal,timestamp))
3197 clp->cl_last_renewal = timestamp;
3198 spin_unlock(&clp->cl_lock);
3199}
3200
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003201static const struct rpc_call_ops nfs4_renew_ops = {
3202 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003203 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003204};
3205
David Howellsadfa6f92006-08-22 20:06:08 -04003206int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207{
3208 struct rpc_message msg = {
3209 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3210 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003211 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003213 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003215 if (!atomic_inc_not_zero(&clp->cl_count))
3216 return -EIO;
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003217 data = kmalloc(sizeof(*data), GFP_KERNEL);
3218 if (data == NULL)
3219 return -ENOMEM;
3220 data->client = clp;
3221 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003223 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224}
3225
David Howellsadfa6f92006-08-22 20:06:08 -04003226int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227{
3228 struct rpc_message msg = {
3229 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3230 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003231 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 };
3233 unsigned long now = jiffies;
3234 int status;
3235
3236 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3237 if (status < 0)
3238 return status;
3239 spin_lock(&clp->cl_lock);
3240 if (time_before(clp->cl_last_renewal,now))
3241 clp->cl_last_renewal = now;
3242 spin_unlock(&clp->cl_lock);
3243 return 0;
3244}
3245
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003246static inline int nfs4_server_supports_acls(struct nfs_server *server)
3247{
3248 return (server->caps & NFS_CAP_ACLS)
3249 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3250 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3251}
3252
3253/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3254 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3255 * the stack.
3256 */
3257#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3258
3259static void buf_to_pages(const void *buf, size_t buflen,
3260 struct page **pages, unsigned int *pgbase)
3261{
3262 const void *p = buf;
3263
3264 *pgbase = offset_in_page(buf);
3265 p -= *pgbase;
3266 while (p < buf + buflen) {
3267 *(pages++) = virt_to_page(p);
3268 p += PAGE_CACHE_SIZE;
3269 }
3270}
3271
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003272struct nfs4_cached_acl {
3273 int cached;
3274 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003275 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003276};
3277
3278static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003279{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003280 struct nfs_inode *nfsi = NFS_I(inode);
3281
3282 spin_lock(&inode->i_lock);
3283 kfree(nfsi->nfs4_acl);
3284 nfsi->nfs4_acl = acl;
3285 spin_unlock(&inode->i_lock);
3286}
3287
3288static void nfs4_zap_acl_attr(struct inode *inode)
3289{
3290 nfs4_set_cached_acl(inode, NULL);
3291}
3292
3293static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3294{
3295 struct nfs_inode *nfsi = NFS_I(inode);
3296 struct nfs4_cached_acl *acl;
3297 int ret = -ENOENT;
3298
3299 spin_lock(&inode->i_lock);
3300 acl = nfsi->nfs4_acl;
3301 if (acl == NULL)
3302 goto out;
3303 if (buf == NULL) /* user is just asking for length */
3304 goto out_len;
3305 if (acl->cached == 0)
3306 goto out;
3307 ret = -ERANGE; /* see getxattr(2) man page */
3308 if (acl->len > buflen)
3309 goto out;
3310 memcpy(buf, acl->data, acl->len);
3311out_len:
3312 ret = acl->len;
3313out:
3314 spin_unlock(&inode->i_lock);
3315 return ret;
3316}
3317
3318static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3319{
3320 struct nfs4_cached_acl *acl;
3321
3322 if (buf && acl_len <= PAGE_SIZE) {
3323 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3324 if (acl == NULL)
3325 goto out;
3326 acl->cached = 1;
3327 memcpy(acl->data, buf, acl_len);
3328 } else {
3329 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3330 if (acl == NULL)
3331 goto out;
3332 acl->cached = 0;
3333 }
3334 acl->len = acl_len;
3335out:
3336 nfs4_set_cached_acl(inode, acl);
3337}
3338
Trond Myklebust16b42892006-08-24 12:27:15 -04003339static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003340{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003341 struct page *pages[NFS4ACL_MAXPAGES];
3342 struct nfs_getaclargs args = {
3343 .fh = NFS_FH(inode),
3344 .acl_pages = pages,
3345 .acl_len = buflen,
3346 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003347 struct nfs_getaclres res = {
3348 .acl_len = buflen,
3349 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003350 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003351 struct rpc_message msg = {
3352 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3353 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003354 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003355 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003356 struct page *localpage = NULL;
3357 int ret;
3358
3359 if (buflen < PAGE_SIZE) {
3360 /* As long as we're doing a round trip to the server anyway,
3361 * let's be prepared for a page of acl data. */
3362 localpage = alloc_page(GFP_KERNEL);
3363 resp_buf = page_address(localpage);
3364 if (localpage == NULL)
3365 return -ENOMEM;
3366 args.acl_pages[0] = localpage;
3367 args.acl_pgbase = 0;
Benny Halevy663c79b2009-04-01 09:21:59 -04003368 args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003369 } else {
3370 resp_buf = buf;
3371 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3372 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003373 ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003374 if (ret)
3375 goto out_free;
Benny Halevy663c79b2009-04-01 09:21:59 -04003376 if (res.acl_len > args.acl_len)
3377 nfs4_write_cached_acl(inode, NULL, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003378 else
Benny Halevy663c79b2009-04-01 09:21:59 -04003379 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003380 if (buf) {
3381 ret = -ERANGE;
Benny Halevy663c79b2009-04-01 09:21:59 -04003382 if (res.acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003383 goto out_free;
3384 if (localpage)
Benny Halevy663c79b2009-04-01 09:21:59 -04003385 memcpy(buf, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003386 }
Benny Halevy663c79b2009-04-01 09:21:59 -04003387 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003388out_free:
3389 if (localpage)
3390 __free_page(localpage);
3391 return ret;
3392}
3393
Trond Myklebust16b42892006-08-24 12:27:15 -04003394static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3395{
3396 struct nfs4_exception exception = { };
3397 ssize_t ret;
3398 do {
3399 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3400 if (ret >= 0)
3401 break;
3402 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3403 } while (exception.retry);
3404 return ret;
3405}
3406
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003407static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3408{
3409 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003410 int ret;
3411
3412 if (!nfs4_server_supports_acls(server))
3413 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003414 ret = nfs_revalidate_inode(server, inode);
3415 if (ret < 0)
3416 return ret;
3417 ret = nfs4_read_cached_acl(inode, buf, buflen);
3418 if (ret != -ENOENT)
3419 return ret;
3420 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003421}
3422
Trond Myklebust16b42892006-08-24 12:27:15 -04003423static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003424{
3425 struct nfs_server *server = NFS_SERVER(inode);
3426 struct page *pages[NFS4ACL_MAXPAGES];
3427 struct nfs_setaclargs arg = {
3428 .fh = NFS_FH(inode),
3429 .acl_pages = pages,
3430 .acl_len = buflen,
3431 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003432 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003433 struct rpc_message msg = {
3434 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3435 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003436 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003437 };
3438 int ret;
3439
3440 if (!nfs4_server_supports_acls(server))
3441 return -EOPNOTSUPP;
Trond Myklebust642ac542005-10-18 14:20:19 -07003442 nfs_inode_return_delegation(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003443 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003444 ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003445 nfs_access_zap_cache(inode);
3446 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003447 return ret;
3448}
3449
Trond Myklebust16b42892006-08-24 12:27:15 -04003450static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3451{
3452 struct nfs4_exception exception = { };
3453 int err;
3454 do {
3455 err = nfs4_handle_exception(NFS_SERVER(inode),
3456 __nfs4_proc_set_acl(inode, buf, buflen),
3457 &exception);
3458 } while (exception.retry);
3459 return err;
3460}
3461
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003463nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003465 struct nfs_client *clp = server->nfs_client;
3466
3467 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 return 0;
3469 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003470 case -NFS4ERR_ADMIN_REVOKED:
3471 case -NFS4ERR_BAD_STATEID:
3472 case -NFS4ERR_OPENMODE:
3473 if (state == NULL)
3474 break;
3475 nfs4_state_mark_reclaim_nograce(clp, state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003476 goto do_state_recovery;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003478 if (state == NULL)
3479 break;
3480 nfs4_state_mark_reclaim_reboot(clp, state);
3481 case -NFS4ERR_STALE_CLIENTID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482 case -NFS4ERR_EXPIRED:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003483 goto do_state_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003484#if defined(CONFIG_NFS_V4_1)
3485 case -NFS4ERR_BADSESSION:
3486 case -NFS4ERR_BADSLOT:
3487 case -NFS4ERR_BAD_HIGH_SLOT:
3488 case -NFS4ERR_DEADSESSION:
3489 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3490 case -NFS4ERR_SEQ_FALSE_RETRY:
3491 case -NFS4ERR_SEQ_MISORDERED:
3492 dprintk("%s ERROR %d, Reset session\n", __func__,
3493 task->tk_status);
Andy Adamson0b9e2d42009-12-04 16:02:14 -05003494 nfs4_schedule_state_recovery(clp);
Andy Adamson4745e312009-04-01 09:22:42 -04003495 task->tk_status = 0;
3496 return -EAGAIN;
3497#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003499 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003500 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003501 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3503 task->tk_status = 0;
3504 return -EAGAIN;
3505 case -NFS4ERR_OLD_STATEID:
3506 task->tk_status = 0;
3507 return -EAGAIN;
3508 }
3509 task->tk_status = nfs4_map_errors(task->tk_status);
3510 return 0;
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003511do_state_recovery:
3512 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
3513 nfs4_schedule_state_recovery(clp);
3514 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3515 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3516 task->tk_status = 0;
3517 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518}
3519
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003520int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3521 unsigned short port, struct rpc_cred *cred,
3522 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523{
3524 nfs4_verifier sc_verifier;
3525 struct nfs4_setclientid setclientid = {
3526 .sc_verifier = &sc_verifier,
3527 .sc_prog = program,
3528 };
3529 struct rpc_message msg = {
3530 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3531 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003532 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003533 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534 };
Al Virobc4785c2006-10-19 23:28:51 -07003535 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 int loop = 0;
3537 int status;
3538
Al Virobc4785c2006-10-19 23:28:51 -07003539 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3541 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3542
3543 for(;;) {
3544 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003545 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003546 clp->cl_ipaddr,
3547 rpc_peeraddr2str(clp->cl_rpcclient,
3548 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003549 rpc_peeraddr2str(clp->cl_rpcclient,
3550 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003551 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 clp->cl_id_uniquifier);
3553 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003554 sizeof(setclientid.sc_netid),
3555 rpc_peeraddr2str(clp->cl_rpcclient,
3556 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003558 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 clp->cl_ipaddr, port >> 8, port & 255);
3560
3561 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3562 if (status != -NFS4ERR_CLID_INUSE)
3563 break;
3564 if (signalled())
3565 break;
3566 if (loop++ & 1)
3567 ssleep(clp->cl_lease_time + 1);
3568 else
3569 if (++clp->cl_id_uniquifier == 0)
3570 break;
3571 }
3572 return status;
3573}
3574
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003575static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3576 struct nfs4_setclientid_res *arg,
3577 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578{
3579 struct nfs_fsinfo fsinfo;
3580 struct rpc_message msg = {
3581 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003582 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003584 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 };
3586 unsigned long now;
3587 int status;
3588
3589 now = jiffies;
3590 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3591 if (status == 0) {
3592 spin_lock(&clp->cl_lock);
3593 clp->cl_lease_time = fsinfo.lease_time * HZ;
3594 clp->cl_last_renewal = now;
3595 spin_unlock(&clp->cl_lock);
3596 }
3597 return status;
3598}
3599
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003600int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3601 struct nfs4_setclientid_res *arg,
3602 struct rpc_cred *cred)
Trond Myklebust51581f32006-03-20 13:44:47 -05003603{
Benny Halevy77e03672008-06-24 20:25:57 +03003604 long timeout = 0;
Trond Myklebust51581f32006-03-20 13:44:47 -05003605 int err;
3606 do {
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003607 err = _nfs4_proc_setclientid_confirm(clp, arg, cred);
Trond Myklebust51581f32006-03-20 13:44:47 -05003608 switch (err) {
3609 case 0:
3610 return err;
3611 case -NFS4ERR_RESOURCE:
3612 /* The IBM lawyers misread another document! */
3613 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -05003614 case -EKEYEXPIRED:
Trond Myklebust51581f32006-03-20 13:44:47 -05003615 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3616 }
3617 } while (err == 0);
3618 return err;
3619}
3620
Trond Myklebustfe650402006-01-03 09:55:18 +01003621struct nfs4_delegreturndata {
3622 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003623 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003624 struct nfs_fh fh;
3625 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003626 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003627 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003628 int rpc_status;
3629};
3630
Trond Myklebustfe650402006-01-03 09:55:18 +01003631static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3632{
3633 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003634
Trond Myklebustd61e6122009-12-05 19:32:19 -05003635 nfs4_sequence_done(data->res.server, &data->res.seq_res,
3636 task->tk_status);
Andy Adamson938e1012009-04-01 09:22:28 -04003637
Ricardo Labiaga79708862009-12-07 09:23:21 -05003638 switch (task->tk_status) {
3639 case -NFS4ERR_STALE_STATEID:
3640 case -NFS4ERR_EXPIRED:
3641 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01003642 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003643 break;
3644 default:
3645 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3646 -EAGAIN) {
3647 nfs_restart_rpc(task, data->res.server->nfs_client);
3648 return;
3649 }
3650 }
3651 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01003652}
3653
3654static void nfs4_delegreturn_release(void *calldata)
3655{
Trond Myklebustfe650402006-01-03 09:55:18 +01003656 kfree(calldata);
3657}
3658
Andy Adamson938e1012009-04-01 09:22:28 -04003659#if defined(CONFIG_NFS_V4_1)
3660static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3661{
3662 struct nfs4_delegreturndata *d_data;
3663
3664 d_data = (struct nfs4_delegreturndata *)data;
3665
Trond Myklebust035168a2010-06-16 09:52:26 -04003666 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04003667 &d_data->args.seq_args,
3668 &d_data->res.seq_res, 1, task))
3669 return;
3670 rpc_call_start(task);
3671}
3672#endif /* CONFIG_NFS_V4_1 */
3673
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003674static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04003675#if defined(CONFIG_NFS_V4_1)
3676 .rpc_call_prepare = nfs4_delegreturn_prepare,
3677#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01003678 .rpc_call_done = nfs4_delegreturn_done,
3679 .rpc_release = nfs4_delegreturn_release,
3680};
3681
Trond Myklebuste6f81072008-01-24 18:14:34 -05003682static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003683{
3684 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003685 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003686 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003687 struct rpc_message msg = {
3688 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3689 .rpc_cred = cred,
3690 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003691 struct rpc_task_setup task_setup_data = {
3692 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003693 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003694 .callback_ops = &nfs4_delegreturn_ops,
3695 .flags = RPC_TASK_ASYNC,
3696 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003697 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003698
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003699 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01003700 if (data == NULL)
3701 return -ENOMEM;
3702 data->args.fhandle = &data->fh;
3703 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003704 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003705 nfs_copy_fh(&data->fh, NFS_FH(inode));
3706 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003707 data->res.fattr = &data->fattr;
3708 data->res.server = server;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003709 data->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003710 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003711 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003712 data->rpc_status = 0;
3713
Trond Myklebustc970aa82007-07-14 15:39:59 -04003714 task_setup_data.callback_data = data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003715 msg.rpc_argp = &data->args,
3716 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003717 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003718 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003719 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003720 if (!issync)
3721 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003722 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003723 if (status != 0)
3724 goto out;
3725 status = data->rpc_status;
3726 if (status != 0)
3727 goto out;
3728 nfs_refresh_inode(inode, &data->fattr);
3729out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003730 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003731 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732}
3733
Trond Myklebuste6f81072008-01-24 18:14:34 -05003734int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735{
3736 struct nfs_server *server = NFS_SERVER(inode);
3737 struct nfs4_exception exception = { };
3738 int err;
3739 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003740 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741 switch (err) {
3742 case -NFS4ERR_STALE_STATEID:
3743 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744 case 0:
3745 return 0;
3746 }
3747 err = nfs4_handle_exception(server, err, &exception);
3748 } while (exception.retry);
3749 return err;
3750}
3751
3752#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3753#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3754
3755/*
3756 * sleep, with exponential backoff, and retry the LOCK operation.
3757 */
3758static unsigned long
3759nfs4_set_lock_task_retry(unsigned long timeout)
3760{
Matthew Wilcox150030b2007-12-06 16:24:39 -05003761 schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762 timeout <<= 1;
3763 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3764 return NFS4_LOCK_MAXTIMEOUT;
3765 return timeout;
3766}
3767
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3769{
3770 struct inode *inode = state->inode;
3771 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003772 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003773 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003775 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003777 struct nfs_lockt_res res = {
3778 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779 };
3780 struct rpc_message msg = {
3781 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3782 .rpc_argp = &arg,
3783 .rpc_resp = &res,
3784 .rpc_cred = state->owner->so_cred,
3785 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 struct nfs4_lock_state *lsp;
3787 int status;
3788
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003789 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003790 status = nfs4_set_lock_state(state, request);
3791 if (status != 0)
3792 goto out;
3793 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003794 arg.lock_owner.id = lsp->ls_id.id;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003795 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003796 switch (status) {
3797 case 0:
3798 request->fl_type = F_UNLCK;
3799 break;
3800 case -NFS4ERR_DENIED:
3801 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05003803 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003804out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805 return status;
3806}
3807
3808static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3809{
3810 struct nfs4_exception exception = { };
3811 int err;
3812
3813 do {
3814 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3815 _nfs4_proc_getlk(state, cmd, request),
3816 &exception);
3817 } while (exception.retry);
3818 return err;
3819}
3820
3821static int do_vfs_lock(struct file *file, struct file_lock *fl)
3822{
3823 int res = 0;
3824 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3825 case FL_POSIX:
3826 res = posix_lock_file_wait(file, fl);
3827 break;
3828 case FL_FLOCK:
3829 res = flock_lock_file_wait(file, fl);
3830 break;
3831 default:
3832 BUG();
3833 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834 return res;
3835}
3836
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003837struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003838 struct nfs_locku_args arg;
3839 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003840 struct nfs4_lock_state *lsp;
3841 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003842 struct file_lock fl;
3843 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003844 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003845};
3846
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003847static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3848 struct nfs_open_context *ctx,
3849 struct nfs4_lock_state *lsp,
3850 struct nfs_seqid *seqid)
3851{
3852 struct nfs4_unlockdata *p;
3853 struct inode *inode = lsp->ls_state->inode;
3854
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003855 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003856 if (p == NULL)
3857 return NULL;
3858 p->arg.fh = NFS_FH(inode);
3859 p->arg.fl = &p->fl;
3860 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003861 p->res.seqid = seqid;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003862 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003863 p->arg.stateid = &lsp->ls_stateid;
3864 p->lsp = lsp;
3865 atomic_inc(&lsp->ls_count);
3866 /* Ensure we don't close file until we're done freeing locks! */
3867 p->ctx = get_nfs_open_context(ctx);
3868 memcpy(&p->fl, fl, sizeof(p->fl));
3869 p->server = NFS_SERVER(inode);
3870 return p;
3871}
3872
Trond Myklebust06f814a2006-01-03 09:55:07 +01003873static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003874{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003875 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003876 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003877 nfs4_put_lock_state(calldata->lsp);
3878 put_nfs_open_context(calldata->ctx);
3879 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003880}
3881
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003882static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003883{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003884 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003885
Andy Adamsona8936932009-04-01 09:22:23 -04003886 nfs4_sequence_done(calldata->server, &calldata->res.seq_res,
3887 task->tk_status);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003888 if (RPC_ASSASSINATED(task))
3889 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003890 switch (task->tk_status) {
3891 case 0:
3892 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003893 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003894 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01003895 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003896 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003897 case -NFS4ERR_BAD_STATEID:
3898 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003899 case -NFS4ERR_STALE_STATEID:
3900 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003901 break;
3902 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003903 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebust0110ee12009-12-07 09:00:24 -05003904 nfs_restart_rpc(task,
Trond Myklebustd61e6122009-12-05 19:32:19 -05003905 calldata->server->nfs_client);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003906 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003907}
3908
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003909static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003910{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003911 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003913 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003914 return;
3915 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003916 /* Note: exit _without_ running nfs4_locku_done */
3917 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003918 return;
3919 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01003920 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04003921 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04003922 &calldata->arg.seq_args,
3923 &calldata->res.seq_res, 1, task))
3924 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003925 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926}
3927
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003928static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003929 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003930 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01003931 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003932};
3933
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003934static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3935 struct nfs_open_context *ctx,
3936 struct nfs4_lock_state *lsp,
3937 struct nfs_seqid *seqid)
3938{
3939 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003940 struct rpc_message msg = {
3941 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3942 .rpc_cred = ctx->cred,
3943 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003944 struct rpc_task_setup task_setup_data = {
3945 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04003946 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003947 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003948 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003949 .flags = RPC_TASK_ASYNC,
3950 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003951
Frank Filz137d6ac2007-07-09 15:32:29 -07003952 /* Ensure this is an unlock - when canceling a lock, the
3953 * canceled lock is passed in, and it won't be an unlock.
3954 */
3955 fl->fl_type = F_UNLCK;
3956
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003957 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3958 if (data == NULL) {
3959 nfs_free_seqid(seqid);
3960 return ERR_PTR(-ENOMEM);
3961 }
3962
Trond Myklebust5138fde2007-07-14 15:40:01 -04003963 msg.rpc_argp = &data->arg,
3964 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003965 task_setup_data.callback_data = data;
3966 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003967}
3968
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3970{
Trond Myklebust19e03c52008-12-23 15:21:44 -05003971 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003972 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003973 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01003974 struct rpc_task *task;
3975 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003976 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977
Trond Myklebust9b073572006-06-29 16:38:34 -04003978 status = nfs4_set_lock_state(state, request);
3979 /* Unlock _before_ we do the RPC call */
3980 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003981 down_read(&nfsi->rwsem);
3982 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
3983 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003984 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003985 }
3986 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003987 if (status != 0)
3988 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003989 /* Is this a delegated lock? */
3990 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04003991 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003992 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003993 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04003994 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003995 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04003996 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04003997 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003998 status = PTR_ERR(task);
3999 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004000 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004001 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004002 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004003out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004004 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004005 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006}
4007
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004008struct nfs4_lockdata {
4009 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004010 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004011 struct nfs4_lock_state *lsp;
4012 struct nfs_open_context *ctx;
4013 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004014 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004015 int rpc_status;
4016 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004017 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004018};
4019
4020static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004021 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4022 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004023{
4024 struct nfs4_lockdata *p;
4025 struct inode *inode = lsp->ls_state->inode;
4026 struct nfs_server *server = NFS_SERVER(inode);
4027
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004028 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004029 if (p == NULL)
4030 return NULL;
4031
4032 p->arg.fh = NFS_FH(inode);
4033 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004034 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004035 if (p->arg.open_seqid == NULL)
4036 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004037 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004038 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004039 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004040 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004041 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04004042 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004043 p->res.lock_seqid = p->arg.lock_seqid;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04004044 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004045 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004046 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004047 atomic_inc(&lsp->ls_count);
4048 p->ctx = get_nfs_open_context(ctx);
4049 memcpy(&p->fl, fl, sizeof(p->fl));
4050 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004051out_free_seqid:
4052 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004053out_free:
4054 kfree(p);
4055 return NULL;
4056}
4057
4058static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4059{
4060 struct nfs4_lockdata *data = calldata;
4061 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062
Harvey Harrison3110ff82008-05-02 13:42:44 -07004063 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004064 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4065 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004066 /* Do we need to do an open_to_lock_owner? */
4067 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004068 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4069 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004070 data->arg.open_stateid = &state->stateid;
4071 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004072 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004073 } else
4074 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004075 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004076 if (nfs4_setup_sequence(data->server,
4077 &data->arg.seq_args,
Andy Adamson66179ef2009-04-01 09:22:22 -04004078 &data->res.seq_res, 1, task))
4079 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004080 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004081 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004082}
4083
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004084static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4085{
4086 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4087 nfs4_lock_prepare(task, calldata);
4088}
4089
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004090static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4091{
4092 struct nfs4_lockdata *data = calldata;
4093
Harvey Harrison3110ff82008-05-02 13:42:44 -07004094 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004095
Trond Myklebustd61e6122009-12-05 19:32:19 -05004096 nfs4_sequence_done(data->server, &data->res.seq_res,
4097 task->tk_status);
Andy Adamson66179ef2009-04-01 09:22:22 -04004098
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004099 data->rpc_status = task->tk_status;
4100 if (RPC_ASSASSINATED(task))
4101 goto out;
4102 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004103 if (data->rpc_status == 0)
4104 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4105 else
4106 goto out;
4107 }
4108 if (data->rpc_status == 0) {
4109 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4110 sizeof(data->lsp->ls_stateid.data));
4111 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Trond Myklebust88be9f92007-06-05 10:42:27 -04004112 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004113 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004114out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004115 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004116}
4117
4118static void nfs4_lock_release(void *calldata)
4119{
4120 struct nfs4_lockdata *data = calldata;
4121
Harvey Harrison3110ff82008-05-02 13:42:44 -07004122 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004123 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004124 if (data->cancelled != 0) {
4125 struct rpc_task *task;
4126 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4127 data->arg.lock_seqid);
4128 if (!IS_ERR(task))
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004129 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004130 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004131 } else
4132 nfs_free_seqid(data->arg.lock_seqid);
4133 nfs4_put_lock_state(data->lsp);
4134 put_nfs_open_context(data->ctx);
4135 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004136 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004137}
4138
4139static const struct rpc_call_ops nfs4_lock_ops = {
4140 .rpc_call_prepare = nfs4_lock_prepare,
4141 .rpc_call_done = nfs4_lock_done,
4142 .rpc_release = nfs4_lock_release,
4143};
4144
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004145static const struct rpc_call_ops nfs4_recover_lock_ops = {
4146 .rpc_call_prepare = nfs4_recover_lock_prepare,
4147 .rpc_call_done = nfs4_lock_done,
4148 .rpc_release = nfs4_lock_release,
4149};
4150
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004151static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4152{
4153 struct nfs_client *clp = server->nfs_client;
4154 struct nfs4_state *state = lsp->ls_state;
4155
4156 switch (error) {
4157 case -NFS4ERR_ADMIN_REVOKED:
4158 case -NFS4ERR_BAD_STATEID:
4159 case -NFS4ERR_EXPIRED:
4160 if (new_lock_owner != 0 ||
4161 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4162 nfs4_state_mark_reclaim_nograce(clp, state);
4163 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004164 break;
4165 case -NFS4ERR_STALE_STATEID:
4166 if (new_lock_owner != 0 ||
4167 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4168 nfs4_state_mark_reclaim_reboot(clp, state);
4169 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004170 };
4171}
4172
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004173static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004174{
4175 struct nfs4_lockdata *data;
4176 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004177 struct rpc_message msg = {
4178 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4179 .rpc_cred = state->owner->so_cred,
4180 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004181 struct rpc_task_setup task_setup_data = {
4182 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004183 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004184 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004185 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004186 .flags = RPC_TASK_ASYNC,
4187 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004188 int ret;
4189
Harvey Harrison3110ff82008-05-02 13:42:44 -07004190 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004191 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004192 fl->fl_u.nfs4_fl.owner,
4193 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004194 if (data == NULL)
4195 return -ENOMEM;
4196 if (IS_SETLKW(cmd))
4197 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004198 if (recovery_type > NFS_LOCK_NEW) {
4199 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004200 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004201 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4202 }
Trond Myklebust5138fde2007-07-14 15:40:01 -04004203 msg.rpc_argp = &data->arg,
4204 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004205 task_setup_data.callback_data = data;
4206 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004207 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004208 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004209 ret = nfs4_wait_for_completion_rpc_task(task);
4210 if (ret == 0) {
4211 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004212 if (ret)
4213 nfs4_handle_setlk_error(data->server, data->lsp,
4214 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004215 } else
4216 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004217 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004218 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004219 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220}
4221
4222static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4223{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004224 struct nfs_server *server = NFS_SERVER(state->inode);
4225 struct nfs4_exception exception = { };
4226 int err;
4227
4228 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004229 /* Cache the lock if possible... */
4230 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4231 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004232 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Jeff Layton2c643482010-01-07 09:42:03 -05004233 if (err != -NFS4ERR_DELAY && err != -EKEYEXPIRED)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004234 break;
4235 nfs4_handle_exception(server, err, &exception);
4236 } while (exception.retry);
4237 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238}
4239
4240static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4241{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004242 struct nfs_server *server = NFS_SERVER(state->inode);
4243 struct nfs4_exception exception = { };
4244 int err;
4245
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004246 err = nfs4_set_lock_state(state, request);
4247 if (err != 0)
4248 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004249 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004250 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4251 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004252 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004253 switch (err) {
4254 default:
4255 goto out;
4256 case -NFS4ERR_GRACE:
4257 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -05004258 case -EKEYEXPIRED:
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004259 nfs4_handle_exception(server, err, &exception);
4260 err = 0;
4261 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004262 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004263out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004264 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265}
4266
4267static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4268{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004269 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004270 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004271 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004273 if ((fl_flags & FL_POSIX) &&
4274 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4275 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004276 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004277 status = nfs4_set_lock_state(state, request);
4278 if (status != 0)
4279 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004280 request->fl_flags |= FL_ACCESS;
4281 status = do_vfs_lock(request->fl_file, request);
4282 if (status < 0)
4283 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004284 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004285 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004286 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004287 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004288 request->fl_flags = fl_flags & ~FL_SLEEP;
4289 status = do_vfs_lock(request->fl_file, request);
4290 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004291 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004292 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004293 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004294 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004295 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004296 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004297 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07004298 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004299out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004300 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004301out:
4302 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 return status;
4304}
4305
4306static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4307{
4308 struct nfs4_exception exception = { };
4309 int err;
4310
4311 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004312 err = _nfs4_proc_setlk(state, cmd, request);
4313 if (err == -NFS4ERR_DENIED)
4314 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004316 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317 } while (exception.retry);
4318 return err;
4319}
4320
4321static int
4322nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4323{
4324 struct nfs_open_context *ctx;
4325 struct nfs4_state *state;
4326 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4327 int status;
4328
4329 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004330 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 state = ctx->state;
4332
4333 if (request->fl_start < 0 || request->fl_end < 0)
4334 return -EINVAL;
4335
Trond Myklebustd9531262009-07-21 19:22:38 -04004336 if (IS_GETLK(cmd)) {
4337 if (state != NULL)
4338 return nfs4_proc_getlk(state, F_GETLK, request);
4339 return 0;
4340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341
4342 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4343 return -EINVAL;
4344
Trond Myklebustd9531262009-07-21 19:22:38 -04004345 if (request->fl_type == F_UNLCK) {
4346 if (state != NULL)
4347 return nfs4_proc_unlck(state, cmd, request);
4348 return 0;
4349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350
Trond Myklebustd9531262009-07-21 19:22:38 -04004351 if (state == NULL)
4352 return -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353 do {
4354 status = nfs4_proc_setlk(state, cmd, request);
4355 if ((status != -EAGAIN) || IS_SETLK(cmd))
4356 break;
4357 timeout = nfs4_set_lock_task_retry(timeout);
4358 status = -ERESTARTSYS;
4359 if (signalled())
4360 break;
4361 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362 return status;
4363}
4364
Trond Myklebust888e6942005-11-04 15:38:11 -05004365int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4366{
4367 struct nfs_server *server = NFS_SERVER(state->inode);
4368 struct nfs4_exception exception = { };
4369 int err;
4370
4371 err = nfs4_set_lock_state(state, fl);
4372 if (err != 0)
4373 goto out;
4374 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004375 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004376 switch (err) {
4377 default:
4378 printk(KERN_ERR "%s: unhandled error %d.\n",
4379 __func__, err);
4380 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004381 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004382 goto out;
4383 case -NFS4ERR_EXPIRED:
4384 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004385 case -NFS4ERR_STALE_STATEID:
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004386 case -NFS4ERR_BADSESSION:
4387 case -NFS4ERR_BADSLOT:
4388 case -NFS4ERR_BAD_HIGH_SLOT:
4389 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4390 case -NFS4ERR_DEADSESSION:
Trond Myklebustd5122202009-06-17 13:22:58 -07004391 nfs4_schedule_state_recovery(server->nfs_client);
4392 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004393 case -ERESTARTSYS:
4394 /*
4395 * The show must go on: exit, but mark the
4396 * stateid as needing recovery.
4397 */
4398 case -NFS4ERR_ADMIN_REVOKED:
4399 case -NFS4ERR_BAD_STATEID:
4400 case -NFS4ERR_OPENMODE:
4401 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
4402 err = 0;
4403 goto out;
4404 case -ENOMEM:
4405 case -NFS4ERR_DENIED:
4406 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4407 err = 0;
4408 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004409 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -05004410 case -EKEYEXPIRED:
Trond Myklebustd5122202009-06-17 13:22:58 -07004411 break;
4412 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004413 err = nfs4_handle_exception(server, err, &exception);
4414 } while (exception.retry);
4415out:
4416 return err;
4417}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004418
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004419#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4420
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004421int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
4422 size_t buflen, int flags)
4423{
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004424 struct inode *inode = dentry->d_inode;
4425
4426 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4427 return -EOPNOTSUPP;
4428
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004429 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004430}
4431
4432/* The getxattr man page suggests returning -ENODATA for unknown attributes,
4433 * and that's what we'll do for e.g. user attributes that haven't been set.
4434 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
4435 * attributes in kernel-managed attribute namespaces. */
4436ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
4437 size_t buflen)
4438{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004439 struct inode *inode = dentry->d_inode;
4440
4441 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4442 return -EOPNOTSUPP;
4443
4444 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004445}
4446
4447ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
4448{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004449 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004450
J. Bruce Fields096455a2006-03-20 23:23:42 -05004451 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4452 return 0;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004453 if (buf && buflen < len)
4454 return -ERANGE;
4455 if (buf)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004456 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
4457 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004458}
4459
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004460static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4461{
4462 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
4463 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4464 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4465 return;
4466
4467 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4468 NFS_ATTR_FATTR_NLINK;
4469 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4470 fattr->nlink = 2;
4471}
4472
Trond Myklebust56659e92007-07-17 21:52:39 -04004473int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004474 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004475{
4476 struct nfs_server *server = NFS_SERVER(dir);
4477 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004478 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4479 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004480 };
4481 struct nfs4_fs_locations_arg args = {
4482 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004483 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004484 .page = page,
4485 .bitmask = bitmask,
4486 };
Benny Halevy22958462009-04-01 09:22:02 -04004487 struct nfs4_fs_locations_res res = {
4488 .fs_locations = fs_locations,
4489 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004490 struct rpc_message msg = {
4491 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4492 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004493 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004494 };
4495 int status;
4496
Harvey Harrison3110ff82008-05-02 13:42:44 -07004497 dprintk("%s: start\n", __func__);
Trond Myklebustc228fd32007-01-13 02:28:11 -05004498 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004499 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004500 fs_locations->nlocations = 0;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04004501 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004502 nfs_fixup_referral_attributes(&fs_locations->fattr);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004503 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004504 return status;
4505}
4506
Andy Adamson557134a2009-04-01 09:21:53 -04004507#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04004508/*
4509 * nfs4_proc_exchange_id()
4510 *
4511 * Since the clientid has expired, all compounds using sessions
4512 * associated with the stale clientid will be returning
4513 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4514 * be in some phase of session reset.
4515 */
Andy Adamson4d643d12009-12-04 15:52:24 -05004516int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004517{
4518 nfs4_verifier verifier;
4519 struct nfs41_exchange_id_args args = {
4520 .client = clp,
4521 .flags = clp->cl_exchange_flags,
4522 };
4523 struct nfs41_exchange_id_res res = {
4524 .client = clp,
4525 };
4526 int status;
4527 struct rpc_message msg = {
4528 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4529 .rpc_argp = &args,
4530 .rpc_resp = &res,
4531 .rpc_cred = cred,
4532 };
4533 __be32 *p;
4534
4535 dprintk("--> %s\n", __func__);
4536 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04004537
Alexandros Batsakis7b183d02009-12-05 13:33:25 -05004538 /* Remove server-only flags */
4539 args.flags &= ~EXCHGID4_FLAG_CONFIRMED_R;
4540
Benny Halevy99fe60d2009-04-01 09:22:29 -04004541 p = (u32 *)verifier.data;
4542 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4543 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4544 args.verifier = &verifier;
4545
4546 while (1) {
4547 args.id_len = scnprintf(args.id, sizeof(args.id),
4548 "%s/%s %u",
4549 clp->cl_ipaddr,
4550 rpc_peeraddr2str(clp->cl_rpcclient,
4551 RPC_DISPLAY_ADDR),
4552 clp->cl_id_uniquifier);
4553
4554 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
4555
Andy Adamson180b62a2010-03-02 13:19:36 -05004556 if (status != -NFS4ERR_CLID_INUSE)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004557 break;
4558
4559 if (signalled())
4560 break;
4561
4562 if (++clp->cl_id_uniquifier == 0)
4563 break;
4564 }
4565
4566 dprintk("<-- %s status= %d\n", __func__, status);
4567 return status;
4568}
4569
Andy Adamson2050f0c2009-04-01 09:22:30 -04004570struct nfs4_get_lease_time_data {
4571 struct nfs4_get_lease_time_args *args;
4572 struct nfs4_get_lease_time_res *res;
4573 struct nfs_client *clp;
4574};
4575
4576static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4577 void *calldata)
4578{
4579 int ret;
4580 struct nfs4_get_lease_time_data *data =
4581 (struct nfs4_get_lease_time_data *)calldata;
4582
4583 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004584 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004585 /* just setup sequence, do not trigger session recovery
4586 since we're invoked within one */
4587 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004588 &data->args->la_seq_args,
4589 &data->res->lr_seq_res, 0, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004590
4591 BUG_ON(ret == -EAGAIN);
4592 rpc_call_start(task);
4593 dprintk("<-- %s\n", __func__);
4594}
4595
4596/*
4597 * Called from nfs4_state_manager thread for session setup, so don't recover
4598 * from sequence operation or clientid errors.
4599 */
4600static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4601{
4602 struct nfs4_get_lease_time_data *data =
4603 (struct nfs4_get_lease_time_data *)calldata;
4604
4605 dprintk("--> %s\n", __func__);
Trond Myklebustd185a332010-06-16 09:52:25 -04004606 nfs41_sequence_done(&data->res->lr_seq_res);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004607 switch (task->tk_status) {
4608 case -NFS4ERR_DELAY:
4609 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05004610 case -EKEYEXPIRED:
Andy Adamson2050f0c2009-04-01 09:22:30 -04004611 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4612 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4613 task->tk_status = 0;
Trond Myklebust0110ee12009-12-07 09:00:24 -05004614 nfs_restart_rpc(task, data->clp);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004615 return;
4616 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04004617 dprintk("<-- %s\n", __func__);
4618}
4619
4620struct rpc_call_ops nfs4_get_lease_time_ops = {
4621 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4622 .rpc_call_done = nfs4_get_lease_time_done,
4623};
4624
4625int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4626{
4627 struct rpc_task *task;
4628 struct nfs4_get_lease_time_args args;
4629 struct nfs4_get_lease_time_res res = {
4630 .lr_fsinfo = fsinfo,
4631 };
4632 struct nfs4_get_lease_time_data data = {
4633 .args = &args,
4634 .res = &res,
4635 .clp = clp,
4636 };
4637 struct rpc_message msg = {
4638 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4639 .rpc_argp = &args,
4640 .rpc_resp = &res,
4641 };
4642 struct rpc_task_setup task_setup = {
4643 .rpc_client = clp->cl_rpcclient,
4644 .rpc_message = &msg,
4645 .callback_ops = &nfs4_get_lease_time_ops,
4646 .callback_data = &data
4647 };
4648 int status;
4649
4650 res.lr_seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
4651 dprintk("--> %s\n", __func__);
4652 task = rpc_run_task(&task_setup);
4653
4654 if (IS_ERR(task))
4655 status = PTR_ERR(task);
4656 else {
4657 status = task->tk_status;
4658 rpc_put_task(task);
4659 }
4660 dprintk("<-- %s return %d\n", __func__, status);
4661
4662 return status;
4663}
4664
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004665/*
4666 * Reset a slot table
4667 */
Andy Adamson104aeba2010-01-14 17:45:10 -05004668static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
4669 int ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004670{
Andy Adamson104aeba2010-01-14 17:45:10 -05004671 struct nfs4_slot *new = NULL;
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004672 int i;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004673 int ret = 0;
4674
Andy Adamson104aeba2010-01-14 17:45:10 -05004675 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
4676 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004677
Andy Adamson104aeba2010-01-14 17:45:10 -05004678 /* Does the newly negotiated max_reqs match the existing slot table? */
4679 if (max_reqs != tbl->max_slots) {
4680 ret = -ENOMEM;
4681 new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004682 GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05004683 if (!new)
4684 goto out;
4685 ret = 0;
4686 kfree(tbl->slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004687 }
4688 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson104aeba2010-01-14 17:45:10 -05004689 if (new) {
4690 tbl->slots = new;
4691 tbl->max_slots = max_reqs;
4692 }
4693 for (i = 0; i < tbl->max_slots; ++i)
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004694 tbl->slots[i].seq_nr = ivalue;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004695 spin_unlock(&tbl->slot_tbl_lock);
4696 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4697 tbl, tbl->slots, tbl->max_slots);
4698out:
4699 dprintk("<-- %s: return %d\n", __func__, ret);
4700 return ret;
4701}
4702
Andy Adamsonfc931582009-04-01 09:22:31 -04004703/*
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004704 * Reset the forechannel and backchannel slot tables
4705 */
4706static int nfs4_reset_slot_tables(struct nfs4_session *session)
4707{
4708 int status;
4709
4710 status = nfs4_reset_slot_table(&session->fc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05004711 session->fc_attrs.max_reqs, 1);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004712 if (status)
4713 return status;
4714
4715 status = nfs4_reset_slot_table(&session->bc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05004716 session->bc_attrs.max_reqs, 0);
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004717 return status;
4718}
4719
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004720/* Destroy the slot table */
4721static void nfs4_destroy_slot_tables(struct nfs4_session *session)
4722{
4723 if (session->fc_slot_table.slots != NULL) {
4724 kfree(session->fc_slot_table.slots);
4725 session->fc_slot_table.slots = NULL;
4726 }
4727 if (session->bc_slot_table.slots != NULL) {
4728 kfree(session->bc_slot_table.slots);
4729 session->bc_slot_table.slots = NULL;
4730 }
4731 return;
4732}
4733
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004734/*
Andy Adamsonfc931582009-04-01 09:22:31 -04004735 * Initialize slot table
4736 */
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004737static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
4738 int max_slots, int ivalue)
Andy Adamsonfc931582009-04-01 09:22:31 -04004739{
Andy Adamsonfc931582009-04-01 09:22:31 -04004740 struct nfs4_slot *slot;
4741 int ret = -ENOMEM;
4742
4743 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
4744
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004745 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
Andy Adamsonfc931582009-04-01 09:22:31 -04004746
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004747 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_NOFS);
Andy Adamsonfc931582009-04-01 09:22:31 -04004748 if (!slot)
4749 goto out;
Andy Adamsonfc931582009-04-01 09:22:31 -04004750 ret = 0;
4751
4752 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonfc931582009-04-01 09:22:31 -04004753 tbl->max_slots = max_slots;
4754 tbl->slots = slot;
4755 tbl->highest_used_slotid = -1; /* no slot is currently used */
4756 spin_unlock(&tbl->slot_tbl_lock);
4757 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4758 tbl, tbl->slots, tbl->max_slots);
4759out:
4760 dprintk("<-- %s: return %d\n", __func__, ret);
4761 return ret;
Andy Adamsonfc931582009-04-01 09:22:31 -04004762}
4763
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004764/*
4765 * Initialize the forechannel and backchannel tables
4766 */
4767static int nfs4_init_slot_tables(struct nfs4_session *session)
4768{
Trond Myklebustf26468f2009-12-05 19:32:11 -05004769 struct nfs4_slot_table *tbl;
4770 int status = 0;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004771
Trond Myklebustf26468f2009-12-05 19:32:11 -05004772 tbl = &session->fc_slot_table;
4773 if (tbl->slots == NULL) {
4774 status = nfs4_init_slot_table(tbl,
4775 session->fc_attrs.max_reqs, 1);
4776 if (status)
4777 return status;
4778 }
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004779
Trond Myklebustf26468f2009-12-05 19:32:11 -05004780 tbl = &session->bc_slot_table;
4781 if (tbl->slots == NULL) {
4782 status = nfs4_init_slot_table(tbl,
4783 session->bc_attrs.max_reqs, 0);
4784 if (status)
4785 nfs4_destroy_slot_tables(session);
4786 }
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004787
4788 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04004789}
4790
4791struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
4792{
4793 struct nfs4_session *session;
4794 struct nfs4_slot_table *tbl;
4795
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004796 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04004797 if (!session)
4798 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04004799
Andy Adamson76db6d92009-04-01 09:22:38 -04004800 /*
4801 * The create session reply races with the server back
4802 * channel probe. Mark the client NFS_CS_SESSION_INITING
4803 * so that the client back channel can find the
4804 * nfs_client struct
4805 */
4806 clp->cl_cons_state = NFS_CS_SESSION_INITING;
Andy Adamsonea028ac2009-12-04 15:55:38 -05004807 init_completion(&session->complete);
Andy Adamson76db6d92009-04-01 09:22:38 -04004808
Andy Adamson557134a2009-04-01 09:21:53 -04004809 tbl = &session->fc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05004810 tbl->highest_used_slotid = -1;
Andy Adamson557134a2009-04-01 09:21:53 -04004811 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004812 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004813
4814 tbl = &session->bc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05004815 tbl->highest_used_slotid = -1;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004816 spin_lock_init(&tbl->slot_tbl_lock);
4817 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
4818
Andy Adamson557134a2009-04-01 09:21:53 -04004819 session->clp = clp;
4820 return session;
4821}
4822
4823void nfs4_destroy_session(struct nfs4_session *session)
4824{
Andy Adamson5a0ffe52009-04-01 09:23:18 -04004825 nfs4_proc_destroy_session(session);
4826 dprintk("%s Destroy backchannel for xprt %p\n",
4827 __func__, session->clp->cl_rpcclient->cl_xprt);
4828 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
4829 NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004830 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04004831 kfree(session);
4832}
4833
Andy Adamsonfc931582009-04-01 09:22:31 -04004834/*
4835 * Initialize the values to be used by the client in CREATE_SESSION
4836 * If nfs4_init_session set the fore channel request and response sizes,
4837 * use them.
4838 *
4839 * Set the back channel max_resp_sz_cached to zero to force the client to
4840 * always set csa_cachethis to FALSE because the current implementation
4841 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
4842 */
4843static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
4844{
4845 struct nfs4_session *session = args->client->cl_session;
4846 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
4847 mxresp_sz = session->fc_attrs.max_resp_sz;
4848
4849 if (mxrqst_sz == 0)
4850 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
4851 if (mxresp_sz == 0)
4852 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
4853 /* Fore channel attributes */
4854 args->fc_attrs.headerpadsz = 0;
4855 args->fc_attrs.max_rqst_sz = mxrqst_sz;
4856 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04004857 args->fc_attrs.max_ops = NFS4_MAX_OPS;
4858 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
4859
4860 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05004861 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04004862 __func__,
4863 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05004864 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04004865
4866 /* Back channel attributes */
4867 args->bc_attrs.headerpadsz = 0;
4868 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
4869 args->bc_attrs.max_resp_sz = PAGE_SIZE;
4870 args->bc_attrs.max_resp_sz_cached = 0;
4871 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
4872 args->bc_attrs.max_reqs = 1;
4873
4874 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
4875 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4876 __func__,
4877 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
4878 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
4879 args->bc_attrs.max_reqs);
4880}
4881
Andy Adamson8d353012009-04-01 09:22:32 -04004882static int _verify_channel_attr(char *chan, char *attr_name, u32 sent, u32 rcvd)
4883{
4884 if (rcvd <= sent)
4885 return 0;
4886 printk(KERN_WARNING "%s: Session INVALID: %s channel %s increased. "
4887 "sent=%u rcvd=%u\n", __func__, chan, attr_name, sent, rcvd);
4888 return -EINVAL;
4889}
4890
4891#define _verify_fore_channel_attr(_name_) \
4892 _verify_channel_attr("fore", #_name_, \
4893 args->fc_attrs._name_, \
4894 session->fc_attrs._name_)
4895
4896#define _verify_back_channel_attr(_name_) \
4897 _verify_channel_attr("back", #_name_, \
4898 args->bc_attrs._name_, \
4899 session->bc_attrs._name_)
4900
4901/*
4902 * The server is not allowed to increase the fore channel header pad size,
4903 * maximum response size, or maximum number of operations.
4904 *
4905 * The back channel attributes are only negotiatied down: We send what the
4906 * (back channel) server insists upon.
4907 */
4908static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
4909 struct nfs4_session *session)
4910{
4911 int ret = 0;
4912
4913 ret |= _verify_fore_channel_attr(headerpadsz);
4914 ret |= _verify_fore_channel_attr(max_resp_sz);
4915 ret |= _verify_fore_channel_attr(max_ops);
4916
4917 ret |= _verify_back_channel_attr(headerpadsz);
4918 ret |= _verify_back_channel_attr(max_rqst_sz);
4919 ret |= _verify_back_channel_attr(max_resp_sz);
4920 ret |= _verify_back_channel_attr(max_resp_sz_cached);
4921 ret |= _verify_back_channel_attr(max_ops);
4922 ret |= _verify_back_channel_attr(max_reqs);
4923
4924 return ret;
4925}
4926
Andy Adamsonfc931582009-04-01 09:22:31 -04004927static int _nfs4_proc_create_session(struct nfs_client *clp)
4928{
4929 struct nfs4_session *session = clp->cl_session;
4930 struct nfs41_create_session_args args = {
4931 .client = clp,
4932 .cb_program = NFS4_CALLBACK,
4933 };
4934 struct nfs41_create_session_res res = {
4935 .client = clp,
4936 };
4937 struct rpc_message msg = {
4938 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
4939 .rpc_argp = &args,
4940 .rpc_resp = &res,
4941 };
4942 int status;
4943
4944 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04004945 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04004946
4947 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4948
Andy Adamson8d353012009-04-01 09:22:32 -04004949 if (!status)
4950 /* Verify the session's negotiated channel_attrs values */
4951 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04004952 if (!status) {
4953 /* Increment the clientid slot sequence id */
4954 clp->cl_seqid++;
4955 }
4956
4957 return status;
4958}
4959
4960/*
4961 * Issues a CREATE_SESSION operation to the server.
4962 * It is the responsibility of the caller to verify the session is
4963 * expired before calling this routine.
4964 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05004965int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04004966{
4967 int status;
4968 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04004969 struct nfs4_session *session = clp->cl_session;
4970
4971 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
4972
4973 status = _nfs4_proc_create_session(clp);
4974 if (status)
4975 goto out;
4976
Trond Myklebustf26468f2009-12-05 19:32:11 -05004977 /* Init and reset the fore channel */
4978 status = nfs4_init_slot_tables(session);
4979 dprintk("slot table initialization returned %d\n", status);
4980 if (status)
4981 goto out;
4982 status = nfs4_reset_slot_tables(session);
4983 dprintk("slot table reset returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04004984 if (status)
4985 goto out;
4986
4987 ptr = (unsigned *)&session->sess_id.data[0];
4988 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
4989 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04004990out:
4991 dprintk("<-- %s\n", __func__);
4992 return status;
4993}
4994
Andy Adamson0f3e66c2009-04-01 09:22:34 -04004995/*
4996 * Issue the over-the-wire RPC DESTROY_SESSION.
4997 * The caller must serialize access to this routine.
4998 */
4999int nfs4_proc_destroy_session(struct nfs4_session *session)
5000{
5001 int status = 0;
5002 struct rpc_message msg;
5003
5004 dprintk("--> nfs4_proc_destroy_session\n");
5005
5006 /* session is still being setup */
5007 if (session->clp->cl_cons_state != NFS_CS_READY)
5008 return status;
5009
5010 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5011 msg.rpc_argp = session;
5012 msg.rpc_resp = NULL;
5013 msg.rpc_cred = NULL;
5014 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
5015
5016 if (status)
5017 printk(KERN_WARNING
5018 "Got error %d from the server on DESTROY_SESSION. "
5019 "Session has been destroyed regardless...\n", status);
5020
5021 dprintk("<-- nfs4_proc_destroy_session\n");
5022 return status;
5023}
5024
Trond Myklebustfccba802009-07-21 16:48:07 -04005025int nfs4_init_session(struct nfs_server *server)
5026{
5027 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005028 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005029 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005030 int ret;
5031
5032 if (!nfs4_has_session(clp))
5033 return 0;
5034
Andy Adamson68bf05e2009-12-15 12:55:02 -05005035 rsize = server->rsize;
5036 if (rsize == 0)
5037 rsize = NFS_MAX_FILE_IO_SIZE;
5038 wsize = server->wsize;
5039 if (wsize == 0)
5040 wsize = NFS_MAX_FILE_IO_SIZE;
5041
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005042 session = clp->cl_session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005043 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5044 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005045
Trond Myklebustfccba802009-07-21 16:48:07 -04005046 ret = nfs4_recover_expired_lease(server);
5047 if (!ret)
5048 ret = nfs4_check_client_ready(clp);
5049 return ret;
5050}
5051
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005052/*
5053 * Renew the cl_session lease.
5054 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005055struct nfs4_sequence_data {
5056 struct nfs_client *clp;
5057 struct nfs4_sequence_args args;
5058 struct nfs4_sequence_res res;
5059};
5060
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005061static void nfs41_sequence_release(void *data)
5062{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005063 struct nfs4_sequence_data *calldata = data;
5064 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005065
Alexandros Batsakis71358402010-02-05 03:45:05 -08005066 if (atomic_read(&clp->cl_count) > 1)
5067 nfs4_schedule_state_renewal(clp);
5068 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005069 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005070}
5071
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005072static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5073{
5074 switch(task->tk_status) {
5075 case -NFS4ERR_DELAY:
5076 case -EKEYEXPIRED:
5077 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5078 return -EAGAIN;
5079 default:
5080 nfs4_schedule_state_recovery(clp);
5081 }
5082 return 0;
5083}
5084
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005085static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005086{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005087 struct nfs4_sequence_data *calldata = data;
5088 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005089
Trond Myklebustd185a332010-06-16 09:52:25 -04005090 nfs41_sequence_done(task->tk_msg.rpc_resp);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005091
5092 if (task->tk_status < 0) {
5093 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005094 if (atomic_read(&clp->cl_count) == 1)
5095 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005096
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005097 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5098 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005099 return;
5100 }
5101 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005102 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005103out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005104 dprintk("<-- %s\n", __func__);
5105}
5106
5107static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5108{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005109 struct nfs4_sequence_data *calldata = data;
5110 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005111 struct nfs4_sequence_args *args;
5112 struct nfs4_sequence_res *res;
5113
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005114 args = task->tk_msg.rpc_argp;
5115 res = task->tk_msg.rpc_resp;
5116
Trond Myklebust035168a2010-06-16 09:52:26 -04005117 if (nfs41_setup_sequence(clp->cl_session, args, res, 0, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005118 return;
5119 rpc_call_start(task);
5120}
5121
5122static const struct rpc_call_ops nfs41_sequence_ops = {
5123 .rpc_call_done = nfs41_sequence_call_done,
5124 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005125 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005126};
5127
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005128static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005129{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005130 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005131 struct rpc_message msg = {
5132 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5133 .rpc_cred = cred,
5134 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005135 struct rpc_task_setup task_setup_data = {
5136 .rpc_client = clp->cl_rpcclient,
5137 .rpc_message = &msg,
5138 .callback_ops = &nfs41_sequence_ops,
5139 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5140 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005141
Alexandros Batsakis71358402010-02-05 03:45:05 -08005142 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005143 return ERR_PTR(-EIO);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005144 calldata = kmalloc(sizeof(*calldata), GFP_NOFS);
5145 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005146 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005147 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005148 }
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005149 calldata->res.sr_slotid = NFS4_MAX_SLOT_TABLE;
5150 msg.rpc_argp = &calldata->args;
5151 msg.rpc_resp = &calldata->res;
5152 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005153 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005154
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005155 return rpc_run_task(&task_setup_data);
5156}
5157
5158static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5159{
5160 struct rpc_task *task;
5161 int ret = 0;
5162
5163 task = _nfs41_proc_sequence(clp, cred);
5164 if (IS_ERR(task))
5165 ret = PTR_ERR(task);
5166 else
5167 rpc_put_task(task);
5168 dprintk("<-- %s status=%d\n", __func__, ret);
5169 return ret;
5170}
5171
5172static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5173{
5174 struct rpc_task *task;
5175 int ret;
5176
5177 task = _nfs41_proc_sequence(clp, cred);
5178 if (IS_ERR(task)) {
5179 ret = PTR_ERR(task);
5180 goto out;
5181 }
5182 ret = rpc_wait_for_completion_task(task);
5183 if (!ret)
5184 ret = task->tk_status;
5185 rpc_put_task(task);
5186out:
5187 dprintk("<-- %s status=%d\n", __func__, ret);
5188 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005189}
5190
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005191struct nfs4_reclaim_complete_data {
5192 struct nfs_client *clp;
5193 struct nfs41_reclaim_complete_args arg;
5194 struct nfs41_reclaim_complete_res res;
5195};
5196
5197static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5198{
5199 struct nfs4_reclaim_complete_data *calldata = data;
5200
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005201 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04005202 if (nfs41_setup_sequence(calldata->clp->cl_session,
5203 &calldata->arg.seq_args,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005204 &calldata->res.seq_res, 0, task))
5205 return;
5206
5207 rpc_call_start(task);
5208}
5209
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005210static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5211{
5212 switch(task->tk_status) {
5213 case 0:
5214 case -NFS4ERR_COMPLETE_ALREADY:
5215 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5216 break;
5217 case -NFS4ERR_DELAY:
5218 case -EKEYEXPIRED:
5219 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5220 return -EAGAIN;
5221 default:
5222 nfs4_schedule_state_recovery(clp);
5223 }
5224 return 0;
5225}
5226
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005227static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5228{
5229 struct nfs4_reclaim_complete_data *calldata = data;
5230 struct nfs_client *clp = calldata->clp;
5231 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5232
5233 dprintk("--> %s\n", __func__);
Trond Myklebustd185a332010-06-16 09:52:25 -04005234 nfs41_sequence_done(res);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005235
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005236 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5237 rpc_restart_call_prepare(task);
5238 return;
5239 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005240 dprintk("<-- %s\n", __func__);
5241}
5242
5243static void nfs4_free_reclaim_complete_data(void *data)
5244{
5245 struct nfs4_reclaim_complete_data *calldata = data;
5246
5247 kfree(calldata);
5248}
5249
5250static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5251 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5252 .rpc_call_done = nfs4_reclaim_complete_done,
5253 .rpc_release = nfs4_free_reclaim_complete_data,
5254};
5255
5256/*
5257 * Issue a global reclaim complete.
5258 */
5259static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5260{
5261 struct nfs4_reclaim_complete_data *calldata;
5262 struct rpc_task *task;
5263 struct rpc_message msg = {
5264 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5265 };
5266 struct rpc_task_setup task_setup_data = {
5267 .rpc_client = clp->cl_rpcclient,
5268 .rpc_message = &msg,
5269 .callback_ops = &nfs4_reclaim_complete_call_ops,
5270 .flags = RPC_TASK_ASYNC,
5271 };
5272 int status = -ENOMEM;
5273
5274 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005275 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005276 if (calldata == NULL)
5277 goto out;
5278 calldata->clp = clp;
5279 calldata->arg.one_fs = 0;
5280 calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
5281
5282 msg.rpc_argp = &calldata->arg;
5283 msg.rpc_resp = &calldata->res;
5284 task_setup_data.callback_data = calldata;
5285 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005286 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005287 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005288 goto out;
5289 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005290 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005291 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005292out:
5293 dprintk("<-- %s status=%d\n", __func__, status);
5294 return status;
5295}
Andy Adamson557134a2009-04-01 09:21:53 -04005296#endif /* CONFIG_NFS_V4_1 */
5297
Andy Adamson591d71c2009-04-01 09:22:47 -04005298struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005299 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005300 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005301 .recover_open = nfs4_open_reclaim,
5302 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04005303 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005304 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005305};
5306
Andy Adamson591d71c2009-04-01 09:22:47 -04005307#if defined(CONFIG_NFS_V4_1)
5308struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
5309 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5310 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5311 .recover_open = nfs4_open_reclaim,
5312 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05005313 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005314 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005315 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04005316};
5317#endif /* CONFIG_NFS_V4_1 */
5318
5319struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005320 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005321 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322 .recover_open = nfs4_open_expired,
5323 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04005324 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005325 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326};
5327
Andy Adamson591d71c2009-04-01 09:22:47 -04005328#if defined(CONFIG_NFS_V4_1)
5329struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
5330 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5331 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5332 .recover_open = nfs4_open_expired,
5333 .recover_lock = nfs4_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05005334 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005335 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04005336};
5337#endif /* CONFIG_NFS_V4_1 */
5338
Benny Halevy29fba382009-04-01 09:22:44 -04005339struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
5340 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04005341 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005342 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04005343};
5344
5345#if defined(CONFIG_NFS_V4_1)
5346struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
5347 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04005348 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005349 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04005350};
5351#endif
5352
5353/*
5354 * Per minor version reboot and network partition recovery ops
5355 */
5356
Andy Adamson591d71c2009-04-01 09:22:47 -04005357struct nfs4_state_recovery_ops *nfs4_reboot_recovery_ops[] = {
5358 &nfs40_reboot_recovery_ops,
5359#if defined(CONFIG_NFS_V4_1)
5360 &nfs41_reboot_recovery_ops,
5361#endif
5362};
5363
5364struct nfs4_state_recovery_ops *nfs4_nograce_recovery_ops[] = {
5365 &nfs40_nograce_recovery_ops,
5366#if defined(CONFIG_NFS_V4_1)
5367 &nfs41_nograce_recovery_ops,
5368#endif
5369};
5370
Benny Halevy29fba382009-04-01 09:22:44 -04005371struct nfs4_state_maintenance_ops *nfs4_state_renewal_ops[] = {
5372 &nfs40_state_renewal_ops,
5373#if defined(CONFIG_NFS_V4_1)
5374 &nfs41_state_renewal_ops,
5375#endif
5376};
5377
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08005378static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005379 .permission = nfs_permission,
5380 .getattr = nfs_getattr,
5381 .setattr = nfs_setattr,
5382 .getxattr = nfs4_getxattr,
5383 .setxattr = nfs4_setxattr,
5384 .listxattr = nfs4_listxattr,
5385};
5386
David Howells509de812006-08-22 20:06:11 -04005387const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388 .version = 4, /* protocol version */
5389 .dentry_ops = &nfs4_dentry_operations,
5390 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005391 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392 .getroot = nfs4_proc_get_root,
5393 .getattr = nfs4_proc_getattr,
5394 .setattr = nfs4_proc_setattr,
David Howells2b3de442006-08-22 20:06:09 -04005395 .lookupfh = nfs4_proc_lookupfh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396 .lookup = nfs4_proc_lookup,
5397 .access = nfs4_proc_access,
5398 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005399 .create = nfs4_proc_create,
5400 .remove = nfs4_proc_remove,
5401 .unlink_setup = nfs4_proc_unlink_setup,
5402 .unlink_done = nfs4_proc_unlink_done,
5403 .rename = nfs4_proc_rename,
5404 .link = nfs4_proc_link,
5405 .symlink = nfs4_proc_symlink,
5406 .mkdir = nfs4_proc_mkdir,
5407 .rmdir = nfs4_proc_remove,
5408 .readdir = nfs4_proc_readdir,
5409 .mknod = nfs4_proc_mknod,
5410 .statfs = nfs4_proc_statfs,
5411 .fsinfo = nfs4_proc_fsinfo,
5412 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04005413 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414 .decode_dirent = nfs4_decode_dirent,
5415 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05005416 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005418 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005420 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005422 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04005423 .close_context = nfs4_close_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424};
5425
5426/*
5427 * Local variables:
5428 * c-basic-offset: 8
5429 * End:
5430 */