blob: fc653c3bc5575e3eed995c1cf339b6bc1d92578d [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 Myklebust62ab4602009-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 Myklebust62ab4602009-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 Batsakis0851de02010-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 Batsakis0851de02010-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 Myklebust16b42892006-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 Myklebust16b42892006-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 Myklebust16b42892006-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 Myklebust16b42892006-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
Andy Adamson8328d592009-04-01 09:22:35 -04003460_nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs_client *clp, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 if (!clp || task->tk_status >= 0)
3463 return 0;
3464 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003465 case -NFS4ERR_ADMIN_REVOKED:
3466 case -NFS4ERR_BAD_STATEID:
3467 case -NFS4ERR_OPENMODE:
3468 if (state == NULL)
3469 break;
3470 nfs4_state_mark_reclaim_nograce(clp, state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003471 goto do_state_recovery;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003473 if (state == NULL)
3474 break;
3475 nfs4_state_mark_reclaim_reboot(clp, state);
3476 case -NFS4ERR_STALE_CLIENTID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 case -NFS4ERR_EXPIRED:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003478 goto do_state_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003479#if defined(CONFIG_NFS_V4_1)
3480 case -NFS4ERR_BADSESSION:
3481 case -NFS4ERR_BADSLOT:
3482 case -NFS4ERR_BAD_HIGH_SLOT:
3483 case -NFS4ERR_DEADSESSION:
3484 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3485 case -NFS4ERR_SEQ_FALSE_RETRY:
3486 case -NFS4ERR_SEQ_MISORDERED:
3487 dprintk("%s ERROR %d, Reset session\n", __func__,
3488 task->tk_status);
Andy Adamson0b9e2d42009-12-04 16:02:14 -05003489 nfs4_schedule_state_recovery(clp);
Andy Adamson4745e312009-04-01 09:22:42 -04003490 task->tk_status = 0;
3491 return -EAGAIN;
3492#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 case -NFS4ERR_DELAY:
Andy Adamson8328d592009-04-01 09:22:35 -04003494 if (server)
3495 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003496 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003497 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3499 task->tk_status = 0;
3500 return -EAGAIN;
3501 case -NFS4ERR_OLD_STATEID:
3502 task->tk_status = 0;
3503 return -EAGAIN;
3504 }
3505 task->tk_status = nfs4_map_errors(task->tk_status);
3506 return 0;
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003507do_state_recovery:
3508 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
3509 nfs4_schedule_state_recovery(clp);
3510 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3511 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3512 task->tk_status = 0;
3513 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514}
3515
Andy Adamson8328d592009-04-01 09:22:35 -04003516static int
3517nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3518{
3519 return _nfs4_async_handle_error(task, server, server->nfs_client, state);
3520}
3521
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003522int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3523 unsigned short port, struct rpc_cred *cred,
3524 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525{
3526 nfs4_verifier sc_verifier;
3527 struct nfs4_setclientid setclientid = {
3528 .sc_verifier = &sc_verifier,
3529 .sc_prog = program,
3530 };
3531 struct rpc_message msg = {
3532 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3533 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003534 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003535 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 };
Al Virobc4785c2006-10-19 23:28:51 -07003537 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 int loop = 0;
3539 int status;
3540
Al Virobc4785c2006-10-19 23:28:51 -07003541 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3543 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3544
3545 for(;;) {
3546 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003547 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003548 clp->cl_ipaddr,
3549 rpc_peeraddr2str(clp->cl_rpcclient,
3550 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003551 rpc_peeraddr2str(clp->cl_rpcclient,
3552 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003553 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 clp->cl_id_uniquifier);
3555 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003556 sizeof(setclientid.sc_netid),
3557 rpc_peeraddr2str(clp->cl_rpcclient,
3558 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003560 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 clp->cl_ipaddr, port >> 8, port & 255);
3562
3563 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3564 if (status != -NFS4ERR_CLID_INUSE)
3565 break;
3566 if (signalled())
3567 break;
3568 if (loop++ & 1)
3569 ssleep(clp->cl_lease_time + 1);
3570 else
3571 if (++clp->cl_id_uniquifier == 0)
3572 break;
3573 }
3574 return status;
3575}
3576
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003577static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3578 struct nfs4_setclientid_res *arg,
3579 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580{
3581 struct nfs_fsinfo fsinfo;
3582 struct rpc_message msg = {
3583 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003584 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003586 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587 };
3588 unsigned long now;
3589 int status;
3590
3591 now = jiffies;
3592 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3593 if (status == 0) {
3594 spin_lock(&clp->cl_lock);
3595 clp->cl_lease_time = fsinfo.lease_time * HZ;
3596 clp->cl_last_renewal = now;
3597 spin_unlock(&clp->cl_lock);
3598 }
3599 return status;
3600}
3601
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003602int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3603 struct nfs4_setclientid_res *arg,
3604 struct rpc_cred *cred)
Trond Myklebust51581f32006-03-20 13:44:47 -05003605{
Benny Halevy77e03672008-06-24 20:25:57 +03003606 long timeout = 0;
Trond Myklebust51581f32006-03-20 13:44:47 -05003607 int err;
3608 do {
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003609 err = _nfs4_proc_setclientid_confirm(clp, arg, cred);
Trond Myklebust51581f32006-03-20 13:44:47 -05003610 switch (err) {
3611 case 0:
3612 return err;
3613 case -NFS4ERR_RESOURCE:
3614 /* The IBM lawyers misread another document! */
3615 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -05003616 case -EKEYEXPIRED:
Trond Myklebust51581f32006-03-20 13:44:47 -05003617 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3618 }
3619 } while (err == 0);
3620 return err;
3621}
3622
Trond Myklebustfe650402006-01-03 09:55:18 +01003623struct nfs4_delegreturndata {
3624 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003625 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003626 struct nfs_fh fh;
3627 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003628 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003629 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003630 int rpc_status;
3631};
3632
Trond Myklebustfe650402006-01-03 09:55:18 +01003633static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3634{
3635 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003636
Trond Myklebustd61e6122009-12-05 19:32:19 -05003637 nfs4_sequence_done(data->res.server, &data->res.seq_res,
3638 task->tk_status);
Andy Adamson938e1012009-04-01 09:22:28 -04003639
Ricardo Labiaga79708862009-12-07 09:23:21 -05003640 switch (task->tk_status) {
3641 case -NFS4ERR_STALE_STATEID:
3642 case -NFS4ERR_EXPIRED:
3643 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01003644 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003645 break;
3646 default:
3647 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3648 -EAGAIN) {
3649 nfs_restart_rpc(task, data->res.server->nfs_client);
3650 return;
3651 }
3652 }
3653 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01003654}
3655
3656static void nfs4_delegreturn_release(void *calldata)
3657{
Trond Myklebustfe650402006-01-03 09:55:18 +01003658 kfree(calldata);
3659}
3660
Andy Adamson938e1012009-04-01 09:22:28 -04003661#if defined(CONFIG_NFS_V4_1)
3662static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3663{
3664 struct nfs4_delegreturndata *d_data;
3665
3666 d_data = (struct nfs4_delegreturndata *)data;
3667
3668 if (nfs4_setup_sequence(d_data->res.server->nfs_client,
3669 &d_data->args.seq_args,
3670 &d_data->res.seq_res, 1, task))
3671 return;
3672 rpc_call_start(task);
3673}
3674#endif /* CONFIG_NFS_V4_1 */
3675
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003676static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04003677#if defined(CONFIG_NFS_V4_1)
3678 .rpc_call_prepare = nfs4_delegreturn_prepare,
3679#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01003680 .rpc_call_done = nfs4_delegreturn_done,
3681 .rpc_release = nfs4_delegreturn_release,
3682};
3683
Trond Myklebuste6f81072008-01-24 18:14:34 -05003684static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003685{
3686 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003687 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003688 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003689 struct rpc_message msg = {
3690 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3691 .rpc_cred = cred,
3692 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003693 struct rpc_task_setup task_setup_data = {
3694 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003695 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003696 .callback_ops = &nfs4_delegreturn_ops,
3697 .flags = RPC_TASK_ASYNC,
3698 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003699 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003700
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003701 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01003702 if (data == NULL)
3703 return -ENOMEM;
3704 data->args.fhandle = &data->fh;
3705 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003706 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003707 nfs_copy_fh(&data->fh, NFS_FH(inode));
3708 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003709 data->res.fattr = &data->fattr;
3710 data->res.server = server;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003711 data->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003712 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003713 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003714 data->rpc_status = 0;
3715
Trond Myklebustc970aa82007-07-14 15:39:59 -04003716 task_setup_data.callback_data = data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003717 msg.rpc_argp = &data->args,
3718 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003719 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003720 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003721 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003722 if (!issync)
3723 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003724 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003725 if (status != 0)
3726 goto out;
3727 status = data->rpc_status;
3728 if (status != 0)
3729 goto out;
3730 nfs_refresh_inode(inode, &data->fattr);
3731out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003732 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003733 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734}
3735
Trond Myklebuste6f81072008-01-24 18:14:34 -05003736int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737{
3738 struct nfs_server *server = NFS_SERVER(inode);
3739 struct nfs4_exception exception = { };
3740 int err;
3741 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003742 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743 switch (err) {
3744 case -NFS4ERR_STALE_STATEID:
3745 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746 case 0:
3747 return 0;
3748 }
3749 err = nfs4_handle_exception(server, err, &exception);
3750 } while (exception.retry);
3751 return err;
3752}
3753
3754#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3755#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3756
3757/*
3758 * sleep, with exponential backoff, and retry the LOCK operation.
3759 */
3760static unsigned long
3761nfs4_set_lock_task_retry(unsigned long timeout)
3762{
Matthew Wilcox150030b2007-12-06 16:24:39 -05003763 schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764 timeout <<= 1;
3765 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3766 return NFS4_LOCK_MAXTIMEOUT;
3767 return timeout;
3768}
3769
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3771{
3772 struct inode *inode = state->inode;
3773 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003774 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003775 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003777 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003779 struct nfs_lockt_res res = {
3780 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781 };
3782 struct rpc_message msg = {
3783 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3784 .rpc_argp = &arg,
3785 .rpc_resp = &res,
3786 .rpc_cred = state->owner->so_cred,
3787 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788 struct nfs4_lock_state *lsp;
3789 int status;
3790
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003791 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003792 status = nfs4_set_lock_state(state, request);
3793 if (status != 0)
3794 goto out;
3795 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003796 arg.lock_owner.id = lsp->ls_id.id;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003797 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003798 switch (status) {
3799 case 0:
3800 request->fl_type = F_UNLCK;
3801 break;
3802 case -NFS4ERR_DENIED:
3803 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05003805 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003806out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 return status;
3808}
3809
3810static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3811{
3812 struct nfs4_exception exception = { };
3813 int err;
3814
3815 do {
3816 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3817 _nfs4_proc_getlk(state, cmd, request),
3818 &exception);
3819 } while (exception.retry);
3820 return err;
3821}
3822
3823static int do_vfs_lock(struct file *file, struct file_lock *fl)
3824{
3825 int res = 0;
3826 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3827 case FL_POSIX:
3828 res = posix_lock_file_wait(file, fl);
3829 break;
3830 case FL_FLOCK:
3831 res = flock_lock_file_wait(file, fl);
3832 break;
3833 default:
3834 BUG();
3835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836 return res;
3837}
3838
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003839struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003840 struct nfs_locku_args arg;
3841 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003842 struct nfs4_lock_state *lsp;
3843 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003844 struct file_lock fl;
3845 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003846 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003847};
3848
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003849static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3850 struct nfs_open_context *ctx,
3851 struct nfs4_lock_state *lsp,
3852 struct nfs_seqid *seqid)
3853{
3854 struct nfs4_unlockdata *p;
3855 struct inode *inode = lsp->ls_state->inode;
3856
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003857 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003858 if (p == NULL)
3859 return NULL;
3860 p->arg.fh = NFS_FH(inode);
3861 p->arg.fl = &p->fl;
3862 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003863 p->res.seqid = seqid;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003864 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003865 p->arg.stateid = &lsp->ls_stateid;
3866 p->lsp = lsp;
3867 atomic_inc(&lsp->ls_count);
3868 /* Ensure we don't close file until we're done freeing locks! */
3869 p->ctx = get_nfs_open_context(ctx);
3870 memcpy(&p->fl, fl, sizeof(p->fl));
3871 p->server = NFS_SERVER(inode);
3872 return p;
3873}
3874
Trond Myklebust06f814a2006-01-03 09:55:07 +01003875static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003876{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003877 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003878 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003879 nfs4_put_lock_state(calldata->lsp);
3880 put_nfs_open_context(calldata->ctx);
3881 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003882}
3883
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003884static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003885{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003886 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003887
Andy Adamsona8936932009-04-01 09:22:23 -04003888 nfs4_sequence_done(calldata->server, &calldata->res.seq_res,
3889 task->tk_status);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003890 if (RPC_ASSASSINATED(task))
3891 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003892 switch (task->tk_status) {
3893 case 0:
3894 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003895 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003896 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01003897 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003898 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003899 case -NFS4ERR_BAD_STATEID:
3900 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003901 case -NFS4ERR_STALE_STATEID:
3902 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003903 break;
3904 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003905 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebust0110ee12009-12-07 09:00:24 -05003906 nfs_restart_rpc(task,
Trond Myklebustd61e6122009-12-05 19:32:19 -05003907 calldata->server->nfs_client);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003908 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003909}
3910
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003911static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003912{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003913 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003915 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003916 return;
3917 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003918 /* Note: exit _without_ running nfs4_locku_done */
3919 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003920 return;
3921 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01003922 calldata->timestamp = jiffies;
Andy Adamsona8936932009-04-01 09:22:23 -04003923 if (nfs4_setup_sequence(calldata->server->nfs_client,
3924 &calldata->arg.seq_args,
3925 &calldata->res.seq_res, 1, task))
3926 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003927 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928}
3929
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003930static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003931 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003932 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01003933 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003934};
3935
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003936static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3937 struct nfs_open_context *ctx,
3938 struct nfs4_lock_state *lsp,
3939 struct nfs_seqid *seqid)
3940{
3941 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003942 struct rpc_message msg = {
3943 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3944 .rpc_cred = ctx->cred,
3945 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003946 struct rpc_task_setup task_setup_data = {
3947 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04003948 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003949 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003950 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003951 .flags = RPC_TASK_ASYNC,
3952 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003953
Frank Filz137d6ac2007-07-09 15:32:29 -07003954 /* Ensure this is an unlock - when canceling a lock, the
3955 * canceled lock is passed in, and it won't be an unlock.
3956 */
3957 fl->fl_type = F_UNLCK;
3958
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003959 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3960 if (data == NULL) {
3961 nfs_free_seqid(seqid);
3962 return ERR_PTR(-ENOMEM);
3963 }
3964
Trond Myklebust5138fde2007-07-14 15:40:01 -04003965 msg.rpc_argp = &data->arg,
3966 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003967 task_setup_data.callback_data = data;
3968 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003969}
3970
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3972{
Trond Myklebust19e03c52008-12-23 15:21:44 -05003973 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003974 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003975 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01003976 struct rpc_task *task;
3977 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003978 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979
Trond Myklebust9b073572006-06-29 16:38:34 -04003980 status = nfs4_set_lock_state(state, request);
3981 /* Unlock _before_ we do the RPC call */
3982 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003983 down_read(&nfsi->rwsem);
3984 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
3985 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003986 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003987 }
3988 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003989 if (status != 0)
3990 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003991 /* Is this a delegated lock? */
3992 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04003993 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003994 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003995 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04003996 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003997 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04003998 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04003999 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004000 status = PTR_ERR(task);
4001 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004002 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004003 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004004 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004005out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004006 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004007 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008}
4009
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004010struct nfs4_lockdata {
4011 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004012 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004013 struct nfs4_lock_state *lsp;
4014 struct nfs_open_context *ctx;
4015 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004016 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004017 int rpc_status;
4018 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004019 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004020};
4021
4022static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004023 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4024 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004025{
4026 struct nfs4_lockdata *p;
4027 struct inode *inode = lsp->ls_state->inode;
4028 struct nfs_server *server = NFS_SERVER(inode);
4029
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004030 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004031 if (p == NULL)
4032 return NULL;
4033
4034 p->arg.fh = NFS_FH(inode);
4035 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004036 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004037 if (p->arg.open_seqid == NULL)
4038 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004039 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004040 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004041 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004042 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004043 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04004044 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004045 p->res.lock_seqid = p->arg.lock_seqid;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04004046 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004047 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004048 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004049 atomic_inc(&lsp->ls_count);
4050 p->ctx = get_nfs_open_context(ctx);
4051 memcpy(&p->fl, fl, sizeof(p->fl));
4052 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004053out_free_seqid:
4054 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004055out_free:
4056 kfree(p);
4057 return NULL;
4058}
4059
4060static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4061{
4062 struct nfs4_lockdata *data = calldata;
4063 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064
Harvey Harrison3110ff82008-05-02 13:42:44 -07004065 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004066 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4067 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004068 /* Do we need to do an open_to_lock_owner? */
4069 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004070 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4071 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004072 data->arg.open_stateid = &state->stateid;
4073 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004074 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004075 } else
4076 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004077 data->timestamp = jiffies;
Andy Adamson66179ef2009-04-01 09:22:22 -04004078 if (nfs4_setup_sequence(data->server->nfs_client, &data->arg.seq_args,
4079 &data->res.seq_res, 1, task))
4080 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004081 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004082 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004083}
4084
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004085static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4086{
4087 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4088 nfs4_lock_prepare(task, calldata);
4089}
4090
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004091static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4092{
4093 struct nfs4_lockdata *data = calldata;
4094
Harvey Harrison3110ff82008-05-02 13:42:44 -07004095 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004096
Trond Myklebustd61e6122009-12-05 19:32:19 -05004097 nfs4_sequence_done(data->server, &data->res.seq_res,
4098 task->tk_status);
Andy Adamson66179ef2009-04-01 09:22:22 -04004099
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004100 data->rpc_status = task->tk_status;
4101 if (RPC_ASSASSINATED(task))
4102 goto out;
4103 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004104 if (data->rpc_status == 0)
4105 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4106 else
4107 goto out;
4108 }
4109 if (data->rpc_status == 0) {
4110 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4111 sizeof(data->lsp->ls_stateid.data));
4112 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Trond Myklebust88be9f92007-06-05 10:42:27 -04004113 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004114 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004115out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004116 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004117}
4118
4119static void nfs4_lock_release(void *calldata)
4120{
4121 struct nfs4_lockdata *data = calldata;
4122
Harvey Harrison3110ff82008-05-02 13:42:44 -07004123 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004124 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004125 if (data->cancelled != 0) {
4126 struct rpc_task *task;
4127 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4128 data->arg.lock_seqid);
4129 if (!IS_ERR(task))
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004130 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004131 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004132 } else
4133 nfs_free_seqid(data->arg.lock_seqid);
4134 nfs4_put_lock_state(data->lsp);
4135 put_nfs_open_context(data->ctx);
4136 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004137 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004138}
4139
4140static const struct rpc_call_ops nfs4_lock_ops = {
4141 .rpc_call_prepare = nfs4_lock_prepare,
4142 .rpc_call_done = nfs4_lock_done,
4143 .rpc_release = nfs4_lock_release,
4144};
4145
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004146static const struct rpc_call_ops nfs4_recover_lock_ops = {
4147 .rpc_call_prepare = nfs4_recover_lock_prepare,
4148 .rpc_call_done = nfs4_lock_done,
4149 .rpc_release = nfs4_lock_release,
4150};
4151
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004152static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4153{
4154 struct nfs_client *clp = server->nfs_client;
4155 struct nfs4_state *state = lsp->ls_state;
4156
4157 switch (error) {
4158 case -NFS4ERR_ADMIN_REVOKED:
4159 case -NFS4ERR_BAD_STATEID:
4160 case -NFS4ERR_EXPIRED:
4161 if (new_lock_owner != 0 ||
4162 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4163 nfs4_state_mark_reclaim_nograce(clp, state);
4164 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004165 break;
4166 case -NFS4ERR_STALE_STATEID:
4167 if (new_lock_owner != 0 ||
4168 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4169 nfs4_state_mark_reclaim_reboot(clp, state);
4170 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004171 };
4172}
4173
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004174static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004175{
4176 struct nfs4_lockdata *data;
4177 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004178 struct rpc_message msg = {
4179 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4180 .rpc_cred = state->owner->so_cred,
4181 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004182 struct rpc_task_setup task_setup_data = {
4183 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004184 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004185 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004186 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004187 .flags = RPC_TASK_ASYNC,
4188 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004189 int ret;
4190
Harvey Harrison3110ff82008-05-02 13:42:44 -07004191 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004192 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004193 fl->fl_u.nfs4_fl.owner,
4194 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004195 if (data == NULL)
4196 return -ENOMEM;
4197 if (IS_SETLKW(cmd))
4198 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004199 if (recovery_type > NFS_LOCK_NEW) {
4200 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004201 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004202 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4203 }
Trond Myklebust5138fde2007-07-14 15:40:01 -04004204 msg.rpc_argp = &data->arg,
4205 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004206 task_setup_data.callback_data = data;
4207 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004208 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004209 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004210 ret = nfs4_wait_for_completion_rpc_task(task);
4211 if (ret == 0) {
4212 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004213 if (ret)
4214 nfs4_handle_setlk_error(data->server, data->lsp,
4215 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004216 } else
4217 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004218 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004219 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004220 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221}
4222
4223static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4224{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004225 struct nfs_server *server = NFS_SERVER(state->inode);
4226 struct nfs4_exception exception = { };
4227 int err;
4228
4229 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004230 /* Cache the lock if possible... */
4231 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4232 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004233 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Jeff Layton2c643482010-01-07 09:42:03 -05004234 if (err != -NFS4ERR_DELAY && err != -EKEYEXPIRED)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004235 break;
4236 nfs4_handle_exception(server, err, &exception);
4237 } while (exception.retry);
4238 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239}
4240
4241static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4242{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004243 struct nfs_server *server = NFS_SERVER(state->inode);
4244 struct nfs4_exception exception = { };
4245 int err;
4246
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004247 err = nfs4_set_lock_state(state, request);
4248 if (err != 0)
4249 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004250 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004251 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4252 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004253 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004254 switch (err) {
4255 default:
4256 goto out;
4257 case -NFS4ERR_GRACE:
4258 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -05004259 case -EKEYEXPIRED:
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004260 nfs4_handle_exception(server, err, &exception);
4261 err = 0;
4262 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004263 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004264out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004265 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266}
4267
4268static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4269{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004270 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004271 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004272 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004274 if ((fl_flags & FL_POSIX) &&
4275 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4276 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004277 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004278 status = nfs4_set_lock_state(state, request);
4279 if (status != 0)
4280 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004281 request->fl_flags |= FL_ACCESS;
4282 status = do_vfs_lock(request->fl_file, request);
4283 if (status < 0)
4284 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004285 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004286 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004287 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004288 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004289 request->fl_flags = fl_flags & ~FL_SLEEP;
4290 status = do_vfs_lock(request->fl_file, request);
4291 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004292 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004293 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004294 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004295 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004296 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004297 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004298 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07004299 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004300out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004301 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004302out:
4303 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304 return status;
4305}
4306
4307static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4308{
4309 struct nfs4_exception exception = { };
4310 int err;
4311
4312 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004313 err = _nfs4_proc_setlk(state, cmd, request);
4314 if (err == -NFS4ERR_DENIED)
4315 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004317 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318 } while (exception.retry);
4319 return err;
4320}
4321
4322static int
4323nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4324{
4325 struct nfs_open_context *ctx;
4326 struct nfs4_state *state;
4327 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4328 int status;
4329
4330 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004331 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332 state = ctx->state;
4333
4334 if (request->fl_start < 0 || request->fl_end < 0)
4335 return -EINVAL;
4336
Trond Myklebustd9531262009-07-21 19:22:38 -04004337 if (IS_GETLK(cmd)) {
4338 if (state != NULL)
4339 return nfs4_proc_getlk(state, F_GETLK, request);
4340 return 0;
4341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342
4343 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4344 return -EINVAL;
4345
Trond Myklebustd9531262009-07-21 19:22:38 -04004346 if (request->fl_type == F_UNLCK) {
4347 if (state != NULL)
4348 return nfs4_proc_unlck(state, cmd, request);
4349 return 0;
4350 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351
Trond Myklebustd9531262009-07-21 19:22:38 -04004352 if (state == NULL)
4353 return -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354 do {
4355 status = nfs4_proc_setlk(state, cmd, request);
4356 if ((status != -EAGAIN) || IS_SETLK(cmd))
4357 break;
4358 timeout = nfs4_set_lock_task_retry(timeout);
4359 status = -ERESTARTSYS;
4360 if (signalled())
4361 break;
4362 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 return status;
4364}
4365
Trond Myklebust888e6942005-11-04 15:38:11 -05004366int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4367{
4368 struct nfs_server *server = NFS_SERVER(state->inode);
4369 struct nfs4_exception exception = { };
4370 int err;
4371
4372 err = nfs4_set_lock_state(state, fl);
4373 if (err != 0)
4374 goto out;
4375 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004376 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004377 switch (err) {
4378 default:
4379 printk(KERN_ERR "%s: unhandled error %d.\n",
4380 __func__, err);
4381 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004382 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004383 goto out;
4384 case -NFS4ERR_EXPIRED:
4385 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004386 case -NFS4ERR_STALE_STATEID:
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004387 case -NFS4ERR_BADSESSION:
4388 case -NFS4ERR_BADSLOT:
4389 case -NFS4ERR_BAD_HIGH_SLOT:
4390 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4391 case -NFS4ERR_DEADSESSION:
Trond Myklebustd5122202009-06-17 13:22:58 -07004392 nfs4_schedule_state_recovery(server->nfs_client);
4393 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004394 case -ERESTARTSYS:
4395 /*
4396 * The show must go on: exit, but mark the
4397 * stateid as needing recovery.
4398 */
4399 case -NFS4ERR_ADMIN_REVOKED:
4400 case -NFS4ERR_BAD_STATEID:
4401 case -NFS4ERR_OPENMODE:
4402 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
4403 err = 0;
4404 goto out;
4405 case -ENOMEM:
4406 case -NFS4ERR_DENIED:
4407 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4408 err = 0;
4409 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004410 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -05004411 case -EKEYEXPIRED:
Trond Myklebustd5122202009-06-17 13:22:58 -07004412 break;
4413 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004414 err = nfs4_handle_exception(server, err, &exception);
4415 } while (exception.retry);
4416out:
4417 return err;
4418}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004419
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004420#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4421
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004422int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
4423 size_t buflen, int flags)
4424{
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004425 struct inode *inode = dentry->d_inode;
4426
4427 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4428 return -EOPNOTSUPP;
4429
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004430 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004431}
4432
4433/* The getxattr man page suggests returning -ENODATA for unknown attributes,
4434 * and that's what we'll do for e.g. user attributes that haven't been set.
4435 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
4436 * attributes in kernel-managed attribute namespaces. */
4437ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
4438 size_t buflen)
4439{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004440 struct inode *inode = dentry->d_inode;
4441
4442 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4443 return -EOPNOTSUPP;
4444
4445 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004446}
4447
4448ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
4449{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004450 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004451
J. Bruce Fields096455a2006-03-20 23:23:42 -05004452 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4453 return 0;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004454 if (buf && buflen < len)
4455 return -ERANGE;
4456 if (buf)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004457 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
4458 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004459}
4460
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004461static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4462{
4463 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
4464 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4465 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4466 return;
4467
4468 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4469 NFS_ATTR_FATTR_NLINK;
4470 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4471 fattr->nlink = 2;
4472}
4473
Trond Myklebust56659e92007-07-17 21:52:39 -04004474int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004475 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004476{
4477 struct nfs_server *server = NFS_SERVER(dir);
4478 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004479 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4480 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004481 };
4482 struct nfs4_fs_locations_arg args = {
4483 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004484 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004485 .page = page,
4486 .bitmask = bitmask,
4487 };
Benny Halevy22958462009-04-01 09:22:02 -04004488 struct nfs4_fs_locations_res res = {
4489 .fs_locations = fs_locations,
4490 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004491 struct rpc_message msg = {
4492 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4493 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004494 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004495 };
4496 int status;
4497
Harvey Harrison3110ff82008-05-02 13:42:44 -07004498 dprintk("%s: start\n", __func__);
Trond Myklebustc228fd32007-01-13 02:28:11 -05004499 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004500 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004501 fs_locations->nlocations = 0;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04004502 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004503 nfs_fixup_referral_attributes(&fs_locations->fattr);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004504 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004505 return status;
4506}
4507
Andy Adamson557134a2009-04-01 09:21:53 -04004508#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04004509/*
4510 * nfs4_proc_exchange_id()
4511 *
4512 * Since the clientid has expired, all compounds using sessions
4513 * associated with the stale clientid will be returning
4514 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4515 * be in some phase of session reset.
4516 */
Andy Adamson4d643d12009-12-04 15:52:24 -05004517int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004518{
4519 nfs4_verifier verifier;
4520 struct nfs41_exchange_id_args args = {
4521 .client = clp,
4522 .flags = clp->cl_exchange_flags,
4523 };
4524 struct nfs41_exchange_id_res res = {
4525 .client = clp,
4526 };
4527 int status;
4528 struct rpc_message msg = {
4529 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4530 .rpc_argp = &args,
4531 .rpc_resp = &res,
4532 .rpc_cred = cred,
4533 };
4534 __be32 *p;
4535
4536 dprintk("--> %s\n", __func__);
4537 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04004538
Alexandros Batsakis7b183d02009-12-05 13:33:25 -05004539 /* Remove server-only flags */
4540 args.flags &= ~EXCHGID4_FLAG_CONFIRMED_R;
4541
Benny Halevy99fe60d2009-04-01 09:22:29 -04004542 p = (u32 *)verifier.data;
4543 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4544 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4545 args.verifier = &verifier;
4546
4547 while (1) {
4548 args.id_len = scnprintf(args.id, sizeof(args.id),
4549 "%s/%s %u",
4550 clp->cl_ipaddr,
4551 rpc_peeraddr2str(clp->cl_rpcclient,
4552 RPC_DISPLAY_ADDR),
4553 clp->cl_id_uniquifier);
4554
4555 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
4556
Andy Adamson180b62a2010-03-02 13:19:36 -05004557 if (status != -NFS4ERR_CLID_INUSE)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004558 break;
4559
4560 if (signalled())
4561 break;
4562
4563 if (++clp->cl_id_uniquifier == 0)
4564 break;
4565 }
4566
4567 dprintk("<-- %s status= %d\n", __func__, status);
4568 return status;
4569}
4570
Andy Adamson2050f0c2009-04-01 09:22:30 -04004571struct nfs4_get_lease_time_data {
4572 struct nfs4_get_lease_time_args *args;
4573 struct nfs4_get_lease_time_res *res;
4574 struct nfs_client *clp;
4575};
4576
4577static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4578 void *calldata)
4579{
4580 int ret;
4581 struct nfs4_get_lease_time_data *data =
4582 (struct nfs4_get_lease_time_data *)calldata;
4583
4584 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004585 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004586 /* just setup sequence, do not trigger session recovery
4587 since we're invoked within one */
4588 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004589 &data->args->la_seq_args,
4590 &data->res->lr_seq_res, 0, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004591
4592 BUG_ON(ret == -EAGAIN);
4593 rpc_call_start(task);
4594 dprintk("<-- %s\n", __func__);
4595}
4596
4597/*
4598 * Called from nfs4_state_manager thread for session setup, so don't recover
4599 * from sequence operation or clientid errors.
4600 */
4601static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4602{
4603 struct nfs4_get_lease_time_data *data =
4604 (struct nfs4_get_lease_time_data *)calldata;
4605
4606 dprintk("--> %s\n", __func__);
Trond Myklebustd185a332010-06-16 09:52:25 -04004607 nfs41_sequence_done(&data->res->lr_seq_res);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004608 switch (task->tk_status) {
4609 case -NFS4ERR_DELAY:
4610 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05004611 case -EKEYEXPIRED:
Andy Adamson2050f0c2009-04-01 09:22:30 -04004612 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4613 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4614 task->tk_status = 0;
Trond Myklebust0110ee12009-12-07 09:00:24 -05004615 nfs_restart_rpc(task, data->clp);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004616 return;
4617 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04004618 dprintk("<-- %s\n", __func__);
4619}
4620
4621struct rpc_call_ops nfs4_get_lease_time_ops = {
4622 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4623 .rpc_call_done = nfs4_get_lease_time_done,
4624};
4625
4626int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4627{
4628 struct rpc_task *task;
4629 struct nfs4_get_lease_time_args args;
4630 struct nfs4_get_lease_time_res res = {
4631 .lr_fsinfo = fsinfo,
4632 };
4633 struct nfs4_get_lease_time_data data = {
4634 .args = &args,
4635 .res = &res,
4636 .clp = clp,
4637 };
4638 struct rpc_message msg = {
4639 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4640 .rpc_argp = &args,
4641 .rpc_resp = &res,
4642 };
4643 struct rpc_task_setup task_setup = {
4644 .rpc_client = clp->cl_rpcclient,
4645 .rpc_message = &msg,
4646 .callback_ops = &nfs4_get_lease_time_ops,
4647 .callback_data = &data
4648 };
4649 int status;
4650
4651 res.lr_seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
4652 dprintk("--> %s\n", __func__);
4653 task = rpc_run_task(&task_setup);
4654
4655 if (IS_ERR(task))
4656 status = PTR_ERR(task);
4657 else {
4658 status = task->tk_status;
4659 rpc_put_task(task);
4660 }
4661 dprintk("<-- %s return %d\n", __func__, status);
4662
4663 return status;
4664}
4665
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004666/*
4667 * Reset a slot table
4668 */
Andy Adamson104aeba2010-01-14 17:45:10 -05004669static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
4670 int ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004671{
Andy Adamson104aeba2010-01-14 17:45:10 -05004672 struct nfs4_slot *new = NULL;
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004673 int i;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004674 int ret = 0;
4675
Andy Adamson104aeba2010-01-14 17:45:10 -05004676 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
4677 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004678
Andy Adamson104aeba2010-01-14 17:45:10 -05004679 /* Does the newly negotiated max_reqs match the existing slot table? */
4680 if (max_reqs != tbl->max_slots) {
4681 ret = -ENOMEM;
4682 new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004683 GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05004684 if (!new)
4685 goto out;
4686 ret = 0;
4687 kfree(tbl->slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004688 }
4689 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson104aeba2010-01-14 17:45:10 -05004690 if (new) {
4691 tbl->slots = new;
4692 tbl->max_slots = max_reqs;
4693 }
4694 for (i = 0; i < tbl->max_slots; ++i)
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004695 tbl->slots[i].seq_nr = ivalue;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004696 spin_unlock(&tbl->slot_tbl_lock);
4697 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4698 tbl, tbl->slots, tbl->max_slots);
4699out:
4700 dprintk("<-- %s: return %d\n", __func__, ret);
4701 return ret;
4702}
4703
Andy Adamsonfc931582009-04-01 09:22:31 -04004704/*
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004705 * Reset the forechannel and backchannel slot tables
4706 */
4707static int nfs4_reset_slot_tables(struct nfs4_session *session)
4708{
4709 int status;
4710
4711 status = nfs4_reset_slot_table(&session->fc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05004712 session->fc_attrs.max_reqs, 1);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004713 if (status)
4714 return status;
4715
4716 status = nfs4_reset_slot_table(&session->bc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05004717 session->bc_attrs.max_reqs, 0);
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004718 return status;
4719}
4720
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004721/* Destroy the slot table */
4722static void nfs4_destroy_slot_tables(struct nfs4_session *session)
4723{
4724 if (session->fc_slot_table.slots != NULL) {
4725 kfree(session->fc_slot_table.slots);
4726 session->fc_slot_table.slots = NULL;
4727 }
4728 if (session->bc_slot_table.slots != NULL) {
4729 kfree(session->bc_slot_table.slots);
4730 session->bc_slot_table.slots = NULL;
4731 }
4732 return;
4733}
4734
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004735/*
Andy Adamsonfc931582009-04-01 09:22:31 -04004736 * Initialize slot table
4737 */
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004738static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
4739 int max_slots, int ivalue)
Andy Adamsonfc931582009-04-01 09:22:31 -04004740{
Andy Adamsonfc931582009-04-01 09:22:31 -04004741 struct nfs4_slot *slot;
4742 int ret = -ENOMEM;
4743
4744 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
4745
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004746 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
Andy Adamsonfc931582009-04-01 09:22:31 -04004747
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004748 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_NOFS);
Andy Adamsonfc931582009-04-01 09:22:31 -04004749 if (!slot)
4750 goto out;
Andy Adamsonfc931582009-04-01 09:22:31 -04004751 ret = 0;
4752
4753 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonfc931582009-04-01 09:22:31 -04004754 tbl->max_slots = max_slots;
4755 tbl->slots = slot;
4756 tbl->highest_used_slotid = -1; /* no slot is currently used */
4757 spin_unlock(&tbl->slot_tbl_lock);
4758 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4759 tbl, tbl->slots, tbl->max_slots);
4760out:
4761 dprintk("<-- %s: return %d\n", __func__, ret);
4762 return ret;
Andy Adamsonfc931582009-04-01 09:22:31 -04004763}
4764
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004765/*
4766 * Initialize the forechannel and backchannel tables
4767 */
4768static int nfs4_init_slot_tables(struct nfs4_session *session)
4769{
Trond Myklebustf26468f2009-12-05 19:32:11 -05004770 struct nfs4_slot_table *tbl;
4771 int status = 0;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004772
Trond Myklebustf26468f2009-12-05 19:32:11 -05004773 tbl = &session->fc_slot_table;
4774 if (tbl->slots == NULL) {
4775 status = nfs4_init_slot_table(tbl,
4776 session->fc_attrs.max_reqs, 1);
4777 if (status)
4778 return status;
4779 }
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004780
Trond Myklebustf26468f2009-12-05 19:32:11 -05004781 tbl = &session->bc_slot_table;
4782 if (tbl->slots == NULL) {
4783 status = nfs4_init_slot_table(tbl,
4784 session->bc_attrs.max_reqs, 0);
4785 if (status)
4786 nfs4_destroy_slot_tables(session);
4787 }
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004788
4789 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04004790}
4791
4792struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
4793{
4794 struct nfs4_session *session;
4795 struct nfs4_slot_table *tbl;
4796
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004797 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04004798 if (!session)
4799 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04004800
Andy Adamson76db6d92009-04-01 09:22:38 -04004801 /*
4802 * The create session reply races with the server back
4803 * channel probe. Mark the client NFS_CS_SESSION_INITING
4804 * so that the client back channel can find the
4805 * nfs_client struct
4806 */
4807 clp->cl_cons_state = NFS_CS_SESSION_INITING;
Andy Adamsonea028ac2009-12-04 15:55:38 -05004808 init_completion(&session->complete);
Andy Adamson76db6d92009-04-01 09:22:38 -04004809
Andy Adamson557134a2009-04-01 09:21:53 -04004810 tbl = &session->fc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05004811 tbl->highest_used_slotid = -1;
Andy Adamson557134a2009-04-01 09:21:53 -04004812 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004813 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004814
4815 tbl = &session->bc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05004816 tbl->highest_used_slotid = -1;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004817 spin_lock_init(&tbl->slot_tbl_lock);
4818 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
4819
Andy Adamson557134a2009-04-01 09:21:53 -04004820 session->clp = clp;
4821 return session;
4822}
4823
4824void nfs4_destroy_session(struct nfs4_session *session)
4825{
Andy Adamson5a0ffe52009-04-01 09:23:18 -04004826 nfs4_proc_destroy_session(session);
4827 dprintk("%s Destroy backchannel for xprt %p\n",
4828 __func__, session->clp->cl_rpcclient->cl_xprt);
4829 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
4830 NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004831 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04004832 kfree(session);
4833}
4834
Andy Adamsonfc931582009-04-01 09:22:31 -04004835/*
4836 * Initialize the values to be used by the client in CREATE_SESSION
4837 * If nfs4_init_session set the fore channel request and response sizes,
4838 * use them.
4839 *
4840 * Set the back channel max_resp_sz_cached to zero to force the client to
4841 * always set csa_cachethis to FALSE because the current implementation
4842 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
4843 */
4844static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
4845{
4846 struct nfs4_session *session = args->client->cl_session;
4847 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
4848 mxresp_sz = session->fc_attrs.max_resp_sz;
4849
4850 if (mxrqst_sz == 0)
4851 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
4852 if (mxresp_sz == 0)
4853 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
4854 /* Fore channel attributes */
4855 args->fc_attrs.headerpadsz = 0;
4856 args->fc_attrs.max_rqst_sz = mxrqst_sz;
4857 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04004858 args->fc_attrs.max_ops = NFS4_MAX_OPS;
4859 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
4860
4861 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05004862 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04004863 __func__,
4864 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05004865 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04004866
4867 /* Back channel attributes */
4868 args->bc_attrs.headerpadsz = 0;
4869 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
4870 args->bc_attrs.max_resp_sz = PAGE_SIZE;
4871 args->bc_attrs.max_resp_sz_cached = 0;
4872 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
4873 args->bc_attrs.max_reqs = 1;
4874
4875 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
4876 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4877 __func__,
4878 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
4879 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
4880 args->bc_attrs.max_reqs);
4881}
4882
Andy Adamson8d353012009-04-01 09:22:32 -04004883static int _verify_channel_attr(char *chan, char *attr_name, u32 sent, u32 rcvd)
4884{
4885 if (rcvd <= sent)
4886 return 0;
4887 printk(KERN_WARNING "%s: Session INVALID: %s channel %s increased. "
4888 "sent=%u rcvd=%u\n", __func__, chan, attr_name, sent, rcvd);
4889 return -EINVAL;
4890}
4891
4892#define _verify_fore_channel_attr(_name_) \
4893 _verify_channel_attr("fore", #_name_, \
4894 args->fc_attrs._name_, \
4895 session->fc_attrs._name_)
4896
4897#define _verify_back_channel_attr(_name_) \
4898 _verify_channel_attr("back", #_name_, \
4899 args->bc_attrs._name_, \
4900 session->bc_attrs._name_)
4901
4902/*
4903 * The server is not allowed to increase the fore channel header pad size,
4904 * maximum response size, or maximum number of operations.
4905 *
4906 * The back channel attributes are only negotiatied down: We send what the
4907 * (back channel) server insists upon.
4908 */
4909static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
4910 struct nfs4_session *session)
4911{
4912 int ret = 0;
4913
4914 ret |= _verify_fore_channel_attr(headerpadsz);
4915 ret |= _verify_fore_channel_attr(max_resp_sz);
4916 ret |= _verify_fore_channel_attr(max_ops);
4917
4918 ret |= _verify_back_channel_attr(headerpadsz);
4919 ret |= _verify_back_channel_attr(max_rqst_sz);
4920 ret |= _verify_back_channel_attr(max_resp_sz);
4921 ret |= _verify_back_channel_attr(max_resp_sz_cached);
4922 ret |= _verify_back_channel_attr(max_ops);
4923 ret |= _verify_back_channel_attr(max_reqs);
4924
4925 return ret;
4926}
4927
Andy Adamsonfc931582009-04-01 09:22:31 -04004928static int _nfs4_proc_create_session(struct nfs_client *clp)
4929{
4930 struct nfs4_session *session = clp->cl_session;
4931 struct nfs41_create_session_args args = {
4932 .client = clp,
4933 .cb_program = NFS4_CALLBACK,
4934 };
4935 struct nfs41_create_session_res res = {
4936 .client = clp,
4937 };
4938 struct rpc_message msg = {
4939 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
4940 .rpc_argp = &args,
4941 .rpc_resp = &res,
4942 };
4943 int status;
4944
4945 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04004946 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04004947
4948 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4949
Andy Adamson8d353012009-04-01 09:22:32 -04004950 if (!status)
4951 /* Verify the session's negotiated channel_attrs values */
4952 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04004953 if (!status) {
4954 /* Increment the clientid slot sequence id */
4955 clp->cl_seqid++;
4956 }
4957
4958 return status;
4959}
4960
4961/*
4962 * Issues a CREATE_SESSION operation to the server.
4963 * It is the responsibility of the caller to verify the session is
4964 * expired before calling this routine.
4965 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05004966int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04004967{
4968 int status;
4969 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04004970 struct nfs4_session *session = clp->cl_session;
4971
4972 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
4973
4974 status = _nfs4_proc_create_session(clp);
4975 if (status)
4976 goto out;
4977
Trond Myklebustf26468f2009-12-05 19:32:11 -05004978 /* Init and reset the fore channel */
4979 status = nfs4_init_slot_tables(session);
4980 dprintk("slot table initialization returned %d\n", status);
4981 if (status)
4982 goto out;
4983 status = nfs4_reset_slot_tables(session);
4984 dprintk("slot table reset returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04004985 if (status)
4986 goto out;
4987
4988 ptr = (unsigned *)&session->sess_id.data[0];
4989 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
4990 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04004991out:
4992 dprintk("<-- %s\n", __func__);
4993 return status;
4994}
4995
Andy Adamson0f3e66c2009-04-01 09:22:34 -04004996/*
4997 * Issue the over-the-wire RPC DESTROY_SESSION.
4998 * The caller must serialize access to this routine.
4999 */
5000int nfs4_proc_destroy_session(struct nfs4_session *session)
5001{
5002 int status = 0;
5003 struct rpc_message msg;
5004
5005 dprintk("--> nfs4_proc_destroy_session\n");
5006
5007 /* session is still being setup */
5008 if (session->clp->cl_cons_state != NFS_CS_READY)
5009 return status;
5010
5011 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5012 msg.rpc_argp = session;
5013 msg.rpc_resp = NULL;
5014 msg.rpc_cred = NULL;
5015 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
5016
5017 if (status)
5018 printk(KERN_WARNING
5019 "Got error %d from the server on DESTROY_SESSION. "
5020 "Session has been destroyed regardless...\n", status);
5021
5022 dprintk("<-- nfs4_proc_destroy_session\n");
5023 return status;
5024}
5025
Trond Myklebustfccba802009-07-21 16:48:07 -04005026int nfs4_init_session(struct nfs_server *server)
5027{
5028 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005029 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005030 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005031 int ret;
5032
5033 if (!nfs4_has_session(clp))
5034 return 0;
5035
Andy Adamson68bf05e2009-12-15 12:55:02 -05005036 rsize = server->rsize;
5037 if (rsize == 0)
5038 rsize = NFS_MAX_FILE_IO_SIZE;
5039 wsize = server->wsize;
5040 if (wsize == 0)
5041 wsize = NFS_MAX_FILE_IO_SIZE;
5042
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005043 session = clp->cl_session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005044 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5045 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005046
Trond Myklebustfccba802009-07-21 16:48:07 -04005047 ret = nfs4_recover_expired_lease(server);
5048 if (!ret)
5049 ret = nfs4_check_client_ready(clp);
5050 return ret;
5051}
5052
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005053/*
5054 * Renew the cl_session lease.
5055 */
5056static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5057{
5058 struct nfs4_sequence_args args;
5059 struct nfs4_sequence_res res;
5060
5061 struct rpc_message msg = {
5062 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5063 .rpc_argp = &args,
5064 .rpc_resp = &res,
5065 .rpc_cred = cred,
5066 };
5067
5068 args.sa_cache_this = 0;
5069
5070 return nfs4_call_sync_sequence(clp, clp->cl_rpcclient, &msg, &args,
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005071 &res, args.sa_cache_this, 1);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005072}
5073
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005074struct nfs4_sequence_data {
5075 struct nfs_client *clp;
5076 struct nfs4_sequence_args args;
5077 struct nfs4_sequence_res res;
5078};
5079
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005080static void nfs41_sequence_release(void *data)
5081{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005082 struct nfs4_sequence_data *calldata = data;
5083 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005084
Alexandros Batsakis71358402010-02-05 03:45:05 -08005085 if (atomic_read(&clp->cl_count) > 1)
5086 nfs4_schedule_state_renewal(clp);
5087 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005088 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005089}
5090
5091static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005092{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005093 struct nfs4_sequence_data *calldata = data;
5094 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005095
Trond Myklebustd185a332010-06-16 09:52:25 -04005096 nfs41_sequence_done(task->tk_msg.rpc_resp);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005097
5098 if (task->tk_status < 0) {
5099 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005100 if (atomic_read(&clp->cl_count) == 1)
5101 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005102
5103 if (_nfs4_async_handle_error(task, NULL, clp, NULL)
5104 == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05005105 nfs_restart_rpc(task, clp);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005106 return;
5107 }
5108 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005109 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005110out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005111 dprintk("<-- %s\n", __func__);
5112}
5113
5114static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5115{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005116 struct nfs4_sequence_data *calldata = data;
5117 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005118 struct nfs4_sequence_args *args;
5119 struct nfs4_sequence_res *res;
5120
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005121 args = task->tk_msg.rpc_argp;
5122 res = task->tk_msg.rpc_resp;
5123
5124 if (nfs4_setup_sequence(clp, args, res, 0, task))
5125 return;
5126 rpc_call_start(task);
5127}
5128
5129static const struct rpc_call_ops nfs41_sequence_ops = {
5130 .rpc_call_done = nfs41_sequence_call_done,
5131 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005132 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005133};
5134
5135static int nfs41_proc_async_sequence(struct nfs_client *clp,
5136 struct rpc_cred *cred)
5137{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005138 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005139 struct rpc_message msg = {
5140 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5141 .rpc_cred = cred,
5142 };
5143
Alexandros Batsakis71358402010-02-05 03:45:05 -08005144 if (!atomic_inc_not_zero(&clp->cl_count))
5145 return -EIO;
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005146 calldata = kmalloc(sizeof(*calldata), GFP_NOFS);
5147 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005148 nfs_put_client(clp);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005149 return -ENOMEM;
5150 }
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005151 calldata->res.sr_slotid = NFS4_MAX_SLOT_TABLE;
5152 msg.rpc_argp = &calldata->args;
5153 msg.rpc_resp = &calldata->res;
5154 calldata->clp = clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005155
5156 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005157 &nfs41_sequence_ops, calldata);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005158}
5159
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005160struct nfs4_reclaim_complete_data {
5161 struct nfs_client *clp;
5162 struct nfs41_reclaim_complete_args arg;
5163 struct nfs41_reclaim_complete_res res;
5164};
5165
5166static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5167{
5168 struct nfs4_reclaim_complete_data *calldata = data;
5169
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005170 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005171 if (nfs4_setup_sequence(calldata->clp, &calldata->arg.seq_args,
5172 &calldata->res.seq_res, 0, task))
5173 return;
5174
5175 rpc_call_start(task);
5176}
5177
5178static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5179{
5180 struct nfs4_reclaim_complete_data *calldata = data;
5181 struct nfs_client *clp = calldata->clp;
5182 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5183
5184 dprintk("--> %s\n", __func__);
Trond Myklebustd185a332010-06-16 09:52:25 -04005185 nfs41_sequence_done(res);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005186 switch (task->tk_status) {
5187 case 0:
5188 case -NFS4ERR_COMPLETE_ALREADY:
5189 break;
5190 case -NFS4ERR_BADSESSION:
5191 case -NFS4ERR_DEADSESSION:
5192 /*
5193 * Handle the session error, but do not retry the operation, as
5194 * we have no way of telling whether the clientid had to be
5195 * reset before we got our reply. If reset, a new wave of
5196 * reclaim operations will follow, containing their own reclaim
5197 * complete. We don't want our retry to get on the way of
5198 * recovery by incorrectly indicating to the server that we're
5199 * done reclaiming state since the process had to be restarted.
5200 */
5201 _nfs4_async_handle_error(task, NULL, clp, NULL);
5202 break;
5203 default:
5204 if (_nfs4_async_handle_error(
5205 task, NULL, clp, NULL) == -EAGAIN) {
5206 rpc_restart_call_prepare(task);
5207 return;
5208 }
5209 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005210
5211 dprintk("<-- %s\n", __func__);
5212}
5213
5214static void nfs4_free_reclaim_complete_data(void *data)
5215{
5216 struct nfs4_reclaim_complete_data *calldata = data;
5217
5218 kfree(calldata);
5219}
5220
5221static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5222 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5223 .rpc_call_done = nfs4_reclaim_complete_done,
5224 .rpc_release = nfs4_free_reclaim_complete_data,
5225};
5226
5227/*
5228 * Issue a global reclaim complete.
5229 */
5230static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5231{
5232 struct nfs4_reclaim_complete_data *calldata;
5233 struct rpc_task *task;
5234 struct rpc_message msg = {
5235 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5236 };
5237 struct rpc_task_setup task_setup_data = {
5238 .rpc_client = clp->cl_rpcclient,
5239 .rpc_message = &msg,
5240 .callback_ops = &nfs4_reclaim_complete_call_ops,
5241 .flags = RPC_TASK_ASYNC,
5242 };
5243 int status = -ENOMEM;
5244
5245 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005246 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005247 if (calldata == NULL)
5248 goto out;
5249 calldata->clp = clp;
5250 calldata->arg.one_fs = 0;
5251 calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
5252
5253 msg.rpc_argp = &calldata->arg;
5254 msg.rpc_resp = &calldata->res;
5255 task_setup_data.callback_data = calldata;
5256 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005257 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005258 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005259 goto out;
5260 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005261 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005262 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005263out:
5264 dprintk("<-- %s status=%d\n", __func__, status);
5265 return status;
5266}
Andy Adamson557134a2009-04-01 09:21:53 -04005267#endif /* CONFIG_NFS_V4_1 */
5268
Andy Adamson591d71c2009-04-01 09:22:47 -04005269struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005270 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005271 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005272 .recover_open = nfs4_open_reclaim,
5273 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04005274 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005275 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005276};
5277
Andy Adamson591d71c2009-04-01 09:22:47 -04005278#if defined(CONFIG_NFS_V4_1)
5279struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
5280 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5281 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5282 .recover_open = nfs4_open_reclaim,
5283 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05005284 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005285 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005286 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04005287};
5288#endif /* CONFIG_NFS_V4_1 */
5289
5290struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005291 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005292 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005293 .recover_open = nfs4_open_expired,
5294 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04005295 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005296 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297};
5298
Andy Adamson591d71c2009-04-01 09:22:47 -04005299#if defined(CONFIG_NFS_V4_1)
5300struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
5301 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5302 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5303 .recover_open = nfs4_open_expired,
5304 .recover_lock = nfs4_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05005305 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005306 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04005307};
5308#endif /* CONFIG_NFS_V4_1 */
5309
Benny Halevy29fba382009-04-01 09:22:44 -04005310struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
5311 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04005312 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005313 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04005314};
5315
5316#if defined(CONFIG_NFS_V4_1)
5317struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
5318 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04005319 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005320 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04005321};
5322#endif
5323
5324/*
5325 * Per minor version reboot and network partition recovery ops
5326 */
5327
Andy Adamson591d71c2009-04-01 09:22:47 -04005328struct nfs4_state_recovery_ops *nfs4_reboot_recovery_ops[] = {
5329 &nfs40_reboot_recovery_ops,
5330#if defined(CONFIG_NFS_V4_1)
5331 &nfs41_reboot_recovery_ops,
5332#endif
5333};
5334
5335struct nfs4_state_recovery_ops *nfs4_nograce_recovery_ops[] = {
5336 &nfs40_nograce_recovery_ops,
5337#if defined(CONFIG_NFS_V4_1)
5338 &nfs41_nograce_recovery_ops,
5339#endif
5340};
5341
Benny Halevy29fba382009-04-01 09:22:44 -04005342struct nfs4_state_maintenance_ops *nfs4_state_renewal_ops[] = {
5343 &nfs40_state_renewal_ops,
5344#if defined(CONFIG_NFS_V4_1)
5345 &nfs41_state_renewal_ops,
5346#endif
5347};
5348
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08005349static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005350 .permission = nfs_permission,
5351 .getattr = nfs_getattr,
5352 .setattr = nfs_setattr,
5353 .getxattr = nfs4_getxattr,
5354 .setxattr = nfs4_setxattr,
5355 .listxattr = nfs4_listxattr,
5356};
5357
David Howells509de812006-08-22 20:06:11 -04005358const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359 .version = 4, /* protocol version */
5360 .dentry_ops = &nfs4_dentry_operations,
5361 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005362 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363 .getroot = nfs4_proc_get_root,
5364 .getattr = nfs4_proc_getattr,
5365 .setattr = nfs4_proc_setattr,
David Howells2b3de442006-08-22 20:06:09 -04005366 .lookupfh = nfs4_proc_lookupfh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005367 .lookup = nfs4_proc_lookup,
5368 .access = nfs4_proc_access,
5369 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370 .create = nfs4_proc_create,
5371 .remove = nfs4_proc_remove,
5372 .unlink_setup = nfs4_proc_unlink_setup,
5373 .unlink_done = nfs4_proc_unlink_done,
5374 .rename = nfs4_proc_rename,
5375 .link = nfs4_proc_link,
5376 .symlink = nfs4_proc_symlink,
5377 .mkdir = nfs4_proc_mkdir,
5378 .rmdir = nfs4_proc_remove,
5379 .readdir = nfs4_proc_readdir,
5380 .mknod = nfs4_proc_mknod,
5381 .statfs = nfs4_proc_statfs,
5382 .fsinfo = nfs4_proc_fsinfo,
5383 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04005384 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385 .decode_dirent = nfs4_decode_dirent,
5386 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05005387 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005389 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005391 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005393 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04005394 .close_context = nfs4_close_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005395};
5396
5397/*
5398 * Local variables:
5399 * c-basic-offset: 8
5400 * End:
5401 */