blob: 70015dd60a9881f16318c78914996102d926f061 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090042#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/sunrpc/clnt.h>
44#include <linux/nfs.h>
45#include <linux/nfs4.h>
46#include <linux/nfs_fs.h>
47#include <linux/nfs_page.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070049#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040050#include <linux/module.h>
Andy Adamson5a0ffe52009-04-01 09:23:18 -040051#include <linux/sunrpc/bc_xprt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Trond Myklebust4ce79712005-06-22 17:16:21 +000053#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050055#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050056#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040057#include "callback.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59#define NFSDBG_FACILITY NFSDBG_PROC
60
Trond Myklebust2066fe82006-09-15 08:30:46 -040061#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#define NFS4_POLL_RETRY_MAX (15*HZ)
63
Trond Myklebusta78cb572009-08-09 15:06:19 -040064#define NFS4_MAX_LOOP_ON_RECOVER (10)
65
Trond Myklebustcdd4e682006-01-03 09:55:12 +010066struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010067static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080068static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050070static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Trond Myklebust99367812007-07-17 21:52:41 -040071static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
72static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040073static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
74 struct nfs_fattr *fattr, struct iattr *sattr,
75 struct nfs4_state *state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Linus Torvalds1da177e2005-04-16 15:20:36 -070077/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050078static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
Trond Myklebust52567b02009-10-23 14:46:42 -040080 if (err >= -1000)
81 return err;
82 switch (err) {
83 case -NFS4ERR_RESOURCE:
84 return -EREMOTEIO;
85 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -070087 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -040088 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 }
Trond Myklebust52567b02009-10-23 14:46:42 -040090 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
92
93/*
94 * This is our standard bitmap for GETATTR requests.
95 */
96const u32 nfs4_fattr_bitmap[2] = {
97 FATTR4_WORD0_TYPE
98 | FATTR4_WORD0_CHANGE
99 | FATTR4_WORD0_SIZE
100 | FATTR4_WORD0_FSID
101 | FATTR4_WORD0_FILEID,
102 FATTR4_WORD1_MODE
103 | FATTR4_WORD1_NUMLINKS
104 | FATTR4_WORD1_OWNER
105 | FATTR4_WORD1_OWNER_GROUP
106 | FATTR4_WORD1_RAWDEV
107 | FATTR4_WORD1_SPACE_USED
108 | FATTR4_WORD1_TIME_ACCESS
109 | FATTR4_WORD1_TIME_METADATA
110 | FATTR4_WORD1_TIME_MODIFY
111};
112
113const u32 nfs4_statfs_bitmap[2] = {
114 FATTR4_WORD0_FILES_AVAIL
115 | FATTR4_WORD0_FILES_FREE
116 | FATTR4_WORD0_FILES_TOTAL,
117 FATTR4_WORD1_SPACE_AVAIL
118 | FATTR4_WORD1_SPACE_FREE
119 | FATTR4_WORD1_SPACE_TOTAL
120};
121
Trond Myklebust4ce79712005-06-22 17:16:21 +0000122const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 FATTR4_WORD0_MAXLINK
124 | FATTR4_WORD0_MAXNAME,
125 0
126};
127
128const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
129 | FATTR4_WORD0_MAXREAD
130 | FATTR4_WORD0_MAXWRITE
131 | FATTR4_WORD0_LEASE_TIME,
132 0
133};
134
Manoj Naik830b8e32006-06-09 09:34:25 -0400135const u32 nfs4_fs_locations_bitmap[2] = {
136 FATTR4_WORD0_TYPE
137 | FATTR4_WORD0_CHANGE
138 | FATTR4_WORD0_SIZE
139 | FATTR4_WORD0_FSID
140 | FATTR4_WORD0_FILEID
141 | FATTR4_WORD0_FS_LOCATIONS,
142 FATTR4_WORD1_MODE
143 | FATTR4_WORD1_NUMLINKS
144 | FATTR4_WORD1_OWNER
145 | FATTR4_WORD1_OWNER_GROUP
146 | FATTR4_WORD1_RAWDEV
147 | FATTR4_WORD1_SPACE_USED
148 | FATTR4_WORD1_TIME_ACCESS
149 | FATTR4_WORD1_TIME_METADATA
150 | FATTR4_WORD1_TIME_MODIFY
151 | FATTR4_WORD1_MOUNTED_ON_FILEID
152};
153
Al Virobc4785c2006-10-19 23:28:51 -0700154static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 struct nfs4_readdir_arg *readdir)
156{
Al Viro0dbb4c62006-10-19 23:28:49 -0700157 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159 BUG_ON(readdir->count < 80);
160 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000161 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
163 return;
164 }
165
166 readdir->cookie = 0;
167 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
168 if (cookie == 2)
169 return;
170
171 /*
172 * NFSv4 servers do not return entries for '.' and '..'
173 * Therefore, we fake these entries here. We let '.'
174 * have cookie 0 and '..' have cookie 1. Note that
175 * when talking to the server, we always send cookie 0
176 * instead of 1 or 2.
177 */
Al Viro0dbb4c62006-10-19 23:28:49 -0700178 start = p = kmap_atomic(*readdir->pages, KM_USER0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 if (cookie == 0) {
181 *p++ = xdr_one; /* next */
182 *p++ = xdr_zero; /* cookie, first word */
183 *p++ = xdr_one; /* cookie, second word */
184 *p++ = xdr_one; /* entry len */
185 memcpy(p, ".\0\0\0", 4); /* entry */
186 p++;
187 *p++ = xdr_one; /* bitmap length */
188 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
189 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400190 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 }
192
193 *p++ = xdr_one; /* next */
194 *p++ = xdr_zero; /* cookie, first word */
195 *p++ = xdr_two; /* cookie, second word */
196 *p++ = xdr_two; /* entry len */
197 memcpy(p, "..\0\0", 4); /* entry */
198 p++;
199 *p++ = xdr_one; /* bitmap length */
200 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
201 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400202 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204 readdir->pgbase = (char *)p - (char *)start;
205 readdir->count -= readdir->pgbase;
206 kunmap_atomic(start, KM_USER0);
207}
208
Trond Myklebust65de8722008-12-23 15:21:44 -0500209static int nfs4_wait_clnt_recover(struct nfs_client *clp)
210{
211 int res;
212
213 might_sleep();
214
Trond Myklebuste005e802008-12-23 15:21:48 -0500215 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400216 nfs_wait_bit_killable, TASK_KILLABLE);
Trond Myklebust65de8722008-12-23 15:21:44 -0500217 return res;
218}
219
220static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
221{
222 int res = 0;
223
224 might_sleep();
225
226 if (*timeout <= 0)
227 *timeout = NFS4_POLL_RETRY_MIN;
228 if (*timeout > NFS4_POLL_RETRY_MAX)
229 *timeout = NFS4_POLL_RETRY_MAX;
230 schedule_timeout_killable(*timeout);
231 if (fatal_signal_pending(current))
232 res = -ERESTARTSYS;
233 *timeout <<= 1;
234 return res;
235}
236
237/* This is the error handling routine for processes that are allowed
238 * to sleep.
239 */
240static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
241{
242 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500243 struct nfs4_state *state = exception->state;
Trond Myklebust65de8722008-12-23 15:21:44 -0500244 int ret = errorcode;
245
246 exception->retry = 0;
247 switch(errorcode) {
248 case 0:
249 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500250 case -NFS4ERR_ADMIN_REVOKED:
251 case -NFS4ERR_BAD_STATEID:
252 case -NFS4ERR_OPENMODE:
253 if (state == NULL)
254 break;
255 nfs4_state_mark_reclaim_nograce(clp, state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500256 goto do_state_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500257 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500258 if (state == NULL)
259 break;
260 nfs4_state_mark_reclaim_reboot(clp, state);
261 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust65de8722008-12-23 15:21:44 -0500262 case -NFS4ERR_EXPIRED:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500263 goto do_state_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500264#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400265 case -NFS4ERR_BADSESSION:
266 case -NFS4ERR_BADSLOT:
267 case -NFS4ERR_BAD_HIGH_SLOT:
268 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
269 case -NFS4ERR_DEADSESSION:
270 case -NFS4ERR_SEQ_FALSE_RETRY:
271 case -NFS4ERR_SEQ_MISORDERED:
272 dprintk("%s ERROR: %d Reset session\n", __func__,
273 errorcode);
Andy Adamson0b9e2d42009-12-04 16:02:14 -0500274 nfs4_schedule_state_recovery(clp);
Andy Adamson4745e312009-04-01 09:22:42 -0400275 exception->retry = 1;
Andy Adamsonb9179232009-12-04 15:55:32 -0500276 break;
Trond Myklebust03391692010-01-26 15:42:38 -0500277#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500278 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500279 if (exception->timeout > HZ) {
280 /* We have retried a decent amount, time to
281 * fail
282 */
283 ret = -EBUSY;
284 break;
285 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500286 case -NFS4ERR_GRACE:
287 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -0500288 case -EKEYEXPIRED:
Trond Myklebust65de8722008-12-23 15:21:44 -0500289 ret = nfs4_delay(server->client, &exception->timeout);
290 if (ret != 0)
291 break;
292 case -NFS4ERR_OLD_STATEID:
293 exception->retry = 1;
294 }
295 /* We failed to handle the error */
296 return nfs4_map_errors(ret);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500297do_state_recovery:
298 nfs4_schedule_state_recovery(clp);
299 ret = nfs4_wait_clnt_recover(clp);
300 if (ret == 0)
301 exception->retry = 1;
302 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500303}
304
305
Trond Myklebust26e976a2006-01-03 09:55:21 +0100306static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
David Howells7539bba2006-08-22 20:06:09 -0400308 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 spin_lock(&clp->cl_lock);
310 if (time_before(clp->cl_last_renewal,timestamp))
311 clp->cl_last_renewal = timestamp;
312 spin_unlock(&clp->cl_lock);
313}
314
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400315#if defined(CONFIG_NFS_V4_1)
316
Benny Halevy510b8172009-04-01 09:22:14 -0400317/*
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400318 * nfs4_free_slot - free a slot and efficiently update slot table.
319 *
320 * freeing a slot is trivially done by clearing its respective bit
321 * in the bitmap.
322 * If the freed slotid equals highest_used_slotid we want to update it
323 * so that the server would be able to size down the slot table if needed,
324 * otherwise we know that the highest_used_slotid is still in use.
325 * When updating highest_used_slotid there may be "holes" in the bitmap
326 * so we need to scan down from highest_used_slotid to 0 looking for the now
327 * highest slotid in use.
328 * If none found, highest_used_slotid is set to -1.
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500329 *
330 * Must be called while holding tbl->slot_tbl_lock
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400331 */
332static void
333nfs4_free_slot(struct nfs4_slot_table *tbl, u8 free_slotid)
334{
335 int slotid = free_slotid;
336
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400337 /* clear used bit in bitmap */
338 __clear_bit(slotid, tbl->used_slots);
339
340 /* update highest_used_slotid when it is freed */
341 if (slotid == tbl->highest_used_slotid) {
342 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500343 if (slotid < tbl->max_slots)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400344 tbl->highest_used_slotid = slotid;
345 else
346 tbl->highest_used_slotid = -1;
347 }
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400348 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
349 free_slotid, tbl->highest_used_slotid);
350}
351
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800352/*
353 * Signal state manager thread if session is drained
354 */
355static void nfs41_check_drain_session_complete(struct nfs4_session *ses)
356{
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800357 struct rpc_task *task;
358
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800359 if (!test_bit(NFS4CLNT_SESSION_DRAINING, &ses->clp->cl_state)) {
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800360 task = rpc_wake_up_next(&ses->fc_slot_table.slot_tbl_waitq);
361 if (task)
362 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800363 return;
364 }
365
366 if (ses->fc_slot_table.highest_used_slotid != -1)
367 return;
368
369 dprintk("%s COMPLETE: Session Drained\n", __func__);
370 complete(&ses->complete);
371}
372
Trond Myklebustd61e6122009-12-05 19:32:19 -0500373static void nfs41_sequence_free_slot(const struct nfs_client *clp,
Andy Adamson13615872009-04-01 09:22:17 -0400374 struct nfs4_sequence_res *res)
375{
376 struct nfs4_slot_table *tbl;
377
Andy Adamson13615872009-04-01 09:22:17 -0400378 tbl = &clp->cl_session->fc_slot_table;
379 if (res->sr_slotid == NFS4_MAX_SLOT_TABLE) {
Andy Adamson13615872009-04-01 09:22:17 -0400380 /* just wake up the next guy waiting since
381 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500382 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500383 return;
Andy Adamson13615872009-04-01 09:22:17 -0400384 }
Andy Adamsonea028ac2009-12-04 15:55:38 -0500385
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500386 spin_lock(&tbl->slot_tbl_lock);
387 nfs4_free_slot(tbl, res->sr_slotid);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800388 nfs41_check_drain_session_complete(clp->cl_session);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500389 spin_unlock(&tbl->slot_tbl_lock);
390 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
Andy Adamson13615872009-04-01 09:22:17 -0400391}
392
Andy Adamsonb0df8062009-04-01 09:22:18 -0400393static void nfs41_sequence_done(struct nfs_client *clp,
394 struct nfs4_sequence_res *res,
395 int rpc_status)
396{
397 unsigned long timestamp;
398 struct nfs4_slot_table *tbl;
399 struct nfs4_slot *slot;
400
401 /*
402 * sr_status remains 1 if an RPC level error occurred. The server
403 * may or may not have processed the sequence operation..
404 * Proceed as if the server received and processed the sequence
405 * operation.
406 */
407 if (res->sr_status == 1)
408 res->sr_status = NFS_OK;
409
410 /* -ERESTARTSYS can result in skipping nfs41_sequence_setup */
411 if (res->sr_slotid == NFS4_MAX_SLOT_TABLE)
412 goto out;
413
Andy Adamson691daf32009-12-04 15:55:39 -0500414 /* Check the SEQUENCE operation status */
Andy Adamsonb0df8062009-04-01 09:22:18 -0400415 if (res->sr_status == 0) {
Andy Adamson691daf32009-12-04 15:55:39 -0500416 tbl = &clp->cl_session->fc_slot_table;
417 slot = tbl->slots + res->sr_slotid;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400418 /* Update the slot's sequence and clientid lease timer */
419 ++slot->seq_nr;
420 timestamp = res->sr_renewal_time;
421 spin_lock(&clp->cl_lock);
422 if (time_before(clp->cl_last_renewal, timestamp))
423 clp->cl_last_renewal = timestamp;
424 spin_unlock(&clp->cl_lock);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500425 /* Check sequence flags */
Alexandros Batsakis71358402010-02-05 03:45:05 -0800426 if (atomic_read(&clp->cl_count) > 1)
427 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400428 }
429out:
430 /* The session may be reset by one of the error handlers. */
431 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
432 nfs41_sequence_free_slot(clp, res);
433}
434
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400435/*
Benny Halevy510b8172009-04-01 09:22:14 -0400436 * nfs4_find_slot - efficiently look for a free slot
437 *
438 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
439 * If found, we mark the slot as used, update the highest_used_slotid,
440 * and respectively set up the sequence operation args.
441 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400442 *
443 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400444 */
445static u8
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800446nfs4_find_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400447{
448 int slotid;
449 u8 ret_id = NFS4_MAX_SLOT_TABLE;
450 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
451
Benny Halevy510b8172009-04-01 09:22:14 -0400452 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
453 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
454 tbl->max_slots);
455 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
456 if (slotid >= tbl->max_slots)
457 goto out;
458 __set_bit(slotid, tbl->used_slots);
459 if (slotid > tbl->highest_used_slotid)
460 tbl->highest_used_slotid = slotid;
461 ret_id = slotid;
462out:
463 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
464 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400465 return ret_id;
466}
467
Andy Adamsonce5039c2009-04-01 09:22:13 -0400468static int nfs41_setup_sequence(struct nfs4_session *session,
469 struct nfs4_sequence_args *args,
470 struct nfs4_sequence_res *res,
471 int cache_reply,
472 struct rpc_task *task)
473{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400474 struct nfs4_slot *slot;
475 struct nfs4_slot_table *tbl;
476 u8 slotid;
477
478 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400479 /* slot already allocated? */
480 if (res->sr_slotid != NFS4_MAX_SLOT_TABLE)
481 return 0;
482
483 memset(res, 0, sizeof(*res));
484 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400485 tbl = &session->fc_slot_table;
486
487 spin_lock(&tbl->slot_tbl_lock);
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800488 if (test_bit(NFS4CLNT_SESSION_DRAINING, &session->clp->cl_state) &&
489 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamsonea028ac2009-12-04 15:55:38 -0500490 /*
491 * The state manager will wait until the slot table is empty.
492 * Schedule the reset thread
493 */
Andy Adamson05f0d232009-12-04 15:55:37 -0500494 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400495 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500496 dprintk("%s Schedule Session Reset\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500497 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400498 }
499
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800500 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
501 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
502 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
503 spin_unlock(&tbl->slot_tbl_lock);
504 dprintk("%s enforce FIFO order\n", __func__);
505 return -EAGAIN;
506 }
507
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800508 slotid = nfs4_find_slot(tbl);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400509 if (slotid == NFS4_MAX_SLOT_TABLE) {
510 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
511 spin_unlock(&tbl->slot_tbl_lock);
512 dprintk("<-- %s: no free slots\n", __func__);
513 return -EAGAIN;
514 }
515 spin_unlock(&tbl->slot_tbl_lock);
516
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800517 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400518 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400519 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400520 args->sa_slotid = slotid;
521 args->sa_cache_this = cache_reply;
522
523 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
524
Benny Halevy99fe60d2009-04-01 09:22:29 -0400525 res->sr_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400526 res->sr_slotid = slotid;
527 res->sr_renewal_time = jiffies;
528 /*
529 * sr_status is only set in decode_sequence, and so will remain
530 * set to 1 if an rpc level failure occurs.
531 */
532 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400533 return 0;
534}
535
536int nfs4_setup_sequence(struct nfs_client *clp,
537 struct nfs4_sequence_args *args,
538 struct nfs4_sequence_res *res,
539 int cache_reply,
540 struct rpc_task *task)
541{
542 int ret = 0;
543
544 dprintk("--> %s clp %p session %p sr_slotid %d\n",
545 __func__, clp, clp->cl_session, res->sr_slotid);
546
547 if (!nfs4_has_session(clp))
548 goto out;
549 ret = nfs41_setup_sequence(clp->cl_session, args, res, cache_reply,
550 task);
Andy Adamson2628edd2009-12-04 15:55:35 -0500551 if (ret && ret != -EAGAIN) {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400552 /* terminate rpc task */
553 task->tk_status = ret;
554 task->tk_action = NULL;
555 }
556out:
557 dprintk("<-- %s status=%d\n", __func__, ret);
558 return ret;
559}
560
561struct nfs41_call_sync_data {
562 struct nfs_client *clp;
563 struct nfs4_sequence_args *seq_args;
564 struct nfs4_sequence_res *seq_res;
565 int cache_reply;
566};
567
568static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
569{
570 struct nfs41_call_sync_data *data = calldata;
571
572 dprintk("--> %s data->clp->cl_session %p\n", __func__,
573 data->clp->cl_session);
574 if (nfs4_setup_sequence(data->clp, data->seq_args,
575 data->seq_res, data->cache_reply, task))
576 return;
577 rpc_call_start(task);
578}
579
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800580static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
581{
582 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
583 nfs41_call_sync_prepare(task, calldata);
584}
585
Andy Adamson69ab40c2009-04-01 09:22:19 -0400586static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
587{
588 struct nfs41_call_sync_data *data = calldata;
589
590 nfs41_sequence_done(data->clp, data->seq_res, task->tk_status);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400591}
592
Andy Adamsonce5039c2009-04-01 09:22:13 -0400593struct rpc_call_ops nfs41_call_sync_ops = {
594 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400595 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400596};
597
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800598struct rpc_call_ops nfs41_call_priv_sync_ops = {
599 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
600 .rpc_call_done = nfs41_call_sync_done,
601};
602
Andy Adamsonce5039c2009-04-01 09:22:13 -0400603static int nfs4_call_sync_sequence(struct nfs_client *clp,
604 struct rpc_clnt *clnt,
605 struct rpc_message *msg,
606 struct nfs4_sequence_args *args,
607 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800608 int cache_reply,
609 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400610{
611 int ret;
612 struct rpc_task *task;
613 struct nfs41_call_sync_data data = {
614 .clp = clp,
615 .seq_args = args,
616 .seq_res = res,
617 .cache_reply = cache_reply,
618 };
619 struct rpc_task_setup task_setup = {
620 .rpc_client = clnt,
621 .rpc_message = msg,
622 .callback_ops = &nfs41_call_sync_ops,
623 .callback_data = &data
624 };
625
626 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800627 if (privileged)
628 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400629 task = rpc_run_task(&task_setup);
630 if (IS_ERR(task))
631 ret = PTR_ERR(task);
632 else {
633 ret = task->tk_status;
634 rpc_put_task(task);
635 }
636 return ret;
637}
638
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400639int _nfs4_call_sync_session(struct nfs_server *server,
640 struct rpc_message *msg,
641 struct nfs4_sequence_args *args,
642 struct nfs4_sequence_res *res,
643 int cache_reply)
644{
Andy Adamsonce5039c2009-04-01 09:22:13 -0400645 return nfs4_call_sync_sequence(server->nfs_client, server->client,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800646 msg, args, res, cache_reply, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400647}
648
649#endif /* CONFIG_NFS_V4_1 */
650
651int _nfs4_call_sync(struct nfs_server *server,
652 struct rpc_message *msg,
653 struct nfs4_sequence_args *args,
654 struct nfs4_sequence_res *res,
655 int cache_reply)
656{
Benny Halevyf3752972009-04-01 09:22:04 -0400657 args->sa_session = res->sr_session = NULL;
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400658 return rpc_call_sync(server->client, msg, 0);
659}
660
661#define nfs4_call_sync(server, msg, args, res, cache_reply) \
662 (server)->nfs_client->cl_call_sync((server), (msg), &(args)->seq_args, \
663 &(res)->seq_res, (cache_reply))
664
Andy Adamsonb0df8062009-04-01 09:22:18 -0400665static void nfs4_sequence_done(const struct nfs_server *server,
666 struct nfs4_sequence_res *res, int rpc_status)
667{
668#ifdef CONFIG_NFS_V4_1
669 if (nfs4_has_session(server->nfs_client))
670 nfs41_sequence_done(server->nfs_client, res, rpc_status);
671#endif /* CONFIG_NFS_V4_1 */
672}
673
Trond Myklebust38478b22006-05-25 01:40:57 -0400674static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
Trond Myklebust38478b22006-05-25 01:40:57 -0400676 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Trond Myklebust38478b22006-05-25 01:40:57 -0400678 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400679 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
680 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400681 nfs_force_lookup_revalidate(dir);
Trond Myklebust40d24702007-10-08 09:24:22 -0400682 nfsi->change_attr = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400683 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684}
685
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100686struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400687 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100688 struct nfs_openargs o_arg;
689 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100690 struct nfs_open_confirmargs c_arg;
691 struct nfs_open_confirmres c_res;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100692 struct nfs_fattr f_attr;
693 struct nfs_fattr dir_attr;
Trond Myklebustad389da2007-06-05 12:30:00 -0400694 struct path path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100695 struct dentry *dir;
696 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400697 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100698 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100699 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400700 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100701 int rpc_status;
702 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100703};
704
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400705
706static void nfs4_init_opendata_res(struct nfs4_opendata *p)
707{
708 p->o_res.f_attr = &p->f_attr;
709 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400710 p->o_res.seqid = p->o_arg.seqid;
711 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400712 p->o_res.server = p->o_arg.server;
713 nfs_fattr_init(&p->f_attr);
714 nfs_fattr_init(&p->dir_attr);
Benny Halevy578e4582009-06-18 22:01:23 -0400715 p->o_res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400716}
717
Trond Myklebustad389da2007-06-05 12:30:00 -0400718static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500719 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400720 const struct iattr *attrs,
721 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100722{
Trond Myklebustad389da2007-06-05 12:30:00 -0400723 struct dentry *parent = dget_parent(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100724 struct inode *dir = parent->d_inode;
725 struct nfs_server *server = NFS_SERVER(dir);
726 struct nfs4_opendata *p;
727
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400728 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100729 if (p == NULL)
730 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400731 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100732 if (p->o_arg.seqid == NULL)
733 goto err_free;
Al Virof6948692010-01-30 13:51:04 -0500734 path_get(path);
735 p->path = *path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100736 p->dir = parent;
737 p->owner = sp;
738 atomic_inc(&sp->so_count);
739 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500740 p->o_arg.open_flags = flags;
741 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400742 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400743 p->o_arg.id = sp->so_owner_id.id;
Trond Myklebustad389da2007-06-05 12:30:00 -0400744 p->o_arg.name = &p->path.dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100745 p->o_arg.server = server;
746 p->o_arg.bitmask = server->attr_bitmask;
747 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100748 if (flags & O_EXCL) {
Alexandros Batsakis4882ef72009-12-05 13:30:21 -0500749 if (nfs4_has_persistent_session(server->nfs_client)) {
750 /* GUARDED */
751 p->o_arg.u.attrs = &p->attrs;
752 memcpy(&p->attrs, attrs, sizeof(p->attrs));
753 } else { /* EXCLUSIVE4_1 */
754 u32 *s = (u32 *) p->o_arg.u.verifier.data;
755 s[0] = jiffies;
756 s[1] = current->pid;
757 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100758 } else if (flags & O_CREAT) {
759 p->o_arg.u.attrs = &p->attrs;
760 memcpy(&p->attrs, attrs, sizeof(p->attrs));
761 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100762 p->c_arg.fh = &p->o_res.fh;
763 p->c_arg.stateid = &p->o_res.stateid;
764 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400765 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400766 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100767 return p;
768err_free:
769 kfree(p);
770err:
771 dput(parent);
772 return NULL;
773}
774
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400775static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100776{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400777 struct nfs4_opendata *p = container_of(kref,
778 struct nfs4_opendata, kref);
779
780 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400781 if (p->state != NULL)
782 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400783 nfs4_put_state_owner(p->owner);
784 dput(p->dir);
Jan Blunck31f31db12008-05-02 13:42:45 -0700785 path_put(&p->path);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400786 kfree(p);
787}
788
789static void nfs4_opendata_put(struct nfs4_opendata *p)
790{
791 if (p != NULL)
792 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100793}
794
Trond Myklebust06f814a2006-01-03 09:55:07 +0100795static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
796{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100797 int ret;
798
Trond Myklebust06f814a2006-01-03 09:55:07 +0100799 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100800 return ret;
801}
802
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500803static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400804{
805 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500806
807 if (open_mode & O_EXCL)
808 goto out;
809 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400810 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500811 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
812 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400813 break;
814 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500815 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
816 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400817 break;
818 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500819 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
820 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400821 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500822out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400823 return ret;
824}
825
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500826static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400827{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500828 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400829 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500830 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400831 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500832 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400833 return 1;
834}
835
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500836static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100837{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500838 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100839 case FMODE_WRITE:
840 state->n_wronly++;
841 break;
842 case FMODE_READ:
843 state->n_rdonly++;
844 break;
845 case FMODE_READ|FMODE_WRITE:
846 state->n_rdwr++;
847 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500848 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100849}
850
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500851static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400852{
853 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
854 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
855 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500856 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400857 case FMODE_READ:
858 set_bit(NFS_O_RDONLY_STATE, &state->flags);
859 break;
860 case FMODE_WRITE:
861 set_bit(NFS_O_WRONLY_STATE, &state->flags);
862 break;
863 case FMODE_READ|FMODE_WRITE:
864 set_bit(NFS_O_RDWR_STATE, &state->flags);
865 }
866}
867
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500868static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400869{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400870 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500871 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400872 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400873}
874
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500875static 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 -0700876{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400877 /*
878 * Protect the call to nfs4_state_set_mode_locked and
879 * serialise the stateid update
880 */
881 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400882 if (deleg_stateid != NULL) {
883 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
884 set_bit(NFS_DELEGATED_STATE, &state->flags);
885 }
886 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500887 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400888 write_sequnlock(&state->seqlock);
889 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500890 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700891 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892}
893
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500894static 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 -0500895{
896 struct nfs_inode *nfsi = NFS_I(state->inode);
897 struct nfs_delegation *deleg_cur;
898 int ret = 0;
899
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500900 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500901
902 rcu_read_lock();
903 deleg_cur = rcu_dereference(nfsi->delegation);
904 if (deleg_cur == NULL)
905 goto no_delegation;
906
907 spin_lock(&deleg_cur->lock);
908 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500909 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500910 goto no_delegation_unlock;
911
912 if (delegation == NULL)
913 delegation = &deleg_cur->stateid;
914 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
915 goto no_delegation_unlock;
916
Trond Myklebustb7391f42008-12-23 15:21:52 -0500917 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500918 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500919 ret = 1;
920no_delegation_unlock:
921 spin_unlock(&deleg_cur->lock);
922no_delegation:
923 rcu_read_unlock();
924
925 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500926 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500927 ret = 1;
928 }
929
930 return ret;
931}
932
933
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500934static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400935{
936 struct nfs_delegation *delegation;
937
938 rcu_read_lock();
939 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500940 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -0400941 rcu_read_unlock();
942 return;
943 }
944 rcu_read_unlock();
945 nfs_inode_return_delegation(inode);
946}
947
Trond Myklebust6ee41262007-07-08 14:11:36 -0400948static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400949{
950 struct nfs4_state *state = opendata->state;
951 struct nfs_inode *nfsi = NFS_I(state->inode);
952 struct nfs_delegation *delegation;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500953 int open_mode = opendata->o_arg.open_flags & O_EXCL;
954 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400955 nfs4_stateid stateid;
956 int ret = -EAGAIN;
957
Trond Myklebustaac00a82007-07-05 19:02:21 -0400958 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500959 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400960 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500961 if (can_open_cached(state, fmode, open_mode)) {
962 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400963 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400964 goto out_return_state;
965 }
966 spin_unlock(&state->owner->so_lock);
967 }
Trond Myklebust34310432008-12-23 15:21:38 -0500968 rcu_read_lock();
969 delegation = rcu_dereference(nfsi->delegation);
970 if (delegation == NULL ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500971 !can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -0500972 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -0400973 break;
Trond Myklebust34310432008-12-23 15:21:38 -0500974 }
Trond Myklebustaac00a82007-07-05 19:02:21 -0400975 /* Save the delegation */
976 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
977 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -0400978 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400979 if (ret != 0)
980 goto out;
981 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -0500982
983 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500984 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -0500985 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400986 }
Trond Myklebustaac00a82007-07-05 19:02:21 -0400987out:
988 return ERR_PTR(ret);
989out_return_state:
990 atomic_inc(&state->count);
991 return state;
992}
993
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100994static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
995{
996 struct inode *inode;
997 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -0400998 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400999 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001000
Trond Myklebustaac00a82007-07-05 19:02:21 -04001001 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001002 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001003 goto out;
1004 }
1005
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001006 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001007 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001008 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001009 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001010 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001011 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001012 goto err;
1013 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001014 state = nfs4_get_open_state(inode, data->owner);
1015 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001016 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001017 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001018 int delegation_flags = 0;
1019
Trond Myklebust003707c2007-07-05 18:07:55 -04001020 rcu_read_lock();
1021 delegation = rcu_dereference(NFS_I(inode)->delegation);
1022 if (delegation)
1023 delegation_flags = delegation->flags;
1024 rcu_read_unlock();
Trond Myklebust15c831b2008-12-23 15:21:39 -05001025 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001026 nfs_inode_set_delegation(state->inode,
1027 data->owner->so_cred,
1028 &data->o_res);
1029 else
1030 nfs_inode_reclaim_delegation(state->inode,
1031 data->owner->so_cred,
1032 &data->o_res);
1033 }
Trond Myklebust34310432008-12-23 15:21:38 -05001034
1035 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001036 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001037 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001038out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001039 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001040err_put_inode:
1041 iput(inode);
1042err:
1043 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001044}
1045
Trond Myklebust864472e2006-01-03 09:55:15 +01001046static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1047{
1048 struct nfs_inode *nfsi = NFS_I(state->inode);
1049 struct nfs_open_context *ctx;
1050
1051 spin_lock(&state->inode->i_lock);
1052 list_for_each_entry(ctx, &nfsi->open_files, list) {
1053 if (ctx->state != state)
1054 continue;
1055 get_nfs_open_context(ctx);
1056 spin_unlock(&state->inode->i_lock);
1057 return ctx;
1058 }
1059 spin_unlock(&state->inode->i_lock);
1060 return ERR_PTR(-ENOENT);
1061}
1062
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001063static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1064{
1065 struct nfs4_opendata *opendata;
1066
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001067 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001068 if (opendata == NULL)
1069 return ERR_PTR(-ENOMEM);
1070 opendata->state = state;
1071 atomic_inc(&state->count);
1072 return opendata;
1073}
1074
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001075static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001076{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001077 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001078 int ret;
1079
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001080 opendata->o_arg.open_flags = 0;
1081 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001082 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1083 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1084 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001085 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001086 if (ret != 0)
1087 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001088 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001089 if (IS_ERR(newstate))
1090 return PTR_ERR(newstate);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001091 nfs4_close_state(&opendata->path, newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001092 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001093 return 0;
1094}
1095
1096static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1097{
Trond Myklebust864472e2006-01-03 09:55:15 +01001098 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001099 int ret;
1100
1101 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001102 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001103 smp_rmb();
1104 if (state->n_rdwr != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001105 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001106 if (ret != 0)
1107 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001108 if (newstate != state)
1109 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001110 }
1111 if (state->n_wronly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001112 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001113 if (ret != 0)
1114 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001115 if (newstate != state)
1116 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001117 }
1118 if (state->n_rdonly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001119 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001120 if (ret != 0)
1121 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001122 if (newstate != state)
1123 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001124 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001125 /*
1126 * We may have performed cached opens for all three recoveries.
1127 * Check if we need to update the current stateid.
1128 */
1129 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1130 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001131 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001132 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1133 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001134 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001135 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001136 return 0;
1137}
1138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139/*
1140 * OPEN_RECLAIM:
1141 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001143static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001145 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001146 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001147 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 int status;
1149
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001150 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1151 if (IS_ERR(opendata))
1152 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001153 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1154 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001155 rcu_read_lock();
1156 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001157 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001158 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001159 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001160 opendata->o_arg.u.delegation_type = delegation_type;
1161 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001162 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 return status;
1164}
1165
Trond Myklebust539cd032007-06-05 11:46:42 -04001166static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167{
1168 struct nfs_server *server = NFS_SERVER(state->inode);
1169 struct nfs4_exception exception = { };
1170 int err;
1171 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001172 err = _nfs4_do_open_reclaim(ctx, state);
Jeff Layton2c643482010-01-07 09:42:03 -05001173 if (err != -NFS4ERR_DELAY && err != -EKEYEXPIRED)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001174 break;
1175 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 } while (exception.retry);
1177 return err;
1178}
1179
Trond Myklebust864472e2006-01-03 09:55:15 +01001180static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1181{
1182 struct nfs_open_context *ctx;
1183 int ret;
1184
1185 ctx = nfs4_state_find_open_context(state);
1186 if (IS_ERR(ctx))
1187 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001188 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001189 put_nfs_open_context(ctx);
1190 return ret;
1191}
1192
Trond Myklebust13437e12007-07-06 15:10:43 -04001193static 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 -07001194{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001195 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001196 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001198 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1199 if (IS_ERR(opendata))
1200 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001201 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -04001202 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001203 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +01001204 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001205 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001206 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207}
1208
Trond Myklebust13437e12007-07-06 15:10:43 -04001209int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210{
1211 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001212 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 int err;
1214 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001215 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 switch (err) {
1217 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001218 case -ENOENT:
1219 case -ESTALE:
1220 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001221 case -NFS4ERR_BADSESSION:
1222 case -NFS4ERR_BADSLOT:
1223 case -NFS4ERR_BAD_HIGH_SLOT:
1224 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1225 case -NFS4ERR_DEADSESSION:
1226 nfs4_schedule_state_recovery(
1227 server->nfs_client);
1228 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 case -NFS4ERR_STALE_CLIENTID:
1230 case -NFS4ERR_STALE_STATEID:
1231 case -NFS4ERR_EXPIRED:
1232 /* Don't recall a delegation if it was lost */
David Howells7539bba2006-08-22 20:06:09 -04001233 nfs4_schedule_state_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001234 goto out;
1235 case -ERESTARTSYS:
1236 /*
1237 * The show must go on: exit, but mark the
1238 * stateid as needing recovery.
1239 */
1240 case -NFS4ERR_ADMIN_REVOKED:
1241 case -NFS4ERR_BAD_STATEID:
1242 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
1243 case -ENOMEM:
1244 err = 0;
1245 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 }
1247 err = nfs4_handle_exception(server, err, &exception);
1248 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001249out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 return err;
1251}
1252
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001253static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1254{
1255 struct nfs4_opendata *data = calldata;
1256
1257 data->rpc_status = task->tk_status;
1258 if (RPC_ASSASSINATED(task))
1259 return;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001260 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001261 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1262 sizeof(data->o_res.stateid.data));
Trond Myklebustbb226292008-01-02 15:19:18 -05001263 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001264 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001265 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001266 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001267}
1268
1269static void nfs4_open_confirm_release(void *calldata)
1270{
1271 struct nfs4_opendata *data = calldata;
1272 struct nfs4_state *state = NULL;
1273
1274 /* If this request hasn't been cancelled, do nothing */
1275 if (data->cancelled == 0)
1276 goto out_free;
1277 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001278 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001279 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001280 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001281 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001282 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001283out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001284 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001285}
1286
1287static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001288 .rpc_call_done = nfs4_open_confirm_done,
1289 .rpc_release = nfs4_open_confirm_release,
1290};
1291
1292/*
1293 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1294 */
1295static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1296{
1297 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1298 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001299 struct rpc_message msg = {
1300 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1301 .rpc_argp = &data->c_arg,
1302 .rpc_resp = &data->c_res,
1303 .rpc_cred = data->owner->so_cred,
1304 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001305 struct rpc_task_setup task_setup_data = {
1306 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001307 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001308 .callback_ops = &nfs4_open_confirm_ops,
1309 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001310 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001311 .flags = RPC_TASK_ASYNC,
1312 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 int status;
1314
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001315 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001316 data->rpc_done = 0;
1317 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001318 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001319 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001320 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001321 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001322 status = nfs4_wait_for_completion_rpc_task(task);
1323 if (status != 0) {
1324 data->cancelled = 1;
1325 smp_wmb();
1326 } else
1327 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001328 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 return status;
1330}
1331
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001332static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001334 struct nfs4_opendata *data = calldata;
1335 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001336
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001337 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1338 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001339 /*
1340 * Check if we still need to send an OPEN call, or if we can use
1341 * a delegation instead.
1342 */
1343 if (data->state != NULL) {
1344 struct nfs_delegation *delegation;
1345
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001346 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001347 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001348 rcu_read_lock();
1349 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1350 if (delegation != NULL &&
Trond Myklebust15c831b2008-12-23 15:21:39 -05001351 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001352 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001353 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001354 }
1355 rcu_read_unlock();
1356 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001357 /* Update sequence id. */
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001358 data->o_arg.id = sp->so_owner_id.id;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001359 data->o_arg.clientid = sp->so_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001360 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001361 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001362 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1363 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001364 data->timestamp = jiffies;
Andy Adamsond8985282009-04-01 09:22:21 -04001365 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
1366 &data->o_arg.seq_args,
1367 &data->o_res.seq_res, 1, task))
1368 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001369 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001370 return;
1371out_no_action:
1372 task->tk_action = NULL;
1373
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001374}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001376static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1377{
1378 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1379 nfs4_open_prepare(task, calldata);
1380}
1381
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001382static void nfs4_open_done(struct rpc_task *task, void *calldata)
1383{
1384 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001386 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001387
Trond Myklebustd61e6122009-12-05 19:32:19 -05001388 nfs4_sequence_done(data->o_arg.server, &data->o_res.seq_res,
1389 task->tk_status);
Andy Adamsond8985282009-04-01 09:22:21 -04001390
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001391 if (RPC_ASSASSINATED(task))
1392 return;
1393 if (task->tk_status == 0) {
1394 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001395 case S_IFREG:
1396 break;
1397 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001398 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001399 break;
1400 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001401 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001402 break;
1403 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001404 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001405 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001406 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001407 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1408 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001409 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001410 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001411}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001412
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001413static void nfs4_open_release(void *calldata)
1414{
1415 struct nfs4_opendata *data = calldata;
1416 struct nfs4_state *state = NULL;
1417
1418 /* If this request hasn't been cancelled, do nothing */
1419 if (data->cancelled == 0)
1420 goto out_free;
1421 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001422 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001423 goto out_free;
1424 /* In case we need an open_confirm, no cleanup! */
1425 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1426 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001427 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001428 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001429 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001430out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001431 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001432}
1433
1434static const struct rpc_call_ops nfs4_open_ops = {
1435 .rpc_call_prepare = nfs4_open_prepare,
1436 .rpc_call_done = nfs4_open_done,
1437 .rpc_release = nfs4_open_release,
1438};
1439
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001440static const struct rpc_call_ops nfs4_recover_open_ops = {
1441 .rpc_call_prepare = nfs4_recover_open_prepare,
1442 .rpc_call_done = nfs4_open_done,
1443 .rpc_release = nfs4_open_release,
1444};
1445
1446static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001447{
1448 struct inode *dir = data->dir->d_inode;
1449 struct nfs_server *server = NFS_SERVER(dir);
1450 struct nfs_openargs *o_arg = &data->o_arg;
1451 struct nfs_openres *o_res = &data->o_res;
1452 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001453 struct rpc_message msg = {
1454 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1455 .rpc_argp = o_arg,
1456 .rpc_resp = o_res,
1457 .rpc_cred = data->owner->so_cred,
1458 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001459 struct rpc_task_setup task_setup_data = {
1460 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001461 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001462 .callback_ops = &nfs4_open_ops,
1463 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001464 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001465 .flags = RPC_TASK_ASYNC,
1466 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001467 int status;
1468
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001469 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001470 data->rpc_done = 0;
1471 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001472 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001473 if (isrecover)
1474 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001475 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001476 if (IS_ERR(task))
1477 return PTR_ERR(task);
1478 status = nfs4_wait_for_completion_rpc_task(task);
1479 if (status != 0) {
1480 data->cancelled = 1;
1481 smp_wmb();
1482 } else
1483 status = data->rpc_status;
1484 rpc_put_task(task);
1485
1486 return status;
1487}
1488
1489static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1490{
1491 struct inode *dir = data->dir->d_inode;
1492 struct nfs_openres *o_res = &data->o_res;
1493 int status;
1494
1495 status = nfs4_run_open_task(data, 1);
1496 if (status != 0 || !data->rpc_done)
1497 return status;
1498
1499 nfs_refresh_inode(dir, o_res->dir_attr);
1500
1501 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1502 status = _nfs4_proc_open_confirm(data);
1503 if (status != 0)
1504 return status;
1505 }
1506
1507 return status;
1508}
1509
1510/*
1511 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1512 */
1513static int _nfs4_proc_open(struct nfs4_opendata *data)
1514{
1515 struct inode *dir = data->dir->d_inode;
1516 struct nfs_server *server = NFS_SERVER(dir);
1517 struct nfs_openargs *o_arg = &data->o_arg;
1518 struct nfs_openres *o_res = &data->o_res;
1519 int status;
1520
1521 status = nfs4_run_open_task(data, 0);
Trond Myklebust3e309912007-07-07 13:19:59 -04001522 if (status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001523 return status;
1524
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001525 if (o_arg->open_flags & O_CREAT) {
1526 update_changeattr(dir, &o_res->cinfo);
1527 nfs_post_op_update_inode(dir, o_res->dir_attr);
1528 } else
1529 nfs_refresh_inode(dir, o_res->dir_attr);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001530 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1531 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001533 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001535 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 }
1537 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001538 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001539 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540}
1541
Trond Myklebust10afec92007-05-14 17:16:04 -04001542static int nfs4_recover_expired_lease(struct nfs_server *server)
Trond Myklebust58d97142006-01-03 09:55:24 +01001543{
David Howells7539bba2006-08-22 20:06:09 -04001544 struct nfs_client *clp = server->nfs_client;
Trond Myklebusta78cb572009-08-09 15:06:19 -04001545 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001546 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001547
Trond Myklebusta78cb572009-08-09 15:06:19 -04001548 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001549 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001550 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001551 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001552 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1553 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001554 break;
Trond Myklebust58d97142006-01-03 09:55:24 +01001555 nfs4_schedule_state_recovery(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001556 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001557 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001558 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001559}
1560
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561/*
1562 * OPEN_EXPIRED:
1563 * reclaim state on the server after a network partition.
1564 * Assumes caller holds the appropriate lock
1565 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001566static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001568 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001569 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001571 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1572 if (IS_ERR(opendata))
1573 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001574 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001575 if (ret == -ESTALE)
Trond Myklebust539cd032007-06-05 11:46:42 -04001576 d_drop(ctx->path.dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001577 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001578 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579}
1580
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001581static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001582{
Trond Myklebust539cd032007-06-05 11:46:42 -04001583 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001584 struct nfs4_exception exception = { };
1585 int err;
1586
1587 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001588 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001589 switch (err) {
1590 default:
1591 goto out;
1592 case -NFS4ERR_GRACE:
1593 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -05001594 case -EKEYEXPIRED:
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001595 nfs4_handle_exception(server, err, &exception);
1596 err = 0;
1597 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001598 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001599out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001600 return err;
1601}
1602
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1604{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001606 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
Trond Myklebust864472e2006-01-03 09:55:15 +01001608 ctx = nfs4_state_find_open_context(state);
1609 if (IS_ERR(ctx))
1610 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001611 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001612 put_nfs_open_context(ctx);
1613 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614}
1615
1616/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001617 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1618 * fields corresponding to attributes that were used to store the verifier.
1619 * Make sure we clobber those fields in the later setattr call
1620 */
1621static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1622{
1623 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1624 !(sattr->ia_valid & ATTR_ATIME_SET))
1625 sattr->ia_valid |= ATTR_ATIME;
1626
1627 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1628 !(sattr->ia_valid & ATTR_MTIME_SET))
1629 sattr->ia_valid |= ATTR_MTIME;
1630}
1631
1632/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001633 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001635static 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 -07001636{
1637 struct nfs4_state_owner *sp;
1638 struct nfs4_state *state = NULL;
1639 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001640 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001641 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
1643 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 status = -ENOMEM;
1645 if (!(sp = nfs4_get_state_owner(server, cred))) {
1646 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1647 goto out_err;
1648 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001649 status = nfs4_recover_expired_lease(server);
1650 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001651 goto err_put_state_owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001652 if (path->dentry->d_inode != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001653 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001654 status = -ENOMEM;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001655 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001656 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001657 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
Trond Myklebustaac00a82007-07-05 19:02:21 -04001659 if (path->dentry->d_inode != NULL)
1660 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1661
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001662 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001664 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001666 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001667 status = PTR_ERR(state);
1668 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001669 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001670 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001671 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001672
1673 if (opendata->o_arg.open_flags & O_EXCL) {
1674 nfs4_exclusive_attrset(opendata, sattr);
1675
1676 nfs_fattr_init(opendata->o_res.f_attr);
1677 status = nfs4_do_setattr(state->inode, cred,
1678 opendata->o_res.f_attr, sattr,
1679 state);
1680 if (status == 0)
1681 nfs_setattr_update_inode(state->inode, sattr);
1682 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1683 }
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001684 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 *res = state;
1687 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001688err_opendata_put:
1689 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001690err_put_state_owner:
1691 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 *res = NULL;
1694 return status;
1695}
1696
1697
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001698static 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 -07001699{
1700 struct nfs4_exception exception = { };
1701 struct nfs4_state *res;
1702 int status;
1703
1704 do {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001705 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 if (status == 0)
1707 break;
1708 /* NOTE: BAD_SEQID means the server and client disagree about the
1709 * book-keeping w.r.t. state-changing operations
1710 * (OPEN/CLOSE/LOCK/LOCKU...)
1711 * It is actually a sign of a bug on the client or on the server.
1712 *
1713 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001714 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 * have unhashed the old state_owner for us, and that we can
1716 * therefore safely retry using a new one. We should still warn
1717 * the user though...
1718 */
1719 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001720 printk(KERN_WARNING "NFS: v4 server %s "
1721 " returned a bad sequence-id error!\n",
1722 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 exception.retry = 1;
1724 continue;
1725 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001726 /*
1727 * BAD_STATEID on OPEN means that the server cancelled our
1728 * state before it received the OPEN_CONFIRM.
1729 * Recover by retrying the request as per the discussion
1730 * on Page 181 of RFC3530.
1731 */
1732 if (status == -NFS4ERR_BAD_STATEID) {
1733 exception.retry = 1;
1734 continue;
1735 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001736 if (status == -EAGAIN) {
1737 /* We must have found a delegation */
1738 exception.retry = 1;
1739 continue;
1740 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1742 status, &exception));
1743 } while (exception.retry);
1744 return res;
1745}
1746
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001747static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1748 struct nfs_fattr *fattr, struct iattr *sattr,
1749 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001751 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001753 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 .iap = sattr,
1755 .server = server,
1756 .bitmask = server->attr_bitmask,
1757 };
1758 struct nfs_setattrres res = {
1759 .fattr = fattr,
1760 .server = server,
1761 };
1762 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001763 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1764 .rpc_argp = &arg,
1765 .rpc_resp = &res,
1766 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001768 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001769 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
Trond Myklebust0e574af2005-10-27 22:12:38 -04001771 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001773 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1774 /* Use that stateid */
1775 } else if (state != NULL) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001776 nfs4_copy_stateid(&arg.stateid, state, current->files);
1777 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1779
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001780 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001781 if (status == 0 && state != NULL)
1782 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001783 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784}
1785
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001786static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1787 struct nfs_fattr *fattr, struct iattr *sattr,
1788 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001790 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 struct nfs4_exception exception = { };
1792 int err;
1793 do {
1794 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001795 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 &exception);
1797 } while (exception.retry);
1798 return err;
1799}
1800
1801struct nfs4_closedata {
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001802 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 struct inode *inode;
1804 struct nfs4_state *state;
1805 struct nfs_closeargs arg;
1806 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001807 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001808 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809};
1810
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001811static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001812{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001813 struct nfs4_closedata *calldata = data;
1814 struct nfs4_state_owner *sp = calldata->state->owner;
Trond Myklebust95121352005-10-18 14:20:12 -07001815
1816 nfs4_put_open_state(calldata->state);
1817 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001818 nfs4_put_state_owner(sp);
Jan Blunck31f31db12008-05-02 13:42:45 -07001819 path_put(&calldata->path);
Trond Myklebust95121352005-10-18 14:20:12 -07001820 kfree(calldata);
1821}
1822
Trond Myklebust88069f72009-12-08 08:33:16 -05001823static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1824 fmode_t fmode)
1825{
1826 spin_lock(&state->owner->so_lock);
1827 if (!(fmode & FMODE_READ))
1828 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1829 if (!(fmode & FMODE_WRITE))
1830 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1831 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1832 spin_unlock(&state->owner->so_lock);
1833}
1834
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001835static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001837 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 struct nfs_server *server = NFS_SERVER(calldata->inode);
1840
Andy Adamson19ddab02009-04-01 09:22:20 -04001841 nfs4_sequence_done(server, &calldata->res.seq_res, task->tk_status);
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001842 if (RPC_ASSASSINATED(task))
1843 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 /* hmm. we are done with the inode, and in the process of freeing
1845 * the state_owner. we keep this around to process errors
1846 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 switch (task->tk_status) {
1848 case 0:
Trond Myklebust45328c32007-07-26 17:47:34 -04001849 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001850 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05001851 nfs4_close_clear_stateid_flags(state,
1852 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 break;
1854 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001855 case -NFS4ERR_OLD_STATEID:
1856 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001858 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001859 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05001861 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1862 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 }
Trond Myklebust72211db2009-12-15 14:47:36 -05001864 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04001865 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866}
1867
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001868static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001870 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001871 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05001872 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07001873
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001874 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001875 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001876
Trond Myklebust88069f72009-12-08 08:33:16 -05001877 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1878 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05001879 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001880 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01001881 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001882 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001883 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
1884 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1885 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04001886 }
1887 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001888 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
1889 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1890 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04001891 }
Trond Myklebuste7616922006-01-03 09:55:13 +01001892 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05001893 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05001894
1895 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001896 /* Note: exit _without_ calling nfs4_close_done */
1897 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07001898 return;
1899 }
Trond Myklebust88069f72009-12-08 08:33:16 -05001900
1901 if (calldata->arg.fmode == 0)
1902 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
1903
Trond Myklebust516a6af2005-10-27 22:12:41 -04001904 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001905 calldata->timestamp = jiffies;
Andy Adamson19ddab02009-04-01 09:22:20 -04001906 if (nfs4_setup_sequence((NFS_SERVER(calldata->inode))->nfs_client,
1907 &calldata->arg.seq_args, &calldata->res.seq_res,
1908 1, task))
1909 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001910 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911}
1912
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001913static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001914 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001915 .rpc_call_done = nfs4_close_done,
1916 .rpc_release = nfs4_free_closedata,
1917};
1918
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919/*
1920 * It is possible for data to be read/written from a mem-mapped file
1921 * after the sys_close call (which hits the vfs layer as a flush).
1922 * This means that we can't safely call nfsv4 close on a file until
1923 * the inode is cleared. This in turn means that we are not good
1924 * NFSv4 citizens - we do not indicate to the server to update the file's
1925 * share state even when we are done with one of the three share
1926 * stateid's in the inode.
1927 *
1928 * NOTE: Caller must be holding the sp->so_owner semaphore!
1929 */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001930int nfs4_do_close(struct path *path, struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001932 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04001934 struct nfs4_state_owner *sp = state->owner;
1935 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001936 struct rpc_message msg = {
1937 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1938 .rpc_cred = state->owner->so_cred,
1939 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001940 struct rpc_task_setup task_setup_data = {
1941 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001942 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001943 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05001944 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001945 .flags = RPC_TASK_ASYNC,
1946 };
Trond Myklebust95121352005-10-18 14:20:12 -07001947 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001949 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07001951 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001952 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001954 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04001955 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001957 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07001958 if (calldata->arg.seqid == NULL)
1959 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001960 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04001961 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001962 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04001963 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001964 calldata->res.server = server;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04001965 calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Al Virof6948692010-01-30 13:51:04 -05001966 path_get(path);
1967 calldata->path = *path;
Trond Myklebust95121352005-10-18 14:20:12 -07001968
Trond Myklebust5138fde2007-07-14 15:40:01 -04001969 msg.rpc_argp = &calldata->arg,
1970 msg.rpc_resp = &calldata->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001971 task_setup_data.callback_data = calldata;
1972 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001973 if (IS_ERR(task))
1974 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001975 status = 0;
1976 if (wait)
1977 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001978 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001979 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07001980out_free_calldata:
1981 kfree(calldata);
1982out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04001983 nfs4_put_open_state(state);
1984 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07001985 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986}
1987
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001988static 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 -07001989{
1990 struct file *filp;
Trond Myklebust1b45c462007-07-03 13:04:56 -04001991 int ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001992
Trond Myklebust1b45c462007-07-03 13:04:56 -04001993 /* If the open_intent is for execute, we have an extra check to make */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001994 if (fmode & FMODE_EXEC) {
Trond Myklebustaf22f942007-08-10 17:45:10 -04001995 ret = nfs_may_open(state->inode,
Trond Myklebust1b45c462007-07-03 13:04:56 -04001996 state->owner->so_cred,
1997 nd->intent.open.flags);
1998 if (ret < 0)
1999 goto out_close;
2000 }
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002001 filp = lookup_instantiate_filp(nd, path->dentry, NULL);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002002 if (!IS_ERR(filp)) {
2003 struct nfs_open_context *ctx;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04002004 ctx = nfs_file_open_context(filp);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002005 ctx->state = state;
Trond Myklebust95cf9592006-04-18 13:14:06 -04002006 return 0;
2007 }
Trond Myklebust1b45c462007-07-03 13:04:56 -04002008 ret = PTR_ERR(filp);
2009out_close:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002010 nfs4_close_sync(path, state, fmode & (FMODE_READ|FMODE_WRITE));
Trond Myklebust1b45c462007-07-03 13:04:56 -04002011 return ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002012}
2013
2014struct dentry *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
2016{
Trond Myklebustad389da2007-06-05 12:30:00 -04002017 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08002018 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04002019 .dentry = dentry,
2020 };
Jan Blunck4ac91372008-02-14 19:34:32 -08002021 struct dentry *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 struct iattr attr;
2023 struct rpc_cred *cred;
2024 struct nfs4_state *state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002025 struct dentry *res;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002026 fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027
2028 if (nd->flags & LOOKUP_CREATE) {
2029 attr.ia_mode = nd->intent.open.create_mode;
2030 attr.ia_valid = ATTR_MODE;
2031 if (!IS_POSIXACL(dir))
Al Viroce3b0f82009-03-29 19:08:22 -04002032 attr.ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 } else {
2034 attr.ia_valid = 0;
2035 BUG_ON(nd->intent.open.flags & O_CREAT);
2036 }
2037
Trond Myklebust98a8e322008-03-12 12:25:28 -04002038 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 if (IS_ERR(cred))
Trond Myklebust02a913a2005-10-18 14:20:17 -07002040 return (struct dentry *)cred;
Trond Myklebust565277f2007-10-15 18:17:53 -04002041 parent = dentry->d_parent;
2042 /* Protect against concurrent sillydeletes */
2043 nfs_block_sillyrename(parent);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002044 state = nfs4_do_open(dir, &path, fmode, nd->intent.open.flags, &attr, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002046 if (IS_ERR(state)) {
Trond Myklebustd75340c2007-10-01 21:42:01 -04002047 if (PTR_ERR(state) == -ENOENT) {
Trond Myklebust02a913a2005-10-18 14:20:17 -07002048 d_add(dentry, NULL);
Trond Myklebustd75340c2007-10-01 21:42:01 -04002049 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2050 }
Trond Myklebust565277f2007-10-15 18:17:53 -04002051 nfs_unblock_sillyrename(parent);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002052 return (struct dentry *)state;
2053 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002054 res = d_add_unique(dentry, igrab(state->inode));
Trond Myklebust02a913a2005-10-18 14:20:17 -07002055 if (res != NULL)
Trond Myklebustdeee9362007-08-27 11:33:00 -04002056 path.dentry = res;
Trond Myklebustd75340c2007-10-01 21:42:01 -04002057 nfs_set_verifier(path.dentry, nfs_save_change_attribute(dir));
Trond Myklebust565277f2007-10-15 18:17:53 -04002058 nfs_unblock_sillyrename(parent);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002059 nfs4_intent_set_file(nd, &path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002060 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061}
2062
2063int
Trond Myklebust02a913a2005-10-18 14:20:17 -07002064nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065{
Trond Myklebustad389da2007-06-05 12:30:00 -04002066 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08002067 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04002068 .dentry = dentry,
2069 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 struct rpc_cred *cred;
2071 struct nfs4_state *state;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002072 fmode_t fmode = openflags & (FMODE_READ | FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073
Trond Myklebust98a8e322008-03-12 12:25:28 -04002074 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 if (IS_ERR(cred))
2076 return PTR_ERR(cred);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002077 state = nfs4_do_open(dir, &path, fmode, openflags, NULL, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002079 if (IS_ERR(state)) {
2080 switch (PTR_ERR(state)) {
2081 case -EPERM:
2082 case -EACCES:
2083 case -EDQUOT:
2084 case -ENOSPC:
2085 case -EROFS:
Al Viro04287f92010-04-08 00:06:07 +01002086 return PTR_ERR(state);
Chuck Leverb87c0ad2006-10-19 23:28:42 -07002087 default:
2088 goto out_drop;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002089 }
Trond Myklebust02a913a2005-10-18 14:20:17 -07002090 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002091 if (state->inode == dentry->d_inode) {
Trond Myklebustd75340c2007-10-01 21:42:01 -04002092 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002093 nfs4_intent_set_file(nd, &path, state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 return 1;
2095 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002096 nfs4_close_sync(&path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002097out_drop:
2098 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 return 0;
2100}
2101
Trond Myklebust1185a552009-12-03 15:54:02 -05002102static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002103{
2104 if (ctx->state == NULL)
2105 return;
2106 if (is_sync)
2107 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
2108 else
2109 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
2110}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111
2112static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2113{
Benny Halevy43652ad2009-04-01 09:21:54 -04002114 struct nfs4_server_caps_arg args = {
2115 .fhandle = fhandle,
2116 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 struct nfs4_server_caps_res res = {};
2118 struct rpc_message msg = {
2119 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002120 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 .rpc_resp = &res,
2122 };
2123 int status;
2124
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002125 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 if (status == 0) {
2127 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002128 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2129 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2130 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2131 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2132 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2134 server->caps |= NFS_CAP_ACLS;
2135 if (res.has_links != 0)
2136 server->caps |= NFS_CAP_HARDLINKS;
2137 if (res.has_symlinks != 0)
2138 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002139 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2140 server->caps |= NFS_CAP_FILEID;
2141 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2142 server->caps |= NFS_CAP_MODE;
2143 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2144 server->caps |= NFS_CAP_NLINK;
2145 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2146 server->caps |= NFS_CAP_OWNER;
2147 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2148 server->caps |= NFS_CAP_OWNER_GROUP;
2149 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2150 server->caps |= NFS_CAP_ATIME;
2151 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2152 server->caps |= NFS_CAP_CTIME;
2153 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2154 server->caps |= NFS_CAP_MTIME;
2155
Trond Myklebusta65318b2009-03-11 14:10:28 -04002156 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2157 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2158 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 server->acl_bitmask = res.acl_bitmask;
2160 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002161
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 return status;
2163}
2164
Trond Myklebust55a97592006-06-09 09:34:19 -04002165int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166{
2167 struct nfs4_exception exception = { };
2168 int err;
2169 do {
2170 err = nfs4_handle_exception(server,
2171 _nfs4_server_capabilities(server, fhandle),
2172 &exception);
2173 } while (exception.retry);
2174 return err;
2175}
2176
2177static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2178 struct nfs_fsinfo *info)
2179{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 struct nfs4_lookup_root_arg args = {
2181 .bitmask = nfs4_fattr_bitmap,
2182 };
2183 struct nfs4_lookup_res res = {
2184 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002185 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 .fh = fhandle,
2187 };
2188 struct rpc_message msg = {
2189 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2190 .rpc_argp = &args,
2191 .rpc_resp = &res,
2192 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002193
Trond Myklebust0e574af2005-10-27 22:12:38 -04002194 nfs_fattr_init(info->fattr);
Trond Myklebustfccba802009-07-21 16:48:07 -04002195 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196}
2197
2198static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2199 struct nfs_fsinfo *info)
2200{
2201 struct nfs4_exception exception = { };
2202 int err;
2203 do {
2204 err = nfs4_handle_exception(server,
2205 _nfs4_lookup_root(server, fhandle, info),
2206 &exception);
2207 } while (exception.retry);
2208 return err;
2209}
2210
David Howells54ceac42006-08-22 20:06:13 -04002211/*
2212 * get the file handle for the "/" directory on the server
2213 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002215 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 int status;
2218
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 status = nfs4_lookup_root(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 if (status == 0)
2221 status = nfs4_server_capabilities(server, fhandle);
2222 if (status == 0)
2223 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002224 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225}
2226
Manoj Naik6b97fd32006-06-09 09:34:29 -04002227/*
2228 * Get locations and (maybe) other attributes of a referral.
2229 * Note that we'll actually follow the referral later when
2230 * we detect fsid mismatch in inode revalidation
2231 */
Trond Myklebust56659e92007-07-17 21:52:39 -04002232static 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 -04002233{
2234 int status = -ENOMEM;
2235 struct page *page = NULL;
2236 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002237
2238 page = alloc_page(GFP_KERNEL);
2239 if (page == NULL)
2240 goto out;
2241 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2242 if (locations == NULL)
2243 goto out;
2244
Trond Myklebustc228fd32007-01-13 02:28:11 -05002245 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002246 if (status != 0)
2247 goto out;
2248 /* Make sure server returned a different fsid for the referral */
2249 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -07002250 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 -04002251 status = -EIO;
2252 goto out;
2253 }
2254
2255 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2256 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
2257 if (!fattr->mode)
2258 fattr->mode = S_IFDIR;
2259 memset(fhandle, 0, sizeof(struct nfs_fh));
2260out:
2261 if (page)
2262 __free_page(page);
2263 if (locations)
2264 kfree(locations);
2265 return status;
2266}
2267
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2269{
2270 struct nfs4_getattr_arg args = {
2271 .fh = fhandle,
2272 .bitmask = server->attr_bitmask,
2273 };
2274 struct nfs4_getattr_res res = {
2275 .fattr = fattr,
2276 .server = server,
2277 };
2278 struct rpc_message msg = {
2279 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2280 .rpc_argp = &args,
2281 .rpc_resp = &res,
2282 };
2283
Trond Myklebust0e574af2005-10-27 22:12:38 -04002284 nfs_fattr_init(fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002285 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286}
2287
2288static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2289{
2290 struct nfs4_exception exception = { };
2291 int err;
2292 do {
2293 err = nfs4_handle_exception(server,
2294 _nfs4_proc_getattr(server, fhandle, fattr),
2295 &exception);
2296 } while (exception.retry);
2297 return err;
2298}
2299
2300/*
2301 * The file is not closed if it is opened due to the a request to change
2302 * the size of the file. The open call will not be needed once the
2303 * VFS layer lookup-intents are implemented.
2304 *
2305 * Close is called when the inode is destroyed.
2306 * If we haven't opened the file for O_WRONLY, we
2307 * need to in the size_change case to obtain a stateid.
2308 *
2309 * Got race?
2310 * Because OPEN is always done by name in nfsv4, it is
2311 * possible that we opened a different file by the same
2312 * name. We can recognize this race condition, but we
2313 * can't do anything about it besides returning an error.
2314 *
2315 * This will be fixed with VFS changes (lookup-intent).
2316 */
2317static int
2318nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2319 struct iattr *sattr)
2320{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002321 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002322 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002323 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 int status;
2325
Trond Myklebust0e574af2005-10-27 22:12:38 -04002326 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327
Trond Myklebustd5308382005-11-04 15:33:38 -05002328 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002329 if (sattr->ia_valid & ATTR_FILE) {
2330 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002331
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002332 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002333 if (ctx) {
2334 cred = ctx->cred;
2335 state = ctx->state;
2336 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002337 }
2338
2339 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002340 if (status == 0)
2341 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 return status;
2343}
2344
Trond Myklebust56659e92007-07-17 21:52:39 -04002345static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
2346 const struct qstr *name, struct nfs_fh *fhandle,
David Howells2b3de442006-08-22 20:06:09 -04002347 struct nfs_fattr *fattr)
2348{
2349 int status;
2350 struct nfs4_lookup_arg args = {
2351 .bitmask = server->attr_bitmask,
2352 .dir_fh = dirfh,
2353 .name = name,
2354 };
2355 struct nfs4_lookup_res res = {
2356 .server = server,
2357 .fattr = fattr,
2358 .fh = fhandle,
2359 };
2360 struct rpc_message msg = {
2361 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2362 .rpc_argp = &args,
2363 .rpc_resp = &res,
2364 };
2365
2366 nfs_fattr_init(fattr);
2367
2368 dprintk("NFS call lookupfh %s\n", name->name);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002369 status = nfs4_call_sync(server, &msg, &args, &res, 0);
David Howells2b3de442006-08-22 20:06:09 -04002370 dprintk("NFS reply lookupfh: %d\n", status);
David Howells2b3de442006-08-22 20:06:09 -04002371 return status;
2372}
2373
2374static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2375 struct qstr *name, struct nfs_fh *fhandle,
2376 struct nfs_fattr *fattr)
2377{
2378 struct nfs4_exception exception = { };
2379 int err;
2380 do {
Trond Myklebust4e56e082007-07-01 18:13:52 -04002381 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
2382 /* FIXME: !!!! */
2383 if (err == -NFS4ERR_MOVED) {
2384 err = -EREMOTE;
2385 break;
2386 }
2387 err = nfs4_handle_exception(server, err, &exception);
David Howells2b3de442006-08-22 20:06:09 -04002388 } while (exception.retry);
2389 return err;
2390}
2391
Trond Myklebust56659e92007-07-17 21:52:39 -04002392static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2394{
Trond Myklebust4e56e082007-07-01 18:13:52 -04002395 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396
2397 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust4e56e082007-07-01 18:13:52 -04002398 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002399 if (status == -NFS4ERR_MOVED)
2400 status = nfs4_get_referral(dir, name, fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 dprintk("NFS reply lookup: %d\n", status);
2402 return status;
2403}
2404
2405static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2406{
2407 struct nfs4_exception exception = { };
2408 int err;
2409 do {
2410 err = nfs4_handle_exception(NFS_SERVER(dir),
2411 _nfs4_proc_lookup(dir, name, fhandle, fattr),
2412 &exception);
2413 } while (exception.retry);
2414 return err;
2415}
2416
2417static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2418{
Trond Myklebust76b32992007-08-10 17:45:11 -04002419 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 struct nfs4_accessargs args = {
2421 .fh = NFS_FH(inode),
Trond Myklebust76b32992007-08-10 17:45:11 -04002422 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002424 struct nfs4_accessres res = {
2425 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002426 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 struct rpc_message msg = {
2428 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2429 .rpc_argp = &args,
2430 .rpc_resp = &res,
2431 .rpc_cred = entry->cred,
2432 };
2433 int mode = entry->mask;
2434 int status;
2435
2436 /*
2437 * Determine which access bits we want to ask for...
2438 */
2439 if (mode & MAY_READ)
2440 args.access |= NFS4_ACCESS_READ;
2441 if (S_ISDIR(inode->i_mode)) {
2442 if (mode & MAY_WRITE)
2443 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2444 if (mode & MAY_EXEC)
2445 args.access |= NFS4_ACCESS_LOOKUP;
2446 } else {
2447 if (mode & MAY_WRITE)
2448 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2449 if (mode & MAY_EXEC)
2450 args.access |= NFS4_ACCESS_EXECUTE;
2451 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002452
2453 res.fattr = nfs_alloc_fattr();
2454 if (res.fattr == NULL)
2455 return -ENOMEM;
2456
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002457 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 if (!status) {
2459 entry->mask = 0;
2460 if (res.access & NFS4_ACCESS_READ)
2461 entry->mask |= MAY_READ;
2462 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2463 entry->mask |= MAY_WRITE;
2464 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2465 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002466 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002468 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 return status;
2470}
2471
2472static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2473{
2474 struct nfs4_exception exception = { };
2475 int err;
2476 do {
2477 err = nfs4_handle_exception(NFS_SERVER(inode),
2478 _nfs4_proc_access(inode, entry),
2479 &exception);
2480 } while (exception.retry);
2481 return err;
2482}
2483
2484/*
2485 * TODO: For the time being, we don't try to get any attributes
2486 * along with any of the zero-copy operations READ, READDIR,
2487 * READLINK, WRITE.
2488 *
2489 * In the case of the first three, we want to put the GETATTR
2490 * after the read-type operation -- this is because it is hard
2491 * to predict the length of a GETATTR response in v4, and thus
2492 * align the READ data correctly. This means that the GETATTR
2493 * may end up partially falling into the page cache, and we should
2494 * shift it into the 'tail' of the xdr_buf before processing.
2495 * To do this efficiently, we need to know the total length
2496 * of data received, which doesn't seem to be available outside
2497 * of the RPC layer.
2498 *
2499 * In the case of WRITE, we also want to put the GETATTR after
2500 * the operation -- in this case because we want to make sure
2501 * we get the post-operation mtime and size. This means that
2502 * we can't use xdr_encode_pages() as written: we need a variant
2503 * of it which would leave room in the 'tail' iovec.
2504 *
2505 * Both of these changes to the XDR layer would in fact be quite
2506 * minor, but I decided to leave them for a subsequent patch.
2507 */
2508static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2509 unsigned int pgbase, unsigned int pglen)
2510{
2511 struct nfs4_readlink args = {
2512 .fh = NFS_FH(inode),
2513 .pgbase = pgbase,
2514 .pglen = pglen,
2515 .pages = &page,
2516 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002517 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 struct rpc_message msg = {
2519 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2520 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002521 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 };
2523
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002524 return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525}
2526
2527static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2528 unsigned int pgbase, unsigned int pglen)
2529{
2530 struct nfs4_exception exception = { };
2531 int err;
2532 do {
2533 err = nfs4_handle_exception(NFS_SERVER(inode),
2534 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2535 &exception);
2536 } while (exception.retry);
2537 return err;
2538}
2539
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540/*
2541 * Got race?
2542 * We will need to arrange for the VFS layer to provide an atomic open.
2543 * Until then, this create/open method is prone to inefficiency and race
2544 * conditions due to the lookup, create, and open VFS calls from sys_open()
2545 * placed on the wire.
2546 *
2547 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2548 * The file will be opened again in the subsequent VFS open call
2549 * (nfs4_proc_file_open).
2550 *
2551 * The open for read will just hang around to be used by any process that
2552 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2553 */
2554
2555static int
2556nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebust02a913a2005-10-18 14:20:17 -07002557 int flags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558{
Trond Myklebustad389da2007-06-05 12:30:00 -04002559 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08002560 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04002561 .dentry = dentry,
2562 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 struct nfs4_state *state;
2564 struct rpc_cred *cred;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002565 fmode_t fmode = flags & (FMODE_READ | FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 int status = 0;
2567
Trond Myklebust98a8e322008-03-12 12:25:28 -04002568 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 if (IS_ERR(cred)) {
2570 status = PTR_ERR(cred);
2571 goto out;
2572 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002573 state = nfs4_do_open(dir, &path, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002574 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 if (IS_ERR(state)) {
2576 status = PTR_ERR(state);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002577 goto out_putcred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002579 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002580 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustad389da2007-06-05 12:30:00 -04002581 if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002582 status = nfs4_intent_set_file(nd, &path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002583 else
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002584 nfs4_close_sync(&path, state, fmode);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002585out_putcred:
2586 put_rpccred(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587out:
2588 return status;
2589}
2590
2591static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2592{
Trond Myklebust16e42952005-10-27 22:12:44 -04002593 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002594 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002596 .name.len = name->len,
2597 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002598 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002600 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002601 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002602 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002604 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2605 .rpc_argp = &args,
2606 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 };
Trond Myklebustd3468902010-04-16 16:22:50 -04002608 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609
Trond Myklebustd3468902010-04-16 16:22:50 -04002610 res.dir_attr = nfs_alloc_fattr();
2611 if (res.dir_attr == NULL)
2612 goto out;
2613
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002614 status = nfs4_call_sync(server, &msg, &args, &res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002615 if (status == 0) {
2616 update_changeattr(dir, &res.cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002617 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002618 }
Trond Myklebustd3468902010-04-16 16:22:50 -04002619 nfs_free_fattr(res.dir_attr);
2620out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 return status;
2622}
2623
2624static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2625{
2626 struct nfs4_exception exception = { };
2627 int err;
2628 do {
2629 err = nfs4_handle_exception(NFS_SERVER(dir),
2630 _nfs4_proc_remove(dir, name),
2631 &exception);
2632 } while (exception.retry);
2633 return err;
2634}
2635
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002636static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002638 struct nfs_server *server = NFS_SERVER(dir);
2639 struct nfs_removeargs *args = msg->rpc_argp;
2640 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641
Trond Myklebusta65318b2009-03-11 14:10:28 -04002642 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002643 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645}
2646
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002647static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002649 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2650
Andy Adamson472cfbd2009-04-01 09:22:24 -04002651 nfs4_sequence_done(res->server, &res->seq_res, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002652 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002653 return 0;
2654 update_changeattr(dir, &res->cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002655 nfs_post_op_update_inode(dir, res->dir_attr);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002656 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657}
2658
2659static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2660 struct inode *new_dir, struct qstr *new_name)
2661{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002662 struct nfs_server *server = NFS_SERVER(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 struct nfs4_rename_arg arg = {
2664 .old_dir = NFS_FH(old_dir),
2665 .new_dir = NFS_FH(new_dir),
2666 .old_name = old_name,
2667 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002668 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 };
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002670 struct nfs4_rename_res res = {
2671 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002672 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 struct rpc_message msg = {
2674 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2675 .rpc_argp = &arg,
2676 .rpc_resp = &res,
2677 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002678 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679
Trond Myklebust011fff72010-04-16 16:22:49 -04002680 res.old_fattr = nfs_alloc_fattr();
2681 res.new_fattr = nfs_alloc_fattr();
2682 if (res.old_fattr == NULL || res.new_fattr == NULL)
2683 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684
Trond Myklebust011fff72010-04-16 16:22:49 -04002685 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 if (!status) {
2687 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002688 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002690 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 }
Trond Myklebust011fff72010-04-16 16:22:49 -04002692out:
2693 nfs_free_fattr(res.new_fattr);
2694 nfs_free_fattr(res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 return status;
2696}
2697
2698static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2699 struct inode *new_dir, struct qstr *new_name)
2700{
2701 struct nfs4_exception exception = { };
2702 int err;
2703 do {
2704 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2705 _nfs4_proc_rename(old_dir, old_name,
2706 new_dir, new_name),
2707 &exception);
2708 } while (exception.retry);
2709 return err;
2710}
2711
2712static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2713{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002714 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 struct nfs4_link_arg arg = {
2716 .fh = NFS_FH(inode),
2717 .dir_fh = NFS_FH(dir),
2718 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002719 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002721 struct nfs4_link_res res = {
2722 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002723 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 struct rpc_message msg = {
2725 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2726 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002727 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 };
Trond Myklebust136f2622010-04-16 16:22:49 -04002729 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730
Trond Myklebust136f2622010-04-16 16:22:49 -04002731 res.fattr = nfs_alloc_fattr();
2732 res.dir_attr = nfs_alloc_fattr();
2733 if (res.fattr == NULL || res.dir_attr == NULL)
2734 goto out;
2735
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002736 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002737 if (!status) {
2738 update_changeattr(dir, &res.cinfo);
2739 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002740 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002741 }
Trond Myklebust136f2622010-04-16 16:22:49 -04002742out:
2743 nfs_free_fattr(res.dir_attr);
2744 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 return status;
2746}
2747
2748static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2749{
2750 struct nfs4_exception exception = { };
2751 int err;
2752 do {
2753 err = nfs4_handle_exception(NFS_SERVER(inode),
2754 _nfs4_proc_link(inode, dir, name),
2755 &exception);
2756 } while (exception.retry);
2757 return err;
2758}
2759
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002760struct nfs4_createdata {
2761 struct rpc_message msg;
2762 struct nfs4_create_arg arg;
2763 struct nfs4_create_res res;
2764 struct nfs_fh fh;
2765 struct nfs_fattr fattr;
2766 struct nfs_fattr dir_fattr;
2767};
2768
2769static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2770 struct qstr *name, struct iattr *sattr, u32 ftype)
2771{
2772 struct nfs4_createdata *data;
2773
2774 data = kzalloc(sizeof(*data), GFP_KERNEL);
2775 if (data != NULL) {
2776 struct nfs_server *server = NFS_SERVER(dir);
2777
2778 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2779 data->msg.rpc_argp = &data->arg;
2780 data->msg.rpc_resp = &data->res;
2781 data->arg.dir_fh = NFS_FH(dir);
2782 data->arg.server = server;
2783 data->arg.name = name;
2784 data->arg.attrs = sattr;
2785 data->arg.ftype = ftype;
2786 data->arg.bitmask = server->attr_bitmask;
2787 data->res.server = server;
2788 data->res.fh = &data->fh;
2789 data->res.fattr = &data->fattr;
2790 data->res.dir_fattr = &data->dir_fattr;
2791 nfs_fattr_init(data->res.fattr);
2792 nfs_fattr_init(data->res.dir_fattr);
2793 }
2794 return data;
2795}
2796
2797static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2798{
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002799 int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
2800 &data->arg, &data->res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002801 if (status == 0) {
2802 update_changeattr(dir, &data->res.dir_cinfo);
2803 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2804 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2805 }
2806 return status;
2807}
2808
2809static void nfs4_free_createdata(struct nfs4_createdata *data)
2810{
2811 kfree(data);
2812}
2813
Chuck Lever4f390c12006-08-22 20:06:22 -04002814static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002815 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002817 struct nfs4_createdata *data;
2818 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819
Chuck Lever94a6d752006-08-22 20:06:23 -04002820 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002821 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002822
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002823 status = -ENOMEM;
2824 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2825 if (data == NULL)
2826 goto out;
2827
2828 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2829 data->arg.u.symlink.pages = &page;
2830 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002832 status = nfs4_do_create(dir, dentry, data);
2833
2834 nfs4_free_createdata(data);
2835out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 return status;
2837}
2838
Chuck Lever4f390c12006-08-22 20:06:22 -04002839static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002840 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841{
2842 struct nfs4_exception exception = { };
2843 int err;
2844 do {
2845 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002846 _nfs4_proc_symlink(dir, dentry, page,
2847 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 &exception);
2849 } while (exception.retry);
2850 return err;
2851}
2852
2853static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2854 struct iattr *sattr)
2855{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002856 struct nfs4_createdata *data;
2857 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002859 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2860 if (data == NULL)
2861 goto out;
2862
2863 status = nfs4_do_create(dir, dentry, data);
2864
2865 nfs4_free_createdata(data);
2866out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 return status;
2868}
2869
2870static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2871 struct iattr *sattr)
2872{
2873 struct nfs4_exception exception = { };
2874 int err;
2875 do {
2876 err = nfs4_handle_exception(NFS_SERVER(dir),
2877 _nfs4_proc_mkdir(dir, dentry, sattr),
2878 &exception);
2879 } while (exception.retry);
2880 return err;
2881}
2882
2883static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2884 u64 cookie, struct page *page, unsigned int count, int plus)
2885{
2886 struct inode *dir = dentry->d_inode;
2887 struct nfs4_readdir_arg args = {
2888 .fh = NFS_FH(dir),
2889 .pages = &page,
2890 .pgbase = 0,
2891 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09002892 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 };
2894 struct nfs4_readdir_res res;
2895 struct rpc_message msg = {
2896 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2897 .rpc_argp = &args,
2898 .rpc_resp = &res,
2899 .rpc_cred = cred,
2900 };
2901 int status;
2902
Harvey Harrison3110ff82008-05-02 13:42:44 -07002903 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00002904 dentry->d_parent->d_name.name,
2905 dentry->d_name.name,
2906 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2908 res.pgbase = args.pgbase;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002909 status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 if (status == 0)
2911 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustc4812992007-09-28 17:11:45 -04002912
2913 nfs_invalidate_atime(dir);
2914
Harvey Harrison3110ff82008-05-02 13:42:44 -07002915 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 return status;
2917}
2918
2919static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2920 u64 cookie, struct page *page, unsigned int count, int plus)
2921{
2922 struct nfs4_exception exception = { };
2923 int err;
2924 do {
2925 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2926 _nfs4_proc_readdir(dentry, cred, cookie,
2927 page, count, plus),
2928 &exception);
2929 } while (exception.retry);
2930 return err;
2931}
2932
2933static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2934 struct iattr *sattr, dev_t rdev)
2935{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002936 struct nfs4_createdata *data;
2937 int mode = sattr->ia_mode;
2938 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939
2940 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2941 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002942
2943 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2944 if (data == NULL)
2945 goto out;
2946
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002948 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002950 data->arg.ftype = NF4BLK;
2951 data->arg.u.device.specdata1 = MAJOR(rdev);
2952 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 }
2954 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002955 data->arg.ftype = NF4CHR;
2956 data->arg.u.device.specdata1 = MAJOR(rdev);
2957 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002960 status = nfs4_do_create(dir, dentry, data);
2961
2962 nfs4_free_createdata(data);
2963out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 return status;
2965}
2966
2967static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2968 struct iattr *sattr, dev_t rdev)
2969{
2970 struct nfs4_exception exception = { };
2971 int err;
2972 do {
2973 err = nfs4_handle_exception(NFS_SERVER(dir),
2974 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2975 &exception);
2976 } while (exception.retry);
2977 return err;
2978}
2979
2980static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2981 struct nfs_fsstat *fsstat)
2982{
2983 struct nfs4_statfs_arg args = {
2984 .fh = fhandle,
2985 .bitmask = server->attr_bitmask,
2986 };
Benny Halevy24ad1482009-04-01 09:21:56 -04002987 struct nfs4_statfs_res res = {
2988 .fsstat = fsstat,
2989 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 struct rpc_message msg = {
2991 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2992 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04002993 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 };
2995
Trond Myklebust0e574af2005-10-27 22:12:38 -04002996 nfs_fattr_init(fsstat->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002997 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998}
2999
3000static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3001{
3002 struct nfs4_exception exception = { };
3003 int err;
3004 do {
3005 err = nfs4_handle_exception(server,
3006 _nfs4_proc_statfs(server, fhandle, fsstat),
3007 &exception);
3008 } while (exception.retry);
3009 return err;
3010}
3011
3012static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3013 struct nfs_fsinfo *fsinfo)
3014{
3015 struct nfs4_fsinfo_arg args = {
3016 .fh = fhandle,
3017 .bitmask = server->attr_bitmask,
3018 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003019 struct nfs4_fsinfo_res res = {
3020 .fsinfo = fsinfo,
3021 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 struct rpc_message msg = {
3023 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3024 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003025 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 };
3027
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003028 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029}
3030
3031static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3032{
3033 struct nfs4_exception exception = { };
3034 int err;
3035
3036 do {
3037 err = nfs4_handle_exception(server,
3038 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3039 &exception);
3040 } while (exception.retry);
3041 return err;
3042}
3043
3044static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3045{
Trond Myklebust0e574af2005-10-27 22:12:38 -04003046 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3048}
3049
3050static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3051 struct nfs_pathconf *pathconf)
3052{
3053 struct nfs4_pathconf_arg args = {
3054 .fh = fhandle,
3055 .bitmask = server->attr_bitmask,
3056 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003057 struct nfs4_pathconf_res res = {
3058 .pathconf = pathconf,
3059 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 struct rpc_message msg = {
3061 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3062 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003063 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 };
3065
3066 /* None of the pathconf attributes are mandatory to implement */
3067 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3068 memset(pathconf, 0, sizeof(*pathconf));
3069 return 0;
3070 }
3071
Trond Myklebust0e574af2005-10-27 22:12:38 -04003072 nfs_fattr_init(pathconf->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003073 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074}
3075
3076static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3077 struct nfs_pathconf *pathconf)
3078{
3079 struct nfs4_exception exception = { };
3080 int err;
3081
3082 do {
3083 err = nfs4_handle_exception(server,
3084 _nfs4_proc_pathconf(server, fhandle, pathconf),
3085 &exception);
3086 } while (exception.retry);
3087 return err;
3088}
3089
Trond Myklebustec06c092006-03-20 13:44:27 -05003090static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091{
Trond Myklebustec06c092006-03-20 13:44:27 -05003092 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093
Andy Adamsonf11c88a2009-04-01 09:22:25 -04003094 dprintk("--> %s\n", __func__);
3095
Andy Adamsonf11c88a2009-04-01 09:22:25 -04003096 nfs4_sequence_done(server, &data->res.seq_res, task->tk_status);
3097
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003098 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003099 nfs_restart_rpc(task, server->nfs_client);
Trond Myklebustec06c092006-03-20 13:44:27 -05003100 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003102
3103 nfs_invalidate_atime(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003105 renew_lease(server, data->timestamp);
3106 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107}
3108
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003109static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 data->timestamp = jiffies;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003112 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113}
3114
Trond Myklebust788e7a82006-03-20 13:44:27 -05003115static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 struct inode *inode = data->inode;
3118
Andy Adamsondef6ed72009-04-01 09:22:26 -04003119 nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
3120 task->tk_status);
3121
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003122 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003123 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003124 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003126 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003128 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003129 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003130 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131}
3132
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003133static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003135 struct nfs_server *server = NFS_SERVER(data->inode);
3136
Trond Myklebusta65318b2009-03-11 14:10:28 -04003137 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003138 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 data->timestamp = jiffies;
3140
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003141 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142}
3143
Trond Myklebust788e7a82006-03-20 13:44:27 -05003144static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 struct inode *inode = data->inode;
3147
Andy Adamson21d9a852009-04-01 09:22:27 -04003148 nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
3149 task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003150 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003151 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003152 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003154 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003155 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156}
3157
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003158static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003160 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161
Trond Myklebusta65318b2009-03-11 14:10:28 -04003162 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003163 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003164 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165}
3166
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003167struct nfs4_renewdata {
3168 struct nfs_client *client;
3169 unsigned long timestamp;
3170};
3171
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172/*
3173 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3174 * standalone procedure for queueing an asynchronous RENEW.
3175 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003176static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003177{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003178 struct nfs4_renewdata *data = calldata;
3179 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003180
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003181 if (atomic_read(&clp->cl_count) > 1)
3182 nfs4_schedule_state_renewal(clp);
3183 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003184 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003185}
3186
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003187static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003189 struct nfs4_renewdata *data = calldata;
3190 struct nfs_client *clp = data->client;
3191 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192
3193 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003194 /* Unless we're shutting down, schedule state recovery! */
3195 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
3196 nfs4_schedule_state_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 return;
3198 }
3199 spin_lock(&clp->cl_lock);
3200 if (time_before(clp->cl_last_renewal,timestamp))
3201 clp->cl_last_renewal = timestamp;
3202 spin_unlock(&clp->cl_lock);
3203}
3204
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003205static const struct rpc_call_ops nfs4_renew_ops = {
3206 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003207 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003208};
3209
David Howellsadfa6f92006-08-22 20:06:08 -04003210int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211{
3212 struct rpc_message msg = {
3213 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3214 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003215 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003217 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003219 if (!atomic_inc_not_zero(&clp->cl_count))
3220 return -EIO;
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003221 data = kmalloc(sizeof(*data), GFP_KERNEL);
3222 if (data == NULL)
3223 return -ENOMEM;
3224 data->client = clp;
3225 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003227 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228}
3229
David Howellsadfa6f92006-08-22 20:06:08 -04003230int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231{
3232 struct rpc_message msg = {
3233 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3234 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003235 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 };
3237 unsigned long now = jiffies;
3238 int status;
3239
3240 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3241 if (status < 0)
3242 return status;
3243 spin_lock(&clp->cl_lock);
3244 if (time_before(clp->cl_last_renewal,now))
3245 clp->cl_last_renewal = now;
3246 spin_unlock(&clp->cl_lock);
3247 return 0;
3248}
3249
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003250static inline int nfs4_server_supports_acls(struct nfs_server *server)
3251{
3252 return (server->caps & NFS_CAP_ACLS)
3253 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3254 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3255}
3256
3257/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3258 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3259 * the stack.
3260 */
3261#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3262
3263static void buf_to_pages(const void *buf, size_t buflen,
3264 struct page **pages, unsigned int *pgbase)
3265{
3266 const void *p = buf;
3267
3268 *pgbase = offset_in_page(buf);
3269 p -= *pgbase;
3270 while (p < buf + buflen) {
3271 *(pages++) = virt_to_page(p);
3272 p += PAGE_CACHE_SIZE;
3273 }
3274}
3275
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003276struct nfs4_cached_acl {
3277 int cached;
3278 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003279 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003280};
3281
3282static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003283{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003284 struct nfs_inode *nfsi = NFS_I(inode);
3285
3286 spin_lock(&inode->i_lock);
3287 kfree(nfsi->nfs4_acl);
3288 nfsi->nfs4_acl = acl;
3289 spin_unlock(&inode->i_lock);
3290}
3291
3292static void nfs4_zap_acl_attr(struct inode *inode)
3293{
3294 nfs4_set_cached_acl(inode, NULL);
3295}
3296
3297static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3298{
3299 struct nfs_inode *nfsi = NFS_I(inode);
3300 struct nfs4_cached_acl *acl;
3301 int ret = -ENOENT;
3302
3303 spin_lock(&inode->i_lock);
3304 acl = nfsi->nfs4_acl;
3305 if (acl == NULL)
3306 goto out;
3307 if (buf == NULL) /* user is just asking for length */
3308 goto out_len;
3309 if (acl->cached == 0)
3310 goto out;
3311 ret = -ERANGE; /* see getxattr(2) man page */
3312 if (acl->len > buflen)
3313 goto out;
3314 memcpy(buf, acl->data, acl->len);
3315out_len:
3316 ret = acl->len;
3317out:
3318 spin_unlock(&inode->i_lock);
3319 return ret;
3320}
3321
3322static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3323{
3324 struct nfs4_cached_acl *acl;
3325
3326 if (buf && acl_len <= PAGE_SIZE) {
3327 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3328 if (acl == NULL)
3329 goto out;
3330 acl->cached = 1;
3331 memcpy(acl->data, buf, acl_len);
3332 } else {
3333 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3334 if (acl == NULL)
3335 goto out;
3336 acl->cached = 0;
3337 }
3338 acl->len = acl_len;
3339out:
3340 nfs4_set_cached_acl(inode, acl);
3341}
3342
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003343static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003344{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003345 struct page *pages[NFS4ACL_MAXPAGES];
3346 struct nfs_getaclargs args = {
3347 .fh = NFS_FH(inode),
3348 .acl_pages = pages,
3349 .acl_len = buflen,
3350 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003351 struct nfs_getaclres res = {
3352 .acl_len = buflen,
3353 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003354 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003355 struct rpc_message msg = {
3356 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3357 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003358 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003359 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003360 struct page *localpage = NULL;
3361 int ret;
3362
3363 if (buflen < PAGE_SIZE) {
3364 /* As long as we're doing a round trip to the server anyway,
3365 * let's be prepared for a page of acl data. */
3366 localpage = alloc_page(GFP_KERNEL);
3367 resp_buf = page_address(localpage);
3368 if (localpage == NULL)
3369 return -ENOMEM;
3370 args.acl_pages[0] = localpage;
3371 args.acl_pgbase = 0;
Benny Halevy663c79b2009-04-01 09:21:59 -04003372 args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003373 } else {
3374 resp_buf = buf;
3375 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3376 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003377 ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003378 if (ret)
3379 goto out_free;
Benny Halevy663c79b2009-04-01 09:21:59 -04003380 if (res.acl_len > args.acl_len)
3381 nfs4_write_cached_acl(inode, NULL, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003382 else
Benny Halevy663c79b2009-04-01 09:21:59 -04003383 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003384 if (buf) {
3385 ret = -ERANGE;
Benny Halevy663c79b2009-04-01 09:21:59 -04003386 if (res.acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003387 goto out_free;
3388 if (localpage)
Benny Halevy663c79b2009-04-01 09:21:59 -04003389 memcpy(buf, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003390 }
Benny Halevy663c79b2009-04-01 09:21:59 -04003391 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003392out_free:
3393 if (localpage)
3394 __free_page(localpage);
3395 return ret;
3396}
3397
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003398static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3399{
3400 struct nfs4_exception exception = { };
3401 ssize_t ret;
3402 do {
3403 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3404 if (ret >= 0)
3405 break;
3406 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3407 } while (exception.retry);
3408 return ret;
3409}
3410
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003411static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3412{
3413 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003414 int ret;
3415
3416 if (!nfs4_server_supports_acls(server))
3417 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003418 ret = nfs_revalidate_inode(server, inode);
3419 if (ret < 0)
3420 return ret;
3421 ret = nfs4_read_cached_acl(inode, buf, buflen);
3422 if (ret != -ENOENT)
3423 return ret;
3424 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003425}
3426
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003427static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003428{
3429 struct nfs_server *server = NFS_SERVER(inode);
3430 struct page *pages[NFS4ACL_MAXPAGES];
3431 struct nfs_setaclargs arg = {
3432 .fh = NFS_FH(inode),
3433 .acl_pages = pages,
3434 .acl_len = buflen,
3435 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003436 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003437 struct rpc_message msg = {
3438 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3439 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003440 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003441 };
3442 int ret;
3443
3444 if (!nfs4_server_supports_acls(server))
3445 return -EOPNOTSUPP;
Trond Myklebust642ac542005-10-18 14:20:19 -07003446 nfs_inode_return_delegation(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003447 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003448 ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003449 nfs_access_zap_cache(inode);
3450 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003451 return ret;
3452}
3453
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003454static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3455{
3456 struct nfs4_exception exception = { };
3457 int err;
3458 do {
3459 err = nfs4_handle_exception(NFS_SERVER(inode),
3460 __nfs4_proc_set_acl(inode, buf, buflen),
3461 &exception);
3462 } while (exception.retry);
3463 return err;
3464}
3465
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466static int
Andy Adamson8328d592009-04-01 09:22:35 -04003467_nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs_client *clp, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 if (!clp || task->tk_status >= 0)
3470 return 0;
3471 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003472 case -NFS4ERR_ADMIN_REVOKED:
3473 case -NFS4ERR_BAD_STATEID:
3474 case -NFS4ERR_OPENMODE:
3475 if (state == NULL)
3476 break;
3477 nfs4_state_mark_reclaim_nograce(clp, state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003478 goto do_state_recovery;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003480 if (state == NULL)
3481 break;
3482 nfs4_state_mark_reclaim_reboot(clp, state);
3483 case -NFS4ERR_STALE_CLIENTID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 case -NFS4ERR_EXPIRED:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003485 goto do_state_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003486#if defined(CONFIG_NFS_V4_1)
3487 case -NFS4ERR_BADSESSION:
3488 case -NFS4ERR_BADSLOT:
3489 case -NFS4ERR_BAD_HIGH_SLOT:
3490 case -NFS4ERR_DEADSESSION:
3491 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3492 case -NFS4ERR_SEQ_FALSE_RETRY:
3493 case -NFS4ERR_SEQ_MISORDERED:
3494 dprintk("%s ERROR %d, Reset session\n", __func__,
3495 task->tk_status);
Andy Adamson0b9e2d42009-12-04 16:02:14 -05003496 nfs4_schedule_state_recovery(clp);
Andy Adamson4745e312009-04-01 09:22:42 -04003497 task->tk_status = 0;
3498 return -EAGAIN;
3499#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 case -NFS4ERR_DELAY:
Andy Adamson8328d592009-04-01 09:22:35 -04003501 if (server)
3502 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003503 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003504 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3506 task->tk_status = 0;
3507 return -EAGAIN;
3508 case -NFS4ERR_OLD_STATEID:
3509 task->tk_status = 0;
3510 return -EAGAIN;
3511 }
3512 task->tk_status = nfs4_map_errors(task->tk_status);
3513 return 0;
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003514do_state_recovery:
3515 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
3516 nfs4_schedule_state_recovery(clp);
3517 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3518 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3519 task->tk_status = 0;
3520 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521}
3522
Andy Adamson8328d592009-04-01 09:22:35 -04003523static int
3524nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3525{
3526 return _nfs4_async_handle_error(task, server, server->nfs_client, state);
3527}
3528
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003529int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3530 unsigned short port, struct rpc_cred *cred,
3531 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532{
3533 nfs4_verifier sc_verifier;
3534 struct nfs4_setclientid setclientid = {
3535 .sc_verifier = &sc_verifier,
3536 .sc_prog = program,
3537 };
3538 struct rpc_message msg = {
3539 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3540 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003541 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003542 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 };
Al Virobc4785c2006-10-19 23:28:51 -07003544 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545 int loop = 0;
3546 int status;
3547
Al Virobc4785c2006-10-19 23:28:51 -07003548 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3550 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3551
3552 for(;;) {
3553 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003554 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003555 clp->cl_ipaddr,
3556 rpc_peeraddr2str(clp->cl_rpcclient,
3557 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003558 rpc_peeraddr2str(clp->cl_rpcclient,
3559 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003560 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 clp->cl_id_uniquifier);
3562 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003563 sizeof(setclientid.sc_netid),
3564 rpc_peeraddr2str(clp->cl_rpcclient,
3565 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003567 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 clp->cl_ipaddr, port >> 8, port & 255);
3569
3570 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3571 if (status != -NFS4ERR_CLID_INUSE)
3572 break;
3573 if (signalled())
3574 break;
3575 if (loop++ & 1)
3576 ssleep(clp->cl_lease_time + 1);
3577 else
3578 if (++clp->cl_id_uniquifier == 0)
3579 break;
3580 }
3581 return status;
3582}
3583
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003584static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3585 struct nfs4_setclientid_res *arg,
3586 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587{
3588 struct nfs_fsinfo fsinfo;
3589 struct rpc_message msg = {
3590 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003591 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003593 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594 };
3595 unsigned long now;
3596 int status;
3597
3598 now = jiffies;
3599 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3600 if (status == 0) {
3601 spin_lock(&clp->cl_lock);
3602 clp->cl_lease_time = fsinfo.lease_time * HZ;
3603 clp->cl_last_renewal = now;
3604 spin_unlock(&clp->cl_lock);
3605 }
3606 return status;
3607}
3608
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003609int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3610 struct nfs4_setclientid_res *arg,
3611 struct rpc_cred *cred)
Trond Myklebust51581f32006-03-20 13:44:47 -05003612{
Benny Halevy77e03672008-06-24 20:25:57 +03003613 long timeout = 0;
Trond Myklebust51581f32006-03-20 13:44:47 -05003614 int err;
3615 do {
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003616 err = _nfs4_proc_setclientid_confirm(clp, arg, cred);
Trond Myklebust51581f32006-03-20 13:44:47 -05003617 switch (err) {
3618 case 0:
3619 return err;
3620 case -NFS4ERR_RESOURCE:
3621 /* The IBM lawyers misread another document! */
3622 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -05003623 case -EKEYEXPIRED:
Trond Myklebust51581f32006-03-20 13:44:47 -05003624 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3625 }
3626 } while (err == 0);
3627 return err;
3628}
3629
Trond Myklebustfe650402006-01-03 09:55:18 +01003630struct nfs4_delegreturndata {
3631 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003632 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003633 struct nfs_fh fh;
3634 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003635 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003636 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003637 int rpc_status;
3638};
3639
Trond Myklebustfe650402006-01-03 09:55:18 +01003640static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3641{
3642 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003643
Trond Myklebustd61e6122009-12-05 19:32:19 -05003644 nfs4_sequence_done(data->res.server, &data->res.seq_res,
3645 task->tk_status);
Andy Adamson938e1012009-04-01 09:22:28 -04003646
Ricardo Labiaga79708862009-12-07 09:23:21 -05003647 switch (task->tk_status) {
3648 case -NFS4ERR_STALE_STATEID:
3649 case -NFS4ERR_EXPIRED:
3650 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01003651 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003652 break;
3653 default:
3654 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3655 -EAGAIN) {
3656 nfs_restart_rpc(task, data->res.server->nfs_client);
3657 return;
3658 }
3659 }
3660 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01003661}
3662
3663static void nfs4_delegreturn_release(void *calldata)
3664{
Trond Myklebustfe650402006-01-03 09:55:18 +01003665 kfree(calldata);
3666}
3667
Andy Adamson938e1012009-04-01 09:22:28 -04003668#if defined(CONFIG_NFS_V4_1)
3669static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3670{
3671 struct nfs4_delegreturndata *d_data;
3672
3673 d_data = (struct nfs4_delegreturndata *)data;
3674
3675 if (nfs4_setup_sequence(d_data->res.server->nfs_client,
3676 &d_data->args.seq_args,
3677 &d_data->res.seq_res, 1, task))
3678 return;
3679 rpc_call_start(task);
3680}
3681#endif /* CONFIG_NFS_V4_1 */
3682
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003683static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04003684#if defined(CONFIG_NFS_V4_1)
3685 .rpc_call_prepare = nfs4_delegreturn_prepare,
3686#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01003687 .rpc_call_done = nfs4_delegreturn_done,
3688 .rpc_release = nfs4_delegreturn_release,
3689};
3690
Trond Myklebuste6f81072008-01-24 18:14:34 -05003691static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003692{
3693 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003694 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003695 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003696 struct rpc_message msg = {
3697 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3698 .rpc_cred = cred,
3699 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003700 struct rpc_task_setup task_setup_data = {
3701 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003702 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003703 .callback_ops = &nfs4_delegreturn_ops,
3704 .flags = RPC_TASK_ASYNC,
3705 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003706 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003707
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003708 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01003709 if (data == NULL)
3710 return -ENOMEM;
3711 data->args.fhandle = &data->fh;
3712 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003713 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003714 nfs_copy_fh(&data->fh, NFS_FH(inode));
3715 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003716 data->res.fattr = &data->fattr;
3717 data->res.server = server;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003718 data->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003719 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003720 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003721 data->rpc_status = 0;
3722
Trond Myklebustc970aa82007-07-14 15:39:59 -04003723 task_setup_data.callback_data = data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003724 msg.rpc_argp = &data->args,
3725 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003726 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003727 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003728 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003729 if (!issync)
3730 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003731 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003732 if (status != 0)
3733 goto out;
3734 status = data->rpc_status;
3735 if (status != 0)
3736 goto out;
3737 nfs_refresh_inode(inode, &data->fattr);
3738out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003739 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003740 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741}
3742
Trond Myklebuste6f81072008-01-24 18:14:34 -05003743int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744{
3745 struct nfs_server *server = NFS_SERVER(inode);
3746 struct nfs4_exception exception = { };
3747 int err;
3748 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003749 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750 switch (err) {
3751 case -NFS4ERR_STALE_STATEID:
3752 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753 case 0:
3754 return 0;
3755 }
3756 err = nfs4_handle_exception(server, err, &exception);
3757 } while (exception.retry);
3758 return err;
3759}
3760
3761#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3762#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3763
3764/*
3765 * sleep, with exponential backoff, and retry the LOCK operation.
3766 */
3767static unsigned long
3768nfs4_set_lock_task_retry(unsigned long timeout)
3769{
Matthew Wilcox150030b2007-12-06 16:24:39 -05003770 schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 timeout <<= 1;
3772 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3773 return NFS4_LOCK_MAXTIMEOUT;
3774 return timeout;
3775}
3776
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3778{
3779 struct inode *inode = state->inode;
3780 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003781 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003782 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003784 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003786 struct nfs_lockt_res res = {
3787 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788 };
3789 struct rpc_message msg = {
3790 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3791 .rpc_argp = &arg,
3792 .rpc_resp = &res,
3793 .rpc_cred = state->owner->so_cred,
3794 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795 struct nfs4_lock_state *lsp;
3796 int status;
3797
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003798 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003799 status = nfs4_set_lock_state(state, request);
3800 if (status != 0)
3801 goto out;
3802 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003803 arg.lock_owner.id = lsp->ls_id.id;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003804 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003805 switch (status) {
3806 case 0:
3807 request->fl_type = F_UNLCK;
3808 break;
3809 case -NFS4ERR_DENIED:
3810 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05003812 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003813out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814 return status;
3815}
3816
3817static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3818{
3819 struct nfs4_exception exception = { };
3820 int err;
3821
3822 do {
3823 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3824 _nfs4_proc_getlk(state, cmd, request),
3825 &exception);
3826 } while (exception.retry);
3827 return err;
3828}
3829
3830static int do_vfs_lock(struct file *file, struct file_lock *fl)
3831{
3832 int res = 0;
3833 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3834 case FL_POSIX:
3835 res = posix_lock_file_wait(file, fl);
3836 break;
3837 case FL_FLOCK:
3838 res = flock_lock_file_wait(file, fl);
3839 break;
3840 default:
3841 BUG();
3842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843 return res;
3844}
3845
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003846struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003847 struct nfs_locku_args arg;
3848 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003849 struct nfs4_lock_state *lsp;
3850 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003851 struct file_lock fl;
3852 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003853 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003854};
3855
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003856static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3857 struct nfs_open_context *ctx,
3858 struct nfs4_lock_state *lsp,
3859 struct nfs_seqid *seqid)
3860{
3861 struct nfs4_unlockdata *p;
3862 struct inode *inode = lsp->ls_state->inode;
3863
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003864 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003865 if (p == NULL)
3866 return NULL;
3867 p->arg.fh = NFS_FH(inode);
3868 p->arg.fl = &p->fl;
3869 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003870 p->res.seqid = seqid;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003871 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003872 p->arg.stateid = &lsp->ls_stateid;
3873 p->lsp = lsp;
3874 atomic_inc(&lsp->ls_count);
3875 /* Ensure we don't close file until we're done freeing locks! */
3876 p->ctx = get_nfs_open_context(ctx);
3877 memcpy(&p->fl, fl, sizeof(p->fl));
3878 p->server = NFS_SERVER(inode);
3879 return p;
3880}
3881
Trond Myklebust06f814a2006-01-03 09:55:07 +01003882static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003883{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003884 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003885 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003886 nfs4_put_lock_state(calldata->lsp);
3887 put_nfs_open_context(calldata->ctx);
3888 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003889}
3890
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003891static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003892{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003893 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003894
Andy Adamsona8936932009-04-01 09:22:23 -04003895 nfs4_sequence_done(calldata->server, &calldata->res.seq_res,
3896 task->tk_status);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003897 if (RPC_ASSASSINATED(task))
3898 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003899 switch (task->tk_status) {
3900 case 0:
3901 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003902 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003903 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01003904 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003905 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003906 case -NFS4ERR_BAD_STATEID:
3907 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003908 case -NFS4ERR_STALE_STATEID:
3909 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003910 break;
3911 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003912 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebust0110ee12009-12-07 09:00:24 -05003913 nfs_restart_rpc(task,
Trond Myklebustd61e6122009-12-05 19:32:19 -05003914 calldata->server->nfs_client);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003915 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003916}
3917
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003918static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003919{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003920 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003922 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003923 return;
3924 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003925 /* Note: exit _without_ running nfs4_locku_done */
3926 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003927 return;
3928 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01003929 calldata->timestamp = jiffies;
Andy Adamsona8936932009-04-01 09:22:23 -04003930 if (nfs4_setup_sequence(calldata->server->nfs_client,
3931 &calldata->arg.seq_args,
3932 &calldata->res.seq_res, 1, task))
3933 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003934 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935}
3936
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003937static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003938 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003939 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01003940 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003941};
3942
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003943static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3944 struct nfs_open_context *ctx,
3945 struct nfs4_lock_state *lsp,
3946 struct nfs_seqid *seqid)
3947{
3948 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003949 struct rpc_message msg = {
3950 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3951 .rpc_cred = ctx->cred,
3952 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003953 struct rpc_task_setup task_setup_data = {
3954 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04003955 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003956 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003957 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003958 .flags = RPC_TASK_ASYNC,
3959 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003960
Frank Filz137d6ac2007-07-09 15:32:29 -07003961 /* Ensure this is an unlock - when canceling a lock, the
3962 * canceled lock is passed in, and it won't be an unlock.
3963 */
3964 fl->fl_type = F_UNLCK;
3965
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003966 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3967 if (data == NULL) {
3968 nfs_free_seqid(seqid);
3969 return ERR_PTR(-ENOMEM);
3970 }
3971
Trond Myklebust5138fde2007-07-14 15:40:01 -04003972 msg.rpc_argp = &data->arg,
3973 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003974 task_setup_data.callback_data = data;
3975 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003976}
3977
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3979{
Trond Myklebust19e03c52008-12-23 15:21:44 -05003980 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003981 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003982 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01003983 struct rpc_task *task;
3984 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003985 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986
Trond Myklebust9b073572006-06-29 16:38:34 -04003987 status = nfs4_set_lock_state(state, request);
3988 /* Unlock _before_ we do the RPC call */
3989 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003990 down_read(&nfsi->rwsem);
3991 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
3992 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003993 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003994 }
3995 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003996 if (status != 0)
3997 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003998 /* Is this a delegated lock? */
3999 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004000 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004001 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004002 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004003 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004004 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004005 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004006 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004007 status = PTR_ERR(task);
4008 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004009 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004010 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004011 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004012out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004013 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004014 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015}
4016
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004017struct nfs4_lockdata {
4018 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004019 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004020 struct nfs4_lock_state *lsp;
4021 struct nfs_open_context *ctx;
4022 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004023 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004024 int rpc_status;
4025 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004026 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004027};
4028
4029static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004030 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4031 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004032{
4033 struct nfs4_lockdata *p;
4034 struct inode *inode = lsp->ls_state->inode;
4035 struct nfs_server *server = NFS_SERVER(inode);
4036
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004037 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004038 if (p == NULL)
4039 return NULL;
4040
4041 p->arg.fh = NFS_FH(inode);
4042 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004043 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004044 if (p->arg.open_seqid == NULL)
4045 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004046 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004047 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004048 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004049 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004050 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04004051 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004052 p->res.lock_seqid = p->arg.lock_seqid;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04004053 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004054 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004055 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004056 atomic_inc(&lsp->ls_count);
4057 p->ctx = get_nfs_open_context(ctx);
4058 memcpy(&p->fl, fl, sizeof(p->fl));
4059 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004060out_free_seqid:
4061 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004062out_free:
4063 kfree(p);
4064 return NULL;
4065}
4066
4067static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4068{
4069 struct nfs4_lockdata *data = calldata;
4070 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071
Harvey Harrison3110ff82008-05-02 13:42:44 -07004072 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004073 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4074 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004075 /* Do we need to do an open_to_lock_owner? */
4076 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004077 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4078 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004079 data->arg.open_stateid = &state->stateid;
4080 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004081 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004082 } else
4083 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004084 data->timestamp = jiffies;
Andy Adamson66179ef2009-04-01 09:22:22 -04004085 if (nfs4_setup_sequence(data->server->nfs_client, &data->arg.seq_args,
4086 &data->res.seq_res, 1, task))
4087 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004088 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004089 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004090}
4091
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004092static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4093{
4094 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4095 nfs4_lock_prepare(task, calldata);
4096}
4097
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004098static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4099{
4100 struct nfs4_lockdata *data = calldata;
4101
Harvey Harrison3110ff82008-05-02 13:42:44 -07004102 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004103
Trond Myklebustd61e6122009-12-05 19:32:19 -05004104 nfs4_sequence_done(data->server, &data->res.seq_res,
4105 task->tk_status);
Andy Adamson66179ef2009-04-01 09:22:22 -04004106
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004107 data->rpc_status = task->tk_status;
4108 if (RPC_ASSASSINATED(task))
4109 goto out;
4110 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004111 if (data->rpc_status == 0)
4112 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4113 else
4114 goto out;
4115 }
4116 if (data->rpc_status == 0) {
4117 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4118 sizeof(data->lsp->ls_stateid.data));
4119 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Trond Myklebust88be9f92007-06-05 10:42:27 -04004120 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004121 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004122out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004123 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004124}
4125
4126static void nfs4_lock_release(void *calldata)
4127{
4128 struct nfs4_lockdata *data = calldata;
4129
Harvey Harrison3110ff82008-05-02 13:42:44 -07004130 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004131 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004132 if (data->cancelled != 0) {
4133 struct rpc_task *task;
4134 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4135 data->arg.lock_seqid);
4136 if (!IS_ERR(task))
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004137 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004138 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004139 } else
4140 nfs_free_seqid(data->arg.lock_seqid);
4141 nfs4_put_lock_state(data->lsp);
4142 put_nfs_open_context(data->ctx);
4143 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004144 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004145}
4146
4147static const struct rpc_call_ops nfs4_lock_ops = {
4148 .rpc_call_prepare = nfs4_lock_prepare,
4149 .rpc_call_done = nfs4_lock_done,
4150 .rpc_release = nfs4_lock_release,
4151};
4152
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004153static const struct rpc_call_ops nfs4_recover_lock_ops = {
4154 .rpc_call_prepare = nfs4_recover_lock_prepare,
4155 .rpc_call_done = nfs4_lock_done,
4156 .rpc_release = nfs4_lock_release,
4157};
4158
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004159static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4160{
4161 struct nfs_client *clp = server->nfs_client;
4162 struct nfs4_state *state = lsp->ls_state;
4163
4164 switch (error) {
4165 case -NFS4ERR_ADMIN_REVOKED:
4166 case -NFS4ERR_BAD_STATEID:
4167 case -NFS4ERR_EXPIRED:
4168 if (new_lock_owner != 0 ||
4169 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4170 nfs4_state_mark_reclaim_nograce(clp, state);
4171 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004172 break;
4173 case -NFS4ERR_STALE_STATEID:
4174 if (new_lock_owner != 0 ||
4175 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4176 nfs4_state_mark_reclaim_reboot(clp, state);
4177 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004178 };
4179}
4180
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004181static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004182{
4183 struct nfs4_lockdata *data;
4184 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004185 struct rpc_message msg = {
4186 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4187 .rpc_cred = state->owner->so_cred,
4188 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004189 struct rpc_task_setup task_setup_data = {
4190 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004191 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004192 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004193 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004194 .flags = RPC_TASK_ASYNC,
4195 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004196 int ret;
4197
Harvey Harrison3110ff82008-05-02 13:42:44 -07004198 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004199 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004200 fl->fl_u.nfs4_fl.owner,
4201 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004202 if (data == NULL)
4203 return -ENOMEM;
4204 if (IS_SETLKW(cmd))
4205 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004206 if (recovery_type > NFS_LOCK_NEW) {
4207 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004208 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004209 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4210 }
Trond Myklebust5138fde2007-07-14 15:40:01 -04004211 msg.rpc_argp = &data->arg,
4212 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004213 task_setup_data.callback_data = data;
4214 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004215 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004216 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004217 ret = nfs4_wait_for_completion_rpc_task(task);
4218 if (ret == 0) {
4219 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004220 if (ret)
4221 nfs4_handle_setlk_error(data->server, data->lsp,
4222 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004223 } else
4224 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004225 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004226 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004227 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228}
4229
4230static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4231{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004232 struct nfs_server *server = NFS_SERVER(state->inode);
4233 struct nfs4_exception exception = { };
4234 int err;
4235
4236 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004237 /* Cache the lock if possible... */
4238 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4239 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004240 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Jeff Layton2c643482010-01-07 09:42:03 -05004241 if (err != -NFS4ERR_DELAY && err != -EKEYEXPIRED)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004242 break;
4243 nfs4_handle_exception(server, err, &exception);
4244 } while (exception.retry);
4245 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246}
4247
4248static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4249{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004250 struct nfs_server *server = NFS_SERVER(state->inode);
4251 struct nfs4_exception exception = { };
4252 int err;
4253
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004254 err = nfs4_set_lock_state(state, request);
4255 if (err != 0)
4256 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004257 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004258 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4259 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004260 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004261 switch (err) {
4262 default:
4263 goto out;
4264 case -NFS4ERR_GRACE:
4265 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -05004266 case -EKEYEXPIRED:
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004267 nfs4_handle_exception(server, err, &exception);
4268 err = 0;
4269 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004270 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004271out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004272 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273}
4274
4275static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4276{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004277 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004278 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004279 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004281 if ((fl_flags & FL_POSIX) &&
4282 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4283 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004284 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004285 status = nfs4_set_lock_state(state, request);
4286 if (status != 0)
4287 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004288 request->fl_flags |= FL_ACCESS;
4289 status = do_vfs_lock(request->fl_file, request);
4290 if (status < 0)
4291 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004292 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004293 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004294 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004295 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004296 request->fl_flags = fl_flags & ~FL_SLEEP;
4297 status = do_vfs_lock(request->fl_file, request);
4298 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004299 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004300 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004301 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004302 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004303 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004304 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004305 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07004306 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004307out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004308 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004309out:
4310 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 return status;
4312}
4313
4314static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4315{
4316 struct nfs4_exception exception = { };
4317 int err;
4318
4319 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004320 err = _nfs4_proc_setlk(state, cmd, request);
4321 if (err == -NFS4ERR_DENIED)
4322 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004324 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 } while (exception.retry);
4326 return err;
4327}
4328
4329static int
4330nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4331{
4332 struct nfs_open_context *ctx;
4333 struct nfs4_state *state;
4334 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4335 int status;
4336
4337 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004338 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 state = ctx->state;
4340
4341 if (request->fl_start < 0 || request->fl_end < 0)
4342 return -EINVAL;
4343
Trond Myklebustd9531262009-07-21 19:22:38 -04004344 if (IS_GETLK(cmd)) {
4345 if (state != NULL)
4346 return nfs4_proc_getlk(state, F_GETLK, request);
4347 return 0;
4348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349
4350 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4351 return -EINVAL;
4352
Trond Myklebustd9531262009-07-21 19:22:38 -04004353 if (request->fl_type == F_UNLCK) {
4354 if (state != NULL)
4355 return nfs4_proc_unlck(state, cmd, request);
4356 return 0;
4357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358
Trond Myklebustd9531262009-07-21 19:22:38 -04004359 if (state == NULL)
4360 return -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361 do {
4362 status = nfs4_proc_setlk(state, cmd, request);
4363 if ((status != -EAGAIN) || IS_SETLK(cmd))
4364 break;
4365 timeout = nfs4_set_lock_task_retry(timeout);
4366 status = -ERESTARTSYS;
4367 if (signalled())
4368 break;
4369 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370 return status;
4371}
4372
Trond Myklebust888e6942005-11-04 15:38:11 -05004373int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4374{
4375 struct nfs_server *server = NFS_SERVER(state->inode);
4376 struct nfs4_exception exception = { };
4377 int err;
4378
4379 err = nfs4_set_lock_state(state, fl);
4380 if (err != 0)
4381 goto out;
4382 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004383 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004384 switch (err) {
4385 default:
4386 printk(KERN_ERR "%s: unhandled error %d.\n",
4387 __func__, err);
4388 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004389 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004390 goto out;
4391 case -NFS4ERR_EXPIRED:
4392 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004393 case -NFS4ERR_STALE_STATEID:
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004394 case -NFS4ERR_BADSESSION:
4395 case -NFS4ERR_BADSLOT:
4396 case -NFS4ERR_BAD_HIGH_SLOT:
4397 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4398 case -NFS4ERR_DEADSESSION:
Trond Myklebustd5122202009-06-17 13:22:58 -07004399 nfs4_schedule_state_recovery(server->nfs_client);
4400 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004401 case -ERESTARTSYS:
4402 /*
4403 * The show must go on: exit, but mark the
4404 * stateid as needing recovery.
4405 */
4406 case -NFS4ERR_ADMIN_REVOKED:
4407 case -NFS4ERR_BAD_STATEID:
4408 case -NFS4ERR_OPENMODE:
4409 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
4410 err = 0;
4411 goto out;
4412 case -ENOMEM:
4413 case -NFS4ERR_DENIED:
4414 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4415 err = 0;
4416 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004417 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -05004418 case -EKEYEXPIRED:
Trond Myklebustd5122202009-06-17 13:22:58 -07004419 break;
4420 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004421 err = nfs4_handle_exception(server, err, &exception);
4422 } while (exception.retry);
4423out:
4424 return err;
4425}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004426
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004427#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4428
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004429int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
4430 size_t buflen, int flags)
4431{
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004432 struct inode *inode = dentry->d_inode;
4433
4434 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4435 return -EOPNOTSUPP;
4436
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004437 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004438}
4439
4440/* The getxattr man page suggests returning -ENODATA for unknown attributes,
4441 * and that's what we'll do for e.g. user attributes that haven't been set.
4442 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
4443 * attributes in kernel-managed attribute namespaces. */
4444ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
4445 size_t buflen)
4446{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004447 struct inode *inode = dentry->d_inode;
4448
4449 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4450 return -EOPNOTSUPP;
4451
4452 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004453}
4454
4455ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
4456{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004457 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004458
J. Bruce Fields096455a2006-03-20 23:23:42 -05004459 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4460 return 0;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004461 if (buf && buflen < len)
4462 return -ERANGE;
4463 if (buf)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004464 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
4465 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004466}
4467
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004468static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4469{
4470 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
4471 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4472 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4473 return;
4474
4475 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4476 NFS_ATTR_FATTR_NLINK;
4477 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4478 fattr->nlink = 2;
4479}
4480
Trond Myklebust56659e92007-07-17 21:52:39 -04004481int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004482 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004483{
4484 struct nfs_server *server = NFS_SERVER(dir);
4485 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004486 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4487 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004488 };
4489 struct nfs4_fs_locations_arg args = {
4490 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004491 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004492 .page = page,
4493 .bitmask = bitmask,
4494 };
Benny Halevy22958462009-04-01 09:22:02 -04004495 struct nfs4_fs_locations_res res = {
4496 .fs_locations = fs_locations,
4497 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004498 struct rpc_message msg = {
4499 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4500 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004501 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004502 };
4503 int status;
4504
Harvey Harrison3110ff82008-05-02 13:42:44 -07004505 dprintk("%s: start\n", __func__);
Trond Myklebustc228fd32007-01-13 02:28:11 -05004506 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004507 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004508 fs_locations->nlocations = 0;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04004509 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004510 nfs_fixup_referral_attributes(&fs_locations->fattr);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004511 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004512 return status;
4513}
4514
Andy Adamson557134a2009-04-01 09:21:53 -04004515#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04004516/*
4517 * nfs4_proc_exchange_id()
4518 *
4519 * Since the clientid has expired, all compounds using sessions
4520 * associated with the stale clientid will be returning
4521 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4522 * be in some phase of session reset.
4523 */
Andy Adamson4d643d12009-12-04 15:52:24 -05004524int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004525{
4526 nfs4_verifier verifier;
4527 struct nfs41_exchange_id_args args = {
4528 .client = clp,
4529 .flags = clp->cl_exchange_flags,
4530 };
4531 struct nfs41_exchange_id_res res = {
4532 .client = clp,
4533 };
4534 int status;
4535 struct rpc_message msg = {
4536 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4537 .rpc_argp = &args,
4538 .rpc_resp = &res,
4539 .rpc_cred = cred,
4540 };
4541 __be32 *p;
4542
4543 dprintk("--> %s\n", __func__);
4544 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04004545
Alexandros Batsakis7b183d02009-12-05 13:33:25 -05004546 /* Remove server-only flags */
4547 args.flags &= ~EXCHGID4_FLAG_CONFIRMED_R;
4548
Benny Halevy99fe60d2009-04-01 09:22:29 -04004549 p = (u32 *)verifier.data;
4550 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4551 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4552 args.verifier = &verifier;
4553
4554 while (1) {
4555 args.id_len = scnprintf(args.id, sizeof(args.id),
4556 "%s/%s %u",
4557 clp->cl_ipaddr,
4558 rpc_peeraddr2str(clp->cl_rpcclient,
4559 RPC_DISPLAY_ADDR),
4560 clp->cl_id_uniquifier);
4561
4562 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
4563
Andy Adamson180b62a2010-03-02 13:19:36 -05004564 if (status != -NFS4ERR_CLID_INUSE)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004565 break;
4566
4567 if (signalled())
4568 break;
4569
4570 if (++clp->cl_id_uniquifier == 0)
4571 break;
4572 }
4573
4574 dprintk("<-- %s status= %d\n", __func__, status);
4575 return status;
4576}
4577
Andy Adamson2050f0c2009-04-01 09:22:30 -04004578struct nfs4_get_lease_time_data {
4579 struct nfs4_get_lease_time_args *args;
4580 struct nfs4_get_lease_time_res *res;
4581 struct nfs_client *clp;
4582};
4583
4584static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4585 void *calldata)
4586{
4587 int ret;
4588 struct nfs4_get_lease_time_data *data =
4589 (struct nfs4_get_lease_time_data *)calldata;
4590
4591 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004592 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004593 /* just setup sequence, do not trigger session recovery
4594 since we're invoked within one */
4595 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004596 &data->args->la_seq_args,
4597 &data->res->lr_seq_res, 0, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004598
4599 BUG_ON(ret == -EAGAIN);
4600 rpc_call_start(task);
4601 dprintk("<-- %s\n", __func__);
4602}
4603
4604/*
4605 * Called from nfs4_state_manager thread for session setup, so don't recover
4606 * from sequence operation or clientid errors.
4607 */
4608static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4609{
4610 struct nfs4_get_lease_time_data *data =
4611 (struct nfs4_get_lease_time_data *)calldata;
4612
4613 dprintk("--> %s\n", __func__);
4614 nfs41_sequence_done(data->clp, &data->res->lr_seq_res, task->tk_status);
4615 switch (task->tk_status) {
4616 case -NFS4ERR_DELAY:
4617 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05004618 case -EKEYEXPIRED:
Andy Adamson2050f0c2009-04-01 09:22:30 -04004619 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4620 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4621 task->tk_status = 0;
Trond Myklebust0110ee12009-12-07 09:00:24 -05004622 nfs_restart_rpc(task, data->clp);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004623 return;
4624 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04004625 dprintk("<-- %s\n", __func__);
4626}
4627
4628struct rpc_call_ops nfs4_get_lease_time_ops = {
4629 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4630 .rpc_call_done = nfs4_get_lease_time_done,
4631};
4632
4633int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4634{
4635 struct rpc_task *task;
4636 struct nfs4_get_lease_time_args args;
4637 struct nfs4_get_lease_time_res res = {
4638 .lr_fsinfo = fsinfo,
4639 };
4640 struct nfs4_get_lease_time_data data = {
4641 .args = &args,
4642 .res = &res,
4643 .clp = clp,
4644 };
4645 struct rpc_message msg = {
4646 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4647 .rpc_argp = &args,
4648 .rpc_resp = &res,
4649 };
4650 struct rpc_task_setup task_setup = {
4651 .rpc_client = clp->cl_rpcclient,
4652 .rpc_message = &msg,
4653 .callback_ops = &nfs4_get_lease_time_ops,
4654 .callback_data = &data
4655 };
4656 int status;
4657
4658 res.lr_seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
4659 dprintk("--> %s\n", __func__);
4660 task = rpc_run_task(&task_setup);
4661
4662 if (IS_ERR(task))
4663 status = PTR_ERR(task);
4664 else {
4665 status = task->tk_status;
4666 rpc_put_task(task);
4667 }
4668 dprintk("<-- %s return %d\n", __func__, status);
4669
4670 return status;
4671}
4672
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004673/*
4674 * Reset a slot table
4675 */
Andy Adamson104aeba2010-01-14 17:45:10 -05004676static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
4677 int ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004678{
Andy Adamson104aeba2010-01-14 17:45:10 -05004679 struct nfs4_slot *new = NULL;
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004680 int i;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004681 int ret = 0;
4682
Andy Adamson104aeba2010-01-14 17:45:10 -05004683 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
4684 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004685
Andy Adamson104aeba2010-01-14 17:45:10 -05004686 /* Does the newly negotiated max_reqs match the existing slot table? */
4687 if (max_reqs != tbl->max_slots) {
4688 ret = -ENOMEM;
4689 new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004690 GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05004691 if (!new)
4692 goto out;
4693 ret = 0;
4694 kfree(tbl->slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004695 }
4696 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson104aeba2010-01-14 17:45:10 -05004697 if (new) {
4698 tbl->slots = new;
4699 tbl->max_slots = max_reqs;
4700 }
4701 for (i = 0; i < tbl->max_slots; ++i)
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004702 tbl->slots[i].seq_nr = ivalue;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004703 spin_unlock(&tbl->slot_tbl_lock);
4704 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4705 tbl, tbl->slots, tbl->max_slots);
4706out:
4707 dprintk("<-- %s: return %d\n", __func__, ret);
4708 return ret;
4709}
4710
Andy Adamsonfc931582009-04-01 09:22:31 -04004711/*
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004712 * Reset the forechannel and backchannel slot tables
4713 */
4714static int nfs4_reset_slot_tables(struct nfs4_session *session)
4715{
4716 int status;
4717
4718 status = nfs4_reset_slot_table(&session->fc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05004719 session->fc_attrs.max_reqs, 1);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004720 if (status)
4721 return status;
4722
4723 status = nfs4_reset_slot_table(&session->bc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05004724 session->bc_attrs.max_reqs, 0);
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004725 return status;
4726}
4727
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004728/* Destroy the slot table */
4729static void nfs4_destroy_slot_tables(struct nfs4_session *session)
4730{
4731 if (session->fc_slot_table.slots != NULL) {
4732 kfree(session->fc_slot_table.slots);
4733 session->fc_slot_table.slots = NULL;
4734 }
4735 if (session->bc_slot_table.slots != NULL) {
4736 kfree(session->bc_slot_table.slots);
4737 session->bc_slot_table.slots = NULL;
4738 }
4739 return;
4740}
4741
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004742/*
Andy Adamsonfc931582009-04-01 09:22:31 -04004743 * Initialize slot table
4744 */
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004745static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
4746 int max_slots, int ivalue)
Andy Adamsonfc931582009-04-01 09:22:31 -04004747{
Andy Adamsonfc931582009-04-01 09:22:31 -04004748 struct nfs4_slot *slot;
4749 int ret = -ENOMEM;
4750
4751 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
4752
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004753 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
Andy Adamsonfc931582009-04-01 09:22:31 -04004754
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004755 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_NOFS);
Andy Adamsonfc931582009-04-01 09:22:31 -04004756 if (!slot)
4757 goto out;
Andy Adamsonfc931582009-04-01 09:22:31 -04004758 ret = 0;
4759
4760 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonfc931582009-04-01 09:22:31 -04004761 tbl->max_slots = max_slots;
4762 tbl->slots = slot;
4763 tbl->highest_used_slotid = -1; /* no slot is currently used */
4764 spin_unlock(&tbl->slot_tbl_lock);
4765 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4766 tbl, tbl->slots, tbl->max_slots);
4767out:
4768 dprintk("<-- %s: return %d\n", __func__, ret);
4769 return ret;
Andy Adamsonfc931582009-04-01 09:22:31 -04004770}
4771
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004772/*
4773 * Initialize the forechannel and backchannel tables
4774 */
4775static int nfs4_init_slot_tables(struct nfs4_session *session)
4776{
Trond Myklebustf26468f2009-12-05 19:32:11 -05004777 struct nfs4_slot_table *tbl;
4778 int status = 0;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004779
Trond Myklebustf26468f2009-12-05 19:32:11 -05004780 tbl = &session->fc_slot_table;
4781 if (tbl->slots == NULL) {
4782 status = nfs4_init_slot_table(tbl,
4783 session->fc_attrs.max_reqs, 1);
4784 if (status)
4785 return status;
4786 }
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004787
Trond Myklebustf26468f2009-12-05 19:32:11 -05004788 tbl = &session->bc_slot_table;
4789 if (tbl->slots == NULL) {
4790 status = nfs4_init_slot_table(tbl,
4791 session->bc_attrs.max_reqs, 0);
4792 if (status)
4793 nfs4_destroy_slot_tables(session);
4794 }
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004795
4796 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04004797}
4798
4799struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
4800{
4801 struct nfs4_session *session;
4802 struct nfs4_slot_table *tbl;
4803
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004804 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04004805 if (!session)
4806 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04004807
Andy Adamson76db6d92009-04-01 09:22:38 -04004808 /*
4809 * The create session reply races with the server back
4810 * channel probe. Mark the client NFS_CS_SESSION_INITING
4811 * so that the client back channel can find the
4812 * nfs_client struct
4813 */
4814 clp->cl_cons_state = NFS_CS_SESSION_INITING;
Andy Adamsonea028ac2009-12-04 15:55:38 -05004815 init_completion(&session->complete);
Andy Adamson76db6d92009-04-01 09:22:38 -04004816
Andy Adamson557134a2009-04-01 09:21:53 -04004817 tbl = &session->fc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05004818 tbl->highest_used_slotid = -1;
Andy Adamson557134a2009-04-01 09:21:53 -04004819 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004820 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004821
4822 tbl = &session->bc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05004823 tbl->highest_used_slotid = -1;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004824 spin_lock_init(&tbl->slot_tbl_lock);
4825 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
4826
Andy Adamson557134a2009-04-01 09:21:53 -04004827 session->clp = clp;
4828 return session;
4829}
4830
4831void nfs4_destroy_session(struct nfs4_session *session)
4832{
Andy Adamson5a0ffe52009-04-01 09:23:18 -04004833 nfs4_proc_destroy_session(session);
4834 dprintk("%s Destroy backchannel for xprt %p\n",
4835 __func__, session->clp->cl_rpcclient->cl_xprt);
4836 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
4837 NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004838 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04004839 kfree(session);
4840}
4841
Andy Adamsonfc931582009-04-01 09:22:31 -04004842/*
4843 * Initialize the values to be used by the client in CREATE_SESSION
4844 * If nfs4_init_session set the fore channel request and response sizes,
4845 * use them.
4846 *
4847 * Set the back channel max_resp_sz_cached to zero to force the client to
4848 * always set csa_cachethis to FALSE because the current implementation
4849 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
4850 */
4851static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
4852{
4853 struct nfs4_session *session = args->client->cl_session;
4854 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
4855 mxresp_sz = session->fc_attrs.max_resp_sz;
4856
4857 if (mxrqst_sz == 0)
4858 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
4859 if (mxresp_sz == 0)
4860 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
4861 /* Fore channel attributes */
4862 args->fc_attrs.headerpadsz = 0;
4863 args->fc_attrs.max_rqst_sz = mxrqst_sz;
4864 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04004865 args->fc_attrs.max_ops = NFS4_MAX_OPS;
4866 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
4867
4868 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05004869 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04004870 __func__,
4871 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05004872 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04004873
4874 /* Back channel attributes */
4875 args->bc_attrs.headerpadsz = 0;
4876 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
4877 args->bc_attrs.max_resp_sz = PAGE_SIZE;
4878 args->bc_attrs.max_resp_sz_cached = 0;
4879 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
4880 args->bc_attrs.max_reqs = 1;
4881
4882 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
4883 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4884 __func__,
4885 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
4886 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
4887 args->bc_attrs.max_reqs);
4888}
4889
Andy Adamson8d353012009-04-01 09:22:32 -04004890static int _verify_channel_attr(char *chan, char *attr_name, u32 sent, u32 rcvd)
4891{
4892 if (rcvd <= sent)
4893 return 0;
4894 printk(KERN_WARNING "%s: Session INVALID: %s channel %s increased. "
4895 "sent=%u rcvd=%u\n", __func__, chan, attr_name, sent, rcvd);
4896 return -EINVAL;
4897}
4898
4899#define _verify_fore_channel_attr(_name_) \
4900 _verify_channel_attr("fore", #_name_, \
4901 args->fc_attrs._name_, \
4902 session->fc_attrs._name_)
4903
4904#define _verify_back_channel_attr(_name_) \
4905 _verify_channel_attr("back", #_name_, \
4906 args->bc_attrs._name_, \
4907 session->bc_attrs._name_)
4908
4909/*
4910 * The server is not allowed to increase the fore channel header pad size,
4911 * maximum response size, or maximum number of operations.
4912 *
4913 * The back channel attributes are only negotiatied down: We send what the
4914 * (back channel) server insists upon.
4915 */
4916static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
4917 struct nfs4_session *session)
4918{
4919 int ret = 0;
4920
4921 ret |= _verify_fore_channel_attr(headerpadsz);
4922 ret |= _verify_fore_channel_attr(max_resp_sz);
4923 ret |= _verify_fore_channel_attr(max_ops);
4924
4925 ret |= _verify_back_channel_attr(headerpadsz);
4926 ret |= _verify_back_channel_attr(max_rqst_sz);
4927 ret |= _verify_back_channel_attr(max_resp_sz);
4928 ret |= _verify_back_channel_attr(max_resp_sz_cached);
4929 ret |= _verify_back_channel_attr(max_ops);
4930 ret |= _verify_back_channel_attr(max_reqs);
4931
4932 return ret;
4933}
4934
Andy Adamsonfc931582009-04-01 09:22:31 -04004935static int _nfs4_proc_create_session(struct nfs_client *clp)
4936{
4937 struct nfs4_session *session = clp->cl_session;
4938 struct nfs41_create_session_args args = {
4939 .client = clp,
4940 .cb_program = NFS4_CALLBACK,
4941 };
4942 struct nfs41_create_session_res res = {
4943 .client = clp,
4944 };
4945 struct rpc_message msg = {
4946 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
4947 .rpc_argp = &args,
4948 .rpc_resp = &res,
4949 };
4950 int status;
4951
4952 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04004953 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04004954
4955 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4956
Andy Adamson8d353012009-04-01 09:22:32 -04004957 if (!status)
4958 /* Verify the session's negotiated channel_attrs values */
4959 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04004960 if (!status) {
4961 /* Increment the clientid slot sequence id */
4962 clp->cl_seqid++;
4963 }
4964
4965 return status;
4966}
4967
4968/*
4969 * Issues a CREATE_SESSION operation to the server.
4970 * It is the responsibility of the caller to verify the session is
4971 * expired before calling this routine.
4972 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05004973int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04004974{
4975 int status;
4976 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04004977 struct nfs4_session *session = clp->cl_session;
4978
4979 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
4980
4981 status = _nfs4_proc_create_session(clp);
4982 if (status)
4983 goto out;
4984
Trond Myklebustf26468f2009-12-05 19:32:11 -05004985 /* Init and reset the fore channel */
4986 status = nfs4_init_slot_tables(session);
4987 dprintk("slot table initialization returned %d\n", status);
4988 if (status)
4989 goto out;
4990 status = nfs4_reset_slot_tables(session);
4991 dprintk("slot table reset returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04004992 if (status)
4993 goto out;
4994
4995 ptr = (unsigned *)&session->sess_id.data[0];
4996 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
4997 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04004998out:
4999 dprintk("<-- %s\n", __func__);
5000 return status;
5001}
5002
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005003/*
5004 * Issue the over-the-wire RPC DESTROY_SESSION.
5005 * The caller must serialize access to this routine.
5006 */
5007int nfs4_proc_destroy_session(struct nfs4_session *session)
5008{
5009 int status = 0;
5010 struct rpc_message msg;
5011
5012 dprintk("--> nfs4_proc_destroy_session\n");
5013
5014 /* session is still being setup */
5015 if (session->clp->cl_cons_state != NFS_CS_READY)
5016 return status;
5017
5018 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5019 msg.rpc_argp = session;
5020 msg.rpc_resp = NULL;
5021 msg.rpc_cred = NULL;
5022 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
5023
5024 if (status)
5025 printk(KERN_WARNING
5026 "Got error %d from the server on DESTROY_SESSION. "
5027 "Session has been destroyed regardless...\n", status);
5028
5029 dprintk("<-- nfs4_proc_destroy_session\n");
5030 return status;
5031}
5032
Trond Myklebustfccba802009-07-21 16:48:07 -04005033int nfs4_init_session(struct nfs_server *server)
5034{
5035 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005036 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005037 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005038 int ret;
5039
5040 if (!nfs4_has_session(clp))
5041 return 0;
5042
Andy Adamson68bf05e2009-12-15 12:55:02 -05005043 rsize = server->rsize;
5044 if (rsize == 0)
5045 rsize = NFS_MAX_FILE_IO_SIZE;
5046 wsize = server->wsize;
5047 if (wsize == 0)
5048 wsize = NFS_MAX_FILE_IO_SIZE;
5049
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005050 session = clp->cl_session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005051 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5052 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005053
Trond Myklebustfccba802009-07-21 16:48:07 -04005054 ret = nfs4_recover_expired_lease(server);
5055 if (!ret)
5056 ret = nfs4_check_client_ready(clp);
5057 return ret;
5058}
5059
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005060/*
5061 * Renew the cl_session lease.
5062 */
5063static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5064{
5065 struct nfs4_sequence_args args;
5066 struct nfs4_sequence_res res;
5067
5068 struct rpc_message msg = {
5069 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5070 .rpc_argp = &args,
5071 .rpc_resp = &res,
5072 .rpc_cred = cred,
5073 };
5074
5075 args.sa_cache_this = 0;
5076
5077 return nfs4_call_sync_sequence(clp, clp->cl_rpcclient, &msg, &args,
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005078 &res, args.sa_cache_this, 1);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005079}
5080
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005081static void nfs41_sequence_release(void *data)
5082{
5083 struct nfs_client *clp = (struct nfs_client *)data;
5084
Alexandros Batsakis71358402010-02-05 03:45:05 -08005085 if (atomic_read(&clp->cl_count) > 1)
5086 nfs4_schedule_state_renewal(clp);
5087 nfs_put_client(clp);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005088}
5089
5090static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005091{
5092 struct nfs_client *clp = (struct nfs_client *)data;
5093
5094 nfs41_sequence_done(clp, task->tk_msg.rpc_resp, task->tk_status);
5095
5096 if (task->tk_status < 0) {
5097 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005098 if (atomic_read(&clp->cl_count) == 1)
5099 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005100
5101 if (_nfs4_async_handle_error(task, NULL, clp, NULL)
5102 == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05005103 nfs_restart_rpc(task, clp);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005104 return;
5105 }
5106 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005107 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005108out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005109 kfree(task->tk_msg.rpc_argp);
5110 kfree(task->tk_msg.rpc_resp);
5111
5112 dprintk("<-- %s\n", __func__);
5113}
5114
5115static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5116{
5117 struct nfs_client *clp;
5118 struct nfs4_sequence_args *args;
5119 struct nfs4_sequence_res *res;
5120
5121 clp = (struct nfs_client *)data;
5122 args = task->tk_msg.rpc_argp;
5123 res = task->tk_msg.rpc_resp;
5124
5125 if (nfs4_setup_sequence(clp, args, res, 0, task))
5126 return;
5127 rpc_call_start(task);
5128}
5129
5130static const struct rpc_call_ops nfs41_sequence_ops = {
5131 .rpc_call_done = nfs41_sequence_call_done,
5132 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005133 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005134};
5135
5136static int nfs41_proc_async_sequence(struct nfs_client *clp,
5137 struct rpc_cred *cred)
5138{
5139 struct nfs4_sequence_args *args;
5140 struct nfs4_sequence_res *res;
5141 struct rpc_message msg = {
5142 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5143 .rpc_cred = cred,
5144 };
5145
Alexandros Batsakis71358402010-02-05 03:45:05 -08005146 if (!atomic_inc_not_zero(&clp->cl_count))
5147 return -EIO;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005148 args = kzalloc(sizeof(*args), GFP_NOFS);
5149 res = kzalloc(sizeof(*res), GFP_NOFS);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005150 if (!args || !res) {
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005151 kfree(args);
Dan Carpenter7dd08a52010-03-06 15:02:22 +03005152 kfree(res);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005153 nfs_put_client(clp);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005154 return -ENOMEM;
5155 }
5156 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
5157 msg.rpc_argp = args;
5158 msg.rpc_resp = res;
5159
5160 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
5161 &nfs41_sequence_ops, (void *)clp);
5162}
5163
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005164struct nfs4_reclaim_complete_data {
5165 struct nfs_client *clp;
5166 struct nfs41_reclaim_complete_args arg;
5167 struct nfs41_reclaim_complete_res res;
5168};
5169
5170static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5171{
5172 struct nfs4_reclaim_complete_data *calldata = data;
5173
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005174 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005175 if (nfs4_setup_sequence(calldata->clp, &calldata->arg.seq_args,
5176 &calldata->res.seq_res, 0, task))
5177 return;
5178
5179 rpc_call_start(task);
5180}
5181
5182static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5183{
5184 struct nfs4_reclaim_complete_data *calldata = data;
5185 struct nfs_client *clp = calldata->clp;
5186 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5187
5188 dprintk("--> %s\n", __func__);
5189 nfs41_sequence_done(clp, res, task->tk_status);
5190 switch (task->tk_status) {
5191 case 0:
5192 case -NFS4ERR_COMPLETE_ALREADY:
5193 break;
5194 case -NFS4ERR_BADSESSION:
5195 case -NFS4ERR_DEADSESSION:
5196 /*
5197 * Handle the session error, but do not retry the operation, as
5198 * we have no way of telling whether the clientid had to be
5199 * reset before we got our reply. If reset, a new wave of
5200 * reclaim operations will follow, containing their own reclaim
5201 * complete. We don't want our retry to get on the way of
5202 * recovery by incorrectly indicating to the server that we're
5203 * done reclaiming state since the process had to be restarted.
5204 */
5205 _nfs4_async_handle_error(task, NULL, clp, NULL);
5206 break;
5207 default:
5208 if (_nfs4_async_handle_error(
5209 task, NULL, clp, NULL) == -EAGAIN) {
5210 rpc_restart_call_prepare(task);
5211 return;
5212 }
5213 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005214
5215 dprintk("<-- %s\n", __func__);
5216}
5217
5218static void nfs4_free_reclaim_complete_data(void *data)
5219{
5220 struct nfs4_reclaim_complete_data *calldata = data;
5221
5222 kfree(calldata);
5223}
5224
5225static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5226 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5227 .rpc_call_done = nfs4_reclaim_complete_done,
5228 .rpc_release = nfs4_free_reclaim_complete_data,
5229};
5230
5231/*
5232 * Issue a global reclaim complete.
5233 */
5234static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5235{
5236 struct nfs4_reclaim_complete_data *calldata;
5237 struct rpc_task *task;
5238 struct rpc_message msg = {
5239 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5240 };
5241 struct rpc_task_setup task_setup_data = {
5242 .rpc_client = clp->cl_rpcclient,
5243 .rpc_message = &msg,
5244 .callback_ops = &nfs4_reclaim_complete_call_ops,
5245 .flags = RPC_TASK_ASYNC,
5246 };
5247 int status = -ENOMEM;
5248
5249 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005250 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005251 if (calldata == NULL)
5252 goto out;
5253 calldata->clp = clp;
5254 calldata->arg.one_fs = 0;
5255 calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
5256
5257 msg.rpc_argp = &calldata->arg;
5258 msg.rpc_resp = &calldata->res;
5259 task_setup_data.callback_data = calldata;
5260 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005261 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005262 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005263 goto out;
5264 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005265 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005266 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005267out:
5268 dprintk("<-- %s status=%d\n", __func__, status);
5269 return status;
5270}
Andy Adamson557134a2009-04-01 09:21:53 -04005271#endif /* CONFIG_NFS_V4_1 */
5272
Andy Adamson591d71c2009-04-01 09:22:47 -04005273struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005274 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005275 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005276 .recover_open = nfs4_open_reclaim,
5277 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04005278 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005279 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005280};
5281
Andy Adamson591d71c2009-04-01 09:22:47 -04005282#if defined(CONFIG_NFS_V4_1)
5283struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
5284 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5285 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5286 .recover_open = nfs4_open_reclaim,
5287 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05005288 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005289 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005290 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04005291};
5292#endif /* CONFIG_NFS_V4_1 */
5293
5294struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005295 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005296 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297 .recover_open = nfs4_open_expired,
5298 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04005299 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005300 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005301};
5302
Andy Adamson591d71c2009-04-01 09:22:47 -04005303#if defined(CONFIG_NFS_V4_1)
5304struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
5305 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5306 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5307 .recover_open = nfs4_open_expired,
5308 .recover_lock = nfs4_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05005309 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005310 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04005311};
5312#endif /* CONFIG_NFS_V4_1 */
5313
Benny Halevy29fba382009-04-01 09:22:44 -04005314struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
5315 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04005316 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005317 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04005318};
5319
5320#if defined(CONFIG_NFS_V4_1)
5321struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
5322 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04005323 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005324 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04005325};
5326#endif
5327
5328/*
5329 * Per minor version reboot and network partition recovery ops
5330 */
5331
Andy Adamson591d71c2009-04-01 09:22:47 -04005332struct nfs4_state_recovery_ops *nfs4_reboot_recovery_ops[] = {
5333 &nfs40_reboot_recovery_ops,
5334#if defined(CONFIG_NFS_V4_1)
5335 &nfs41_reboot_recovery_ops,
5336#endif
5337};
5338
5339struct nfs4_state_recovery_ops *nfs4_nograce_recovery_ops[] = {
5340 &nfs40_nograce_recovery_ops,
5341#if defined(CONFIG_NFS_V4_1)
5342 &nfs41_nograce_recovery_ops,
5343#endif
5344};
5345
Benny Halevy29fba382009-04-01 09:22:44 -04005346struct nfs4_state_maintenance_ops *nfs4_state_renewal_ops[] = {
5347 &nfs40_state_renewal_ops,
5348#if defined(CONFIG_NFS_V4_1)
5349 &nfs41_state_renewal_ops,
5350#endif
5351};
5352
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08005353static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005354 .permission = nfs_permission,
5355 .getattr = nfs_getattr,
5356 .setattr = nfs_setattr,
5357 .getxattr = nfs4_getxattr,
5358 .setxattr = nfs4_setxattr,
5359 .listxattr = nfs4_listxattr,
5360};
5361
David Howells509de812006-08-22 20:06:11 -04005362const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363 .version = 4, /* protocol version */
5364 .dentry_ops = &nfs4_dentry_operations,
5365 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005366 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005367 .getroot = nfs4_proc_get_root,
5368 .getattr = nfs4_proc_getattr,
5369 .setattr = nfs4_proc_setattr,
David Howells2b3de442006-08-22 20:06:09 -04005370 .lookupfh = nfs4_proc_lookupfh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371 .lookup = nfs4_proc_lookup,
5372 .access = nfs4_proc_access,
5373 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374 .create = nfs4_proc_create,
5375 .remove = nfs4_proc_remove,
5376 .unlink_setup = nfs4_proc_unlink_setup,
5377 .unlink_done = nfs4_proc_unlink_done,
5378 .rename = nfs4_proc_rename,
5379 .link = nfs4_proc_link,
5380 .symlink = nfs4_proc_symlink,
5381 .mkdir = nfs4_proc_mkdir,
5382 .rmdir = nfs4_proc_remove,
5383 .readdir = nfs4_proc_readdir,
5384 .mknod = nfs4_proc_mknod,
5385 .statfs = nfs4_proc_statfs,
5386 .fsinfo = nfs4_proc_fsinfo,
5387 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04005388 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389 .decode_dirent = nfs4_decode_dirent,
5390 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05005391 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005393 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005394 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005395 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005397 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04005398 .close_context = nfs4_close_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005399};
5400
5401/*
5402 * Local variables:
5403 * c-basic-offset: 8
5404 * End:
5405 */