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