blob: 9f222f57e75bbcb9c57b398b114ba5a6bc783445 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090042#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/sunrpc/clnt.h>
44#include <linux/nfs.h>
45#include <linux/nfs4.h>
46#include <linux/nfs_fs.h>
47#include <linux/nfs_page.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070049#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040050#include <linux/module.h>
Andy Adamson5a0ffe52009-04-01 09:23:18 -040051#include <linux/sunrpc/bc_xprt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Trond Myklebust4ce79712005-06-22 17:16:21 +000053#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050055#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050056#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040057#include "callback.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59#define NFSDBG_FACILITY NFSDBG_PROC
60
Trond Myklebust2066fe82006-09-15 08:30:46 -040061#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#define NFS4_POLL_RETRY_MAX (15*HZ)
63
Trond Myklebusta78cb572009-08-09 15:06:19 -040064#define NFS4_MAX_LOOP_ON_RECOVER (10)
65
Trond Myklebustcdd4e682006-01-03 09:55:12 +010066struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010067static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080068static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050070static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Trond Myklebust99367812007-07-17 21:52:41 -040071static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
72static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040073static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
74 struct nfs_fattr *fattr, struct iattr *sattr,
75 struct nfs4_state *state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Linus Torvalds1da177e2005-04-16 15:20:36 -070077/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050078static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
Trond Myklebust52567b02009-10-23 14:46:42 -040080 if (err >= -1000)
81 return err;
82 switch (err) {
83 case -NFS4ERR_RESOURCE:
84 return -EREMOTEIO;
85 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -070087 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -040088 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 }
Trond Myklebust52567b02009-10-23 14:46:42 -040090 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
92
93/*
94 * This is our standard bitmap for GETATTR requests.
95 */
96const u32 nfs4_fattr_bitmap[2] = {
97 FATTR4_WORD0_TYPE
98 | FATTR4_WORD0_CHANGE
99 | FATTR4_WORD0_SIZE
100 | FATTR4_WORD0_FSID
101 | FATTR4_WORD0_FILEID,
102 FATTR4_WORD1_MODE
103 | FATTR4_WORD1_NUMLINKS
104 | FATTR4_WORD1_OWNER
105 | FATTR4_WORD1_OWNER_GROUP
106 | FATTR4_WORD1_RAWDEV
107 | FATTR4_WORD1_SPACE_USED
108 | FATTR4_WORD1_TIME_ACCESS
109 | FATTR4_WORD1_TIME_METADATA
110 | FATTR4_WORD1_TIME_MODIFY
111};
112
113const u32 nfs4_statfs_bitmap[2] = {
114 FATTR4_WORD0_FILES_AVAIL
115 | FATTR4_WORD0_FILES_FREE
116 | FATTR4_WORD0_FILES_TOTAL,
117 FATTR4_WORD1_SPACE_AVAIL
118 | FATTR4_WORD1_SPACE_FREE
119 | FATTR4_WORD1_SPACE_TOTAL
120};
121
Trond Myklebust4ce79712005-06-22 17:16:21 +0000122const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 FATTR4_WORD0_MAXLINK
124 | FATTR4_WORD0_MAXNAME,
125 0
126};
127
128const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
129 | FATTR4_WORD0_MAXREAD
130 | FATTR4_WORD0_MAXWRITE
131 | FATTR4_WORD0_LEASE_TIME,
132 0
133};
134
Manoj Naik830b8e32006-06-09 09:34:25 -0400135const u32 nfs4_fs_locations_bitmap[2] = {
136 FATTR4_WORD0_TYPE
137 | FATTR4_WORD0_CHANGE
138 | FATTR4_WORD0_SIZE
139 | FATTR4_WORD0_FSID
140 | FATTR4_WORD0_FILEID
141 | FATTR4_WORD0_FS_LOCATIONS,
142 FATTR4_WORD1_MODE
143 | FATTR4_WORD1_NUMLINKS
144 | FATTR4_WORD1_OWNER
145 | FATTR4_WORD1_OWNER_GROUP
146 | FATTR4_WORD1_RAWDEV
147 | FATTR4_WORD1_SPACE_USED
148 | FATTR4_WORD1_TIME_ACCESS
149 | FATTR4_WORD1_TIME_METADATA
150 | FATTR4_WORD1_TIME_MODIFY
151 | FATTR4_WORD1_MOUNTED_ON_FILEID
152};
153
Al Virobc4785c2006-10-19 23:28:51 -0700154static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 struct nfs4_readdir_arg *readdir)
156{
Al Viro0dbb4c62006-10-19 23:28:49 -0700157 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159 BUG_ON(readdir->count < 80);
160 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000161 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
163 return;
164 }
165
166 readdir->cookie = 0;
167 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
168 if (cookie == 2)
169 return;
170
171 /*
172 * NFSv4 servers do not return entries for '.' and '..'
173 * Therefore, we fake these entries here. We let '.'
174 * have cookie 0 and '..' have cookie 1. Note that
175 * when talking to the server, we always send cookie 0
176 * instead of 1 or 2.
177 */
Al Viro0dbb4c62006-10-19 23:28:49 -0700178 start = p = kmap_atomic(*readdir->pages, KM_USER0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 if (cookie == 0) {
181 *p++ = xdr_one; /* next */
182 *p++ = xdr_zero; /* cookie, first word */
183 *p++ = xdr_one; /* cookie, second word */
184 *p++ = xdr_one; /* entry len */
185 memcpy(p, ".\0\0\0", 4); /* entry */
186 p++;
187 *p++ = xdr_one; /* bitmap length */
188 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
189 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400190 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 }
192
193 *p++ = xdr_one; /* next */
194 *p++ = xdr_zero; /* cookie, first word */
195 *p++ = xdr_two; /* cookie, second word */
196 *p++ = xdr_two; /* entry len */
197 memcpy(p, "..\0\0", 4); /* entry */
198 p++;
199 *p++ = xdr_one; /* bitmap length */
200 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
201 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400202 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204 readdir->pgbase = (char *)p - (char *)start;
205 readdir->count -= readdir->pgbase;
206 kunmap_atomic(start, KM_USER0);
207}
208
Trond Myklebust65de8722008-12-23 15:21:44 -0500209static int nfs4_wait_clnt_recover(struct nfs_client *clp)
210{
211 int res;
212
213 might_sleep();
214
Trond Myklebuste005e802008-12-23 15:21:48 -0500215 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400216 nfs_wait_bit_killable, TASK_KILLABLE);
Trond Myklebust65de8722008-12-23 15:21:44 -0500217 return res;
218}
219
220static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
221{
222 int res = 0;
223
224 might_sleep();
225
226 if (*timeout <= 0)
227 *timeout = NFS4_POLL_RETRY_MIN;
228 if (*timeout > NFS4_POLL_RETRY_MAX)
229 *timeout = NFS4_POLL_RETRY_MAX;
230 schedule_timeout_killable(*timeout);
231 if (fatal_signal_pending(current))
232 res = -ERESTARTSYS;
233 *timeout <<= 1;
234 return res;
235}
236
237/* This is the error handling routine for processes that are allowed
238 * to sleep.
239 */
240static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
241{
242 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500243 struct nfs4_state *state = exception->state;
Trond Myklebust65de8722008-12-23 15:21:44 -0500244 int ret = errorcode;
245
246 exception->retry = 0;
247 switch(errorcode) {
248 case 0:
249 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500250 case -NFS4ERR_ADMIN_REVOKED:
251 case -NFS4ERR_BAD_STATEID:
252 case -NFS4ERR_OPENMODE:
253 if (state == NULL)
254 break;
255 nfs4_state_mark_reclaim_nograce(clp, state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500256 goto do_state_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500257 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500258 if (state == NULL)
259 break;
260 nfs4_state_mark_reclaim_reboot(clp, state);
261 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust65de8722008-12-23 15:21:44 -0500262 case -NFS4ERR_EXPIRED:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500263 goto do_state_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500264#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400265 case -NFS4ERR_BADSESSION:
266 case -NFS4ERR_BADSLOT:
267 case -NFS4ERR_BAD_HIGH_SLOT:
268 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
269 case -NFS4ERR_DEADSESSION:
270 case -NFS4ERR_SEQ_FALSE_RETRY:
271 case -NFS4ERR_SEQ_MISORDERED:
272 dprintk("%s ERROR: %d Reset session\n", __func__,
273 errorcode);
Andy Adamson0b9e2d42009-12-04 16:02:14 -0500274 nfs4_schedule_state_recovery(clp);
Andy Adamson4745e312009-04-01 09:22:42 -0400275 exception->retry = 1;
Andy Adamsonb9179232009-12-04 15:55:32 -0500276 break;
Trond Myklebust03391692010-01-26 15:42:38 -0500277#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500278 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500279 if (exception->timeout > HZ) {
280 /* We have retried a decent amount, time to
281 * fail
282 */
283 ret = -EBUSY;
284 break;
285 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500286 case -NFS4ERR_GRACE:
287 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -0500288 case -EKEYEXPIRED:
Trond Myklebust65de8722008-12-23 15:21:44 -0500289 ret = nfs4_delay(server->client, &exception->timeout);
290 if (ret != 0)
291 break;
292 case -NFS4ERR_OLD_STATEID:
293 exception->retry = 1;
294 }
295 /* We failed to handle the error */
296 return nfs4_map_errors(ret);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500297do_state_recovery:
298 nfs4_schedule_state_recovery(clp);
299 ret = nfs4_wait_clnt_recover(clp);
300 if (ret == 0)
301 exception->retry = 1;
302 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500303}
304
305
Trond Myklebust26e976a2006-01-03 09:55:21 +0100306static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
David Howells7539bba2006-08-22 20:06:09 -0400308 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 spin_lock(&clp->cl_lock);
310 if (time_before(clp->cl_last_renewal,timestamp))
311 clp->cl_last_renewal = timestamp;
312 spin_unlock(&clp->cl_lock);
313}
314
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400315#if defined(CONFIG_NFS_V4_1)
316
Benny Halevy510b8172009-04-01 09:22:14 -0400317/*
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400318 * nfs4_free_slot - free a slot and efficiently update slot table.
319 *
320 * freeing a slot is trivially done by clearing its respective bit
321 * in the bitmap.
322 * If the freed slotid equals highest_used_slotid we want to update it
323 * so that the server would be able to size down the slot table if needed,
324 * otherwise we know that the highest_used_slotid is still in use.
325 * When updating highest_used_slotid there may be "holes" in the bitmap
326 * so we need to scan down from highest_used_slotid to 0 looking for the now
327 * highest slotid in use.
328 * If none found, highest_used_slotid is set to -1.
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500329 *
330 * Must be called while holding tbl->slot_tbl_lock
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400331 */
332static void
333nfs4_free_slot(struct nfs4_slot_table *tbl, u8 free_slotid)
334{
335 int slotid = free_slotid;
336
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400337 /* clear used bit in bitmap */
338 __clear_bit(slotid, tbl->used_slots);
339
340 /* update highest_used_slotid when it is freed */
341 if (slotid == tbl->highest_used_slotid) {
342 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500343 if (slotid < tbl->max_slots)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400344 tbl->highest_used_slotid = slotid;
345 else
346 tbl->highest_used_slotid = -1;
347 }
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400348 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
349 free_slotid, tbl->highest_used_slotid);
350}
351
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800352/*
353 * Signal state manager thread if session is drained
354 */
355static void nfs41_check_drain_session_complete(struct nfs4_session *ses)
356{
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800357 struct rpc_task *task;
358
Trond Myklebusta2118c32010-06-16 09:52:26 -0400359 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800360 task = rpc_wake_up_next(&ses->fc_slot_table.slot_tbl_waitq);
361 if (task)
362 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800363 return;
364 }
365
366 if (ses->fc_slot_table.highest_used_slotid != -1)
367 return;
368
369 dprintk("%s COMPLETE: Session Drained\n", __func__);
370 complete(&ses->complete);
371}
372
Trond Myklebustd185a332010-06-16 09:52:25 -0400373static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400374{
375 struct nfs4_slot_table *tbl;
376
Trond Myklebustd185a332010-06-16 09:52:25 -0400377 tbl = &res->sr_session->fc_slot_table;
Andy Adamson13615872009-04-01 09:22:17 -0400378 if (res->sr_slotid == NFS4_MAX_SLOT_TABLE) {
Andy Adamson13615872009-04-01 09:22:17 -0400379 /* just wake up the next guy waiting since
380 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500381 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500382 return;
Andy Adamson13615872009-04-01 09:22:17 -0400383 }
Andy Adamsonea028ac2009-12-04 15:55:38 -0500384
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500385 spin_lock(&tbl->slot_tbl_lock);
386 nfs4_free_slot(tbl, res->sr_slotid);
Trond Myklebustd185a332010-06-16 09:52:25 -0400387 nfs41_check_drain_session_complete(res->sr_session);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500388 spin_unlock(&tbl->slot_tbl_lock);
389 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
Andy Adamson13615872009-04-01 09:22:17 -0400390}
391
Trond Myklebustd185a332010-06-16 09:52:25 -0400392static void nfs41_sequence_done(struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400393{
394 unsigned long timestamp;
395 struct nfs4_slot_table *tbl;
396 struct nfs4_slot *slot;
397
398 /*
399 * sr_status remains 1 if an RPC level error occurred. The server
400 * may or may not have processed the sequence operation..
401 * Proceed as if the server received and processed the sequence
402 * operation.
403 */
404 if (res->sr_status == 1)
405 res->sr_status = NFS_OK;
406
407 /* -ERESTARTSYS can result in skipping nfs41_sequence_setup */
408 if (res->sr_slotid == NFS4_MAX_SLOT_TABLE)
409 goto out;
410
Andy Adamson691daf32009-12-04 15:55:39 -0500411 /* Check the SEQUENCE operation status */
Andy Adamsonb0df8062009-04-01 09:22:18 -0400412 if (res->sr_status == 0) {
Trond Myklebustd185a332010-06-16 09:52:25 -0400413 struct nfs_client *clp = res->sr_session->clp;
414 tbl = &res->sr_session->fc_slot_table;
Andy Adamson691daf32009-12-04 15:55:39 -0500415 slot = tbl->slots + res->sr_slotid;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400416 /* Update the slot's sequence and clientid lease timer */
417 ++slot->seq_nr;
418 timestamp = res->sr_renewal_time;
419 spin_lock(&clp->cl_lock);
420 if (time_before(clp->cl_last_renewal, timestamp))
421 clp->cl_last_renewal = timestamp;
422 spin_unlock(&clp->cl_lock);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500423 /* Check sequence flags */
Alexandros Batsakis71358402010-02-05 03:45:05 -0800424 if (atomic_read(&clp->cl_count) > 1)
425 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400426 }
427out:
428 /* The session may be reset by one of the error handlers. */
429 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400430 nfs41_sequence_free_slot(res);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400431}
432
Trond Myklebustdf896452010-06-16 09:52:26 -0400433static void nfs4_sequence_done(const struct nfs_server *server,
434 struct nfs4_sequence_res *res, int rpc_status)
435{
436 if (res->sr_session != NULL)
437 nfs41_sequence_done(res);
438}
439
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400440/*
Benny Halevy510b8172009-04-01 09:22:14 -0400441 * nfs4_find_slot - efficiently look for a free slot
442 *
443 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
444 * If found, we mark the slot as used, update the highest_used_slotid,
445 * and respectively set up the sequence operation args.
446 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400447 *
448 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400449 */
450static u8
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800451nfs4_find_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400452{
453 int slotid;
454 u8 ret_id = NFS4_MAX_SLOT_TABLE;
455 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
456
Benny Halevy510b8172009-04-01 09:22:14 -0400457 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
458 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
459 tbl->max_slots);
460 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
461 if (slotid >= tbl->max_slots)
462 goto out;
463 __set_bit(slotid, tbl->used_slots);
464 if (slotid > tbl->highest_used_slotid)
465 tbl->highest_used_slotid = slotid;
466 ret_id = slotid;
467out:
468 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
469 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400470 return ret_id;
471}
472
Andy Adamsonce5039c2009-04-01 09:22:13 -0400473static int nfs41_setup_sequence(struct nfs4_session *session,
474 struct nfs4_sequence_args *args,
475 struct nfs4_sequence_res *res,
476 int cache_reply,
477 struct rpc_task *task)
478{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400479 struct nfs4_slot *slot;
480 struct nfs4_slot_table *tbl;
481 u8 slotid;
482
483 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400484 /* slot already allocated? */
485 if (res->sr_slotid != NFS4_MAX_SLOT_TABLE)
486 return 0;
487
Andy Adamsonce5039c2009-04-01 09:22:13 -0400488 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400489 tbl = &session->fc_slot_table;
490
491 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400492 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800493 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamsonea028ac2009-12-04 15:55:38 -0500494 /*
495 * The state manager will wait until the slot table is empty.
496 * Schedule the reset thread
497 */
Andy Adamson05f0d232009-12-04 15:55:37 -0500498 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400499 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500500 dprintk("%s Schedule Session Reset\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500501 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400502 }
503
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800504 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
505 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
506 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
507 spin_unlock(&tbl->slot_tbl_lock);
508 dprintk("%s enforce FIFO order\n", __func__);
509 return -EAGAIN;
510 }
511
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800512 slotid = nfs4_find_slot(tbl);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400513 if (slotid == NFS4_MAX_SLOT_TABLE) {
514 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
515 spin_unlock(&tbl->slot_tbl_lock);
516 dprintk("<-- %s: no free slots\n", __func__);
517 return -EAGAIN;
518 }
519 spin_unlock(&tbl->slot_tbl_lock);
520
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800521 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400522 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400523 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400524 args->sa_slotid = slotid;
525 args->sa_cache_this = cache_reply;
526
527 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
528
Benny Halevy99fe60d2009-04-01 09:22:29 -0400529 res->sr_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400530 res->sr_slotid = slotid;
531 res->sr_renewal_time = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400532 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400533 /*
534 * sr_status is only set in decode_sequence, and so will remain
535 * set to 1 if an rpc level failure occurs.
536 */
537 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400538 return 0;
539}
540
Trond Myklebust035168ab2010-06-16 09:52:26 -0400541int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400542 struct nfs4_sequence_args *args,
543 struct nfs4_sequence_res *res,
544 int cache_reply,
545 struct rpc_task *task)
546{
Trond Myklebust035168ab2010-06-16 09:52:26 -0400547 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400548 int ret = 0;
549
Trond Myklebust035168ab2010-06-16 09:52:26 -0400550 if (session == NULL) {
551 args->sa_session = NULL;
552 res->sr_session = NULL;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400553 goto out;
Trond Myklebust035168ab2010-06-16 09:52:26 -0400554 }
555
556 dprintk("--> %s clp %p session %p sr_slotid %d\n",
557 __func__, session->clp, session, res->sr_slotid);
558
559 ret = nfs41_setup_sequence(session, args, res, cache_reply,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400560 task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400561out:
562 dprintk("<-- %s status=%d\n", __func__, ret);
563 return ret;
564}
565
566struct nfs41_call_sync_data {
Trond Myklebust035168ab2010-06-16 09:52:26 -0400567 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400568 struct nfs4_sequence_args *seq_args;
569 struct nfs4_sequence_res *seq_res;
570 int cache_reply;
571};
572
573static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
574{
575 struct nfs41_call_sync_data *data = calldata;
576
Trond Myklebust035168ab2010-06-16 09:52:26 -0400577 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
578
579 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400580 data->seq_res, data->cache_reply, task))
581 return;
582 rpc_call_start(task);
583}
584
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800585static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
586{
587 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
588 nfs41_call_sync_prepare(task, calldata);
589}
590
Andy Adamson69ab40c2009-04-01 09:22:19 -0400591static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
592{
593 struct nfs41_call_sync_data *data = calldata;
594
Trond Myklebustd185a332010-06-16 09:52:25 -0400595 nfs41_sequence_done(data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400596}
597
Andy Adamsonce5039c2009-04-01 09:22:13 -0400598struct rpc_call_ops nfs41_call_sync_ops = {
599 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400600 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400601};
602
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800603struct rpc_call_ops nfs41_call_priv_sync_ops = {
604 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
605 .rpc_call_done = nfs41_call_sync_done,
606};
607
Trond Myklebust035168ab2010-06-16 09:52:26 -0400608static int nfs4_call_sync_sequence(struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400609 struct rpc_message *msg,
610 struct nfs4_sequence_args *args,
611 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800612 int cache_reply,
613 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400614{
615 int ret;
616 struct rpc_task *task;
617 struct nfs41_call_sync_data data = {
Trond Myklebust035168ab2010-06-16 09:52:26 -0400618 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400619 .seq_args = args,
620 .seq_res = res,
621 .cache_reply = cache_reply,
622 };
623 struct rpc_task_setup task_setup = {
Trond Myklebust035168ab2010-06-16 09:52:26 -0400624 .rpc_client = server->client,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400625 .rpc_message = msg,
626 .callback_ops = &nfs41_call_sync_ops,
627 .callback_data = &data
628 };
629
630 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800631 if (privileged)
632 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400633 task = rpc_run_task(&task_setup);
634 if (IS_ERR(task))
635 ret = PTR_ERR(task);
636 else {
637 ret = task->tk_status;
638 rpc_put_task(task);
639 }
640 return ret;
641}
642
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400643int _nfs4_call_sync_session(struct nfs_server *server,
644 struct rpc_message *msg,
645 struct nfs4_sequence_args *args,
646 struct nfs4_sequence_res *res,
647 int cache_reply)
648{
Trond Myklebust035168ab2010-06-16 09:52:26 -0400649 return nfs4_call_sync_sequence(server, msg, args, res, cache_reply, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400650}
651
Trond Myklebustdf896452010-06-16 09:52:26 -0400652#else
653static void nfs4_sequence_done(const struct nfs_server *server,
654 struct nfs4_sequence_res *res, int rpc_status)
655{
656}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400657#endif /* CONFIG_NFS_V4_1 */
658
659int _nfs4_call_sync(struct nfs_server *server,
660 struct rpc_message *msg,
661 struct nfs4_sequence_args *args,
662 struct nfs4_sequence_res *res,
663 int cache_reply)
664{
Benny Halevyf3752972009-04-01 09:22:04 -0400665 args->sa_session = res->sr_session = NULL;
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400666 return rpc_call_sync(server->client, msg, 0);
667}
668
669#define nfs4_call_sync(server, msg, args, res, cache_reply) \
Trond Myklebust97dc1352010-06-16 09:52:26 -0400670 (server)->nfs_client->cl_mvops->call_sync((server), (msg), &(args)->seq_args, \
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400671 &(res)->seq_res, (cache_reply))
672
Trond Myklebust38478b22006-05-25 01:40:57 -0400673static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
Trond Myklebust38478b22006-05-25 01:40:57 -0400675 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Trond Myklebust38478b22006-05-25 01:40:57 -0400677 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400678 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
679 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400680 nfs_force_lookup_revalidate(dir);
Trond Myklebust40d24702007-10-08 09:24:22 -0400681 nfsi->change_attr = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400682 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683}
684
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100685struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400686 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100687 struct nfs_openargs o_arg;
688 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100689 struct nfs_open_confirmargs c_arg;
690 struct nfs_open_confirmres c_res;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100691 struct nfs_fattr f_attr;
692 struct nfs_fattr dir_attr;
Trond Myklebustad389da2007-06-05 12:30:00 -0400693 struct path path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100694 struct dentry *dir;
695 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400696 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100697 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100698 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400699 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100700 int rpc_status;
701 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100702};
703
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400704
705static void nfs4_init_opendata_res(struct nfs4_opendata *p)
706{
707 p->o_res.f_attr = &p->f_attr;
708 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400709 p->o_res.seqid = p->o_arg.seqid;
710 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400711 p->o_res.server = p->o_arg.server;
712 nfs_fattr_init(&p->f_attr);
713 nfs_fattr_init(&p->dir_attr);
Benny Halevy578e4582009-06-18 22:01:23 -0400714 p->o_res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400715}
716
Trond Myklebustad389da2007-06-05 12:30:00 -0400717static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500718 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400719 const struct iattr *attrs,
720 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100721{
Trond Myklebustad389da2007-06-05 12:30:00 -0400722 struct dentry *parent = dget_parent(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100723 struct inode *dir = parent->d_inode;
724 struct nfs_server *server = NFS_SERVER(dir);
725 struct nfs4_opendata *p;
726
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400727 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100728 if (p == NULL)
729 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400730 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100731 if (p->o_arg.seqid == NULL)
732 goto err_free;
Al Virof6948692010-01-30 13:51:04 -0500733 path_get(path);
734 p->path = *path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100735 p->dir = parent;
736 p->owner = sp;
737 atomic_inc(&sp->so_count);
738 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500739 p->o_arg.open_flags = flags;
740 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400741 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400742 p->o_arg.id = sp->so_owner_id.id;
Trond Myklebustad389da2007-06-05 12:30:00 -0400743 p->o_arg.name = &p->path.dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100744 p->o_arg.server = server;
745 p->o_arg.bitmask = server->attr_bitmask;
746 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100747 if (flags & O_EXCL) {
Alexandros Batsakis4882ef72009-12-05 13:30:21 -0500748 if (nfs4_has_persistent_session(server->nfs_client)) {
749 /* GUARDED */
750 p->o_arg.u.attrs = &p->attrs;
751 memcpy(&p->attrs, attrs, sizeof(p->attrs));
752 } else { /* EXCLUSIVE4_1 */
753 u32 *s = (u32 *) p->o_arg.u.verifier.data;
754 s[0] = jiffies;
755 s[1] = current->pid;
756 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100757 } else if (flags & O_CREAT) {
758 p->o_arg.u.attrs = &p->attrs;
759 memcpy(&p->attrs, attrs, sizeof(p->attrs));
760 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100761 p->c_arg.fh = &p->o_res.fh;
762 p->c_arg.stateid = &p->o_res.stateid;
763 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400764 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400765 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100766 return p;
767err_free:
768 kfree(p);
769err:
770 dput(parent);
771 return NULL;
772}
773
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400774static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100775{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400776 struct nfs4_opendata *p = container_of(kref,
777 struct nfs4_opendata, kref);
778
779 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400780 if (p->state != NULL)
781 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400782 nfs4_put_state_owner(p->owner);
783 dput(p->dir);
Jan Blunck31f31db12008-05-02 13:42:45 -0700784 path_put(&p->path);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400785 kfree(p);
786}
787
788static void nfs4_opendata_put(struct nfs4_opendata *p)
789{
790 if (p != NULL)
791 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100792}
793
Trond Myklebust06f814a2006-01-03 09:55:07 +0100794static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
795{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100796 int ret;
797
Trond Myklebust06f814a2006-01-03 09:55:07 +0100798 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100799 return ret;
800}
801
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500802static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400803{
804 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500805
806 if (open_mode & O_EXCL)
807 goto out;
808 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400809 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500810 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
811 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400812 break;
813 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500814 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
815 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400816 break;
817 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500818 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
819 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400820 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500821out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400822 return ret;
823}
824
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500825static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400826{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500827 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400828 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500829 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400830 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500831 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400832 return 1;
833}
834
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500835static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100836{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500837 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100838 case FMODE_WRITE:
839 state->n_wronly++;
840 break;
841 case FMODE_READ:
842 state->n_rdonly++;
843 break;
844 case FMODE_READ|FMODE_WRITE:
845 state->n_rdwr++;
846 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500847 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100848}
849
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500850static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400851{
852 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
853 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
854 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500855 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400856 case FMODE_READ:
857 set_bit(NFS_O_RDONLY_STATE, &state->flags);
858 break;
859 case FMODE_WRITE:
860 set_bit(NFS_O_WRONLY_STATE, &state->flags);
861 break;
862 case FMODE_READ|FMODE_WRITE:
863 set_bit(NFS_O_RDWR_STATE, &state->flags);
864 }
865}
866
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500867static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400868{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400869 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500870 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400871 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400872}
873
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500874static 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 -0700875{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400876 /*
877 * Protect the call to nfs4_state_set_mode_locked and
878 * serialise the stateid update
879 */
880 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400881 if (deleg_stateid != NULL) {
882 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
883 set_bit(NFS_DELEGATED_STATE, &state->flags);
884 }
885 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500886 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400887 write_sequnlock(&state->seqlock);
888 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500889 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700890 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891}
892
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500893static 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 -0500894{
895 struct nfs_inode *nfsi = NFS_I(state->inode);
896 struct nfs_delegation *deleg_cur;
897 int ret = 0;
898
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500899 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500900
901 rcu_read_lock();
902 deleg_cur = rcu_dereference(nfsi->delegation);
903 if (deleg_cur == NULL)
904 goto no_delegation;
905
906 spin_lock(&deleg_cur->lock);
907 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500908 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500909 goto no_delegation_unlock;
910
911 if (delegation == NULL)
912 delegation = &deleg_cur->stateid;
913 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
914 goto no_delegation_unlock;
915
Trond Myklebustb7391f42008-12-23 15:21:52 -0500916 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500917 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500918 ret = 1;
919no_delegation_unlock:
920 spin_unlock(&deleg_cur->lock);
921no_delegation:
922 rcu_read_unlock();
923
924 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500925 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500926 ret = 1;
927 }
928
929 return ret;
930}
931
932
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500933static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400934{
935 struct nfs_delegation *delegation;
936
937 rcu_read_lock();
938 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500939 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -0400940 rcu_read_unlock();
941 return;
942 }
943 rcu_read_unlock();
944 nfs_inode_return_delegation(inode);
945}
946
Trond Myklebust6ee41262007-07-08 14:11:36 -0400947static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400948{
949 struct nfs4_state *state = opendata->state;
950 struct nfs_inode *nfsi = NFS_I(state->inode);
951 struct nfs_delegation *delegation;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500952 int open_mode = opendata->o_arg.open_flags & O_EXCL;
953 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400954 nfs4_stateid stateid;
955 int ret = -EAGAIN;
956
Trond Myklebustaac00a82007-07-05 19:02:21 -0400957 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500958 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400959 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500960 if (can_open_cached(state, fmode, open_mode)) {
961 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400962 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400963 goto out_return_state;
964 }
965 spin_unlock(&state->owner->so_lock);
966 }
Trond Myklebust34310432008-12-23 15:21:38 -0500967 rcu_read_lock();
968 delegation = rcu_dereference(nfsi->delegation);
969 if (delegation == NULL ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500970 !can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -0500971 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -0400972 break;
Trond Myklebust34310432008-12-23 15:21:38 -0500973 }
Trond Myklebustaac00a82007-07-05 19:02:21 -0400974 /* Save the delegation */
975 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
976 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -0400977 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400978 if (ret != 0)
979 goto out;
980 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -0500981
982 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500983 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -0500984 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400985 }
Trond Myklebustaac00a82007-07-05 19:02:21 -0400986out:
987 return ERR_PTR(ret);
988out_return_state:
989 atomic_inc(&state->count);
990 return state;
991}
992
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100993static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
994{
995 struct inode *inode;
996 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -0400997 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400998 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100999
Trond Myklebustaac00a82007-07-05 19:02:21 -04001000 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001001 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001002 goto out;
1003 }
1004
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001005 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001006 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001007 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001008 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001009 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001010 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001011 goto err;
1012 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001013 state = nfs4_get_open_state(inode, data->owner);
1014 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001015 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001016 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001017 int delegation_flags = 0;
1018
Trond Myklebust003707c2007-07-05 18:07:55 -04001019 rcu_read_lock();
1020 delegation = rcu_dereference(NFS_I(inode)->delegation);
1021 if (delegation)
1022 delegation_flags = delegation->flags;
1023 rcu_read_unlock();
Trond Myklebust15c831b2008-12-23 15:21:39 -05001024 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001025 nfs_inode_set_delegation(state->inode,
1026 data->owner->so_cred,
1027 &data->o_res);
1028 else
1029 nfs_inode_reclaim_delegation(state->inode,
1030 data->owner->so_cred,
1031 &data->o_res);
1032 }
Trond Myklebust34310432008-12-23 15:21:38 -05001033
1034 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001035 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001036 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001037out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001038 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001039err_put_inode:
1040 iput(inode);
1041err:
1042 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001043}
1044
Trond Myklebust864472e2006-01-03 09:55:15 +01001045static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1046{
1047 struct nfs_inode *nfsi = NFS_I(state->inode);
1048 struct nfs_open_context *ctx;
1049
1050 spin_lock(&state->inode->i_lock);
1051 list_for_each_entry(ctx, &nfsi->open_files, list) {
1052 if (ctx->state != state)
1053 continue;
1054 get_nfs_open_context(ctx);
1055 spin_unlock(&state->inode->i_lock);
1056 return ctx;
1057 }
1058 spin_unlock(&state->inode->i_lock);
1059 return ERR_PTR(-ENOENT);
1060}
1061
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001062static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1063{
1064 struct nfs4_opendata *opendata;
1065
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001066 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001067 if (opendata == NULL)
1068 return ERR_PTR(-ENOMEM);
1069 opendata->state = state;
1070 atomic_inc(&state->count);
1071 return opendata;
1072}
1073
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001074static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001075{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001076 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001077 int ret;
1078
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001079 opendata->o_arg.open_flags = 0;
1080 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001081 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1082 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1083 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001084 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001085 if (ret != 0)
1086 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001087 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001088 if (IS_ERR(newstate))
1089 return PTR_ERR(newstate);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001090 nfs4_close_state(&opendata->path, newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001091 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001092 return 0;
1093}
1094
1095static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1096{
Trond Myklebust864472e2006-01-03 09:55:15 +01001097 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001098 int ret;
1099
1100 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001101 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001102 smp_rmb();
1103 if (state->n_rdwr != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001104 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001105 if (ret != 0)
1106 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001107 if (newstate != state)
1108 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001109 }
1110 if (state->n_wronly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001111 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001112 if (ret != 0)
1113 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001114 if (newstate != state)
1115 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001116 }
1117 if (state->n_rdonly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001118 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001119 if (ret != 0)
1120 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001121 if (newstate != state)
1122 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001123 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001124 /*
1125 * We may have performed cached opens for all three recoveries.
1126 * Check if we need to update the current stateid.
1127 */
1128 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1129 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001130 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001131 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1132 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001133 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001134 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001135 return 0;
1136}
1137
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138/*
1139 * OPEN_RECLAIM:
1140 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001142static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001144 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001145 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001146 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 int status;
1148
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001149 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1150 if (IS_ERR(opendata))
1151 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001152 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1153 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001154 rcu_read_lock();
1155 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001156 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001157 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001158 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001159 opendata->o_arg.u.delegation_type = delegation_type;
1160 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001161 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 return status;
1163}
1164
Trond Myklebust539cd032007-06-05 11:46:42 -04001165static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166{
1167 struct nfs_server *server = NFS_SERVER(state->inode);
1168 struct nfs4_exception exception = { };
1169 int err;
1170 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001171 err = _nfs4_do_open_reclaim(ctx, state);
Jeff Layton2c643482010-01-07 09:42:03 -05001172 if (err != -NFS4ERR_DELAY && err != -EKEYEXPIRED)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001173 break;
1174 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 } while (exception.retry);
1176 return err;
1177}
1178
Trond Myklebust864472e2006-01-03 09:55:15 +01001179static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1180{
1181 struct nfs_open_context *ctx;
1182 int ret;
1183
1184 ctx = nfs4_state_find_open_context(state);
1185 if (IS_ERR(ctx))
1186 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001187 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001188 put_nfs_open_context(ctx);
1189 return ret;
1190}
1191
Trond Myklebust13437e12007-07-06 15:10:43 -04001192static 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 -07001193{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001194 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001195 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001197 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1198 if (IS_ERR(opendata))
1199 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001200 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -04001201 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001202 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +01001203 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001204 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001205 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206}
1207
Trond Myklebust13437e12007-07-06 15:10:43 -04001208int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209{
1210 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001211 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 int err;
1213 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001214 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 switch (err) {
1216 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001217 case -ENOENT:
1218 case -ESTALE:
1219 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001220 case -NFS4ERR_BADSESSION:
1221 case -NFS4ERR_BADSLOT:
1222 case -NFS4ERR_BAD_HIGH_SLOT:
1223 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1224 case -NFS4ERR_DEADSESSION:
1225 nfs4_schedule_state_recovery(
1226 server->nfs_client);
1227 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 case -NFS4ERR_STALE_CLIENTID:
1229 case -NFS4ERR_STALE_STATEID:
1230 case -NFS4ERR_EXPIRED:
1231 /* Don't recall a delegation if it was lost */
David Howells7539bba2006-08-22 20:06:09 -04001232 nfs4_schedule_state_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001233 goto out;
1234 case -ERESTARTSYS:
1235 /*
1236 * The show must go on: exit, but mark the
1237 * stateid as needing recovery.
1238 */
1239 case -NFS4ERR_ADMIN_REVOKED:
1240 case -NFS4ERR_BAD_STATEID:
1241 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
1242 case -ENOMEM:
1243 err = 0;
1244 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 }
1246 err = nfs4_handle_exception(server, err, &exception);
1247 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001248out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 return err;
1250}
1251
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001252static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1253{
1254 struct nfs4_opendata *data = calldata;
1255
1256 data->rpc_status = task->tk_status;
1257 if (RPC_ASSASSINATED(task))
1258 return;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001259 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001260 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1261 sizeof(data->o_res.stateid.data));
Trond Myklebustbb226292008-01-02 15:19:18 -05001262 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001263 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001264 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001265 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001266}
1267
1268static void nfs4_open_confirm_release(void *calldata)
1269{
1270 struct nfs4_opendata *data = calldata;
1271 struct nfs4_state *state = NULL;
1272
1273 /* If this request hasn't been cancelled, do nothing */
1274 if (data->cancelled == 0)
1275 goto out_free;
1276 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001277 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001278 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001279 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001280 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001281 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001282out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001283 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001284}
1285
1286static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001287 .rpc_call_done = nfs4_open_confirm_done,
1288 .rpc_release = nfs4_open_confirm_release,
1289};
1290
1291/*
1292 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1293 */
1294static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1295{
1296 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1297 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001298 struct rpc_message msg = {
1299 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1300 .rpc_argp = &data->c_arg,
1301 .rpc_resp = &data->c_res,
1302 .rpc_cred = data->owner->so_cred,
1303 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001304 struct rpc_task_setup task_setup_data = {
1305 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001306 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001307 .callback_ops = &nfs4_open_confirm_ops,
1308 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001309 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001310 .flags = RPC_TASK_ASYNC,
1311 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 int status;
1313
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001314 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001315 data->rpc_done = 0;
1316 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001317 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001318 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001319 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001320 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001321 status = nfs4_wait_for_completion_rpc_task(task);
1322 if (status != 0) {
1323 data->cancelled = 1;
1324 smp_wmb();
1325 } else
1326 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001327 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 return status;
1329}
1330
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001331static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001333 struct nfs4_opendata *data = calldata;
1334 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001335
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001336 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1337 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001338 /*
1339 * Check if we still need to send an OPEN call, or if we can use
1340 * a delegation instead.
1341 */
1342 if (data->state != NULL) {
1343 struct nfs_delegation *delegation;
1344
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001345 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001346 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001347 rcu_read_lock();
1348 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1349 if (delegation != NULL &&
Trond Myklebust15c831b2008-12-23 15:21:39 -05001350 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001351 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001352 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001353 }
1354 rcu_read_unlock();
1355 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001356 /* Update sequence id. */
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001357 data->o_arg.id = sp->so_owner_id.id;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001358 data->o_arg.clientid = sp->so_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001359 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001360 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001361 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1362 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001363 data->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04001364 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001365 &data->o_arg.seq_args,
1366 &data->o_res.seq_res, 1, task))
1367 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001368 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001369 return;
1370out_no_action:
1371 task->tk_action = NULL;
1372
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001373}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001375static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1376{
1377 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1378 nfs4_open_prepare(task, calldata);
1379}
1380
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001381static void nfs4_open_done(struct rpc_task *task, void *calldata)
1382{
1383 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001385 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001386
Trond Myklebustd61e6122009-12-05 19:32:19 -05001387 nfs4_sequence_done(data->o_arg.server, &data->o_res.seq_res,
1388 task->tk_status);
Andy Adamsond8985282009-04-01 09:22:21 -04001389
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001390 if (RPC_ASSASSINATED(task))
1391 return;
1392 if (task->tk_status == 0) {
1393 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001394 case S_IFREG:
1395 break;
1396 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001397 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001398 break;
1399 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001400 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001401 break;
1402 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001403 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001404 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001405 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001406 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1407 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001408 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001409 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001410}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001411
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001412static void nfs4_open_release(void *calldata)
1413{
1414 struct nfs4_opendata *data = calldata;
1415 struct nfs4_state *state = NULL;
1416
1417 /* If this request hasn't been cancelled, do nothing */
1418 if (data->cancelled == 0)
1419 goto out_free;
1420 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001421 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001422 goto out_free;
1423 /* In case we need an open_confirm, no cleanup! */
1424 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1425 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001426 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001427 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001428 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001429out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001430 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001431}
1432
1433static const struct rpc_call_ops nfs4_open_ops = {
1434 .rpc_call_prepare = nfs4_open_prepare,
1435 .rpc_call_done = nfs4_open_done,
1436 .rpc_release = nfs4_open_release,
1437};
1438
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001439static const struct rpc_call_ops nfs4_recover_open_ops = {
1440 .rpc_call_prepare = nfs4_recover_open_prepare,
1441 .rpc_call_done = nfs4_open_done,
1442 .rpc_release = nfs4_open_release,
1443};
1444
1445static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001446{
1447 struct inode *dir = data->dir->d_inode;
1448 struct nfs_server *server = NFS_SERVER(dir);
1449 struct nfs_openargs *o_arg = &data->o_arg;
1450 struct nfs_openres *o_res = &data->o_res;
1451 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001452 struct rpc_message msg = {
1453 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1454 .rpc_argp = o_arg,
1455 .rpc_resp = o_res,
1456 .rpc_cred = data->owner->so_cred,
1457 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001458 struct rpc_task_setup task_setup_data = {
1459 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001460 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001461 .callback_ops = &nfs4_open_ops,
1462 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001463 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001464 .flags = RPC_TASK_ASYNC,
1465 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001466 int status;
1467
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001468 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001469 data->rpc_done = 0;
1470 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001471 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001472 if (isrecover)
1473 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001474 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001475 if (IS_ERR(task))
1476 return PTR_ERR(task);
1477 status = nfs4_wait_for_completion_rpc_task(task);
1478 if (status != 0) {
1479 data->cancelled = 1;
1480 smp_wmb();
1481 } else
1482 status = data->rpc_status;
1483 rpc_put_task(task);
1484
1485 return status;
1486}
1487
1488static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1489{
1490 struct inode *dir = data->dir->d_inode;
1491 struct nfs_openres *o_res = &data->o_res;
1492 int status;
1493
1494 status = nfs4_run_open_task(data, 1);
1495 if (status != 0 || !data->rpc_done)
1496 return status;
1497
1498 nfs_refresh_inode(dir, o_res->dir_attr);
1499
1500 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1501 status = _nfs4_proc_open_confirm(data);
1502 if (status != 0)
1503 return status;
1504 }
1505
1506 return status;
1507}
1508
1509/*
1510 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1511 */
1512static int _nfs4_proc_open(struct nfs4_opendata *data)
1513{
1514 struct inode *dir = data->dir->d_inode;
1515 struct nfs_server *server = NFS_SERVER(dir);
1516 struct nfs_openargs *o_arg = &data->o_arg;
1517 struct nfs_openres *o_res = &data->o_res;
1518 int status;
1519
1520 status = nfs4_run_open_task(data, 0);
Trond Myklebust3e309912007-07-07 13:19:59 -04001521 if (status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001522 return status;
1523
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001524 if (o_arg->open_flags & O_CREAT) {
1525 update_changeattr(dir, &o_res->cinfo);
1526 nfs_post_op_update_inode(dir, o_res->dir_attr);
1527 } else
1528 nfs_refresh_inode(dir, o_res->dir_attr);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001529 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1530 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001532 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001534 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 }
1536 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001537 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001538 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539}
1540
Trond Myklebust10afec902007-05-14 17:16:04 -04001541static int nfs4_recover_expired_lease(struct nfs_server *server)
Trond Myklebust58d97142006-01-03 09:55:24 +01001542{
David Howells7539bba2006-08-22 20:06:09 -04001543 struct nfs_client *clp = server->nfs_client;
Trond Myklebusta78cb572009-08-09 15:06:19 -04001544 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001545 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001546
Trond Myklebusta78cb572009-08-09 15:06:19 -04001547 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001548 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001549 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001550 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001551 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1552 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001553 break;
Trond Myklebust58d97142006-01-03 09:55:24 +01001554 nfs4_schedule_state_recovery(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001555 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001556 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001557 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001558}
1559
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560/*
1561 * OPEN_EXPIRED:
1562 * reclaim state on the server after a network partition.
1563 * Assumes caller holds the appropriate lock
1564 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001565static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001567 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001568 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001570 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1571 if (IS_ERR(opendata))
1572 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001573 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001574 if (ret == -ESTALE)
Trond Myklebust539cd032007-06-05 11:46:42 -04001575 d_drop(ctx->path.dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001576 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001577 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578}
1579
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001580static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001581{
Trond Myklebust539cd032007-06-05 11:46:42 -04001582 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001583 struct nfs4_exception exception = { };
1584 int err;
1585
1586 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001587 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001588 switch (err) {
1589 default:
1590 goto out;
1591 case -NFS4ERR_GRACE:
1592 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -05001593 case -EKEYEXPIRED:
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001594 nfs4_handle_exception(server, err, &exception);
1595 err = 0;
1596 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001597 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001598out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001599 return err;
1600}
1601
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1603{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001605 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
Trond Myklebust864472e2006-01-03 09:55:15 +01001607 ctx = nfs4_state_find_open_context(state);
1608 if (IS_ERR(ctx))
1609 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001610 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001611 put_nfs_open_context(ctx);
1612 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613}
1614
1615/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001616 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1617 * fields corresponding to attributes that were used to store the verifier.
1618 * Make sure we clobber those fields in the later setattr call
1619 */
1620static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1621{
1622 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1623 !(sattr->ia_valid & ATTR_ATIME_SET))
1624 sattr->ia_valid |= ATTR_ATIME;
1625
1626 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1627 !(sattr->ia_valid & ATTR_MTIME_SET))
1628 sattr->ia_valid |= ATTR_MTIME;
1629}
1630
1631/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001632 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001634static 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 -07001635{
1636 struct nfs4_state_owner *sp;
1637 struct nfs4_state *state = NULL;
1638 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001639 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001640 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
1642 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 status = -ENOMEM;
1644 if (!(sp = nfs4_get_state_owner(server, cred))) {
1645 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1646 goto out_err;
1647 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001648 status = nfs4_recover_expired_lease(server);
1649 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001650 goto err_put_state_owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001651 if (path->dentry->d_inode != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001652 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001653 status = -ENOMEM;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001654 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001655 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001656 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
Trond Myklebustaac00a82007-07-05 19:02:21 -04001658 if (path->dentry->d_inode != NULL)
1659 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1660
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001661 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001663 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001665 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001666 status = PTR_ERR(state);
1667 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001668 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001669 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001670 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001671
1672 if (opendata->o_arg.open_flags & O_EXCL) {
1673 nfs4_exclusive_attrset(opendata, sattr);
1674
1675 nfs_fattr_init(opendata->o_res.f_attr);
1676 status = nfs4_do_setattr(state->inode, cred,
1677 opendata->o_res.f_attr, sattr,
1678 state);
1679 if (status == 0)
1680 nfs_setattr_update_inode(state->inode, sattr);
1681 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1682 }
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001683 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 *res = state;
1686 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001687err_opendata_put:
1688 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001689err_put_state_owner:
1690 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 *res = NULL;
1693 return status;
1694}
1695
1696
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001697static 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 -07001698{
1699 struct nfs4_exception exception = { };
1700 struct nfs4_state *res;
1701 int status;
1702
1703 do {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001704 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 if (status == 0)
1706 break;
1707 /* NOTE: BAD_SEQID means the server and client disagree about the
1708 * book-keeping w.r.t. state-changing operations
1709 * (OPEN/CLOSE/LOCK/LOCKU...)
1710 * It is actually a sign of a bug on the client or on the server.
1711 *
1712 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001713 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 * have unhashed the old state_owner for us, and that we can
1715 * therefore safely retry using a new one. We should still warn
1716 * the user though...
1717 */
1718 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001719 printk(KERN_WARNING "NFS: v4 server %s "
1720 " returned a bad sequence-id error!\n",
1721 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 exception.retry = 1;
1723 continue;
1724 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001725 /*
1726 * BAD_STATEID on OPEN means that the server cancelled our
1727 * state before it received the OPEN_CONFIRM.
1728 * Recover by retrying the request as per the discussion
1729 * on Page 181 of RFC3530.
1730 */
1731 if (status == -NFS4ERR_BAD_STATEID) {
1732 exception.retry = 1;
1733 continue;
1734 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001735 if (status == -EAGAIN) {
1736 /* We must have found a delegation */
1737 exception.retry = 1;
1738 continue;
1739 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1741 status, &exception));
1742 } while (exception.retry);
1743 return res;
1744}
1745
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001746static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1747 struct nfs_fattr *fattr, struct iattr *sattr,
1748 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001750 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001752 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 .iap = sattr,
1754 .server = server,
1755 .bitmask = server->attr_bitmask,
1756 };
1757 struct nfs_setattrres res = {
1758 .fattr = fattr,
1759 .server = server,
1760 };
1761 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001762 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1763 .rpc_argp = &arg,
1764 .rpc_resp = &res,
1765 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001767 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001768 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
Trond Myklebust0e574af2005-10-27 22:12:38 -04001770 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001772 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1773 /* Use that stateid */
1774 } else if (state != NULL) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001775 nfs4_copy_stateid(&arg.stateid, state, current->files);
1776 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1778
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001779 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001780 if (status == 0 && state != NULL)
1781 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001782 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783}
1784
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001785static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1786 struct nfs_fattr *fattr, struct iattr *sattr,
1787 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001789 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 struct nfs4_exception exception = { };
1791 int err;
1792 do {
1793 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001794 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 &exception);
1796 } while (exception.retry);
1797 return err;
1798}
1799
1800struct nfs4_closedata {
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001801 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 struct inode *inode;
1803 struct nfs4_state *state;
1804 struct nfs_closeargs arg;
1805 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001806 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001807 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808};
1809
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001810static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001811{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001812 struct nfs4_closedata *calldata = data;
1813 struct nfs4_state_owner *sp = calldata->state->owner;
Trond Myklebust95121352005-10-18 14:20:12 -07001814
1815 nfs4_put_open_state(calldata->state);
1816 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001817 nfs4_put_state_owner(sp);
Jan Blunck31f31db12008-05-02 13:42:45 -07001818 path_put(&calldata->path);
Trond Myklebust95121352005-10-18 14:20:12 -07001819 kfree(calldata);
1820}
1821
Trond Myklebust88069f72009-12-08 08:33:16 -05001822static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1823 fmode_t fmode)
1824{
1825 spin_lock(&state->owner->so_lock);
1826 if (!(fmode & FMODE_READ))
1827 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1828 if (!(fmode & FMODE_WRITE))
1829 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1830 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1831 spin_unlock(&state->owner->so_lock);
1832}
1833
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001834static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001836 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 struct nfs_server *server = NFS_SERVER(calldata->inode);
1839
Andy Adamson19ddab02009-04-01 09:22:20 -04001840 nfs4_sequence_done(server, &calldata->res.seq_res, task->tk_status);
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001841 if (RPC_ASSASSINATED(task))
1842 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 /* hmm. we are done with the inode, and in the process of freeing
1844 * the state_owner. we keep this around to process errors
1845 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 switch (task->tk_status) {
1847 case 0:
Trond Myklebust45328c32007-07-26 17:47:34 -04001848 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001849 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05001850 nfs4_close_clear_stateid_flags(state,
1851 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 break;
1853 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001854 case -NFS4ERR_OLD_STATEID:
1855 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001857 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001858 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05001860 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1861 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 }
Trond Myklebust72211db2009-12-15 14:47:36 -05001863 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04001864 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865}
1866
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001867static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001869 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001870 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05001871 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07001872
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001873 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001874 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001875
Trond Myklebust88069f72009-12-08 08:33:16 -05001876 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1877 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05001878 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001879 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01001880 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001881 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001882 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
1883 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1884 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04001885 }
1886 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001887 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
1888 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1889 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04001890 }
Trond Myklebuste7616922006-01-03 09:55:13 +01001891 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05001892 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05001893
1894 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001895 /* Note: exit _without_ calling nfs4_close_done */
1896 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07001897 return;
1898 }
Trond Myklebust88069f72009-12-08 08:33:16 -05001899
1900 if (calldata->arg.fmode == 0)
1901 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
1902
Trond Myklebust516a6af2005-10-27 22:12:41 -04001903 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001904 calldata->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04001905 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
Andy Adamson19ddab02009-04-01 09:22:20 -04001906 &calldata->arg.seq_args, &calldata->res.seq_res,
1907 1, task))
1908 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001909 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910}
1911
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001912static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001913 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001914 .rpc_call_done = nfs4_close_done,
1915 .rpc_release = nfs4_free_closedata,
1916};
1917
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918/*
1919 * It is possible for data to be read/written from a mem-mapped file
1920 * after the sys_close call (which hits the vfs layer as a flush).
1921 * This means that we can't safely call nfsv4 close on a file until
1922 * the inode is cleared. This in turn means that we are not good
1923 * NFSv4 citizens - we do not indicate to the server to update the file's
1924 * share state even when we are done with one of the three share
1925 * stateid's in the inode.
1926 *
1927 * NOTE: Caller must be holding the sp->so_owner semaphore!
1928 */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001929int nfs4_do_close(struct path *path, struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001931 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04001933 struct nfs4_state_owner *sp = state->owner;
1934 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001935 struct rpc_message msg = {
1936 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1937 .rpc_cred = state->owner->so_cred,
1938 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001939 struct rpc_task_setup task_setup_data = {
1940 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001941 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001942 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05001943 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001944 .flags = RPC_TASK_ASYNC,
1945 };
Trond Myklebust95121352005-10-18 14:20:12 -07001946 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001948 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07001950 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001951 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001953 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04001954 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001956 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07001957 if (calldata->arg.seqid == NULL)
1958 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001959 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04001960 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001961 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04001962 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001963 calldata->res.server = server;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04001964 calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Al Virof6948692010-01-30 13:51:04 -05001965 path_get(path);
1966 calldata->path = *path;
Trond Myklebust95121352005-10-18 14:20:12 -07001967
Trond Myklebust5138fde2007-07-14 15:40:01 -04001968 msg.rpc_argp = &calldata->arg,
1969 msg.rpc_resp = &calldata->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001970 task_setup_data.callback_data = calldata;
1971 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001972 if (IS_ERR(task))
1973 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001974 status = 0;
1975 if (wait)
1976 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001977 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001978 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07001979out_free_calldata:
1980 kfree(calldata);
1981out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04001982 nfs4_put_open_state(state);
1983 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07001984 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985}
1986
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001987static 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 -07001988{
1989 struct file *filp;
Trond Myklebust1b45c462007-07-03 13:04:56 -04001990 int ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001991
Trond Myklebust1b45c462007-07-03 13:04:56 -04001992 /* If the open_intent is for execute, we have an extra check to make */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001993 if (fmode & FMODE_EXEC) {
Trond Myklebustaf22f942007-08-10 17:45:10 -04001994 ret = nfs_may_open(state->inode,
Trond Myklebust1b45c462007-07-03 13:04:56 -04001995 state->owner->so_cred,
1996 nd->intent.open.flags);
1997 if (ret < 0)
1998 goto out_close;
1999 }
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002000 filp = lookup_instantiate_filp(nd, path->dentry, NULL);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002001 if (!IS_ERR(filp)) {
2002 struct nfs_open_context *ctx;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04002003 ctx = nfs_file_open_context(filp);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002004 ctx->state = state;
Trond Myklebust95cf9592006-04-18 13:14:06 -04002005 return 0;
2006 }
Trond Myklebust1b45c462007-07-03 13:04:56 -04002007 ret = PTR_ERR(filp);
2008out_close:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002009 nfs4_close_sync(path, state, fmode & (FMODE_READ|FMODE_WRITE));
Trond Myklebust1b45c462007-07-03 13:04:56 -04002010 return ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002011}
2012
2013struct dentry *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
2015{
Trond Myklebustad389da2007-06-05 12:30:00 -04002016 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08002017 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04002018 .dentry = dentry,
2019 };
Jan Blunck4ac91372008-02-14 19:34:32 -08002020 struct dentry *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 struct iattr attr;
2022 struct rpc_cred *cred;
2023 struct nfs4_state *state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002024 struct dentry *res;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002025 fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
2027 if (nd->flags & LOOKUP_CREATE) {
2028 attr.ia_mode = nd->intent.open.create_mode;
2029 attr.ia_valid = ATTR_MODE;
2030 if (!IS_POSIXACL(dir))
Al Viroce3b0f82009-03-29 19:08:22 -04002031 attr.ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 } else {
2033 attr.ia_valid = 0;
2034 BUG_ON(nd->intent.open.flags & O_CREAT);
2035 }
2036
Trond Myklebust98a8e322008-03-12 12:25:28 -04002037 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 if (IS_ERR(cred))
Trond Myklebust02a913a2005-10-18 14:20:17 -07002039 return (struct dentry *)cred;
Trond Myklebust565277f2007-10-15 18:17:53 -04002040 parent = dentry->d_parent;
2041 /* Protect against concurrent sillydeletes */
2042 nfs_block_sillyrename(parent);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002043 state = nfs4_do_open(dir, &path, fmode, nd->intent.open.flags, &attr, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002045 if (IS_ERR(state)) {
Trond Myklebustd75340c2007-10-01 21:42:01 -04002046 if (PTR_ERR(state) == -ENOENT) {
Trond Myklebust02a913a2005-10-18 14:20:17 -07002047 d_add(dentry, NULL);
Trond Myklebustd75340c2007-10-01 21:42:01 -04002048 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2049 }
Trond Myklebust565277f2007-10-15 18:17:53 -04002050 nfs_unblock_sillyrename(parent);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002051 return (struct dentry *)state;
2052 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002053 res = d_add_unique(dentry, igrab(state->inode));
Trond Myklebust02a913a2005-10-18 14:20:17 -07002054 if (res != NULL)
Trond Myklebustdeee9362007-08-27 11:33:00 -04002055 path.dentry = res;
Trond Myklebustd75340c2007-10-01 21:42:01 -04002056 nfs_set_verifier(path.dentry, nfs_save_change_attribute(dir));
Trond Myklebust565277f2007-10-15 18:17:53 -04002057 nfs_unblock_sillyrename(parent);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002058 nfs4_intent_set_file(nd, &path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002059 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060}
2061
2062int
Trond Myklebust02a913a2005-10-18 14:20:17 -07002063nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064{
Trond Myklebustad389da2007-06-05 12:30:00 -04002065 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08002066 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04002067 .dentry = dentry,
2068 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 struct rpc_cred *cred;
2070 struct nfs4_state *state;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002071 fmode_t fmode = openflags & (FMODE_READ | FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
Trond Myklebust98a8e322008-03-12 12:25:28 -04002073 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 if (IS_ERR(cred))
2075 return PTR_ERR(cred);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002076 state = nfs4_do_open(dir, &path, fmode, openflags, NULL, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002078 if (IS_ERR(state)) {
2079 switch (PTR_ERR(state)) {
2080 case -EPERM:
2081 case -EACCES:
2082 case -EDQUOT:
2083 case -ENOSPC:
2084 case -EROFS:
Al Viro04287f92010-04-08 00:06:07 +01002085 return PTR_ERR(state);
Chuck Leverb87c0ad2006-10-19 23:28:42 -07002086 default:
2087 goto out_drop;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002088 }
Trond Myklebust02a913a2005-10-18 14:20:17 -07002089 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002090 if (state->inode == dentry->d_inode) {
Trond Myklebustd75340c2007-10-01 21:42:01 -04002091 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002092 nfs4_intent_set_file(nd, &path, state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 return 1;
2094 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002095 nfs4_close_sync(&path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002096out_drop:
2097 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 return 0;
2099}
2100
Trond Myklebust1185a552009-12-03 15:54:02 -05002101static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002102{
2103 if (ctx->state == NULL)
2104 return;
2105 if (is_sync)
2106 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
2107 else
2108 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
2109}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110
2111static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2112{
Benny Halevy43652ad2009-04-01 09:21:54 -04002113 struct nfs4_server_caps_arg args = {
2114 .fhandle = fhandle,
2115 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 struct nfs4_server_caps_res res = {};
2117 struct rpc_message msg = {
2118 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002119 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 .rpc_resp = &res,
2121 };
2122 int status;
2123
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002124 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 if (status == 0) {
2126 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04002127 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2128 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2129 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2130 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2131 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2133 server->caps |= NFS_CAP_ACLS;
2134 if (res.has_links != 0)
2135 server->caps |= NFS_CAP_HARDLINKS;
2136 if (res.has_symlinks != 0)
2137 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04002138 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2139 server->caps |= NFS_CAP_FILEID;
2140 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2141 server->caps |= NFS_CAP_MODE;
2142 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2143 server->caps |= NFS_CAP_NLINK;
2144 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2145 server->caps |= NFS_CAP_OWNER;
2146 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2147 server->caps |= NFS_CAP_OWNER_GROUP;
2148 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2149 server->caps |= NFS_CAP_ATIME;
2150 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2151 server->caps |= NFS_CAP_CTIME;
2152 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2153 server->caps |= NFS_CAP_MTIME;
2154
Trond Myklebusta65318b2009-03-11 14:10:28 -04002155 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2156 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2157 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 server->acl_bitmask = res.acl_bitmask;
2159 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002160
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 return status;
2162}
2163
Trond Myklebust55a97592006-06-09 09:34:19 -04002164int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165{
2166 struct nfs4_exception exception = { };
2167 int err;
2168 do {
2169 err = nfs4_handle_exception(server,
2170 _nfs4_server_capabilities(server, fhandle),
2171 &exception);
2172 } while (exception.retry);
2173 return err;
2174}
2175
2176static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2177 struct nfs_fsinfo *info)
2178{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 struct nfs4_lookup_root_arg args = {
2180 .bitmask = nfs4_fattr_bitmap,
2181 };
2182 struct nfs4_lookup_res res = {
2183 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002184 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 .fh = fhandle,
2186 };
2187 struct rpc_message msg = {
2188 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2189 .rpc_argp = &args,
2190 .rpc_resp = &res,
2191 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002192
Trond Myklebust0e574af2005-10-27 22:12:38 -04002193 nfs_fattr_init(info->fattr);
Trond Myklebustfccba802009-07-21 16:48:07 -04002194 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195}
2196
2197static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2198 struct nfs_fsinfo *info)
2199{
2200 struct nfs4_exception exception = { };
2201 int err;
2202 do {
2203 err = nfs4_handle_exception(server,
2204 _nfs4_lookup_root(server, fhandle, info),
2205 &exception);
2206 } while (exception.retry);
2207 return err;
2208}
2209
David Howells54ceac42006-08-22 20:06:13 -04002210/*
2211 * get the file handle for the "/" directory on the server
2212 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002214 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 int status;
2217
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 status = nfs4_lookup_root(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 if (status == 0)
2220 status = nfs4_server_capabilities(server, fhandle);
2221 if (status == 0)
2222 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002223 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224}
2225
Manoj Naik6b97fd32006-06-09 09:34:29 -04002226/*
2227 * Get locations and (maybe) other attributes of a referral.
2228 * Note that we'll actually follow the referral later when
2229 * we detect fsid mismatch in inode revalidation
2230 */
Trond Myklebust56659e92007-07-17 21:52:39 -04002231static 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 -04002232{
2233 int status = -ENOMEM;
2234 struct page *page = NULL;
2235 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002236
2237 page = alloc_page(GFP_KERNEL);
2238 if (page == NULL)
2239 goto out;
2240 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2241 if (locations == NULL)
2242 goto out;
2243
Trond Myklebustc228fd32007-01-13 02:28:11 -05002244 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002245 if (status != 0)
2246 goto out;
2247 /* Make sure server returned a different fsid for the referral */
2248 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -07002249 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 -04002250 status = -EIO;
2251 goto out;
2252 }
2253
2254 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2255 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
2256 if (!fattr->mode)
2257 fattr->mode = S_IFDIR;
2258 memset(fhandle, 0, sizeof(struct nfs_fh));
2259out:
2260 if (page)
2261 __free_page(page);
2262 if (locations)
2263 kfree(locations);
2264 return status;
2265}
2266
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2268{
2269 struct nfs4_getattr_arg args = {
2270 .fh = fhandle,
2271 .bitmask = server->attr_bitmask,
2272 };
2273 struct nfs4_getattr_res res = {
2274 .fattr = fattr,
2275 .server = server,
2276 };
2277 struct rpc_message msg = {
2278 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2279 .rpc_argp = &args,
2280 .rpc_resp = &res,
2281 };
2282
Trond Myklebust0e574af2005-10-27 22:12:38 -04002283 nfs_fattr_init(fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002284 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285}
2286
2287static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2288{
2289 struct nfs4_exception exception = { };
2290 int err;
2291 do {
2292 err = nfs4_handle_exception(server,
2293 _nfs4_proc_getattr(server, fhandle, fattr),
2294 &exception);
2295 } while (exception.retry);
2296 return err;
2297}
2298
2299/*
2300 * The file is not closed if it is opened due to the a request to change
2301 * the size of the file. The open call will not be needed once the
2302 * VFS layer lookup-intents are implemented.
2303 *
2304 * Close is called when the inode is destroyed.
2305 * If we haven't opened the file for O_WRONLY, we
2306 * need to in the size_change case to obtain a stateid.
2307 *
2308 * Got race?
2309 * Because OPEN is always done by name in nfsv4, it is
2310 * possible that we opened a different file by the same
2311 * name. We can recognize this race condition, but we
2312 * can't do anything about it besides returning an error.
2313 *
2314 * This will be fixed with VFS changes (lookup-intent).
2315 */
2316static int
2317nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2318 struct iattr *sattr)
2319{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002320 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002321 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002322 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 int status;
2324
Trond Myklebust0e574af2005-10-27 22:12:38 -04002325 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326
Trond Myklebustd5308382005-11-04 15:33:38 -05002327 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002328 if (sattr->ia_valid & ATTR_FILE) {
2329 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002330
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002331 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002332 if (ctx) {
2333 cred = ctx->cred;
2334 state = ctx->state;
2335 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002336 }
2337
2338 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002339 if (status == 0)
2340 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 return status;
2342}
2343
Trond Myklebust56659e92007-07-17 21:52:39 -04002344static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
2345 const struct qstr *name, struct nfs_fh *fhandle,
David Howells2b3de442006-08-22 20:06:09 -04002346 struct nfs_fattr *fattr)
2347{
2348 int status;
2349 struct nfs4_lookup_arg args = {
2350 .bitmask = server->attr_bitmask,
2351 .dir_fh = dirfh,
2352 .name = name,
2353 };
2354 struct nfs4_lookup_res res = {
2355 .server = server,
2356 .fattr = fattr,
2357 .fh = fhandle,
2358 };
2359 struct rpc_message msg = {
2360 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2361 .rpc_argp = &args,
2362 .rpc_resp = &res,
2363 };
2364
2365 nfs_fattr_init(fattr);
2366
2367 dprintk("NFS call lookupfh %s\n", name->name);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002368 status = nfs4_call_sync(server, &msg, &args, &res, 0);
David Howells2b3de442006-08-22 20:06:09 -04002369 dprintk("NFS reply lookupfh: %d\n", status);
David Howells2b3de442006-08-22 20:06:09 -04002370 return status;
2371}
2372
2373static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2374 struct qstr *name, struct nfs_fh *fhandle,
2375 struct nfs_fattr *fattr)
2376{
2377 struct nfs4_exception exception = { };
2378 int err;
2379 do {
Trond Myklebust4e56e082007-07-01 18:13:52 -04002380 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
2381 /* FIXME: !!!! */
2382 if (err == -NFS4ERR_MOVED) {
2383 err = -EREMOTE;
2384 break;
2385 }
2386 err = nfs4_handle_exception(server, err, &exception);
David Howells2b3de442006-08-22 20:06:09 -04002387 } while (exception.retry);
2388 return err;
2389}
2390
Trond Myklebust56659e92007-07-17 21:52:39 -04002391static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2393{
Trond Myklebust4e56e082007-07-01 18:13:52 -04002394 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395
2396 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust4e56e082007-07-01 18:13:52 -04002397 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002398 if (status == -NFS4ERR_MOVED)
2399 status = nfs4_get_referral(dir, name, fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 dprintk("NFS reply lookup: %d\n", status);
2401 return status;
2402}
2403
2404static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2405{
2406 struct nfs4_exception exception = { };
2407 int err;
2408 do {
2409 err = nfs4_handle_exception(NFS_SERVER(dir),
2410 _nfs4_proc_lookup(dir, name, fhandle, fattr),
2411 &exception);
2412 } while (exception.retry);
2413 return err;
2414}
2415
2416static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2417{
Trond Myklebust76b32992007-08-10 17:45:11 -04002418 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 struct nfs4_accessargs args = {
2420 .fh = NFS_FH(inode),
Trond Myklebust76b32992007-08-10 17:45:11 -04002421 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002423 struct nfs4_accessres res = {
2424 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002425 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 struct rpc_message msg = {
2427 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2428 .rpc_argp = &args,
2429 .rpc_resp = &res,
2430 .rpc_cred = entry->cred,
2431 };
2432 int mode = entry->mask;
2433 int status;
2434
2435 /*
2436 * Determine which access bits we want to ask for...
2437 */
2438 if (mode & MAY_READ)
2439 args.access |= NFS4_ACCESS_READ;
2440 if (S_ISDIR(inode->i_mode)) {
2441 if (mode & MAY_WRITE)
2442 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2443 if (mode & MAY_EXEC)
2444 args.access |= NFS4_ACCESS_LOOKUP;
2445 } else {
2446 if (mode & MAY_WRITE)
2447 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2448 if (mode & MAY_EXEC)
2449 args.access |= NFS4_ACCESS_EXECUTE;
2450 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002451
2452 res.fattr = nfs_alloc_fattr();
2453 if (res.fattr == NULL)
2454 return -ENOMEM;
2455
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002456 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 if (!status) {
2458 entry->mask = 0;
2459 if (res.access & NFS4_ACCESS_READ)
2460 entry->mask |= MAY_READ;
2461 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2462 entry->mask |= MAY_WRITE;
2463 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2464 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002465 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002467 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 return status;
2469}
2470
2471static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2472{
2473 struct nfs4_exception exception = { };
2474 int err;
2475 do {
2476 err = nfs4_handle_exception(NFS_SERVER(inode),
2477 _nfs4_proc_access(inode, entry),
2478 &exception);
2479 } while (exception.retry);
2480 return err;
2481}
2482
2483/*
2484 * TODO: For the time being, we don't try to get any attributes
2485 * along with any of the zero-copy operations READ, READDIR,
2486 * READLINK, WRITE.
2487 *
2488 * In the case of the first three, we want to put the GETATTR
2489 * after the read-type operation -- this is because it is hard
2490 * to predict the length of a GETATTR response in v4, and thus
2491 * align the READ data correctly. This means that the GETATTR
2492 * may end up partially falling into the page cache, and we should
2493 * shift it into the 'tail' of the xdr_buf before processing.
2494 * To do this efficiently, we need to know the total length
2495 * of data received, which doesn't seem to be available outside
2496 * of the RPC layer.
2497 *
2498 * In the case of WRITE, we also want to put the GETATTR after
2499 * the operation -- in this case because we want to make sure
2500 * we get the post-operation mtime and size. This means that
2501 * we can't use xdr_encode_pages() as written: we need a variant
2502 * of it which would leave room in the 'tail' iovec.
2503 *
2504 * Both of these changes to the XDR layer would in fact be quite
2505 * minor, but I decided to leave them for a subsequent patch.
2506 */
2507static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2508 unsigned int pgbase, unsigned int pglen)
2509{
2510 struct nfs4_readlink args = {
2511 .fh = NFS_FH(inode),
2512 .pgbase = pgbase,
2513 .pglen = pglen,
2514 .pages = &page,
2515 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002516 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 struct rpc_message msg = {
2518 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2519 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002520 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 };
2522
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002523 return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524}
2525
2526static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2527 unsigned int pgbase, unsigned int pglen)
2528{
2529 struct nfs4_exception exception = { };
2530 int err;
2531 do {
2532 err = nfs4_handle_exception(NFS_SERVER(inode),
2533 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2534 &exception);
2535 } while (exception.retry);
2536 return err;
2537}
2538
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539/*
2540 * Got race?
2541 * We will need to arrange for the VFS layer to provide an atomic open.
2542 * Until then, this create/open method is prone to inefficiency and race
2543 * conditions due to the lookup, create, and open VFS calls from sys_open()
2544 * placed on the wire.
2545 *
2546 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2547 * The file will be opened again in the subsequent VFS open call
2548 * (nfs4_proc_file_open).
2549 *
2550 * The open for read will just hang around to be used by any process that
2551 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2552 */
2553
2554static int
2555nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebust02a913a2005-10-18 14:20:17 -07002556 int flags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557{
Trond Myklebustad389da2007-06-05 12:30:00 -04002558 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08002559 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04002560 .dentry = dentry,
2561 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 struct nfs4_state *state;
2563 struct rpc_cred *cred;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002564 fmode_t fmode = flags & (FMODE_READ | FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 int status = 0;
2566
Trond Myklebust98a8e322008-03-12 12:25:28 -04002567 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 if (IS_ERR(cred)) {
2569 status = PTR_ERR(cred);
2570 goto out;
2571 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002572 state = nfs4_do_open(dir, &path, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002573 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 if (IS_ERR(state)) {
2575 status = PTR_ERR(state);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002576 goto out_putcred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002578 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002579 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustad389da2007-06-05 12:30:00 -04002580 if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002581 status = nfs4_intent_set_file(nd, &path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002582 else
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002583 nfs4_close_sync(&path, state, fmode);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002584out_putcred:
2585 put_rpccred(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586out:
2587 return status;
2588}
2589
2590static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2591{
Trond Myklebust16e42952005-10-27 22:12:44 -04002592 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002593 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002595 .name.len = name->len,
2596 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002597 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002599 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002600 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002601 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002603 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2604 .rpc_argp = &args,
2605 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 };
Trond Myklebustd3468902010-04-16 16:22:50 -04002607 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608
Trond Myklebustd3468902010-04-16 16:22:50 -04002609 res.dir_attr = nfs_alloc_fattr();
2610 if (res.dir_attr == NULL)
2611 goto out;
2612
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002613 status = nfs4_call_sync(server, &msg, &args, &res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002614 if (status == 0) {
2615 update_changeattr(dir, &res.cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002616 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002617 }
Trond Myklebustd3468902010-04-16 16:22:50 -04002618 nfs_free_fattr(res.dir_attr);
2619out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 return status;
2621}
2622
2623static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2624{
2625 struct nfs4_exception exception = { };
2626 int err;
2627 do {
2628 err = nfs4_handle_exception(NFS_SERVER(dir),
2629 _nfs4_proc_remove(dir, name),
2630 &exception);
2631 } while (exception.retry);
2632 return err;
2633}
2634
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002635static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002637 struct nfs_server *server = NFS_SERVER(dir);
2638 struct nfs_removeargs *args = msg->rpc_argp;
2639 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640
Trond Myklebusta65318b2009-03-11 14:10:28 -04002641 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002642 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644}
2645
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002646static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002648 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2649
Andy Adamson472cfbd2009-04-01 09:22:24 -04002650 nfs4_sequence_done(res->server, &res->seq_res, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002651 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002652 return 0;
2653 update_changeattr(dir, &res->cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002654 nfs_post_op_update_inode(dir, res->dir_attr);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002655 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656}
2657
2658static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2659 struct inode *new_dir, struct qstr *new_name)
2660{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002661 struct nfs_server *server = NFS_SERVER(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 struct nfs4_rename_arg arg = {
2663 .old_dir = NFS_FH(old_dir),
2664 .new_dir = NFS_FH(new_dir),
2665 .old_name = old_name,
2666 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002667 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 };
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002669 struct nfs4_rename_res res = {
2670 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002671 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 struct rpc_message msg = {
2673 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2674 .rpc_argp = &arg,
2675 .rpc_resp = &res,
2676 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002677 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678
Trond Myklebust011fff72010-04-16 16:22:49 -04002679 res.old_fattr = nfs_alloc_fattr();
2680 res.new_fattr = nfs_alloc_fattr();
2681 if (res.old_fattr == NULL || res.new_fattr == NULL)
2682 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683
Trond Myklebust011fff72010-04-16 16:22:49 -04002684 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 if (!status) {
2686 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002687 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002689 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 }
Trond Myklebust011fff72010-04-16 16:22:49 -04002691out:
2692 nfs_free_fattr(res.new_fattr);
2693 nfs_free_fattr(res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 return status;
2695}
2696
2697static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2698 struct inode *new_dir, struct qstr *new_name)
2699{
2700 struct nfs4_exception exception = { };
2701 int err;
2702 do {
2703 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2704 _nfs4_proc_rename(old_dir, old_name,
2705 new_dir, new_name),
2706 &exception);
2707 } while (exception.retry);
2708 return err;
2709}
2710
2711static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2712{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002713 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 struct nfs4_link_arg arg = {
2715 .fh = NFS_FH(inode),
2716 .dir_fh = NFS_FH(dir),
2717 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002718 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002720 struct nfs4_link_res res = {
2721 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002722 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 struct rpc_message msg = {
2724 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2725 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002726 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 };
Trond Myklebust136f2622010-04-16 16:22:49 -04002728 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729
Trond Myklebust136f2622010-04-16 16:22:49 -04002730 res.fattr = nfs_alloc_fattr();
2731 res.dir_attr = nfs_alloc_fattr();
2732 if (res.fattr == NULL || res.dir_attr == NULL)
2733 goto out;
2734
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002735 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002736 if (!status) {
2737 update_changeattr(dir, &res.cinfo);
2738 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002739 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002740 }
Trond Myklebust136f2622010-04-16 16:22:49 -04002741out:
2742 nfs_free_fattr(res.dir_attr);
2743 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 return status;
2745}
2746
2747static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2748{
2749 struct nfs4_exception exception = { };
2750 int err;
2751 do {
2752 err = nfs4_handle_exception(NFS_SERVER(inode),
2753 _nfs4_proc_link(inode, dir, name),
2754 &exception);
2755 } while (exception.retry);
2756 return err;
2757}
2758
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002759struct nfs4_createdata {
2760 struct rpc_message msg;
2761 struct nfs4_create_arg arg;
2762 struct nfs4_create_res res;
2763 struct nfs_fh fh;
2764 struct nfs_fattr fattr;
2765 struct nfs_fattr dir_fattr;
2766};
2767
2768static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2769 struct qstr *name, struct iattr *sattr, u32 ftype)
2770{
2771 struct nfs4_createdata *data;
2772
2773 data = kzalloc(sizeof(*data), GFP_KERNEL);
2774 if (data != NULL) {
2775 struct nfs_server *server = NFS_SERVER(dir);
2776
2777 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2778 data->msg.rpc_argp = &data->arg;
2779 data->msg.rpc_resp = &data->res;
2780 data->arg.dir_fh = NFS_FH(dir);
2781 data->arg.server = server;
2782 data->arg.name = name;
2783 data->arg.attrs = sattr;
2784 data->arg.ftype = ftype;
2785 data->arg.bitmask = server->attr_bitmask;
2786 data->res.server = server;
2787 data->res.fh = &data->fh;
2788 data->res.fattr = &data->fattr;
2789 data->res.dir_fattr = &data->dir_fattr;
2790 nfs_fattr_init(data->res.fattr);
2791 nfs_fattr_init(data->res.dir_fattr);
2792 }
2793 return data;
2794}
2795
2796static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2797{
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002798 int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
2799 &data->arg, &data->res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002800 if (status == 0) {
2801 update_changeattr(dir, &data->res.dir_cinfo);
2802 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2803 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2804 }
2805 return status;
2806}
2807
2808static void nfs4_free_createdata(struct nfs4_createdata *data)
2809{
2810 kfree(data);
2811}
2812
Chuck Lever4f390c12006-08-22 20:06:22 -04002813static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002814 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002816 struct nfs4_createdata *data;
2817 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818
Chuck Lever94a6d752006-08-22 20:06:23 -04002819 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002820 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002821
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002822 status = -ENOMEM;
2823 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2824 if (data == NULL)
2825 goto out;
2826
2827 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2828 data->arg.u.symlink.pages = &page;
2829 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002831 status = nfs4_do_create(dir, dentry, data);
2832
2833 nfs4_free_createdata(data);
2834out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 return status;
2836}
2837
Chuck Lever4f390c12006-08-22 20:06:22 -04002838static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002839 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840{
2841 struct nfs4_exception exception = { };
2842 int err;
2843 do {
2844 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002845 _nfs4_proc_symlink(dir, dentry, page,
2846 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 &exception);
2848 } while (exception.retry);
2849 return err;
2850}
2851
2852static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2853 struct iattr *sattr)
2854{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002855 struct nfs4_createdata *data;
2856 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002858 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2859 if (data == NULL)
2860 goto out;
2861
2862 status = nfs4_do_create(dir, dentry, data);
2863
2864 nfs4_free_createdata(data);
2865out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 return status;
2867}
2868
2869static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2870 struct iattr *sattr)
2871{
2872 struct nfs4_exception exception = { };
2873 int err;
2874 do {
2875 err = nfs4_handle_exception(NFS_SERVER(dir),
2876 _nfs4_proc_mkdir(dir, dentry, sattr),
2877 &exception);
2878 } while (exception.retry);
2879 return err;
2880}
2881
2882static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2883 u64 cookie, struct page *page, unsigned int count, int plus)
2884{
2885 struct inode *dir = dentry->d_inode;
2886 struct nfs4_readdir_arg args = {
2887 .fh = NFS_FH(dir),
2888 .pages = &page,
2889 .pgbase = 0,
2890 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09002891 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 };
2893 struct nfs4_readdir_res res;
2894 struct rpc_message msg = {
2895 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2896 .rpc_argp = &args,
2897 .rpc_resp = &res,
2898 .rpc_cred = cred,
2899 };
2900 int status;
2901
Harvey Harrison3110ff82008-05-02 13:42:44 -07002902 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00002903 dentry->d_parent->d_name.name,
2904 dentry->d_name.name,
2905 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2907 res.pgbase = args.pgbase;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002908 status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 if (status == 0)
2910 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustc4812992007-09-28 17:11:45 -04002911
2912 nfs_invalidate_atime(dir);
2913
Harvey Harrison3110ff82008-05-02 13:42:44 -07002914 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 return status;
2916}
2917
2918static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2919 u64 cookie, struct page *page, unsigned int count, int plus)
2920{
2921 struct nfs4_exception exception = { };
2922 int err;
2923 do {
2924 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2925 _nfs4_proc_readdir(dentry, cred, cookie,
2926 page, count, plus),
2927 &exception);
2928 } while (exception.retry);
2929 return err;
2930}
2931
2932static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2933 struct iattr *sattr, dev_t rdev)
2934{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002935 struct nfs4_createdata *data;
2936 int mode = sattr->ia_mode;
2937 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938
2939 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2940 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002941
2942 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2943 if (data == NULL)
2944 goto out;
2945
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002947 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002949 data->arg.ftype = NF4BLK;
2950 data->arg.u.device.specdata1 = MAJOR(rdev);
2951 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 }
2953 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002954 data->arg.ftype = NF4CHR;
2955 data->arg.u.device.specdata1 = MAJOR(rdev);
2956 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002959 status = nfs4_do_create(dir, dentry, data);
2960
2961 nfs4_free_createdata(data);
2962out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 return status;
2964}
2965
2966static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2967 struct iattr *sattr, dev_t rdev)
2968{
2969 struct nfs4_exception exception = { };
2970 int err;
2971 do {
2972 err = nfs4_handle_exception(NFS_SERVER(dir),
2973 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2974 &exception);
2975 } while (exception.retry);
2976 return err;
2977}
2978
2979static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2980 struct nfs_fsstat *fsstat)
2981{
2982 struct nfs4_statfs_arg args = {
2983 .fh = fhandle,
2984 .bitmask = server->attr_bitmask,
2985 };
Benny Halevy24ad1482009-04-01 09:21:56 -04002986 struct nfs4_statfs_res res = {
2987 .fsstat = fsstat,
2988 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 struct rpc_message msg = {
2990 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2991 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04002992 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 };
2994
Trond Myklebust0e574af2005-10-27 22:12:38 -04002995 nfs_fattr_init(fsstat->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002996 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997}
2998
2999static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3000{
3001 struct nfs4_exception exception = { };
3002 int err;
3003 do {
3004 err = nfs4_handle_exception(server,
3005 _nfs4_proc_statfs(server, fhandle, fsstat),
3006 &exception);
3007 } while (exception.retry);
3008 return err;
3009}
3010
3011static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3012 struct nfs_fsinfo *fsinfo)
3013{
3014 struct nfs4_fsinfo_arg args = {
3015 .fh = fhandle,
3016 .bitmask = server->attr_bitmask,
3017 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003018 struct nfs4_fsinfo_res res = {
3019 .fsinfo = fsinfo,
3020 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 struct rpc_message msg = {
3022 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3023 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003024 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 };
3026
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003027 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028}
3029
3030static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3031{
3032 struct nfs4_exception exception = { };
3033 int err;
3034
3035 do {
3036 err = nfs4_handle_exception(server,
3037 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3038 &exception);
3039 } while (exception.retry);
3040 return err;
3041}
3042
3043static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3044{
Trond Myklebust0e574af2005-10-27 22:12:38 -04003045 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3047}
3048
3049static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3050 struct nfs_pathconf *pathconf)
3051{
3052 struct nfs4_pathconf_arg args = {
3053 .fh = fhandle,
3054 .bitmask = server->attr_bitmask,
3055 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003056 struct nfs4_pathconf_res res = {
3057 .pathconf = pathconf,
3058 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 struct rpc_message msg = {
3060 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3061 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003062 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 };
3064
3065 /* None of the pathconf attributes are mandatory to implement */
3066 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3067 memset(pathconf, 0, sizeof(*pathconf));
3068 return 0;
3069 }
3070
Trond Myklebust0e574af2005-10-27 22:12:38 -04003071 nfs_fattr_init(pathconf->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003072 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073}
3074
3075static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3076 struct nfs_pathconf *pathconf)
3077{
3078 struct nfs4_exception exception = { };
3079 int err;
3080
3081 do {
3082 err = nfs4_handle_exception(server,
3083 _nfs4_proc_pathconf(server, fhandle, pathconf),
3084 &exception);
3085 } while (exception.retry);
3086 return err;
3087}
3088
Trond Myklebustec06c092006-03-20 13:44:27 -05003089static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090{
Trond Myklebustec06c092006-03-20 13:44:27 -05003091 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092
Andy Adamsonf11c88a2009-04-01 09:22:25 -04003093 dprintk("--> %s\n", __func__);
3094
Andy Adamsonf11c88a2009-04-01 09:22:25 -04003095 nfs4_sequence_done(server, &data->res.seq_res, task->tk_status);
3096
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003097 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003098 nfs_restart_rpc(task, server->nfs_client);
Trond Myklebustec06c092006-03-20 13:44:27 -05003099 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003101
3102 nfs_invalidate_atime(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003104 renew_lease(server, data->timestamp);
3105 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106}
3107
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003108static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 data->timestamp = jiffies;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003111 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112}
3113
Trond Myklebust788e7a82006-03-20 13:44:27 -05003114static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 struct inode *inode = data->inode;
3117
Andy Adamsondef6ed72009-04-01 09:22:26 -04003118 nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
3119 task->tk_status);
3120
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003121 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003122 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003123 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003125 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003127 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003128 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003129 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130}
3131
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003132static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003134 struct nfs_server *server = NFS_SERVER(data->inode);
3135
Trond Myklebusta65318b2009-03-11 14:10:28 -04003136 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003137 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 data->timestamp = jiffies;
3139
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003140 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141}
3142
Trond Myklebust788e7a82006-03-20 13:44:27 -05003143static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 struct inode *inode = data->inode;
3146
Andy Adamson21d9a852009-04-01 09:22:27 -04003147 nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
3148 task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003149 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003150 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003151 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003153 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003154 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155}
3156
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003157static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003159 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160
Trond Myklebusta65318b2009-03-11 14:10:28 -04003161 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003162 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003163 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164}
3165
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003166struct nfs4_renewdata {
3167 struct nfs_client *client;
3168 unsigned long timestamp;
3169};
3170
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171/*
3172 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3173 * standalone procedure for queueing an asynchronous RENEW.
3174 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003175static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003176{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003177 struct nfs4_renewdata *data = calldata;
3178 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003179
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003180 if (atomic_read(&clp->cl_count) > 1)
3181 nfs4_schedule_state_renewal(clp);
3182 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003183 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003184}
3185
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003186static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003188 struct nfs4_renewdata *data = calldata;
3189 struct nfs_client *clp = data->client;
3190 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191
3192 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003193 /* Unless we're shutting down, schedule state recovery! */
3194 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
3195 nfs4_schedule_state_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 return;
3197 }
3198 spin_lock(&clp->cl_lock);
3199 if (time_before(clp->cl_last_renewal,timestamp))
3200 clp->cl_last_renewal = timestamp;
3201 spin_unlock(&clp->cl_lock);
3202}
3203
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003204static const struct rpc_call_ops nfs4_renew_ops = {
3205 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003206 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003207};
3208
David Howellsadfa6f92006-08-22 20:06:08 -04003209int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210{
3211 struct rpc_message msg = {
3212 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3213 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003214 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003216 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003218 if (!atomic_inc_not_zero(&clp->cl_count))
3219 return -EIO;
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003220 data = kmalloc(sizeof(*data), GFP_KERNEL);
3221 if (data == NULL)
3222 return -ENOMEM;
3223 data->client = clp;
3224 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003226 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227}
3228
David Howellsadfa6f92006-08-22 20:06:08 -04003229int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230{
3231 struct rpc_message msg = {
3232 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3233 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003234 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 };
3236 unsigned long now = jiffies;
3237 int status;
3238
3239 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3240 if (status < 0)
3241 return status;
3242 spin_lock(&clp->cl_lock);
3243 if (time_before(clp->cl_last_renewal,now))
3244 clp->cl_last_renewal = now;
3245 spin_unlock(&clp->cl_lock);
3246 return 0;
3247}
3248
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003249static inline int nfs4_server_supports_acls(struct nfs_server *server)
3250{
3251 return (server->caps & NFS_CAP_ACLS)
3252 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3253 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3254}
3255
3256/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3257 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3258 * the stack.
3259 */
3260#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3261
3262static void buf_to_pages(const void *buf, size_t buflen,
3263 struct page **pages, unsigned int *pgbase)
3264{
3265 const void *p = buf;
3266
3267 *pgbase = offset_in_page(buf);
3268 p -= *pgbase;
3269 while (p < buf + buflen) {
3270 *(pages++) = virt_to_page(p);
3271 p += PAGE_CACHE_SIZE;
3272 }
3273}
3274
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003275struct nfs4_cached_acl {
3276 int cached;
3277 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003278 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003279};
3280
3281static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003282{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003283 struct nfs_inode *nfsi = NFS_I(inode);
3284
3285 spin_lock(&inode->i_lock);
3286 kfree(nfsi->nfs4_acl);
3287 nfsi->nfs4_acl = acl;
3288 spin_unlock(&inode->i_lock);
3289}
3290
3291static void nfs4_zap_acl_attr(struct inode *inode)
3292{
3293 nfs4_set_cached_acl(inode, NULL);
3294}
3295
3296static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3297{
3298 struct nfs_inode *nfsi = NFS_I(inode);
3299 struct nfs4_cached_acl *acl;
3300 int ret = -ENOENT;
3301
3302 spin_lock(&inode->i_lock);
3303 acl = nfsi->nfs4_acl;
3304 if (acl == NULL)
3305 goto out;
3306 if (buf == NULL) /* user is just asking for length */
3307 goto out_len;
3308 if (acl->cached == 0)
3309 goto out;
3310 ret = -ERANGE; /* see getxattr(2) man page */
3311 if (acl->len > buflen)
3312 goto out;
3313 memcpy(buf, acl->data, acl->len);
3314out_len:
3315 ret = acl->len;
3316out:
3317 spin_unlock(&inode->i_lock);
3318 return ret;
3319}
3320
3321static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3322{
3323 struct nfs4_cached_acl *acl;
3324
3325 if (buf && acl_len <= PAGE_SIZE) {
3326 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3327 if (acl == NULL)
3328 goto out;
3329 acl->cached = 1;
3330 memcpy(acl->data, buf, acl_len);
3331 } else {
3332 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3333 if (acl == NULL)
3334 goto out;
3335 acl->cached = 0;
3336 }
3337 acl->len = acl_len;
3338out:
3339 nfs4_set_cached_acl(inode, acl);
3340}
3341
Trond Myklebust16b42892006-08-24 12:27:15 -04003342static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003343{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003344 struct page *pages[NFS4ACL_MAXPAGES];
3345 struct nfs_getaclargs args = {
3346 .fh = NFS_FH(inode),
3347 .acl_pages = pages,
3348 .acl_len = buflen,
3349 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003350 struct nfs_getaclres res = {
3351 .acl_len = buflen,
3352 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003353 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003354 struct rpc_message msg = {
3355 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3356 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003357 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003358 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003359 struct page *localpage = NULL;
3360 int ret;
3361
3362 if (buflen < PAGE_SIZE) {
3363 /* As long as we're doing a round trip to the server anyway,
3364 * let's be prepared for a page of acl data. */
3365 localpage = alloc_page(GFP_KERNEL);
3366 resp_buf = page_address(localpage);
3367 if (localpage == NULL)
3368 return -ENOMEM;
3369 args.acl_pages[0] = localpage;
3370 args.acl_pgbase = 0;
Benny Halevy663c79b2009-04-01 09:21:59 -04003371 args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003372 } else {
3373 resp_buf = buf;
3374 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3375 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003376 ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003377 if (ret)
3378 goto out_free;
Benny Halevy663c79b2009-04-01 09:21:59 -04003379 if (res.acl_len > args.acl_len)
3380 nfs4_write_cached_acl(inode, NULL, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003381 else
Benny Halevy663c79b2009-04-01 09:21:59 -04003382 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003383 if (buf) {
3384 ret = -ERANGE;
Benny Halevy663c79b2009-04-01 09:21:59 -04003385 if (res.acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003386 goto out_free;
3387 if (localpage)
Benny Halevy663c79b2009-04-01 09:21:59 -04003388 memcpy(buf, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003389 }
Benny Halevy663c79b2009-04-01 09:21:59 -04003390 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003391out_free:
3392 if (localpage)
3393 __free_page(localpage);
3394 return ret;
3395}
3396
Trond Myklebust16b42892006-08-24 12:27:15 -04003397static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3398{
3399 struct nfs4_exception exception = { };
3400 ssize_t ret;
3401 do {
3402 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3403 if (ret >= 0)
3404 break;
3405 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3406 } while (exception.retry);
3407 return ret;
3408}
3409
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003410static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3411{
3412 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003413 int ret;
3414
3415 if (!nfs4_server_supports_acls(server))
3416 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003417 ret = nfs_revalidate_inode(server, inode);
3418 if (ret < 0)
3419 return ret;
3420 ret = nfs4_read_cached_acl(inode, buf, buflen);
3421 if (ret != -ENOENT)
3422 return ret;
3423 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003424}
3425
Trond Myklebust16b42892006-08-24 12:27:15 -04003426static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003427{
3428 struct nfs_server *server = NFS_SERVER(inode);
3429 struct page *pages[NFS4ACL_MAXPAGES];
3430 struct nfs_setaclargs arg = {
3431 .fh = NFS_FH(inode),
3432 .acl_pages = pages,
3433 .acl_len = buflen,
3434 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003435 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003436 struct rpc_message msg = {
3437 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3438 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003439 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003440 };
3441 int ret;
3442
3443 if (!nfs4_server_supports_acls(server))
3444 return -EOPNOTSUPP;
Trond Myklebust642ac542005-10-18 14:20:19 -07003445 nfs_inode_return_delegation(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003446 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003447 ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003448 nfs_access_zap_cache(inode);
3449 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003450 return ret;
3451}
3452
Trond Myklebust16b42892006-08-24 12:27:15 -04003453static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3454{
3455 struct nfs4_exception exception = { };
3456 int err;
3457 do {
3458 err = nfs4_handle_exception(NFS_SERVER(inode),
3459 __nfs4_proc_set_acl(inode, buf, buflen),
3460 &exception);
3461 } while (exception.retry);
3462 return err;
3463}
3464
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003466nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003468 struct nfs_client *clp = server->nfs_client;
3469
3470 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 return 0;
3472 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003473 case -NFS4ERR_ADMIN_REVOKED:
3474 case -NFS4ERR_BAD_STATEID:
3475 case -NFS4ERR_OPENMODE:
3476 if (state == NULL)
3477 break;
3478 nfs4_state_mark_reclaim_nograce(clp, state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003479 goto do_state_recovery;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003481 if (state == NULL)
3482 break;
3483 nfs4_state_mark_reclaim_reboot(clp, state);
3484 case -NFS4ERR_STALE_CLIENTID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 case -NFS4ERR_EXPIRED:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003486 goto do_state_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003487#if defined(CONFIG_NFS_V4_1)
3488 case -NFS4ERR_BADSESSION:
3489 case -NFS4ERR_BADSLOT:
3490 case -NFS4ERR_BAD_HIGH_SLOT:
3491 case -NFS4ERR_DEADSESSION:
3492 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3493 case -NFS4ERR_SEQ_FALSE_RETRY:
3494 case -NFS4ERR_SEQ_MISORDERED:
3495 dprintk("%s ERROR %d, Reset session\n", __func__,
3496 task->tk_status);
Andy Adamson0b9e2d42009-12-04 16:02:14 -05003497 nfs4_schedule_state_recovery(clp);
Andy Adamson4745e312009-04-01 09:22:42 -04003498 task->tk_status = 0;
3499 return -EAGAIN;
3500#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003502 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003503 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003504 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3506 task->tk_status = 0;
3507 return -EAGAIN;
3508 case -NFS4ERR_OLD_STATEID:
3509 task->tk_status = 0;
3510 return -EAGAIN;
3511 }
3512 task->tk_status = nfs4_map_errors(task->tk_status);
3513 return 0;
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003514do_state_recovery:
3515 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
3516 nfs4_schedule_state_recovery(clp);
3517 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3518 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3519 task->tk_status = 0;
3520 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521}
3522
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003523int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3524 unsigned short port, struct rpc_cred *cred,
3525 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526{
3527 nfs4_verifier sc_verifier;
3528 struct nfs4_setclientid setclientid = {
3529 .sc_verifier = &sc_verifier,
3530 .sc_prog = program,
3531 };
3532 struct rpc_message msg = {
3533 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3534 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003535 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003536 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 };
Al Virobc4785c2006-10-19 23:28:51 -07003538 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539 int loop = 0;
3540 int status;
3541
Al Virobc4785c2006-10-19 23:28:51 -07003542 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3544 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3545
3546 for(;;) {
3547 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003548 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003549 clp->cl_ipaddr,
3550 rpc_peeraddr2str(clp->cl_rpcclient,
3551 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003552 rpc_peeraddr2str(clp->cl_rpcclient,
3553 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003554 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 clp->cl_id_uniquifier);
3556 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003557 sizeof(setclientid.sc_netid),
3558 rpc_peeraddr2str(clp->cl_rpcclient,
3559 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003561 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 clp->cl_ipaddr, port >> 8, port & 255);
3563
3564 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3565 if (status != -NFS4ERR_CLID_INUSE)
3566 break;
3567 if (signalled())
3568 break;
3569 if (loop++ & 1)
3570 ssleep(clp->cl_lease_time + 1);
3571 else
3572 if (++clp->cl_id_uniquifier == 0)
3573 break;
3574 }
3575 return status;
3576}
3577
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003578static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3579 struct nfs4_setclientid_res *arg,
3580 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581{
3582 struct nfs_fsinfo fsinfo;
3583 struct rpc_message msg = {
3584 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003585 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003587 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 };
3589 unsigned long now;
3590 int status;
3591
3592 now = jiffies;
3593 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3594 if (status == 0) {
3595 spin_lock(&clp->cl_lock);
3596 clp->cl_lease_time = fsinfo.lease_time * HZ;
3597 clp->cl_last_renewal = now;
3598 spin_unlock(&clp->cl_lock);
3599 }
3600 return status;
3601}
3602
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003603int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3604 struct nfs4_setclientid_res *arg,
3605 struct rpc_cred *cred)
Trond Myklebust51581f32006-03-20 13:44:47 -05003606{
Benny Halevy77e03672008-06-24 20:25:57 +03003607 long timeout = 0;
Trond Myklebust51581f32006-03-20 13:44:47 -05003608 int err;
3609 do {
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003610 err = _nfs4_proc_setclientid_confirm(clp, arg, cred);
Trond Myklebust51581f32006-03-20 13:44:47 -05003611 switch (err) {
3612 case 0:
3613 return err;
3614 case -NFS4ERR_RESOURCE:
3615 /* The IBM lawyers misread another document! */
3616 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -05003617 case -EKEYEXPIRED:
Trond Myklebust51581f32006-03-20 13:44:47 -05003618 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3619 }
3620 } while (err == 0);
3621 return err;
3622}
3623
Trond Myklebustfe650402006-01-03 09:55:18 +01003624struct nfs4_delegreturndata {
3625 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003626 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003627 struct nfs_fh fh;
3628 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003629 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003630 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003631 int rpc_status;
3632};
3633
Trond Myklebustfe650402006-01-03 09:55:18 +01003634static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3635{
3636 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003637
Trond Myklebustd61e6122009-12-05 19:32:19 -05003638 nfs4_sequence_done(data->res.server, &data->res.seq_res,
3639 task->tk_status);
Andy Adamson938e1012009-04-01 09:22:28 -04003640
Ricardo Labiaga79708862009-12-07 09:23:21 -05003641 switch (task->tk_status) {
3642 case -NFS4ERR_STALE_STATEID:
3643 case -NFS4ERR_EXPIRED:
3644 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01003645 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003646 break;
3647 default:
3648 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3649 -EAGAIN) {
3650 nfs_restart_rpc(task, data->res.server->nfs_client);
3651 return;
3652 }
3653 }
3654 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01003655}
3656
3657static void nfs4_delegreturn_release(void *calldata)
3658{
Trond Myklebustfe650402006-01-03 09:55:18 +01003659 kfree(calldata);
3660}
3661
Andy Adamson938e1012009-04-01 09:22:28 -04003662#if defined(CONFIG_NFS_V4_1)
3663static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3664{
3665 struct nfs4_delegreturndata *d_data;
3666
3667 d_data = (struct nfs4_delegreturndata *)data;
3668
Trond Myklebust035168ab2010-06-16 09:52:26 -04003669 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04003670 &d_data->args.seq_args,
3671 &d_data->res.seq_res, 1, task))
3672 return;
3673 rpc_call_start(task);
3674}
3675#endif /* CONFIG_NFS_V4_1 */
3676
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003677static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04003678#if defined(CONFIG_NFS_V4_1)
3679 .rpc_call_prepare = nfs4_delegreturn_prepare,
3680#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01003681 .rpc_call_done = nfs4_delegreturn_done,
3682 .rpc_release = nfs4_delegreturn_release,
3683};
3684
Trond Myklebuste6f81072008-01-24 18:14:34 -05003685static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003686{
3687 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003688 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003689 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003690 struct rpc_message msg = {
3691 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3692 .rpc_cred = cred,
3693 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003694 struct rpc_task_setup task_setup_data = {
3695 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003696 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003697 .callback_ops = &nfs4_delegreturn_ops,
3698 .flags = RPC_TASK_ASYNC,
3699 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003700 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003701
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003702 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01003703 if (data == NULL)
3704 return -ENOMEM;
3705 data->args.fhandle = &data->fh;
3706 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003707 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003708 nfs_copy_fh(&data->fh, NFS_FH(inode));
3709 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003710 data->res.fattr = &data->fattr;
3711 data->res.server = server;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003712 data->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003713 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003714 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003715 data->rpc_status = 0;
3716
Trond Myklebustc970aa82007-07-14 15:39:59 -04003717 task_setup_data.callback_data = data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003718 msg.rpc_argp = &data->args,
3719 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003720 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003721 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003722 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003723 if (!issync)
3724 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003725 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003726 if (status != 0)
3727 goto out;
3728 status = data->rpc_status;
3729 if (status != 0)
3730 goto out;
3731 nfs_refresh_inode(inode, &data->fattr);
3732out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003733 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003734 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735}
3736
Trond Myklebuste6f81072008-01-24 18:14:34 -05003737int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738{
3739 struct nfs_server *server = NFS_SERVER(inode);
3740 struct nfs4_exception exception = { };
3741 int err;
3742 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003743 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744 switch (err) {
3745 case -NFS4ERR_STALE_STATEID:
3746 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747 case 0:
3748 return 0;
3749 }
3750 err = nfs4_handle_exception(server, err, &exception);
3751 } while (exception.retry);
3752 return err;
3753}
3754
3755#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3756#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3757
3758/*
3759 * sleep, with exponential backoff, and retry the LOCK operation.
3760 */
3761static unsigned long
3762nfs4_set_lock_task_retry(unsigned long timeout)
3763{
Matthew Wilcox150030b2007-12-06 16:24:39 -05003764 schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 timeout <<= 1;
3766 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3767 return NFS4_LOCK_MAXTIMEOUT;
3768 return timeout;
3769}
3770
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3772{
3773 struct inode *inode = state->inode;
3774 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003775 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003776 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003778 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003780 struct nfs_lockt_res res = {
3781 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 };
3783 struct rpc_message msg = {
3784 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3785 .rpc_argp = &arg,
3786 .rpc_resp = &res,
3787 .rpc_cred = state->owner->so_cred,
3788 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 struct nfs4_lock_state *lsp;
3790 int status;
3791
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003792 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003793 status = nfs4_set_lock_state(state, request);
3794 if (status != 0)
3795 goto out;
3796 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003797 arg.lock_owner.id = lsp->ls_id.id;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003798 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003799 switch (status) {
3800 case 0:
3801 request->fl_type = F_UNLCK;
3802 break;
3803 case -NFS4ERR_DENIED:
3804 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05003806 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003807out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 return status;
3809}
3810
3811static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3812{
3813 struct nfs4_exception exception = { };
3814 int err;
3815
3816 do {
3817 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3818 _nfs4_proc_getlk(state, cmd, request),
3819 &exception);
3820 } while (exception.retry);
3821 return err;
3822}
3823
3824static int do_vfs_lock(struct file *file, struct file_lock *fl)
3825{
3826 int res = 0;
3827 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3828 case FL_POSIX:
3829 res = posix_lock_file_wait(file, fl);
3830 break;
3831 case FL_FLOCK:
3832 res = flock_lock_file_wait(file, fl);
3833 break;
3834 default:
3835 BUG();
3836 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837 return res;
3838}
3839
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003840struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003841 struct nfs_locku_args arg;
3842 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003843 struct nfs4_lock_state *lsp;
3844 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003845 struct file_lock fl;
3846 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003847 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003848};
3849
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003850static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3851 struct nfs_open_context *ctx,
3852 struct nfs4_lock_state *lsp,
3853 struct nfs_seqid *seqid)
3854{
3855 struct nfs4_unlockdata *p;
3856 struct inode *inode = lsp->ls_state->inode;
3857
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003858 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003859 if (p == NULL)
3860 return NULL;
3861 p->arg.fh = NFS_FH(inode);
3862 p->arg.fl = &p->fl;
3863 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003864 p->res.seqid = seqid;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003865 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003866 p->arg.stateid = &lsp->ls_stateid;
3867 p->lsp = lsp;
3868 atomic_inc(&lsp->ls_count);
3869 /* Ensure we don't close file until we're done freeing locks! */
3870 p->ctx = get_nfs_open_context(ctx);
3871 memcpy(&p->fl, fl, sizeof(p->fl));
3872 p->server = NFS_SERVER(inode);
3873 return p;
3874}
3875
Trond Myklebust06f814a2006-01-03 09:55:07 +01003876static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003877{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003878 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003879 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003880 nfs4_put_lock_state(calldata->lsp);
3881 put_nfs_open_context(calldata->ctx);
3882 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003883}
3884
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003885static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003886{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003887 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003888
Andy Adamsona8936932009-04-01 09:22:23 -04003889 nfs4_sequence_done(calldata->server, &calldata->res.seq_res,
3890 task->tk_status);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003891 if (RPC_ASSASSINATED(task))
3892 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003893 switch (task->tk_status) {
3894 case 0:
3895 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003896 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003897 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01003898 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003899 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003900 case -NFS4ERR_BAD_STATEID:
3901 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003902 case -NFS4ERR_STALE_STATEID:
3903 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003904 break;
3905 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003906 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebust0110ee12009-12-07 09:00:24 -05003907 nfs_restart_rpc(task,
Trond Myklebustd61e6122009-12-05 19:32:19 -05003908 calldata->server->nfs_client);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003909 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003910}
3911
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003912static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003913{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003914 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003916 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003917 return;
3918 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003919 /* Note: exit _without_ running nfs4_locku_done */
3920 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003921 return;
3922 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01003923 calldata->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04003924 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04003925 &calldata->arg.seq_args,
3926 &calldata->res.seq_res, 1, task))
3927 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003928 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929}
3930
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003931static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003932 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003933 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01003934 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003935};
3936
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003937static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3938 struct nfs_open_context *ctx,
3939 struct nfs4_lock_state *lsp,
3940 struct nfs_seqid *seqid)
3941{
3942 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003943 struct rpc_message msg = {
3944 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3945 .rpc_cred = ctx->cred,
3946 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003947 struct rpc_task_setup task_setup_data = {
3948 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04003949 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003950 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003951 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003952 .flags = RPC_TASK_ASYNC,
3953 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003954
Frank Filz137d6ac2007-07-09 15:32:29 -07003955 /* Ensure this is an unlock - when canceling a lock, the
3956 * canceled lock is passed in, and it won't be an unlock.
3957 */
3958 fl->fl_type = F_UNLCK;
3959
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003960 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3961 if (data == NULL) {
3962 nfs_free_seqid(seqid);
3963 return ERR_PTR(-ENOMEM);
3964 }
3965
Trond Myklebust5138fde2007-07-14 15:40:01 -04003966 msg.rpc_argp = &data->arg,
3967 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003968 task_setup_data.callback_data = data;
3969 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003970}
3971
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3973{
Trond Myklebust19e03c52008-12-23 15:21:44 -05003974 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003975 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003976 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01003977 struct rpc_task *task;
3978 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003979 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980
Trond Myklebust9b073572006-06-29 16:38:34 -04003981 status = nfs4_set_lock_state(state, request);
3982 /* Unlock _before_ we do the RPC call */
3983 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003984 down_read(&nfsi->rwsem);
3985 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
3986 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003987 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003988 }
3989 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003990 if (status != 0)
3991 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003992 /* Is this a delegated lock? */
3993 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04003994 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003995 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003996 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04003997 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003998 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04003999 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004000 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004001 status = PTR_ERR(task);
4002 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004003 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004004 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004005 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004006out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004007 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004008 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009}
4010
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004011struct nfs4_lockdata {
4012 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004013 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004014 struct nfs4_lock_state *lsp;
4015 struct nfs_open_context *ctx;
4016 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004017 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004018 int rpc_status;
4019 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004020 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004021};
4022
4023static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004024 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4025 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004026{
4027 struct nfs4_lockdata *p;
4028 struct inode *inode = lsp->ls_state->inode;
4029 struct nfs_server *server = NFS_SERVER(inode);
4030
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004031 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004032 if (p == NULL)
4033 return NULL;
4034
4035 p->arg.fh = NFS_FH(inode);
4036 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004037 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004038 if (p->arg.open_seqid == NULL)
4039 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004040 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004041 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004042 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004043 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004044 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04004045 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004046 p->res.lock_seqid = p->arg.lock_seqid;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04004047 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004048 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004049 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004050 atomic_inc(&lsp->ls_count);
4051 p->ctx = get_nfs_open_context(ctx);
4052 memcpy(&p->fl, fl, sizeof(p->fl));
4053 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004054out_free_seqid:
4055 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004056out_free:
4057 kfree(p);
4058 return NULL;
4059}
4060
4061static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4062{
4063 struct nfs4_lockdata *data = calldata;
4064 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065
Harvey Harrison3110ff82008-05-02 13:42:44 -07004066 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004067 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4068 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004069 /* Do we need to do an open_to_lock_owner? */
4070 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004071 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4072 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004073 data->arg.open_stateid = &state->stateid;
4074 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004075 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004076 } else
4077 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004078 data->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04004079 if (nfs4_setup_sequence(data->server,
4080 &data->arg.seq_args,
Andy Adamson66179ef2009-04-01 09:22:22 -04004081 &data->res.seq_res, 1, task))
4082 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004083 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004084 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004085}
4086
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004087static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4088{
4089 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4090 nfs4_lock_prepare(task, calldata);
4091}
4092
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004093static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4094{
4095 struct nfs4_lockdata *data = calldata;
4096
Harvey Harrison3110ff82008-05-02 13:42:44 -07004097 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004098
Trond Myklebustd61e6122009-12-05 19:32:19 -05004099 nfs4_sequence_done(data->server, &data->res.seq_res,
4100 task->tk_status);
Andy Adamson66179ef2009-04-01 09:22:22 -04004101
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004102 data->rpc_status = task->tk_status;
4103 if (RPC_ASSASSINATED(task))
4104 goto out;
4105 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004106 if (data->rpc_status == 0)
4107 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4108 else
4109 goto out;
4110 }
4111 if (data->rpc_status == 0) {
4112 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4113 sizeof(data->lsp->ls_stateid.data));
4114 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Trond Myklebust88be9f92007-06-05 10:42:27 -04004115 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004116 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004117out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004118 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004119}
4120
4121static void nfs4_lock_release(void *calldata)
4122{
4123 struct nfs4_lockdata *data = calldata;
4124
Harvey Harrison3110ff82008-05-02 13:42:44 -07004125 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004126 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004127 if (data->cancelled != 0) {
4128 struct rpc_task *task;
4129 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4130 data->arg.lock_seqid);
4131 if (!IS_ERR(task))
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004132 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004133 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004134 } else
4135 nfs_free_seqid(data->arg.lock_seqid);
4136 nfs4_put_lock_state(data->lsp);
4137 put_nfs_open_context(data->ctx);
4138 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004139 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004140}
4141
4142static const struct rpc_call_ops nfs4_lock_ops = {
4143 .rpc_call_prepare = nfs4_lock_prepare,
4144 .rpc_call_done = nfs4_lock_done,
4145 .rpc_release = nfs4_lock_release,
4146};
4147
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004148static const struct rpc_call_ops nfs4_recover_lock_ops = {
4149 .rpc_call_prepare = nfs4_recover_lock_prepare,
4150 .rpc_call_done = nfs4_lock_done,
4151 .rpc_release = nfs4_lock_release,
4152};
4153
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004154static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4155{
4156 struct nfs_client *clp = server->nfs_client;
4157 struct nfs4_state *state = lsp->ls_state;
4158
4159 switch (error) {
4160 case -NFS4ERR_ADMIN_REVOKED:
4161 case -NFS4ERR_BAD_STATEID:
4162 case -NFS4ERR_EXPIRED:
4163 if (new_lock_owner != 0 ||
4164 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4165 nfs4_state_mark_reclaim_nograce(clp, state);
4166 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004167 break;
4168 case -NFS4ERR_STALE_STATEID:
4169 if (new_lock_owner != 0 ||
4170 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4171 nfs4_state_mark_reclaim_reboot(clp, state);
4172 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004173 };
4174}
4175
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004176static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004177{
4178 struct nfs4_lockdata *data;
4179 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004180 struct rpc_message msg = {
4181 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4182 .rpc_cred = state->owner->so_cred,
4183 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004184 struct rpc_task_setup task_setup_data = {
4185 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004186 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004187 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004188 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004189 .flags = RPC_TASK_ASYNC,
4190 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004191 int ret;
4192
Harvey Harrison3110ff82008-05-02 13:42:44 -07004193 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004194 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004195 fl->fl_u.nfs4_fl.owner,
4196 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004197 if (data == NULL)
4198 return -ENOMEM;
4199 if (IS_SETLKW(cmd))
4200 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004201 if (recovery_type > NFS_LOCK_NEW) {
4202 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004203 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004204 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4205 }
Trond Myklebust5138fde2007-07-14 15:40:01 -04004206 msg.rpc_argp = &data->arg,
4207 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004208 task_setup_data.callback_data = data;
4209 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004210 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004211 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004212 ret = nfs4_wait_for_completion_rpc_task(task);
4213 if (ret == 0) {
4214 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004215 if (ret)
4216 nfs4_handle_setlk_error(data->server, data->lsp,
4217 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004218 } else
4219 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004220 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004221 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004222 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223}
4224
4225static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4226{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004227 struct nfs_server *server = NFS_SERVER(state->inode);
4228 struct nfs4_exception exception = { };
4229 int err;
4230
4231 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004232 /* Cache the lock if possible... */
4233 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4234 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004235 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Jeff Layton2c643482010-01-07 09:42:03 -05004236 if (err != -NFS4ERR_DELAY && err != -EKEYEXPIRED)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004237 break;
4238 nfs4_handle_exception(server, err, &exception);
4239 } while (exception.retry);
4240 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241}
4242
4243static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4244{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004245 struct nfs_server *server = NFS_SERVER(state->inode);
4246 struct nfs4_exception exception = { };
4247 int err;
4248
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004249 err = nfs4_set_lock_state(state, request);
4250 if (err != 0)
4251 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004252 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004253 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4254 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004255 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004256 switch (err) {
4257 default:
4258 goto out;
4259 case -NFS4ERR_GRACE:
4260 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -05004261 case -EKEYEXPIRED:
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004262 nfs4_handle_exception(server, err, &exception);
4263 err = 0;
4264 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004265 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004266out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004267 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268}
4269
4270static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4271{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004272 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004273 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004274 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004276 if ((fl_flags & FL_POSIX) &&
4277 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4278 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004279 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004280 status = nfs4_set_lock_state(state, request);
4281 if (status != 0)
4282 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004283 request->fl_flags |= FL_ACCESS;
4284 status = do_vfs_lock(request->fl_file, request);
4285 if (status < 0)
4286 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004287 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004288 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004289 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004290 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004291 request->fl_flags = fl_flags & ~FL_SLEEP;
4292 status = do_vfs_lock(request->fl_file, request);
4293 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004294 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004295 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004296 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004297 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004298 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004299 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004300 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07004301 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004302out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004303 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004304out:
4305 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306 return status;
4307}
4308
4309static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4310{
4311 struct nfs4_exception exception = { };
4312 int err;
4313
4314 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004315 err = _nfs4_proc_setlk(state, cmd, request);
4316 if (err == -NFS4ERR_DENIED)
4317 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004319 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320 } while (exception.retry);
4321 return err;
4322}
4323
4324static int
4325nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4326{
4327 struct nfs_open_context *ctx;
4328 struct nfs4_state *state;
4329 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4330 int status;
4331
4332 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004333 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334 state = ctx->state;
4335
4336 if (request->fl_start < 0 || request->fl_end < 0)
4337 return -EINVAL;
4338
Trond Myklebustd9531262009-07-21 19:22:38 -04004339 if (IS_GETLK(cmd)) {
4340 if (state != NULL)
4341 return nfs4_proc_getlk(state, F_GETLK, request);
4342 return 0;
4343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344
4345 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4346 return -EINVAL;
4347
Trond Myklebustd9531262009-07-21 19:22:38 -04004348 if (request->fl_type == F_UNLCK) {
4349 if (state != NULL)
4350 return nfs4_proc_unlck(state, cmd, request);
4351 return 0;
4352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353
Trond Myklebustd9531262009-07-21 19:22:38 -04004354 if (state == NULL)
4355 return -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356 do {
4357 status = nfs4_proc_setlk(state, cmd, request);
4358 if ((status != -EAGAIN) || IS_SETLK(cmd))
4359 break;
4360 timeout = nfs4_set_lock_task_retry(timeout);
4361 status = -ERESTARTSYS;
4362 if (signalled())
4363 break;
4364 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365 return status;
4366}
4367
Trond Myklebust888e6942005-11-04 15:38:11 -05004368int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4369{
4370 struct nfs_server *server = NFS_SERVER(state->inode);
4371 struct nfs4_exception exception = { };
4372 int err;
4373
4374 err = nfs4_set_lock_state(state, fl);
4375 if (err != 0)
4376 goto out;
4377 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004378 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004379 switch (err) {
4380 default:
4381 printk(KERN_ERR "%s: unhandled error %d.\n",
4382 __func__, err);
4383 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004384 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004385 goto out;
4386 case -NFS4ERR_EXPIRED:
4387 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004388 case -NFS4ERR_STALE_STATEID:
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004389 case -NFS4ERR_BADSESSION:
4390 case -NFS4ERR_BADSLOT:
4391 case -NFS4ERR_BAD_HIGH_SLOT:
4392 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4393 case -NFS4ERR_DEADSESSION:
Trond Myklebustd5122202009-06-17 13:22:58 -07004394 nfs4_schedule_state_recovery(server->nfs_client);
4395 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004396 case -ERESTARTSYS:
4397 /*
4398 * The show must go on: exit, but mark the
4399 * stateid as needing recovery.
4400 */
4401 case -NFS4ERR_ADMIN_REVOKED:
4402 case -NFS4ERR_BAD_STATEID:
4403 case -NFS4ERR_OPENMODE:
4404 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
4405 err = 0;
4406 goto out;
4407 case -ENOMEM:
4408 case -NFS4ERR_DENIED:
4409 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4410 err = 0;
4411 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004412 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -05004413 case -EKEYEXPIRED:
Trond Myklebustd5122202009-06-17 13:22:58 -07004414 break;
4415 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004416 err = nfs4_handle_exception(server, err, &exception);
4417 } while (exception.retry);
4418out:
4419 return err;
4420}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004421
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004422#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4423
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004424int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
4425 size_t buflen, int flags)
4426{
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004427 struct inode *inode = dentry->d_inode;
4428
4429 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4430 return -EOPNOTSUPP;
4431
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004432 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004433}
4434
4435/* The getxattr man page suggests returning -ENODATA for unknown attributes,
4436 * and that's what we'll do for e.g. user attributes that haven't been set.
4437 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
4438 * attributes in kernel-managed attribute namespaces. */
4439ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
4440 size_t buflen)
4441{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004442 struct inode *inode = dentry->d_inode;
4443
4444 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4445 return -EOPNOTSUPP;
4446
4447 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004448}
4449
4450ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
4451{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004452 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004453
J. Bruce Fields096455a2006-03-20 23:23:42 -05004454 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4455 return 0;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004456 if (buf && buflen < len)
4457 return -ERANGE;
4458 if (buf)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004459 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
4460 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004461}
4462
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004463static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4464{
4465 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
4466 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4467 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4468 return;
4469
4470 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4471 NFS_ATTR_FATTR_NLINK;
4472 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4473 fattr->nlink = 2;
4474}
4475
Trond Myklebust56659e92007-07-17 21:52:39 -04004476int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004477 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004478{
4479 struct nfs_server *server = NFS_SERVER(dir);
4480 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004481 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4482 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004483 };
4484 struct nfs4_fs_locations_arg args = {
4485 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004486 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004487 .page = page,
4488 .bitmask = bitmask,
4489 };
Benny Halevy22958462009-04-01 09:22:02 -04004490 struct nfs4_fs_locations_res res = {
4491 .fs_locations = fs_locations,
4492 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004493 struct rpc_message msg = {
4494 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4495 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004496 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004497 };
4498 int status;
4499
Harvey Harrison3110ff82008-05-02 13:42:44 -07004500 dprintk("%s: start\n", __func__);
Trond Myklebustc228fd32007-01-13 02:28:11 -05004501 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004502 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004503 fs_locations->nlocations = 0;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04004504 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004505 nfs_fixup_referral_attributes(&fs_locations->fattr);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004506 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004507 return status;
4508}
4509
Andy Adamson557134a2009-04-01 09:21:53 -04004510#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04004511/*
4512 * nfs4_proc_exchange_id()
4513 *
4514 * Since the clientid has expired, all compounds using sessions
4515 * associated with the stale clientid will be returning
4516 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4517 * be in some phase of session reset.
4518 */
Andy Adamson4d643d12009-12-04 15:52:24 -05004519int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004520{
4521 nfs4_verifier verifier;
4522 struct nfs41_exchange_id_args args = {
4523 .client = clp,
4524 .flags = clp->cl_exchange_flags,
4525 };
4526 struct nfs41_exchange_id_res res = {
4527 .client = clp,
4528 };
4529 int status;
4530 struct rpc_message msg = {
4531 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4532 .rpc_argp = &args,
4533 .rpc_resp = &res,
4534 .rpc_cred = cred,
4535 };
4536 __be32 *p;
4537
4538 dprintk("--> %s\n", __func__);
4539 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04004540
Alexandros Batsakis7b183d02009-12-05 13:33:25 -05004541 /* Remove server-only flags */
4542 args.flags &= ~EXCHGID4_FLAG_CONFIRMED_R;
4543
Benny Halevy99fe60d2009-04-01 09:22:29 -04004544 p = (u32 *)verifier.data;
4545 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4546 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4547 args.verifier = &verifier;
4548
4549 while (1) {
4550 args.id_len = scnprintf(args.id, sizeof(args.id),
4551 "%s/%s %u",
4552 clp->cl_ipaddr,
4553 rpc_peeraddr2str(clp->cl_rpcclient,
4554 RPC_DISPLAY_ADDR),
4555 clp->cl_id_uniquifier);
4556
4557 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
4558
Andy Adamson180b62a2010-03-02 13:19:36 -05004559 if (status != -NFS4ERR_CLID_INUSE)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004560 break;
4561
4562 if (signalled())
4563 break;
4564
4565 if (++clp->cl_id_uniquifier == 0)
4566 break;
4567 }
4568
4569 dprintk("<-- %s status= %d\n", __func__, status);
4570 return status;
4571}
4572
Andy Adamson2050f0c2009-04-01 09:22:30 -04004573struct nfs4_get_lease_time_data {
4574 struct nfs4_get_lease_time_args *args;
4575 struct nfs4_get_lease_time_res *res;
4576 struct nfs_client *clp;
4577};
4578
4579static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4580 void *calldata)
4581{
4582 int ret;
4583 struct nfs4_get_lease_time_data *data =
4584 (struct nfs4_get_lease_time_data *)calldata;
4585
4586 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004587 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004588 /* just setup sequence, do not trigger session recovery
4589 since we're invoked within one */
4590 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004591 &data->args->la_seq_args,
4592 &data->res->lr_seq_res, 0, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004593
4594 BUG_ON(ret == -EAGAIN);
4595 rpc_call_start(task);
4596 dprintk("<-- %s\n", __func__);
4597}
4598
4599/*
4600 * Called from nfs4_state_manager thread for session setup, so don't recover
4601 * from sequence operation or clientid errors.
4602 */
4603static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4604{
4605 struct nfs4_get_lease_time_data *data =
4606 (struct nfs4_get_lease_time_data *)calldata;
4607
4608 dprintk("--> %s\n", __func__);
Trond Myklebustd185a332010-06-16 09:52:25 -04004609 nfs41_sequence_done(&data->res->lr_seq_res);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004610 switch (task->tk_status) {
4611 case -NFS4ERR_DELAY:
4612 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05004613 case -EKEYEXPIRED:
Andy Adamson2050f0c2009-04-01 09:22:30 -04004614 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4615 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4616 task->tk_status = 0;
Trond Myklebust0110ee12009-12-07 09:00:24 -05004617 nfs_restart_rpc(task, data->clp);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004618 return;
4619 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04004620 dprintk("<-- %s\n", __func__);
4621}
4622
4623struct rpc_call_ops nfs4_get_lease_time_ops = {
4624 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4625 .rpc_call_done = nfs4_get_lease_time_done,
4626};
4627
4628int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4629{
4630 struct rpc_task *task;
4631 struct nfs4_get_lease_time_args args;
4632 struct nfs4_get_lease_time_res res = {
4633 .lr_fsinfo = fsinfo,
4634 };
4635 struct nfs4_get_lease_time_data data = {
4636 .args = &args,
4637 .res = &res,
4638 .clp = clp,
4639 };
4640 struct rpc_message msg = {
4641 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4642 .rpc_argp = &args,
4643 .rpc_resp = &res,
4644 };
4645 struct rpc_task_setup task_setup = {
4646 .rpc_client = clp->cl_rpcclient,
4647 .rpc_message = &msg,
4648 .callback_ops = &nfs4_get_lease_time_ops,
4649 .callback_data = &data
4650 };
4651 int status;
4652
4653 res.lr_seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
4654 dprintk("--> %s\n", __func__);
4655 task = rpc_run_task(&task_setup);
4656
4657 if (IS_ERR(task))
4658 status = PTR_ERR(task);
4659 else {
4660 status = task->tk_status;
4661 rpc_put_task(task);
4662 }
4663 dprintk("<-- %s return %d\n", __func__, status);
4664
4665 return status;
4666}
4667
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004668/*
4669 * Reset a slot table
4670 */
Andy Adamson104aeba2010-01-14 17:45:10 -05004671static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
4672 int ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004673{
Andy Adamson104aeba2010-01-14 17:45:10 -05004674 struct nfs4_slot *new = NULL;
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004675 int i;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004676 int ret = 0;
4677
Andy Adamson104aeba2010-01-14 17:45:10 -05004678 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
4679 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004680
Andy Adamson104aeba2010-01-14 17:45:10 -05004681 /* Does the newly negotiated max_reqs match the existing slot table? */
4682 if (max_reqs != tbl->max_slots) {
4683 ret = -ENOMEM;
4684 new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004685 GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05004686 if (!new)
4687 goto out;
4688 ret = 0;
4689 kfree(tbl->slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004690 }
4691 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson104aeba2010-01-14 17:45:10 -05004692 if (new) {
4693 tbl->slots = new;
4694 tbl->max_slots = max_reqs;
4695 }
4696 for (i = 0; i < tbl->max_slots; ++i)
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004697 tbl->slots[i].seq_nr = ivalue;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004698 spin_unlock(&tbl->slot_tbl_lock);
4699 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4700 tbl, tbl->slots, tbl->max_slots);
4701out:
4702 dprintk("<-- %s: return %d\n", __func__, ret);
4703 return ret;
4704}
4705
Andy Adamsonfc931582009-04-01 09:22:31 -04004706/*
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004707 * Reset the forechannel and backchannel slot tables
4708 */
4709static int nfs4_reset_slot_tables(struct nfs4_session *session)
4710{
4711 int status;
4712
4713 status = nfs4_reset_slot_table(&session->fc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05004714 session->fc_attrs.max_reqs, 1);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004715 if (status)
4716 return status;
4717
4718 status = nfs4_reset_slot_table(&session->bc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05004719 session->bc_attrs.max_reqs, 0);
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004720 return status;
4721}
4722
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004723/* Destroy the slot table */
4724static void nfs4_destroy_slot_tables(struct nfs4_session *session)
4725{
4726 if (session->fc_slot_table.slots != NULL) {
4727 kfree(session->fc_slot_table.slots);
4728 session->fc_slot_table.slots = NULL;
4729 }
4730 if (session->bc_slot_table.slots != NULL) {
4731 kfree(session->bc_slot_table.slots);
4732 session->bc_slot_table.slots = NULL;
4733 }
4734 return;
4735}
4736
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004737/*
Andy Adamsonfc931582009-04-01 09:22:31 -04004738 * Initialize slot table
4739 */
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004740static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
4741 int max_slots, int ivalue)
Andy Adamsonfc931582009-04-01 09:22:31 -04004742{
Andy Adamsonfc931582009-04-01 09:22:31 -04004743 struct nfs4_slot *slot;
4744 int ret = -ENOMEM;
4745
4746 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
4747
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004748 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
Andy Adamsonfc931582009-04-01 09:22:31 -04004749
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004750 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_NOFS);
Andy Adamsonfc931582009-04-01 09:22:31 -04004751 if (!slot)
4752 goto out;
Andy Adamsonfc931582009-04-01 09:22:31 -04004753 ret = 0;
4754
4755 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonfc931582009-04-01 09:22:31 -04004756 tbl->max_slots = max_slots;
4757 tbl->slots = slot;
4758 tbl->highest_used_slotid = -1; /* no slot is currently used */
4759 spin_unlock(&tbl->slot_tbl_lock);
4760 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4761 tbl, tbl->slots, tbl->max_slots);
4762out:
4763 dprintk("<-- %s: return %d\n", __func__, ret);
4764 return ret;
Andy Adamsonfc931582009-04-01 09:22:31 -04004765}
4766
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004767/*
4768 * Initialize the forechannel and backchannel tables
4769 */
4770static int nfs4_init_slot_tables(struct nfs4_session *session)
4771{
Trond Myklebustf26468f2009-12-05 19:32:11 -05004772 struct nfs4_slot_table *tbl;
4773 int status = 0;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004774
Trond Myklebustf26468f2009-12-05 19:32:11 -05004775 tbl = &session->fc_slot_table;
4776 if (tbl->slots == NULL) {
4777 status = nfs4_init_slot_table(tbl,
4778 session->fc_attrs.max_reqs, 1);
4779 if (status)
4780 return status;
4781 }
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004782
Trond Myklebustf26468f2009-12-05 19:32:11 -05004783 tbl = &session->bc_slot_table;
4784 if (tbl->slots == NULL) {
4785 status = nfs4_init_slot_table(tbl,
4786 session->bc_attrs.max_reqs, 0);
4787 if (status)
4788 nfs4_destroy_slot_tables(session);
4789 }
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004790
4791 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04004792}
4793
4794struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
4795{
4796 struct nfs4_session *session;
4797 struct nfs4_slot_table *tbl;
4798
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004799 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04004800 if (!session)
4801 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04004802
Andy Adamson76db6d92009-04-01 09:22:38 -04004803 /*
4804 * The create session reply races with the server back
4805 * channel probe. Mark the client NFS_CS_SESSION_INITING
4806 * so that the client back channel can find the
4807 * nfs_client struct
4808 */
4809 clp->cl_cons_state = NFS_CS_SESSION_INITING;
Andy Adamsonea028ac2009-12-04 15:55:38 -05004810 init_completion(&session->complete);
Andy Adamson76db6d92009-04-01 09:22:38 -04004811
Andy Adamson557134a2009-04-01 09:21:53 -04004812 tbl = &session->fc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05004813 tbl->highest_used_slotid = -1;
Andy Adamson557134a2009-04-01 09:21:53 -04004814 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004815 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004816
4817 tbl = &session->bc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05004818 tbl->highest_used_slotid = -1;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004819 spin_lock_init(&tbl->slot_tbl_lock);
4820 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
4821
Andy Adamson557134a2009-04-01 09:21:53 -04004822 session->clp = clp;
4823 return session;
4824}
4825
4826void nfs4_destroy_session(struct nfs4_session *session)
4827{
Andy Adamson5a0ffe52009-04-01 09:23:18 -04004828 nfs4_proc_destroy_session(session);
4829 dprintk("%s Destroy backchannel for xprt %p\n",
4830 __func__, session->clp->cl_rpcclient->cl_xprt);
4831 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
4832 NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004833 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04004834 kfree(session);
4835}
4836
Andy Adamsonfc931582009-04-01 09:22:31 -04004837/*
4838 * Initialize the values to be used by the client in CREATE_SESSION
4839 * If nfs4_init_session set the fore channel request and response sizes,
4840 * use them.
4841 *
4842 * Set the back channel max_resp_sz_cached to zero to force the client to
4843 * always set csa_cachethis to FALSE because the current implementation
4844 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
4845 */
4846static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
4847{
4848 struct nfs4_session *session = args->client->cl_session;
4849 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
4850 mxresp_sz = session->fc_attrs.max_resp_sz;
4851
4852 if (mxrqst_sz == 0)
4853 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
4854 if (mxresp_sz == 0)
4855 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
4856 /* Fore channel attributes */
4857 args->fc_attrs.headerpadsz = 0;
4858 args->fc_attrs.max_rqst_sz = mxrqst_sz;
4859 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04004860 args->fc_attrs.max_ops = NFS4_MAX_OPS;
4861 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
4862
4863 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05004864 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04004865 __func__,
4866 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05004867 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04004868
4869 /* Back channel attributes */
4870 args->bc_attrs.headerpadsz = 0;
4871 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
4872 args->bc_attrs.max_resp_sz = PAGE_SIZE;
4873 args->bc_attrs.max_resp_sz_cached = 0;
4874 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
4875 args->bc_attrs.max_reqs = 1;
4876
4877 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
4878 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4879 __func__,
4880 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
4881 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
4882 args->bc_attrs.max_reqs);
4883}
4884
Andy Adamson8d353012009-04-01 09:22:32 -04004885static int _verify_channel_attr(char *chan, char *attr_name, u32 sent, u32 rcvd)
4886{
4887 if (rcvd <= sent)
4888 return 0;
4889 printk(KERN_WARNING "%s: Session INVALID: %s channel %s increased. "
4890 "sent=%u rcvd=%u\n", __func__, chan, attr_name, sent, rcvd);
4891 return -EINVAL;
4892}
4893
4894#define _verify_fore_channel_attr(_name_) \
4895 _verify_channel_attr("fore", #_name_, \
4896 args->fc_attrs._name_, \
4897 session->fc_attrs._name_)
4898
4899#define _verify_back_channel_attr(_name_) \
4900 _verify_channel_attr("back", #_name_, \
4901 args->bc_attrs._name_, \
4902 session->bc_attrs._name_)
4903
4904/*
4905 * The server is not allowed to increase the fore channel header pad size,
4906 * maximum response size, or maximum number of operations.
4907 *
4908 * The back channel attributes are only negotiatied down: We send what the
4909 * (back channel) server insists upon.
4910 */
4911static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
4912 struct nfs4_session *session)
4913{
4914 int ret = 0;
4915
4916 ret |= _verify_fore_channel_attr(headerpadsz);
4917 ret |= _verify_fore_channel_attr(max_resp_sz);
4918 ret |= _verify_fore_channel_attr(max_ops);
4919
4920 ret |= _verify_back_channel_attr(headerpadsz);
4921 ret |= _verify_back_channel_attr(max_rqst_sz);
4922 ret |= _verify_back_channel_attr(max_resp_sz);
4923 ret |= _verify_back_channel_attr(max_resp_sz_cached);
4924 ret |= _verify_back_channel_attr(max_ops);
4925 ret |= _verify_back_channel_attr(max_reqs);
4926
4927 return ret;
4928}
4929
Andy Adamsonfc931582009-04-01 09:22:31 -04004930static int _nfs4_proc_create_session(struct nfs_client *clp)
4931{
4932 struct nfs4_session *session = clp->cl_session;
4933 struct nfs41_create_session_args args = {
4934 .client = clp,
4935 .cb_program = NFS4_CALLBACK,
4936 };
4937 struct nfs41_create_session_res res = {
4938 .client = clp,
4939 };
4940 struct rpc_message msg = {
4941 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
4942 .rpc_argp = &args,
4943 .rpc_resp = &res,
4944 };
4945 int status;
4946
4947 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04004948 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04004949
4950 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4951
Andy Adamson8d353012009-04-01 09:22:32 -04004952 if (!status)
4953 /* Verify the session's negotiated channel_attrs values */
4954 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04004955 if (!status) {
4956 /* Increment the clientid slot sequence id */
4957 clp->cl_seqid++;
4958 }
4959
4960 return status;
4961}
4962
4963/*
4964 * Issues a CREATE_SESSION operation to the server.
4965 * It is the responsibility of the caller to verify the session is
4966 * expired before calling this routine.
4967 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05004968int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04004969{
4970 int status;
4971 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04004972 struct nfs4_session *session = clp->cl_session;
4973
4974 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
4975
4976 status = _nfs4_proc_create_session(clp);
4977 if (status)
4978 goto out;
4979
Trond Myklebustf26468f2009-12-05 19:32:11 -05004980 /* Init and reset the fore channel */
4981 status = nfs4_init_slot_tables(session);
4982 dprintk("slot table initialization returned %d\n", status);
4983 if (status)
4984 goto out;
4985 status = nfs4_reset_slot_tables(session);
4986 dprintk("slot table reset returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04004987 if (status)
4988 goto out;
4989
4990 ptr = (unsigned *)&session->sess_id.data[0];
4991 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
4992 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04004993out:
4994 dprintk("<-- %s\n", __func__);
4995 return status;
4996}
4997
Andy Adamson0f3e66c2009-04-01 09:22:34 -04004998/*
4999 * Issue the over-the-wire RPC DESTROY_SESSION.
5000 * The caller must serialize access to this routine.
5001 */
5002int nfs4_proc_destroy_session(struct nfs4_session *session)
5003{
5004 int status = 0;
5005 struct rpc_message msg;
5006
5007 dprintk("--> nfs4_proc_destroy_session\n");
5008
5009 /* session is still being setup */
5010 if (session->clp->cl_cons_state != NFS_CS_READY)
5011 return status;
5012
5013 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5014 msg.rpc_argp = session;
5015 msg.rpc_resp = NULL;
5016 msg.rpc_cred = NULL;
5017 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
5018
5019 if (status)
5020 printk(KERN_WARNING
5021 "Got error %d from the server on DESTROY_SESSION. "
5022 "Session has been destroyed regardless...\n", status);
5023
5024 dprintk("<-- nfs4_proc_destroy_session\n");
5025 return status;
5026}
5027
Trond Myklebustfccba802009-07-21 16:48:07 -04005028int nfs4_init_session(struct nfs_server *server)
5029{
5030 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005031 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005032 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005033 int ret;
5034
5035 if (!nfs4_has_session(clp))
5036 return 0;
5037
Andy Adamson68bf05e2009-12-15 12:55:02 -05005038 rsize = server->rsize;
5039 if (rsize == 0)
5040 rsize = NFS_MAX_FILE_IO_SIZE;
5041 wsize = server->wsize;
5042 if (wsize == 0)
5043 wsize = NFS_MAX_FILE_IO_SIZE;
5044
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005045 session = clp->cl_session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005046 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5047 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005048
Trond Myklebustfccba802009-07-21 16:48:07 -04005049 ret = nfs4_recover_expired_lease(server);
5050 if (!ret)
5051 ret = nfs4_check_client_ready(clp);
5052 return ret;
5053}
5054
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005055/*
5056 * Renew the cl_session lease.
5057 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005058struct nfs4_sequence_data {
5059 struct nfs_client *clp;
5060 struct nfs4_sequence_args args;
5061 struct nfs4_sequence_res res;
5062};
5063
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005064static void nfs41_sequence_release(void *data)
5065{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005066 struct nfs4_sequence_data *calldata = data;
5067 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005068
Alexandros Batsakis71358402010-02-05 03:45:05 -08005069 if (atomic_read(&clp->cl_count) > 1)
5070 nfs4_schedule_state_renewal(clp);
5071 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005072 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005073}
5074
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005075static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5076{
5077 switch(task->tk_status) {
5078 case -NFS4ERR_DELAY:
5079 case -EKEYEXPIRED:
5080 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5081 return -EAGAIN;
5082 default:
5083 nfs4_schedule_state_recovery(clp);
5084 }
5085 return 0;
5086}
5087
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005088static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005089{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005090 struct nfs4_sequence_data *calldata = data;
5091 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005092
Trond Myklebustd185a332010-06-16 09:52:25 -04005093 nfs41_sequence_done(task->tk_msg.rpc_resp);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005094
5095 if (task->tk_status < 0) {
5096 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005097 if (atomic_read(&clp->cl_count) == 1)
5098 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005099
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005100 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5101 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005102 return;
5103 }
5104 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005105 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005106out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005107 dprintk("<-- %s\n", __func__);
5108}
5109
5110static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5111{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005112 struct nfs4_sequence_data *calldata = data;
5113 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005114 struct nfs4_sequence_args *args;
5115 struct nfs4_sequence_res *res;
5116
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005117 args = task->tk_msg.rpc_argp;
5118 res = task->tk_msg.rpc_resp;
5119
Trond Myklebust035168ab2010-06-16 09:52:26 -04005120 if (nfs41_setup_sequence(clp->cl_session, args, res, 0, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005121 return;
5122 rpc_call_start(task);
5123}
5124
5125static const struct rpc_call_ops nfs41_sequence_ops = {
5126 .rpc_call_done = nfs41_sequence_call_done,
5127 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005128 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005129};
5130
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005131static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005132{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005133 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005134 struct rpc_message msg = {
5135 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5136 .rpc_cred = cred,
5137 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005138 struct rpc_task_setup task_setup_data = {
5139 .rpc_client = clp->cl_rpcclient,
5140 .rpc_message = &msg,
5141 .callback_ops = &nfs41_sequence_ops,
5142 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5143 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005144
Alexandros Batsakis71358402010-02-05 03:45:05 -08005145 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005146 return ERR_PTR(-EIO);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005147 calldata = kmalloc(sizeof(*calldata), GFP_NOFS);
5148 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005149 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005150 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005151 }
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005152 calldata->res.sr_slotid = NFS4_MAX_SLOT_TABLE;
5153 msg.rpc_argp = &calldata->args;
5154 msg.rpc_resp = &calldata->res;
5155 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005156 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005157
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005158 return rpc_run_task(&task_setup_data);
5159}
5160
5161static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5162{
5163 struct rpc_task *task;
5164 int ret = 0;
5165
5166 task = _nfs41_proc_sequence(clp, cred);
5167 if (IS_ERR(task))
5168 ret = PTR_ERR(task);
5169 else
5170 rpc_put_task(task);
5171 dprintk("<-- %s status=%d\n", __func__, ret);
5172 return ret;
5173}
5174
5175static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5176{
5177 struct rpc_task *task;
5178 int ret;
5179
5180 task = _nfs41_proc_sequence(clp, cred);
5181 if (IS_ERR(task)) {
5182 ret = PTR_ERR(task);
5183 goto out;
5184 }
5185 ret = rpc_wait_for_completion_task(task);
5186 if (!ret)
5187 ret = task->tk_status;
5188 rpc_put_task(task);
5189out:
5190 dprintk("<-- %s status=%d\n", __func__, ret);
5191 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005192}
5193
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005194struct nfs4_reclaim_complete_data {
5195 struct nfs_client *clp;
5196 struct nfs41_reclaim_complete_args arg;
5197 struct nfs41_reclaim_complete_res res;
5198};
5199
5200static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5201{
5202 struct nfs4_reclaim_complete_data *calldata = data;
5203
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005204 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168ab2010-06-16 09:52:26 -04005205 if (nfs41_setup_sequence(calldata->clp->cl_session,
5206 &calldata->arg.seq_args,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005207 &calldata->res.seq_res, 0, task))
5208 return;
5209
5210 rpc_call_start(task);
5211}
5212
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005213static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5214{
5215 switch(task->tk_status) {
5216 case 0:
5217 case -NFS4ERR_COMPLETE_ALREADY:
5218 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5219 break;
5220 case -NFS4ERR_DELAY:
5221 case -EKEYEXPIRED:
5222 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5223 return -EAGAIN;
5224 default:
5225 nfs4_schedule_state_recovery(clp);
5226 }
5227 return 0;
5228}
5229
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005230static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5231{
5232 struct nfs4_reclaim_complete_data *calldata = data;
5233 struct nfs_client *clp = calldata->clp;
5234 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5235
5236 dprintk("--> %s\n", __func__);
Trond Myklebustd185a332010-06-16 09:52:25 -04005237 nfs41_sequence_done(res);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005238
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005239 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5240 rpc_restart_call_prepare(task);
5241 return;
5242 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005243 dprintk("<-- %s\n", __func__);
5244}
5245
5246static void nfs4_free_reclaim_complete_data(void *data)
5247{
5248 struct nfs4_reclaim_complete_data *calldata = data;
5249
5250 kfree(calldata);
5251}
5252
5253static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5254 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5255 .rpc_call_done = nfs4_reclaim_complete_done,
5256 .rpc_release = nfs4_free_reclaim_complete_data,
5257};
5258
5259/*
5260 * Issue a global reclaim complete.
5261 */
5262static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5263{
5264 struct nfs4_reclaim_complete_data *calldata;
5265 struct rpc_task *task;
5266 struct rpc_message msg = {
5267 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5268 };
5269 struct rpc_task_setup task_setup_data = {
5270 .rpc_client = clp->cl_rpcclient,
5271 .rpc_message = &msg,
5272 .callback_ops = &nfs4_reclaim_complete_call_ops,
5273 .flags = RPC_TASK_ASYNC,
5274 };
5275 int status = -ENOMEM;
5276
5277 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005278 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005279 if (calldata == NULL)
5280 goto out;
5281 calldata->clp = clp;
5282 calldata->arg.one_fs = 0;
5283 calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
5284
5285 msg.rpc_argp = &calldata->arg;
5286 msg.rpc_resp = &calldata->res;
5287 task_setup_data.callback_data = calldata;
5288 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005289 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005290 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005291 goto out;
5292 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005293 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005294 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005295out:
5296 dprintk("<-- %s status=%d\n", __func__, status);
5297 return status;
5298}
Andy Adamson557134a2009-04-01 09:21:53 -04005299#endif /* CONFIG_NFS_V4_1 */
5300
Andy Adamson591d71c2009-04-01 09:22:47 -04005301struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005302 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005303 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304 .recover_open = nfs4_open_reclaim,
5305 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04005306 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005307 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005308};
5309
Andy Adamson591d71c2009-04-01 09:22:47 -04005310#if defined(CONFIG_NFS_V4_1)
5311struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
5312 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5313 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5314 .recover_open = nfs4_open_reclaim,
5315 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05005316 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005317 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005318 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04005319};
5320#endif /* CONFIG_NFS_V4_1 */
5321
5322struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005323 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005324 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005325 .recover_open = nfs4_open_expired,
5326 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04005327 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005328 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005329};
5330
Andy Adamson591d71c2009-04-01 09:22:47 -04005331#if defined(CONFIG_NFS_V4_1)
5332struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
5333 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5334 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5335 .recover_open = nfs4_open_expired,
5336 .recover_lock = nfs4_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05005337 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005338 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04005339};
5340#endif /* CONFIG_NFS_V4_1 */
5341
Benny Halevy29fba382009-04-01 09:22:44 -04005342struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
5343 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04005344 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005345 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04005346};
5347
5348#if defined(CONFIG_NFS_V4_1)
5349struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
5350 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04005351 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005352 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04005353};
5354#endif
5355
Trond Myklebust97dc1352010-06-16 09:52:26 -04005356static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
5357 .minor_version = 0,
5358 .call_sync = _nfs4_call_sync,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04005359 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
5360 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
5361 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04005362};
5363
5364#if defined(CONFIG_NFS_V4_1)
5365static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
5366 .minor_version = 1,
5367 .call_sync = _nfs4_call_sync_session,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04005368 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
5369 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
5370 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04005371};
5372#endif
5373
Trond Myklebust97dc1352010-06-16 09:52:26 -04005374const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
5375 [0] = &nfs_v4_0_minor_ops,
5376#if defined(CONFIG_NFS_V4_1)
5377 [1] = &nfs_v4_1_minor_ops,
5378#endif
5379};
5380
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08005381static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005382 .permission = nfs_permission,
5383 .getattr = nfs_getattr,
5384 .setattr = nfs_setattr,
5385 .getxattr = nfs4_getxattr,
5386 .setxattr = nfs4_setxattr,
5387 .listxattr = nfs4_listxattr,
5388};
5389
David Howells509de812006-08-22 20:06:11 -04005390const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005391 .version = 4, /* protocol version */
5392 .dentry_ops = &nfs4_dentry_operations,
5393 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005394 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005395 .getroot = nfs4_proc_get_root,
5396 .getattr = nfs4_proc_getattr,
5397 .setattr = nfs4_proc_setattr,
David Howells2b3de442006-08-22 20:06:09 -04005398 .lookupfh = nfs4_proc_lookupfh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005399 .lookup = nfs4_proc_lookup,
5400 .access = nfs4_proc_access,
5401 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402 .create = nfs4_proc_create,
5403 .remove = nfs4_proc_remove,
5404 .unlink_setup = nfs4_proc_unlink_setup,
5405 .unlink_done = nfs4_proc_unlink_done,
5406 .rename = nfs4_proc_rename,
5407 .link = nfs4_proc_link,
5408 .symlink = nfs4_proc_symlink,
5409 .mkdir = nfs4_proc_mkdir,
5410 .rmdir = nfs4_proc_remove,
5411 .readdir = nfs4_proc_readdir,
5412 .mknod = nfs4_proc_mknod,
5413 .statfs = nfs4_proc_statfs,
5414 .fsinfo = nfs4_proc_fsinfo,
5415 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04005416 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417 .decode_dirent = nfs4_decode_dirent,
5418 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05005419 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005421 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005423 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005425 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04005426 .close_context = nfs4_close_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427};
5428
5429/*
5430 * Local variables:
5431 * c-basic-offset: 8
5432 * End:
5433 */