blob: 5d3e8a2db99fe1cdec5101348bcc0bf133325593 [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
Trond Myklebusta2118c32010-06-16 09:52:26 -0400359 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_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
Trond Myklebustdf896452010-06-16 09:52:26 -0400433static void nfs4_sequence_done(const struct nfs_server *server,
434 struct nfs4_sequence_res *res, int rpc_status)
435{
436 if (res->sr_session != NULL)
437 nfs41_sequence_done(res);
438}
439
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400440/*
Benny Halevy510b8172009-04-01 09:22:14 -0400441 * nfs4_find_slot - efficiently look for a free slot
442 *
443 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
444 * If found, we mark the slot as used, update the highest_used_slotid,
445 * and respectively set up the sequence operation args.
446 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400447 *
448 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400449 */
450static u8
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800451nfs4_find_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400452{
453 int slotid;
454 u8 ret_id = NFS4_MAX_SLOT_TABLE;
455 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
456
Benny Halevy510b8172009-04-01 09:22:14 -0400457 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
458 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
459 tbl->max_slots);
460 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
461 if (slotid >= tbl->max_slots)
462 goto out;
463 __set_bit(slotid, tbl->used_slots);
464 if (slotid > tbl->highest_used_slotid)
465 tbl->highest_used_slotid = slotid;
466 ret_id = slotid;
467out:
468 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
469 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400470 return ret_id;
471}
472
Andy Adamsonce5039c2009-04-01 09:22:13 -0400473static int nfs41_setup_sequence(struct nfs4_session *session,
474 struct nfs4_sequence_args *args,
475 struct nfs4_sequence_res *res,
476 int cache_reply,
477 struct rpc_task *task)
478{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400479 struct nfs4_slot *slot;
480 struct nfs4_slot_table *tbl;
481 u8 slotid;
482
483 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400484 /* slot already allocated? */
485 if (res->sr_slotid != NFS4_MAX_SLOT_TABLE)
486 return 0;
487
Andy Adamsonce5039c2009-04-01 09:22:13 -0400488 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400489 tbl = &session->fc_slot_table;
490
491 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400492 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800493 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamsonea028ac2009-12-04 15:55:38 -0500494 /*
495 * The state manager will wait until the slot table is empty.
496 * Schedule the reset thread
497 */
Andy Adamson05f0d232009-12-04 15:55:37 -0500498 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400499 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500500 dprintk("%s Schedule Session Reset\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500501 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400502 }
503
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800504 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
505 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
506 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
507 spin_unlock(&tbl->slot_tbl_lock);
508 dprintk("%s enforce FIFO order\n", __func__);
509 return -EAGAIN;
510 }
511
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800512 slotid = nfs4_find_slot(tbl);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400513 if (slotid == NFS4_MAX_SLOT_TABLE) {
514 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
515 spin_unlock(&tbl->slot_tbl_lock);
516 dprintk("<-- %s: no free slots\n", __func__);
517 return -EAGAIN;
518 }
519 spin_unlock(&tbl->slot_tbl_lock);
520
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800521 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400522 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400523 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400524 args->sa_slotid = slotid;
525 args->sa_cache_this = cache_reply;
526
527 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
528
Benny Halevy99fe60d2009-04-01 09:22:29 -0400529 res->sr_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400530 res->sr_slotid = slotid;
531 res->sr_renewal_time = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400532 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400533 /*
534 * sr_status is only set in decode_sequence, and so will remain
535 * set to 1 if an rpc level failure occurs.
536 */
537 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400538 return 0;
539}
540
Trond Myklebust035168a2010-06-16 09:52:26 -0400541int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400542 struct nfs4_sequence_args *args,
543 struct nfs4_sequence_res *res,
544 int cache_reply,
545 struct rpc_task *task)
546{
Trond Myklebust035168a2010-06-16 09:52:26 -0400547 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400548 int ret = 0;
549
Trond Myklebust035168a2010-06-16 09:52:26 -0400550 if (session == NULL) {
551 args->sa_session = NULL;
552 res->sr_session = NULL;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400553 goto out;
Trond Myklebust035168a2010-06-16 09:52:26 -0400554 }
555
556 dprintk("--> %s clp %p session %p sr_slotid %d\n",
557 __func__, session->clp, session, res->sr_slotid);
558
559 ret = nfs41_setup_sequence(session, args, res, cache_reply,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400560 task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400561out:
562 dprintk("<-- %s status=%d\n", __func__, ret);
563 return ret;
564}
565
566struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400567 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400568 struct nfs4_sequence_args *seq_args;
569 struct nfs4_sequence_res *seq_res;
570 int cache_reply;
571};
572
573static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
574{
575 struct nfs41_call_sync_data *data = calldata;
576
Trond Myklebust035168a2010-06-16 09:52:26 -0400577 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
578
579 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400580 data->seq_res, data->cache_reply, task))
581 return;
582 rpc_call_start(task);
583}
584
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800585static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
586{
587 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
588 nfs41_call_sync_prepare(task, calldata);
589}
590
Andy Adamson69ab40c2009-04-01 09:22:19 -0400591static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
592{
593 struct nfs41_call_sync_data *data = calldata;
594
Trond Myklebustd185a332010-06-16 09:52:25 -0400595 nfs41_sequence_done(data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400596}
597
Andy Adamsonce5039c2009-04-01 09:22:13 -0400598struct rpc_call_ops nfs41_call_sync_ops = {
599 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400600 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400601};
602
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800603struct rpc_call_ops nfs41_call_priv_sync_ops = {
604 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
605 .rpc_call_done = nfs41_call_sync_done,
606};
607
Trond Myklebust035168a2010-06-16 09:52:26 -0400608static int nfs4_call_sync_sequence(struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400609 struct rpc_message *msg,
610 struct nfs4_sequence_args *args,
611 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800612 int cache_reply,
613 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400614{
615 int ret;
616 struct rpc_task *task;
617 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400618 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400619 .seq_args = args,
620 .seq_res = res,
621 .cache_reply = cache_reply,
622 };
623 struct rpc_task_setup task_setup = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400624 .rpc_client = server->client,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400625 .rpc_message = msg,
626 .callback_ops = &nfs41_call_sync_ops,
627 .callback_data = &data
628 };
629
630 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800631 if (privileged)
632 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400633 task = rpc_run_task(&task_setup);
634 if (IS_ERR(task))
635 ret = PTR_ERR(task);
636 else {
637 ret = task->tk_status;
638 rpc_put_task(task);
639 }
640 return ret;
641}
642
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400643int _nfs4_call_sync_session(struct nfs_server *server,
644 struct rpc_message *msg,
645 struct nfs4_sequence_args *args,
646 struct nfs4_sequence_res *res,
647 int cache_reply)
648{
Trond Myklebust035168a2010-06-16 09:52:26 -0400649 return nfs4_call_sync_sequence(server, msg, args, res, cache_reply, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400650}
651
Trond Myklebustdf896452010-06-16 09:52:26 -0400652#else
653static void nfs4_sequence_done(const struct nfs_server *server,
654 struct nfs4_sequence_res *res, int rpc_status)
655{
656}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400657#endif /* CONFIG_NFS_V4_1 */
658
659int _nfs4_call_sync(struct nfs_server *server,
660 struct rpc_message *msg,
661 struct nfs4_sequence_args *args,
662 struct nfs4_sequence_res *res,
663 int cache_reply)
664{
Benny Halevyf3752972009-04-01 09:22:04 -0400665 args->sa_session = res->sr_session = NULL;
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400666 return rpc_call_sync(server->client, msg, 0);
667}
668
669#define nfs4_call_sync(server, msg, args, res, cache_reply) \
Trond Myklebust97dc1352010-06-16 09:52:26 -0400670 (server)->nfs_client->cl_mvops->call_sync((server), (msg), &(args)->seq_args, \
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400671 &(res)->seq_res, (cache_reply))
672
Trond Myklebust38478b22006-05-25 01:40:57 -0400673static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
Trond Myklebust38478b22006-05-25 01:40:57 -0400675 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Trond Myklebust38478b22006-05-25 01:40:57 -0400677 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400678 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
679 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400680 nfs_force_lookup_revalidate(dir);
Trond Myklebust40d24702007-10-08 09:24:22 -0400681 nfsi->change_attr = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400682 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683}
684
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100685struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400686 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100687 struct nfs_openargs o_arg;
688 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100689 struct nfs_open_confirmargs c_arg;
690 struct nfs_open_confirmres c_res;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100691 struct nfs_fattr f_attr;
692 struct nfs_fattr dir_attr;
Trond Myklebustad389da2007-06-05 12:30:00 -0400693 struct path path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100694 struct dentry *dir;
695 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400696 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100697 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100698 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400699 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100700 int rpc_status;
701 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100702};
703
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400704
705static void nfs4_init_opendata_res(struct nfs4_opendata *p)
706{
707 p->o_res.f_attr = &p->f_attr;
708 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400709 p->o_res.seqid = p->o_arg.seqid;
710 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400711 p->o_res.server = p->o_arg.server;
712 nfs_fattr_init(&p->f_attr);
713 nfs_fattr_init(&p->dir_attr);
Benny Halevy578e4582009-06-18 22:01:23 -0400714 p->o_res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400715}
716
Trond Myklebustad389da2007-06-05 12:30:00 -0400717static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500718 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400719 const struct iattr *attrs,
720 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100721{
Trond Myklebustad389da2007-06-05 12:30:00 -0400722 struct dentry *parent = dget_parent(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100723 struct inode *dir = parent->d_inode;
724 struct nfs_server *server = NFS_SERVER(dir);
725 struct nfs4_opendata *p;
726
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400727 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100728 if (p == NULL)
729 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400730 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100731 if (p->o_arg.seqid == NULL)
732 goto err_free;
Al Virof6948692010-01-30 13:51:04 -0500733 path_get(path);
734 p->path = *path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100735 p->dir = parent;
736 p->owner = sp;
737 atomic_inc(&sp->so_count);
738 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500739 p->o_arg.open_flags = flags;
740 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400741 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400742 p->o_arg.id = sp->so_owner_id.id;
Trond Myklebustad389da2007-06-05 12:30:00 -0400743 p->o_arg.name = &p->path.dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100744 p->o_arg.server = server;
745 p->o_arg.bitmask = server->attr_bitmask;
746 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400747 if (flags & O_CREAT) {
748 u32 *s;
749
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100750 p->o_arg.u.attrs = &p->attrs;
751 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400752 s = (u32 *) p->o_arg.u.verifier.data;
753 s[0] = jiffies;
754 s[1] = current->pid;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100755 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100756 p->c_arg.fh = &p->o_res.fh;
757 p->c_arg.stateid = &p->o_res.stateid;
758 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400759 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400760 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100761 return p;
762err_free:
763 kfree(p);
764err:
765 dput(parent);
766 return NULL;
767}
768
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400769static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100770{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400771 struct nfs4_opendata *p = container_of(kref,
772 struct nfs4_opendata, kref);
773
774 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400775 if (p->state != NULL)
776 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400777 nfs4_put_state_owner(p->owner);
778 dput(p->dir);
Jan Blunck31f31db12008-05-02 13:42:45 -0700779 path_put(&p->path);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400780 kfree(p);
781}
782
783static void nfs4_opendata_put(struct nfs4_opendata *p)
784{
785 if (p != NULL)
786 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100787}
788
Trond Myklebust06f814a2006-01-03 09:55:07 +0100789static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
790{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100791 int ret;
792
Trond Myklebust06f814a2006-01-03 09:55:07 +0100793 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100794 return ret;
795}
796
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500797static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400798{
799 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500800
801 if (open_mode & O_EXCL)
802 goto out;
803 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400804 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500805 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
806 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400807 break;
808 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500809 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
810 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400811 break;
812 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500813 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
814 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400815 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500816out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400817 return ret;
818}
819
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500820static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400821{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500822 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400823 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500824 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400825 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500826 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400827 return 1;
828}
829
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500830static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100831{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500832 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100833 case FMODE_WRITE:
834 state->n_wronly++;
835 break;
836 case FMODE_READ:
837 state->n_rdonly++;
838 break;
839 case FMODE_READ|FMODE_WRITE:
840 state->n_rdwr++;
841 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500842 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100843}
844
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500845static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400846{
847 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
848 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
849 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500850 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400851 case FMODE_READ:
852 set_bit(NFS_O_RDONLY_STATE, &state->flags);
853 break;
854 case FMODE_WRITE:
855 set_bit(NFS_O_WRONLY_STATE, &state->flags);
856 break;
857 case FMODE_READ|FMODE_WRITE:
858 set_bit(NFS_O_RDWR_STATE, &state->flags);
859 }
860}
861
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500862static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400863{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400864 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500865 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400866 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400867}
868
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500869static 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 -0700870{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400871 /*
872 * Protect the call to nfs4_state_set_mode_locked and
873 * serialise the stateid update
874 */
875 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400876 if (deleg_stateid != NULL) {
877 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
878 set_bit(NFS_DELEGATED_STATE, &state->flags);
879 }
880 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500881 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400882 write_sequnlock(&state->seqlock);
883 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500884 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700885 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886}
887
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500888static 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 -0500889{
890 struct nfs_inode *nfsi = NFS_I(state->inode);
891 struct nfs_delegation *deleg_cur;
892 int ret = 0;
893
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500894 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500895
896 rcu_read_lock();
897 deleg_cur = rcu_dereference(nfsi->delegation);
898 if (deleg_cur == NULL)
899 goto no_delegation;
900
901 spin_lock(&deleg_cur->lock);
902 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500903 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500904 goto no_delegation_unlock;
905
906 if (delegation == NULL)
907 delegation = &deleg_cur->stateid;
908 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
909 goto no_delegation_unlock;
910
Trond Myklebustb7391f42008-12-23 15:21:52 -0500911 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500912 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500913 ret = 1;
914no_delegation_unlock:
915 spin_unlock(&deleg_cur->lock);
916no_delegation:
917 rcu_read_unlock();
918
919 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500920 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500921 ret = 1;
922 }
923
924 return ret;
925}
926
927
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500928static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400929{
930 struct nfs_delegation *delegation;
931
932 rcu_read_lock();
933 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500934 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -0400935 rcu_read_unlock();
936 return;
937 }
938 rcu_read_unlock();
939 nfs_inode_return_delegation(inode);
940}
941
Trond Myklebust6ee41262007-07-08 14:11:36 -0400942static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400943{
944 struct nfs4_state *state = opendata->state;
945 struct nfs_inode *nfsi = NFS_I(state->inode);
946 struct nfs_delegation *delegation;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500947 int open_mode = opendata->o_arg.open_flags & O_EXCL;
948 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400949 nfs4_stateid stateid;
950 int ret = -EAGAIN;
951
Trond Myklebustaac00a82007-07-05 19:02:21 -0400952 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500953 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400954 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500955 if (can_open_cached(state, fmode, open_mode)) {
956 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400957 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400958 goto out_return_state;
959 }
960 spin_unlock(&state->owner->so_lock);
961 }
Trond Myklebust34310432008-12-23 15:21:38 -0500962 rcu_read_lock();
963 delegation = rcu_dereference(nfsi->delegation);
964 if (delegation == NULL ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500965 !can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -0500966 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -0400967 break;
Trond Myklebust34310432008-12-23 15:21:38 -0500968 }
Trond Myklebustaac00a82007-07-05 19:02:21 -0400969 /* Save the delegation */
970 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
971 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -0400972 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400973 if (ret != 0)
974 goto out;
975 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -0500976
977 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500978 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -0500979 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400980 }
Trond Myklebustaac00a82007-07-05 19:02:21 -0400981out:
982 return ERR_PTR(ret);
983out_return_state:
984 atomic_inc(&state->count);
985 return state;
986}
987
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100988static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
989{
990 struct inode *inode;
991 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -0400992 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400993 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100994
Trond Myklebustaac00a82007-07-05 19:02:21 -0400995 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400996 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400997 goto out;
998 }
999
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001000 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001001 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001002 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001003 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001004 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001005 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001006 goto err;
1007 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001008 state = nfs4_get_open_state(inode, data->owner);
1009 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001010 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001011 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001012 int delegation_flags = 0;
1013
Trond Myklebust003707c2007-07-05 18:07:55 -04001014 rcu_read_lock();
1015 delegation = rcu_dereference(NFS_I(inode)->delegation);
1016 if (delegation)
1017 delegation_flags = delegation->flags;
1018 rcu_read_unlock();
Trond Myklebust15c831b2008-12-23 15:21:39 -05001019 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001020 nfs_inode_set_delegation(state->inode,
1021 data->owner->so_cred,
1022 &data->o_res);
1023 else
1024 nfs_inode_reclaim_delegation(state->inode,
1025 data->owner->so_cred,
1026 &data->o_res);
1027 }
Trond Myklebust34310432008-12-23 15:21:38 -05001028
1029 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001030 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001031 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001032out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001033 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001034err_put_inode:
1035 iput(inode);
1036err:
1037 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001038}
1039
Trond Myklebust864472e2006-01-03 09:55:15 +01001040static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1041{
1042 struct nfs_inode *nfsi = NFS_I(state->inode);
1043 struct nfs_open_context *ctx;
1044
1045 spin_lock(&state->inode->i_lock);
1046 list_for_each_entry(ctx, &nfsi->open_files, list) {
1047 if (ctx->state != state)
1048 continue;
1049 get_nfs_open_context(ctx);
1050 spin_unlock(&state->inode->i_lock);
1051 return ctx;
1052 }
1053 spin_unlock(&state->inode->i_lock);
1054 return ERR_PTR(-ENOENT);
1055}
1056
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001057static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1058{
1059 struct nfs4_opendata *opendata;
1060
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001061 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001062 if (opendata == NULL)
1063 return ERR_PTR(-ENOMEM);
1064 opendata->state = state;
1065 atomic_inc(&state->count);
1066 return opendata;
1067}
1068
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001069static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001070{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001071 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001072 int ret;
1073
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001074 opendata->o_arg.open_flags = 0;
1075 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001076 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1077 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1078 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001079 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001080 if (ret != 0)
1081 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001082 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001083 if (IS_ERR(newstate))
1084 return PTR_ERR(newstate);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001085 nfs4_close_state(&opendata->path, newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001086 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001087 return 0;
1088}
1089
1090static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1091{
Trond Myklebust864472e2006-01-03 09:55:15 +01001092 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001093 int ret;
1094
1095 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001096 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001097 smp_rmb();
1098 if (state->n_rdwr != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001099 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001100 if (ret != 0)
1101 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001102 if (newstate != state)
1103 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001104 }
1105 if (state->n_wronly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001106 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001107 if (ret != 0)
1108 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001109 if (newstate != state)
1110 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001111 }
1112 if (state->n_rdonly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001113 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001114 if (ret != 0)
1115 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001116 if (newstate != state)
1117 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001118 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001119 /*
1120 * We may have performed cached opens for all three recoveries.
1121 * Check if we need to update the current stateid.
1122 */
1123 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1124 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001125 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001126 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1127 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001128 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001129 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001130 return 0;
1131}
1132
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133/*
1134 * OPEN_RECLAIM:
1135 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001137static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001139 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001140 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001141 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 int status;
1143
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001144 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1145 if (IS_ERR(opendata))
1146 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001147 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1148 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001149 rcu_read_lock();
1150 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001151 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001152 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001153 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001154 opendata->o_arg.u.delegation_type = delegation_type;
1155 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001156 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 return status;
1158}
1159
Trond Myklebust539cd032007-06-05 11:46:42 -04001160static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161{
1162 struct nfs_server *server = NFS_SERVER(state->inode);
1163 struct nfs4_exception exception = { };
1164 int err;
1165 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001166 err = _nfs4_do_open_reclaim(ctx, state);
Jeff Layton2c643482010-01-07 09:42:03 -05001167 if (err != -NFS4ERR_DELAY && err != -EKEYEXPIRED)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001168 break;
1169 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 } while (exception.retry);
1171 return err;
1172}
1173
Trond Myklebust864472e2006-01-03 09:55:15 +01001174static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1175{
1176 struct nfs_open_context *ctx;
1177 int ret;
1178
1179 ctx = nfs4_state_find_open_context(state);
1180 if (IS_ERR(ctx))
1181 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001182 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001183 put_nfs_open_context(ctx);
1184 return ret;
1185}
1186
Trond Myklebust13437e12007-07-06 15:10:43 -04001187static 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 -07001188{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001189 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001190 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001192 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1193 if (IS_ERR(opendata))
1194 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001195 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -04001196 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001197 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +01001198 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001199 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001200 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201}
1202
Trond Myklebust13437e12007-07-06 15:10:43 -04001203int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204{
1205 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001206 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 int err;
1208 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001209 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 switch (err) {
1211 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001212 case -ENOENT:
1213 case -ESTALE:
1214 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001215 case -NFS4ERR_BADSESSION:
1216 case -NFS4ERR_BADSLOT:
1217 case -NFS4ERR_BAD_HIGH_SLOT:
1218 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1219 case -NFS4ERR_DEADSESSION:
1220 nfs4_schedule_state_recovery(
1221 server->nfs_client);
1222 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 case -NFS4ERR_STALE_CLIENTID:
1224 case -NFS4ERR_STALE_STATEID:
1225 case -NFS4ERR_EXPIRED:
1226 /* Don't recall a delegation if it was lost */
David Howells7539bba2006-08-22 20:06:09 -04001227 nfs4_schedule_state_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001228 goto out;
1229 case -ERESTARTSYS:
1230 /*
1231 * The show must go on: exit, but mark the
1232 * stateid as needing recovery.
1233 */
1234 case -NFS4ERR_ADMIN_REVOKED:
1235 case -NFS4ERR_BAD_STATEID:
1236 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
1237 case -ENOMEM:
1238 err = 0;
1239 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 }
1241 err = nfs4_handle_exception(server, err, &exception);
1242 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001243out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 return err;
1245}
1246
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001247static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1248{
1249 struct nfs4_opendata *data = calldata;
1250
1251 data->rpc_status = task->tk_status;
1252 if (RPC_ASSASSINATED(task))
1253 return;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001254 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001255 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1256 sizeof(data->o_res.stateid.data));
Trond Myklebustbb226292008-01-02 15:19:18 -05001257 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001258 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001259 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001260 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001261}
1262
1263static void nfs4_open_confirm_release(void *calldata)
1264{
1265 struct nfs4_opendata *data = calldata;
1266 struct nfs4_state *state = NULL;
1267
1268 /* If this request hasn't been cancelled, do nothing */
1269 if (data->cancelled == 0)
1270 goto out_free;
1271 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001272 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001273 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001274 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001275 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001276 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001277out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001278 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001279}
1280
1281static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001282 .rpc_call_done = nfs4_open_confirm_done,
1283 .rpc_release = nfs4_open_confirm_release,
1284};
1285
1286/*
1287 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1288 */
1289static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1290{
1291 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1292 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001293 struct rpc_message msg = {
1294 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1295 .rpc_argp = &data->c_arg,
1296 .rpc_resp = &data->c_res,
1297 .rpc_cred = data->owner->so_cred,
1298 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001299 struct rpc_task_setup task_setup_data = {
1300 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001301 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001302 .callback_ops = &nfs4_open_confirm_ops,
1303 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001304 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001305 .flags = RPC_TASK_ASYNC,
1306 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 int status;
1308
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001309 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001310 data->rpc_done = 0;
1311 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001312 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001313 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001314 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001315 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001316 status = nfs4_wait_for_completion_rpc_task(task);
1317 if (status != 0) {
1318 data->cancelled = 1;
1319 smp_wmb();
1320 } else
1321 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001322 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 return status;
1324}
1325
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001326static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001328 struct nfs4_opendata *data = calldata;
1329 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001330
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001331 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1332 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001333 /*
1334 * Check if we still need to send an OPEN call, or if we can use
1335 * a delegation instead.
1336 */
1337 if (data->state != NULL) {
1338 struct nfs_delegation *delegation;
1339
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001340 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001341 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001342 rcu_read_lock();
1343 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1344 if (delegation != NULL &&
Trond Myklebust15c831b2008-12-23 15:21:39 -05001345 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001346 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001347 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001348 }
1349 rcu_read_unlock();
1350 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001351 /* Update sequence id. */
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001352 data->o_arg.id = sp->so_owner_id.id;
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001353 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001354 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001355 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001356 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1357 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001358 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001359 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001360 &data->o_arg.seq_args,
1361 &data->o_res.seq_res, 1, task))
1362 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001363 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001364 return;
1365out_no_action:
1366 task->tk_action = NULL;
1367
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001368}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001370static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1371{
1372 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1373 nfs4_open_prepare(task, calldata);
1374}
1375
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001376static void nfs4_open_done(struct rpc_task *task, void *calldata)
1377{
1378 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001380 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001381
Trond Myklebustd61e6122009-12-05 19:32:19 -05001382 nfs4_sequence_done(data->o_arg.server, &data->o_res.seq_res,
1383 task->tk_status);
Andy Adamsond8985282009-04-01 09:22:21 -04001384
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001385 if (RPC_ASSASSINATED(task))
1386 return;
1387 if (task->tk_status == 0) {
1388 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001389 case S_IFREG:
1390 break;
1391 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001392 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001393 break;
1394 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001395 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001396 break;
1397 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001398 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001399 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001400 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001401 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1402 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001403 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001404 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001405}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001406
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001407static void nfs4_open_release(void *calldata)
1408{
1409 struct nfs4_opendata *data = calldata;
1410 struct nfs4_state *state = NULL;
1411
1412 /* If this request hasn't been cancelled, do nothing */
1413 if (data->cancelled == 0)
1414 goto out_free;
1415 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001416 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001417 goto out_free;
1418 /* In case we need an open_confirm, no cleanup! */
1419 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1420 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001421 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001422 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001423 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001424out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001425 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001426}
1427
1428static const struct rpc_call_ops nfs4_open_ops = {
1429 .rpc_call_prepare = nfs4_open_prepare,
1430 .rpc_call_done = nfs4_open_done,
1431 .rpc_release = nfs4_open_release,
1432};
1433
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001434static const struct rpc_call_ops nfs4_recover_open_ops = {
1435 .rpc_call_prepare = nfs4_recover_open_prepare,
1436 .rpc_call_done = nfs4_open_done,
1437 .rpc_release = nfs4_open_release,
1438};
1439
1440static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001441{
1442 struct inode *dir = data->dir->d_inode;
1443 struct nfs_server *server = NFS_SERVER(dir);
1444 struct nfs_openargs *o_arg = &data->o_arg;
1445 struct nfs_openres *o_res = &data->o_res;
1446 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001447 struct rpc_message msg = {
1448 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1449 .rpc_argp = o_arg,
1450 .rpc_resp = o_res,
1451 .rpc_cred = data->owner->so_cred,
1452 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001453 struct rpc_task_setup task_setup_data = {
1454 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001455 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001456 .callback_ops = &nfs4_open_ops,
1457 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001458 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001459 .flags = RPC_TASK_ASYNC,
1460 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001461 int status;
1462
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001463 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001464 data->rpc_done = 0;
1465 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001466 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001467 if (isrecover)
1468 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001469 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001470 if (IS_ERR(task))
1471 return PTR_ERR(task);
1472 status = nfs4_wait_for_completion_rpc_task(task);
1473 if (status != 0) {
1474 data->cancelled = 1;
1475 smp_wmb();
1476 } else
1477 status = data->rpc_status;
1478 rpc_put_task(task);
1479
1480 return status;
1481}
1482
1483static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1484{
1485 struct inode *dir = data->dir->d_inode;
1486 struct nfs_openres *o_res = &data->o_res;
1487 int status;
1488
1489 status = nfs4_run_open_task(data, 1);
1490 if (status != 0 || !data->rpc_done)
1491 return status;
1492
1493 nfs_refresh_inode(dir, o_res->dir_attr);
1494
1495 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1496 status = _nfs4_proc_open_confirm(data);
1497 if (status != 0)
1498 return status;
1499 }
1500
1501 return status;
1502}
1503
1504/*
1505 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1506 */
1507static int _nfs4_proc_open(struct nfs4_opendata *data)
1508{
1509 struct inode *dir = data->dir->d_inode;
1510 struct nfs_server *server = NFS_SERVER(dir);
1511 struct nfs_openargs *o_arg = &data->o_arg;
1512 struct nfs_openres *o_res = &data->o_res;
1513 int status;
1514
1515 status = nfs4_run_open_task(data, 0);
Trond Myklebust3e309912007-07-07 13:19:59 -04001516 if (status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001517 return status;
1518
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001519 if (o_arg->open_flags & O_CREAT) {
1520 update_changeattr(dir, &o_res->cinfo);
1521 nfs_post_op_update_inode(dir, o_res->dir_attr);
1522 } else
1523 nfs_refresh_inode(dir, o_res->dir_attr);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001524 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1525 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001527 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001529 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 }
1531 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001532 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001533 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534}
1535
Trond Myklebust10afec92007-05-14 17:16:04 -04001536static int nfs4_recover_expired_lease(struct nfs_server *server)
Trond Myklebust58d97142006-01-03 09:55:24 +01001537{
David Howells7539bba2006-08-22 20:06:09 -04001538 struct nfs_client *clp = server->nfs_client;
Trond Myklebusta78cb572009-08-09 15:06:19 -04001539 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001540 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001541
Trond Myklebusta78cb572009-08-09 15:06:19 -04001542 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001543 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001544 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001545 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001546 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1547 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001548 break;
Trond Myklebust58d97142006-01-03 09:55:24 +01001549 nfs4_schedule_state_recovery(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001550 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001551 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001552 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001553}
1554
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555/*
1556 * OPEN_EXPIRED:
1557 * reclaim state on the server after a network partition.
1558 * Assumes caller holds the appropriate lock
1559 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001560static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001562 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001563 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001565 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1566 if (IS_ERR(opendata))
1567 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001568 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001569 if (ret == -ESTALE)
Trond Myklebust539cd032007-06-05 11:46:42 -04001570 d_drop(ctx->path.dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001571 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001572 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573}
1574
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001575static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001576{
Trond Myklebust539cd032007-06-05 11:46:42 -04001577 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001578 struct nfs4_exception exception = { };
1579 int err;
1580
1581 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001582 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001583 switch (err) {
1584 default:
1585 goto out;
1586 case -NFS4ERR_GRACE:
1587 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -05001588 case -EKEYEXPIRED:
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001589 nfs4_handle_exception(server, err, &exception);
1590 err = 0;
1591 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001592 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001593out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001594 return err;
1595}
1596
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1598{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001600 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
Trond Myklebust864472e2006-01-03 09:55:15 +01001602 ctx = nfs4_state_find_open_context(state);
1603 if (IS_ERR(ctx))
1604 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001605 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001606 put_nfs_open_context(ctx);
1607 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608}
1609
1610/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001611 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1612 * fields corresponding to attributes that were used to store the verifier.
1613 * Make sure we clobber those fields in the later setattr call
1614 */
1615static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1616{
1617 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1618 !(sattr->ia_valid & ATTR_ATIME_SET))
1619 sattr->ia_valid |= ATTR_ATIME;
1620
1621 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1622 !(sattr->ia_valid & ATTR_MTIME_SET))
1623 sattr->ia_valid |= ATTR_MTIME;
1624}
1625
1626/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001627 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001629static 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 -07001630{
1631 struct nfs4_state_owner *sp;
1632 struct nfs4_state *state = NULL;
1633 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001634 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001635 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
1637 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 status = -ENOMEM;
1639 if (!(sp = nfs4_get_state_owner(server, cred))) {
1640 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1641 goto out_err;
1642 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001643 status = nfs4_recover_expired_lease(server);
1644 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001645 goto err_put_state_owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001646 if (path->dentry->d_inode != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001647 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001648 status = -ENOMEM;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001649 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001650 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001651 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
Trond Myklebustaac00a82007-07-05 19:02:21 -04001653 if (path->dentry->d_inode != NULL)
1654 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1655
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001656 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001658 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001660 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001661 status = PTR_ERR(state);
1662 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001663 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001664 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001665 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001666
1667 if (opendata->o_arg.open_flags & O_EXCL) {
1668 nfs4_exclusive_attrset(opendata, sattr);
1669
1670 nfs_fattr_init(opendata->o_res.f_attr);
1671 status = nfs4_do_setattr(state->inode, cred,
1672 opendata->o_res.f_attr, sattr,
1673 state);
1674 if (status == 0)
1675 nfs_setattr_update_inode(state->inode, sattr);
1676 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1677 }
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001678 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 *res = state;
1681 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001682err_opendata_put:
1683 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001684err_put_state_owner:
1685 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 *res = NULL;
1688 return status;
1689}
1690
1691
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001692static 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 -07001693{
1694 struct nfs4_exception exception = { };
1695 struct nfs4_state *res;
1696 int status;
1697
1698 do {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001699 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 if (status == 0)
1701 break;
1702 /* NOTE: BAD_SEQID means the server and client disagree about the
1703 * book-keeping w.r.t. state-changing operations
1704 * (OPEN/CLOSE/LOCK/LOCKU...)
1705 * It is actually a sign of a bug on the client or on the server.
1706 *
1707 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001708 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 * have unhashed the old state_owner for us, and that we can
1710 * therefore safely retry using a new one. We should still warn
1711 * the user though...
1712 */
1713 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001714 printk(KERN_WARNING "NFS: v4 server %s "
1715 " returned a bad sequence-id error!\n",
1716 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 exception.retry = 1;
1718 continue;
1719 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001720 /*
1721 * BAD_STATEID on OPEN means that the server cancelled our
1722 * state before it received the OPEN_CONFIRM.
1723 * Recover by retrying the request as per the discussion
1724 * on Page 181 of RFC3530.
1725 */
1726 if (status == -NFS4ERR_BAD_STATEID) {
1727 exception.retry = 1;
1728 continue;
1729 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001730 if (status == -EAGAIN) {
1731 /* We must have found a delegation */
1732 exception.retry = 1;
1733 continue;
1734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1736 status, &exception));
1737 } while (exception.retry);
1738 return res;
1739}
1740
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001741static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1742 struct nfs_fattr *fattr, struct iattr *sattr,
1743 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001745 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001747 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 .iap = sattr,
1749 .server = server,
1750 .bitmask = server->attr_bitmask,
1751 };
1752 struct nfs_setattrres res = {
1753 .fattr = fattr,
1754 .server = server,
1755 };
1756 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001757 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1758 .rpc_argp = &arg,
1759 .rpc_resp = &res,
1760 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001762 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001763 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
Trond Myklebust0e574af2005-10-27 22:12:38 -04001765 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001767 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1768 /* Use that stateid */
1769 } else if (state != NULL) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001770 nfs4_copy_stateid(&arg.stateid, state, current->files);
1771 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1773
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001774 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001775 if (status == 0 && state != NULL)
1776 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001777 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778}
1779
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001780static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1781 struct nfs_fattr *fattr, struct iattr *sattr,
1782 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001784 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 struct nfs4_exception exception = { };
1786 int err;
1787 do {
1788 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001789 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 &exception);
1791 } while (exception.retry);
1792 return err;
1793}
1794
1795struct nfs4_closedata {
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001796 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 struct inode *inode;
1798 struct nfs4_state *state;
1799 struct nfs_closeargs arg;
1800 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001801 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001802 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803};
1804
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001805static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001806{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001807 struct nfs4_closedata *calldata = data;
1808 struct nfs4_state_owner *sp = calldata->state->owner;
Trond Myklebust95121352005-10-18 14:20:12 -07001809
1810 nfs4_put_open_state(calldata->state);
1811 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001812 nfs4_put_state_owner(sp);
Jan Blunck31f31db12008-05-02 13:42:45 -07001813 path_put(&calldata->path);
Trond Myklebust95121352005-10-18 14:20:12 -07001814 kfree(calldata);
1815}
1816
Trond Myklebust88069f72009-12-08 08:33:16 -05001817static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1818 fmode_t fmode)
1819{
1820 spin_lock(&state->owner->so_lock);
1821 if (!(fmode & FMODE_READ))
1822 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1823 if (!(fmode & FMODE_WRITE))
1824 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1825 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1826 spin_unlock(&state->owner->so_lock);
1827}
1828
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001829static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001831 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 struct nfs_server *server = NFS_SERVER(calldata->inode);
1834
Andy Adamson19ddab02009-04-01 09:22:20 -04001835 nfs4_sequence_done(server, &calldata->res.seq_res, task->tk_status);
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001836 if (RPC_ASSASSINATED(task))
1837 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 /* hmm. we are done with the inode, and in the process of freeing
1839 * the state_owner. we keep this around to process errors
1840 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 switch (task->tk_status) {
1842 case 0:
Trond Myklebust45328c32007-07-26 17:47:34 -04001843 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001844 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05001845 nfs4_close_clear_stateid_flags(state,
1846 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 break;
1848 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001849 case -NFS4ERR_OLD_STATEID:
1850 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001852 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001853 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05001855 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1856 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 }
Trond Myklebust72211db2009-12-15 14:47:36 -05001858 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04001859 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860}
1861
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001862static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001864 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001865 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05001866 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07001867
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001868 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001869 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001870
Trond Myklebust88069f72009-12-08 08:33:16 -05001871 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1872 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05001873 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001874 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01001875 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001876 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001877 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
1878 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1879 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04001880 }
1881 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001882 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
1883 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1884 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04001885 }
Trond Myklebuste7616922006-01-03 09:55:13 +01001886 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05001887 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05001888
1889 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001890 /* Note: exit _without_ calling nfs4_close_done */
1891 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07001892 return;
1893 }
Trond Myklebust88069f72009-12-08 08:33:16 -05001894
1895 if (calldata->arg.fmode == 0)
1896 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
1897
Trond Myklebust516a6af2005-10-27 22:12:41 -04001898 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001899 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001900 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
Andy Adamson19ddab02009-04-01 09:22:20 -04001901 &calldata->arg.seq_args, &calldata->res.seq_res,
1902 1, task))
1903 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001904 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905}
1906
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001907static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001908 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001909 .rpc_call_done = nfs4_close_done,
1910 .rpc_release = nfs4_free_closedata,
1911};
1912
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913/*
1914 * It is possible for data to be read/written from a mem-mapped file
1915 * after the sys_close call (which hits the vfs layer as a flush).
1916 * This means that we can't safely call nfsv4 close on a file until
1917 * the inode is cleared. This in turn means that we are not good
1918 * NFSv4 citizens - we do not indicate to the server to update the file's
1919 * share state even when we are done with one of the three share
1920 * stateid's in the inode.
1921 *
1922 * NOTE: Caller must be holding the sp->so_owner semaphore!
1923 */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001924int nfs4_do_close(struct path *path, struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001926 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04001928 struct nfs4_state_owner *sp = state->owner;
1929 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001930 struct rpc_message msg = {
1931 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1932 .rpc_cred = state->owner->so_cred,
1933 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001934 struct rpc_task_setup task_setup_data = {
1935 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001936 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001937 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05001938 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001939 .flags = RPC_TASK_ASYNC,
1940 };
Trond Myklebust95121352005-10-18 14:20:12 -07001941 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001943 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07001945 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001946 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001948 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04001949 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001951 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07001952 if (calldata->arg.seqid == NULL)
1953 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001954 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04001955 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001956 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04001957 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001958 calldata->res.server = server;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04001959 calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Al Virof6948692010-01-30 13:51:04 -05001960 path_get(path);
1961 calldata->path = *path;
Trond Myklebust95121352005-10-18 14:20:12 -07001962
Trond Myklebust5138fde2007-07-14 15:40:01 -04001963 msg.rpc_argp = &calldata->arg,
1964 msg.rpc_resp = &calldata->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001965 task_setup_data.callback_data = calldata;
1966 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001967 if (IS_ERR(task))
1968 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001969 status = 0;
1970 if (wait)
1971 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001972 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001973 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07001974out_free_calldata:
1975 kfree(calldata);
1976out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04001977 nfs4_put_open_state(state);
1978 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07001979 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980}
1981
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001982static 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 -07001983{
1984 struct file *filp;
Trond Myklebust1b45c462007-07-03 13:04:56 -04001985 int ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001986
Trond Myklebust1b45c462007-07-03 13:04:56 -04001987 /* If the open_intent is for execute, we have an extra check to make */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001988 if (fmode & FMODE_EXEC) {
Trond Myklebustaf22f942007-08-10 17:45:10 -04001989 ret = nfs_may_open(state->inode,
Trond Myklebust1b45c462007-07-03 13:04:56 -04001990 state->owner->so_cred,
1991 nd->intent.open.flags);
1992 if (ret < 0)
1993 goto out_close;
1994 }
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001995 filp = lookup_instantiate_filp(nd, path->dentry, NULL);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001996 if (!IS_ERR(filp)) {
1997 struct nfs_open_context *ctx;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04001998 ctx = nfs_file_open_context(filp);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001999 ctx->state = state;
Trond Myklebust95cf9592006-04-18 13:14:06 -04002000 return 0;
2001 }
Trond Myklebust1b45c462007-07-03 13:04:56 -04002002 ret = PTR_ERR(filp);
2003out_close:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002004 nfs4_close_sync(path, state, fmode & (FMODE_READ|FMODE_WRITE));
Trond Myklebust1b45c462007-07-03 13:04:56 -04002005 return ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002006}
2007
2008struct dentry *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
2010{
Trond Myklebustad389da2007-06-05 12:30:00 -04002011 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08002012 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04002013 .dentry = dentry,
2014 };
Jan Blunck4ac91372008-02-14 19:34:32 -08002015 struct dentry *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 struct iattr attr;
2017 struct rpc_cred *cred;
2018 struct nfs4_state *state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002019 struct dentry *res;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002020 fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
2022 if (nd->flags & LOOKUP_CREATE) {
2023 attr.ia_mode = nd->intent.open.create_mode;
2024 attr.ia_valid = ATTR_MODE;
2025 if (!IS_POSIXACL(dir))
Al Viroce3b0f82009-03-29 19:08:22 -04002026 attr.ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 } else {
2028 attr.ia_valid = 0;
2029 BUG_ON(nd->intent.open.flags & O_CREAT);
2030 }
2031
Trond Myklebust98a8e322008-03-12 12:25:28 -04002032 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 if (IS_ERR(cred))
Trond Myklebust02a913a2005-10-18 14:20:17 -07002034 return (struct dentry *)cred;
Trond Myklebust565277f2007-10-15 18:17:53 -04002035 parent = dentry->d_parent;
2036 /* Protect against concurrent sillydeletes */
2037 nfs_block_sillyrename(parent);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002038 state = nfs4_do_open(dir, &path, fmode, nd->intent.open.flags, &attr, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002040 if (IS_ERR(state)) {
Trond Myklebustd75340c2007-10-01 21:42:01 -04002041 if (PTR_ERR(state) == -ENOENT) {
Trond Myklebust02a913a2005-10-18 14:20:17 -07002042 d_add(dentry, NULL);
Trond Myklebustd75340c2007-10-01 21:42:01 -04002043 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2044 }
Trond Myklebust565277f2007-10-15 18:17:53 -04002045 nfs_unblock_sillyrename(parent);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002046 return (struct dentry *)state;
2047 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002048 res = d_add_unique(dentry, igrab(state->inode));
Trond Myklebust02a913a2005-10-18 14:20:17 -07002049 if (res != NULL)
Trond Myklebustdeee9362007-08-27 11:33:00 -04002050 path.dentry = res;
Trond Myklebustd75340c2007-10-01 21:42:01 -04002051 nfs_set_verifier(path.dentry, nfs_save_change_attribute(dir));
Trond Myklebust565277f2007-10-15 18:17:53 -04002052 nfs_unblock_sillyrename(parent);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002053 nfs4_intent_set_file(nd, &path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002054 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055}
2056
2057int
Trond Myklebust02a913a2005-10-18 14:20:17 -07002058nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059{
Trond Myklebustad389da2007-06-05 12:30:00 -04002060 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08002061 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04002062 .dentry = dentry,
2063 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 struct rpc_cred *cred;
2065 struct nfs4_state *state;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002066 fmode_t fmode = openflags & (FMODE_READ | FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
Trond Myklebust98a8e322008-03-12 12:25:28 -04002068 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 if (IS_ERR(cred))
2070 return PTR_ERR(cred);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002071 state = nfs4_do_open(dir, &path, fmode, openflags, NULL, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002073 if (IS_ERR(state)) {
2074 switch (PTR_ERR(state)) {
2075 case -EPERM:
2076 case -EACCES:
2077 case -EDQUOT:
2078 case -ENOSPC:
2079 case -EROFS:
Al Viro04287f92010-04-08 00:06:07 +01002080 return PTR_ERR(state);
Chuck Leverb87c0ad2006-10-19 23:28:42 -07002081 default:
2082 goto out_drop;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002083 }
Trond Myklebust02a913a2005-10-18 14:20:17 -07002084 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002085 if (state->inode == dentry->d_inode) {
Trond Myklebustd75340c2007-10-01 21:42:01 -04002086 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002087 nfs4_intent_set_file(nd, &path, state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 return 1;
2089 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002090 nfs4_close_sync(&path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002091out_drop:
2092 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 return 0;
2094}
2095
Trond Myklebust1185a552009-12-03 15:54:02 -05002096static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002097{
2098 if (ctx->state == NULL)
2099 return;
2100 if (is_sync)
2101 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
2102 else
2103 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
2104}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105
2106static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2107{
Benny Halevy43652ad2009-04-01 09:21:54 -04002108 struct nfs4_server_caps_arg args = {
2109 .fhandle = fhandle,
2110 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 struct nfs4_server_caps_res res = {};
2112 struct rpc_message msg = {
2113 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002114 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 .rpc_resp = &res,
2116 };
2117 int status;
2118
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002119 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 if (status == 0) {
2121 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002122 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2123 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2124 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2125 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2126 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2128 server->caps |= NFS_CAP_ACLS;
2129 if (res.has_links != 0)
2130 server->caps |= NFS_CAP_HARDLINKS;
2131 if (res.has_symlinks != 0)
2132 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002133 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2134 server->caps |= NFS_CAP_FILEID;
2135 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2136 server->caps |= NFS_CAP_MODE;
2137 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2138 server->caps |= NFS_CAP_NLINK;
2139 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2140 server->caps |= NFS_CAP_OWNER;
2141 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2142 server->caps |= NFS_CAP_OWNER_GROUP;
2143 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2144 server->caps |= NFS_CAP_ATIME;
2145 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2146 server->caps |= NFS_CAP_CTIME;
2147 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2148 server->caps |= NFS_CAP_MTIME;
2149
Trond Myklebusta65318b2009-03-11 14:10:28 -04002150 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2151 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2152 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 server->acl_bitmask = res.acl_bitmask;
2154 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002155
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 return status;
2157}
2158
Trond Myklebust55a97592006-06-09 09:34:19 -04002159int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160{
2161 struct nfs4_exception exception = { };
2162 int err;
2163 do {
2164 err = nfs4_handle_exception(server,
2165 _nfs4_server_capabilities(server, fhandle),
2166 &exception);
2167 } while (exception.retry);
2168 return err;
2169}
2170
2171static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2172 struct nfs_fsinfo *info)
2173{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 struct nfs4_lookup_root_arg args = {
2175 .bitmask = nfs4_fattr_bitmap,
2176 };
2177 struct nfs4_lookup_res res = {
2178 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002179 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 .fh = fhandle,
2181 };
2182 struct rpc_message msg = {
2183 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2184 .rpc_argp = &args,
2185 .rpc_resp = &res,
2186 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002187
Trond Myklebust0e574af2005-10-27 22:12:38 -04002188 nfs_fattr_init(info->fattr);
Trond Myklebustfccba802009-07-21 16:48:07 -04002189 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190}
2191
2192static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2193 struct nfs_fsinfo *info)
2194{
2195 struct nfs4_exception exception = { };
2196 int err;
2197 do {
2198 err = nfs4_handle_exception(server,
2199 _nfs4_lookup_root(server, fhandle, info),
2200 &exception);
2201 } while (exception.retry);
2202 return err;
2203}
2204
David Howells54ceac42006-08-22 20:06:13 -04002205/*
2206 * get the file handle for the "/" directory on the server
2207 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002209 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 int status;
2212
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 status = nfs4_lookup_root(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 if (status == 0)
2215 status = nfs4_server_capabilities(server, fhandle);
2216 if (status == 0)
2217 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002218 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219}
2220
Manoj Naik6b97fd32006-06-09 09:34:29 -04002221/*
2222 * Get locations and (maybe) other attributes of a referral.
2223 * Note that we'll actually follow the referral later when
2224 * we detect fsid mismatch in inode revalidation
2225 */
Trond Myklebust56659e92007-07-17 21:52:39 -04002226static 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 -04002227{
2228 int status = -ENOMEM;
2229 struct page *page = NULL;
2230 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002231
2232 page = alloc_page(GFP_KERNEL);
2233 if (page == NULL)
2234 goto out;
2235 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2236 if (locations == NULL)
2237 goto out;
2238
Trond Myklebustc228fd32007-01-13 02:28:11 -05002239 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002240 if (status != 0)
2241 goto out;
2242 /* Make sure server returned a different fsid for the referral */
2243 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -07002244 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 -04002245 status = -EIO;
2246 goto out;
2247 }
2248
2249 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2250 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
2251 if (!fattr->mode)
2252 fattr->mode = S_IFDIR;
2253 memset(fhandle, 0, sizeof(struct nfs_fh));
2254out:
2255 if (page)
2256 __free_page(page);
2257 if (locations)
2258 kfree(locations);
2259 return status;
2260}
2261
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2263{
2264 struct nfs4_getattr_arg args = {
2265 .fh = fhandle,
2266 .bitmask = server->attr_bitmask,
2267 };
2268 struct nfs4_getattr_res res = {
2269 .fattr = fattr,
2270 .server = server,
2271 };
2272 struct rpc_message msg = {
2273 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2274 .rpc_argp = &args,
2275 .rpc_resp = &res,
2276 };
2277
Trond Myklebust0e574af2005-10-27 22:12:38 -04002278 nfs_fattr_init(fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002279 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280}
2281
2282static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2283{
2284 struct nfs4_exception exception = { };
2285 int err;
2286 do {
2287 err = nfs4_handle_exception(server,
2288 _nfs4_proc_getattr(server, fhandle, fattr),
2289 &exception);
2290 } while (exception.retry);
2291 return err;
2292}
2293
2294/*
2295 * The file is not closed if it is opened due to the a request to change
2296 * the size of the file. The open call will not be needed once the
2297 * VFS layer lookup-intents are implemented.
2298 *
2299 * Close is called when the inode is destroyed.
2300 * If we haven't opened the file for O_WRONLY, we
2301 * need to in the size_change case to obtain a stateid.
2302 *
2303 * Got race?
2304 * Because OPEN is always done by name in nfsv4, it is
2305 * possible that we opened a different file by the same
2306 * name. We can recognize this race condition, but we
2307 * can't do anything about it besides returning an error.
2308 *
2309 * This will be fixed with VFS changes (lookup-intent).
2310 */
2311static int
2312nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2313 struct iattr *sattr)
2314{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002315 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002316 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002317 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 int status;
2319
Trond Myklebust0e574af2005-10-27 22:12:38 -04002320 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321
Trond Myklebustd5308382005-11-04 15:33:38 -05002322 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002323 if (sattr->ia_valid & ATTR_FILE) {
2324 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002325
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002326 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002327 if (ctx) {
2328 cred = ctx->cred;
2329 state = ctx->state;
2330 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002331 }
2332
2333 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002334 if (status == 0)
2335 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 return status;
2337}
2338
Trond Myklebust56659e92007-07-17 21:52:39 -04002339static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
2340 const struct qstr *name, struct nfs_fh *fhandle,
David Howells2b3de442006-08-22 20:06:09 -04002341 struct nfs_fattr *fattr)
2342{
2343 int status;
2344 struct nfs4_lookup_arg args = {
2345 .bitmask = server->attr_bitmask,
2346 .dir_fh = dirfh,
2347 .name = name,
2348 };
2349 struct nfs4_lookup_res res = {
2350 .server = server,
2351 .fattr = fattr,
2352 .fh = fhandle,
2353 };
2354 struct rpc_message msg = {
2355 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2356 .rpc_argp = &args,
2357 .rpc_resp = &res,
2358 };
2359
2360 nfs_fattr_init(fattr);
2361
2362 dprintk("NFS call lookupfh %s\n", name->name);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002363 status = nfs4_call_sync(server, &msg, &args, &res, 0);
David Howells2b3de442006-08-22 20:06:09 -04002364 dprintk("NFS reply lookupfh: %d\n", status);
David Howells2b3de442006-08-22 20:06:09 -04002365 return status;
2366}
2367
2368static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2369 struct qstr *name, struct nfs_fh *fhandle,
2370 struct nfs_fattr *fattr)
2371{
2372 struct nfs4_exception exception = { };
2373 int err;
2374 do {
Trond Myklebust4e56e082007-07-01 18:13:52 -04002375 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
2376 /* FIXME: !!!! */
2377 if (err == -NFS4ERR_MOVED) {
2378 err = -EREMOTE;
2379 break;
2380 }
2381 err = nfs4_handle_exception(server, err, &exception);
David Howells2b3de442006-08-22 20:06:09 -04002382 } while (exception.retry);
2383 return err;
2384}
2385
Trond Myklebust56659e92007-07-17 21:52:39 -04002386static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2388{
Trond Myklebust4e56e082007-07-01 18:13:52 -04002389 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390
2391 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust4e56e082007-07-01 18:13:52 -04002392 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002393 if (status == -NFS4ERR_MOVED)
2394 status = nfs4_get_referral(dir, name, fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 dprintk("NFS reply lookup: %d\n", status);
2396 return status;
2397}
2398
2399static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2400{
2401 struct nfs4_exception exception = { };
2402 int err;
2403 do {
2404 err = nfs4_handle_exception(NFS_SERVER(dir),
2405 _nfs4_proc_lookup(dir, name, fhandle, fattr),
2406 &exception);
2407 } while (exception.retry);
2408 return err;
2409}
2410
2411static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2412{
Trond Myklebust76b32992007-08-10 17:45:11 -04002413 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 struct nfs4_accessargs args = {
2415 .fh = NFS_FH(inode),
Trond Myklebust76b32992007-08-10 17:45:11 -04002416 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002418 struct nfs4_accessres res = {
2419 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002420 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 struct rpc_message msg = {
2422 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2423 .rpc_argp = &args,
2424 .rpc_resp = &res,
2425 .rpc_cred = entry->cred,
2426 };
2427 int mode = entry->mask;
2428 int status;
2429
2430 /*
2431 * Determine which access bits we want to ask for...
2432 */
2433 if (mode & MAY_READ)
2434 args.access |= NFS4_ACCESS_READ;
2435 if (S_ISDIR(inode->i_mode)) {
2436 if (mode & MAY_WRITE)
2437 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2438 if (mode & MAY_EXEC)
2439 args.access |= NFS4_ACCESS_LOOKUP;
2440 } else {
2441 if (mode & MAY_WRITE)
2442 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2443 if (mode & MAY_EXEC)
2444 args.access |= NFS4_ACCESS_EXECUTE;
2445 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002446
2447 res.fattr = nfs_alloc_fattr();
2448 if (res.fattr == NULL)
2449 return -ENOMEM;
2450
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002451 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 if (!status) {
2453 entry->mask = 0;
2454 if (res.access & NFS4_ACCESS_READ)
2455 entry->mask |= MAY_READ;
2456 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2457 entry->mask |= MAY_WRITE;
2458 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2459 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002460 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002462 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 return status;
2464}
2465
2466static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2467{
2468 struct nfs4_exception exception = { };
2469 int err;
2470 do {
2471 err = nfs4_handle_exception(NFS_SERVER(inode),
2472 _nfs4_proc_access(inode, entry),
2473 &exception);
2474 } while (exception.retry);
2475 return err;
2476}
2477
2478/*
2479 * TODO: For the time being, we don't try to get any attributes
2480 * along with any of the zero-copy operations READ, READDIR,
2481 * READLINK, WRITE.
2482 *
2483 * In the case of the first three, we want to put the GETATTR
2484 * after the read-type operation -- this is because it is hard
2485 * to predict the length of a GETATTR response in v4, and thus
2486 * align the READ data correctly. This means that the GETATTR
2487 * may end up partially falling into the page cache, and we should
2488 * shift it into the 'tail' of the xdr_buf before processing.
2489 * To do this efficiently, we need to know the total length
2490 * of data received, which doesn't seem to be available outside
2491 * of the RPC layer.
2492 *
2493 * In the case of WRITE, we also want to put the GETATTR after
2494 * the operation -- in this case because we want to make sure
2495 * we get the post-operation mtime and size. This means that
2496 * we can't use xdr_encode_pages() as written: we need a variant
2497 * of it which would leave room in the 'tail' iovec.
2498 *
2499 * Both of these changes to the XDR layer would in fact be quite
2500 * minor, but I decided to leave them for a subsequent patch.
2501 */
2502static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2503 unsigned int pgbase, unsigned int pglen)
2504{
2505 struct nfs4_readlink args = {
2506 .fh = NFS_FH(inode),
2507 .pgbase = pgbase,
2508 .pglen = pglen,
2509 .pages = &page,
2510 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002511 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 struct rpc_message msg = {
2513 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2514 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002515 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 };
2517
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002518 return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519}
2520
2521static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2522 unsigned int pgbase, unsigned int pglen)
2523{
2524 struct nfs4_exception exception = { };
2525 int err;
2526 do {
2527 err = nfs4_handle_exception(NFS_SERVER(inode),
2528 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2529 &exception);
2530 } while (exception.retry);
2531 return err;
2532}
2533
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534/*
2535 * Got race?
2536 * We will need to arrange for the VFS layer to provide an atomic open.
2537 * Until then, this create/open method is prone to inefficiency and race
2538 * conditions due to the lookup, create, and open VFS calls from sys_open()
2539 * placed on the wire.
2540 *
2541 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2542 * The file will be opened again in the subsequent VFS open call
2543 * (nfs4_proc_file_open).
2544 *
2545 * The open for read will just hang around to be used by any process that
2546 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2547 */
2548
2549static int
2550nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebust02a913a2005-10-18 14:20:17 -07002551 int flags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552{
Trond Myklebustad389da2007-06-05 12:30:00 -04002553 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08002554 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04002555 .dentry = dentry,
2556 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 struct nfs4_state *state;
2558 struct rpc_cred *cred;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002559 fmode_t fmode = flags & (FMODE_READ | FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 int status = 0;
2561
Trond Myklebust98a8e322008-03-12 12:25:28 -04002562 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 if (IS_ERR(cred)) {
2564 status = PTR_ERR(cred);
2565 goto out;
2566 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002567 state = nfs4_do_open(dir, &path, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002568 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 if (IS_ERR(state)) {
2570 status = PTR_ERR(state);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002571 goto out_putcred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002573 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002574 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustad389da2007-06-05 12:30:00 -04002575 if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002576 status = nfs4_intent_set_file(nd, &path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002577 else
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002578 nfs4_close_sync(&path, state, fmode);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002579out_putcred:
2580 put_rpccred(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581out:
2582 return status;
2583}
2584
2585static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2586{
Trond Myklebust16e42952005-10-27 22:12:44 -04002587 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002588 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002590 .name.len = name->len,
2591 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002592 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002594 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002595 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002596 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002598 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2599 .rpc_argp = &args,
2600 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 };
Trond Myklebustd3468902010-04-16 16:22:50 -04002602 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603
Trond Myklebustd3468902010-04-16 16:22:50 -04002604 res.dir_attr = nfs_alloc_fattr();
2605 if (res.dir_attr == NULL)
2606 goto out;
2607
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002608 status = nfs4_call_sync(server, &msg, &args, &res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002609 if (status == 0) {
2610 update_changeattr(dir, &res.cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002611 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002612 }
Trond Myklebustd3468902010-04-16 16:22:50 -04002613 nfs_free_fattr(res.dir_attr);
2614out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 return status;
2616}
2617
2618static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2619{
2620 struct nfs4_exception exception = { };
2621 int err;
2622 do {
2623 err = nfs4_handle_exception(NFS_SERVER(dir),
2624 _nfs4_proc_remove(dir, name),
2625 &exception);
2626 } while (exception.retry);
2627 return err;
2628}
2629
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002630static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002632 struct nfs_server *server = NFS_SERVER(dir);
2633 struct nfs_removeargs *args = msg->rpc_argp;
2634 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635
Trond Myklebusta65318b2009-03-11 14:10:28 -04002636 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002637 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639}
2640
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002641static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002643 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2644
Andy Adamson472cfbd2009-04-01 09:22:24 -04002645 nfs4_sequence_done(res->server, &res->seq_res, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002646 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002647 return 0;
2648 update_changeattr(dir, &res->cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002649 nfs_post_op_update_inode(dir, res->dir_attr);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002650 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651}
2652
2653static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2654 struct inode *new_dir, struct qstr *new_name)
2655{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002656 struct nfs_server *server = NFS_SERVER(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 struct nfs4_rename_arg arg = {
2658 .old_dir = NFS_FH(old_dir),
2659 .new_dir = NFS_FH(new_dir),
2660 .old_name = old_name,
2661 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002662 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 };
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002664 struct nfs4_rename_res res = {
2665 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002666 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 struct rpc_message msg = {
2668 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2669 .rpc_argp = &arg,
2670 .rpc_resp = &res,
2671 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002672 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673
Trond Myklebust011fff72010-04-16 16:22:49 -04002674 res.old_fattr = nfs_alloc_fattr();
2675 res.new_fattr = nfs_alloc_fattr();
2676 if (res.old_fattr == NULL || res.new_fattr == NULL)
2677 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678
Trond Myklebust011fff72010-04-16 16:22:49 -04002679 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 if (!status) {
2681 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002682 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002684 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 }
Trond Myklebust011fff72010-04-16 16:22:49 -04002686out:
2687 nfs_free_fattr(res.new_fattr);
2688 nfs_free_fattr(res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 return status;
2690}
2691
2692static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2693 struct inode *new_dir, struct qstr *new_name)
2694{
2695 struct nfs4_exception exception = { };
2696 int err;
2697 do {
2698 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2699 _nfs4_proc_rename(old_dir, old_name,
2700 new_dir, new_name),
2701 &exception);
2702 } while (exception.retry);
2703 return err;
2704}
2705
2706static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2707{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002708 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 struct nfs4_link_arg arg = {
2710 .fh = NFS_FH(inode),
2711 .dir_fh = NFS_FH(dir),
2712 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002713 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002715 struct nfs4_link_res res = {
2716 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002717 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 struct rpc_message msg = {
2719 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2720 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002721 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 };
Trond Myklebust136f2622010-04-16 16:22:49 -04002723 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724
Trond Myklebust136f2622010-04-16 16:22:49 -04002725 res.fattr = nfs_alloc_fattr();
2726 res.dir_attr = nfs_alloc_fattr();
2727 if (res.fattr == NULL || res.dir_attr == NULL)
2728 goto out;
2729
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002730 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002731 if (!status) {
2732 update_changeattr(dir, &res.cinfo);
2733 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002734 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002735 }
Trond Myklebust136f2622010-04-16 16:22:49 -04002736out:
2737 nfs_free_fattr(res.dir_attr);
2738 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 return status;
2740}
2741
2742static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2743{
2744 struct nfs4_exception exception = { };
2745 int err;
2746 do {
2747 err = nfs4_handle_exception(NFS_SERVER(inode),
2748 _nfs4_proc_link(inode, dir, name),
2749 &exception);
2750 } while (exception.retry);
2751 return err;
2752}
2753
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002754struct nfs4_createdata {
2755 struct rpc_message msg;
2756 struct nfs4_create_arg arg;
2757 struct nfs4_create_res res;
2758 struct nfs_fh fh;
2759 struct nfs_fattr fattr;
2760 struct nfs_fattr dir_fattr;
2761};
2762
2763static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2764 struct qstr *name, struct iattr *sattr, u32 ftype)
2765{
2766 struct nfs4_createdata *data;
2767
2768 data = kzalloc(sizeof(*data), GFP_KERNEL);
2769 if (data != NULL) {
2770 struct nfs_server *server = NFS_SERVER(dir);
2771
2772 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2773 data->msg.rpc_argp = &data->arg;
2774 data->msg.rpc_resp = &data->res;
2775 data->arg.dir_fh = NFS_FH(dir);
2776 data->arg.server = server;
2777 data->arg.name = name;
2778 data->arg.attrs = sattr;
2779 data->arg.ftype = ftype;
2780 data->arg.bitmask = server->attr_bitmask;
2781 data->res.server = server;
2782 data->res.fh = &data->fh;
2783 data->res.fattr = &data->fattr;
2784 data->res.dir_fattr = &data->dir_fattr;
2785 nfs_fattr_init(data->res.fattr);
2786 nfs_fattr_init(data->res.dir_fattr);
2787 }
2788 return data;
2789}
2790
2791static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2792{
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002793 int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
2794 &data->arg, &data->res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002795 if (status == 0) {
2796 update_changeattr(dir, &data->res.dir_cinfo);
2797 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2798 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2799 }
2800 return status;
2801}
2802
2803static void nfs4_free_createdata(struct nfs4_createdata *data)
2804{
2805 kfree(data);
2806}
2807
Chuck Lever4f390c12006-08-22 20:06:22 -04002808static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002809 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002811 struct nfs4_createdata *data;
2812 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813
Chuck Lever94a6d752006-08-22 20:06:23 -04002814 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002815 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002816
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002817 status = -ENOMEM;
2818 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2819 if (data == NULL)
2820 goto out;
2821
2822 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2823 data->arg.u.symlink.pages = &page;
2824 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002826 status = nfs4_do_create(dir, dentry, data);
2827
2828 nfs4_free_createdata(data);
2829out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 return status;
2831}
2832
Chuck Lever4f390c12006-08-22 20:06:22 -04002833static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002834 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835{
2836 struct nfs4_exception exception = { };
2837 int err;
2838 do {
2839 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002840 _nfs4_proc_symlink(dir, dentry, page,
2841 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 &exception);
2843 } while (exception.retry);
2844 return err;
2845}
2846
2847static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2848 struct iattr *sattr)
2849{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002850 struct nfs4_createdata *data;
2851 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002853 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2854 if (data == NULL)
2855 goto out;
2856
2857 status = nfs4_do_create(dir, dentry, data);
2858
2859 nfs4_free_createdata(data);
2860out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 return status;
2862}
2863
2864static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2865 struct iattr *sattr)
2866{
2867 struct nfs4_exception exception = { };
2868 int err;
2869 do {
2870 err = nfs4_handle_exception(NFS_SERVER(dir),
2871 _nfs4_proc_mkdir(dir, dentry, sattr),
2872 &exception);
2873 } while (exception.retry);
2874 return err;
2875}
2876
2877static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2878 u64 cookie, struct page *page, unsigned int count, int plus)
2879{
2880 struct inode *dir = dentry->d_inode;
2881 struct nfs4_readdir_arg args = {
2882 .fh = NFS_FH(dir),
2883 .pages = &page,
2884 .pgbase = 0,
2885 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09002886 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 };
2888 struct nfs4_readdir_res res;
2889 struct rpc_message msg = {
2890 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2891 .rpc_argp = &args,
2892 .rpc_resp = &res,
2893 .rpc_cred = cred,
2894 };
2895 int status;
2896
Harvey Harrison3110ff82008-05-02 13:42:44 -07002897 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00002898 dentry->d_parent->d_name.name,
2899 dentry->d_name.name,
2900 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2902 res.pgbase = args.pgbase;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002903 status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 if (status == 0)
2905 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustc4812992007-09-28 17:11:45 -04002906
2907 nfs_invalidate_atime(dir);
2908
Harvey Harrison3110ff82008-05-02 13:42:44 -07002909 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 return status;
2911}
2912
2913static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2914 u64 cookie, struct page *page, unsigned int count, int plus)
2915{
2916 struct nfs4_exception exception = { };
2917 int err;
2918 do {
2919 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2920 _nfs4_proc_readdir(dentry, cred, cookie,
2921 page, count, plus),
2922 &exception);
2923 } while (exception.retry);
2924 return err;
2925}
2926
2927static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2928 struct iattr *sattr, dev_t rdev)
2929{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002930 struct nfs4_createdata *data;
2931 int mode = sattr->ia_mode;
2932 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933
2934 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2935 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002936
2937 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2938 if (data == NULL)
2939 goto out;
2940
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002942 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002944 data->arg.ftype = NF4BLK;
2945 data->arg.u.device.specdata1 = MAJOR(rdev);
2946 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 }
2948 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002949 data->arg.ftype = NF4CHR;
2950 data->arg.u.device.specdata1 = MAJOR(rdev);
2951 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002954 status = nfs4_do_create(dir, dentry, data);
2955
2956 nfs4_free_createdata(data);
2957out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 return status;
2959}
2960
2961static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2962 struct iattr *sattr, dev_t rdev)
2963{
2964 struct nfs4_exception exception = { };
2965 int err;
2966 do {
2967 err = nfs4_handle_exception(NFS_SERVER(dir),
2968 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2969 &exception);
2970 } while (exception.retry);
2971 return err;
2972}
2973
2974static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2975 struct nfs_fsstat *fsstat)
2976{
2977 struct nfs4_statfs_arg args = {
2978 .fh = fhandle,
2979 .bitmask = server->attr_bitmask,
2980 };
Benny Halevy24ad1482009-04-01 09:21:56 -04002981 struct nfs4_statfs_res res = {
2982 .fsstat = fsstat,
2983 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 struct rpc_message msg = {
2985 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2986 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04002987 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 };
2989
Trond Myklebust0e574af2005-10-27 22:12:38 -04002990 nfs_fattr_init(fsstat->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002991 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992}
2993
2994static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2995{
2996 struct nfs4_exception exception = { };
2997 int err;
2998 do {
2999 err = nfs4_handle_exception(server,
3000 _nfs4_proc_statfs(server, fhandle, fsstat),
3001 &exception);
3002 } while (exception.retry);
3003 return err;
3004}
3005
3006static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3007 struct nfs_fsinfo *fsinfo)
3008{
3009 struct nfs4_fsinfo_arg args = {
3010 .fh = fhandle,
3011 .bitmask = server->attr_bitmask,
3012 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003013 struct nfs4_fsinfo_res res = {
3014 .fsinfo = fsinfo,
3015 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 struct rpc_message msg = {
3017 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3018 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003019 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 };
3021
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003022 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023}
3024
3025static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3026{
3027 struct nfs4_exception exception = { };
3028 int err;
3029
3030 do {
3031 err = nfs4_handle_exception(server,
3032 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3033 &exception);
3034 } while (exception.retry);
3035 return err;
3036}
3037
3038static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3039{
Trond Myklebust0e574af2005-10-27 22:12:38 -04003040 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3042}
3043
3044static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3045 struct nfs_pathconf *pathconf)
3046{
3047 struct nfs4_pathconf_arg args = {
3048 .fh = fhandle,
3049 .bitmask = server->attr_bitmask,
3050 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003051 struct nfs4_pathconf_res res = {
3052 .pathconf = pathconf,
3053 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 struct rpc_message msg = {
3055 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3056 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003057 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 };
3059
3060 /* None of the pathconf attributes are mandatory to implement */
3061 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3062 memset(pathconf, 0, sizeof(*pathconf));
3063 return 0;
3064 }
3065
Trond Myklebust0e574af2005-10-27 22:12:38 -04003066 nfs_fattr_init(pathconf->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003067 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068}
3069
3070static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3071 struct nfs_pathconf *pathconf)
3072{
3073 struct nfs4_exception exception = { };
3074 int err;
3075
3076 do {
3077 err = nfs4_handle_exception(server,
3078 _nfs4_proc_pathconf(server, fhandle, pathconf),
3079 &exception);
3080 } while (exception.retry);
3081 return err;
3082}
3083
Trond Myklebustec06c092006-03-20 13:44:27 -05003084static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085{
Trond Myklebustec06c092006-03-20 13:44:27 -05003086 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087
Andy Adamsonf11c88a2009-04-01 09:22:25 -04003088 dprintk("--> %s\n", __func__);
3089
Andy Adamsonf11c88a2009-04-01 09:22:25 -04003090 nfs4_sequence_done(server, &data->res.seq_res, task->tk_status);
3091
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003092 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003093 nfs_restart_rpc(task, server->nfs_client);
Trond Myklebustec06c092006-03-20 13:44:27 -05003094 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003096
3097 nfs_invalidate_atime(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003099 renew_lease(server, data->timestamp);
3100 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101}
3102
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003103static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 data->timestamp = jiffies;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003106 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107}
3108
Trond Myklebust788e7a82006-03-20 13:44:27 -05003109static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 struct inode *inode = data->inode;
3112
Andy Adamsondef6ed72009-04-01 09:22:26 -04003113 nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
3114 task->tk_status);
3115
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003116 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003117 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003118 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003120 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003122 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003123 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003124 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125}
3126
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003127static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003129 struct nfs_server *server = NFS_SERVER(data->inode);
3130
Trond Myklebusta65318b2009-03-11 14:10:28 -04003131 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003132 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 data->timestamp = jiffies;
3134
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003135 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136}
3137
Trond Myklebust788e7a82006-03-20 13:44:27 -05003138static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 struct inode *inode = data->inode;
3141
Andy Adamson21d9a852009-04-01 09:22:27 -04003142 nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
3143 task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003144 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003145 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003146 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003148 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003149 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150}
3151
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003152static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003154 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155
Trond Myklebusta65318b2009-03-11 14:10:28 -04003156 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003157 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003158 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159}
3160
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003161struct nfs4_renewdata {
3162 struct nfs_client *client;
3163 unsigned long timestamp;
3164};
3165
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166/*
3167 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3168 * standalone procedure for queueing an asynchronous RENEW.
3169 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003170static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003171{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003172 struct nfs4_renewdata *data = calldata;
3173 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003174
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003175 if (atomic_read(&clp->cl_count) > 1)
3176 nfs4_schedule_state_renewal(clp);
3177 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003178 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003179}
3180
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003181static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003183 struct nfs4_renewdata *data = calldata;
3184 struct nfs_client *clp = data->client;
3185 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186
3187 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003188 /* Unless we're shutting down, schedule state recovery! */
3189 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
3190 nfs4_schedule_state_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 return;
3192 }
3193 spin_lock(&clp->cl_lock);
3194 if (time_before(clp->cl_last_renewal,timestamp))
3195 clp->cl_last_renewal = timestamp;
3196 spin_unlock(&clp->cl_lock);
3197}
3198
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003199static const struct rpc_call_ops nfs4_renew_ops = {
3200 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003201 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003202};
3203
David Howellsadfa6f92006-08-22 20:06:08 -04003204int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205{
3206 struct rpc_message msg = {
3207 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3208 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003209 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003211 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003213 if (!atomic_inc_not_zero(&clp->cl_count))
3214 return -EIO;
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003215 data = kmalloc(sizeof(*data), GFP_KERNEL);
3216 if (data == NULL)
3217 return -ENOMEM;
3218 data->client = clp;
3219 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003221 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222}
3223
David Howellsadfa6f92006-08-22 20:06:08 -04003224int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225{
3226 struct rpc_message msg = {
3227 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3228 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003229 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 };
3231 unsigned long now = jiffies;
3232 int status;
3233
3234 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3235 if (status < 0)
3236 return status;
3237 spin_lock(&clp->cl_lock);
3238 if (time_before(clp->cl_last_renewal,now))
3239 clp->cl_last_renewal = now;
3240 spin_unlock(&clp->cl_lock);
3241 return 0;
3242}
3243
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003244static inline int nfs4_server_supports_acls(struct nfs_server *server)
3245{
3246 return (server->caps & NFS_CAP_ACLS)
3247 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3248 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3249}
3250
3251/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3252 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3253 * the stack.
3254 */
3255#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3256
3257static void buf_to_pages(const void *buf, size_t buflen,
3258 struct page **pages, unsigned int *pgbase)
3259{
3260 const void *p = buf;
3261
3262 *pgbase = offset_in_page(buf);
3263 p -= *pgbase;
3264 while (p < buf + buflen) {
3265 *(pages++) = virt_to_page(p);
3266 p += PAGE_CACHE_SIZE;
3267 }
3268}
3269
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003270struct nfs4_cached_acl {
3271 int cached;
3272 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003273 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003274};
3275
3276static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003277{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003278 struct nfs_inode *nfsi = NFS_I(inode);
3279
3280 spin_lock(&inode->i_lock);
3281 kfree(nfsi->nfs4_acl);
3282 nfsi->nfs4_acl = acl;
3283 spin_unlock(&inode->i_lock);
3284}
3285
3286static void nfs4_zap_acl_attr(struct inode *inode)
3287{
3288 nfs4_set_cached_acl(inode, NULL);
3289}
3290
3291static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3292{
3293 struct nfs_inode *nfsi = NFS_I(inode);
3294 struct nfs4_cached_acl *acl;
3295 int ret = -ENOENT;
3296
3297 spin_lock(&inode->i_lock);
3298 acl = nfsi->nfs4_acl;
3299 if (acl == NULL)
3300 goto out;
3301 if (buf == NULL) /* user is just asking for length */
3302 goto out_len;
3303 if (acl->cached == 0)
3304 goto out;
3305 ret = -ERANGE; /* see getxattr(2) man page */
3306 if (acl->len > buflen)
3307 goto out;
3308 memcpy(buf, acl->data, acl->len);
3309out_len:
3310 ret = acl->len;
3311out:
3312 spin_unlock(&inode->i_lock);
3313 return ret;
3314}
3315
3316static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3317{
3318 struct nfs4_cached_acl *acl;
3319
3320 if (buf && acl_len <= PAGE_SIZE) {
3321 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3322 if (acl == NULL)
3323 goto out;
3324 acl->cached = 1;
3325 memcpy(acl->data, buf, acl_len);
3326 } else {
3327 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3328 if (acl == NULL)
3329 goto out;
3330 acl->cached = 0;
3331 }
3332 acl->len = acl_len;
3333out:
3334 nfs4_set_cached_acl(inode, acl);
3335}
3336
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003337static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003338{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003339 struct page *pages[NFS4ACL_MAXPAGES];
3340 struct nfs_getaclargs args = {
3341 .fh = NFS_FH(inode),
3342 .acl_pages = pages,
3343 .acl_len = buflen,
3344 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003345 struct nfs_getaclres res = {
3346 .acl_len = buflen,
3347 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003348 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003349 struct rpc_message msg = {
3350 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3351 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003352 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003353 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003354 struct page *localpage = NULL;
3355 int ret;
3356
3357 if (buflen < PAGE_SIZE) {
3358 /* As long as we're doing a round trip to the server anyway,
3359 * let's be prepared for a page of acl data. */
3360 localpage = alloc_page(GFP_KERNEL);
3361 resp_buf = page_address(localpage);
3362 if (localpage == NULL)
3363 return -ENOMEM;
3364 args.acl_pages[0] = localpage;
3365 args.acl_pgbase = 0;
Benny Halevy663c79b2009-04-01 09:21:59 -04003366 args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003367 } else {
3368 resp_buf = buf;
3369 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3370 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003371 ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003372 if (ret)
3373 goto out_free;
Benny Halevy663c79b2009-04-01 09:21:59 -04003374 if (res.acl_len > args.acl_len)
3375 nfs4_write_cached_acl(inode, NULL, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003376 else
Benny Halevy663c79b2009-04-01 09:21:59 -04003377 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003378 if (buf) {
3379 ret = -ERANGE;
Benny Halevy663c79b2009-04-01 09:21:59 -04003380 if (res.acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003381 goto out_free;
3382 if (localpage)
Benny Halevy663c79b2009-04-01 09:21:59 -04003383 memcpy(buf, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003384 }
Benny Halevy663c79b2009-04-01 09:21:59 -04003385 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003386out_free:
3387 if (localpage)
3388 __free_page(localpage);
3389 return ret;
3390}
3391
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003392static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3393{
3394 struct nfs4_exception exception = { };
3395 ssize_t ret;
3396 do {
3397 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3398 if (ret >= 0)
3399 break;
3400 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3401 } while (exception.retry);
3402 return ret;
3403}
3404
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003405static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3406{
3407 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003408 int ret;
3409
3410 if (!nfs4_server_supports_acls(server))
3411 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003412 ret = nfs_revalidate_inode(server, inode);
3413 if (ret < 0)
3414 return ret;
3415 ret = nfs4_read_cached_acl(inode, buf, buflen);
3416 if (ret != -ENOENT)
3417 return ret;
3418 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003419}
3420
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003421static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003422{
3423 struct nfs_server *server = NFS_SERVER(inode);
3424 struct page *pages[NFS4ACL_MAXPAGES];
3425 struct nfs_setaclargs arg = {
3426 .fh = NFS_FH(inode),
3427 .acl_pages = pages,
3428 .acl_len = buflen,
3429 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003430 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003431 struct rpc_message msg = {
3432 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3433 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003434 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003435 };
3436 int ret;
3437
3438 if (!nfs4_server_supports_acls(server))
3439 return -EOPNOTSUPP;
Trond Myklebust642ac542005-10-18 14:20:19 -07003440 nfs_inode_return_delegation(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003441 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003442 ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003443 nfs_access_zap_cache(inode);
3444 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003445 return ret;
3446}
3447
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003448static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3449{
3450 struct nfs4_exception exception = { };
3451 int err;
3452 do {
3453 err = nfs4_handle_exception(NFS_SERVER(inode),
3454 __nfs4_proc_set_acl(inode, buf, buflen),
3455 &exception);
3456 } while (exception.retry);
3457 return err;
3458}
3459
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003461nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003463 struct nfs_client *clp = server->nfs_client;
3464
3465 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 return 0;
3467 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003468 case -NFS4ERR_ADMIN_REVOKED:
3469 case -NFS4ERR_BAD_STATEID:
3470 case -NFS4ERR_OPENMODE:
3471 if (state == NULL)
3472 break;
3473 nfs4_state_mark_reclaim_nograce(clp, state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003474 goto do_state_recovery;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003476 if (state == NULL)
3477 break;
3478 nfs4_state_mark_reclaim_reboot(clp, state);
3479 case -NFS4ERR_STALE_CLIENTID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 case -NFS4ERR_EXPIRED:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003481 goto do_state_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003482#if defined(CONFIG_NFS_V4_1)
3483 case -NFS4ERR_BADSESSION:
3484 case -NFS4ERR_BADSLOT:
3485 case -NFS4ERR_BAD_HIGH_SLOT:
3486 case -NFS4ERR_DEADSESSION:
3487 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3488 case -NFS4ERR_SEQ_FALSE_RETRY:
3489 case -NFS4ERR_SEQ_MISORDERED:
3490 dprintk("%s ERROR %d, Reset session\n", __func__,
3491 task->tk_status);
Andy Adamson0b9e2d42009-12-04 16:02:14 -05003492 nfs4_schedule_state_recovery(clp);
Andy Adamson4745e312009-04-01 09:22:42 -04003493 task->tk_status = 0;
3494 return -EAGAIN;
3495#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003497 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003498 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003499 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3501 task->tk_status = 0;
3502 return -EAGAIN;
3503 case -NFS4ERR_OLD_STATEID:
3504 task->tk_status = 0;
3505 return -EAGAIN;
3506 }
3507 task->tk_status = nfs4_map_errors(task->tk_status);
3508 return 0;
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003509do_state_recovery:
3510 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
3511 nfs4_schedule_state_recovery(clp);
3512 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3513 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3514 task->tk_status = 0;
3515 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516}
3517
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003518int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3519 unsigned short port, struct rpc_cred *cred,
3520 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521{
3522 nfs4_verifier sc_verifier;
3523 struct nfs4_setclientid setclientid = {
3524 .sc_verifier = &sc_verifier,
3525 .sc_prog = program,
3526 };
3527 struct rpc_message msg = {
3528 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3529 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003530 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003531 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 };
Al Virobc4785c2006-10-19 23:28:51 -07003533 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534 int loop = 0;
3535 int status;
3536
Al Virobc4785c2006-10-19 23:28:51 -07003537 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3539 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3540
3541 for(;;) {
3542 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003543 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003544 clp->cl_ipaddr,
3545 rpc_peeraddr2str(clp->cl_rpcclient,
3546 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003547 rpc_peeraddr2str(clp->cl_rpcclient,
3548 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003549 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550 clp->cl_id_uniquifier);
3551 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003552 sizeof(setclientid.sc_netid),
3553 rpc_peeraddr2str(clp->cl_rpcclient,
3554 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003556 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 clp->cl_ipaddr, port >> 8, port & 255);
3558
3559 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3560 if (status != -NFS4ERR_CLID_INUSE)
3561 break;
3562 if (signalled())
3563 break;
3564 if (loop++ & 1)
3565 ssleep(clp->cl_lease_time + 1);
3566 else
3567 if (++clp->cl_id_uniquifier == 0)
3568 break;
3569 }
3570 return status;
3571}
3572
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003573static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3574 struct nfs4_setclientid_res *arg,
3575 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576{
3577 struct nfs_fsinfo fsinfo;
3578 struct rpc_message msg = {
3579 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003580 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003582 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583 };
3584 unsigned long now;
3585 int status;
3586
3587 now = jiffies;
3588 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3589 if (status == 0) {
3590 spin_lock(&clp->cl_lock);
3591 clp->cl_lease_time = fsinfo.lease_time * HZ;
3592 clp->cl_last_renewal = now;
3593 spin_unlock(&clp->cl_lock);
3594 }
3595 return status;
3596}
3597
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003598int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3599 struct nfs4_setclientid_res *arg,
3600 struct rpc_cred *cred)
Trond Myklebust51581f32006-03-20 13:44:47 -05003601{
Benny Halevy77e03672008-06-24 20:25:57 +03003602 long timeout = 0;
Trond Myklebust51581f32006-03-20 13:44:47 -05003603 int err;
3604 do {
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003605 err = _nfs4_proc_setclientid_confirm(clp, arg, cred);
Trond Myklebust51581f32006-03-20 13:44:47 -05003606 switch (err) {
3607 case 0:
3608 return err;
3609 case -NFS4ERR_RESOURCE:
3610 /* The IBM lawyers misread another document! */
3611 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -05003612 case -EKEYEXPIRED:
Trond Myklebust51581f32006-03-20 13:44:47 -05003613 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3614 }
3615 } while (err == 0);
3616 return err;
3617}
3618
Trond Myklebustfe650402006-01-03 09:55:18 +01003619struct nfs4_delegreturndata {
3620 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003621 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003622 struct nfs_fh fh;
3623 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003624 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003625 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003626 int rpc_status;
3627};
3628
Trond Myklebustfe650402006-01-03 09:55:18 +01003629static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3630{
3631 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003632
Trond Myklebustd61e6122009-12-05 19:32:19 -05003633 nfs4_sequence_done(data->res.server, &data->res.seq_res,
3634 task->tk_status);
Andy Adamson938e1012009-04-01 09:22:28 -04003635
Ricardo Labiaga79708862009-12-07 09:23:21 -05003636 switch (task->tk_status) {
3637 case -NFS4ERR_STALE_STATEID:
3638 case -NFS4ERR_EXPIRED:
3639 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01003640 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003641 break;
3642 default:
3643 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3644 -EAGAIN) {
3645 nfs_restart_rpc(task, data->res.server->nfs_client);
3646 return;
3647 }
3648 }
3649 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01003650}
3651
3652static void nfs4_delegreturn_release(void *calldata)
3653{
Trond Myklebustfe650402006-01-03 09:55:18 +01003654 kfree(calldata);
3655}
3656
Andy Adamson938e1012009-04-01 09:22:28 -04003657#if defined(CONFIG_NFS_V4_1)
3658static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3659{
3660 struct nfs4_delegreturndata *d_data;
3661
3662 d_data = (struct nfs4_delegreturndata *)data;
3663
Trond Myklebust035168a2010-06-16 09:52:26 -04003664 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04003665 &d_data->args.seq_args,
3666 &d_data->res.seq_res, 1, task))
3667 return;
3668 rpc_call_start(task);
3669}
3670#endif /* CONFIG_NFS_V4_1 */
3671
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003672static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04003673#if defined(CONFIG_NFS_V4_1)
3674 .rpc_call_prepare = nfs4_delegreturn_prepare,
3675#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01003676 .rpc_call_done = nfs4_delegreturn_done,
3677 .rpc_release = nfs4_delegreturn_release,
3678};
3679
Trond Myklebuste6f81072008-01-24 18:14:34 -05003680static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003681{
3682 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003683 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003684 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003685 struct rpc_message msg = {
3686 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3687 .rpc_cred = cred,
3688 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003689 struct rpc_task_setup task_setup_data = {
3690 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003691 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003692 .callback_ops = &nfs4_delegreturn_ops,
3693 .flags = RPC_TASK_ASYNC,
3694 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003695 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003696
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003697 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01003698 if (data == NULL)
3699 return -ENOMEM;
3700 data->args.fhandle = &data->fh;
3701 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003702 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003703 nfs_copy_fh(&data->fh, NFS_FH(inode));
3704 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003705 data->res.fattr = &data->fattr;
3706 data->res.server = server;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003707 data->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003708 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003709 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003710 data->rpc_status = 0;
3711
Trond Myklebustc970aa82007-07-14 15:39:59 -04003712 task_setup_data.callback_data = data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003713 msg.rpc_argp = &data->args,
3714 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003715 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003716 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003717 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003718 if (!issync)
3719 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003720 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003721 if (status != 0)
3722 goto out;
3723 status = data->rpc_status;
3724 if (status != 0)
3725 goto out;
3726 nfs_refresh_inode(inode, &data->fattr);
3727out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003728 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003729 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730}
3731
Trond Myklebuste6f81072008-01-24 18:14:34 -05003732int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733{
3734 struct nfs_server *server = NFS_SERVER(inode);
3735 struct nfs4_exception exception = { };
3736 int err;
3737 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003738 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739 switch (err) {
3740 case -NFS4ERR_STALE_STATEID:
3741 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742 case 0:
3743 return 0;
3744 }
3745 err = nfs4_handle_exception(server, err, &exception);
3746 } while (exception.retry);
3747 return err;
3748}
3749
3750#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3751#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3752
3753/*
3754 * sleep, with exponential backoff, and retry the LOCK operation.
3755 */
3756static unsigned long
3757nfs4_set_lock_task_retry(unsigned long timeout)
3758{
Matthew Wilcox150030b2007-12-06 16:24:39 -05003759 schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 timeout <<= 1;
3761 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3762 return NFS4_LOCK_MAXTIMEOUT;
3763 return timeout;
3764}
3765
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3767{
3768 struct inode *inode = state->inode;
3769 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003770 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003771 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003773 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003775 struct nfs_lockt_res res = {
3776 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777 };
3778 struct rpc_message msg = {
3779 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3780 .rpc_argp = &arg,
3781 .rpc_resp = &res,
3782 .rpc_cred = state->owner->so_cred,
3783 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 struct nfs4_lock_state *lsp;
3785 int status;
3786
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003787 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003788 status = nfs4_set_lock_state(state, request);
3789 if (status != 0)
3790 goto out;
3791 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003792 arg.lock_owner.id = lsp->ls_id.id;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003793 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003794 switch (status) {
3795 case 0:
3796 request->fl_type = F_UNLCK;
3797 break;
3798 case -NFS4ERR_DENIED:
3799 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05003801 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003802out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 return status;
3804}
3805
3806static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3807{
3808 struct nfs4_exception exception = { };
3809 int err;
3810
3811 do {
3812 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3813 _nfs4_proc_getlk(state, cmd, request),
3814 &exception);
3815 } while (exception.retry);
3816 return err;
3817}
3818
3819static int do_vfs_lock(struct file *file, struct file_lock *fl)
3820{
3821 int res = 0;
3822 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3823 case FL_POSIX:
3824 res = posix_lock_file_wait(file, fl);
3825 break;
3826 case FL_FLOCK:
3827 res = flock_lock_file_wait(file, fl);
3828 break;
3829 default:
3830 BUG();
3831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832 return res;
3833}
3834
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003835struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003836 struct nfs_locku_args arg;
3837 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003838 struct nfs4_lock_state *lsp;
3839 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003840 struct file_lock fl;
3841 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003842 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003843};
3844
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003845static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3846 struct nfs_open_context *ctx,
3847 struct nfs4_lock_state *lsp,
3848 struct nfs_seqid *seqid)
3849{
3850 struct nfs4_unlockdata *p;
3851 struct inode *inode = lsp->ls_state->inode;
3852
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003853 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003854 if (p == NULL)
3855 return NULL;
3856 p->arg.fh = NFS_FH(inode);
3857 p->arg.fl = &p->fl;
3858 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003859 p->res.seqid = seqid;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003860 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003861 p->arg.stateid = &lsp->ls_stateid;
3862 p->lsp = lsp;
3863 atomic_inc(&lsp->ls_count);
3864 /* Ensure we don't close file until we're done freeing locks! */
3865 p->ctx = get_nfs_open_context(ctx);
3866 memcpy(&p->fl, fl, sizeof(p->fl));
3867 p->server = NFS_SERVER(inode);
3868 return p;
3869}
3870
Trond Myklebust06f814a2006-01-03 09:55:07 +01003871static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003872{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003873 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003874 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003875 nfs4_put_lock_state(calldata->lsp);
3876 put_nfs_open_context(calldata->ctx);
3877 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003878}
3879
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003880static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003881{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003882 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003883
Andy Adamsona8936932009-04-01 09:22:23 -04003884 nfs4_sequence_done(calldata->server, &calldata->res.seq_res,
3885 task->tk_status);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003886 if (RPC_ASSASSINATED(task))
3887 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003888 switch (task->tk_status) {
3889 case 0:
3890 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003891 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003892 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01003893 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003894 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003895 case -NFS4ERR_BAD_STATEID:
3896 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003897 case -NFS4ERR_STALE_STATEID:
3898 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003899 break;
3900 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003901 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebust0110ee12009-12-07 09:00:24 -05003902 nfs_restart_rpc(task,
Trond Myklebustd61e6122009-12-05 19:32:19 -05003903 calldata->server->nfs_client);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003904 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003905}
3906
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003907static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003908{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003909 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003911 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003912 return;
3913 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003914 /* Note: exit _without_ running nfs4_locku_done */
3915 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003916 return;
3917 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01003918 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04003919 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04003920 &calldata->arg.seq_args,
3921 &calldata->res.seq_res, 1, task))
3922 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003923 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924}
3925
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003926static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003927 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003928 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01003929 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003930};
3931
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003932static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3933 struct nfs_open_context *ctx,
3934 struct nfs4_lock_state *lsp,
3935 struct nfs_seqid *seqid)
3936{
3937 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003938 struct rpc_message msg = {
3939 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3940 .rpc_cred = ctx->cred,
3941 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003942 struct rpc_task_setup task_setup_data = {
3943 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04003944 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003945 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003946 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003947 .flags = RPC_TASK_ASYNC,
3948 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003949
Frank Filz137d6ac2007-07-09 15:32:29 -07003950 /* Ensure this is an unlock - when canceling a lock, the
3951 * canceled lock is passed in, and it won't be an unlock.
3952 */
3953 fl->fl_type = F_UNLCK;
3954
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003955 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3956 if (data == NULL) {
3957 nfs_free_seqid(seqid);
3958 return ERR_PTR(-ENOMEM);
3959 }
3960
Trond Myklebust5138fde2007-07-14 15:40:01 -04003961 msg.rpc_argp = &data->arg,
3962 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003963 task_setup_data.callback_data = data;
3964 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003965}
3966
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3968{
Trond Myklebust19e03c52008-12-23 15:21:44 -05003969 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003970 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003971 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01003972 struct rpc_task *task;
3973 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003974 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975
Trond Myklebust9b073572006-06-29 16:38:34 -04003976 status = nfs4_set_lock_state(state, request);
3977 /* Unlock _before_ we do the RPC call */
3978 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003979 down_read(&nfsi->rwsem);
3980 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
3981 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003982 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003983 }
3984 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003985 if (status != 0)
3986 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003987 /* Is this a delegated lock? */
3988 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04003989 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003990 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003991 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04003992 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003993 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04003994 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04003995 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003996 status = PTR_ERR(task);
3997 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04003998 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003999 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004000 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004001out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004002 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004003 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004}
4005
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004006struct nfs4_lockdata {
4007 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004008 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004009 struct nfs4_lock_state *lsp;
4010 struct nfs_open_context *ctx;
4011 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004012 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004013 int rpc_status;
4014 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004015 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004016};
4017
4018static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004019 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4020 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004021{
4022 struct nfs4_lockdata *p;
4023 struct inode *inode = lsp->ls_state->inode;
4024 struct nfs_server *server = NFS_SERVER(inode);
4025
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004026 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004027 if (p == NULL)
4028 return NULL;
4029
4030 p->arg.fh = NFS_FH(inode);
4031 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004032 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004033 if (p->arg.open_seqid == NULL)
4034 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004035 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004036 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004037 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004038 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004039 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04004040 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004041 p->res.lock_seqid = p->arg.lock_seqid;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04004042 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004043 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004044 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004045 atomic_inc(&lsp->ls_count);
4046 p->ctx = get_nfs_open_context(ctx);
4047 memcpy(&p->fl, fl, sizeof(p->fl));
4048 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004049out_free_seqid:
4050 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004051out_free:
4052 kfree(p);
4053 return NULL;
4054}
4055
4056static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4057{
4058 struct nfs4_lockdata *data = calldata;
4059 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060
Harvey Harrison3110ff82008-05-02 13:42:44 -07004061 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004062 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4063 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004064 /* Do we need to do an open_to_lock_owner? */
4065 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004066 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4067 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004068 data->arg.open_stateid = &state->stateid;
4069 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004070 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004071 } else
4072 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004073 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004074 if (nfs4_setup_sequence(data->server,
4075 &data->arg.seq_args,
Andy Adamson66179ef2009-04-01 09:22:22 -04004076 &data->res.seq_res, 1, task))
4077 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004078 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004079 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004080}
4081
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004082static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4083{
4084 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4085 nfs4_lock_prepare(task, calldata);
4086}
4087
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004088static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4089{
4090 struct nfs4_lockdata *data = calldata;
4091
Harvey Harrison3110ff82008-05-02 13:42:44 -07004092 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004093
Trond Myklebustd61e6122009-12-05 19:32:19 -05004094 nfs4_sequence_done(data->server, &data->res.seq_res,
4095 task->tk_status);
Andy Adamson66179ef2009-04-01 09:22:22 -04004096
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004097 data->rpc_status = task->tk_status;
4098 if (RPC_ASSASSINATED(task))
4099 goto out;
4100 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004101 if (data->rpc_status == 0)
4102 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4103 else
4104 goto out;
4105 }
4106 if (data->rpc_status == 0) {
4107 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4108 sizeof(data->lsp->ls_stateid.data));
4109 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Trond Myklebust88be9f92007-06-05 10:42:27 -04004110 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004111 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004112out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004113 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004114}
4115
4116static void nfs4_lock_release(void *calldata)
4117{
4118 struct nfs4_lockdata *data = calldata;
4119
Harvey Harrison3110ff82008-05-02 13:42:44 -07004120 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004121 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004122 if (data->cancelled != 0) {
4123 struct rpc_task *task;
4124 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4125 data->arg.lock_seqid);
4126 if (!IS_ERR(task))
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004127 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004128 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004129 } else
4130 nfs_free_seqid(data->arg.lock_seqid);
4131 nfs4_put_lock_state(data->lsp);
4132 put_nfs_open_context(data->ctx);
4133 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004134 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004135}
4136
4137static const struct rpc_call_ops nfs4_lock_ops = {
4138 .rpc_call_prepare = nfs4_lock_prepare,
4139 .rpc_call_done = nfs4_lock_done,
4140 .rpc_release = nfs4_lock_release,
4141};
4142
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004143static const struct rpc_call_ops nfs4_recover_lock_ops = {
4144 .rpc_call_prepare = nfs4_recover_lock_prepare,
4145 .rpc_call_done = nfs4_lock_done,
4146 .rpc_release = nfs4_lock_release,
4147};
4148
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004149static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4150{
4151 struct nfs_client *clp = server->nfs_client;
4152 struct nfs4_state *state = lsp->ls_state;
4153
4154 switch (error) {
4155 case -NFS4ERR_ADMIN_REVOKED:
4156 case -NFS4ERR_BAD_STATEID:
4157 case -NFS4ERR_EXPIRED:
4158 if (new_lock_owner != 0 ||
4159 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4160 nfs4_state_mark_reclaim_nograce(clp, state);
4161 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004162 break;
4163 case -NFS4ERR_STALE_STATEID:
4164 if (new_lock_owner != 0 ||
4165 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4166 nfs4_state_mark_reclaim_reboot(clp, state);
4167 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004168 };
4169}
4170
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004171static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004172{
4173 struct nfs4_lockdata *data;
4174 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004175 struct rpc_message msg = {
4176 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4177 .rpc_cred = state->owner->so_cred,
4178 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004179 struct rpc_task_setup task_setup_data = {
4180 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004181 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004182 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004183 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004184 .flags = RPC_TASK_ASYNC,
4185 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004186 int ret;
4187
Harvey Harrison3110ff82008-05-02 13:42:44 -07004188 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004189 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004190 fl->fl_u.nfs4_fl.owner,
4191 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004192 if (data == NULL)
4193 return -ENOMEM;
4194 if (IS_SETLKW(cmd))
4195 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004196 if (recovery_type > NFS_LOCK_NEW) {
4197 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004198 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004199 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4200 }
Trond Myklebust5138fde2007-07-14 15:40:01 -04004201 msg.rpc_argp = &data->arg,
4202 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004203 task_setup_data.callback_data = data;
4204 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004205 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004206 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004207 ret = nfs4_wait_for_completion_rpc_task(task);
4208 if (ret == 0) {
4209 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004210 if (ret)
4211 nfs4_handle_setlk_error(data->server, data->lsp,
4212 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004213 } else
4214 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004215 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004216 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004217 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218}
4219
4220static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4221{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004222 struct nfs_server *server = NFS_SERVER(state->inode);
4223 struct nfs4_exception exception = { };
4224 int err;
4225
4226 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004227 /* Cache the lock if possible... */
4228 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4229 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004230 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Jeff Layton2c643482010-01-07 09:42:03 -05004231 if (err != -NFS4ERR_DELAY && err != -EKEYEXPIRED)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004232 break;
4233 nfs4_handle_exception(server, err, &exception);
4234 } while (exception.retry);
4235 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236}
4237
4238static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4239{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004240 struct nfs_server *server = NFS_SERVER(state->inode);
4241 struct nfs4_exception exception = { };
4242 int err;
4243
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004244 err = nfs4_set_lock_state(state, request);
4245 if (err != 0)
4246 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004247 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004248 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4249 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004250 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004251 switch (err) {
4252 default:
4253 goto out;
4254 case -NFS4ERR_GRACE:
4255 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -05004256 case -EKEYEXPIRED:
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004257 nfs4_handle_exception(server, err, &exception);
4258 err = 0;
4259 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004260 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004261out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004262 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263}
4264
4265static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4266{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004267 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004268 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004269 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004271 if ((fl_flags & FL_POSIX) &&
4272 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4273 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004274 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004275 status = nfs4_set_lock_state(state, request);
4276 if (status != 0)
4277 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004278 request->fl_flags |= FL_ACCESS;
4279 status = do_vfs_lock(request->fl_file, request);
4280 if (status < 0)
4281 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004282 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004283 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004284 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004285 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004286 request->fl_flags = fl_flags & ~FL_SLEEP;
4287 status = do_vfs_lock(request->fl_file, request);
4288 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004289 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004290 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004291 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004292 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004293 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004294 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004295 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07004296 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004297out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004298 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004299out:
4300 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301 return status;
4302}
4303
4304static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4305{
4306 struct nfs4_exception exception = { };
4307 int err;
4308
4309 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004310 err = _nfs4_proc_setlk(state, cmd, request);
4311 if (err == -NFS4ERR_DENIED)
4312 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004314 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 } while (exception.retry);
4316 return err;
4317}
4318
4319static int
4320nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4321{
4322 struct nfs_open_context *ctx;
4323 struct nfs4_state *state;
4324 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4325 int status;
4326
4327 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004328 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 state = ctx->state;
4330
4331 if (request->fl_start < 0 || request->fl_end < 0)
4332 return -EINVAL;
4333
Trond Myklebustd9531262009-07-21 19:22:38 -04004334 if (IS_GETLK(cmd)) {
4335 if (state != NULL)
4336 return nfs4_proc_getlk(state, F_GETLK, request);
4337 return 0;
4338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339
4340 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4341 return -EINVAL;
4342
Trond Myklebustd9531262009-07-21 19:22:38 -04004343 if (request->fl_type == F_UNLCK) {
4344 if (state != NULL)
4345 return nfs4_proc_unlck(state, cmd, request);
4346 return 0;
4347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348
Trond Myklebustd9531262009-07-21 19:22:38 -04004349 if (state == NULL)
4350 return -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 do {
4352 status = nfs4_proc_setlk(state, cmd, request);
4353 if ((status != -EAGAIN) || IS_SETLK(cmd))
4354 break;
4355 timeout = nfs4_set_lock_task_retry(timeout);
4356 status = -ERESTARTSYS;
4357 if (signalled())
4358 break;
4359 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360 return status;
4361}
4362
Trond Myklebust888e6942005-11-04 15:38:11 -05004363int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4364{
4365 struct nfs_server *server = NFS_SERVER(state->inode);
4366 struct nfs4_exception exception = { };
4367 int err;
4368
4369 err = nfs4_set_lock_state(state, fl);
4370 if (err != 0)
4371 goto out;
4372 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004373 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004374 switch (err) {
4375 default:
4376 printk(KERN_ERR "%s: unhandled error %d.\n",
4377 __func__, err);
4378 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004379 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004380 goto out;
4381 case -NFS4ERR_EXPIRED:
4382 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004383 case -NFS4ERR_STALE_STATEID:
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004384 case -NFS4ERR_BADSESSION:
4385 case -NFS4ERR_BADSLOT:
4386 case -NFS4ERR_BAD_HIGH_SLOT:
4387 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4388 case -NFS4ERR_DEADSESSION:
Trond Myklebustd5122202009-06-17 13:22:58 -07004389 nfs4_schedule_state_recovery(server->nfs_client);
4390 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004391 case -ERESTARTSYS:
4392 /*
4393 * The show must go on: exit, but mark the
4394 * stateid as needing recovery.
4395 */
4396 case -NFS4ERR_ADMIN_REVOKED:
4397 case -NFS4ERR_BAD_STATEID:
4398 case -NFS4ERR_OPENMODE:
4399 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
4400 err = 0;
4401 goto out;
4402 case -ENOMEM:
4403 case -NFS4ERR_DENIED:
4404 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4405 err = 0;
4406 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004407 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -05004408 case -EKEYEXPIRED:
Trond Myklebustd5122202009-06-17 13:22:58 -07004409 break;
4410 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004411 err = nfs4_handle_exception(server, err, &exception);
4412 } while (exception.retry);
4413out:
4414 return err;
4415}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004416
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004417static void nfs4_release_lockowner_release(void *calldata)
4418{
4419 kfree(calldata);
4420}
4421
4422const struct rpc_call_ops nfs4_release_lockowner_ops = {
4423 .rpc_release = nfs4_release_lockowner_release,
4424};
4425
4426void nfs4_release_lockowner(const struct nfs4_lock_state *lsp)
4427{
4428 struct nfs_server *server = lsp->ls_state->owner->so_server;
4429 struct nfs_release_lockowner_args *args;
4430 struct rpc_message msg = {
4431 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4432 };
4433
4434 if (server->nfs_client->cl_mvops->minor_version != 0)
4435 return;
4436 args = kmalloc(sizeof(*args), GFP_NOFS);
4437 if (!args)
4438 return;
4439 args->lock_owner.clientid = server->nfs_client->cl_clientid;
4440 args->lock_owner.id = lsp->ls_id.id;
4441 msg.rpc_argp = args;
4442 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, args);
4443}
4444
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004445#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4446
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004447int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
4448 size_t buflen, int flags)
4449{
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004450 struct inode *inode = dentry->d_inode;
4451
4452 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4453 return -EOPNOTSUPP;
4454
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004455 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004456}
4457
4458/* The getxattr man page suggests returning -ENODATA for unknown attributes,
4459 * and that's what we'll do for e.g. user attributes that haven't been set.
4460 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
4461 * attributes in kernel-managed attribute namespaces. */
4462ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
4463 size_t buflen)
4464{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004465 struct inode *inode = dentry->d_inode;
4466
4467 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4468 return -EOPNOTSUPP;
4469
4470 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004471}
4472
4473ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
4474{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004475 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004476
J. Bruce Fields096455a2006-03-20 23:23:42 -05004477 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4478 return 0;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004479 if (buf && buflen < len)
4480 return -ERANGE;
4481 if (buf)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004482 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
4483 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004484}
4485
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004486static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4487{
4488 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
4489 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4490 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4491 return;
4492
4493 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4494 NFS_ATTR_FATTR_NLINK;
4495 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4496 fattr->nlink = 2;
4497}
4498
Trond Myklebust56659e92007-07-17 21:52:39 -04004499int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004500 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004501{
4502 struct nfs_server *server = NFS_SERVER(dir);
4503 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004504 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4505 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004506 };
4507 struct nfs4_fs_locations_arg args = {
4508 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004509 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004510 .page = page,
4511 .bitmask = bitmask,
4512 };
Benny Halevy22958462009-04-01 09:22:02 -04004513 struct nfs4_fs_locations_res res = {
4514 .fs_locations = fs_locations,
4515 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004516 struct rpc_message msg = {
4517 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4518 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004519 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004520 };
4521 int status;
4522
Harvey Harrison3110ff82008-05-02 13:42:44 -07004523 dprintk("%s: start\n", __func__);
Trond Myklebustc228fd32007-01-13 02:28:11 -05004524 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004525 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004526 fs_locations->nlocations = 0;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04004527 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004528 nfs_fixup_referral_attributes(&fs_locations->fattr);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004529 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004530 return status;
4531}
4532
Andy Adamson557134a2009-04-01 09:21:53 -04004533#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04004534/*
4535 * nfs4_proc_exchange_id()
4536 *
4537 * Since the clientid has expired, all compounds using sessions
4538 * associated with the stale clientid will be returning
4539 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4540 * be in some phase of session reset.
4541 */
Andy Adamson4d643d12009-12-04 15:52:24 -05004542int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004543{
4544 nfs4_verifier verifier;
4545 struct nfs41_exchange_id_args args = {
4546 .client = clp,
4547 .flags = clp->cl_exchange_flags,
4548 };
4549 struct nfs41_exchange_id_res res = {
4550 .client = clp,
4551 };
4552 int status;
4553 struct rpc_message msg = {
4554 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4555 .rpc_argp = &args,
4556 .rpc_resp = &res,
4557 .rpc_cred = cred,
4558 };
4559 __be32 *p;
4560
4561 dprintk("--> %s\n", __func__);
4562 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04004563
Alexandros Batsakis7b183d02009-12-05 13:33:25 -05004564 /* Remove server-only flags */
4565 args.flags &= ~EXCHGID4_FLAG_CONFIRMED_R;
4566
Benny Halevy99fe60d2009-04-01 09:22:29 -04004567 p = (u32 *)verifier.data;
4568 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4569 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4570 args.verifier = &verifier;
4571
4572 while (1) {
4573 args.id_len = scnprintf(args.id, sizeof(args.id),
4574 "%s/%s %u",
4575 clp->cl_ipaddr,
4576 rpc_peeraddr2str(clp->cl_rpcclient,
4577 RPC_DISPLAY_ADDR),
4578 clp->cl_id_uniquifier);
4579
4580 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
4581
Andy Adamson180b62a2010-03-02 13:19:36 -05004582 if (status != -NFS4ERR_CLID_INUSE)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004583 break;
4584
4585 if (signalled())
4586 break;
4587
4588 if (++clp->cl_id_uniquifier == 0)
4589 break;
4590 }
4591
4592 dprintk("<-- %s status= %d\n", __func__, status);
4593 return status;
4594}
4595
Andy Adamson2050f0c2009-04-01 09:22:30 -04004596struct nfs4_get_lease_time_data {
4597 struct nfs4_get_lease_time_args *args;
4598 struct nfs4_get_lease_time_res *res;
4599 struct nfs_client *clp;
4600};
4601
4602static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4603 void *calldata)
4604{
4605 int ret;
4606 struct nfs4_get_lease_time_data *data =
4607 (struct nfs4_get_lease_time_data *)calldata;
4608
4609 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004610 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004611 /* just setup sequence, do not trigger session recovery
4612 since we're invoked within one */
4613 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004614 &data->args->la_seq_args,
4615 &data->res->lr_seq_res, 0, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004616
4617 BUG_ON(ret == -EAGAIN);
4618 rpc_call_start(task);
4619 dprintk("<-- %s\n", __func__);
4620}
4621
4622/*
4623 * Called from nfs4_state_manager thread for session setup, so don't recover
4624 * from sequence operation or clientid errors.
4625 */
4626static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4627{
4628 struct nfs4_get_lease_time_data *data =
4629 (struct nfs4_get_lease_time_data *)calldata;
4630
4631 dprintk("--> %s\n", __func__);
Trond Myklebustd185a332010-06-16 09:52:25 -04004632 nfs41_sequence_done(&data->res->lr_seq_res);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004633 switch (task->tk_status) {
4634 case -NFS4ERR_DELAY:
4635 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05004636 case -EKEYEXPIRED:
Andy Adamson2050f0c2009-04-01 09:22:30 -04004637 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4638 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4639 task->tk_status = 0;
Trond Myklebust0110ee12009-12-07 09:00:24 -05004640 nfs_restart_rpc(task, data->clp);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004641 return;
4642 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04004643 dprintk("<-- %s\n", __func__);
4644}
4645
4646struct rpc_call_ops nfs4_get_lease_time_ops = {
4647 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4648 .rpc_call_done = nfs4_get_lease_time_done,
4649};
4650
4651int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4652{
4653 struct rpc_task *task;
4654 struct nfs4_get_lease_time_args args;
4655 struct nfs4_get_lease_time_res res = {
4656 .lr_fsinfo = fsinfo,
4657 };
4658 struct nfs4_get_lease_time_data data = {
4659 .args = &args,
4660 .res = &res,
4661 .clp = clp,
4662 };
4663 struct rpc_message msg = {
4664 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4665 .rpc_argp = &args,
4666 .rpc_resp = &res,
4667 };
4668 struct rpc_task_setup task_setup = {
4669 .rpc_client = clp->cl_rpcclient,
4670 .rpc_message = &msg,
4671 .callback_ops = &nfs4_get_lease_time_ops,
4672 .callback_data = &data
4673 };
4674 int status;
4675
4676 res.lr_seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
4677 dprintk("--> %s\n", __func__);
4678 task = rpc_run_task(&task_setup);
4679
4680 if (IS_ERR(task))
4681 status = PTR_ERR(task);
4682 else {
4683 status = task->tk_status;
4684 rpc_put_task(task);
4685 }
4686 dprintk("<-- %s return %d\n", __func__, status);
4687
4688 return status;
4689}
4690
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004691/*
4692 * Reset a slot table
4693 */
Andy Adamson104aeba2010-01-14 17:45:10 -05004694static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
4695 int ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004696{
Andy Adamson104aeba2010-01-14 17:45:10 -05004697 struct nfs4_slot *new = NULL;
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004698 int i;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004699 int ret = 0;
4700
Andy Adamson104aeba2010-01-14 17:45:10 -05004701 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
4702 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004703
Andy Adamson104aeba2010-01-14 17:45:10 -05004704 /* Does the newly negotiated max_reqs match the existing slot table? */
4705 if (max_reqs != tbl->max_slots) {
4706 ret = -ENOMEM;
4707 new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004708 GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05004709 if (!new)
4710 goto out;
4711 ret = 0;
4712 kfree(tbl->slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004713 }
4714 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson104aeba2010-01-14 17:45:10 -05004715 if (new) {
4716 tbl->slots = new;
4717 tbl->max_slots = max_reqs;
4718 }
4719 for (i = 0; i < tbl->max_slots; ++i)
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004720 tbl->slots[i].seq_nr = ivalue;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004721 spin_unlock(&tbl->slot_tbl_lock);
4722 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4723 tbl, tbl->slots, tbl->max_slots);
4724out:
4725 dprintk("<-- %s: return %d\n", __func__, ret);
4726 return ret;
4727}
4728
Andy Adamsonfc931582009-04-01 09:22:31 -04004729/*
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004730 * Reset the forechannel and backchannel slot tables
4731 */
4732static int nfs4_reset_slot_tables(struct nfs4_session *session)
4733{
4734 int status;
4735
4736 status = nfs4_reset_slot_table(&session->fc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05004737 session->fc_attrs.max_reqs, 1);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004738 if (status)
4739 return status;
4740
4741 status = nfs4_reset_slot_table(&session->bc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05004742 session->bc_attrs.max_reqs, 0);
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004743 return status;
4744}
4745
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004746/* Destroy the slot table */
4747static void nfs4_destroy_slot_tables(struct nfs4_session *session)
4748{
4749 if (session->fc_slot_table.slots != NULL) {
4750 kfree(session->fc_slot_table.slots);
4751 session->fc_slot_table.slots = NULL;
4752 }
4753 if (session->bc_slot_table.slots != NULL) {
4754 kfree(session->bc_slot_table.slots);
4755 session->bc_slot_table.slots = NULL;
4756 }
4757 return;
4758}
4759
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004760/*
Andy Adamsonfc931582009-04-01 09:22:31 -04004761 * Initialize slot table
4762 */
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004763static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
4764 int max_slots, int ivalue)
Andy Adamsonfc931582009-04-01 09:22:31 -04004765{
Andy Adamsonfc931582009-04-01 09:22:31 -04004766 struct nfs4_slot *slot;
4767 int ret = -ENOMEM;
4768
4769 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
4770
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004771 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
Andy Adamsonfc931582009-04-01 09:22:31 -04004772
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004773 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_NOFS);
Andy Adamsonfc931582009-04-01 09:22:31 -04004774 if (!slot)
4775 goto out;
Andy Adamsonfc931582009-04-01 09:22:31 -04004776 ret = 0;
4777
4778 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonfc931582009-04-01 09:22:31 -04004779 tbl->max_slots = max_slots;
4780 tbl->slots = slot;
4781 tbl->highest_used_slotid = -1; /* no slot is currently used */
4782 spin_unlock(&tbl->slot_tbl_lock);
4783 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4784 tbl, tbl->slots, tbl->max_slots);
4785out:
4786 dprintk("<-- %s: return %d\n", __func__, ret);
4787 return ret;
Andy Adamsonfc931582009-04-01 09:22:31 -04004788}
4789
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004790/*
4791 * Initialize the forechannel and backchannel tables
4792 */
4793static int nfs4_init_slot_tables(struct nfs4_session *session)
4794{
Trond Myklebustf26468f2009-12-05 19:32:11 -05004795 struct nfs4_slot_table *tbl;
4796 int status = 0;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004797
Trond Myklebustf26468f2009-12-05 19:32:11 -05004798 tbl = &session->fc_slot_table;
4799 if (tbl->slots == NULL) {
4800 status = nfs4_init_slot_table(tbl,
4801 session->fc_attrs.max_reqs, 1);
4802 if (status)
4803 return status;
4804 }
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004805
Trond Myklebustf26468f2009-12-05 19:32:11 -05004806 tbl = &session->bc_slot_table;
4807 if (tbl->slots == NULL) {
4808 status = nfs4_init_slot_table(tbl,
4809 session->bc_attrs.max_reqs, 0);
4810 if (status)
4811 nfs4_destroy_slot_tables(session);
4812 }
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004813
4814 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04004815}
4816
4817struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
4818{
4819 struct nfs4_session *session;
4820 struct nfs4_slot_table *tbl;
4821
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004822 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04004823 if (!session)
4824 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04004825
Andy Adamsonea028ac2009-12-04 15:55:38 -05004826 init_completion(&session->complete);
Andy Adamson76db6d92009-04-01 09:22:38 -04004827
Andy Adamson557134a2009-04-01 09:21:53 -04004828 tbl = &session->fc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05004829 tbl->highest_used_slotid = -1;
Andy Adamson557134a2009-04-01 09:21:53 -04004830 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004831 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004832
4833 tbl = &session->bc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05004834 tbl->highest_used_slotid = -1;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004835 spin_lock_init(&tbl->slot_tbl_lock);
4836 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
4837
Trond Myklebust1055d762010-06-16 09:52:27 -04004838 session->session_state = 1<<NFS4_SESSION_INITING;
4839
Andy Adamson557134a2009-04-01 09:21:53 -04004840 session->clp = clp;
4841 return session;
4842}
4843
4844void nfs4_destroy_session(struct nfs4_session *session)
4845{
Andy Adamson5a0ffe52009-04-01 09:23:18 -04004846 nfs4_proc_destroy_session(session);
4847 dprintk("%s Destroy backchannel for xprt %p\n",
4848 __func__, session->clp->cl_rpcclient->cl_xprt);
4849 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
4850 NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004851 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04004852 kfree(session);
4853}
4854
Andy Adamsonfc931582009-04-01 09:22:31 -04004855/*
4856 * Initialize the values to be used by the client in CREATE_SESSION
4857 * If nfs4_init_session set the fore channel request and response sizes,
4858 * use them.
4859 *
4860 * Set the back channel max_resp_sz_cached to zero to force the client to
4861 * always set csa_cachethis to FALSE because the current implementation
4862 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
4863 */
4864static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
4865{
4866 struct nfs4_session *session = args->client->cl_session;
4867 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
4868 mxresp_sz = session->fc_attrs.max_resp_sz;
4869
4870 if (mxrqst_sz == 0)
4871 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
4872 if (mxresp_sz == 0)
4873 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
4874 /* Fore channel attributes */
4875 args->fc_attrs.headerpadsz = 0;
4876 args->fc_attrs.max_rqst_sz = mxrqst_sz;
4877 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04004878 args->fc_attrs.max_ops = NFS4_MAX_OPS;
4879 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
4880
4881 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05004882 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04004883 __func__,
4884 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05004885 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04004886
4887 /* Back channel attributes */
4888 args->bc_attrs.headerpadsz = 0;
4889 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
4890 args->bc_attrs.max_resp_sz = PAGE_SIZE;
4891 args->bc_attrs.max_resp_sz_cached = 0;
4892 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
4893 args->bc_attrs.max_reqs = 1;
4894
4895 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
4896 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4897 __func__,
4898 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
4899 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
4900 args->bc_attrs.max_reqs);
4901}
4902
Andy Adamson8d353012009-04-01 09:22:32 -04004903static int _verify_channel_attr(char *chan, char *attr_name, u32 sent, u32 rcvd)
4904{
4905 if (rcvd <= sent)
4906 return 0;
4907 printk(KERN_WARNING "%s: Session INVALID: %s channel %s increased. "
4908 "sent=%u rcvd=%u\n", __func__, chan, attr_name, sent, rcvd);
4909 return -EINVAL;
4910}
4911
4912#define _verify_fore_channel_attr(_name_) \
4913 _verify_channel_attr("fore", #_name_, \
4914 args->fc_attrs._name_, \
4915 session->fc_attrs._name_)
4916
4917#define _verify_back_channel_attr(_name_) \
4918 _verify_channel_attr("back", #_name_, \
4919 args->bc_attrs._name_, \
4920 session->bc_attrs._name_)
4921
4922/*
4923 * The server is not allowed to increase the fore channel header pad size,
4924 * maximum response size, or maximum number of operations.
4925 *
4926 * The back channel attributes are only negotiatied down: We send what the
4927 * (back channel) server insists upon.
4928 */
4929static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
4930 struct nfs4_session *session)
4931{
4932 int ret = 0;
4933
4934 ret |= _verify_fore_channel_attr(headerpadsz);
4935 ret |= _verify_fore_channel_attr(max_resp_sz);
4936 ret |= _verify_fore_channel_attr(max_ops);
4937
4938 ret |= _verify_back_channel_attr(headerpadsz);
4939 ret |= _verify_back_channel_attr(max_rqst_sz);
4940 ret |= _verify_back_channel_attr(max_resp_sz);
4941 ret |= _verify_back_channel_attr(max_resp_sz_cached);
4942 ret |= _verify_back_channel_attr(max_ops);
4943 ret |= _verify_back_channel_attr(max_reqs);
4944
4945 return ret;
4946}
4947
Andy Adamsonfc931582009-04-01 09:22:31 -04004948static int _nfs4_proc_create_session(struct nfs_client *clp)
4949{
4950 struct nfs4_session *session = clp->cl_session;
4951 struct nfs41_create_session_args args = {
4952 .client = clp,
4953 .cb_program = NFS4_CALLBACK,
4954 };
4955 struct nfs41_create_session_res res = {
4956 .client = clp,
4957 };
4958 struct rpc_message msg = {
4959 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
4960 .rpc_argp = &args,
4961 .rpc_resp = &res,
4962 };
4963 int status;
4964
4965 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04004966 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04004967
4968 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4969
Andy Adamson8d353012009-04-01 09:22:32 -04004970 if (!status)
4971 /* Verify the session's negotiated channel_attrs values */
4972 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04004973 if (!status) {
4974 /* Increment the clientid slot sequence id */
4975 clp->cl_seqid++;
4976 }
4977
4978 return status;
4979}
4980
4981/*
4982 * Issues a CREATE_SESSION operation to the server.
4983 * It is the responsibility of the caller to verify the session is
4984 * expired before calling this routine.
4985 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05004986int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04004987{
4988 int status;
4989 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04004990 struct nfs4_session *session = clp->cl_session;
4991
4992 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
4993
4994 status = _nfs4_proc_create_session(clp);
4995 if (status)
4996 goto out;
4997
Trond Myklebustf26468f2009-12-05 19:32:11 -05004998 /* Init and reset the fore channel */
4999 status = nfs4_init_slot_tables(session);
5000 dprintk("slot table initialization returned %d\n", status);
5001 if (status)
5002 goto out;
5003 status = nfs4_reset_slot_tables(session);
5004 dprintk("slot table reset returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005005 if (status)
5006 goto out;
5007
5008 ptr = (unsigned *)&session->sess_id.data[0];
5009 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5010 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005011out:
5012 dprintk("<-- %s\n", __func__);
5013 return status;
5014}
5015
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005016/*
5017 * Issue the over-the-wire RPC DESTROY_SESSION.
5018 * The caller must serialize access to this routine.
5019 */
5020int nfs4_proc_destroy_session(struct nfs4_session *session)
5021{
5022 int status = 0;
5023 struct rpc_message msg;
5024
5025 dprintk("--> nfs4_proc_destroy_session\n");
5026
5027 /* session is still being setup */
5028 if (session->clp->cl_cons_state != NFS_CS_READY)
5029 return status;
5030
5031 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5032 msg.rpc_argp = session;
5033 msg.rpc_resp = NULL;
5034 msg.rpc_cred = NULL;
5035 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
5036
5037 if (status)
5038 printk(KERN_WARNING
5039 "Got error %d from the server on DESTROY_SESSION. "
5040 "Session has been destroyed regardless...\n", status);
5041
5042 dprintk("<-- nfs4_proc_destroy_session\n");
5043 return status;
5044}
5045
Trond Myklebustfccba802009-07-21 16:48:07 -04005046int nfs4_init_session(struct nfs_server *server)
5047{
5048 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005049 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005050 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005051 int ret;
5052
5053 if (!nfs4_has_session(clp))
5054 return 0;
5055
Trond Myklebust1055d762010-06-16 09:52:27 -04005056 session = clp->cl_session;
5057 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5058 return 0;
5059
Andy Adamson68bf05e2009-12-15 12:55:02 -05005060 rsize = server->rsize;
5061 if (rsize == 0)
5062 rsize = NFS_MAX_FILE_IO_SIZE;
5063 wsize = server->wsize;
5064 if (wsize == 0)
5065 wsize = NFS_MAX_FILE_IO_SIZE;
5066
Andy Adamson68bf05e2009-12-15 12:55:02 -05005067 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5068 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005069
Trond Myklebustfccba802009-07-21 16:48:07 -04005070 ret = nfs4_recover_expired_lease(server);
5071 if (!ret)
5072 ret = nfs4_check_client_ready(clp);
5073 return ret;
5074}
5075
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005076/*
5077 * Renew the cl_session lease.
5078 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005079struct nfs4_sequence_data {
5080 struct nfs_client *clp;
5081 struct nfs4_sequence_args args;
5082 struct nfs4_sequence_res res;
5083};
5084
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005085static void nfs41_sequence_release(void *data)
5086{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005087 struct nfs4_sequence_data *calldata = data;
5088 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005089
Alexandros Batsakis71358402010-02-05 03:45:05 -08005090 if (atomic_read(&clp->cl_count) > 1)
5091 nfs4_schedule_state_renewal(clp);
5092 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005093 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005094}
5095
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005096static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5097{
5098 switch(task->tk_status) {
5099 case -NFS4ERR_DELAY:
5100 case -EKEYEXPIRED:
5101 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5102 return -EAGAIN;
5103 default:
5104 nfs4_schedule_state_recovery(clp);
5105 }
5106 return 0;
5107}
5108
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005109static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005110{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005111 struct nfs4_sequence_data *calldata = data;
5112 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005113
Trond Myklebustd185a332010-06-16 09:52:25 -04005114 nfs41_sequence_done(task->tk_msg.rpc_resp);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005115
5116 if (task->tk_status < 0) {
5117 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005118 if (atomic_read(&clp->cl_count) == 1)
5119 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005120
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005121 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5122 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005123 return;
5124 }
5125 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005126 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005127out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005128 dprintk("<-- %s\n", __func__);
5129}
5130
5131static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5132{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005133 struct nfs4_sequence_data *calldata = data;
5134 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005135 struct nfs4_sequence_args *args;
5136 struct nfs4_sequence_res *res;
5137
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005138 args = task->tk_msg.rpc_argp;
5139 res = task->tk_msg.rpc_resp;
5140
Trond Myklebust035168a2010-06-16 09:52:26 -04005141 if (nfs41_setup_sequence(clp->cl_session, args, res, 0, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005142 return;
5143 rpc_call_start(task);
5144}
5145
5146static const struct rpc_call_ops nfs41_sequence_ops = {
5147 .rpc_call_done = nfs41_sequence_call_done,
5148 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005149 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005150};
5151
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005152static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005153{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005154 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005155 struct rpc_message msg = {
5156 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5157 .rpc_cred = cred,
5158 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005159 struct rpc_task_setup task_setup_data = {
5160 .rpc_client = clp->cl_rpcclient,
5161 .rpc_message = &msg,
5162 .callback_ops = &nfs41_sequence_ops,
5163 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5164 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005165
Alexandros Batsakis71358402010-02-05 03:45:05 -08005166 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005167 return ERR_PTR(-EIO);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005168 calldata = kmalloc(sizeof(*calldata), GFP_NOFS);
5169 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005170 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005171 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005172 }
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005173 calldata->res.sr_slotid = NFS4_MAX_SLOT_TABLE;
5174 msg.rpc_argp = &calldata->args;
5175 msg.rpc_resp = &calldata->res;
5176 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005177 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005178
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005179 return rpc_run_task(&task_setup_data);
5180}
5181
5182static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5183{
5184 struct rpc_task *task;
5185 int ret = 0;
5186
5187 task = _nfs41_proc_sequence(clp, cred);
5188 if (IS_ERR(task))
5189 ret = PTR_ERR(task);
5190 else
5191 rpc_put_task(task);
5192 dprintk("<-- %s status=%d\n", __func__, ret);
5193 return ret;
5194}
5195
5196static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5197{
5198 struct rpc_task *task;
5199 int ret;
5200
5201 task = _nfs41_proc_sequence(clp, cred);
5202 if (IS_ERR(task)) {
5203 ret = PTR_ERR(task);
5204 goto out;
5205 }
5206 ret = rpc_wait_for_completion_task(task);
5207 if (!ret)
5208 ret = task->tk_status;
5209 rpc_put_task(task);
5210out:
5211 dprintk("<-- %s status=%d\n", __func__, ret);
5212 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005213}
5214
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005215struct nfs4_reclaim_complete_data {
5216 struct nfs_client *clp;
5217 struct nfs41_reclaim_complete_args arg;
5218 struct nfs41_reclaim_complete_res res;
5219};
5220
5221static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5222{
5223 struct nfs4_reclaim_complete_data *calldata = data;
5224
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005225 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04005226 if (nfs41_setup_sequence(calldata->clp->cl_session,
5227 &calldata->arg.seq_args,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005228 &calldata->res.seq_res, 0, task))
5229 return;
5230
5231 rpc_call_start(task);
5232}
5233
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005234static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5235{
5236 switch(task->tk_status) {
5237 case 0:
5238 case -NFS4ERR_COMPLETE_ALREADY:
5239 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5240 break;
5241 case -NFS4ERR_DELAY:
5242 case -EKEYEXPIRED:
5243 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5244 return -EAGAIN;
5245 default:
5246 nfs4_schedule_state_recovery(clp);
5247 }
5248 return 0;
5249}
5250
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005251static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5252{
5253 struct nfs4_reclaim_complete_data *calldata = data;
5254 struct nfs_client *clp = calldata->clp;
5255 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5256
5257 dprintk("--> %s\n", __func__);
Trond Myklebustd185a332010-06-16 09:52:25 -04005258 nfs41_sequence_done(res);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005259
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005260 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5261 rpc_restart_call_prepare(task);
5262 return;
5263 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005264 dprintk("<-- %s\n", __func__);
5265}
5266
5267static void nfs4_free_reclaim_complete_data(void *data)
5268{
5269 struct nfs4_reclaim_complete_data *calldata = data;
5270
5271 kfree(calldata);
5272}
5273
5274static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5275 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5276 .rpc_call_done = nfs4_reclaim_complete_done,
5277 .rpc_release = nfs4_free_reclaim_complete_data,
5278};
5279
5280/*
5281 * Issue a global reclaim complete.
5282 */
5283static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5284{
5285 struct nfs4_reclaim_complete_data *calldata;
5286 struct rpc_task *task;
5287 struct rpc_message msg = {
5288 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5289 };
5290 struct rpc_task_setup task_setup_data = {
5291 .rpc_client = clp->cl_rpcclient,
5292 .rpc_message = &msg,
5293 .callback_ops = &nfs4_reclaim_complete_call_ops,
5294 .flags = RPC_TASK_ASYNC,
5295 };
5296 int status = -ENOMEM;
5297
5298 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005299 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005300 if (calldata == NULL)
5301 goto out;
5302 calldata->clp = clp;
5303 calldata->arg.one_fs = 0;
5304 calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
5305
5306 msg.rpc_argp = &calldata->arg;
5307 msg.rpc_resp = &calldata->res;
5308 task_setup_data.callback_data = calldata;
5309 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005310 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005311 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005312 goto out;
5313 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005314 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005315 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005316out:
5317 dprintk("<-- %s status=%d\n", __func__, status);
5318 return status;
5319}
Andy Adamson557134a2009-04-01 09:21:53 -04005320#endif /* CONFIG_NFS_V4_1 */
5321
Andy Adamson591d71c2009-04-01 09:22:47 -04005322struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005323 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005324 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005325 .recover_open = nfs4_open_reclaim,
5326 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04005327 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005328 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005329};
5330
Andy Adamson591d71c2009-04-01 09:22:47 -04005331#if defined(CONFIG_NFS_V4_1)
5332struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
5333 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5334 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5335 .recover_open = nfs4_open_reclaim,
5336 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05005337 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005338 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005339 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04005340};
5341#endif /* CONFIG_NFS_V4_1 */
5342
5343struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005344 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005345 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346 .recover_open = nfs4_open_expired,
5347 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04005348 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005349 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350};
5351
Andy Adamson591d71c2009-04-01 09:22:47 -04005352#if defined(CONFIG_NFS_V4_1)
5353struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
5354 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5355 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5356 .recover_open = nfs4_open_expired,
5357 .recover_lock = nfs4_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05005358 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005359 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04005360};
5361#endif /* CONFIG_NFS_V4_1 */
5362
Benny Halevy29fba382009-04-01 09:22:44 -04005363struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
5364 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04005365 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005366 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04005367};
5368
5369#if defined(CONFIG_NFS_V4_1)
5370struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
5371 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04005372 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005373 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04005374};
5375#endif
5376
Trond Myklebust97dc1352010-06-16 09:52:26 -04005377static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
5378 .minor_version = 0,
5379 .call_sync = _nfs4_call_sync,
Trond Myklebuste047a102010-06-16 09:52:27 -04005380 .validate_stateid = nfs4_validate_delegation_stateid,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04005381 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
5382 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
5383 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04005384};
5385
5386#if defined(CONFIG_NFS_V4_1)
5387static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
5388 .minor_version = 1,
5389 .call_sync = _nfs4_call_sync_session,
Trond Myklebuste047a102010-06-16 09:52:27 -04005390 .validate_stateid = nfs41_validate_delegation_stateid,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04005391 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
5392 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
5393 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04005394};
5395#endif
5396
Trond Myklebust97dc1352010-06-16 09:52:26 -04005397const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
5398 [0] = &nfs_v4_0_minor_ops,
5399#if defined(CONFIG_NFS_V4_1)
5400 [1] = &nfs_v4_1_minor_ops,
5401#endif
5402};
5403
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08005404static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005405 .permission = nfs_permission,
5406 .getattr = nfs_getattr,
5407 .setattr = nfs_setattr,
5408 .getxattr = nfs4_getxattr,
5409 .setxattr = nfs4_setxattr,
5410 .listxattr = nfs4_listxattr,
5411};
5412
David Howells509de812006-08-22 20:06:11 -04005413const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414 .version = 4, /* protocol version */
5415 .dentry_ops = &nfs4_dentry_operations,
5416 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005417 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418 .getroot = nfs4_proc_get_root,
5419 .getattr = nfs4_proc_getattr,
5420 .setattr = nfs4_proc_setattr,
David Howells2b3de442006-08-22 20:06:09 -04005421 .lookupfh = nfs4_proc_lookupfh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422 .lookup = nfs4_proc_lookup,
5423 .access = nfs4_proc_access,
5424 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425 .create = nfs4_proc_create,
5426 .remove = nfs4_proc_remove,
5427 .unlink_setup = nfs4_proc_unlink_setup,
5428 .unlink_done = nfs4_proc_unlink_done,
5429 .rename = nfs4_proc_rename,
5430 .link = nfs4_proc_link,
5431 .symlink = nfs4_proc_symlink,
5432 .mkdir = nfs4_proc_mkdir,
5433 .rmdir = nfs4_proc_remove,
5434 .readdir = nfs4_proc_readdir,
5435 .mknod = nfs4_proc_mknod,
5436 .statfs = nfs4_proc_statfs,
5437 .fsinfo = nfs4_proc_fsinfo,
5438 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04005439 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440 .decode_dirent = nfs4_decode_dirent,
5441 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05005442 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005444 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005446 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005448 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04005449 .close_context = nfs4_close_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450};
5451
5452/*
5453 * Local variables:
5454 * c-basic-offset: 8
5455 * End:
5456 */