blob: 75847e4898a09566720219c9c4c4ff5beebaaed9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090042#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/sunrpc/clnt.h>
44#include <linux/nfs.h>
45#include <linux/nfs4.h>
46#include <linux/nfs_fs.h>
47#include <linux/nfs_page.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070049#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040050#include <linux/module.h>
Andy Adamson5a0ffe52009-04-01 09:23:18 -040051#include <linux/sunrpc/bc_xprt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Trond Myklebust4ce79712005-06-22 17:16:21 +000053#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050055#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050056#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040057#include "callback.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59#define NFSDBG_FACILITY NFSDBG_PROC
60
Trond Myklebust2066fe82006-09-15 08:30:46 -040061#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#define NFS4_POLL_RETRY_MAX (15*HZ)
63
Trond Myklebusta78cb572009-08-09 15:06:19 -040064#define NFS4_MAX_LOOP_ON_RECOVER (10)
65
Trond Myklebustcdd4e682006-01-03 09:55:12 +010066struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010067static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080068static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050070static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Trond Myklebust99367812007-07-17 21:52:41 -040071static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
72static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040073static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
74 struct nfs_fattr *fattr, struct iattr *sattr,
75 struct nfs4_state *state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Linus Torvalds1da177e2005-04-16 15:20:36 -070077/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050078static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
Trond Myklebust52567b02009-10-23 14:46:42 -040080 if (err >= -1000)
81 return err;
82 switch (err) {
83 case -NFS4ERR_RESOURCE:
84 return -EREMOTEIO;
85 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -070087 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -040088 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 }
Trond Myklebust52567b02009-10-23 14:46:42 -040090 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
92
93/*
94 * This is our standard bitmap for GETATTR requests.
95 */
96const u32 nfs4_fattr_bitmap[2] = {
97 FATTR4_WORD0_TYPE
98 | FATTR4_WORD0_CHANGE
99 | FATTR4_WORD0_SIZE
100 | FATTR4_WORD0_FSID
101 | FATTR4_WORD0_FILEID,
102 FATTR4_WORD1_MODE
103 | FATTR4_WORD1_NUMLINKS
104 | FATTR4_WORD1_OWNER
105 | FATTR4_WORD1_OWNER_GROUP
106 | FATTR4_WORD1_RAWDEV
107 | FATTR4_WORD1_SPACE_USED
108 | FATTR4_WORD1_TIME_ACCESS
109 | FATTR4_WORD1_TIME_METADATA
110 | FATTR4_WORD1_TIME_MODIFY
111};
112
113const u32 nfs4_statfs_bitmap[2] = {
114 FATTR4_WORD0_FILES_AVAIL
115 | FATTR4_WORD0_FILES_FREE
116 | FATTR4_WORD0_FILES_TOTAL,
117 FATTR4_WORD1_SPACE_AVAIL
118 | FATTR4_WORD1_SPACE_FREE
119 | FATTR4_WORD1_SPACE_TOTAL
120};
121
Trond Myklebust4ce79712005-06-22 17:16:21 +0000122const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 FATTR4_WORD0_MAXLINK
124 | FATTR4_WORD0_MAXNAME,
125 0
126};
127
128const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
129 | FATTR4_WORD0_MAXREAD
130 | FATTR4_WORD0_MAXWRITE
131 | FATTR4_WORD0_LEASE_TIME,
132 0
133};
134
Manoj Naik830b8e32006-06-09 09:34:25 -0400135const u32 nfs4_fs_locations_bitmap[2] = {
136 FATTR4_WORD0_TYPE
137 | FATTR4_WORD0_CHANGE
138 | FATTR4_WORD0_SIZE
139 | FATTR4_WORD0_FSID
140 | FATTR4_WORD0_FILEID
141 | FATTR4_WORD0_FS_LOCATIONS,
142 FATTR4_WORD1_MODE
143 | FATTR4_WORD1_NUMLINKS
144 | FATTR4_WORD1_OWNER
145 | FATTR4_WORD1_OWNER_GROUP
146 | FATTR4_WORD1_RAWDEV
147 | FATTR4_WORD1_SPACE_USED
148 | FATTR4_WORD1_TIME_ACCESS
149 | FATTR4_WORD1_TIME_METADATA
150 | FATTR4_WORD1_TIME_MODIFY
151 | FATTR4_WORD1_MOUNTED_ON_FILEID
152};
153
Al Virobc4785c2006-10-19 23:28:51 -0700154static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 struct nfs4_readdir_arg *readdir)
156{
Al Viro0dbb4c62006-10-19 23:28:49 -0700157 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159 BUG_ON(readdir->count < 80);
160 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000161 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
163 return;
164 }
165
166 readdir->cookie = 0;
167 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
168 if (cookie == 2)
169 return;
170
171 /*
172 * NFSv4 servers do not return entries for '.' and '..'
173 * Therefore, we fake these entries here. We let '.'
174 * have cookie 0 and '..' have cookie 1. Note that
175 * when talking to the server, we always send cookie 0
176 * instead of 1 or 2.
177 */
Al Viro0dbb4c62006-10-19 23:28:49 -0700178 start = p = kmap_atomic(*readdir->pages, KM_USER0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 if (cookie == 0) {
181 *p++ = xdr_one; /* next */
182 *p++ = xdr_zero; /* cookie, first word */
183 *p++ = xdr_one; /* cookie, second word */
184 *p++ = xdr_one; /* entry len */
185 memcpy(p, ".\0\0\0", 4); /* entry */
186 p++;
187 *p++ = xdr_one; /* bitmap length */
188 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
189 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400190 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 }
192
193 *p++ = xdr_one; /* next */
194 *p++ = xdr_zero; /* cookie, first word */
195 *p++ = xdr_two; /* cookie, second word */
196 *p++ = xdr_two; /* entry len */
197 memcpy(p, "..\0\0", 4); /* entry */
198 p++;
199 *p++ = xdr_one; /* bitmap length */
200 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
201 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400202 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204 readdir->pgbase = (char *)p - (char *)start;
205 readdir->count -= readdir->pgbase;
206 kunmap_atomic(start, KM_USER0);
207}
208
Trond Myklebust65de8722008-12-23 15:21:44 -0500209static int nfs4_wait_clnt_recover(struct nfs_client *clp)
210{
211 int res;
212
213 might_sleep();
214
Trond Myklebuste005e802008-12-23 15:21:48 -0500215 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400216 nfs_wait_bit_killable, TASK_KILLABLE);
Trond Myklebust65de8722008-12-23 15:21:44 -0500217 return res;
218}
219
220static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
221{
222 int res = 0;
223
224 might_sleep();
225
226 if (*timeout <= 0)
227 *timeout = NFS4_POLL_RETRY_MIN;
228 if (*timeout > NFS4_POLL_RETRY_MAX)
229 *timeout = NFS4_POLL_RETRY_MAX;
230 schedule_timeout_killable(*timeout);
231 if (fatal_signal_pending(current))
232 res = -ERESTARTSYS;
233 *timeout <<= 1;
234 return res;
235}
236
237/* This is the error handling routine for processes that are allowed
238 * to sleep.
239 */
240static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
241{
242 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500243 struct nfs4_state *state = exception->state;
Trond Myklebust65de8722008-12-23 15:21:44 -0500244 int ret = errorcode;
245
246 exception->retry = 0;
247 switch(errorcode) {
248 case 0:
249 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500250 case -NFS4ERR_ADMIN_REVOKED:
251 case -NFS4ERR_BAD_STATEID:
252 case -NFS4ERR_OPENMODE:
253 if (state == NULL)
254 break;
255 nfs4_state_mark_reclaim_nograce(clp, state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500256 goto do_state_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500257 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500258 if (state == NULL)
259 break;
260 nfs4_state_mark_reclaim_reboot(clp, state);
261 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust65de8722008-12-23 15:21:44 -0500262 case -NFS4ERR_EXPIRED:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500263 goto do_state_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500264#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400265 case -NFS4ERR_BADSESSION:
266 case -NFS4ERR_BADSLOT:
267 case -NFS4ERR_BAD_HIGH_SLOT:
268 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
269 case -NFS4ERR_DEADSESSION:
270 case -NFS4ERR_SEQ_FALSE_RETRY:
271 case -NFS4ERR_SEQ_MISORDERED:
272 dprintk("%s ERROR: %d Reset session\n", __func__,
273 errorcode);
Andy Adamson0b9e2d42009-12-04 16:02:14 -0500274 nfs4_schedule_state_recovery(clp);
Andy Adamson4745e312009-04-01 09:22:42 -0400275 exception->retry = 1;
Andy Adamsonb9179232009-12-04 15:55:32 -0500276 break;
Trond Myklebust03391692010-01-26 15:42:38 -0500277#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500278 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500279 if (exception->timeout > HZ) {
280 /* We have retried a decent amount, time to
281 * fail
282 */
283 ret = -EBUSY;
284 break;
285 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500286 case -NFS4ERR_GRACE:
287 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -0500288 case -EKEYEXPIRED:
Trond Myklebust65de8722008-12-23 15:21:44 -0500289 ret = nfs4_delay(server->client, &exception->timeout);
290 if (ret != 0)
291 break;
292 case -NFS4ERR_OLD_STATEID:
293 exception->retry = 1;
294 }
295 /* We failed to handle the error */
296 return nfs4_map_errors(ret);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500297do_state_recovery:
298 nfs4_schedule_state_recovery(clp);
299 ret = nfs4_wait_clnt_recover(clp);
300 if (ret == 0)
301 exception->retry = 1;
302 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500303}
304
305
Trond Myklebust26e976a2006-01-03 09:55:21 +0100306static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
David Howells7539bba2006-08-22 20:06:09 -0400308 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 spin_lock(&clp->cl_lock);
310 if (time_before(clp->cl_last_renewal,timestamp))
311 clp->cl_last_renewal = timestamp;
312 spin_unlock(&clp->cl_lock);
313}
314
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400315#if defined(CONFIG_NFS_V4_1)
316
Benny Halevy510b8172009-04-01 09:22:14 -0400317/*
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400318 * nfs4_free_slot - free a slot and efficiently update slot table.
319 *
320 * freeing a slot is trivially done by clearing its respective bit
321 * in the bitmap.
322 * If the freed slotid equals highest_used_slotid we want to update it
323 * so that the server would be able to size down the slot table if needed,
324 * otherwise we know that the highest_used_slotid is still in use.
325 * When updating highest_used_slotid there may be "holes" in the bitmap
326 * so we need to scan down from highest_used_slotid to 0 looking for the now
327 * highest slotid in use.
328 * If none found, highest_used_slotid is set to -1.
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500329 *
330 * Must be called while holding tbl->slot_tbl_lock
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400331 */
332static void
333nfs4_free_slot(struct nfs4_slot_table *tbl, u8 free_slotid)
334{
335 int slotid = free_slotid;
336
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400337 /* clear used bit in bitmap */
338 __clear_bit(slotid, tbl->used_slots);
339
340 /* update highest_used_slotid when it is freed */
341 if (slotid == tbl->highest_used_slotid) {
342 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500343 if (slotid < tbl->max_slots)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400344 tbl->highest_used_slotid = slotid;
345 else
346 tbl->highest_used_slotid = -1;
347 }
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400348 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
349 free_slotid, tbl->highest_used_slotid);
350}
351
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800352/*
353 * Signal state manager thread if session is drained
354 */
355static void nfs41_check_drain_session_complete(struct nfs4_session *ses)
356{
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800357 struct rpc_task *task;
358
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800359 if (!test_bit(NFS4CLNT_SESSION_DRAINING, &ses->clp->cl_state)) {
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800360 task = rpc_wake_up_next(&ses->fc_slot_table.slot_tbl_waitq);
361 if (task)
362 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800363 return;
364 }
365
366 if (ses->fc_slot_table.highest_used_slotid != -1)
367 return;
368
369 dprintk("%s COMPLETE: Session Drained\n", __func__);
370 complete(&ses->complete);
371}
372
Trond Myklebustd185a332010-06-16 09:52:25 -0400373static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400374{
375 struct nfs4_slot_table *tbl;
376
Trond Myklebustd185a332010-06-16 09:52:25 -0400377 tbl = &res->sr_session->fc_slot_table;
Andy Adamson13615872009-04-01 09:22:17 -0400378 if (res->sr_slotid == NFS4_MAX_SLOT_TABLE) {
Andy Adamson13615872009-04-01 09:22:17 -0400379 /* just wake up the next guy waiting since
380 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500381 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500382 return;
Andy Adamson13615872009-04-01 09:22:17 -0400383 }
Andy Adamsonea028ac2009-12-04 15:55:38 -0500384
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500385 spin_lock(&tbl->slot_tbl_lock);
386 nfs4_free_slot(tbl, res->sr_slotid);
Trond Myklebustd185a332010-06-16 09:52:25 -0400387 nfs41_check_drain_session_complete(res->sr_session);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500388 spin_unlock(&tbl->slot_tbl_lock);
389 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
Andy Adamson13615872009-04-01 09:22:17 -0400390}
391
Trond Myklebustd185a332010-06-16 09:52:25 -0400392static void nfs41_sequence_done(struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400393{
394 unsigned long timestamp;
395 struct nfs4_slot_table *tbl;
396 struct nfs4_slot *slot;
397
398 /*
399 * sr_status remains 1 if an RPC level error occurred. The server
400 * may or may not have processed the sequence operation..
401 * Proceed as if the server received and processed the sequence
402 * operation.
403 */
404 if (res->sr_status == 1)
405 res->sr_status = NFS_OK;
406
407 /* -ERESTARTSYS can result in skipping nfs41_sequence_setup */
408 if (res->sr_slotid == NFS4_MAX_SLOT_TABLE)
409 goto out;
410
Andy Adamson691daf32009-12-04 15:55:39 -0500411 /* Check the SEQUENCE operation status */
Andy Adamsonb0df8062009-04-01 09:22:18 -0400412 if (res->sr_status == 0) {
Trond Myklebustd185a332010-06-16 09:52:25 -0400413 struct nfs_client *clp = res->sr_session->clp;
414 tbl = &res->sr_session->fc_slot_table;
Andy Adamson691daf32009-12-04 15:55:39 -0500415 slot = tbl->slots + res->sr_slotid;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400416 /* Update the slot's sequence and clientid lease timer */
417 ++slot->seq_nr;
418 timestamp = res->sr_renewal_time;
419 spin_lock(&clp->cl_lock);
420 if (time_before(clp->cl_last_renewal, timestamp))
421 clp->cl_last_renewal = timestamp;
422 spin_unlock(&clp->cl_lock);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500423 /* Check sequence flags */
Alexandros Batsakis71358402010-02-05 03:45:05 -0800424 if (atomic_read(&clp->cl_count) > 1)
425 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400426 }
427out:
428 /* The session may be reset by one of the error handlers. */
429 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400430 nfs41_sequence_free_slot(res);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400431}
432
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400433/*
Benny Halevy510b8172009-04-01 09:22:14 -0400434 * nfs4_find_slot - efficiently look for a free slot
435 *
436 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
437 * If found, we mark the slot as used, update the highest_used_slotid,
438 * and respectively set up the sequence operation args.
439 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400440 *
441 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400442 */
443static u8
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800444nfs4_find_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400445{
446 int slotid;
447 u8 ret_id = NFS4_MAX_SLOT_TABLE;
448 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
449
Benny Halevy510b8172009-04-01 09:22:14 -0400450 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
451 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
452 tbl->max_slots);
453 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
454 if (slotid >= tbl->max_slots)
455 goto out;
456 __set_bit(slotid, tbl->used_slots);
457 if (slotid > tbl->highest_used_slotid)
458 tbl->highest_used_slotid = slotid;
459 ret_id = slotid;
460out:
461 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
462 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400463 return ret_id;
464}
465
Andy Adamsonce5039c2009-04-01 09:22:13 -0400466static int nfs41_setup_sequence(struct nfs4_session *session,
467 struct nfs4_sequence_args *args,
468 struct nfs4_sequence_res *res,
469 int cache_reply,
470 struct rpc_task *task)
471{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400472 struct nfs4_slot *slot;
473 struct nfs4_slot_table *tbl;
474 u8 slotid;
475
476 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400477 /* slot already allocated? */
478 if (res->sr_slotid != NFS4_MAX_SLOT_TABLE)
479 return 0;
480
Andy Adamsonce5039c2009-04-01 09:22:13 -0400481 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400482 tbl = &session->fc_slot_table;
483
484 spin_lock(&tbl->slot_tbl_lock);
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800485 if (test_bit(NFS4CLNT_SESSION_DRAINING, &session->clp->cl_state) &&
486 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamsonea028ac2009-12-04 15:55:38 -0500487 /*
488 * The state manager will wait until the slot table is empty.
489 * Schedule the reset thread
490 */
Andy Adamson05f0d232009-12-04 15:55:37 -0500491 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400492 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500493 dprintk("%s Schedule Session Reset\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500494 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400495 }
496
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800497 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
498 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
499 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
500 spin_unlock(&tbl->slot_tbl_lock);
501 dprintk("%s enforce FIFO order\n", __func__);
502 return -EAGAIN;
503 }
504
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800505 slotid = nfs4_find_slot(tbl);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400506 if (slotid == NFS4_MAX_SLOT_TABLE) {
507 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
508 spin_unlock(&tbl->slot_tbl_lock);
509 dprintk("<-- %s: no free slots\n", __func__);
510 return -EAGAIN;
511 }
512 spin_unlock(&tbl->slot_tbl_lock);
513
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800514 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400515 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400516 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400517 args->sa_slotid = slotid;
518 args->sa_cache_this = cache_reply;
519
520 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
521
Benny Halevy99fe60d2009-04-01 09:22:29 -0400522 res->sr_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400523 res->sr_slotid = slotid;
524 res->sr_renewal_time = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400525 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400526 /*
527 * sr_status is only set in decode_sequence, and so will remain
528 * set to 1 if an rpc level failure occurs.
529 */
530 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400531 return 0;
532}
533
534int nfs4_setup_sequence(struct nfs_client *clp,
535 struct nfs4_sequence_args *args,
536 struct nfs4_sequence_res *res,
537 int cache_reply,
538 struct rpc_task *task)
539{
540 int ret = 0;
541
542 dprintk("--> %s clp %p session %p sr_slotid %d\n",
543 __func__, clp, clp->cl_session, res->sr_slotid);
544
545 if (!nfs4_has_session(clp))
546 goto out;
547 ret = nfs41_setup_sequence(clp->cl_session, args, res, cache_reply,
548 task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400549out:
550 dprintk("<-- %s status=%d\n", __func__, ret);
551 return ret;
552}
553
554struct nfs41_call_sync_data {
555 struct nfs_client *clp;
556 struct nfs4_sequence_args *seq_args;
557 struct nfs4_sequence_res *seq_res;
558 int cache_reply;
559};
560
561static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
562{
563 struct nfs41_call_sync_data *data = calldata;
564
565 dprintk("--> %s data->clp->cl_session %p\n", __func__,
566 data->clp->cl_session);
567 if (nfs4_setup_sequence(data->clp, data->seq_args,
568 data->seq_res, data->cache_reply, task))
569 return;
570 rpc_call_start(task);
571}
572
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800573static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
574{
575 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
576 nfs41_call_sync_prepare(task, calldata);
577}
578
Andy Adamson69ab40c2009-04-01 09:22:19 -0400579static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
580{
581 struct nfs41_call_sync_data *data = calldata;
582
Trond Myklebustd185a332010-06-16 09:52:25 -0400583 nfs41_sequence_done(data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400584}
585
Andy Adamsonce5039c2009-04-01 09:22:13 -0400586struct rpc_call_ops nfs41_call_sync_ops = {
587 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400588 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400589};
590
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800591struct rpc_call_ops nfs41_call_priv_sync_ops = {
592 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
593 .rpc_call_done = nfs41_call_sync_done,
594};
595
Andy Adamsonce5039c2009-04-01 09:22:13 -0400596static int nfs4_call_sync_sequence(struct nfs_client *clp,
597 struct rpc_clnt *clnt,
598 struct rpc_message *msg,
599 struct nfs4_sequence_args *args,
600 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800601 int cache_reply,
602 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400603{
604 int ret;
605 struct rpc_task *task;
606 struct nfs41_call_sync_data data = {
607 .clp = clp,
608 .seq_args = args,
609 .seq_res = res,
610 .cache_reply = cache_reply,
611 };
612 struct rpc_task_setup task_setup = {
613 .rpc_client = clnt,
614 .rpc_message = msg,
615 .callback_ops = &nfs41_call_sync_ops,
616 .callback_data = &data
617 };
618
619 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800620 if (privileged)
621 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400622 task = rpc_run_task(&task_setup);
623 if (IS_ERR(task))
624 ret = PTR_ERR(task);
625 else {
626 ret = task->tk_status;
627 rpc_put_task(task);
628 }
629 return ret;
630}
631
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400632int _nfs4_call_sync_session(struct nfs_server *server,
633 struct rpc_message *msg,
634 struct nfs4_sequence_args *args,
635 struct nfs4_sequence_res *res,
636 int cache_reply)
637{
Andy Adamsonce5039c2009-04-01 09:22:13 -0400638 return nfs4_call_sync_sequence(server->nfs_client, server->client,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800639 msg, args, res, cache_reply, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400640}
641
642#endif /* CONFIG_NFS_V4_1 */
643
644int _nfs4_call_sync(struct nfs_server *server,
645 struct rpc_message *msg,
646 struct nfs4_sequence_args *args,
647 struct nfs4_sequence_res *res,
648 int cache_reply)
649{
Benny Halevyf3752972009-04-01 09:22:04 -0400650 args->sa_session = res->sr_session = NULL;
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400651 return rpc_call_sync(server->client, msg, 0);
652}
653
654#define nfs4_call_sync(server, msg, args, res, cache_reply) \
655 (server)->nfs_client->cl_call_sync((server), (msg), &(args)->seq_args, \
656 &(res)->seq_res, (cache_reply))
657
Andy Adamsonb0df8062009-04-01 09:22:18 -0400658static void nfs4_sequence_done(const struct nfs_server *server,
659 struct nfs4_sequence_res *res, int rpc_status)
660{
661#ifdef CONFIG_NFS_V4_1
662 if (nfs4_has_session(server->nfs_client))
Trond Myklebustd185a332010-06-16 09:52:25 -0400663 nfs41_sequence_done(res);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400664#endif /* CONFIG_NFS_V4_1 */
665}
666
Trond Myklebust38478b22006-05-25 01:40:57 -0400667static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
Trond Myklebust38478b22006-05-25 01:40:57 -0400669 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Trond Myklebust38478b22006-05-25 01:40:57 -0400671 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400672 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
673 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400674 nfs_force_lookup_revalidate(dir);
Trond Myklebust40d24702007-10-08 09:24:22 -0400675 nfsi->change_attr = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400676 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677}
678
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100679struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400680 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100681 struct nfs_openargs o_arg;
682 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100683 struct nfs_open_confirmargs c_arg;
684 struct nfs_open_confirmres c_res;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100685 struct nfs_fattr f_attr;
686 struct nfs_fattr dir_attr;
Trond Myklebustad389da2007-06-05 12:30:00 -0400687 struct path path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100688 struct dentry *dir;
689 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400690 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100691 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100692 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400693 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100694 int rpc_status;
695 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100696};
697
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400698
699static void nfs4_init_opendata_res(struct nfs4_opendata *p)
700{
701 p->o_res.f_attr = &p->f_attr;
702 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400703 p->o_res.seqid = p->o_arg.seqid;
704 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400705 p->o_res.server = p->o_arg.server;
706 nfs_fattr_init(&p->f_attr);
707 nfs_fattr_init(&p->dir_attr);
Benny Halevy578e4582009-06-18 22:01:23 -0400708 p->o_res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400709}
710
Trond Myklebustad389da2007-06-05 12:30:00 -0400711static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500712 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400713 const struct iattr *attrs,
714 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100715{
Trond Myklebustad389da2007-06-05 12:30:00 -0400716 struct dentry *parent = dget_parent(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100717 struct inode *dir = parent->d_inode;
718 struct nfs_server *server = NFS_SERVER(dir);
719 struct nfs4_opendata *p;
720
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400721 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100722 if (p == NULL)
723 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400724 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100725 if (p->o_arg.seqid == NULL)
726 goto err_free;
Al Virof6948692010-01-30 13:51:04 -0500727 path_get(path);
728 p->path = *path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100729 p->dir = parent;
730 p->owner = sp;
731 atomic_inc(&sp->so_count);
732 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500733 p->o_arg.open_flags = flags;
734 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400735 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400736 p->o_arg.id = sp->so_owner_id.id;
Trond Myklebustad389da2007-06-05 12:30:00 -0400737 p->o_arg.name = &p->path.dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100738 p->o_arg.server = server;
739 p->o_arg.bitmask = server->attr_bitmask;
740 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100741 if (flags & O_EXCL) {
Alexandros Batsakis4882ef72009-12-05 13:30:21 -0500742 if (nfs4_has_persistent_session(server->nfs_client)) {
743 /* GUARDED */
744 p->o_arg.u.attrs = &p->attrs;
745 memcpy(&p->attrs, attrs, sizeof(p->attrs));
746 } else { /* EXCLUSIVE4_1 */
747 u32 *s = (u32 *) p->o_arg.u.verifier.data;
748 s[0] = jiffies;
749 s[1] = current->pid;
750 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100751 } else if (flags & O_CREAT) {
752 p->o_arg.u.attrs = &p->attrs;
753 memcpy(&p->attrs, attrs, sizeof(p->attrs));
754 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100755 p->c_arg.fh = &p->o_res.fh;
756 p->c_arg.stateid = &p->o_res.stateid;
757 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400758 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400759 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100760 return p;
761err_free:
762 kfree(p);
763err:
764 dput(parent);
765 return NULL;
766}
767
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400768static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100769{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400770 struct nfs4_opendata *p = container_of(kref,
771 struct nfs4_opendata, kref);
772
773 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400774 if (p->state != NULL)
775 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400776 nfs4_put_state_owner(p->owner);
777 dput(p->dir);
Jan Blunck31f31db12008-05-02 13:42:45 -0700778 path_put(&p->path);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400779 kfree(p);
780}
781
782static void nfs4_opendata_put(struct nfs4_opendata *p)
783{
784 if (p != NULL)
785 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100786}
787
Trond Myklebust06f814a2006-01-03 09:55:07 +0100788static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
789{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100790 int ret;
791
Trond Myklebust06f814a2006-01-03 09:55:07 +0100792 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100793 return ret;
794}
795
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500796static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400797{
798 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500799
800 if (open_mode & O_EXCL)
801 goto out;
802 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400803 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500804 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
805 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400806 break;
807 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500808 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
809 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400810 break;
811 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500812 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
813 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400814 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500815out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400816 return ret;
817}
818
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500819static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400820{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500821 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400822 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500823 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400824 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500825 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400826 return 1;
827}
828
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500829static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100830{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500831 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100832 case FMODE_WRITE:
833 state->n_wronly++;
834 break;
835 case FMODE_READ:
836 state->n_rdonly++;
837 break;
838 case FMODE_READ|FMODE_WRITE:
839 state->n_rdwr++;
840 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500841 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100842}
843
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500844static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400845{
846 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
847 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
848 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500849 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400850 case FMODE_READ:
851 set_bit(NFS_O_RDONLY_STATE, &state->flags);
852 break;
853 case FMODE_WRITE:
854 set_bit(NFS_O_WRONLY_STATE, &state->flags);
855 break;
856 case FMODE_READ|FMODE_WRITE:
857 set_bit(NFS_O_RDWR_STATE, &state->flags);
858 }
859}
860
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500861static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400862{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400863 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500864 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400865 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400866}
867
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500868static 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 -0700869{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400870 /*
871 * Protect the call to nfs4_state_set_mode_locked and
872 * serialise the stateid update
873 */
874 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400875 if (deleg_stateid != NULL) {
876 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
877 set_bit(NFS_DELEGATED_STATE, &state->flags);
878 }
879 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500880 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400881 write_sequnlock(&state->seqlock);
882 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500883 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700884 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885}
886
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500887static 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 -0500888{
889 struct nfs_inode *nfsi = NFS_I(state->inode);
890 struct nfs_delegation *deleg_cur;
891 int ret = 0;
892
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500893 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500894
895 rcu_read_lock();
896 deleg_cur = rcu_dereference(nfsi->delegation);
897 if (deleg_cur == NULL)
898 goto no_delegation;
899
900 spin_lock(&deleg_cur->lock);
901 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500902 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500903 goto no_delegation_unlock;
904
905 if (delegation == NULL)
906 delegation = &deleg_cur->stateid;
907 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
908 goto no_delegation_unlock;
909
Trond Myklebustb7391f42008-12-23 15:21:52 -0500910 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500911 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500912 ret = 1;
913no_delegation_unlock:
914 spin_unlock(&deleg_cur->lock);
915no_delegation:
916 rcu_read_unlock();
917
918 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500919 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500920 ret = 1;
921 }
922
923 return ret;
924}
925
926
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500927static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400928{
929 struct nfs_delegation *delegation;
930
931 rcu_read_lock();
932 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500933 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -0400934 rcu_read_unlock();
935 return;
936 }
937 rcu_read_unlock();
938 nfs_inode_return_delegation(inode);
939}
940
Trond Myklebust6ee41262007-07-08 14:11:36 -0400941static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400942{
943 struct nfs4_state *state = opendata->state;
944 struct nfs_inode *nfsi = NFS_I(state->inode);
945 struct nfs_delegation *delegation;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500946 int open_mode = opendata->o_arg.open_flags & O_EXCL;
947 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400948 nfs4_stateid stateid;
949 int ret = -EAGAIN;
950
Trond Myklebustaac00a82007-07-05 19:02:21 -0400951 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500952 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400953 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500954 if (can_open_cached(state, fmode, open_mode)) {
955 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400956 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400957 goto out_return_state;
958 }
959 spin_unlock(&state->owner->so_lock);
960 }
Trond Myklebust34310432008-12-23 15:21:38 -0500961 rcu_read_lock();
962 delegation = rcu_dereference(nfsi->delegation);
963 if (delegation == NULL ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500964 !can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -0500965 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -0400966 break;
Trond Myklebust34310432008-12-23 15:21:38 -0500967 }
Trond Myklebustaac00a82007-07-05 19:02:21 -0400968 /* Save the delegation */
969 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
970 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -0400971 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400972 if (ret != 0)
973 goto out;
974 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -0500975
976 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500977 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -0500978 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400979 }
Trond Myklebustaac00a82007-07-05 19:02:21 -0400980out:
981 return ERR_PTR(ret);
982out_return_state:
983 atomic_inc(&state->count);
984 return state;
985}
986
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100987static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
988{
989 struct inode *inode;
990 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -0400991 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400992 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100993
Trond Myklebustaac00a82007-07-05 19:02:21 -0400994 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400995 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400996 goto out;
997 }
998
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400999 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001000 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001001 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001002 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001003 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001004 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001005 goto err;
1006 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001007 state = nfs4_get_open_state(inode, data->owner);
1008 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001009 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001010 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001011 int delegation_flags = 0;
1012
Trond Myklebust003707c2007-07-05 18:07:55 -04001013 rcu_read_lock();
1014 delegation = rcu_dereference(NFS_I(inode)->delegation);
1015 if (delegation)
1016 delegation_flags = delegation->flags;
1017 rcu_read_unlock();
Trond Myklebust15c831b2008-12-23 15:21:39 -05001018 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001019 nfs_inode_set_delegation(state->inode,
1020 data->owner->so_cred,
1021 &data->o_res);
1022 else
1023 nfs_inode_reclaim_delegation(state->inode,
1024 data->owner->so_cred,
1025 &data->o_res);
1026 }
Trond Myklebust34310432008-12-23 15:21:38 -05001027
1028 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001029 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001030 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001031out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001032 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001033err_put_inode:
1034 iput(inode);
1035err:
1036 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001037}
1038
Trond Myklebust864472e2006-01-03 09:55:15 +01001039static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1040{
1041 struct nfs_inode *nfsi = NFS_I(state->inode);
1042 struct nfs_open_context *ctx;
1043
1044 spin_lock(&state->inode->i_lock);
1045 list_for_each_entry(ctx, &nfsi->open_files, list) {
1046 if (ctx->state != state)
1047 continue;
1048 get_nfs_open_context(ctx);
1049 spin_unlock(&state->inode->i_lock);
1050 return ctx;
1051 }
1052 spin_unlock(&state->inode->i_lock);
1053 return ERR_PTR(-ENOENT);
1054}
1055
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001056static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1057{
1058 struct nfs4_opendata *opendata;
1059
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001060 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001061 if (opendata == NULL)
1062 return ERR_PTR(-ENOMEM);
1063 opendata->state = state;
1064 atomic_inc(&state->count);
1065 return opendata;
1066}
1067
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001068static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001069{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001070 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001071 int ret;
1072
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001073 opendata->o_arg.open_flags = 0;
1074 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001075 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1076 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1077 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001078 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001079 if (ret != 0)
1080 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001081 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001082 if (IS_ERR(newstate))
1083 return PTR_ERR(newstate);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001084 nfs4_close_state(&opendata->path, newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001085 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001086 return 0;
1087}
1088
1089static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1090{
Trond Myklebust864472e2006-01-03 09:55:15 +01001091 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001092 int ret;
1093
1094 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001095 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001096 smp_rmb();
1097 if (state->n_rdwr != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001098 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001099 if (ret != 0)
1100 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001101 if (newstate != state)
1102 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001103 }
1104 if (state->n_wronly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001105 ret = nfs4_open_recover_helper(opendata, 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_rdonly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001112 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &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 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001118 /*
1119 * We may have performed cached opens for all three recoveries.
1120 * Check if we need to update the current stateid.
1121 */
1122 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1123 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001124 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001125 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1126 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001127 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001128 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001129 return 0;
1130}
1131
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132/*
1133 * OPEN_RECLAIM:
1134 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001136static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001138 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001139 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001140 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 int status;
1142
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001143 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1144 if (IS_ERR(opendata))
1145 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001146 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1147 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001148 rcu_read_lock();
1149 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001150 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001151 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001152 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001153 opendata->o_arg.u.delegation_type = delegation_type;
1154 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001155 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 return status;
1157}
1158
Trond Myklebust539cd032007-06-05 11:46:42 -04001159static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160{
1161 struct nfs_server *server = NFS_SERVER(state->inode);
1162 struct nfs4_exception exception = { };
1163 int err;
1164 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001165 err = _nfs4_do_open_reclaim(ctx, state);
Jeff Layton2c643482010-01-07 09:42:03 -05001166 if (err != -NFS4ERR_DELAY && err != -EKEYEXPIRED)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001167 break;
1168 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 } while (exception.retry);
1170 return err;
1171}
1172
Trond Myklebust864472e2006-01-03 09:55:15 +01001173static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1174{
1175 struct nfs_open_context *ctx;
1176 int ret;
1177
1178 ctx = nfs4_state_find_open_context(state);
1179 if (IS_ERR(ctx))
1180 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001181 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001182 put_nfs_open_context(ctx);
1183 return ret;
1184}
1185
Trond Myklebust13437e12007-07-06 15:10:43 -04001186static 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 -07001187{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001188 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001189 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001191 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1192 if (IS_ERR(opendata))
1193 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001194 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -04001195 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001196 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +01001197 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001198 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001199 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200}
1201
Trond Myklebust13437e12007-07-06 15:10:43 -04001202int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203{
1204 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001205 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 int err;
1207 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001208 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 switch (err) {
1210 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001211 case -ENOENT:
1212 case -ESTALE:
1213 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001214 case -NFS4ERR_BADSESSION:
1215 case -NFS4ERR_BADSLOT:
1216 case -NFS4ERR_BAD_HIGH_SLOT:
1217 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1218 case -NFS4ERR_DEADSESSION:
1219 nfs4_schedule_state_recovery(
1220 server->nfs_client);
1221 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 case -NFS4ERR_STALE_CLIENTID:
1223 case -NFS4ERR_STALE_STATEID:
1224 case -NFS4ERR_EXPIRED:
1225 /* Don't recall a delegation if it was lost */
David Howells7539bba2006-08-22 20:06:09 -04001226 nfs4_schedule_state_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001227 goto out;
1228 case -ERESTARTSYS:
1229 /*
1230 * The show must go on: exit, but mark the
1231 * stateid as needing recovery.
1232 */
1233 case -NFS4ERR_ADMIN_REVOKED:
1234 case -NFS4ERR_BAD_STATEID:
1235 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
1236 case -ENOMEM:
1237 err = 0;
1238 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 }
1240 err = nfs4_handle_exception(server, err, &exception);
1241 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001242out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 return err;
1244}
1245
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001246static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1247{
1248 struct nfs4_opendata *data = calldata;
1249
1250 data->rpc_status = task->tk_status;
1251 if (RPC_ASSASSINATED(task))
1252 return;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001253 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001254 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1255 sizeof(data->o_res.stateid.data));
Trond Myklebustbb226292008-01-02 15:19:18 -05001256 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001257 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001258 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001259 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001260}
1261
1262static void nfs4_open_confirm_release(void *calldata)
1263{
1264 struct nfs4_opendata *data = calldata;
1265 struct nfs4_state *state = NULL;
1266
1267 /* If this request hasn't been cancelled, do nothing */
1268 if (data->cancelled == 0)
1269 goto out_free;
1270 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001271 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001272 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001273 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001274 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001275 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001276out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001277 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001278}
1279
1280static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001281 .rpc_call_done = nfs4_open_confirm_done,
1282 .rpc_release = nfs4_open_confirm_release,
1283};
1284
1285/*
1286 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1287 */
1288static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1289{
1290 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1291 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001292 struct rpc_message msg = {
1293 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1294 .rpc_argp = &data->c_arg,
1295 .rpc_resp = &data->c_res,
1296 .rpc_cred = data->owner->so_cred,
1297 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001298 struct rpc_task_setup task_setup_data = {
1299 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001300 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001301 .callback_ops = &nfs4_open_confirm_ops,
1302 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001303 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001304 .flags = RPC_TASK_ASYNC,
1305 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 int status;
1307
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001308 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001309 data->rpc_done = 0;
1310 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001311 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001312 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001313 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001314 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001315 status = nfs4_wait_for_completion_rpc_task(task);
1316 if (status != 0) {
1317 data->cancelled = 1;
1318 smp_wmb();
1319 } else
1320 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001321 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 return status;
1323}
1324
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001325static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001327 struct nfs4_opendata *data = calldata;
1328 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001329
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001330 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1331 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001332 /*
1333 * Check if we still need to send an OPEN call, or if we can use
1334 * a delegation instead.
1335 */
1336 if (data->state != NULL) {
1337 struct nfs_delegation *delegation;
1338
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001339 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001340 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001341 rcu_read_lock();
1342 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1343 if (delegation != NULL &&
Trond Myklebust15c831b2008-12-23 15:21:39 -05001344 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001345 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001346 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001347 }
1348 rcu_read_unlock();
1349 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001350 /* Update sequence id. */
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001351 data->o_arg.id = sp->so_owner_id.id;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001352 data->o_arg.clientid = sp->so_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001353 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001354 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001355 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1356 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001357 data->timestamp = jiffies;
Andy Adamsond8985282009-04-01 09:22:21 -04001358 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
1359 &data->o_arg.seq_args,
1360 &data->o_res.seq_res, 1, task))
1361 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001362 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001363 return;
1364out_no_action:
1365 task->tk_action = NULL;
1366
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001367}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001369static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1370{
1371 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1372 nfs4_open_prepare(task, calldata);
1373}
1374
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001375static void nfs4_open_done(struct rpc_task *task, void *calldata)
1376{
1377 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001379 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001380
Trond Myklebustd61e6122009-12-05 19:32:19 -05001381 nfs4_sequence_done(data->o_arg.server, &data->o_res.seq_res,
1382 task->tk_status);
Andy Adamsond8985282009-04-01 09:22:21 -04001383
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001384 if (RPC_ASSASSINATED(task))
1385 return;
1386 if (task->tk_status == 0) {
1387 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001388 case S_IFREG:
1389 break;
1390 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001391 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001392 break;
1393 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001394 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001395 break;
1396 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001397 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001398 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001399 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001400 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1401 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001402 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001403 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001404}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001405
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001406static void nfs4_open_release(void *calldata)
1407{
1408 struct nfs4_opendata *data = calldata;
1409 struct nfs4_state *state = NULL;
1410
1411 /* If this request hasn't been cancelled, do nothing */
1412 if (data->cancelled == 0)
1413 goto out_free;
1414 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001415 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001416 goto out_free;
1417 /* In case we need an open_confirm, no cleanup! */
1418 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1419 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001420 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001421 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001422 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001423out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001424 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001425}
1426
1427static const struct rpc_call_ops nfs4_open_ops = {
1428 .rpc_call_prepare = nfs4_open_prepare,
1429 .rpc_call_done = nfs4_open_done,
1430 .rpc_release = nfs4_open_release,
1431};
1432
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001433static const struct rpc_call_ops nfs4_recover_open_ops = {
1434 .rpc_call_prepare = nfs4_recover_open_prepare,
1435 .rpc_call_done = nfs4_open_done,
1436 .rpc_release = nfs4_open_release,
1437};
1438
1439static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001440{
1441 struct inode *dir = data->dir->d_inode;
1442 struct nfs_server *server = NFS_SERVER(dir);
1443 struct nfs_openargs *o_arg = &data->o_arg;
1444 struct nfs_openres *o_res = &data->o_res;
1445 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001446 struct rpc_message msg = {
1447 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1448 .rpc_argp = o_arg,
1449 .rpc_resp = o_res,
1450 .rpc_cred = data->owner->so_cred,
1451 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001452 struct rpc_task_setup task_setup_data = {
1453 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001454 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001455 .callback_ops = &nfs4_open_ops,
1456 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001457 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001458 .flags = RPC_TASK_ASYNC,
1459 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001460 int status;
1461
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001462 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001463 data->rpc_done = 0;
1464 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001465 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001466 if (isrecover)
1467 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001468 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001469 if (IS_ERR(task))
1470 return PTR_ERR(task);
1471 status = nfs4_wait_for_completion_rpc_task(task);
1472 if (status != 0) {
1473 data->cancelled = 1;
1474 smp_wmb();
1475 } else
1476 status = data->rpc_status;
1477 rpc_put_task(task);
1478
1479 return status;
1480}
1481
1482static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1483{
1484 struct inode *dir = data->dir->d_inode;
1485 struct nfs_openres *o_res = &data->o_res;
1486 int status;
1487
1488 status = nfs4_run_open_task(data, 1);
1489 if (status != 0 || !data->rpc_done)
1490 return status;
1491
1492 nfs_refresh_inode(dir, o_res->dir_attr);
1493
1494 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1495 status = _nfs4_proc_open_confirm(data);
1496 if (status != 0)
1497 return status;
1498 }
1499
1500 return status;
1501}
1502
1503/*
1504 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1505 */
1506static int _nfs4_proc_open(struct nfs4_opendata *data)
1507{
1508 struct inode *dir = data->dir->d_inode;
1509 struct nfs_server *server = NFS_SERVER(dir);
1510 struct nfs_openargs *o_arg = &data->o_arg;
1511 struct nfs_openres *o_res = &data->o_res;
1512 int status;
1513
1514 status = nfs4_run_open_task(data, 0);
Trond Myklebust3e309912007-07-07 13:19:59 -04001515 if (status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001516 return status;
1517
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001518 if (o_arg->open_flags & O_CREAT) {
1519 update_changeattr(dir, &o_res->cinfo);
1520 nfs_post_op_update_inode(dir, o_res->dir_attr);
1521 } else
1522 nfs_refresh_inode(dir, o_res->dir_attr);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001523 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1524 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001526 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001528 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 }
1530 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001531 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001532 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533}
1534
Trond Myklebust10afec92007-05-14 17:16:04 -04001535static int nfs4_recover_expired_lease(struct nfs_server *server)
Trond Myklebust58d97142006-01-03 09:55:24 +01001536{
David Howells7539bba2006-08-22 20:06:09 -04001537 struct nfs_client *clp = server->nfs_client;
Trond Myklebusta78cb572009-08-09 15:06:19 -04001538 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001539 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001540
Trond Myklebusta78cb572009-08-09 15:06:19 -04001541 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001542 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001543 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001544 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001545 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1546 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001547 break;
Trond Myklebust58d97142006-01-03 09:55:24 +01001548 nfs4_schedule_state_recovery(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001549 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001550 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001551 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001552}
1553
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554/*
1555 * OPEN_EXPIRED:
1556 * reclaim state on the server after a network partition.
1557 * Assumes caller holds the appropriate lock
1558 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001559static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001561 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001562 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001564 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1565 if (IS_ERR(opendata))
1566 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001567 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001568 if (ret == -ESTALE)
Trond Myklebust539cd032007-06-05 11:46:42 -04001569 d_drop(ctx->path.dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001570 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001571 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572}
1573
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001574static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001575{
Trond Myklebust539cd032007-06-05 11:46:42 -04001576 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001577 struct nfs4_exception exception = { };
1578 int err;
1579
1580 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001581 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001582 switch (err) {
1583 default:
1584 goto out;
1585 case -NFS4ERR_GRACE:
1586 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -05001587 case -EKEYEXPIRED:
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001588 nfs4_handle_exception(server, err, &exception);
1589 err = 0;
1590 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001591 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001592out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001593 return err;
1594}
1595
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1597{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001599 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
Trond Myklebust864472e2006-01-03 09:55:15 +01001601 ctx = nfs4_state_find_open_context(state);
1602 if (IS_ERR(ctx))
1603 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001604 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001605 put_nfs_open_context(ctx);
1606 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607}
1608
1609/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001610 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1611 * fields corresponding to attributes that were used to store the verifier.
1612 * Make sure we clobber those fields in the later setattr call
1613 */
1614static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1615{
1616 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1617 !(sattr->ia_valid & ATTR_ATIME_SET))
1618 sattr->ia_valid |= ATTR_ATIME;
1619
1620 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1621 !(sattr->ia_valid & ATTR_MTIME_SET))
1622 sattr->ia_valid |= ATTR_MTIME;
1623}
1624
1625/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001626 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001628static 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 -07001629{
1630 struct nfs4_state_owner *sp;
1631 struct nfs4_state *state = NULL;
1632 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001633 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001634 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
1636 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 status = -ENOMEM;
1638 if (!(sp = nfs4_get_state_owner(server, cred))) {
1639 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1640 goto out_err;
1641 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001642 status = nfs4_recover_expired_lease(server);
1643 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001644 goto err_put_state_owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001645 if (path->dentry->d_inode != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001646 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001647 status = -ENOMEM;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001648 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001649 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001650 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
Trond Myklebustaac00a82007-07-05 19:02:21 -04001652 if (path->dentry->d_inode != NULL)
1653 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1654
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001655 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001657 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001659 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001660 status = PTR_ERR(state);
1661 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001662 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001663 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001664 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001665
1666 if (opendata->o_arg.open_flags & O_EXCL) {
1667 nfs4_exclusive_attrset(opendata, sattr);
1668
1669 nfs_fattr_init(opendata->o_res.f_attr);
1670 status = nfs4_do_setattr(state->inode, cred,
1671 opendata->o_res.f_attr, sattr,
1672 state);
1673 if (status == 0)
1674 nfs_setattr_update_inode(state->inode, sattr);
1675 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1676 }
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001677 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 *res = state;
1680 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001681err_opendata_put:
1682 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001683err_put_state_owner:
1684 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 *res = NULL;
1687 return status;
1688}
1689
1690
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001691static 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 -07001692{
1693 struct nfs4_exception exception = { };
1694 struct nfs4_state *res;
1695 int status;
1696
1697 do {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001698 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 if (status == 0)
1700 break;
1701 /* NOTE: BAD_SEQID means the server and client disagree about the
1702 * book-keeping w.r.t. state-changing operations
1703 * (OPEN/CLOSE/LOCK/LOCKU...)
1704 * It is actually a sign of a bug on the client or on the server.
1705 *
1706 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001707 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 * have unhashed the old state_owner for us, and that we can
1709 * therefore safely retry using a new one. We should still warn
1710 * the user though...
1711 */
1712 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001713 printk(KERN_WARNING "NFS: v4 server %s "
1714 " returned a bad sequence-id error!\n",
1715 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 exception.retry = 1;
1717 continue;
1718 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001719 /*
1720 * BAD_STATEID on OPEN means that the server cancelled our
1721 * state before it received the OPEN_CONFIRM.
1722 * Recover by retrying the request as per the discussion
1723 * on Page 181 of RFC3530.
1724 */
1725 if (status == -NFS4ERR_BAD_STATEID) {
1726 exception.retry = 1;
1727 continue;
1728 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001729 if (status == -EAGAIN) {
1730 /* We must have found a delegation */
1731 exception.retry = 1;
1732 continue;
1733 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1735 status, &exception));
1736 } while (exception.retry);
1737 return res;
1738}
1739
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001740static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1741 struct nfs_fattr *fattr, struct iattr *sattr,
1742 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001744 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001746 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 .iap = sattr,
1748 .server = server,
1749 .bitmask = server->attr_bitmask,
1750 };
1751 struct nfs_setattrres res = {
1752 .fattr = fattr,
1753 .server = server,
1754 };
1755 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001756 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1757 .rpc_argp = &arg,
1758 .rpc_resp = &res,
1759 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001761 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001762 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
Trond Myklebust0e574af2005-10-27 22:12:38 -04001764 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001766 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1767 /* Use that stateid */
1768 } else if (state != NULL) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001769 nfs4_copy_stateid(&arg.stateid, state, current->files);
1770 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1772
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001773 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001774 if (status == 0 && state != NULL)
1775 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001776 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777}
1778
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001779static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1780 struct nfs_fattr *fattr, struct iattr *sattr,
1781 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001783 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 struct nfs4_exception exception = { };
1785 int err;
1786 do {
1787 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001788 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 &exception);
1790 } while (exception.retry);
1791 return err;
1792}
1793
1794struct nfs4_closedata {
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001795 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 struct inode *inode;
1797 struct nfs4_state *state;
1798 struct nfs_closeargs arg;
1799 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001800 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001801 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802};
1803
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001804static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001805{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001806 struct nfs4_closedata *calldata = data;
1807 struct nfs4_state_owner *sp = calldata->state->owner;
Trond Myklebust95121352005-10-18 14:20:12 -07001808
1809 nfs4_put_open_state(calldata->state);
1810 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001811 nfs4_put_state_owner(sp);
Jan Blunck31f31db12008-05-02 13:42:45 -07001812 path_put(&calldata->path);
Trond Myklebust95121352005-10-18 14:20:12 -07001813 kfree(calldata);
1814}
1815
Trond Myklebust88069f72009-12-08 08:33:16 -05001816static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1817 fmode_t fmode)
1818{
1819 spin_lock(&state->owner->so_lock);
1820 if (!(fmode & FMODE_READ))
1821 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1822 if (!(fmode & FMODE_WRITE))
1823 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1824 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1825 spin_unlock(&state->owner->so_lock);
1826}
1827
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001828static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001830 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 struct nfs_server *server = NFS_SERVER(calldata->inode);
1833
Andy Adamson19ddab02009-04-01 09:22:20 -04001834 nfs4_sequence_done(server, &calldata->res.seq_res, task->tk_status);
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001835 if (RPC_ASSASSINATED(task))
1836 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 /* hmm. we are done with the inode, and in the process of freeing
1838 * the state_owner. we keep this around to process errors
1839 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 switch (task->tk_status) {
1841 case 0:
Trond Myklebust45328c32007-07-26 17:47:34 -04001842 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001843 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05001844 nfs4_close_clear_stateid_flags(state,
1845 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 break;
1847 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001848 case -NFS4ERR_OLD_STATEID:
1849 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001851 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001852 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05001854 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1855 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 }
Trond Myklebust72211db2009-12-15 14:47:36 -05001857 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04001858 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859}
1860
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001861static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001863 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001864 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05001865 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07001866
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001867 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001868 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001869
Trond Myklebust88069f72009-12-08 08:33:16 -05001870 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1871 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05001872 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001873 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01001874 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001875 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001876 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
1877 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1878 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04001879 }
1880 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001881 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
1882 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1883 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04001884 }
Trond Myklebuste7616922006-01-03 09:55:13 +01001885 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05001886 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05001887
1888 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001889 /* Note: exit _without_ calling nfs4_close_done */
1890 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07001891 return;
1892 }
Trond Myklebust88069f72009-12-08 08:33:16 -05001893
1894 if (calldata->arg.fmode == 0)
1895 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
1896
Trond Myklebust516a6af2005-10-27 22:12:41 -04001897 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001898 calldata->timestamp = jiffies;
Andy Adamson19ddab02009-04-01 09:22:20 -04001899 if (nfs4_setup_sequence((NFS_SERVER(calldata->inode))->nfs_client,
1900 &calldata->arg.seq_args, &calldata->res.seq_res,
1901 1, task))
1902 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001903 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904}
1905
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001906static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001907 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001908 .rpc_call_done = nfs4_close_done,
1909 .rpc_release = nfs4_free_closedata,
1910};
1911
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912/*
1913 * It is possible for data to be read/written from a mem-mapped file
1914 * after the sys_close call (which hits the vfs layer as a flush).
1915 * This means that we can't safely call nfsv4 close on a file until
1916 * the inode is cleared. This in turn means that we are not good
1917 * NFSv4 citizens - we do not indicate to the server to update the file's
1918 * share state even when we are done with one of the three share
1919 * stateid's in the inode.
1920 *
1921 * NOTE: Caller must be holding the sp->so_owner semaphore!
1922 */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001923int nfs4_do_close(struct path *path, struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001925 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04001927 struct nfs4_state_owner *sp = state->owner;
1928 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001929 struct rpc_message msg = {
1930 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1931 .rpc_cred = state->owner->so_cred,
1932 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001933 struct rpc_task_setup task_setup_data = {
1934 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001935 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001936 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05001937 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001938 .flags = RPC_TASK_ASYNC,
1939 };
Trond Myklebust95121352005-10-18 14:20:12 -07001940 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001942 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07001944 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001945 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001947 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04001948 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001950 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07001951 if (calldata->arg.seqid == NULL)
1952 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001953 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04001954 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001955 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04001956 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001957 calldata->res.server = server;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04001958 calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Al Virof6948692010-01-30 13:51:04 -05001959 path_get(path);
1960 calldata->path = *path;
Trond Myklebust95121352005-10-18 14:20:12 -07001961
Trond Myklebust5138fde2007-07-14 15:40:01 -04001962 msg.rpc_argp = &calldata->arg,
1963 msg.rpc_resp = &calldata->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001964 task_setup_data.callback_data = calldata;
1965 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001966 if (IS_ERR(task))
1967 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001968 status = 0;
1969 if (wait)
1970 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001971 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001972 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07001973out_free_calldata:
1974 kfree(calldata);
1975out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04001976 nfs4_put_open_state(state);
1977 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07001978 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979}
1980
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001981static 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 -07001982{
1983 struct file *filp;
Trond Myklebust1b45c462007-07-03 13:04:56 -04001984 int ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001985
Trond Myklebust1b45c462007-07-03 13:04:56 -04001986 /* If the open_intent is for execute, we have an extra check to make */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001987 if (fmode & FMODE_EXEC) {
Trond Myklebustaf22f942007-08-10 17:45:10 -04001988 ret = nfs_may_open(state->inode,
Trond Myklebust1b45c462007-07-03 13:04:56 -04001989 state->owner->so_cred,
1990 nd->intent.open.flags);
1991 if (ret < 0)
1992 goto out_close;
1993 }
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001994 filp = lookup_instantiate_filp(nd, path->dentry, NULL);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001995 if (!IS_ERR(filp)) {
1996 struct nfs_open_context *ctx;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04001997 ctx = nfs_file_open_context(filp);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001998 ctx->state = state;
Trond Myklebust95cf9592006-04-18 13:14:06 -04001999 return 0;
2000 }
Trond Myklebust1b45c462007-07-03 13:04:56 -04002001 ret = PTR_ERR(filp);
2002out_close:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002003 nfs4_close_sync(path, state, fmode & (FMODE_READ|FMODE_WRITE));
Trond Myklebust1b45c462007-07-03 13:04:56 -04002004 return ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002005}
2006
2007struct dentry *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
2009{
Trond Myklebustad389da2007-06-05 12:30:00 -04002010 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08002011 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04002012 .dentry = dentry,
2013 };
Jan Blunck4ac91372008-02-14 19:34:32 -08002014 struct dentry *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 struct iattr attr;
2016 struct rpc_cred *cred;
2017 struct nfs4_state *state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002018 struct dentry *res;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002019 fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
2021 if (nd->flags & LOOKUP_CREATE) {
2022 attr.ia_mode = nd->intent.open.create_mode;
2023 attr.ia_valid = ATTR_MODE;
2024 if (!IS_POSIXACL(dir))
Al Viroce3b0f82009-03-29 19:08:22 -04002025 attr.ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 } else {
2027 attr.ia_valid = 0;
2028 BUG_ON(nd->intent.open.flags & O_CREAT);
2029 }
2030
Trond Myklebust98a8e322008-03-12 12:25:28 -04002031 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 if (IS_ERR(cred))
Trond Myklebust02a913a2005-10-18 14:20:17 -07002033 return (struct dentry *)cred;
Trond Myklebust565277f2007-10-15 18:17:53 -04002034 parent = dentry->d_parent;
2035 /* Protect against concurrent sillydeletes */
2036 nfs_block_sillyrename(parent);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002037 state = nfs4_do_open(dir, &path, fmode, nd->intent.open.flags, &attr, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002039 if (IS_ERR(state)) {
Trond Myklebustd75340c2007-10-01 21:42:01 -04002040 if (PTR_ERR(state) == -ENOENT) {
Trond Myklebust02a913a2005-10-18 14:20:17 -07002041 d_add(dentry, NULL);
Trond Myklebustd75340c2007-10-01 21:42:01 -04002042 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2043 }
Trond Myklebust565277f2007-10-15 18:17:53 -04002044 nfs_unblock_sillyrename(parent);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002045 return (struct dentry *)state;
2046 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002047 res = d_add_unique(dentry, igrab(state->inode));
Trond Myklebust02a913a2005-10-18 14:20:17 -07002048 if (res != NULL)
Trond Myklebustdeee9362007-08-27 11:33:00 -04002049 path.dentry = res;
Trond Myklebustd75340c2007-10-01 21:42:01 -04002050 nfs_set_verifier(path.dentry, nfs_save_change_attribute(dir));
Trond Myklebust565277f2007-10-15 18:17:53 -04002051 nfs_unblock_sillyrename(parent);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002052 nfs4_intent_set_file(nd, &path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002053 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054}
2055
2056int
Trond Myklebust02a913a2005-10-18 14:20:17 -07002057nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058{
Trond Myklebustad389da2007-06-05 12:30:00 -04002059 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08002060 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04002061 .dentry = dentry,
2062 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 struct rpc_cred *cred;
2064 struct nfs4_state *state;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002065 fmode_t fmode = openflags & (FMODE_READ | FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
Trond Myklebust98a8e322008-03-12 12:25:28 -04002067 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 if (IS_ERR(cred))
2069 return PTR_ERR(cred);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002070 state = nfs4_do_open(dir, &path, fmode, openflags, NULL, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002072 if (IS_ERR(state)) {
2073 switch (PTR_ERR(state)) {
2074 case -EPERM:
2075 case -EACCES:
2076 case -EDQUOT:
2077 case -ENOSPC:
2078 case -EROFS:
Al Viro04287f92010-04-08 00:06:07 +01002079 return PTR_ERR(state);
Chuck Leverb87c0ad2006-10-19 23:28:42 -07002080 default:
2081 goto out_drop;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002082 }
Trond Myklebust02a913a2005-10-18 14:20:17 -07002083 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002084 if (state->inode == dentry->d_inode) {
Trond Myklebustd75340c2007-10-01 21:42:01 -04002085 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002086 nfs4_intent_set_file(nd, &path, state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 return 1;
2088 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002089 nfs4_close_sync(&path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002090out_drop:
2091 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 return 0;
2093}
2094
Trond Myklebust1185a552009-12-03 15:54:02 -05002095static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002096{
2097 if (ctx->state == NULL)
2098 return;
2099 if (is_sync)
2100 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
2101 else
2102 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
2103}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
2105static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2106{
Benny Halevy43652ad2009-04-01 09:21:54 -04002107 struct nfs4_server_caps_arg args = {
2108 .fhandle = fhandle,
2109 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 struct nfs4_server_caps_res res = {};
2111 struct rpc_message msg = {
2112 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002113 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 .rpc_resp = &res,
2115 };
2116 int status;
2117
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002118 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 if (status == 0) {
2120 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002121 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2122 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2123 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2124 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2125 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2127 server->caps |= NFS_CAP_ACLS;
2128 if (res.has_links != 0)
2129 server->caps |= NFS_CAP_HARDLINKS;
2130 if (res.has_symlinks != 0)
2131 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002132 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2133 server->caps |= NFS_CAP_FILEID;
2134 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2135 server->caps |= NFS_CAP_MODE;
2136 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2137 server->caps |= NFS_CAP_NLINK;
2138 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2139 server->caps |= NFS_CAP_OWNER;
2140 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2141 server->caps |= NFS_CAP_OWNER_GROUP;
2142 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2143 server->caps |= NFS_CAP_ATIME;
2144 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2145 server->caps |= NFS_CAP_CTIME;
2146 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2147 server->caps |= NFS_CAP_MTIME;
2148
Trond Myklebusta65318b2009-03-11 14:10:28 -04002149 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2150 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2151 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 server->acl_bitmask = res.acl_bitmask;
2153 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002154
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 return status;
2156}
2157
Trond Myklebust55a97592006-06-09 09:34:19 -04002158int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159{
2160 struct nfs4_exception exception = { };
2161 int err;
2162 do {
2163 err = nfs4_handle_exception(server,
2164 _nfs4_server_capabilities(server, fhandle),
2165 &exception);
2166 } while (exception.retry);
2167 return err;
2168}
2169
2170static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2171 struct nfs_fsinfo *info)
2172{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 struct nfs4_lookup_root_arg args = {
2174 .bitmask = nfs4_fattr_bitmap,
2175 };
2176 struct nfs4_lookup_res res = {
2177 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002178 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 .fh = fhandle,
2180 };
2181 struct rpc_message msg = {
2182 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2183 .rpc_argp = &args,
2184 .rpc_resp = &res,
2185 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002186
Trond Myklebust0e574af2005-10-27 22:12:38 -04002187 nfs_fattr_init(info->fattr);
Trond Myklebustfccba802009-07-21 16:48:07 -04002188 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189}
2190
2191static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2192 struct nfs_fsinfo *info)
2193{
2194 struct nfs4_exception exception = { };
2195 int err;
2196 do {
2197 err = nfs4_handle_exception(server,
2198 _nfs4_lookup_root(server, fhandle, info),
2199 &exception);
2200 } while (exception.retry);
2201 return err;
2202}
2203
David Howells54ceac42006-08-22 20:06:13 -04002204/*
2205 * get the file handle for the "/" directory on the server
2206 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002208 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 int status;
2211
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 status = nfs4_lookup_root(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 if (status == 0)
2214 status = nfs4_server_capabilities(server, fhandle);
2215 if (status == 0)
2216 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002217 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218}
2219
Manoj Naik6b97fd32006-06-09 09:34:29 -04002220/*
2221 * Get locations and (maybe) other attributes of a referral.
2222 * Note that we'll actually follow the referral later when
2223 * we detect fsid mismatch in inode revalidation
2224 */
Trond Myklebust56659e92007-07-17 21:52:39 -04002225static 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 -04002226{
2227 int status = -ENOMEM;
2228 struct page *page = NULL;
2229 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002230
2231 page = alloc_page(GFP_KERNEL);
2232 if (page == NULL)
2233 goto out;
2234 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2235 if (locations == NULL)
2236 goto out;
2237
Trond Myklebustc228fd32007-01-13 02:28:11 -05002238 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002239 if (status != 0)
2240 goto out;
2241 /* Make sure server returned a different fsid for the referral */
2242 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -07002243 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 -04002244 status = -EIO;
2245 goto out;
2246 }
2247
2248 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2249 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
2250 if (!fattr->mode)
2251 fattr->mode = S_IFDIR;
2252 memset(fhandle, 0, sizeof(struct nfs_fh));
2253out:
2254 if (page)
2255 __free_page(page);
2256 if (locations)
2257 kfree(locations);
2258 return status;
2259}
2260
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2262{
2263 struct nfs4_getattr_arg args = {
2264 .fh = fhandle,
2265 .bitmask = server->attr_bitmask,
2266 };
2267 struct nfs4_getattr_res res = {
2268 .fattr = fattr,
2269 .server = server,
2270 };
2271 struct rpc_message msg = {
2272 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2273 .rpc_argp = &args,
2274 .rpc_resp = &res,
2275 };
2276
Trond Myklebust0e574af2005-10-27 22:12:38 -04002277 nfs_fattr_init(fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002278 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279}
2280
2281static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2282{
2283 struct nfs4_exception exception = { };
2284 int err;
2285 do {
2286 err = nfs4_handle_exception(server,
2287 _nfs4_proc_getattr(server, fhandle, fattr),
2288 &exception);
2289 } while (exception.retry);
2290 return err;
2291}
2292
2293/*
2294 * The file is not closed if it is opened due to the a request to change
2295 * the size of the file. The open call will not be needed once the
2296 * VFS layer lookup-intents are implemented.
2297 *
2298 * Close is called when the inode is destroyed.
2299 * If we haven't opened the file for O_WRONLY, we
2300 * need to in the size_change case to obtain a stateid.
2301 *
2302 * Got race?
2303 * Because OPEN is always done by name in nfsv4, it is
2304 * possible that we opened a different file by the same
2305 * name. We can recognize this race condition, but we
2306 * can't do anything about it besides returning an error.
2307 *
2308 * This will be fixed with VFS changes (lookup-intent).
2309 */
2310static int
2311nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2312 struct iattr *sattr)
2313{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002314 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002315 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002316 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 int status;
2318
Trond Myklebust0e574af2005-10-27 22:12:38 -04002319 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
Trond Myklebustd5308382005-11-04 15:33:38 -05002321 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002322 if (sattr->ia_valid & ATTR_FILE) {
2323 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002324
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002325 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002326 if (ctx) {
2327 cred = ctx->cred;
2328 state = ctx->state;
2329 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002330 }
2331
2332 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002333 if (status == 0)
2334 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 return status;
2336}
2337
Trond Myklebust56659e92007-07-17 21:52:39 -04002338static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
2339 const struct qstr *name, struct nfs_fh *fhandle,
David Howells2b3de442006-08-22 20:06:09 -04002340 struct nfs_fattr *fattr)
2341{
2342 int status;
2343 struct nfs4_lookup_arg args = {
2344 .bitmask = server->attr_bitmask,
2345 .dir_fh = dirfh,
2346 .name = name,
2347 };
2348 struct nfs4_lookup_res res = {
2349 .server = server,
2350 .fattr = fattr,
2351 .fh = fhandle,
2352 };
2353 struct rpc_message msg = {
2354 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2355 .rpc_argp = &args,
2356 .rpc_resp = &res,
2357 };
2358
2359 nfs_fattr_init(fattr);
2360
2361 dprintk("NFS call lookupfh %s\n", name->name);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002362 status = nfs4_call_sync(server, &msg, &args, &res, 0);
David Howells2b3de442006-08-22 20:06:09 -04002363 dprintk("NFS reply lookupfh: %d\n", status);
David Howells2b3de442006-08-22 20:06:09 -04002364 return status;
2365}
2366
2367static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2368 struct qstr *name, struct nfs_fh *fhandle,
2369 struct nfs_fattr *fattr)
2370{
2371 struct nfs4_exception exception = { };
2372 int err;
2373 do {
Trond Myklebust4e56e082007-07-01 18:13:52 -04002374 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
2375 /* FIXME: !!!! */
2376 if (err == -NFS4ERR_MOVED) {
2377 err = -EREMOTE;
2378 break;
2379 }
2380 err = nfs4_handle_exception(server, err, &exception);
David Howells2b3de442006-08-22 20:06:09 -04002381 } while (exception.retry);
2382 return err;
2383}
2384
Trond Myklebust56659e92007-07-17 21:52:39 -04002385static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2387{
Trond Myklebust4e56e082007-07-01 18:13:52 -04002388 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389
2390 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust4e56e082007-07-01 18:13:52 -04002391 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002392 if (status == -NFS4ERR_MOVED)
2393 status = nfs4_get_referral(dir, name, fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 dprintk("NFS reply lookup: %d\n", status);
2395 return status;
2396}
2397
2398static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2399{
2400 struct nfs4_exception exception = { };
2401 int err;
2402 do {
2403 err = nfs4_handle_exception(NFS_SERVER(dir),
2404 _nfs4_proc_lookup(dir, name, fhandle, fattr),
2405 &exception);
2406 } while (exception.retry);
2407 return err;
2408}
2409
2410static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2411{
Trond Myklebust76b32992007-08-10 17:45:11 -04002412 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 struct nfs4_accessargs args = {
2414 .fh = NFS_FH(inode),
Trond Myklebust76b32992007-08-10 17:45:11 -04002415 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002417 struct nfs4_accessres res = {
2418 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002419 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 struct rpc_message msg = {
2421 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2422 .rpc_argp = &args,
2423 .rpc_resp = &res,
2424 .rpc_cred = entry->cred,
2425 };
2426 int mode = entry->mask;
2427 int status;
2428
2429 /*
2430 * Determine which access bits we want to ask for...
2431 */
2432 if (mode & MAY_READ)
2433 args.access |= NFS4_ACCESS_READ;
2434 if (S_ISDIR(inode->i_mode)) {
2435 if (mode & MAY_WRITE)
2436 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2437 if (mode & MAY_EXEC)
2438 args.access |= NFS4_ACCESS_LOOKUP;
2439 } else {
2440 if (mode & MAY_WRITE)
2441 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2442 if (mode & MAY_EXEC)
2443 args.access |= NFS4_ACCESS_EXECUTE;
2444 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002445
2446 res.fattr = nfs_alloc_fattr();
2447 if (res.fattr == NULL)
2448 return -ENOMEM;
2449
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002450 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 if (!status) {
2452 entry->mask = 0;
2453 if (res.access & NFS4_ACCESS_READ)
2454 entry->mask |= MAY_READ;
2455 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2456 entry->mask |= MAY_WRITE;
2457 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2458 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002459 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002461 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 return status;
2463}
2464
2465static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2466{
2467 struct nfs4_exception exception = { };
2468 int err;
2469 do {
2470 err = nfs4_handle_exception(NFS_SERVER(inode),
2471 _nfs4_proc_access(inode, entry),
2472 &exception);
2473 } while (exception.retry);
2474 return err;
2475}
2476
2477/*
2478 * TODO: For the time being, we don't try to get any attributes
2479 * along with any of the zero-copy operations READ, READDIR,
2480 * READLINK, WRITE.
2481 *
2482 * In the case of the first three, we want to put the GETATTR
2483 * after the read-type operation -- this is because it is hard
2484 * to predict the length of a GETATTR response in v4, and thus
2485 * align the READ data correctly. This means that the GETATTR
2486 * may end up partially falling into the page cache, and we should
2487 * shift it into the 'tail' of the xdr_buf before processing.
2488 * To do this efficiently, we need to know the total length
2489 * of data received, which doesn't seem to be available outside
2490 * of the RPC layer.
2491 *
2492 * In the case of WRITE, we also want to put the GETATTR after
2493 * the operation -- in this case because we want to make sure
2494 * we get the post-operation mtime and size. This means that
2495 * we can't use xdr_encode_pages() as written: we need a variant
2496 * of it which would leave room in the 'tail' iovec.
2497 *
2498 * Both of these changes to the XDR layer would in fact be quite
2499 * minor, but I decided to leave them for a subsequent patch.
2500 */
2501static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2502 unsigned int pgbase, unsigned int pglen)
2503{
2504 struct nfs4_readlink args = {
2505 .fh = NFS_FH(inode),
2506 .pgbase = pgbase,
2507 .pglen = pglen,
2508 .pages = &page,
2509 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002510 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 struct rpc_message msg = {
2512 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2513 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002514 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 };
2516
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002517 return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518}
2519
2520static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2521 unsigned int pgbase, unsigned int pglen)
2522{
2523 struct nfs4_exception exception = { };
2524 int err;
2525 do {
2526 err = nfs4_handle_exception(NFS_SERVER(inode),
2527 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2528 &exception);
2529 } while (exception.retry);
2530 return err;
2531}
2532
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533/*
2534 * Got race?
2535 * We will need to arrange for the VFS layer to provide an atomic open.
2536 * Until then, this create/open method is prone to inefficiency and race
2537 * conditions due to the lookup, create, and open VFS calls from sys_open()
2538 * placed on the wire.
2539 *
2540 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2541 * The file will be opened again in the subsequent VFS open call
2542 * (nfs4_proc_file_open).
2543 *
2544 * The open for read will just hang around to be used by any process that
2545 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2546 */
2547
2548static int
2549nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebust02a913a2005-10-18 14:20:17 -07002550 int flags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551{
Trond Myklebustad389da2007-06-05 12:30:00 -04002552 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08002553 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04002554 .dentry = dentry,
2555 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 struct nfs4_state *state;
2557 struct rpc_cred *cred;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002558 fmode_t fmode = flags & (FMODE_READ | FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 int status = 0;
2560
Trond Myklebust98a8e322008-03-12 12:25:28 -04002561 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 if (IS_ERR(cred)) {
2563 status = PTR_ERR(cred);
2564 goto out;
2565 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002566 state = nfs4_do_open(dir, &path, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002567 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 if (IS_ERR(state)) {
2569 status = PTR_ERR(state);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002570 goto out_putcred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002572 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002573 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustad389da2007-06-05 12:30:00 -04002574 if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002575 status = nfs4_intent_set_file(nd, &path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002576 else
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002577 nfs4_close_sync(&path, state, fmode);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002578out_putcred:
2579 put_rpccred(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580out:
2581 return status;
2582}
2583
2584static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2585{
Trond Myklebust16e42952005-10-27 22:12:44 -04002586 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002587 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002589 .name.len = name->len,
2590 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002591 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002593 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002594 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002595 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002597 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2598 .rpc_argp = &args,
2599 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 };
Trond Myklebustd3468902010-04-16 16:22:50 -04002601 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602
Trond Myklebustd3468902010-04-16 16:22:50 -04002603 res.dir_attr = nfs_alloc_fattr();
2604 if (res.dir_attr == NULL)
2605 goto out;
2606
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002607 status = nfs4_call_sync(server, &msg, &args, &res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002608 if (status == 0) {
2609 update_changeattr(dir, &res.cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002610 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002611 }
Trond Myklebustd3468902010-04-16 16:22:50 -04002612 nfs_free_fattr(res.dir_attr);
2613out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 return status;
2615}
2616
2617static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2618{
2619 struct nfs4_exception exception = { };
2620 int err;
2621 do {
2622 err = nfs4_handle_exception(NFS_SERVER(dir),
2623 _nfs4_proc_remove(dir, name),
2624 &exception);
2625 } while (exception.retry);
2626 return err;
2627}
2628
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002629static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002631 struct nfs_server *server = NFS_SERVER(dir);
2632 struct nfs_removeargs *args = msg->rpc_argp;
2633 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634
Trond Myklebusta65318b2009-03-11 14:10:28 -04002635 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002636 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638}
2639
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002640static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002642 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2643
Andy Adamson472cfbd2009-04-01 09:22:24 -04002644 nfs4_sequence_done(res->server, &res->seq_res, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002645 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002646 return 0;
2647 update_changeattr(dir, &res->cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002648 nfs_post_op_update_inode(dir, res->dir_attr);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002649 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650}
2651
2652static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2653 struct inode *new_dir, struct qstr *new_name)
2654{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002655 struct nfs_server *server = NFS_SERVER(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 struct nfs4_rename_arg arg = {
2657 .old_dir = NFS_FH(old_dir),
2658 .new_dir = NFS_FH(new_dir),
2659 .old_name = old_name,
2660 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002661 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 };
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002663 struct nfs4_rename_res res = {
2664 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002665 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 struct rpc_message msg = {
2667 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2668 .rpc_argp = &arg,
2669 .rpc_resp = &res,
2670 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002671 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672
Trond Myklebust011fff72010-04-16 16:22:49 -04002673 res.old_fattr = nfs_alloc_fattr();
2674 res.new_fattr = nfs_alloc_fattr();
2675 if (res.old_fattr == NULL || res.new_fattr == NULL)
2676 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677
Trond Myklebust011fff72010-04-16 16:22:49 -04002678 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 if (!status) {
2680 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002681 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002683 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 }
Trond Myklebust011fff72010-04-16 16:22:49 -04002685out:
2686 nfs_free_fattr(res.new_fattr);
2687 nfs_free_fattr(res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 return status;
2689}
2690
2691static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2692 struct inode *new_dir, struct qstr *new_name)
2693{
2694 struct nfs4_exception exception = { };
2695 int err;
2696 do {
2697 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2698 _nfs4_proc_rename(old_dir, old_name,
2699 new_dir, new_name),
2700 &exception);
2701 } while (exception.retry);
2702 return err;
2703}
2704
2705static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2706{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002707 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 struct nfs4_link_arg arg = {
2709 .fh = NFS_FH(inode),
2710 .dir_fh = NFS_FH(dir),
2711 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002712 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002714 struct nfs4_link_res res = {
2715 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002716 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 struct rpc_message msg = {
2718 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2719 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002720 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 };
Trond Myklebust136f2622010-04-16 16:22:49 -04002722 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723
Trond Myklebust136f2622010-04-16 16:22:49 -04002724 res.fattr = nfs_alloc_fattr();
2725 res.dir_attr = nfs_alloc_fattr();
2726 if (res.fattr == NULL || res.dir_attr == NULL)
2727 goto out;
2728
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002729 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002730 if (!status) {
2731 update_changeattr(dir, &res.cinfo);
2732 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002733 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002734 }
Trond Myklebust136f2622010-04-16 16:22:49 -04002735out:
2736 nfs_free_fattr(res.dir_attr);
2737 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 return status;
2739}
2740
2741static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2742{
2743 struct nfs4_exception exception = { };
2744 int err;
2745 do {
2746 err = nfs4_handle_exception(NFS_SERVER(inode),
2747 _nfs4_proc_link(inode, dir, name),
2748 &exception);
2749 } while (exception.retry);
2750 return err;
2751}
2752
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002753struct nfs4_createdata {
2754 struct rpc_message msg;
2755 struct nfs4_create_arg arg;
2756 struct nfs4_create_res res;
2757 struct nfs_fh fh;
2758 struct nfs_fattr fattr;
2759 struct nfs_fattr dir_fattr;
2760};
2761
2762static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2763 struct qstr *name, struct iattr *sattr, u32 ftype)
2764{
2765 struct nfs4_createdata *data;
2766
2767 data = kzalloc(sizeof(*data), GFP_KERNEL);
2768 if (data != NULL) {
2769 struct nfs_server *server = NFS_SERVER(dir);
2770
2771 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2772 data->msg.rpc_argp = &data->arg;
2773 data->msg.rpc_resp = &data->res;
2774 data->arg.dir_fh = NFS_FH(dir);
2775 data->arg.server = server;
2776 data->arg.name = name;
2777 data->arg.attrs = sattr;
2778 data->arg.ftype = ftype;
2779 data->arg.bitmask = server->attr_bitmask;
2780 data->res.server = server;
2781 data->res.fh = &data->fh;
2782 data->res.fattr = &data->fattr;
2783 data->res.dir_fattr = &data->dir_fattr;
2784 nfs_fattr_init(data->res.fattr);
2785 nfs_fattr_init(data->res.dir_fattr);
2786 }
2787 return data;
2788}
2789
2790static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2791{
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002792 int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
2793 &data->arg, &data->res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002794 if (status == 0) {
2795 update_changeattr(dir, &data->res.dir_cinfo);
2796 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2797 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2798 }
2799 return status;
2800}
2801
2802static void nfs4_free_createdata(struct nfs4_createdata *data)
2803{
2804 kfree(data);
2805}
2806
Chuck Lever4f390c12006-08-22 20:06:22 -04002807static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002808 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002810 struct nfs4_createdata *data;
2811 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812
Chuck Lever94a6d752006-08-22 20:06:23 -04002813 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002814 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002815
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002816 status = -ENOMEM;
2817 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2818 if (data == NULL)
2819 goto out;
2820
2821 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2822 data->arg.u.symlink.pages = &page;
2823 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002825 status = nfs4_do_create(dir, dentry, data);
2826
2827 nfs4_free_createdata(data);
2828out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 return status;
2830}
2831
Chuck Lever4f390c12006-08-22 20:06:22 -04002832static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002833 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834{
2835 struct nfs4_exception exception = { };
2836 int err;
2837 do {
2838 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002839 _nfs4_proc_symlink(dir, dentry, page,
2840 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 &exception);
2842 } while (exception.retry);
2843 return err;
2844}
2845
2846static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2847 struct iattr *sattr)
2848{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002849 struct nfs4_createdata *data;
2850 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002852 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2853 if (data == NULL)
2854 goto out;
2855
2856 status = nfs4_do_create(dir, dentry, data);
2857
2858 nfs4_free_createdata(data);
2859out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 return status;
2861}
2862
2863static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2864 struct iattr *sattr)
2865{
2866 struct nfs4_exception exception = { };
2867 int err;
2868 do {
2869 err = nfs4_handle_exception(NFS_SERVER(dir),
2870 _nfs4_proc_mkdir(dir, dentry, sattr),
2871 &exception);
2872 } while (exception.retry);
2873 return err;
2874}
2875
2876static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2877 u64 cookie, struct page *page, unsigned int count, int plus)
2878{
2879 struct inode *dir = dentry->d_inode;
2880 struct nfs4_readdir_arg args = {
2881 .fh = NFS_FH(dir),
2882 .pages = &page,
2883 .pgbase = 0,
2884 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09002885 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 };
2887 struct nfs4_readdir_res res;
2888 struct rpc_message msg = {
2889 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2890 .rpc_argp = &args,
2891 .rpc_resp = &res,
2892 .rpc_cred = cred,
2893 };
2894 int status;
2895
Harvey Harrison3110ff82008-05-02 13:42:44 -07002896 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00002897 dentry->d_parent->d_name.name,
2898 dentry->d_name.name,
2899 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2901 res.pgbase = args.pgbase;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002902 status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 if (status == 0)
2904 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustc4812992007-09-28 17:11:45 -04002905
2906 nfs_invalidate_atime(dir);
2907
Harvey Harrison3110ff82008-05-02 13:42:44 -07002908 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 return status;
2910}
2911
2912static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2913 u64 cookie, struct page *page, unsigned int count, int plus)
2914{
2915 struct nfs4_exception exception = { };
2916 int err;
2917 do {
2918 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2919 _nfs4_proc_readdir(dentry, cred, cookie,
2920 page, count, plus),
2921 &exception);
2922 } while (exception.retry);
2923 return err;
2924}
2925
2926static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2927 struct iattr *sattr, dev_t rdev)
2928{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002929 struct nfs4_createdata *data;
2930 int mode = sattr->ia_mode;
2931 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932
2933 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2934 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002935
2936 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2937 if (data == NULL)
2938 goto out;
2939
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002941 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002943 data->arg.ftype = NF4BLK;
2944 data->arg.u.device.specdata1 = MAJOR(rdev);
2945 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 }
2947 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002948 data->arg.ftype = NF4CHR;
2949 data->arg.u.device.specdata1 = MAJOR(rdev);
2950 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002953 status = nfs4_do_create(dir, dentry, data);
2954
2955 nfs4_free_createdata(data);
2956out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 return status;
2958}
2959
2960static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2961 struct iattr *sattr, dev_t rdev)
2962{
2963 struct nfs4_exception exception = { };
2964 int err;
2965 do {
2966 err = nfs4_handle_exception(NFS_SERVER(dir),
2967 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2968 &exception);
2969 } while (exception.retry);
2970 return err;
2971}
2972
2973static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2974 struct nfs_fsstat *fsstat)
2975{
2976 struct nfs4_statfs_arg args = {
2977 .fh = fhandle,
2978 .bitmask = server->attr_bitmask,
2979 };
Benny Halevy24ad1482009-04-01 09:21:56 -04002980 struct nfs4_statfs_res res = {
2981 .fsstat = fsstat,
2982 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 struct rpc_message msg = {
2984 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2985 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04002986 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 };
2988
Trond Myklebust0e574af2005-10-27 22:12:38 -04002989 nfs_fattr_init(fsstat->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002990 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991}
2992
2993static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2994{
2995 struct nfs4_exception exception = { };
2996 int err;
2997 do {
2998 err = nfs4_handle_exception(server,
2999 _nfs4_proc_statfs(server, fhandle, fsstat),
3000 &exception);
3001 } while (exception.retry);
3002 return err;
3003}
3004
3005static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3006 struct nfs_fsinfo *fsinfo)
3007{
3008 struct nfs4_fsinfo_arg args = {
3009 .fh = fhandle,
3010 .bitmask = server->attr_bitmask,
3011 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003012 struct nfs4_fsinfo_res res = {
3013 .fsinfo = fsinfo,
3014 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 struct rpc_message msg = {
3016 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3017 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003018 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 };
3020
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003021 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022}
3023
3024static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3025{
3026 struct nfs4_exception exception = { };
3027 int err;
3028
3029 do {
3030 err = nfs4_handle_exception(server,
3031 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3032 &exception);
3033 } while (exception.retry);
3034 return err;
3035}
3036
3037static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3038{
Trond Myklebust0e574af2005-10-27 22:12:38 -04003039 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3041}
3042
3043static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3044 struct nfs_pathconf *pathconf)
3045{
3046 struct nfs4_pathconf_arg args = {
3047 .fh = fhandle,
3048 .bitmask = server->attr_bitmask,
3049 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003050 struct nfs4_pathconf_res res = {
3051 .pathconf = pathconf,
3052 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 struct rpc_message msg = {
3054 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3055 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003056 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 };
3058
3059 /* None of the pathconf attributes are mandatory to implement */
3060 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3061 memset(pathconf, 0, sizeof(*pathconf));
3062 return 0;
3063 }
3064
Trond Myklebust0e574af2005-10-27 22:12:38 -04003065 nfs_fattr_init(pathconf->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003066 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067}
3068
3069static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3070 struct nfs_pathconf *pathconf)
3071{
3072 struct nfs4_exception exception = { };
3073 int err;
3074
3075 do {
3076 err = nfs4_handle_exception(server,
3077 _nfs4_proc_pathconf(server, fhandle, pathconf),
3078 &exception);
3079 } while (exception.retry);
3080 return err;
3081}
3082
Trond Myklebustec06c092006-03-20 13:44:27 -05003083static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084{
Trond Myklebustec06c092006-03-20 13:44:27 -05003085 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086
Andy Adamsonf11c88a2009-04-01 09:22:25 -04003087 dprintk("--> %s\n", __func__);
3088
Andy Adamsonf11c88a2009-04-01 09:22:25 -04003089 nfs4_sequence_done(server, &data->res.seq_res, task->tk_status);
3090
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003091 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003092 nfs_restart_rpc(task, server->nfs_client);
Trond Myklebustec06c092006-03-20 13:44:27 -05003093 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003095
3096 nfs_invalidate_atime(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003098 renew_lease(server, data->timestamp);
3099 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100}
3101
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003102static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 data->timestamp = jiffies;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003105 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106}
3107
Trond Myklebust788e7a82006-03-20 13:44:27 -05003108static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 struct inode *inode = data->inode;
3111
Andy Adamsondef6ed72009-04-01 09:22:26 -04003112 nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
3113 task->tk_status);
3114
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003115 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003116 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003117 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003119 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003121 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003122 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003123 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124}
3125
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003126static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003128 struct nfs_server *server = NFS_SERVER(data->inode);
3129
Trond Myklebusta65318b2009-03-11 14:10:28 -04003130 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003131 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 data->timestamp = jiffies;
3133
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003134 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135}
3136
Trond Myklebust788e7a82006-03-20 13:44:27 -05003137static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 struct inode *inode = data->inode;
3140
Andy Adamson21d9a852009-04-01 09:22:27 -04003141 nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
3142 task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003143 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003144 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003145 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003147 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003148 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149}
3150
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003151static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003153 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154
Trond Myklebusta65318b2009-03-11 14:10:28 -04003155 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003156 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003157 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158}
3159
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003160struct nfs4_renewdata {
3161 struct nfs_client *client;
3162 unsigned long timestamp;
3163};
3164
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165/*
3166 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3167 * standalone procedure for queueing an asynchronous RENEW.
3168 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003169static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003170{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003171 struct nfs4_renewdata *data = calldata;
3172 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003173
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003174 if (atomic_read(&clp->cl_count) > 1)
3175 nfs4_schedule_state_renewal(clp);
3176 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003177 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003178}
3179
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003180static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003182 struct nfs4_renewdata *data = calldata;
3183 struct nfs_client *clp = data->client;
3184 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185
3186 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003187 /* Unless we're shutting down, schedule state recovery! */
3188 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
3189 nfs4_schedule_state_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 return;
3191 }
3192 spin_lock(&clp->cl_lock);
3193 if (time_before(clp->cl_last_renewal,timestamp))
3194 clp->cl_last_renewal = timestamp;
3195 spin_unlock(&clp->cl_lock);
3196}
3197
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003198static const struct rpc_call_ops nfs4_renew_ops = {
3199 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003200 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003201};
3202
David Howellsadfa6f92006-08-22 20:06:08 -04003203int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204{
3205 struct rpc_message msg = {
3206 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3207 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003208 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003210 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003212 if (!atomic_inc_not_zero(&clp->cl_count))
3213 return -EIO;
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003214 data = kmalloc(sizeof(*data), GFP_KERNEL);
3215 if (data == NULL)
3216 return -ENOMEM;
3217 data->client = clp;
3218 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003220 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221}
3222
David Howellsadfa6f92006-08-22 20:06:08 -04003223int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224{
3225 struct rpc_message msg = {
3226 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3227 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003228 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 };
3230 unsigned long now = jiffies;
3231 int status;
3232
3233 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3234 if (status < 0)
3235 return status;
3236 spin_lock(&clp->cl_lock);
3237 if (time_before(clp->cl_last_renewal,now))
3238 clp->cl_last_renewal = now;
3239 spin_unlock(&clp->cl_lock);
3240 return 0;
3241}
3242
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003243static inline int nfs4_server_supports_acls(struct nfs_server *server)
3244{
3245 return (server->caps & NFS_CAP_ACLS)
3246 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3247 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3248}
3249
3250/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3251 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3252 * the stack.
3253 */
3254#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3255
3256static void buf_to_pages(const void *buf, size_t buflen,
3257 struct page **pages, unsigned int *pgbase)
3258{
3259 const void *p = buf;
3260
3261 *pgbase = offset_in_page(buf);
3262 p -= *pgbase;
3263 while (p < buf + buflen) {
3264 *(pages++) = virt_to_page(p);
3265 p += PAGE_CACHE_SIZE;
3266 }
3267}
3268
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003269struct nfs4_cached_acl {
3270 int cached;
3271 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003272 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003273};
3274
3275static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003276{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003277 struct nfs_inode *nfsi = NFS_I(inode);
3278
3279 spin_lock(&inode->i_lock);
3280 kfree(nfsi->nfs4_acl);
3281 nfsi->nfs4_acl = acl;
3282 spin_unlock(&inode->i_lock);
3283}
3284
3285static void nfs4_zap_acl_attr(struct inode *inode)
3286{
3287 nfs4_set_cached_acl(inode, NULL);
3288}
3289
3290static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3291{
3292 struct nfs_inode *nfsi = NFS_I(inode);
3293 struct nfs4_cached_acl *acl;
3294 int ret = -ENOENT;
3295
3296 spin_lock(&inode->i_lock);
3297 acl = nfsi->nfs4_acl;
3298 if (acl == NULL)
3299 goto out;
3300 if (buf == NULL) /* user is just asking for length */
3301 goto out_len;
3302 if (acl->cached == 0)
3303 goto out;
3304 ret = -ERANGE; /* see getxattr(2) man page */
3305 if (acl->len > buflen)
3306 goto out;
3307 memcpy(buf, acl->data, acl->len);
3308out_len:
3309 ret = acl->len;
3310out:
3311 spin_unlock(&inode->i_lock);
3312 return ret;
3313}
3314
3315static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3316{
3317 struct nfs4_cached_acl *acl;
3318
3319 if (buf && acl_len <= PAGE_SIZE) {
3320 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3321 if (acl == NULL)
3322 goto out;
3323 acl->cached = 1;
3324 memcpy(acl->data, buf, acl_len);
3325 } else {
3326 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3327 if (acl == NULL)
3328 goto out;
3329 acl->cached = 0;
3330 }
3331 acl->len = acl_len;
3332out:
3333 nfs4_set_cached_acl(inode, acl);
3334}
3335
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003336static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003337{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003338 struct page *pages[NFS4ACL_MAXPAGES];
3339 struct nfs_getaclargs args = {
3340 .fh = NFS_FH(inode),
3341 .acl_pages = pages,
3342 .acl_len = buflen,
3343 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003344 struct nfs_getaclres res = {
3345 .acl_len = buflen,
3346 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003347 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003348 struct rpc_message msg = {
3349 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3350 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003351 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003352 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003353 struct page *localpage = NULL;
3354 int ret;
3355
3356 if (buflen < PAGE_SIZE) {
3357 /* As long as we're doing a round trip to the server anyway,
3358 * let's be prepared for a page of acl data. */
3359 localpage = alloc_page(GFP_KERNEL);
3360 resp_buf = page_address(localpage);
3361 if (localpage == NULL)
3362 return -ENOMEM;
3363 args.acl_pages[0] = localpage;
3364 args.acl_pgbase = 0;
Benny Halevy663c79b2009-04-01 09:21:59 -04003365 args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003366 } else {
3367 resp_buf = buf;
3368 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3369 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003370 ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003371 if (ret)
3372 goto out_free;
Benny Halevy663c79b2009-04-01 09:21:59 -04003373 if (res.acl_len > args.acl_len)
3374 nfs4_write_cached_acl(inode, NULL, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003375 else
Benny Halevy663c79b2009-04-01 09:21:59 -04003376 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003377 if (buf) {
3378 ret = -ERANGE;
Benny Halevy663c79b2009-04-01 09:21:59 -04003379 if (res.acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003380 goto out_free;
3381 if (localpage)
Benny Halevy663c79b2009-04-01 09:21:59 -04003382 memcpy(buf, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003383 }
Benny Halevy663c79b2009-04-01 09:21:59 -04003384 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003385out_free:
3386 if (localpage)
3387 __free_page(localpage);
3388 return ret;
3389}
3390
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003391static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3392{
3393 struct nfs4_exception exception = { };
3394 ssize_t ret;
3395 do {
3396 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3397 if (ret >= 0)
3398 break;
3399 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3400 } while (exception.retry);
3401 return ret;
3402}
3403
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003404static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3405{
3406 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003407 int ret;
3408
3409 if (!nfs4_server_supports_acls(server))
3410 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003411 ret = nfs_revalidate_inode(server, inode);
3412 if (ret < 0)
3413 return ret;
3414 ret = nfs4_read_cached_acl(inode, buf, buflen);
3415 if (ret != -ENOENT)
3416 return ret;
3417 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003418}
3419
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003420static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003421{
3422 struct nfs_server *server = NFS_SERVER(inode);
3423 struct page *pages[NFS4ACL_MAXPAGES];
3424 struct nfs_setaclargs arg = {
3425 .fh = NFS_FH(inode),
3426 .acl_pages = pages,
3427 .acl_len = buflen,
3428 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003429 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003430 struct rpc_message msg = {
3431 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3432 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003433 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003434 };
3435 int ret;
3436
3437 if (!nfs4_server_supports_acls(server))
3438 return -EOPNOTSUPP;
Trond Myklebust642ac542005-10-18 14:20:19 -07003439 nfs_inode_return_delegation(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003440 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003441 ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003442 nfs_access_zap_cache(inode);
3443 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003444 return ret;
3445}
3446
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003447static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3448{
3449 struct nfs4_exception exception = { };
3450 int err;
3451 do {
3452 err = nfs4_handle_exception(NFS_SERVER(inode),
3453 __nfs4_proc_set_acl(inode, buf, buflen),
3454 &exception);
3455 } while (exception.retry);
3456 return err;
3457}
3458
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003460nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003462 struct nfs_client *clp = server->nfs_client;
3463
3464 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 return 0;
3466 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003467 case -NFS4ERR_ADMIN_REVOKED:
3468 case -NFS4ERR_BAD_STATEID:
3469 case -NFS4ERR_OPENMODE:
3470 if (state == NULL)
3471 break;
3472 nfs4_state_mark_reclaim_nograce(clp, state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003473 goto do_state_recovery;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003475 if (state == NULL)
3476 break;
3477 nfs4_state_mark_reclaim_reboot(clp, state);
3478 case -NFS4ERR_STALE_CLIENTID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 case -NFS4ERR_EXPIRED:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003480 goto do_state_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003481#if defined(CONFIG_NFS_V4_1)
3482 case -NFS4ERR_BADSESSION:
3483 case -NFS4ERR_BADSLOT:
3484 case -NFS4ERR_BAD_HIGH_SLOT:
3485 case -NFS4ERR_DEADSESSION:
3486 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3487 case -NFS4ERR_SEQ_FALSE_RETRY:
3488 case -NFS4ERR_SEQ_MISORDERED:
3489 dprintk("%s ERROR %d, Reset session\n", __func__,
3490 task->tk_status);
Andy Adamson0b9e2d42009-12-04 16:02:14 -05003491 nfs4_schedule_state_recovery(clp);
Andy Adamson4745e312009-04-01 09:22:42 -04003492 task->tk_status = 0;
3493 return -EAGAIN;
3494#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003496 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003497 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003498 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3500 task->tk_status = 0;
3501 return -EAGAIN;
3502 case -NFS4ERR_OLD_STATEID:
3503 task->tk_status = 0;
3504 return -EAGAIN;
3505 }
3506 task->tk_status = nfs4_map_errors(task->tk_status);
3507 return 0;
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003508do_state_recovery:
3509 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
3510 nfs4_schedule_state_recovery(clp);
3511 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3512 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3513 task->tk_status = 0;
3514 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515}
3516
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003517int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3518 unsigned short port, struct rpc_cred *cred,
3519 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520{
3521 nfs4_verifier sc_verifier;
3522 struct nfs4_setclientid setclientid = {
3523 .sc_verifier = &sc_verifier,
3524 .sc_prog = program,
3525 };
3526 struct rpc_message msg = {
3527 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3528 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003529 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003530 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 };
Al Virobc4785c2006-10-19 23:28:51 -07003532 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 int loop = 0;
3534 int status;
3535
Al Virobc4785c2006-10-19 23:28:51 -07003536 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3538 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3539
3540 for(;;) {
3541 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003542 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003543 clp->cl_ipaddr,
3544 rpc_peeraddr2str(clp->cl_rpcclient,
3545 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003546 rpc_peeraddr2str(clp->cl_rpcclient,
3547 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003548 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549 clp->cl_id_uniquifier);
3550 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003551 sizeof(setclientid.sc_netid),
3552 rpc_peeraddr2str(clp->cl_rpcclient,
3553 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003555 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 clp->cl_ipaddr, port >> 8, port & 255);
3557
3558 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3559 if (status != -NFS4ERR_CLID_INUSE)
3560 break;
3561 if (signalled())
3562 break;
3563 if (loop++ & 1)
3564 ssleep(clp->cl_lease_time + 1);
3565 else
3566 if (++clp->cl_id_uniquifier == 0)
3567 break;
3568 }
3569 return status;
3570}
3571
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003572static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3573 struct nfs4_setclientid_res *arg,
3574 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575{
3576 struct nfs_fsinfo fsinfo;
3577 struct rpc_message msg = {
3578 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003579 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003581 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 };
3583 unsigned long now;
3584 int status;
3585
3586 now = jiffies;
3587 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3588 if (status == 0) {
3589 spin_lock(&clp->cl_lock);
3590 clp->cl_lease_time = fsinfo.lease_time * HZ;
3591 clp->cl_last_renewal = now;
3592 spin_unlock(&clp->cl_lock);
3593 }
3594 return status;
3595}
3596
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003597int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3598 struct nfs4_setclientid_res *arg,
3599 struct rpc_cred *cred)
Trond Myklebust51581f32006-03-20 13:44:47 -05003600{
Benny Halevy77e03672008-06-24 20:25:57 +03003601 long timeout = 0;
Trond Myklebust51581f32006-03-20 13:44:47 -05003602 int err;
3603 do {
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003604 err = _nfs4_proc_setclientid_confirm(clp, arg, cred);
Trond Myklebust51581f32006-03-20 13:44:47 -05003605 switch (err) {
3606 case 0:
3607 return err;
3608 case -NFS4ERR_RESOURCE:
3609 /* The IBM lawyers misread another document! */
3610 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -05003611 case -EKEYEXPIRED:
Trond Myklebust51581f32006-03-20 13:44:47 -05003612 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3613 }
3614 } while (err == 0);
3615 return err;
3616}
3617
Trond Myklebustfe650402006-01-03 09:55:18 +01003618struct nfs4_delegreturndata {
3619 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003620 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003621 struct nfs_fh fh;
3622 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003623 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003624 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003625 int rpc_status;
3626};
3627
Trond Myklebustfe650402006-01-03 09:55:18 +01003628static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3629{
3630 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003631
Trond Myklebustd61e6122009-12-05 19:32:19 -05003632 nfs4_sequence_done(data->res.server, &data->res.seq_res,
3633 task->tk_status);
Andy Adamson938e1012009-04-01 09:22:28 -04003634
Ricardo Labiaga79708862009-12-07 09:23:21 -05003635 switch (task->tk_status) {
3636 case -NFS4ERR_STALE_STATEID:
3637 case -NFS4ERR_EXPIRED:
3638 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01003639 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003640 break;
3641 default:
3642 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3643 -EAGAIN) {
3644 nfs_restart_rpc(task, data->res.server->nfs_client);
3645 return;
3646 }
3647 }
3648 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01003649}
3650
3651static void nfs4_delegreturn_release(void *calldata)
3652{
Trond Myklebustfe650402006-01-03 09:55:18 +01003653 kfree(calldata);
3654}
3655
Andy Adamson938e1012009-04-01 09:22:28 -04003656#if defined(CONFIG_NFS_V4_1)
3657static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3658{
3659 struct nfs4_delegreturndata *d_data;
3660
3661 d_data = (struct nfs4_delegreturndata *)data;
3662
3663 if (nfs4_setup_sequence(d_data->res.server->nfs_client,
3664 &d_data->args.seq_args,
3665 &d_data->res.seq_res, 1, task))
3666 return;
3667 rpc_call_start(task);
3668}
3669#endif /* CONFIG_NFS_V4_1 */
3670
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003671static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04003672#if defined(CONFIG_NFS_V4_1)
3673 .rpc_call_prepare = nfs4_delegreturn_prepare,
3674#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01003675 .rpc_call_done = nfs4_delegreturn_done,
3676 .rpc_release = nfs4_delegreturn_release,
3677};
3678
Trond Myklebuste6f81072008-01-24 18:14:34 -05003679static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003680{
3681 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003682 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003683 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003684 struct rpc_message msg = {
3685 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3686 .rpc_cred = cred,
3687 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003688 struct rpc_task_setup task_setup_data = {
3689 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003690 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003691 .callback_ops = &nfs4_delegreturn_ops,
3692 .flags = RPC_TASK_ASYNC,
3693 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003694 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003695
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003696 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01003697 if (data == NULL)
3698 return -ENOMEM;
3699 data->args.fhandle = &data->fh;
3700 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003701 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003702 nfs_copy_fh(&data->fh, NFS_FH(inode));
3703 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003704 data->res.fattr = &data->fattr;
3705 data->res.server = server;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003706 data->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003707 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003708 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003709 data->rpc_status = 0;
3710
Trond Myklebustc970aa82007-07-14 15:39:59 -04003711 task_setup_data.callback_data = data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003712 msg.rpc_argp = &data->args,
3713 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003714 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003715 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003716 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003717 if (!issync)
3718 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003719 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003720 if (status != 0)
3721 goto out;
3722 status = data->rpc_status;
3723 if (status != 0)
3724 goto out;
3725 nfs_refresh_inode(inode, &data->fattr);
3726out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003727 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003728 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729}
3730
Trond Myklebuste6f81072008-01-24 18:14:34 -05003731int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732{
3733 struct nfs_server *server = NFS_SERVER(inode);
3734 struct nfs4_exception exception = { };
3735 int err;
3736 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003737 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738 switch (err) {
3739 case -NFS4ERR_STALE_STATEID:
3740 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741 case 0:
3742 return 0;
3743 }
3744 err = nfs4_handle_exception(server, err, &exception);
3745 } while (exception.retry);
3746 return err;
3747}
3748
3749#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3750#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3751
3752/*
3753 * sleep, with exponential backoff, and retry the LOCK operation.
3754 */
3755static unsigned long
3756nfs4_set_lock_task_retry(unsigned long timeout)
3757{
Matthew Wilcox150030b2007-12-06 16:24:39 -05003758 schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759 timeout <<= 1;
3760 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3761 return NFS4_LOCK_MAXTIMEOUT;
3762 return timeout;
3763}
3764
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3766{
3767 struct inode *inode = state->inode;
3768 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003769 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003770 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003772 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003774 struct nfs_lockt_res res = {
3775 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776 };
3777 struct rpc_message msg = {
3778 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3779 .rpc_argp = &arg,
3780 .rpc_resp = &res,
3781 .rpc_cred = state->owner->so_cred,
3782 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783 struct nfs4_lock_state *lsp;
3784 int status;
3785
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003786 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003787 status = nfs4_set_lock_state(state, request);
3788 if (status != 0)
3789 goto out;
3790 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003791 arg.lock_owner.id = lsp->ls_id.id;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003792 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003793 switch (status) {
3794 case 0:
3795 request->fl_type = F_UNLCK;
3796 break;
3797 case -NFS4ERR_DENIED:
3798 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05003800 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003801out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 return status;
3803}
3804
3805static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3806{
3807 struct nfs4_exception exception = { };
3808 int err;
3809
3810 do {
3811 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3812 _nfs4_proc_getlk(state, cmd, request),
3813 &exception);
3814 } while (exception.retry);
3815 return err;
3816}
3817
3818static int do_vfs_lock(struct file *file, struct file_lock *fl)
3819{
3820 int res = 0;
3821 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3822 case FL_POSIX:
3823 res = posix_lock_file_wait(file, fl);
3824 break;
3825 case FL_FLOCK:
3826 res = flock_lock_file_wait(file, fl);
3827 break;
3828 default:
3829 BUG();
3830 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831 return res;
3832}
3833
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003834struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003835 struct nfs_locku_args arg;
3836 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003837 struct nfs4_lock_state *lsp;
3838 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003839 struct file_lock fl;
3840 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003841 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003842};
3843
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003844static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3845 struct nfs_open_context *ctx,
3846 struct nfs4_lock_state *lsp,
3847 struct nfs_seqid *seqid)
3848{
3849 struct nfs4_unlockdata *p;
3850 struct inode *inode = lsp->ls_state->inode;
3851
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003852 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003853 if (p == NULL)
3854 return NULL;
3855 p->arg.fh = NFS_FH(inode);
3856 p->arg.fl = &p->fl;
3857 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003858 p->res.seqid = seqid;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003859 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003860 p->arg.stateid = &lsp->ls_stateid;
3861 p->lsp = lsp;
3862 atomic_inc(&lsp->ls_count);
3863 /* Ensure we don't close file until we're done freeing locks! */
3864 p->ctx = get_nfs_open_context(ctx);
3865 memcpy(&p->fl, fl, sizeof(p->fl));
3866 p->server = NFS_SERVER(inode);
3867 return p;
3868}
3869
Trond Myklebust06f814a2006-01-03 09:55:07 +01003870static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003871{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003872 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003873 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003874 nfs4_put_lock_state(calldata->lsp);
3875 put_nfs_open_context(calldata->ctx);
3876 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003877}
3878
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003879static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003880{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003881 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003882
Andy Adamsona8936932009-04-01 09:22:23 -04003883 nfs4_sequence_done(calldata->server, &calldata->res.seq_res,
3884 task->tk_status);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003885 if (RPC_ASSASSINATED(task))
3886 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003887 switch (task->tk_status) {
3888 case 0:
3889 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003890 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003891 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01003892 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003893 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003894 case -NFS4ERR_BAD_STATEID:
3895 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003896 case -NFS4ERR_STALE_STATEID:
3897 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003898 break;
3899 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003900 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebust0110ee12009-12-07 09:00:24 -05003901 nfs_restart_rpc(task,
Trond Myklebustd61e6122009-12-05 19:32:19 -05003902 calldata->server->nfs_client);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003903 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003904}
3905
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003906static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003907{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003908 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003910 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003911 return;
3912 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003913 /* Note: exit _without_ running nfs4_locku_done */
3914 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003915 return;
3916 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01003917 calldata->timestamp = jiffies;
Andy Adamsona8936932009-04-01 09:22:23 -04003918 if (nfs4_setup_sequence(calldata->server->nfs_client,
3919 &calldata->arg.seq_args,
3920 &calldata->res.seq_res, 1, task))
3921 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003922 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923}
3924
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003925static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003926 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003927 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01003928 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003929};
3930
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003931static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3932 struct nfs_open_context *ctx,
3933 struct nfs4_lock_state *lsp,
3934 struct nfs_seqid *seqid)
3935{
3936 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003937 struct rpc_message msg = {
3938 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3939 .rpc_cred = ctx->cred,
3940 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003941 struct rpc_task_setup task_setup_data = {
3942 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04003943 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003944 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003945 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003946 .flags = RPC_TASK_ASYNC,
3947 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003948
Frank Filz137d6ac2007-07-09 15:32:29 -07003949 /* Ensure this is an unlock - when canceling a lock, the
3950 * canceled lock is passed in, and it won't be an unlock.
3951 */
3952 fl->fl_type = F_UNLCK;
3953
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003954 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3955 if (data == NULL) {
3956 nfs_free_seqid(seqid);
3957 return ERR_PTR(-ENOMEM);
3958 }
3959
Trond Myklebust5138fde2007-07-14 15:40:01 -04003960 msg.rpc_argp = &data->arg,
3961 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003962 task_setup_data.callback_data = data;
3963 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003964}
3965
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3967{
Trond Myklebust19e03c52008-12-23 15:21:44 -05003968 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003969 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003970 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01003971 struct rpc_task *task;
3972 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003973 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974
Trond Myklebust9b073572006-06-29 16:38:34 -04003975 status = nfs4_set_lock_state(state, request);
3976 /* Unlock _before_ we do the RPC call */
3977 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003978 down_read(&nfsi->rwsem);
3979 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
3980 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003981 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003982 }
3983 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003984 if (status != 0)
3985 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003986 /* Is this a delegated lock? */
3987 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04003988 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003989 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003990 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04003991 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003992 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04003993 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04003994 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003995 status = PTR_ERR(task);
3996 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04003997 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003998 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003999 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004000out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004001 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004002 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003}
4004
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004005struct nfs4_lockdata {
4006 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004007 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004008 struct nfs4_lock_state *lsp;
4009 struct nfs_open_context *ctx;
4010 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004011 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004012 int rpc_status;
4013 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004014 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004015};
4016
4017static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004018 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4019 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004020{
4021 struct nfs4_lockdata *p;
4022 struct inode *inode = lsp->ls_state->inode;
4023 struct nfs_server *server = NFS_SERVER(inode);
4024
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004025 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004026 if (p == NULL)
4027 return NULL;
4028
4029 p->arg.fh = NFS_FH(inode);
4030 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004031 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004032 if (p->arg.open_seqid == NULL)
4033 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004034 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004035 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004036 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004037 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004038 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04004039 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004040 p->res.lock_seqid = p->arg.lock_seqid;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04004041 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004042 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004043 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004044 atomic_inc(&lsp->ls_count);
4045 p->ctx = get_nfs_open_context(ctx);
4046 memcpy(&p->fl, fl, sizeof(p->fl));
4047 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004048out_free_seqid:
4049 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004050out_free:
4051 kfree(p);
4052 return NULL;
4053}
4054
4055static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4056{
4057 struct nfs4_lockdata *data = calldata;
4058 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059
Harvey Harrison3110ff82008-05-02 13:42:44 -07004060 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004061 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4062 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004063 /* Do we need to do an open_to_lock_owner? */
4064 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004065 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4066 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004067 data->arg.open_stateid = &state->stateid;
4068 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004069 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004070 } else
4071 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004072 data->timestamp = jiffies;
Andy Adamson66179ef2009-04-01 09:22:22 -04004073 if (nfs4_setup_sequence(data->server->nfs_client, &data->arg.seq_args,
4074 &data->res.seq_res, 1, task))
4075 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004076 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004077 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004078}
4079
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004080static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4081{
4082 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4083 nfs4_lock_prepare(task, calldata);
4084}
4085
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004086static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4087{
4088 struct nfs4_lockdata *data = calldata;
4089
Harvey Harrison3110ff82008-05-02 13:42:44 -07004090 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004091
Trond Myklebustd61e6122009-12-05 19:32:19 -05004092 nfs4_sequence_done(data->server, &data->res.seq_res,
4093 task->tk_status);
Andy Adamson66179ef2009-04-01 09:22:22 -04004094
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004095 data->rpc_status = task->tk_status;
4096 if (RPC_ASSASSINATED(task))
4097 goto out;
4098 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004099 if (data->rpc_status == 0)
4100 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4101 else
4102 goto out;
4103 }
4104 if (data->rpc_status == 0) {
4105 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4106 sizeof(data->lsp->ls_stateid.data));
4107 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Trond Myklebust88be9f92007-06-05 10:42:27 -04004108 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004109 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004110out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004111 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004112}
4113
4114static void nfs4_lock_release(void *calldata)
4115{
4116 struct nfs4_lockdata *data = calldata;
4117
Harvey Harrison3110ff82008-05-02 13:42:44 -07004118 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004119 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004120 if (data->cancelled != 0) {
4121 struct rpc_task *task;
4122 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4123 data->arg.lock_seqid);
4124 if (!IS_ERR(task))
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004125 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004126 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004127 } else
4128 nfs_free_seqid(data->arg.lock_seqid);
4129 nfs4_put_lock_state(data->lsp);
4130 put_nfs_open_context(data->ctx);
4131 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004132 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004133}
4134
4135static const struct rpc_call_ops nfs4_lock_ops = {
4136 .rpc_call_prepare = nfs4_lock_prepare,
4137 .rpc_call_done = nfs4_lock_done,
4138 .rpc_release = nfs4_lock_release,
4139};
4140
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004141static const struct rpc_call_ops nfs4_recover_lock_ops = {
4142 .rpc_call_prepare = nfs4_recover_lock_prepare,
4143 .rpc_call_done = nfs4_lock_done,
4144 .rpc_release = nfs4_lock_release,
4145};
4146
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004147static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4148{
4149 struct nfs_client *clp = server->nfs_client;
4150 struct nfs4_state *state = lsp->ls_state;
4151
4152 switch (error) {
4153 case -NFS4ERR_ADMIN_REVOKED:
4154 case -NFS4ERR_BAD_STATEID:
4155 case -NFS4ERR_EXPIRED:
4156 if (new_lock_owner != 0 ||
4157 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4158 nfs4_state_mark_reclaim_nograce(clp, state);
4159 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004160 break;
4161 case -NFS4ERR_STALE_STATEID:
4162 if (new_lock_owner != 0 ||
4163 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4164 nfs4_state_mark_reclaim_reboot(clp, state);
4165 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004166 };
4167}
4168
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004169static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004170{
4171 struct nfs4_lockdata *data;
4172 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004173 struct rpc_message msg = {
4174 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4175 .rpc_cred = state->owner->so_cred,
4176 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004177 struct rpc_task_setup task_setup_data = {
4178 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004179 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004180 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004181 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004182 .flags = RPC_TASK_ASYNC,
4183 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004184 int ret;
4185
Harvey Harrison3110ff82008-05-02 13:42:44 -07004186 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004187 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004188 fl->fl_u.nfs4_fl.owner,
4189 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004190 if (data == NULL)
4191 return -ENOMEM;
4192 if (IS_SETLKW(cmd))
4193 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004194 if (recovery_type > NFS_LOCK_NEW) {
4195 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004196 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004197 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4198 }
Trond Myklebust5138fde2007-07-14 15:40:01 -04004199 msg.rpc_argp = &data->arg,
4200 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004201 task_setup_data.callback_data = data;
4202 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004203 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004204 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004205 ret = nfs4_wait_for_completion_rpc_task(task);
4206 if (ret == 0) {
4207 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004208 if (ret)
4209 nfs4_handle_setlk_error(data->server, data->lsp,
4210 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004211 } else
4212 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004213 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004214 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004215 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216}
4217
4218static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4219{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004220 struct nfs_server *server = NFS_SERVER(state->inode);
4221 struct nfs4_exception exception = { };
4222 int err;
4223
4224 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004225 /* Cache the lock if possible... */
4226 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4227 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004228 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Jeff Layton2c643482010-01-07 09:42:03 -05004229 if (err != -NFS4ERR_DELAY && err != -EKEYEXPIRED)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004230 break;
4231 nfs4_handle_exception(server, err, &exception);
4232 } while (exception.retry);
4233 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234}
4235
4236static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4237{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004238 struct nfs_server *server = NFS_SERVER(state->inode);
4239 struct nfs4_exception exception = { };
4240 int err;
4241
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004242 err = nfs4_set_lock_state(state, request);
4243 if (err != 0)
4244 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004245 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004246 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4247 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004248 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004249 switch (err) {
4250 default:
4251 goto out;
4252 case -NFS4ERR_GRACE:
4253 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -05004254 case -EKEYEXPIRED:
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004255 nfs4_handle_exception(server, err, &exception);
4256 err = 0;
4257 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004258 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004259out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004260 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261}
4262
4263static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4264{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004265 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004266 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004267 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004269 if ((fl_flags & FL_POSIX) &&
4270 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4271 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004272 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004273 status = nfs4_set_lock_state(state, request);
4274 if (status != 0)
4275 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004276 request->fl_flags |= FL_ACCESS;
4277 status = do_vfs_lock(request->fl_file, request);
4278 if (status < 0)
4279 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004280 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004281 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004282 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004283 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004284 request->fl_flags = fl_flags & ~FL_SLEEP;
4285 status = do_vfs_lock(request->fl_file, request);
4286 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004287 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004288 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004289 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004290 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004291 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004292 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004293 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07004294 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004295out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004296 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004297out:
4298 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299 return status;
4300}
4301
4302static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4303{
4304 struct nfs4_exception exception = { };
4305 int err;
4306
4307 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004308 err = _nfs4_proc_setlk(state, cmd, request);
4309 if (err == -NFS4ERR_DENIED)
4310 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004312 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 } while (exception.retry);
4314 return err;
4315}
4316
4317static int
4318nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4319{
4320 struct nfs_open_context *ctx;
4321 struct nfs4_state *state;
4322 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4323 int status;
4324
4325 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004326 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 state = ctx->state;
4328
4329 if (request->fl_start < 0 || request->fl_end < 0)
4330 return -EINVAL;
4331
Trond Myklebustd9531262009-07-21 19:22:38 -04004332 if (IS_GETLK(cmd)) {
4333 if (state != NULL)
4334 return nfs4_proc_getlk(state, F_GETLK, request);
4335 return 0;
4336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337
4338 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4339 return -EINVAL;
4340
Trond Myklebustd9531262009-07-21 19:22:38 -04004341 if (request->fl_type == F_UNLCK) {
4342 if (state != NULL)
4343 return nfs4_proc_unlck(state, cmd, request);
4344 return 0;
4345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346
Trond Myklebustd9531262009-07-21 19:22:38 -04004347 if (state == NULL)
4348 return -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349 do {
4350 status = nfs4_proc_setlk(state, cmd, request);
4351 if ((status != -EAGAIN) || IS_SETLK(cmd))
4352 break;
4353 timeout = nfs4_set_lock_task_retry(timeout);
4354 status = -ERESTARTSYS;
4355 if (signalled())
4356 break;
4357 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358 return status;
4359}
4360
Trond Myklebust888e6942005-11-04 15:38:11 -05004361int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4362{
4363 struct nfs_server *server = NFS_SERVER(state->inode);
4364 struct nfs4_exception exception = { };
4365 int err;
4366
4367 err = nfs4_set_lock_state(state, fl);
4368 if (err != 0)
4369 goto out;
4370 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004371 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004372 switch (err) {
4373 default:
4374 printk(KERN_ERR "%s: unhandled error %d.\n",
4375 __func__, err);
4376 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004377 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004378 goto out;
4379 case -NFS4ERR_EXPIRED:
4380 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004381 case -NFS4ERR_STALE_STATEID:
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004382 case -NFS4ERR_BADSESSION:
4383 case -NFS4ERR_BADSLOT:
4384 case -NFS4ERR_BAD_HIGH_SLOT:
4385 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4386 case -NFS4ERR_DEADSESSION:
Trond Myklebustd5122202009-06-17 13:22:58 -07004387 nfs4_schedule_state_recovery(server->nfs_client);
4388 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004389 case -ERESTARTSYS:
4390 /*
4391 * The show must go on: exit, but mark the
4392 * stateid as needing recovery.
4393 */
4394 case -NFS4ERR_ADMIN_REVOKED:
4395 case -NFS4ERR_BAD_STATEID:
4396 case -NFS4ERR_OPENMODE:
4397 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
4398 err = 0;
4399 goto out;
4400 case -ENOMEM:
4401 case -NFS4ERR_DENIED:
4402 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4403 err = 0;
4404 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004405 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -05004406 case -EKEYEXPIRED:
Trond Myklebustd5122202009-06-17 13:22:58 -07004407 break;
4408 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004409 err = nfs4_handle_exception(server, err, &exception);
4410 } while (exception.retry);
4411out:
4412 return err;
4413}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004414
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004415#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4416
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004417int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
4418 size_t buflen, int flags)
4419{
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004420 struct inode *inode = dentry->d_inode;
4421
4422 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4423 return -EOPNOTSUPP;
4424
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004425 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004426}
4427
4428/* The getxattr man page suggests returning -ENODATA for unknown attributes,
4429 * and that's what we'll do for e.g. user attributes that haven't been set.
4430 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
4431 * attributes in kernel-managed attribute namespaces. */
4432ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
4433 size_t buflen)
4434{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004435 struct inode *inode = dentry->d_inode;
4436
4437 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4438 return -EOPNOTSUPP;
4439
4440 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004441}
4442
4443ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
4444{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004445 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004446
J. Bruce Fields096455a2006-03-20 23:23:42 -05004447 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4448 return 0;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004449 if (buf && buflen < len)
4450 return -ERANGE;
4451 if (buf)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004452 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
4453 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004454}
4455
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004456static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4457{
4458 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
4459 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4460 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4461 return;
4462
4463 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4464 NFS_ATTR_FATTR_NLINK;
4465 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4466 fattr->nlink = 2;
4467}
4468
Trond Myklebust56659e92007-07-17 21:52:39 -04004469int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004470 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004471{
4472 struct nfs_server *server = NFS_SERVER(dir);
4473 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004474 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4475 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004476 };
4477 struct nfs4_fs_locations_arg args = {
4478 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004479 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004480 .page = page,
4481 .bitmask = bitmask,
4482 };
Benny Halevy22958462009-04-01 09:22:02 -04004483 struct nfs4_fs_locations_res res = {
4484 .fs_locations = fs_locations,
4485 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004486 struct rpc_message msg = {
4487 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4488 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004489 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004490 };
4491 int status;
4492
Harvey Harrison3110ff82008-05-02 13:42:44 -07004493 dprintk("%s: start\n", __func__);
Trond Myklebustc228fd32007-01-13 02:28:11 -05004494 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004495 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004496 fs_locations->nlocations = 0;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04004497 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004498 nfs_fixup_referral_attributes(&fs_locations->fattr);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004499 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004500 return status;
4501}
4502
Andy Adamson557134a2009-04-01 09:21:53 -04004503#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04004504/*
4505 * nfs4_proc_exchange_id()
4506 *
4507 * Since the clientid has expired, all compounds using sessions
4508 * associated with the stale clientid will be returning
4509 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4510 * be in some phase of session reset.
4511 */
Andy Adamson4d643d12009-12-04 15:52:24 -05004512int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004513{
4514 nfs4_verifier verifier;
4515 struct nfs41_exchange_id_args args = {
4516 .client = clp,
4517 .flags = clp->cl_exchange_flags,
4518 };
4519 struct nfs41_exchange_id_res res = {
4520 .client = clp,
4521 };
4522 int status;
4523 struct rpc_message msg = {
4524 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4525 .rpc_argp = &args,
4526 .rpc_resp = &res,
4527 .rpc_cred = cred,
4528 };
4529 __be32 *p;
4530
4531 dprintk("--> %s\n", __func__);
4532 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04004533
Alexandros Batsakis7b183d02009-12-05 13:33:25 -05004534 /* Remove server-only flags */
4535 args.flags &= ~EXCHGID4_FLAG_CONFIRMED_R;
4536
Benny Halevy99fe60d2009-04-01 09:22:29 -04004537 p = (u32 *)verifier.data;
4538 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4539 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4540 args.verifier = &verifier;
4541
4542 while (1) {
4543 args.id_len = scnprintf(args.id, sizeof(args.id),
4544 "%s/%s %u",
4545 clp->cl_ipaddr,
4546 rpc_peeraddr2str(clp->cl_rpcclient,
4547 RPC_DISPLAY_ADDR),
4548 clp->cl_id_uniquifier);
4549
4550 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
4551
Andy Adamson180b62a2010-03-02 13:19:36 -05004552 if (status != -NFS4ERR_CLID_INUSE)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004553 break;
4554
4555 if (signalled())
4556 break;
4557
4558 if (++clp->cl_id_uniquifier == 0)
4559 break;
4560 }
4561
4562 dprintk("<-- %s status= %d\n", __func__, status);
4563 return status;
4564}
4565
Andy Adamson2050f0c2009-04-01 09:22:30 -04004566struct nfs4_get_lease_time_data {
4567 struct nfs4_get_lease_time_args *args;
4568 struct nfs4_get_lease_time_res *res;
4569 struct nfs_client *clp;
4570};
4571
4572static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4573 void *calldata)
4574{
4575 int ret;
4576 struct nfs4_get_lease_time_data *data =
4577 (struct nfs4_get_lease_time_data *)calldata;
4578
4579 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004580 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004581 /* just setup sequence, do not trigger session recovery
4582 since we're invoked within one */
4583 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004584 &data->args->la_seq_args,
4585 &data->res->lr_seq_res, 0, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004586
4587 BUG_ON(ret == -EAGAIN);
4588 rpc_call_start(task);
4589 dprintk("<-- %s\n", __func__);
4590}
4591
4592/*
4593 * Called from nfs4_state_manager thread for session setup, so don't recover
4594 * from sequence operation or clientid errors.
4595 */
4596static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4597{
4598 struct nfs4_get_lease_time_data *data =
4599 (struct nfs4_get_lease_time_data *)calldata;
4600
4601 dprintk("--> %s\n", __func__);
Trond Myklebustd185a332010-06-16 09:52:25 -04004602 nfs41_sequence_done(&data->res->lr_seq_res);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004603 switch (task->tk_status) {
4604 case -NFS4ERR_DELAY:
4605 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05004606 case -EKEYEXPIRED:
Andy Adamson2050f0c2009-04-01 09:22:30 -04004607 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4608 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4609 task->tk_status = 0;
Trond Myklebust0110ee12009-12-07 09:00:24 -05004610 nfs_restart_rpc(task, data->clp);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004611 return;
4612 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04004613 dprintk("<-- %s\n", __func__);
4614}
4615
4616struct rpc_call_ops nfs4_get_lease_time_ops = {
4617 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4618 .rpc_call_done = nfs4_get_lease_time_done,
4619};
4620
4621int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4622{
4623 struct rpc_task *task;
4624 struct nfs4_get_lease_time_args args;
4625 struct nfs4_get_lease_time_res res = {
4626 .lr_fsinfo = fsinfo,
4627 };
4628 struct nfs4_get_lease_time_data data = {
4629 .args = &args,
4630 .res = &res,
4631 .clp = clp,
4632 };
4633 struct rpc_message msg = {
4634 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4635 .rpc_argp = &args,
4636 .rpc_resp = &res,
4637 };
4638 struct rpc_task_setup task_setup = {
4639 .rpc_client = clp->cl_rpcclient,
4640 .rpc_message = &msg,
4641 .callback_ops = &nfs4_get_lease_time_ops,
4642 .callback_data = &data
4643 };
4644 int status;
4645
4646 res.lr_seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
4647 dprintk("--> %s\n", __func__);
4648 task = rpc_run_task(&task_setup);
4649
4650 if (IS_ERR(task))
4651 status = PTR_ERR(task);
4652 else {
4653 status = task->tk_status;
4654 rpc_put_task(task);
4655 }
4656 dprintk("<-- %s return %d\n", __func__, status);
4657
4658 return status;
4659}
4660
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004661/*
4662 * Reset a slot table
4663 */
Andy Adamson104aeba2010-01-14 17:45:10 -05004664static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
4665 int ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004666{
Andy Adamson104aeba2010-01-14 17:45:10 -05004667 struct nfs4_slot *new = NULL;
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004668 int i;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004669 int ret = 0;
4670
Andy Adamson104aeba2010-01-14 17:45:10 -05004671 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
4672 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004673
Andy Adamson104aeba2010-01-14 17:45:10 -05004674 /* Does the newly negotiated max_reqs match the existing slot table? */
4675 if (max_reqs != tbl->max_slots) {
4676 ret = -ENOMEM;
4677 new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004678 GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05004679 if (!new)
4680 goto out;
4681 ret = 0;
4682 kfree(tbl->slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004683 }
4684 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson104aeba2010-01-14 17:45:10 -05004685 if (new) {
4686 tbl->slots = new;
4687 tbl->max_slots = max_reqs;
4688 }
4689 for (i = 0; i < tbl->max_slots; ++i)
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004690 tbl->slots[i].seq_nr = ivalue;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004691 spin_unlock(&tbl->slot_tbl_lock);
4692 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4693 tbl, tbl->slots, tbl->max_slots);
4694out:
4695 dprintk("<-- %s: return %d\n", __func__, ret);
4696 return ret;
4697}
4698
Andy Adamsonfc931582009-04-01 09:22:31 -04004699/*
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004700 * Reset the forechannel and backchannel slot tables
4701 */
4702static int nfs4_reset_slot_tables(struct nfs4_session *session)
4703{
4704 int status;
4705
4706 status = nfs4_reset_slot_table(&session->fc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05004707 session->fc_attrs.max_reqs, 1);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004708 if (status)
4709 return status;
4710
4711 status = nfs4_reset_slot_table(&session->bc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05004712 session->bc_attrs.max_reqs, 0);
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004713 return status;
4714}
4715
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004716/* Destroy the slot table */
4717static void nfs4_destroy_slot_tables(struct nfs4_session *session)
4718{
4719 if (session->fc_slot_table.slots != NULL) {
4720 kfree(session->fc_slot_table.slots);
4721 session->fc_slot_table.slots = NULL;
4722 }
4723 if (session->bc_slot_table.slots != NULL) {
4724 kfree(session->bc_slot_table.slots);
4725 session->bc_slot_table.slots = NULL;
4726 }
4727 return;
4728}
4729
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004730/*
Andy Adamsonfc931582009-04-01 09:22:31 -04004731 * Initialize slot table
4732 */
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004733static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
4734 int max_slots, int ivalue)
Andy Adamsonfc931582009-04-01 09:22:31 -04004735{
Andy Adamsonfc931582009-04-01 09:22:31 -04004736 struct nfs4_slot *slot;
4737 int ret = -ENOMEM;
4738
4739 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
4740
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004741 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
Andy Adamsonfc931582009-04-01 09:22:31 -04004742
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004743 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_NOFS);
Andy Adamsonfc931582009-04-01 09:22:31 -04004744 if (!slot)
4745 goto out;
Andy Adamsonfc931582009-04-01 09:22:31 -04004746 ret = 0;
4747
4748 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonfc931582009-04-01 09:22:31 -04004749 tbl->max_slots = max_slots;
4750 tbl->slots = slot;
4751 tbl->highest_used_slotid = -1; /* no slot is currently used */
4752 spin_unlock(&tbl->slot_tbl_lock);
4753 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4754 tbl, tbl->slots, tbl->max_slots);
4755out:
4756 dprintk("<-- %s: return %d\n", __func__, ret);
4757 return ret;
Andy Adamsonfc931582009-04-01 09:22:31 -04004758}
4759
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004760/*
4761 * Initialize the forechannel and backchannel tables
4762 */
4763static int nfs4_init_slot_tables(struct nfs4_session *session)
4764{
Trond Myklebustf26468f2009-12-05 19:32:11 -05004765 struct nfs4_slot_table *tbl;
4766 int status = 0;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004767
Trond Myklebustf26468f2009-12-05 19:32:11 -05004768 tbl = &session->fc_slot_table;
4769 if (tbl->slots == NULL) {
4770 status = nfs4_init_slot_table(tbl,
4771 session->fc_attrs.max_reqs, 1);
4772 if (status)
4773 return status;
4774 }
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004775
Trond Myklebustf26468f2009-12-05 19:32:11 -05004776 tbl = &session->bc_slot_table;
4777 if (tbl->slots == NULL) {
4778 status = nfs4_init_slot_table(tbl,
4779 session->bc_attrs.max_reqs, 0);
4780 if (status)
4781 nfs4_destroy_slot_tables(session);
4782 }
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004783
4784 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04004785}
4786
4787struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
4788{
4789 struct nfs4_session *session;
4790 struct nfs4_slot_table *tbl;
4791
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004792 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04004793 if (!session)
4794 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04004795
Andy Adamson76db6d92009-04-01 09:22:38 -04004796 /*
4797 * The create session reply races with the server back
4798 * channel probe. Mark the client NFS_CS_SESSION_INITING
4799 * so that the client back channel can find the
4800 * nfs_client struct
4801 */
4802 clp->cl_cons_state = NFS_CS_SESSION_INITING;
Andy Adamsonea028ac2009-12-04 15:55:38 -05004803 init_completion(&session->complete);
Andy Adamson76db6d92009-04-01 09:22:38 -04004804
Andy Adamson557134a2009-04-01 09:21:53 -04004805 tbl = &session->fc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05004806 tbl->highest_used_slotid = -1;
Andy Adamson557134a2009-04-01 09:21:53 -04004807 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004808 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004809
4810 tbl = &session->bc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05004811 tbl->highest_used_slotid = -1;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004812 spin_lock_init(&tbl->slot_tbl_lock);
4813 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
4814
Andy Adamson557134a2009-04-01 09:21:53 -04004815 session->clp = clp;
4816 return session;
4817}
4818
4819void nfs4_destroy_session(struct nfs4_session *session)
4820{
Andy Adamson5a0ffe52009-04-01 09:23:18 -04004821 nfs4_proc_destroy_session(session);
4822 dprintk("%s Destroy backchannel for xprt %p\n",
4823 __func__, session->clp->cl_rpcclient->cl_xprt);
4824 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
4825 NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004826 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04004827 kfree(session);
4828}
4829
Andy Adamsonfc931582009-04-01 09:22:31 -04004830/*
4831 * Initialize the values to be used by the client in CREATE_SESSION
4832 * If nfs4_init_session set the fore channel request and response sizes,
4833 * use them.
4834 *
4835 * Set the back channel max_resp_sz_cached to zero to force the client to
4836 * always set csa_cachethis to FALSE because the current implementation
4837 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
4838 */
4839static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
4840{
4841 struct nfs4_session *session = args->client->cl_session;
4842 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
4843 mxresp_sz = session->fc_attrs.max_resp_sz;
4844
4845 if (mxrqst_sz == 0)
4846 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
4847 if (mxresp_sz == 0)
4848 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
4849 /* Fore channel attributes */
4850 args->fc_attrs.headerpadsz = 0;
4851 args->fc_attrs.max_rqst_sz = mxrqst_sz;
4852 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04004853 args->fc_attrs.max_ops = NFS4_MAX_OPS;
4854 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
4855
4856 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05004857 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04004858 __func__,
4859 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05004860 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04004861
4862 /* Back channel attributes */
4863 args->bc_attrs.headerpadsz = 0;
4864 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
4865 args->bc_attrs.max_resp_sz = PAGE_SIZE;
4866 args->bc_attrs.max_resp_sz_cached = 0;
4867 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
4868 args->bc_attrs.max_reqs = 1;
4869
4870 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
4871 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4872 __func__,
4873 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
4874 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
4875 args->bc_attrs.max_reqs);
4876}
4877
Andy Adamson8d353012009-04-01 09:22:32 -04004878static int _verify_channel_attr(char *chan, char *attr_name, u32 sent, u32 rcvd)
4879{
4880 if (rcvd <= sent)
4881 return 0;
4882 printk(KERN_WARNING "%s: Session INVALID: %s channel %s increased. "
4883 "sent=%u rcvd=%u\n", __func__, chan, attr_name, sent, rcvd);
4884 return -EINVAL;
4885}
4886
4887#define _verify_fore_channel_attr(_name_) \
4888 _verify_channel_attr("fore", #_name_, \
4889 args->fc_attrs._name_, \
4890 session->fc_attrs._name_)
4891
4892#define _verify_back_channel_attr(_name_) \
4893 _verify_channel_attr("back", #_name_, \
4894 args->bc_attrs._name_, \
4895 session->bc_attrs._name_)
4896
4897/*
4898 * The server is not allowed to increase the fore channel header pad size,
4899 * maximum response size, or maximum number of operations.
4900 *
4901 * The back channel attributes are only negotiatied down: We send what the
4902 * (back channel) server insists upon.
4903 */
4904static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
4905 struct nfs4_session *session)
4906{
4907 int ret = 0;
4908
4909 ret |= _verify_fore_channel_attr(headerpadsz);
4910 ret |= _verify_fore_channel_attr(max_resp_sz);
4911 ret |= _verify_fore_channel_attr(max_ops);
4912
4913 ret |= _verify_back_channel_attr(headerpadsz);
4914 ret |= _verify_back_channel_attr(max_rqst_sz);
4915 ret |= _verify_back_channel_attr(max_resp_sz);
4916 ret |= _verify_back_channel_attr(max_resp_sz_cached);
4917 ret |= _verify_back_channel_attr(max_ops);
4918 ret |= _verify_back_channel_attr(max_reqs);
4919
4920 return ret;
4921}
4922
Andy Adamsonfc931582009-04-01 09:22:31 -04004923static int _nfs4_proc_create_session(struct nfs_client *clp)
4924{
4925 struct nfs4_session *session = clp->cl_session;
4926 struct nfs41_create_session_args args = {
4927 .client = clp,
4928 .cb_program = NFS4_CALLBACK,
4929 };
4930 struct nfs41_create_session_res res = {
4931 .client = clp,
4932 };
4933 struct rpc_message msg = {
4934 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
4935 .rpc_argp = &args,
4936 .rpc_resp = &res,
4937 };
4938 int status;
4939
4940 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04004941 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04004942
4943 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4944
Andy Adamson8d353012009-04-01 09:22:32 -04004945 if (!status)
4946 /* Verify the session's negotiated channel_attrs values */
4947 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04004948 if (!status) {
4949 /* Increment the clientid slot sequence id */
4950 clp->cl_seqid++;
4951 }
4952
4953 return status;
4954}
4955
4956/*
4957 * Issues a CREATE_SESSION operation to the server.
4958 * It is the responsibility of the caller to verify the session is
4959 * expired before calling this routine.
4960 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05004961int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04004962{
4963 int status;
4964 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04004965 struct nfs4_session *session = clp->cl_session;
4966
4967 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
4968
4969 status = _nfs4_proc_create_session(clp);
4970 if (status)
4971 goto out;
4972
Trond Myklebustf26468f2009-12-05 19:32:11 -05004973 /* Init and reset the fore channel */
4974 status = nfs4_init_slot_tables(session);
4975 dprintk("slot table initialization returned %d\n", status);
4976 if (status)
4977 goto out;
4978 status = nfs4_reset_slot_tables(session);
4979 dprintk("slot table reset returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04004980 if (status)
4981 goto out;
4982
4983 ptr = (unsigned *)&session->sess_id.data[0];
4984 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
4985 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04004986out:
4987 dprintk("<-- %s\n", __func__);
4988 return status;
4989}
4990
Andy Adamson0f3e66c2009-04-01 09:22:34 -04004991/*
4992 * Issue the over-the-wire RPC DESTROY_SESSION.
4993 * The caller must serialize access to this routine.
4994 */
4995int nfs4_proc_destroy_session(struct nfs4_session *session)
4996{
4997 int status = 0;
4998 struct rpc_message msg;
4999
5000 dprintk("--> nfs4_proc_destroy_session\n");
5001
5002 /* session is still being setup */
5003 if (session->clp->cl_cons_state != NFS_CS_READY)
5004 return status;
5005
5006 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5007 msg.rpc_argp = session;
5008 msg.rpc_resp = NULL;
5009 msg.rpc_cred = NULL;
5010 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
5011
5012 if (status)
5013 printk(KERN_WARNING
5014 "Got error %d from the server on DESTROY_SESSION. "
5015 "Session has been destroyed regardless...\n", status);
5016
5017 dprintk("<-- nfs4_proc_destroy_session\n");
5018 return status;
5019}
5020
Trond Myklebustfccba802009-07-21 16:48:07 -04005021int nfs4_init_session(struct nfs_server *server)
5022{
5023 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005024 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005025 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005026 int ret;
5027
5028 if (!nfs4_has_session(clp))
5029 return 0;
5030
Andy Adamson68bf05e2009-12-15 12:55:02 -05005031 rsize = server->rsize;
5032 if (rsize == 0)
5033 rsize = NFS_MAX_FILE_IO_SIZE;
5034 wsize = server->wsize;
5035 if (wsize == 0)
5036 wsize = NFS_MAX_FILE_IO_SIZE;
5037
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005038 session = clp->cl_session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005039 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5040 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005041
Trond Myklebustfccba802009-07-21 16:48:07 -04005042 ret = nfs4_recover_expired_lease(server);
5043 if (!ret)
5044 ret = nfs4_check_client_ready(clp);
5045 return ret;
5046}
5047
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005048/*
5049 * Renew the cl_session lease.
5050 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005051struct nfs4_sequence_data {
5052 struct nfs_client *clp;
5053 struct nfs4_sequence_args args;
5054 struct nfs4_sequence_res res;
5055};
5056
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005057static void nfs41_sequence_release(void *data)
5058{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005059 struct nfs4_sequence_data *calldata = data;
5060 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005061
Alexandros Batsakis71358402010-02-05 03:45:05 -08005062 if (atomic_read(&clp->cl_count) > 1)
5063 nfs4_schedule_state_renewal(clp);
5064 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005065 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005066}
5067
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005068static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5069{
5070 switch(task->tk_status) {
5071 case -NFS4ERR_DELAY:
5072 case -EKEYEXPIRED:
5073 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5074 return -EAGAIN;
5075 default:
5076 nfs4_schedule_state_recovery(clp);
5077 }
5078 return 0;
5079}
5080
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005081static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005082{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005083 struct nfs4_sequence_data *calldata = data;
5084 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005085
Trond Myklebustd185a332010-06-16 09:52:25 -04005086 nfs41_sequence_done(task->tk_msg.rpc_resp);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005087
5088 if (task->tk_status < 0) {
5089 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005090 if (atomic_read(&clp->cl_count) == 1)
5091 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005092
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005093 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5094 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005095 return;
5096 }
5097 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005098 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005099out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005100 dprintk("<-- %s\n", __func__);
5101}
5102
5103static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5104{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005105 struct nfs4_sequence_data *calldata = data;
5106 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005107 struct nfs4_sequence_args *args;
5108 struct nfs4_sequence_res *res;
5109
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005110 args = task->tk_msg.rpc_argp;
5111 res = task->tk_msg.rpc_resp;
5112
5113 if (nfs4_setup_sequence(clp, args, res, 0, task))
5114 return;
5115 rpc_call_start(task);
5116}
5117
5118static const struct rpc_call_ops nfs41_sequence_ops = {
5119 .rpc_call_done = nfs41_sequence_call_done,
5120 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005121 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005122};
5123
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005124static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005125{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005126 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005127 struct rpc_message msg = {
5128 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5129 .rpc_cred = cred,
5130 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005131 struct rpc_task_setup task_setup_data = {
5132 .rpc_client = clp->cl_rpcclient,
5133 .rpc_message = &msg,
5134 .callback_ops = &nfs41_sequence_ops,
5135 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5136 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005137
Alexandros Batsakis71358402010-02-05 03:45:05 -08005138 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005139 return ERR_PTR(-EIO);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005140 calldata = kmalloc(sizeof(*calldata), GFP_NOFS);
5141 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005142 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005143 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005144 }
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005145 calldata->res.sr_slotid = NFS4_MAX_SLOT_TABLE;
5146 msg.rpc_argp = &calldata->args;
5147 msg.rpc_resp = &calldata->res;
5148 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005149 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005150
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005151 return rpc_run_task(&task_setup_data);
5152}
5153
5154static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5155{
5156 struct rpc_task *task;
5157 int ret = 0;
5158
5159 task = _nfs41_proc_sequence(clp, cred);
5160 if (IS_ERR(task))
5161 ret = PTR_ERR(task);
5162 else
5163 rpc_put_task(task);
5164 dprintk("<-- %s status=%d\n", __func__, ret);
5165 return ret;
5166}
5167
5168static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5169{
5170 struct rpc_task *task;
5171 int ret;
5172
5173 task = _nfs41_proc_sequence(clp, cred);
5174 if (IS_ERR(task)) {
5175 ret = PTR_ERR(task);
5176 goto out;
5177 }
5178 ret = rpc_wait_for_completion_task(task);
5179 if (!ret)
5180 ret = task->tk_status;
5181 rpc_put_task(task);
5182out:
5183 dprintk("<-- %s status=%d\n", __func__, ret);
5184 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005185}
5186
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005187struct nfs4_reclaim_complete_data {
5188 struct nfs_client *clp;
5189 struct nfs41_reclaim_complete_args arg;
5190 struct nfs41_reclaim_complete_res res;
5191};
5192
5193static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5194{
5195 struct nfs4_reclaim_complete_data *calldata = data;
5196
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005197 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005198 if (nfs4_setup_sequence(calldata->clp, &calldata->arg.seq_args,
5199 &calldata->res.seq_res, 0, task))
5200 return;
5201
5202 rpc_call_start(task);
5203}
5204
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005205static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5206{
5207 switch(task->tk_status) {
5208 case 0:
5209 case -NFS4ERR_COMPLETE_ALREADY:
5210 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5211 break;
5212 case -NFS4ERR_DELAY:
5213 case -EKEYEXPIRED:
5214 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5215 return -EAGAIN;
5216 default:
5217 nfs4_schedule_state_recovery(clp);
5218 }
5219 return 0;
5220}
5221
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005222static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5223{
5224 struct nfs4_reclaim_complete_data *calldata = data;
5225 struct nfs_client *clp = calldata->clp;
5226 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5227
5228 dprintk("--> %s\n", __func__);
Trond Myklebustd185a332010-06-16 09:52:25 -04005229 nfs41_sequence_done(res);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005230
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005231 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5232 rpc_restart_call_prepare(task);
5233 return;
5234 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005235 dprintk("<-- %s\n", __func__);
5236}
5237
5238static void nfs4_free_reclaim_complete_data(void *data)
5239{
5240 struct nfs4_reclaim_complete_data *calldata = data;
5241
5242 kfree(calldata);
5243}
5244
5245static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5246 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5247 .rpc_call_done = nfs4_reclaim_complete_done,
5248 .rpc_release = nfs4_free_reclaim_complete_data,
5249};
5250
5251/*
5252 * Issue a global reclaim complete.
5253 */
5254static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5255{
5256 struct nfs4_reclaim_complete_data *calldata;
5257 struct rpc_task *task;
5258 struct rpc_message msg = {
5259 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5260 };
5261 struct rpc_task_setup task_setup_data = {
5262 .rpc_client = clp->cl_rpcclient,
5263 .rpc_message = &msg,
5264 .callback_ops = &nfs4_reclaim_complete_call_ops,
5265 .flags = RPC_TASK_ASYNC,
5266 };
5267 int status = -ENOMEM;
5268
5269 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005270 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005271 if (calldata == NULL)
5272 goto out;
5273 calldata->clp = clp;
5274 calldata->arg.one_fs = 0;
5275 calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
5276
5277 msg.rpc_argp = &calldata->arg;
5278 msg.rpc_resp = &calldata->res;
5279 task_setup_data.callback_data = calldata;
5280 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005281 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005282 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005283 goto out;
5284 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005285 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005286 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005287out:
5288 dprintk("<-- %s status=%d\n", __func__, status);
5289 return status;
5290}
Andy Adamson557134a2009-04-01 09:21:53 -04005291#endif /* CONFIG_NFS_V4_1 */
5292
Andy Adamson591d71c2009-04-01 09:22:47 -04005293struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005294 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005295 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005296 .recover_open = nfs4_open_reclaim,
5297 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04005298 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005299 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300};
5301
Andy Adamson591d71c2009-04-01 09:22:47 -04005302#if defined(CONFIG_NFS_V4_1)
5303struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
5304 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5305 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5306 .recover_open = nfs4_open_reclaim,
5307 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05005308 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005309 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005310 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04005311};
5312#endif /* CONFIG_NFS_V4_1 */
5313
5314struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005315 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005316 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005317 .recover_open = nfs4_open_expired,
5318 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04005319 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005320 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005321};
5322
Andy Adamson591d71c2009-04-01 09:22:47 -04005323#if defined(CONFIG_NFS_V4_1)
5324struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
5325 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5326 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5327 .recover_open = nfs4_open_expired,
5328 .recover_lock = nfs4_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05005329 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005330 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04005331};
5332#endif /* CONFIG_NFS_V4_1 */
5333
Benny Halevy29fba382009-04-01 09:22:44 -04005334struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
5335 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04005336 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005337 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04005338};
5339
5340#if defined(CONFIG_NFS_V4_1)
5341struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
5342 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04005343 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005344 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04005345};
5346#endif
5347
5348/*
5349 * Per minor version reboot and network partition recovery ops
5350 */
5351
Andy Adamson591d71c2009-04-01 09:22:47 -04005352struct nfs4_state_recovery_ops *nfs4_reboot_recovery_ops[] = {
5353 &nfs40_reboot_recovery_ops,
5354#if defined(CONFIG_NFS_V4_1)
5355 &nfs41_reboot_recovery_ops,
5356#endif
5357};
5358
5359struct nfs4_state_recovery_ops *nfs4_nograce_recovery_ops[] = {
5360 &nfs40_nograce_recovery_ops,
5361#if defined(CONFIG_NFS_V4_1)
5362 &nfs41_nograce_recovery_ops,
5363#endif
5364};
5365
Benny Halevy29fba382009-04-01 09:22:44 -04005366struct nfs4_state_maintenance_ops *nfs4_state_renewal_ops[] = {
5367 &nfs40_state_renewal_ops,
5368#if defined(CONFIG_NFS_V4_1)
5369 &nfs41_state_renewal_ops,
5370#endif
5371};
5372
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08005373static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005374 .permission = nfs_permission,
5375 .getattr = nfs_getattr,
5376 .setattr = nfs_setattr,
5377 .getxattr = nfs4_getxattr,
5378 .setxattr = nfs4_setxattr,
5379 .listxattr = nfs4_listxattr,
5380};
5381
David Howells509de812006-08-22 20:06:11 -04005382const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005383 .version = 4, /* protocol version */
5384 .dentry_ops = &nfs4_dentry_operations,
5385 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005386 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387 .getroot = nfs4_proc_get_root,
5388 .getattr = nfs4_proc_getattr,
5389 .setattr = nfs4_proc_setattr,
David Howells2b3de442006-08-22 20:06:09 -04005390 .lookupfh = nfs4_proc_lookupfh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005391 .lookup = nfs4_proc_lookup,
5392 .access = nfs4_proc_access,
5393 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005394 .create = nfs4_proc_create,
5395 .remove = nfs4_proc_remove,
5396 .unlink_setup = nfs4_proc_unlink_setup,
5397 .unlink_done = nfs4_proc_unlink_done,
5398 .rename = nfs4_proc_rename,
5399 .link = nfs4_proc_link,
5400 .symlink = nfs4_proc_symlink,
5401 .mkdir = nfs4_proc_mkdir,
5402 .rmdir = nfs4_proc_remove,
5403 .readdir = nfs4_proc_readdir,
5404 .mknod = nfs4_proc_mknod,
5405 .statfs = nfs4_proc_statfs,
5406 .fsinfo = nfs4_proc_fsinfo,
5407 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04005408 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409 .decode_dirent = nfs4_decode_dirent,
5410 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05005411 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005413 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005415 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005417 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04005418 .close_context = nfs4_close_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419};
5420
5421/*
5422 * Local variables:
5423 * c-basic-offset: 8
5424 * End:
5425 */