blob: d0cb6e163320764dc88b058505ff0a2bbf3fd41e [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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Linus Torvalds1da177e2005-04-16 15:20:36 -070074/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050075static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
Trond Myklebust52567b02009-10-23 14:46:42 -040077 if (err >= -1000)
78 return err;
79 switch (err) {
80 case -NFS4ERR_RESOURCE:
81 return -EREMOTEIO;
82 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -070084 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -040085 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 }
Trond Myklebust52567b02009-10-23 14:46:42 -040087 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088}
89
90/*
91 * This is our standard bitmap for GETATTR requests.
92 */
93const u32 nfs4_fattr_bitmap[2] = {
94 FATTR4_WORD0_TYPE
95 | FATTR4_WORD0_CHANGE
96 | FATTR4_WORD0_SIZE
97 | FATTR4_WORD0_FSID
98 | FATTR4_WORD0_FILEID,
99 FATTR4_WORD1_MODE
100 | FATTR4_WORD1_NUMLINKS
101 | FATTR4_WORD1_OWNER
102 | FATTR4_WORD1_OWNER_GROUP
103 | FATTR4_WORD1_RAWDEV
104 | FATTR4_WORD1_SPACE_USED
105 | FATTR4_WORD1_TIME_ACCESS
106 | FATTR4_WORD1_TIME_METADATA
107 | FATTR4_WORD1_TIME_MODIFY
108};
109
110const u32 nfs4_statfs_bitmap[2] = {
111 FATTR4_WORD0_FILES_AVAIL
112 | FATTR4_WORD0_FILES_FREE
113 | FATTR4_WORD0_FILES_TOTAL,
114 FATTR4_WORD1_SPACE_AVAIL
115 | FATTR4_WORD1_SPACE_FREE
116 | FATTR4_WORD1_SPACE_TOTAL
117};
118
Trond Myklebust4ce79712005-06-22 17:16:21 +0000119const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 FATTR4_WORD0_MAXLINK
121 | FATTR4_WORD0_MAXNAME,
122 0
123};
124
125const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
126 | FATTR4_WORD0_MAXREAD
127 | FATTR4_WORD0_MAXWRITE
128 | FATTR4_WORD0_LEASE_TIME,
129 0
130};
131
Manoj Naik830b8e32006-06-09 09:34:25 -0400132const u32 nfs4_fs_locations_bitmap[2] = {
133 FATTR4_WORD0_TYPE
134 | FATTR4_WORD0_CHANGE
135 | FATTR4_WORD0_SIZE
136 | FATTR4_WORD0_FSID
137 | FATTR4_WORD0_FILEID
138 | FATTR4_WORD0_FS_LOCATIONS,
139 FATTR4_WORD1_MODE
140 | FATTR4_WORD1_NUMLINKS
141 | FATTR4_WORD1_OWNER
142 | FATTR4_WORD1_OWNER_GROUP
143 | FATTR4_WORD1_RAWDEV
144 | FATTR4_WORD1_SPACE_USED
145 | FATTR4_WORD1_TIME_ACCESS
146 | FATTR4_WORD1_TIME_METADATA
147 | FATTR4_WORD1_TIME_MODIFY
148 | FATTR4_WORD1_MOUNTED_ON_FILEID
149};
150
Al Virobc4785c2006-10-19 23:28:51 -0700151static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 struct nfs4_readdir_arg *readdir)
153{
Al Viro0dbb4c62006-10-19 23:28:49 -0700154 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 BUG_ON(readdir->count < 80);
157 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000158 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
160 return;
161 }
162
163 readdir->cookie = 0;
164 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
165 if (cookie == 2)
166 return;
167
168 /*
169 * NFSv4 servers do not return entries for '.' and '..'
170 * Therefore, we fake these entries here. We let '.'
171 * have cookie 0 and '..' have cookie 1. Note that
172 * when talking to the server, we always send cookie 0
173 * instead of 1 or 2.
174 */
Al Viro0dbb4c62006-10-19 23:28:49 -0700175 start = p = kmap_atomic(*readdir->pages, KM_USER0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177 if (cookie == 0) {
178 *p++ = xdr_one; /* next */
179 *p++ = xdr_zero; /* cookie, first word */
180 *p++ = xdr_one; /* cookie, second word */
181 *p++ = xdr_one; /* entry len */
182 memcpy(p, ".\0\0\0", 4); /* entry */
183 p++;
184 *p++ = xdr_one; /* bitmap length */
185 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
186 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400187 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 }
189
190 *p++ = xdr_one; /* next */
191 *p++ = xdr_zero; /* cookie, first word */
192 *p++ = xdr_two; /* cookie, second word */
193 *p++ = xdr_two; /* entry len */
194 memcpy(p, "..\0\0", 4); /* entry */
195 p++;
196 *p++ = xdr_one; /* bitmap length */
197 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
198 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400199 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201 readdir->pgbase = (char *)p - (char *)start;
202 readdir->count -= readdir->pgbase;
203 kunmap_atomic(start, KM_USER0);
204}
205
Trond Myklebust65de8722008-12-23 15:21:44 -0500206static int nfs4_wait_clnt_recover(struct nfs_client *clp)
207{
208 int res;
209
210 might_sleep();
211
Trond Myklebuste005e802008-12-23 15:21:48 -0500212 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400213 nfs_wait_bit_killable, TASK_KILLABLE);
Trond Myklebust65de8722008-12-23 15:21:44 -0500214 return res;
215}
216
217static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
218{
219 int res = 0;
220
221 might_sleep();
222
223 if (*timeout <= 0)
224 *timeout = NFS4_POLL_RETRY_MIN;
225 if (*timeout > NFS4_POLL_RETRY_MAX)
226 *timeout = NFS4_POLL_RETRY_MAX;
227 schedule_timeout_killable(*timeout);
228 if (fatal_signal_pending(current))
229 res = -ERESTARTSYS;
230 *timeout <<= 1;
231 return res;
232}
233
234/* This is the error handling routine for processes that are allowed
235 * to sleep.
236 */
237static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
238{
239 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500240 struct nfs4_state *state = exception->state;
Trond Myklebust65de8722008-12-23 15:21:44 -0500241 int ret = errorcode;
242
243 exception->retry = 0;
244 switch(errorcode) {
245 case 0:
246 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500247 case -NFS4ERR_ADMIN_REVOKED:
248 case -NFS4ERR_BAD_STATEID:
249 case -NFS4ERR_OPENMODE:
250 if (state == NULL)
251 break;
252 nfs4_state_mark_reclaim_nograce(clp, state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500253 goto do_state_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500254 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500255 if (state == NULL)
256 break;
257 nfs4_state_mark_reclaim_reboot(clp, state);
258 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust65de8722008-12-23 15:21:44 -0500259 case -NFS4ERR_EXPIRED:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500260 goto do_state_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500261#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400262 case -NFS4ERR_BADSESSION:
263 case -NFS4ERR_BADSLOT:
264 case -NFS4ERR_BAD_HIGH_SLOT:
265 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
266 case -NFS4ERR_DEADSESSION:
267 case -NFS4ERR_SEQ_FALSE_RETRY:
268 case -NFS4ERR_SEQ_MISORDERED:
269 dprintk("%s ERROR: %d Reset session\n", __func__,
270 errorcode);
Andy Adamson0b9e2d42009-12-04 16:02:14 -0500271 nfs4_schedule_state_recovery(clp);
Andy Adamson4745e312009-04-01 09:22:42 -0400272 exception->retry = 1;
Andy Adamsonb9179232009-12-04 15:55:32 -0500273 break;
Trond Myklebust03391692010-01-26 15:42:38 -0500274#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500275 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500276 if (exception->timeout > HZ) {
277 /* We have retried a decent amount, time to
278 * fail
279 */
280 ret = -EBUSY;
281 break;
282 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500283 case -NFS4ERR_GRACE:
284 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -0500285 case -EKEYEXPIRED:
Trond Myklebust65de8722008-12-23 15:21:44 -0500286 ret = nfs4_delay(server->client, &exception->timeout);
287 if (ret != 0)
288 break;
289 case -NFS4ERR_OLD_STATEID:
290 exception->retry = 1;
291 }
292 /* We failed to handle the error */
293 return nfs4_map_errors(ret);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500294do_state_recovery:
295 nfs4_schedule_state_recovery(clp);
296 ret = nfs4_wait_clnt_recover(clp);
297 if (ret == 0)
298 exception->retry = 1;
299 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500300}
301
302
Trond Myklebust26e976a2006-01-03 09:55:21 +0100303static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
David Howells7539bba2006-08-22 20:06:09 -0400305 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 spin_lock(&clp->cl_lock);
307 if (time_before(clp->cl_last_renewal,timestamp))
308 clp->cl_last_renewal = timestamp;
309 spin_unlock(&clp->cl_lock);
310}
311
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400312#if defined(CONFIG_NFS_V4_1)
313
Benny Halevy510b8172009-04-01 09:22:14 -0400314/*
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400315 * nfs4_free_slot - free a slot and efficiently update slot table.
316 *
317 * freeing a slot is trivially done by clearing its respective bit
318 * in the bitmap.
319 * If the freed slotid equals highest_used_slotid we want to update it
320 * so that the server would be able to size down the slot table if needed,
321 * otherwise we know that the highest_used_slotid is still in use.
322 * When updating highest_used_slotid there may be "holes" in the bitmap
323 * so we need to scan down from highest_used_slotid to 0 looking for the now
324 * highest slotid in use.
325 * If none found, highest_used_slotid is set to -1.
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500326 *
327 * Must be called while holding tbl->slot_tbl_lock
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400328 */
329static void
330nfs4_free_slot(struct nfs4_slot_table *tbl, u8 free_slotid)
331{
332 int slotid = free_slotid;
333
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400334 /* clear used bit in bitmap */
335 __clear_bit(slotid, tbl->used_slots);
336
337 /* update highest_used_slotid when it is freed */
338 if (slotid == tbl->highest_used_slotid) {
339 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500340 if (slotid < tbl->max_slots)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400341 tbl->highest_used_slotid = slotid;
342 else
343 tbl->highest_used_slotid = -1;
344 }
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400345 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
346 free_slotid, tbl->highest_used_slotid);
347}
348
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800349/*
350 * Signal state manager thread if session is drained
351 */
352static void nfs41_check_drain_session_complete(struct nfs4_session *ses)
353{
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800354 struct rpc_task *task;
355
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800356 if (!test_bit(NFS4CLNT_SESSION_DRAINING, &ses->clp->cl_state)) {
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800357 task = rpc_wake_up_next(&ses->fc_slot_table.slot_tbl_waitq);
358 if (task)
359 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800360 return;
361 }
362
363 if (ses->fc_slot_table.highest_used_slotid != -1)
364 return;
365
366 dprintk("%s COMPLETE: Session Drained\n", __func__);
367 complete(&ses->complete);
368}
369
Trond Myklebustd61e6122009-12-05 19:32:19 -0500370static void nfs41_sequence_free_slot(const struct nfs_client *clp,
Andy Adamson13615872009-04-01 09:22:17 -0400371 struct nfs4_sequence_res *res)
372{
373 struct nfs4_slot_table *tbl;
374
Andy Adamson13615872009-04-01 09:22:17 -0400375 tbl = &clp->cl_session->fc_slot_table;
376 if (res->sr_slotid == NFS4_MAX_SLOT_TABLE) {
Andy Adamson13615872009-04-01 09:22:17 -0400377 /* just wake up the next guy waiting since
378 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500379 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500380 return;
Andy Adamson13615872009-04-01 09:22:17 -0400381 }
Andy Adamsonea028ac2009-12-04 15:55:38 -0500382
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500383 spin_lock(&tbl->slot_tbl_lock);
384 nfs4_free_slot(tbl, res->sr_slotid);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800385 nfs41_check_drain_session_complete(clp->cl_session);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500386 spin_unlock(&tbl->slot_tbl_lock);
387 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
Andy Adamson13615872009-04-01 09:22:17 -0400388}
389
Andy Adamsonb0df8062009-04-01 09:22:18 -0400390static void nfs41_sequence_done(struct nfs_client *clp,
391 struct nfs4_sequence_res *res,
392 int rpc_status)
393{
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) {
Andy Adamson691daf32009-12-04 15:55:39 -0500413 tbl = &clp->cl_session->fc_slot_table;
414 slot = tbl->slots + res->sr_slotid;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400415 /* Update the slot's sequence and clientid lease timer */
416 ++slot->seq_nr;
417 timestamp = res->sr_renewal_time;
418 spin_lock(&clp->cl_lock);
419 if (time_before(clp->cl_last_renewal, timestamp))
420 clp->cl_last_renewal = timestamp;
421 spin_unlock(&clp->cl_lock);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500422 /* Check sequence flags */
Alexandros Batsakis71358402010-02-05 03:45:05 -0800423 if (atomic_read(&clp->cl_count) > 1)
424 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400425 }
426out:
427 /* The session may be reset by one of the error handlers. */
428 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
429 nfs41_sequence_free_slot(clp, res);
430}
431
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400432/*
Benny Halevy510b8172009-04-01 09:22:14 -0400433 * nfs4_find_slot - efficiently look for a free slot
434 *
435 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
436 * If found, we mark the slot as used, update the highest_used_slotid,
437 * and respectively set up the sequence operation args.
438 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400439 *
440 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400441 */
442static u8
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800443nfs4_find_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400444{
445 int slotid;
446 u8 ret_id = NFS4_MAX_SLOT_TABLE;
447 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
448
Benny Halevy510b8172009-04-01 09:22:14 -0400449 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
450 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
451 tbl->max_slots);
452 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
453 if (slotid >= tbl->max_slots)
454 goto out;
455 __set_bit(slotid, tbl->used_slots);
456 if (slotid > tbl->highest_used_slotid)
457 tbl->highest_used_slotid = slotid;
458 ret_id = slotid;
459out:
460 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
461 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400462 return ret_id;
463}
464
Andy Adamsonce5039c2009-04-01 09:22:13 -0400465static int nfs41_setup_sequence(struct nfs4_session *session,
466 struct nfs4_sequence_args *args,
467 struct nfs4_sequence_res *res,
468 int cache_reply,
469 struct rpc_task *task)
470{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400471 struct nfs4_slot *slot;
472 struct nfs4_slot_table *tbl;
473 u8 slotid;
474
475 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400476 /* slot already allocated? */
477 if (res->sr_slotid != NFS4_MAX_SLOT_TABLE)
478 return 0;
479
480 memset(res, 0, sizeof(*res));
481 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400482 tbl = &session->fc_slot_table;
483
484 spin_lock(&tbl->slot_tbl_lock);
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800485 if (test_bit(NFS4CLNT_SESSION_DRAINING, &session->clp->cl_state) &&
486 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamsonea028ac2009-12-04 15:55:38 -0500487 /*
488 * The state manager will wait until the slot table is empty.
489 * Schedule the reset thread
490 */
Andy Adamson05f0d232009-12-04 15:55:37 -0500491 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400492 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500493 dprintk("%s Schedule Session Reset\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500494 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400495 }
496
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800497 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
498 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
499 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
500 spin_unlock(&tbl->slot_tbl_lock);
501 dprintk("%s enforce FIFO order\n", __func__);
502 return -EAGAIN;
503 }
504
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800505 slotid = nfs4_find_slot(tbl);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400506 if (slotid == NFS4_MAX_SLOT_TABLE) {
507 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
508 spin_unlock(&tbl->slot_tbl_lock);
509 dprintk("<-- %s: no free slots\n", __func__);
510 return -EAGAIN;
511 }
512 spin_unlock(&tbl->slot_tbl_lock);
513
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800514 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400515 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400516 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400517 args->sa_slotid = slotid;
518 args->sa_cache_this = cache_reply;
519
520 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
521
Benny Halevy99fe60d2009-04-01 09:22:29 -0400522 res->sr_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400523 res->sr_slotid = slotid;
524 res->sr_renewal_time = jiffies;
525 /*
526 * sr_status is only set in decode_sequence, and so will remain
527 * set to 1 if an rpc level failure occurs.
528 */
529 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400530 return 0;
531}
532
533int nfs4_setup_sequence(struct nfs_client *clp,
534 struct nfs4_sequence_args *args,
535 struct nfs4_sequence_res *res,
536 int cache_reply,
537 struct rpc_task *task)
538{
539 int ret = 0;
540
541 dprintk("--> %s clp %p session %p sr_slotid %d\n",
542 __func__, clp, clp->cl_session, res->sr_slotid);
543
544 if (!nfs4_has_session(clp))
545 goto out;
546 ret = nfs41_setup_sequence(clp->cl_session, args, res, cache_reply,
547 task);
Andy Adamson2628edd2009-12-04 15:55:35 -0500548 if (ret && ret != -EAGAIN) {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400549 /* terminate rpc task */
550 task->tk_status = ret;
551 task->tk_action = NULL;
552 }
553out:
554 dprintk("<-- %s status=%d\n", __func__, ret);
555 return ret;
556}
557
558struct nfs41_call_sync_data {
559 struct nfs_client *clp;
560 struct nfs4_sequence_args *seq_args;
561 struct nfs4_sequence_res *seq_res;
562 int cache_reply;
563};
564
565static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
566{
567 struct nfs41_call_sync_data *data = calldata;
568
569 dprintk("--> %s data->clp->cl_session %p\n", __func__,
570 data->clp->cl_session);
571 if (nfs4_setup_sequence(data->clp, data->seq_args,
572 data->seq_res, data->cache_reply, task))
573 return;
574 rpc_call_start(task);
575}
576
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800577static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
578{
579 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
580 nfs41_call_sync_prepare(task, calldata);
581}
582
Andy Adamson69ab40c2009-04-01 09:22:19 -0400583static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
584{
585 struct nfs41_call_sync_data *data = calldata;
586
587 nfs41_sequence_done(data->clp, data->seq_res, task->tk_status);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400588}
589
Andy Adamsonce5039c2009-04-01 09:22:13 -0400590struct rpc_call_ops nfs41_call_sync_ops = {
591 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400592 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400593};
594
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800595struct rpc_call_ops nfs41_call_priv_sync_ops = {
596 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
597 .rpc_call_done = nfs41_call_sync_done,
598};
599
Andy Adamsonce5039c2009-04-01 09:22:13 -0400600static int nfs4_call_sync_sequence(struct nfs_client *clp,
601 struct rpc_clnt *clnt,
602 struct rpc_message *msg,
603 struct nfs4_sequence_args *args,
604 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800605 int cache_reply,
606 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400607{
608 int ret;
609 struct rpc_task *task;
610 struct nfs41_call_sync_data data = {
611 .clp = clp,
612 .seq_args = args,
613 .seq_res = res,
614 .cache_reply = cache_reply,
615 };
616 struct rpc_task_setup task_setup = {
617 .rpc_client = clnt,
618 .rpc_message = msg,
619 .callback_ops = &nfs41_call_sync_ops,
620 .callback_data = &data
621 };
622
623 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800624 if (privileged)
625 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400626 task = rpc_run_task(&task_setup);
627 if (IS_ERR(task))
628 ret = PTR_ERR(task);
629 else {
630 ret = task->tk_status;
631 rpc_put_task(task);
632 }
633 return ret;
634}
635
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400636int _nfs4_call_sync_session(struct nfs_server *server,
637 struct rpc_message *msg,
638 struct nfs4_sequence_args *args,
639 struct nfs4_sequence_res *res,
640 int cache_reply)
641{
Andy Adamsonce5039c2009-04-01 09:22:13 -0400642 return nfs4_call_sync_sequence(server->nfs_client, server->client,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800643 msg, args, res, cache_reply, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400644}
645
646#endif /* CONFIG_NFS_V4_1 */
647
648int _nfs4_call_sync(struct nfs_server *server,
649 struct rpc_message *msg,
650 struct nfs4_sequence_args *args,
651 struct nfs4_sequence_res *res,
652 int cache_reply)
653{
Benny Halevyf3752972009-04-01 09:22:04 -0400654 args->sa_session = res->sr_session = NULL;
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400655 return rpc_call_sync(server->client, msg, 0);
656}
657
658#define nfs4_call_sync(server, msg, args, res, cache_reply) \
659 (server)->nfs_client->cl_call_sync((server), (msg), &(args)->seq_args, \
660 &(res)->seq_res, (cache_reply))
661
Andy Adamsonb0df8062009-04-01 09:22:18 -0400662static void nfs4_sequence_done(const struct nfs_server *server,
663 struct nfs4_sequence_res *res, int rpc_status)
664{
665#ifdef CONFIG_NFS_V4_1
666 if (nfs4_has_session(server->nfs_client))
667 nfs41_sequence_done(server->nfs_client, res, rpc_status);
668#endif /* CONFIG_NFS_V4_1 */
669}
670
Trond Myklebust38478b22006-05-25 01:40:57 -0400671static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
Trond Myklebust38478b22006-05-25 01:40:57 -0400673 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Trond Myklebust38478b22006-05-25 01:40:57 -0400675 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400676 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
677 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400678 nfs_force_lookup_revalidate(dir);
Trond Myklebust40d24702007-10-08 09:24:22 -0400679 nfsi->change_attr = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400680 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681}
682
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100683struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400684 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100685 struct nfs_openargs o_arg;
686 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100687 struct nfs_open_confirmargs c_arg;
688 struct nfs_open_confirmres c_res;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100689 struct nfs_fattr f_attr;
690 struct nfs_fattr dir_attr;
Trond Myklebustad389da2007-06-05 12:30:00 -0400691 struct path path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100692 struct dentry *dir;
693 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400694 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100695 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100696 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400697 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100698 int rpc_status;
699 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100700};
701
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400702
703static void nfs4_init_opendata_res(struct nfs4_opendata *p)
704{
705 p->o_res.f_attr = &p->f_attr;
706 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400707 p->o_res.seqid = p->o_arg.seqid;
708 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400709 p->o_res.server = p->o_arg.server;
710 nfs_fattr_init(&p->f_attr);
711 nfs_fattr_init(&p->dir_attr);
Benny Halevy578e4582009-06-18 22:01:23 -0400712 p->o_res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400713}
714
Trond Myklebustad389da2007-06-05 12:30:00 -0400715static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500716 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100717 const struct iattr *attrs)
718{
Trond Myklebustad389da2007-06-05 12:30:00 -0400719 struct dentry *parent = dget_parent(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100720 struct inode *dir = parent->d_inode;
721 struct nfs_server *server = NFS_SERVER(dir);
722 struct nfs4_opendata *p;
723
724 p = kzalloc(sizeof(*p), GFP_KERNEL);
725 if (p == NULL)
726 goto err;
727 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
728 if (p->o_arg.seqid == NULL)
729 goto err_free;
Al Virof6948692010-01-30 13:51:04 -0500730 path_get(path);
731 p->path = *path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100732 p->dir = parent;
733 p->owner = sp;
734 atomic_inc(&sp->so_count);
735 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500736 p->o_arg.open_flags = flags;
737 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400738 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400739 p->o_arg.id = sp->so_owner_id.id;
Trond Myklebustad389da2007-06-05 12:30:00 -0400740 p->o_arg.name = &p->path.dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100741 p->o_arg.server = server;
742 p->o_arg.bitmask = server->attr_bitmask;
743 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100744 if (flags & O_EXCL) {
Alexandros Batsakis4882ef72009-12-05 13:30:21 -0500745 if (nfs4_has_persistent_session(server->nfs_client)) {
746 /* GUARDED */
747 p->o_arg.u.attrs = &p->attrs;
748 memcpy(&p->attrs, attrs, sizeof(p->attrs));
749 } else { /* EXCLUSIVE4_1 */
750 u32 *s = (u32 *) p->o_arg.u.verifier.data;
751 s[0] = jiffies;
752 s[1] = current->pid;
753 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100754 } else if (flags & O_CREAT) {
755 p->o_arg.u.attrs = &p->attrs;
756 memcpy(&p->attrs, attrs, sizeof(p->attrs));
757 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100758 p->c_arg.fh = &p->o_res.fh;
759 p->c_arg.stateid = &p->o_res.stateid;
760 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400761 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400762 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100763 return p;
764err_free:
765 kfree(p);
766err:
767 dput(parent);
768 return NULL;
769}
770
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400771static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100772{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400773 struct nfs4_opendata *p = container_of(kref,
774 struct nfs4_opendata, kref);
775
776 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400777 if (p->state != NULL)
778 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400779 nfs4_put_state_owner(p->owner);
780 dput(p->dir);
Jan Blunck31f31db12008-05-02 13:42:45 -0700781 path_put(&p->path);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400782 kfree(p);
783}
784
785static void nfs4_opendata_put(struct nfs4_opendata *p)
786{
787 if (p != NULL)
788 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100789}
790
Trond Myklebust06f814a2006-01-03 09:55:07 +0100791static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
792{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100793 int ret;
794
Trond Myklebust06f814a2006-01-03 09:55:07 +0100795 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100796 return ret;
797}
798
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500799static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400800{
801 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500802
803 if (open_mode & O_EXCL)
804 goto out;
805 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400806 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500807 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
808 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400809 break;
810 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500811 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
812 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400813 break;
814 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500815 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
816 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400817 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500818out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400819 return ret;
820}
821
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500822static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400823{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500824 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400825 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500826 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400827 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500828 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400829 return 1;
830}
831
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500832static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100833{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500834 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100835 case FMODE_WRITE:
836 state->n_wronly++;
837 break;
838 case FMODE_READ:
839 state->n_rdonly++;
840 break;
841 case FMODE_READ|FMODE_WRITE:
842 state->n_rdwr++;
843 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500844 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100845}
846
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500847static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400848{
849 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
850 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
851 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500852 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400853 case FMODE_READ:
854 set_bit(NFS_O_RDONLY_STATE, &state->flags);
855 break;
856 case FMODE_WRITE:
857 set_bit(NFS_O_WRONLY_STATE, &state->flags);
858 break;
859 case FMODE_READ|FMODE_WRITE:
860 set_bit(NFS_O_RDWR_STATE, &state->flags);
861 }
862}
863
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500864static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400865{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400866 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500867 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400868 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400869}
870
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500871static 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 -0700872{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400873 /*
874 * Protect the call to nfs4_state_set_mode_locked and
875 * serialise the stateid update
876 */
877 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400878 if (deleg_stateid != NULL) {
879 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
880 set_bit(NFS_DELEGATED_STATE, &state->flags);
881 }
882 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500883 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400884 write_sequnlock(&state->seqlock);
885 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500886 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700887 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888}
889
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500890static 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 -0500891{
892 struct nfs_inode *nfsi = NFS_I(state->inode);
893 struct nfs_delegation *deleg_cur;
894 int ret = 0;
895
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500896 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500897
898 rcu_read_lock();
899 deleg_cur = rcu_dereference(nfsi->delegation);
900 if (deleg_cur == NULL)
901 goto no_delegation;
902
903 spin_lock(&deleg_cur->lock);
904 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500905 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500906 goto no_delegation_unlock;
907
908 if (delegation == NULL)
909 delegation = &deleg_cur->stateid;
910 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
911 goto no_delegation_unlock;
912
Trond Myklebustb7391f42008-12-23 15:21:52 -0500913 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500914 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500915 ret = 1;
916no_delegation_unlock:
917 spin_unlock(&deleg_cur->lock);
918no_delegation:
919 rcu_read_unlock();
920
921 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500922 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500923 ret = 1;
924 }
925
926 return ret;
927}
928
929
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500930static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400931{
932 struct nfs_delegation *delegation;
933
934 rcu_read_lock();
935 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500936 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -0400937 rcu_read_unlock();
938 return;
939 }
940 rcu_read_unlock();
941 nfs_inode_return_delegation(inode);
942}
943
Trond Myklebust6ee41262007-07-08 14:11:36 -0400944static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400945{
946 struct nfs4_state *state = opendata->state;
947 struct nfs_inode *nfsi = NFS_I(state->inode);
948 struct nfs_delegation *delegation;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500949 int open_mode = opendata->o_arg.open_flags & O_EXCL;
950 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400951 nfs4_stateid stateid;
952 int ret = -EAGAIN;
953
Trond Myklebustaac00a82007-07-05 19:02:21 -0400954 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500955 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400956 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500957 if (can_open_cached(state, fmode, open_mode)) {
958 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400959 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400960 goto out_return_state;
961 }
962 spin_unlock(&state->owner->so_lock);
963 }
Trond Myklebust34310432008-12-23 15:21:38 -0500964 rcu_read_lock();
965 delegation = rcu_dereference(nfsi->delegation);
966 if (delegation == NULL ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500967 !can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -0500968 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -0400969 break;
Trond Myklebust34310432008-12-23 15:21:38 -0500970 }
Trond Myklebustaac00a82007-07-05 19:02:21 -0400971 /* Save the delegation */
972 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
973 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -0400974 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400975 if (ret != 0)
976 goto out;
977 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -0500978
979 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500980 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -0500981 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400982 }
Trond Myklebustaac00a82007-07-05 19:02:21 -0400983out:
984 return ERR_PTR(ret);
985out_return_state:
986 atomic_inc(&state->count);
987 return state;
988}
989
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100990static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
991{
992 struct inode *inode;
993 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -0400994 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400995 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100996
Trond Myklebustaac00a82007-07-05 19:02:21 -0400997 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400998 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400999 goto out;
1000 }
1001
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001002 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001003 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001004 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001005 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001006 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001007 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001008 goto err;
1009 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001010 state = nfs4_get_open_state(inode, data->owner);
1011 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001012 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001013 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001014 int delegation_flags = 0;
1015
Trond Myklebust003707c2007-07-05 18:07:55 -04001016 rcu_read_lock();
1017 delegation = rcu_dereference(NFS_I(inode)->delegation);
1018 if (delegation)
1019 delegation_flags = delegation->flags;
1020 rcu_read_unlock();
Trond Myklebust15c831b2008-12-23 15:21:39 -05001021 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001022 nfs_inode_set_delegation(state->inode,
1023 data->owner->so_cred,
1024 &data->o_res);
1025 else
1026 nfs_inode_reclaim_delegation(state->inode,
1027 data->owner->so_cred,
1028 &data->o_res);
1029 }
Trond Myklebust34310432008-12-23 15:21:38 -05001030
1031 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001032 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001033 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001034out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001035 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001036err_put_inode:
1037 iput(inode);
1038err:
1039 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001040}
1041
Trond Myklebust864472e2006-01-03 09:55:15 +01001042static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1043{
1044 struct nfs_inode *nfsi = NFS_I(state->inode);
1045 struct nfs_open_context *ctx;
1046
1047 spin_lock(&state->inode->i_lock);
1048 list_for_each_entry(ctx, &nfsi->open_files, list) {
1049 if (ctx->state != state)
1050 continue;
1051 get_nfs_open_context(ctx);
1052 spin_unlock(&state->inode->i_lock);
1053 return ctx;
1054 }
1055 spin_unlock(&state->inode->i_lock);
1056 return ERR_PTR(-ENOENT);
1057}
1058
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001059static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1060{
1061 struct nfs4_opendata *opendata;
1062
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001063 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001064 if (opendata == NULL)
1065 return ERR_PTR(-ENOMEM);
1066 opendata->state = state;
1067 atomic_inc(&state->count);
1068 return opendata;
1069}
1070
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001071static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001072{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001073 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001074 int ret;
1075
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001076 opendata->o_arg.open_flags = 0;
1077 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001078 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1079 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1080 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001081 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001082 if (ret != 0)
1083 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001084 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001085 if (IS_ERR(newstate))
1086 return PTR_ERR(newstate);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001087 nfs4_close_state(&opendata->path, newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001088 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001089 return 0;
1090}
1091
1092static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1093{
Trond Myklebust864472e2006-01-03 09:55:15 +01001094 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001095 int ret;
1096
1097 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001098 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001099 smp_rmb();
1100 if (state->n_rdwr != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001101 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001102 if (ret != 0)
1103 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001104 if (newstate != state)
1105 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001106 }
1107 if (state->n_wronly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001108 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001109 if (ret != 0)
1110 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001111 if (newstate != state)
1112 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001113 }
1114 if (state->n_rdonly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001115 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001116 if (ret != 0)
1117 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001118 if (newstate != state)
1119 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001120 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001121 /*
1122 * We may have performed cached opens for all three recoveries.
1123 * Check if we need to update the current stateid.
1124 */
1125 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1126 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001127 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001128 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1129 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001130 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001131 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001132 return 0;
1133}
1134
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135/*
1136 * OPEN_RECLAIM:
1137 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001139static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001141 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001142 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001143 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 int status;
1145
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001146 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1147 if (IS_ERR(opendata))
1148 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001149 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1150 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001151 rcu_read_lock();
1152 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001153 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001154 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001155 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001156 opendata->o_arg.u.delegation_type = delegation_type;
1157 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001158 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 return status;
1160}
1161
Trond Myklebust539cd032007-06-05 11:46:42 -04001162static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163{
1164 struct nfs_server *server = NFS_SERVER(state->inode);
1165 struct nfs4_exception exception = { };
1166 int err;
1167 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001168 err = _nfs4_do_open_reclaim(ctx, state);
Jeff Layton2c643482010-01-07 09:42:03 -05001169 if (err != -NFS4ERR_DELAY && err != -EKEYEXPIRED)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001170 break;
1171 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 } while (exception.retry);
1173 return err;
1174}
1175
Trond Myklebust864472e2006-01-03 09:55:15 +01001176static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1177{
1178 struct nfs_open_context *ctx;
1179 int ret;
1180
1181 ctx = nfs4_state_find_open_context(state);
1182 if (IS_ERR(ctx))
1183 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001184 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001185 put_nfs_open_context(ctx);
1186 return ret;
1187}
1188
Trond Myklebust13437e12007-07-06 15:10:43 -04001189static 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 -07001190{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001191 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001192 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001194 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1195 if (IS_ERR(opendata))
1196 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001197 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -04001198 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001199 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +01001200 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001201 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001202 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203}
1204
Trond Myklebust13437e12007-07-06 15:10:43 -04001205int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206{
1207 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001208 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 int err;
1210 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001211 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 switch (err) {
1213 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001214 case -ENOENT:
1215 case -ESTALE:
1216 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001217 case -NFS4ERR_BADSESSION:
1218 case -NFS4ERR_BADSLOT:
1219 case -NFS4ERR_BAD_HIGH_SLOT:
1220 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1221 case -NFS4ERR_DEADSESSION:
1222 nfs4_schedule_state_recovery(
1223 server->nfs_client);
1224 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 case -NFS4ERR_STALE_CLIENTID:
1226 case -NFS4ERR_STALE_STATEID:
1227 case -NFS4ERR_EXPIRED:
1228 /* Don't recall a delegation if it was lost */
David Howells7539bba2006-08-22 20:06:09 -04001229 nfs4_schedule_state_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001230 goto out;
1231 case -ERESTARTSYS:
1232 /*
1233 * The show must go on: exit, but mark the
1234 * stateid as needing recovery.
1235 */
1236 case -NFS4ERR_ADMIN_REVOKED:
1237 case -NFS4ERR_BAD_STATEID:
1238 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
1239 case -ENOMEM:
1240 err = 0;
1241 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 }
1243 err = nfs4_handle_exception(server, err, &exception);
1244 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001245out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 return err;
1247}
1248
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001249static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1250{
1251 struct nfs4_opendata *data = calldata;
1252
1253 data->rpc_status = task->tk_status;
1254 if (RPC_ASSASSINATED(task))
1255 return;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001256 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001257 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1258 sizeof(data->o_res.stateid.data));
Trond Myklebustbb226292008-01-02 15:19:18 -05001259 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001260 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001261 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001262 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001263}
1264
1265static void nfs4_open_confirm_release(void *calldata)
1266{
1267 struct nfs4_opendata *data = calldata;
1268 struct nfs4_state *state = NULL;
1269
1270 /* If this request hasn't been cancelled, do nothing */
1271 if (data->cancelled == 0)
1272 goto out_free;
1273 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001274 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001275 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001276 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001277 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001278 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001279out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001280 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001281}
1282
1283static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001284 .rpc_call_done = nfs4_open_confirm_done,
1285 .rpc_release = nfs4_open_confirm_release,
1286};
1287
1288/*
1289 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1290 */
1291static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1292{
1293 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1294 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001295 struct rpc_message msg = {
1296 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1297 .rpc_argp = &data->c_arg,
1298 .rpc_resp = &data->c_res,
1299 .rpc_cred = data->owner->so_cred,
1300 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001301 struct rpc_task_setup task_setup_data = {
1302 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001303 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001304 .callback_ops = &nfs4_open_confirm_ops,
1305 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001306 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001307 .flags = RPC_TASK_ASYNC,
1308 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 int status;
1310
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001311 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001312 data->rpc_done = 0;
1313 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001314 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001315 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001316 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001317 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001318 status = nfs4_wait_for_completion_rpc_task(task);
1319 if (status != 0) {
1320 data->cancelled = 1;
1321 smp_wmb();
1322 } else
1323 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001324 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 return status;
1326}
1327
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001328static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001330 struct nfs4_opendata *data = calldata;
1331 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001332
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001333 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1334 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001335 /*
1336 * Check if we still need to send an OPEN call, or if we can use
1337 * a delegation instead.
1338 */
1339 if (data->state != NULL) {
1340 struct nfs_delegation *delegation;
1341
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001342 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001343 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001344 rcu_read_lock();
1345 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1346 if (delegation != NULL &&
Trond Myklebust15c831b2008-12-23 15:21:39 -05001347 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001348 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001349 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001350 }
1351 rcu_read_unlock();
1352 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001353 /* Update sequence id. */
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001354 data->o_arg.id = sp->so_owner_id.id;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001355 data->o_arg.clientid = sp->so_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001356 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001357 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001358 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1359 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001360 data->timestamp = jiffies;
Andy Adamsond8985282009-04-01 09:22:21 -04001361 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
1362 &data->o_arg.seq_args,
1363 &data->o_res.seq_res, 1, task))
1364 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001365 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001366 return;
1367out_no_action:
1368 task->tk_action = NULL;
1369
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001370}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001372static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1373{
1374 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1375 nfs4_open_prepare(task, calldata);
1376}
1377
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001378static void nfs4_open_done(struct rpc_task *task, void *calldata)
1379{
1380 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001382 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001383
Trond Myklebustd61e6122009-12-05 19:32:19 -05001384 nfs4_sequence_done(data->o_arg.server, &data->o_res.seq_res,
1385 task->tk_status);
Andy Adamsond8985282009-04-01 09:22:21 -04001386
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001387 if (RPC_ASSASSINATED(task))
1388 return;
1389 if (task->tk_status == 0) {
1390 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001391 case S_IFREG:
1392 break;
1393 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001394 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001395 break;
1396 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001397 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001398 break;
1399 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001400 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001401 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001402 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001403 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1404 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001405 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001406 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001407}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001408
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001409static void nfs4_open_release(void *calldata)
1410{
1411 struct nfs4_opendata *data = calldata;
1412 struct nfs4_state *state = NULL;
1413
1414 /* If this request hasn't been cancelled, do nothing */
1415 if (data->cancelled == 0)
1416 goto out_free;
1417 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001418 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001419 goto out_free;
1420 /* In case we need an open_confirm, no cleanup! */
1421 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1422 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001423 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001424 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001425 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001426out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001427 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001428}
1429
1430static const struct rpc_call_ops nfs4_open_ops = {
1431 .rpc_call_prepare = nfs4_open_prepare,
1432 .rpc_call_done = nfs4_open_done,
1433 .rpc_release = nfs4_open_release,
1434};
1435
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001436static const struct rpc_call_ops nfs4_recover_open_ops = {
1437 .rpc_call_prepare = nfs4_recover_open_prepare,
1438 .rpc_call_done = nfs4_open_done,
1439 .rpc_release = nfs4_open_release,
1440};
1441
1442static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001443{
1444 struct inode *dir = data->dir->d_inode;
1445 struct nfs_server *server = NFS_SERVER(dir);
1446 struct nfs_openargs *o_arg = &data->o_arg;
1447 struct nfs_openres *o_res = &data->o_res;
1448 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001449 struct rpc_message msg = {
1450 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1451 .rpc_argp = o_arg,
1452 .rpc_resp = o_res,
1453 .rpc_cred = data->owner->so_cred,
1454 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001455 struct rpc_task_setup task_setup_data = {
1456 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001457 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001458 .callback_ops = &nfs4_open_ops,
1459 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001460 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001461 .flags = RPC_TASK_ASYNC,
1462 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001463 int status;
1464
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001465 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001466 data->rpc_done = 0;
1467 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001468 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001469 if (isrecover)
1470 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001471 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001472 if (IS_ERR(task))
1473 return PTR_ERR(task);
1474 status = nfs4_wait_for_completion_rpc_task(task);
1475 if (status != 0) {
1476 data->cancelled = 1;
1477 smp_wmb();
1478 } else
1479 status = data->rpc_status;
1480 rpc_put_task(task);
1481
1482 return status;
1483}
1484
1485static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1486{
1487 struct inode *dir = data->dir->d_inode;
1488 struct nfs_openres *o_res = &data->o_res;
1489 int status;
1490
1491 status = nfs4_run_open_task(data, 1);
1492 if (status != 0 || !data->rpc_done)
1493 return status;
1494
1495 nfs_refresh_inode(dir, o_res->dir_attr);
1496
1497 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1498 status = _nfs4_proc_open_confirm(data);
1499 if (status != 0)
1500 return status;
1501 }
1502
1503 return status;
1504}
1505
1506/*
1507 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1508 */
1509static int _nfs4_proc_open(struct nfs4_opendata *data)
1510{
1511 struct inode *dir = data->dir->d_inode;
1512 struct nfs_server *server = NFS_SERVER(dir);
1513 struct nfs_openargs *o_arg = &data->o_arg;
1514 struct nfs_openres *o_res = &data->o_res;
1515 int status;
1516
1517 status = nfs4_run_open_task(data, 0);
Trond Myklebust3e309912007-07-07 13:19:59 -04001518 if (status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001519 return status;
1520
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001521 if (o_arg->open_flags & O_CREAT) {
1522 update_changeattr(dir, &o_res->cinfo);
1523 nfs_post_op_update_inode(dir, o_res->dir_attr);
1524 } else
1525 nfs_refresh_inode(dir, o_res->dir_attr);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001526 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1527 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001529 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001531 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 }
1533 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001534 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001535 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536}
1537
Trond Myklebust10afec92007-05-14 17:16:04 -04001538static int nfs4_recover_expired_lease(struct nfs_server *server)
Trond Myklebust58d97142006-01-03 09:55:24 +01001539{
David Howells7539bba2006-08-22 20:06:09 -04001540 struct nfs_client *clp = server->nfs_client;
Trond Myklebusta78cb572009-08-09 15:06:19 -04001541 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001542 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001543
Trond Myklebusta78cb572009-08-09 15:06:19 -04001544 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001545 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001546 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001547 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001548 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1549 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001550 break;
Trond Myklebust58d97142006-01-03 09:55:24 +01001551 nfs4_schedule_state_recovery(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001552 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001553 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001554 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001555}
1556
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557/*
1558 * OPEN_EXPIRED:
1559 * reclaim state on the server after a network partition.
1560 * Assumes caller holds the appropriate lock
1561 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001562static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001564 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001565 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001567 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1568 if (IS_ERR(opendata))
1569 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001570 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001571 if (ret == -ESTALE)
Trond Myklebust539cd032007-06-05 11:46:42 -04001572 d_drop(ctx->path.dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001573 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001574 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575}
1576
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001577static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001578{
Trond Myklebust539cd032007-06-05 11:46:42 -04001579 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001580 struct nfs4_exception exception = { };
1581 int err;
1582
1583 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001584 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001585 switch (err) {
1586 default:
1587 goto out;
1588 case -NFS4ERR_GRACE:
1589 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -05001590 case -EKEYEXPIRED:
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001591 nfs4_handle_exception(server, err, &exception);
1592 err = 0;
1593 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001594 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001595out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001596 return err;
1597}
1598
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1600{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001602 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
Trond Myklebust864472e2006-01-03 09:55:15 +01001604 ctx = nfs4_state_find_open_context(state);
1605 if (IS_ERR(ctx))
1606 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001607 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001608 put_nfs_open_context(ctx);
1609 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610}
1611
1612/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001613 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1614 * fields corresponding to attributes that were used to store the verifier.
1615 * Make sure we clobber those fields in the later setattr call
1616 */
1617static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1618{
1619 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1620 !(sattr->ia_valid & ATTR_ATIME_SET))
1621 sattr->ia_valid |= ATTR_ATIME;
1622
1623 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1624 !(sattr->ia_valid & ATTR_MTIME_SET))
1625 sattr->ia_valid |= ATTR_MTIME;
1626}
1627
1628/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001629 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001631static 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 -07001632{
1633 struct nfs4_state_owner *sp;
1634 struct nfs4_state *state = NULL;
1635 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001636 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001637 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
1639 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 status = -ENOMEM;
1641 if (!(sp = nfs4_get_state_owner(server, cred))) {
1642 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1643 goto out_err;
1644 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001645 status = nfs4_recover_expired_lease(server);
1646 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001647 goto err_put_state_owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001648 if (path->dentry->d_inode != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001649 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001650 status = -ENOMEM;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001651 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001652 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001653 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
Trond Myklebustaac00a82007-07-05 19:02:21 -04001655 if (path->dentry->d_inode != NULL)
1656 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1657
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001658 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001660 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001662 if (opendata->o_arg.open_flags & O_EXCL)
1663 nfs4_exclusive_attrset(opendata, sattr);
1664
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001665 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001666 status = PTR_ERR(state);
1667 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001668 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001669 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001670 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001671 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 *res = state;
1674 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001675err_opendata_put:
1676 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001677err_put_state_owner:
1678 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 *res = NULL;
1681 return status;
1682}
1683
1684
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001685static 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 -07001686{
1687 struct nfs4_exception exception = { };
1688 struct nfs4_state *res;
1689 int status;
1690
1691 do {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001692 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 if (status == 0)
1694 break;
1695 /* NOTE: BAD_SEQID means the server and client disagree about the
1696 * book-keeping w.r.t. state-changing operations
1697 * (OPEN/CLOSE/LOCK/LOCKU...)
1698 * It is actually a sign of a bug on the client or on the server.
1699 *
1700 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001701 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 * have unhashed the old state_owner for us, and that we can
1703 * therefore safely retry using a new one. We should still warn
1704 * the user though...
1705 */
1706 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001707 printk(KERN_WARNING "NFS: v4 server %s "
1708 " returned a bad sequence-id error!\n",
1709 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 exception.retry = 1;
1711 continue;
1712 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001713 /*
1714 * BAD_STATEID on OPEN means that the server cancelled our
1715 * state before it received the OPEN_CONFIRM.
1716 * Recover by retrying the request as per the discussion
1717 * on Page 181 of RFC3530.
1718 */
1719 if (status == -NFS4ERR_BAD_STATEID) {
1720 exception.retry = 1;
1721 continue;
1722 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001723 if (status == -EAGAIN) {
1724 /* We must have found a delegation */
1725 exception.retry = 1;
1726 continue;
1727 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1729 status, &exception));
1730 } while (exception.retry);
1731 return res;
1732}
1733
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001734static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1735 struct nfs_fattr *fattr, struct iattr *sattr,
1736 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001738 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001740 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 .iap = sattr,
1742 .server = server,
1743 .bitmask = server->attr_bitmask,
1744 };
1745 struct nfs_setattrres res = {
1746 .fattr = fattr,
1747 .server = server,
1748 };
1749 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001750 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1751 .rpc_argp = &arg,
1752 .rpc_resp = &res,
1753 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001755 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001756 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757
Trond Myklebust0e574af2005-10-27 22:12:38 -04001758 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001760 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1761 /* Use that stateid */
1762 } else if (state != NULL) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001763 nfs4_copy_stateid(&arg.stateid, state, current->files);
1764 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1766
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001767 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001768 if (status == 0 && state != NULL)
1769 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001770 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771}
1772
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001773static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1774 struct nfs_fattr *fattr, struct iattr *sattr,
1775 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001777 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 struct nfs4_exception exception = { };
1779 int err;
1780 do {
1781 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001782 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 &exception);
1784 } while (exception.retry);
1785 return err;
1786}
1787
1788struct nfs4_closedata {
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001789 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 struct inode *inode;
1791 struct nfs4_state *state;
1792 struct nfs_closeargs arg;
1793 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001794 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001795 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796};
1797
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001798static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001799{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001800 struct nfs4_closedata *calldata = data;
1801 struct nfs4_state_owner *sp = calldata->state->owner;
Trond Myklebust95121352005-10-18 14:20:12 -07001802
1803 nfs4_put_open_state(calldata->state);
1804 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001805 nfs4_put_state_owner(sp);
Jan Blunck31f31db12008-05-02 13:42:45 -07001806 path_put(&calldata->path);
Trond Myklebust95121352005-10-18 14:20:12 -07001807 kfree(calldata);
1808}
1809
Trond Myklebust88069f72009-12-08 08:33:16 -05001810static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1811 fmode_t fmode)
1812{
1813 spin_lock(&state->owner->so_lock);
1814 if (!(fmode & FMODE_READ))
1815 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1816 if (!(fmode & FMODE_WRITE))
1817 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1818 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1819 spin_unlock(&state->owner->so_lock);
1820}
1821
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001822static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001824 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 struct nfs_server *server = NFS_SERVER(calldata->inode);
1827
Andy Adamson19ddab02009-04-01 09:22:20 -04001828 nfs4_sequence_done(server, &calldata->res.seq_res, task->tk_status);
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001829 if (RPC_ASSASSINATED(task))
1830 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 /* hmm. we are done with the inode, and in the process of freeing
1832 * the state_owner. we keep this around to process errors
1833 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 switch (task->tk_status) {
1835 case 0:
Trond Myklebust45328c32007-07-26 17:47:34 -04001836 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001837 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05001838 nfs4_close_clear_stateid_flags(state,
1839 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 break;
1841 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001842 case -NFS4ERR_OLD_STATEID:
1843 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001845 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001846 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05001848 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1849 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 }
Trond Myklebust72211db2009-12-15 14:47:36 -05001851 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04001852 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853}
1854
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001855static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001857 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001858 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05001859 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07001860
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001861 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001862 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001863
Trond Myklebust88069f72009-12-08 08:33:16 -05001864 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1865 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05001866 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001867 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01001868 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001869 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001870 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
1871 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1872 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04001873 }
1874 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001875 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
1876 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1877 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04001878 }
Trond Myklebuste7616922006-01-03 09:55:13 +01001879 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05001880 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05001881
1882 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001883 /* Note: exit _without_ calling nfs4_close_done */
1884 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07001885 return;
1886 }
Trond Myklebust88069f72009-12-08 08:33:16 -05001887
1888 if (calldata->arg.fmode == 0)
1889 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
1890
Trond Myklebust516a6af2005-10-27 22:12:41 -04001891 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001892 calldata->timestamp = jiffies;
Andy Adamson19ddab02009-04-01 09:22:20 -04001893 if (nfs4_setup_sequence((NFS_SERVER(calldata->inode))->nfs_client,
1894 &calldata->arg.seq_args, &calldata->res.seq_res,
1895 1, task))
1896 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001897 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898}
1899
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001900static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001901 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001902 .rpc_call_done = nfs4_close_done,
1903 .rpc_release = nfs4_free_closedata,
1904};
1905
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906/*
1907 * It is possible for data to be read/written from a mem-mapped file
1908 * after the sys_close call (which hits the vfs layer as a flush).
1909 * This means that we can't safely call nfsv4 close on a file until
1910 * the inode is cleared. This in turn means that we are not good
1911 * NFSv4 citizens - we do not indicate to the server to update the file's
1912 * share state even when we are done with one of the three share
1913 * stateid's in the inode.
1914 *
1915 * NOTE: Caller must be holding the sp->so_owner semaphore!
1916 */
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001917int nfs4_do_close(struct path *path, struct nfs4_state *state, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001919 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04001921 struct nfs4_state_owner *sp = state->owner;
1922 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001923 struct rpc_message msg = {
1924 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1925 .rpc_cred = state->owner->so_cred,
1926 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001927 struct rpc_task_setup task_setup_data = {
1928 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001929 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001930 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05001931 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001932 .flags = RPC_TASK_ASYNC,
1933 };
Trond Myklebust95121352005-10-18 14:20:12 -07001934 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
Andy Adamson19ddab02009-04-01 09:22:20 -04001936 calldata = kzalloc(sizeof(*calldata), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07001938 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001939 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001941 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04001942 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 /* Serialization for the sequence id */
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001944 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001945 if (calldata->arg.seqid == NULL)
1946 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001947 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04001948 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001949 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04001950 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001951 calldata->res.server = server;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04001952 calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Al Virof6948692010-01-30 13:51:04 -05001953 path_get(path);
1954 calldata->path = *path;
Trond Myklebust95121352005-10-18 14:20:12 -07001955
Trond Myklebust5138fde2007-07-14 15:40:01 -04001956 msg.rpc_argp = &calldata->arg,
1957 msg.rpc_resp = &calldata->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001958 task_setup_data.callback_data = calldata;
1959 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001960 if (IS_ERR(task))
1961 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001962 status = 0;
1963 if (wait)
1964 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001965 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001966 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07001967out_free_calldata:
1968 kfree(calldata);
1969out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04001970 nfs4_put_open_state(state);
1971 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07001972 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973}
1974
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001975static 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 -07001976{
1977 struct file *filp;
Trond Myklebust1b45c462007-07-03 13:04:56 -04001978 int ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001979
Trond Myklebust1b45c462007-07-03 13:04:56 -04001980 /* If the open_intent is for execute, we have an extra check to make */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001981 if (fmode & FMODE_EXEC) {
Trond Myklebustaf22f942007-08-10 17:45:10 -04001982 ret = nfs_may_open(state->inode,
Trond Myklebust1b45c462007-07-03 13:04:56 -04001983 state->owner->so_cred,
1984 nd->intent.open.flags);
1985 if (ret < 0)
1986 goto out_close;
1987 }
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001988 filp = lookup_instantiate_filp(nd, path->dentry, NULL);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001989 if (!IS_ERR(filp)) {
1990 struct nfs_open_context *ctx;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04001991 ctx = nfs_file_open_context(filp);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001992 ctx->state = state;
Trond Myklebust95cf9592006-04-18 13:14:06 -04001993 return 0;
1994 }
Trond Myklebust1b45c462007-07-03 13:04:56 -04001995 ret = PTR_ERR(filp);
1996out_close:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001997 nfs4_close_sync(path, state, fmode & (FMODE_READ|FMODE_WRITE));
Trond Myklebust1b45c462007-07-03 13:04:56 -04001998 return ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001999}
2000
2001struct dentry *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
2003{
Trond Myklebustad389da2007-06-05 12:30:00 -04002004 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08002005 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04002006 .dentry = dentry,
2007 };
Jan Blunck4ac91372008-02-14 19:34:32 -08002008 struct dentry *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 struct iattr attr;
2010 struct rpc_cred *cred;
2011 struct nfs4_state *state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002012 struct dentry *res;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002013 fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
2015 if (nd->flags & LOOKUP_CREATE) {
2016 attr.ia_mode = nd->intent.open.create_mode;
2017 attr.ia_valid = ATTR_MODE;
2018 if (!IS_POSIXACL(dir))
Al Viroce3b0f82009-03-29 19:08:22 -04002019 attr.ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 } else {
2021 attr.ia_valid = 0;
2022 BUG_ON(nd->intent.open.flags & O_CREAT);
2023 }
2024
Trond Myklebust98a8e322008-03-12 12:25:28 -04002025 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 if (IS_ERR(cred))
Trond Myklebust02a913a2005-10-18 14:20:17 -07002027 return (struct dentry *)cred;
Trond Myklebust565277f2007-10-15 18:17:53 -04002028 parent = dentry->d_parent;
2029 /* Protect against concurrent sillydeletes */
2030 nfs_block_sillyrename(parent);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002031 state = nfs4_do_open(dir, &path, fmode, nd->intent.open.flags, &attr, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002033 if (IS_ERR(state)) {
Trond Myklebustd75340c2007-10-01 21:42:01 -04002034 if (PTR_ERR(state) == -ENOENT) {
Trond Myklebust02a913a2005-10-18 14:20:17 -07002035 d_add(dentry, NULL);
Trond Myklebustd75340c2007-10-01 21:42:01 -04002036 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2037 }
Trond Myklebust565277f2007-10-15 18:17:53 -04002038 nfs_unblock_sillyrename(parent);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002039 return (struct dentry *)state;
2040 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002041 res = d_add_unique(dentry, igrab(state->inode));
Trond Myklebust02a913a2005-10-18 14:20:17 -07002042 if (res != NULL)
Trond Myklebustdeee9362007-08-27 11:33:00 -04002043 path.dentry = res;
Trond Myklebustd75340c2007-10-01 21:42:01 -04002044 nfs_set_verifier(path.dentry, nfs_save_change_attribute(dir));
Trond Myklebust565277f2007-10-15 18:17:53 -04002045 nfs_unblock_sillyrename(parent);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002046 nfs4_intent_set_file(nd, &path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002047 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048}
2049
2050int
Trond Myklebust02a913a2005-10-18 14:20:17 -07002051nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052{
Trond Myklebustad389da2007-06-05 12:30:00 -04002053 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08002054 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04002055 .dentry = dentry,
2056 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 struct rpc_cred *cred;
2058 struct nfs4_state *state;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002059 fmode_t fmode = openflags & (FMODE_READ | FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
Trond Myklebust98a8e322008-03-12 12:25:28 -04002061 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 if (IS_ERR(cred))
2063 return PTR_ERR(cred);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002064 state = nfs4_do_open(dir, &path, fmode, openflags, NULL, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002066 if (IS_ERR(state)) {
2067 switch (PTR_ERR(state)) {
2068 case -EPERM:
2069 case -EACCES:
2070 case -EDQUOT:
2071 case -ENOSPC:
2072 case -EROFS:
Al Viro04287f92010-04-08 00:06:07 +01002073 return PTR_ERR(state);
Chuck Leverb87c0ad2006-10-19 23:28:42 -07002074 default:
2075 goto out_drop;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002076 }
Trond Myklebust02a913a2005-10-18 14:20:17 -07002077 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002078 if (state->inode == dentry->d_inode) {
Trond Myklebustd75340c2007-10-01 21:42:01 -04002079 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002080 nfs4_intent_set_file(nd, &path, state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 return 1;
2082 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002083 nfs4_close_sync(&path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002084out_drop:
2085 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 return 0;
2087}
2088
Trond Myklebust1185a552009-12-03 15:54:02 -05002089static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002090{
2091 if (ctx->state == NULL)
2092 return;
2093 if (is_sync)
2094 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
2095 else
2096 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
2097}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
2099static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2100{
Benny Halevy43652ad2009-04-01 09:21:54 -04002101 struct nfs4_server_caps_arg args = {
2102 .fhandle = fhandle,
2103 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 struct nfs4_server_caps_res res = {};
2105 struct rpc_message msg = {
2106 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002107 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 .rpc_resp = &res,
2109 };
2110 int status;
2111
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002112 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 if (status == 0) {
2114 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002115 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2116 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2117 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2118 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2119 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2121 server->caps |= NFS_CAP_ACLS;
2122 if (res.has_links != 0)
2123 server->caps |= NFS_CAP_HARDLINKS;
2124 if (res.has_symlinks != 0)
2125 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002126 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2127 server->caps |= NFS_CAP_FILEID;
2128 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2129 server->caps |= NFS_CAP_MODE;
2130 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2131 server->caps |= NFS_CAP_NLINK;
2132 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2133 server->caps |= NFS_CAP_OWNER;
2134 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2135 server->caps |= NFS_CAP_OWNER_GROUP;
2136 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2137 server->caps |= NFS_CAP_ATIME;
2138 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2139 server->caps |= NFS_CAP_CTIME;
2140 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2141 server->caps |= NFS_CAP_MTIME;
2142
Trond Myklebusta65318b2009-03-11 14:10:28 -04002143 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2144 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2145 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 server->acl_bitmask = res.acl_bitmask;
2147 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002148
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 return status;
2150}
2151
Trond Myklebust55a97592006-06-09 09:34:19 -04002152int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153{
2154 struct nfs4_exception exception = { };
2155 int err;
2156 do {
2157 err = nfs4_handle_exception(server,
2158 _nfs4_server_capabilities(server, fhandle),
2159 &exception);
2160 } while (exception.retry);
2161 return err;
2162}
2163
2164static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2165 struct nfs_fsinfo *info)
2166{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 struct nfs4_lookup_root_arg args = {
2168 .bitmask = nfs4_fattr_bitmap,
2169 };
2170 struct nfs4_lookup_res res = {
2171 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002172 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 .fh = fhandle,
2174 };
2175 struct rpc_message msg = {
2176 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2177 .rpc_argp = &args,
2178 .rpc_resp = &res,
2179 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002180
Trond Myklebust0e574af2005-10-27 22:12:38 -04002181 nfs_fattr_init(info->fattr);
Trond Myklebustfccba802009-07-21 16:48:07 -04002182 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183}
2184
2185static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2186 struct nfs_fsinfo *info)
2187{
2188 struct nfs4_exception exception = { };
2189 int err;
2190 do {
2191 err = nfs4_handle_exception(server,
2192 _nfs4_lookup_root(server, fhandle, info),
2193 &exception);
2194 } while (exception.retry);
2195 return err;
2196}
2197
David Howells54ceac42006-08-22 20:06:13 -04002198/*
2199 * get the file handle for the "/" directory on the server
2200 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002202 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 int status;
2205
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 status = nfs4_lookup_root(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 if (status == 0)
2208 status = nfs4_server_capabilities(server, fhandle);
2209 if (status == 0)
2210 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002211 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212}
2213
Manoj Naik6b97fd32006-06-09 09:34:29 -04002214/*
2215 * Get locations and (maybe) other attributes of a referral.
2216 * Note that we'll actually follow the referral later when
2217 * we detect fsid mismatch in inode revalidation
2218 */
Trond Myklebust56659e92007-07-17 21:52:39 -04002219static 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 -04002220{
2221 int status = -ENOMEM;
2222 struct page *page = NULL;
2223 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002224
2225 page = alloc_page(GFP_KERNEL);
2226 if (page == NULL)
2227 goto out;
2228 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2229 if (locations == NULL)
2230 goto out;
2231
Trond Myklebustc228fd32007-01-13 02:28:11 -05002232 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002233 if (status != 0)
2234 goto out;
2235 /* Make sure server returned a different fsid for the referral */
2236 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -07002237 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 -04002238 status = -EIO;
2239 goto out;
2240 }
2241
2242 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2243 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
2244 if (!fattr->mode)
2245 fattr->mode = S_IFDIR;
2246 memset(fhandle, 0, sizeof(struct nfs_fh));
2247out:
2248 if (page)
2249 __free_page(page);
2250 if (locations)
2251 kfree(locations);
2252 return status;
2253}
2254
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2256{
2257 struct nfs4_getattr_arg args = {
2258 .fh = fhandle,
2259 .bitmask = server->attr_bitmask,
2260 };
2261 struct nfs4_getattr_res res = {
2262 .fattr = fattr,
2263 .server = server,
2264 };
2265 struct rpc_message msg = {
2266 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2267 .rpc_argp = &args,
2268 .rpc_resp = &res,
2269 };
2270
Trond Myklebust0e574af2005-10-27 22:12:38 -04002271 nfs_fattr_init(fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002272 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273}
2274
2275static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2276{
2277 struct nfs4_exception exception = { };
2278 int err;
2279 do {
2280 err = nfs4_handle_exception(server,
2281 _nfs4_proc_getattr(server, fhandle, fattr),
2282 &exception);
2283 } while (exception.retry);
2284 return err;
2285}
2286
2287/*
2288 * The file is not closed if it is opened due to the a request to change
2289 * the size of the file. The open call will not be needed once the
2290 * VFS layer lookup-intents are implemented.
2291 *
2292 * Close is called when the inode is destroyed.
2293 * If we haven't opened the file for O_WRONLY, we
2294 * need to in the size_change case to obtain a stateid.
2295 *
2296 * Got race?
2297 * Because OPEN is always done by name in nfsv4, it is
2298 * possible that we opened a different file by the same
2299 * name. We can recognize this race condition, but we
2300 * can't do anything about it besides returning an error.
2301 *
2302 * This will be fixed with VFS changes (lookup-intent).
2303 */
2304static int
2305nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2306 struct iattr *sattr)
2307{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002308 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002309 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002310 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 int status;
2312
Trond Myklebust0e574af2005-10-27 22:12:38 -04002313 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314
Trond Myklebustd5308382005-11-04 15:33:38 -05002315 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002316 if (sattr->ia_valid & ATTR_FILE) {
2317 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002318
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002319 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002320 if (ctx) {
2321 cred = ctx->cred;
2322 state = ctx->state;
2323 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002324 }
2325
2326 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002327 if (status == 0)
2328 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 return status;
2330}
2331
Trond Myklebust56659e92007-07-17 21:52:39 -04002332static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
2333 const struct qstr *name, struct nfs_fh *fhandle,
David Howells2b3de442006-08-22 20:06:09 -04002334 struct nfs_fattr *fattr)
2335{
2336 int status;
2337 struct nfs4_lookup_arg args = {
2338 .bitmask = server->attr_bitmask,
2339 .dir_fh = dirfh,
2340 .name = name,
2341 };
2342 struct nfs4_lookup_res res = {
2343 .server = server,
2344 .fattr = fattr,
2345 .fh = fhandle,
2346 };
2347 struct rpc_message msg = {
2348 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2349 .rpc_argp = &args,
2350 .rpc_resp = &res,
2351 };
2352
2353 nfs_fattr_init(fattr);
2354
2355 dprintk("NFS call lookupfh %s\n", name->name);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002356 status = nfs4_call_sync(server, &msg, &args, &res, 0);
David Howells2b3de442006-08-22 20:06:09 -04002357 dprintk("NFS reply lookupfh: %d\n", status);
David Howells2b3de442006-08-22 20:06:09 -04002358 return status;
2359}
2360
2361static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2362 struct qstr *name, struct nfs_fh *fhandle,
2363 struct nfs_fattr *fattr)
2364{
2365 struct nfs4_exception exception = { };
2366 int err;
2367 do {
Trond Myklebust4e56e082007-07-01 18:13:52 -04002368 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
2369 /* FIXME: !!!! */
2370 if (err == -NFS4ERR_MOVED) {
2371 err = -EREMOTE;
2372 break;
2373 }
2374 err = nfs4_handle_exception(server, err, &exception);
David Howells2b3de442006-08-22 20:06:09 -04002375 } while (exception.retry);
2376 return err;
2377}
2378
Trond Myklebust56659e92007-07-17 21:52:39 -04002379static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2381{
Trond Myklebust4e56e082007-07-01 18:13:52 -04002382 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383
2384 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust4e56e082007-07-01 18:13:52 -04002385 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002386 if (status == -NFS4ERR_MOVED)
2387 status = nfs4_get_referral(dir, name, fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 dprintk("NFS reply lookup: %d\n", status);
2389 return status;
2390}
2391
2392static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2393{
2394 struct nfs4_exception exception = { };
2395 int err;
2396 do {
2397 err = nfs4_handle_exception(NFS_SERVER(dir),
2398 _nfs4_proc_lookup(dir, name, fhandle, fattr),
2399 &exception);
2400 } while (exception.retry);
2401 return err;
2402}
2403
2404static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2405{
Trond Myklebust76b32992007-08-10 17:45:11 -04002406 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 struct nfs4_accessargs args = {
2408 .fh = NFS_FH(inode),
Trond Myklebust76b32992007-08-10 17:45:11 -04002409 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002411 struct nfs4_accessres res = {
2412 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002413 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 struct rpc_message msg = {
2415 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2416 .rpc_argp = &args,
2417 .rpc_resp = &res,
2418 .rpc_cred = entry->cred,
2419 };
2420 int mode = entry->mask;
2421 int status;
2422
2423 /*
2424 * Determine which access bits we want to ask for...
2425 */
2426 if (mode & MAY_READ)
2427 args.access |= NFS4_ACCESS_READ;
2428 if (S_ISDIR(inode->i_mode)) {
2429 if (mode & MAY_WRITE)
2430 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2431 if (mode & MAY_EXEC)
2432 args.access |= NFS4_ACCESS_LOOKUP;
2433 } else {
2434 if (mode & MAY_WRITE)
2435 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2436 if (mode & MAY_EXEC)
2437 args.access |= NFS4_ACCESS_EXECUTE;
2438 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002439
2440 res.fattr = nfs_alloc_fattr();
2441 if (res.fattr == NULL)
2442 return -ENOMEM;
2443
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002444 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 if (!status) {
2446 entry->mask = 0;
2447 if (res.access & NFS4_ACCESS_READ)
2448 entry->mask |= MAY_READ;
2449 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2450 entry->mask |= MAY_WRITE;
2451 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2452 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002453 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002455 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 return status;
2457}
2458
2459static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2460{
2461 struct nfs4_exception exception = { };
2462 int err;
2463 do {
2464 err = nfs4_handle_exception(NFS_SERVER(inode),
2465 _nfs4_proc_access(inode, entry),
2466 &exception);
2467 } while (exception.retry);
2468 return err;
2469}
2470
2471/*
2472 * TODO: For the time being, we don't try to get any attributes
2473 * along with any of the zero-copy operations READ, READDIR,
2474 * READLINK, WRITE.
2475 *
2476 * In the case of the first three, we want to put the GETATTR
2477 * after the read-type operation -- this is because it is hard
2478 * to predict the length of a GETATTR response in v4, and thus
2479 * align the READ data correctly. This means that the GETATTR
2480 * may end up partially falling into the page cache, and we should
2481 * shift it into the 'tail' of the xdr_buf before processing.
2482 * To do this efficiently, we need to know the total length
2483 * of data received, which doesn't seem to be available outside
2484 * of the RPC layer.
2485 *
2486 * In the case of WRITE, we also want to put the GETATTR after
2487 * the operation -- in this case because we want to make sure
2488 * we get the post-operation mtime and size. This means that
2489 * we can't use xdr_encode_pages() as written: we need a variant
2490 * of it which would leave room in the 'tail' iovec.
2491 *
2492 * Both of these changes to the XDR layer would in fact be quite
2493 * minor, but I decided to leave them for a subsequent patch.
2494 */
2495static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2496 unsigned int pgbase, unsigned int pglen)
2497{
2498 struct nfs4_readlink args = {
2499 .fh = NFS_FH(inode),
2500 .pgbase = pgbase,
2501 .pglen = pglen,
2502 .pages = &page,
2503 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002504 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 struct rpc_message msg = {
2506 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2507 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002508 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 };
2510
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002511 return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512}
2513
2514static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2515 unsigned int pgbase, unsigned int pglen)
2516{
2517 struct nfs4_exception exception = { };
2518 int err;
2519 do {
2520 err = nfs4_handle_exception(NFS_SERVER(inode),
2521 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2522 &exception);
2523 } while (exception.retry);
2524 return err;
2525}
2526
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527/*
2528 * Got race?
2529 * We will need to arrange for the VFS layer to provide an atomic open.
2530 * Until then, this create/open method is prone to inefficiency and race
2531 * conditions due to the lookup, create, and open VFS calls from sys_open()
2532 * placed on the wire.
2533 *
2534 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2535 * The file will be opened again in the subsequent VFS open call
2536 * (nfs4_proc_file_open).
2537 *
2538 * The open for read will just hang around to be used by any process that
2539 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2540 */
2541
2542static int
2543nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebust02a913a2005-10-18 14:20:17 -07002544 int flags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545{
Trond Myklebustad389da2007-06-05 12:30:00 -04002546 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08002547 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04002548 .dentry = dentry,
2549 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 struct nfs4_state *state;
2551 struct rpc_cred *cred;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002552 fmode_t fmode = flags & (FMODE_READ | FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 int status = 0;
2554
Trond Myklebust98a8e322008-03-12 12:25:28 -04002555 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 if (IS_ERR(cred)) {
2557 status = PTR_ERR(cred);
2558 goto out;
2559 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002560 state = nfs4_do_open(dir, &path, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002561 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 if (IS_ERR(state)) {
2563 status = PTR_ERR(state);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002564 goto out_putcred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002566 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002567 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 if (flags & O_EXCL) {
2569 struct nfs_fattr fattr;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002570 status = nfs4_do_setattr(state->inode, cred, &fattr, sattr, state);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002571 if (status == 0)
Trond Myklebust65e43082005-08-16 11:49:44 -04002572 nfs_setattr_update_inode(state->inode, sattr);
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002573 nfs_post_op_update_inode(state->inode, &fattr);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002574 }
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
3161/*
3162 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3163 * standalone procedure for queueing an asynchronous RENEW.
3164 */
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003165static void nfs4_renew_release(void *data)
3166{
3167 struct nfs_client *clp = data;
3168
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003169 if (atomic_read(&clp->cl_count) > 1)
3170 nfs4_schedule_state_renewal(clp);
3171 nfs_put_client(clp);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003172}
3173
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003174static void nfs4_renew_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175{
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003176 struct nfs_client *clp = data;
3177 unsigned long timestamp = task->tk_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178
3179 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003180 /* Unless we're shutting down, schedule state recovery! */
3181 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
3182 nfs4_schedule_state_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 return;
3184 }
3185 spin_lock(&clp->cl_lock);
3186 if (time_before(clp->cl_last_renewal,timestamp))
3187 clp->cl_last_renewal = timestamp;
3188 spin_unlock(&clp->cl_lock);
3189}
3190
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003191static const struct rpc_call_ops nfs4_renew_ops = {
3192 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003193 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003194};
3195
David Howellsadfa6f92006-08-22 20:06:08 -04003196int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197{
3198 struct rpc_message msg = {
3199 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3200 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003201 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 };
3203
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003204 if (!atomic_inc_not_zero(&clp->cl_count))
3205 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003207 &nfs4_renew_ops, clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208}
3209
David Howellsadfa6f92006-08-22 20:06:08 -04003210int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211{
3212 struct rpc_message msg = {
3213 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3214 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003215 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 };
3217 unsigned long now = jiffies;
3218 int status;
3219
3220 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3221 if (status < 0)
3222 return status;
3223 spin_lock(&clp->cl_lock);
3224 if (time_before(clp->cl_last_renewal,now))
3225 clp->cl_last_renewal = now;
3226 spin_unlock(&clp->cl_lock);
3227 return 0;
3228}
3229
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003230static inline int nfs4_server_supports_acls(struct nfs_server *server)
3231{
3232 return (server->caps & NFS_CAP_ACLS)
3233 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3234 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3235}
3236
3237/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3238 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3239 * the stack.
3240 */
3241#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3242
3243static void buf_to_pages(const void *buf, size_t buflen,
3244 struct page **pages, unsigned int *pgbase)
3245{
3246 const void *p = buf;
3247
3248 *pgbase = offset_in_page(buf);
3249 p -= *pgbase;
3250 while (p < buf + buflen) {
3251 *(pages++) = virt_to_page(p);
3252 p += PAGE_CACHE_SIZE;
3253 }
3254}
3255
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003256struct nfs4_cached_acl {
3257 int cached;
3258 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003259 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003260};
3261
3262static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003263{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003264 struct nfs_inode *nfsi = NFS_I(inode);
3265
3266 spin_lock(&inode->i_lock);
3267 kfree(nfsi->nfs4_acl);
3268 nfsi->nfs4_acl = acl;
3269 spin_unlock(&inode->i_lock);
3270}
3271
3272static void nfs4_zap_acl_attr(struct inode *inode)
3273{
3274 nfs4_set_cached_acl(inode, NULL);
3275}
3276
3277static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3278{
3279 struct nfs_inode *nfsi = NFS_I(inode);
3280 struct nfs4_cached_acl *acl;
3281 int ret = -ENOENT;
3282
3283 spin_lock(&inode->i_lock);
3284 acl = nfsi->nfs4_acl;
3285 if (acl == NULL)
3286 goto out;
3287 if (buf == NULL) /* user is just asking for length */
3288 goto out_len;
3289 if (acl->cached == 0)
3290 goto out;
3291 ret = -ERANGE; /* see getxattr(2) man page */
3292 if (acl->len > buflen)
3293 goto out;
3294 memcpy(buf, acl->data, acl->len);
3295out_len:
3296 ret = acl->len;
3297out:
3298 spin_unlock(&inode->i_lock);
3299 return ret;
3300}
3301
3302static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3303{
3304 struct nfs4_cached_acl *acl;
3305
3306 if (buf && acl_len <= PAGE_SIZE) {
3307 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3308 if (acl == NULL)
3309 goto out;
3310 acl->cached = 1;
3311 memcpy(acl->data, buf, acl_len);
3312 } else {
3313 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3314 if (acl == NULL)
3315 goto out;
3316 acl->cached = 0;
3317 }
3318 acl->len = acl_len;
3319out:
3320 nfs4_set_cached_acl(inode, acl);
3321}
3322
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003323static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003324{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003325 struct page *pages[NFS4ACL_MAXPAGES];
3326 struct nfs_getaclargs args = {
3327 .fh = NFS_FH(inode),
3328 .acl_pages = pages,
3329 .acl_len = buflen,
3330 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003331 struct nfs_getaclres res = {
3332 .acl_len = buflen,
3333 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003334 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003335 struct rpc_message msg = {
3336 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3337 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003338 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003339 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003340 struct page *localpage = NULL;
3341 int ret;
3342
3343 if (buflen < PAGE_SIZE) {
3344 /* As long as we're doing a round trip to the server anyway,
3345 * let's be prepared for a page of acl data. */
3346 localpage = alloc_page(GFP_KERNEL);
3347 resp_buf = page_address(localpage);
3348 if (localpage == NULL)
3349 return -ENOMEM;
3350 args.acl_pages[0] = localpage;
3351 args.acl_pgbase = 0;
Benny Halevy663c79b2009-04-01 09:21:59 -04003352 args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003353 } else {
3354 resp_buf = buf;
3355 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3356 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003357 ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003358 if (ret)
3359 goto out_free;
Benny Halevy663c79b2009-04-01 09:21:59 -04003360 if (res.acl_len > args.acl_len)
3361 nfs4_write_cached_acl(inode, NULL, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003362 else
Benny Halevy663c79b2009-04-01 09:21:59 -04003363 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003364 if (buf) {
3365 ret = -ERANGE;
Benny Halevy663c79b2009-04-01 09:21:59 -04003366 if (res.acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003367 goto out_free;
3368 if (localpage)
Benny Halevy663c79b2009-04-01 09:21:59 -04003369 memcpy(buf, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003370 }
Benny Halevy663c79b2009-04-01 09:21:59 -04003371 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003372out_free:
3373 if (localpage)
3374 __free_page(localpage);
3375 return ret;
3376}
3377
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003378static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3379{
3380 struct nfs4_exception exception = { };
3381 ssize_t ret;
3382 do {
3383 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3384 if (ret >= 0)
3385 break;
3386 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3387 } while (exception.retry);
3388 return ret;
3389}
3390
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003391static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3392{
3393 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003394 int ret;
3395
3396 if (!nfs4_server_supports_acls(server))
3397 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003398 ret = nfs_revalidate_inode(server, inode);
3399 if (ret < 0)
3400 return ret;
3401 ret = nfs4_read_cached_acl(inode, buf, buflen);
3402 if (ret != -ENOENT)
3403 return ret;
3404 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003405}
3406
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003407static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003408{
3409 struct nfs_server *server = NFS_SERVER(inode);
3410 struct page *pages[NFS4ACL_MAXPAGES];
3411 struct nfs_setaclargs arg = {
3412 .fh = NFS_FH(inode),
3413 .acl_pages = pages,
3414 .acl_len = buflen,
3415 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003416 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003417 struct rpc_message msg = {
3418 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3419 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003420 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003421 };
3422 int ret;
3423
3424 if (!nfs4_server_supports_acls(server))
3425 return -EOPNOTSUPP;
Trond Myklebust642ac542005-10-18 14:20:19 -07003426 nfs_inode_return_delegation(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003427 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003428 ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003429 nfs_access_zap_cache(inode);
3430 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003431 return ret;
3432}
3433
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003434static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3435{
3436 struct nfs4_exception exception = { };
3437 int err;
3438 do {
3439 err = nfs4_handle_exception(NFS_SERVER(inode),
3440 __nfs4_proc_set_acl(inode, buf, buflen),
3441 &exception);
3442 } while (exception.retry);
3443 return err;
3444}
3445
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446static int
Andy Adamson8328d592009-04-01 09:22:35 -04003447_nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs_client *clp, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 if (!clp || task->tk_status >= 0)
3450 return 0;
3451 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003452 case -NFS4ERR_ADMIN_REVOKED:
3453 case -NFS4ERR_BAD_STATEID:
3454 case -NFS4ERR_OPENMODE:
3455 if (state == NULL)
3456 break;
3457 nfs4_state_mark_reclaim_nograce(clp, state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003458 goto do_state_recovery;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003460 if (state == NULL)
3461 break;
3462 nfs4_state_mark_reclaim_reboot(clp, state);
3463 case -NFS4ERR_STALE_CLIENTID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 case -NFS4ERR_EXPIRED:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003465 goto do_state_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003466#if defined(CONFIG_NFS_V4_1)
3467 case -NFS4ERR_BADSESSION:
3468 case -NFS4ERR_BADSLOT:
3469 case -NFS4ERR_BAD_HIGH_SLOT:
3470 case -NFS4ERR_DEADSESSION:
3471 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3472 case -NFS4ERR_SEQ_FALSE_RETRY:
3473 case -NFS4ERR_SEQ_MISORDERED:
3474 dprintk("%s ERROR %d, Reset session\n", __func__,
3475 task->tk_status);
Andy Adamson0b9e2d42009-12-04 16:02:14 -05003476 nfs4_schedule_state_recovery(clp);
Andy Adamson4745e312009-04-01 09:22:42 -04003477 task->tk_status = 0;
3478 return -EAGAIN;
3479#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 case -NFS4ERR_DELAY:
Andy Adamson8328d592009-04-01 09:22:35 -04003481 if (server)
3482 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003483 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003484 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3486 task->tk_status = 0;
3487 return -EAGAIN;
3488 case -NFS4ERR_OLD_STATEID:
3489 task->tk_status = 0;
3490 return -EAGAIN;
3491 }
3492 task->tk_status = nfs4_map_errors(task->tk_status);
3493 return 0;
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003494do_state_recovery:
3495 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
3496 nfs4_schedule_state_recovery(clp);
3497 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3498 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3499 task->tk_status = 0;
3500 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501}
3502
Andy Adamson8328d592009-04-01 09:22:35 -04003503static int
3504nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3505{
3506 return _nfs4_async_handle_error(task, server, server->nfs_client, state);
3507}
3508
David Howellsadfa6f92006-08-22 20:06:08 -04003509int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, unsigned short port, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510{
3511 nfs4_verifier sc_verifier;
3512 struct nfs4_setclientid setclientid = {
3513 .sc_verifier = &sc_verifier,
3514 .sc_prog = program,
3515 };
3516 struct rpc_message msg = {
3517 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3518 .rpc_argp = &setclientid,
3519 .rpc_resp = clp,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003520 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 };
Al Virobc4785c2006-10-19 23:28:51 -07003522 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523 int loop = 0;
3524 int status;
3525
Al Virobc4785c2006-10-19 23:28:51 -07003526 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3528 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3529
3530 for(;;) {
3531 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003532 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003533 clp->cl_ipaddr,
3534 rpc_peeraddr2str(clp->cl_rpcclient,
3535 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003536 rpc_peeraddr2str(clp->cl_rpcclient,
3537 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003538 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539 clp->cl_id_uniquifier);
3540 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003541 sizeof(setclientid.sc_netid),
3542 rpc_peeraddr2str(clp->cl_rpcclient,
3543 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003545 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 clp->cl_ipaddr, port >> 8, port & 255);
3547
3548 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3549 if (status != -NFS4ERR_CLID_INUSE)
3550 break;
3551 if (signalled())
3552 break;
3553 if (loop++ & 1)
3554 ssleep(clp->cl_lease_time + 1);
3555 else
3556 if (++clp->cl_id_uniquifier == 0)
3557 break;
3558 }
3559 return status;
3560}
3561
David Howellsadfa6f92006-08-22 20:06:08 -04003562static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563{
3564 struct nfs_fsinfo fsinfo;
3565 struct rpc_message msg = {
3566 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
3567 .rpc_argp = clp,
3568 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003569 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 };
3571 unsigned long now;
3572 int status;
3573
3574 now = jiffies;
3575 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3576 if (status == 0) {
3577 spin_lock(&clp->cl_lock);
3578 clp->cl_lease_time = fsinfo.lease_time * HZ;
3579 clp->cl_last_renewal = now;
3580 spin_unlock(&clp->cl_lock);
3581 }
3582 return status;
3583}
3584
David Howellsadfa6f92006-08-22 20:06:08 -04003585int nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
Trond Myklebust51581f32006-03-20 13:44:47 -05003586{
Benny Halevy77e03672008-06-24 20:25:57 +03003587 long timeout = 0;
Trond Myklebust51581f32006-03-20 13:44:47 -05003588 int err;
3589 do {
3590 err = _nfs4_proc_setclientid_confirm(clp, cred);
3591 switch (err) {
3592 case 0:
3593 return err;
3594 case -NFS4ERR_RESOURCE:
3595 /* The IBM lawyers misread another document! */
3596 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -05003597 case -EKEYEXPIRED:
Trond Myklebust51581f32006-03-20 13:44:47 -05003598 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3599 }
3600 } while (err == 0);
3601 return err;
3602}
3603
Trond Myklebustfe650402006-01-03 09:55:18 +01003604struct nfs4_delegreturndata {
3605 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003606 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003607 struct nfs_fh fh;
3608 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003609 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003610 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003611 int rpc_status;
3612};
3613
Trond Myklebustfe650402006-01-03 09:55:18 +01003614static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3615{
3616 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003617
Trond Myklebustd61e6122009-12-05 19:32:19 -05003618 nfs4_sequence_done(data->res.server, &data->res.seq_res,
3619 task->tk_status);
Andy Adamson938e1012009-04-01 09:22:28 -04003620
Ricardo Labiaga79708862009-12-07 09:23:21 -05003621 switch (task->tk_status) {
3622 case -NFS4ERR_STALE_STATEID:
3623 case -NFS4ERR_EXPIRED:
3624 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01003625 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003626 break;
3627 default:
3628 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3629 -EAGAIN) {
3630 nfs_restart_rpc(task, data->res.server->nfs_client);
3631 return;
3632 }
3633 }
3634 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01003635}
3636
3637static void nfs4_delegreturn_release(void *calldata)
3638{
Trond Myklebustfe650402006-01-03 09:55:18 +01003639 kfree(calldata);
3640}
3641
Andy Adamson938e1012009-04-01 09:22:28 -04003642#if defined(CONFIG_NFS_V4_1)
3643static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3644{
3645 struct nfs4_delegreturndata *d_data;
3646
3647 d_data = (struct nfs4_delegreturndata *)data;
3648
3649 if (nfs4_setup_sequence(d_data->res.server->nfs_client,
3650 &d_data->args.seq_args,
3651 &d_data->res.seq_res, 1, task))
3652 return;
3653 rpc_call_start(task);
3654}
3655#endif /* CONFIG_NFS_V4_1 */
3656
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003657static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04003658#if defined(CONFIG_NFS_V4_1)
3659 .rpc_call_prepare = nfs4_delegreturn_prepare,
3660#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01003661 .rpc_call_done = nfs4_delegreturn_done,
3662 .rpc_release = nfs4_delegreturn_release,
3663};
3664
Trond Myklebuste6f81072008-01-24 18:14:34 -05003665static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003666{
3667 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003668 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003669 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003670 struct rpc_message msg = {
3671 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3672 .rpc_cred = cred,
3673 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003674 struct rpc_task_setup task_setup_data = {
3675 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003676 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003677 .callback_ops = &nfs4_delegreturn_ops,
3678 .flags = RPC_TASK_ASYNC,
3679 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003680 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003681
Andy Adamson938e1012009-04-01 09:22:28 -04003682 data = kzalloc(sizeof(*data), GFP_KERNEL);
Trond Myklebustfe650402006-01-03 09:55:18 +01003683 if (data == NULL)
3684 return -ENOMEM;
3685 data->args.fhandle = &data->fh;
3686 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003687 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003688 nfs_copy_fh(&data->fh, NFS_FH(inode));
3689 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003690 data->res.fattr = &data->fattr;
3691 data->res.server = server;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003692 data->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003693 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003694 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003695 data->rpc_status = 0;
3696
Trond Myklebustc970aa82007-07-14 15:39:59 -04003697 task_setup_data.callback_data = data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003698 msg.rpc_argp = &data->args,
3699 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003700 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003701 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003702 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003703 if (!issync)
3704 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003705 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003706 if (status != 0)
3707 goto out;
3708 status = data->rpc_status;
3709 if (status != 0)
3710 goto out;
3711 nfs_refresh_inode(inode, &data->fattr);
3712out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003713 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003714 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715}
3716
Trond Myklebuste6f81072008-01-24 18:14:34 -05003717int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718{
3719 struct nfs_server *server = NFS_SERVER(inode);
3720 struct nfs4_exception exception = { };
3721 int err;
3722 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003723 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724 switch (err) {
3725 case -NFS4ERR_STALE_STATEID:
3726 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 case 0:
3728 return 0;
3729 }
3730 err = nfs4_handle_exception(server, err, &exception);
3731 } while (exception.retry);
3732 return err;
3733}
3734
3735#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3736#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3737
3738/*
3739 * sleep, with exponential backoff, and retry the LOCK operation.
3740 */
3741static unsigned long
3742nfs4_set_lock_task_retry(unsigned long timeout)
3743{
Matthew Wilcox150030b2007-12-06 16:24:39 -05003744 schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745 timeout <<= 1;
3746 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3747 return NFS4_LOCK_MAXTIMEOUT;
3748 return timeout;
3749}
3750
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3752{
3753 struct inode *inode = state->inode;
3754 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003755 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003756 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003758 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003760 struct nfs_lockt_res res = {
3761 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762 };
3763 struct rpc_message msg = {
3764 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3765 .rpc_argp = &arg,
3766 .rpc_resp = &res,
3767 .rpc_cred = state->owner->so_cred,
3768 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769 struct nfs4_lock_state *lsp;
3770 int status;
3771
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003772 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003773 status = nfs4_set_lock_state(state, request);
3774 if (status != 0)
3775 goto out;
3776 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003777 arg.lock_owner.id = lsp->ls_id.id;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003778 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003779 switch (status) {
3780 case 0:
3781 request->fl_type = F_UNLCK;
3782 break;
3783 case -NFS4ERR_DENIED:
3784 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05003786 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003787out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788 return status;
3789}
3790
3791static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3792{
3793 struct nfs4_exception exception = { };
3794 int err;
3795
3796 do {
3797 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3798 _nfs4_proc_getlk(state, cmd, request),
3799 &exception);
3800 } while (exception.retry);
3801 return err;
3802}
3803
3804static int do_vfs_lock(struct file *file, struct file_lock *fl)
3805{
3806 int res = 0;
3807 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3808 case FL_POSIX:
3809 res = posix_lock_file_wait(file, fl);
3810 break;
3811 case FL_FLOCK:
3812 res = flock_lock_file_wait(file, fl);
3813 break;
3814 default:
3815 BUG();
3816 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817 return res;
3818}
3819
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003820struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003821 struct nfs_locku_args arg;
3822 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003823 struct nfs4_lock_state *lsp;
3824 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003825 struct file_lock fl;
3826 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003827 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003828};
3829
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003830static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3831 struct nfs_open_context *ctx,
3832 struct nfs4_lock_state *lsp,
3833 struct nfs_seqid *seqid)
3834{
3835 struct nfs4_unlockdata *p;
3836 struct inode *inode = lsp->ls_state->inode;
3837
Andy Adamsona8936932009-04-01 09:22:23 -04003838 p = kzalloc(sizeof(*p), GFP_KERNEL);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003839 if (p == NULL)
3840 return NULL;
3841 p->arg.fh = NFS_FH(inode);
3842 p->arg.fl = &p->fl;
3843 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003844 p->res.seqid = seqid;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003845 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003846 p->arg.stateid = &lsp->ls_stateid;
3847 p->lsp = lsp;
3848 atomic_inc(&lsp->ls_count);
3849 /* Ensure we don't close file until we're done freeing locks! */
3850 p->ctx = get_nfs_open_context(ctx);
3851 memcpy(&p->fl, fl, sizeof(p->fl));
3852 p->server = NFS_SERVER(inode);
3853 return p;
3854}
3855
Trond Myklebust06f814a2006-01-03 09:55:07 +01003856static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003857{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003858 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003859 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003860 nfs4_put_lock_state(calldata->lsp);
3861 put_nfs_open_context(calldata->ctx);
3862 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003863}
3864
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003865static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003866{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003867 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003868
Andy Adamsona8936932009-04-01 09:22:23 -04003869 nfs4_sequence_done(calldata->server, &calldata->res.seq_res,
3870 task->tk_status);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003871 if (RPC_ASSASSINATED(task))
3872 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003873 switch (task->tk_status) {
3874 case 0:
3875 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003876 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003877 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01003878 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003879 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003880 case -NFS4ERR_BAD_STATEID:
3881 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003882 case -NFS4ERR_STALE_STATEID:
3883 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003884 break;
3885 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003886 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebust0110ee12009-12-07 09:00:24 -05003887 nfs_restart_rpc(task,
Trond Myklebustd61e6122009-12-05 19:32:19 -05003888 calldata->server->nfs_client);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003889 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003890}
3891
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003892static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003893{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003894 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003896 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003897 return;
3898 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003899 /* Note: exit _without_ running nfs4_locku_done */
3900 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003901 return;
3902 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01003903 calldata->timestamp = jiffies;
Andy Adamsona8936932009-04-01 09:22:23 -04003904 if (nfs4_setup_sequence(calldata->server->nfs_client,
3905 &calldata->arg.seq_args,
3906 &calldata->res.seq_res, 1, task))
3907 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003908 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909}
3910
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003911static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003912 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003913 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01003914 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003915};
3916
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003917static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3918 struct nfs_open_context *ctx,
3919 struct nfs4_lock_state *lsp,
3920 struct nfs_seqid *seqid)
3921{
3922 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003923 struct rpc_message msg = {
3924 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3925 .rpc_cred = ctx->cred,
3926 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003927 struct rpc_task_setup task_setup_data = {
3928 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04003929 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003930 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003931 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003932 .flags = RPC_TASK_ASYNC,
3933 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003934
Frank Filz137d6ac2007-07-09 15:32:29 -07003935 /* Ensure this is an unlock - when canceling a lock, the
3936 * canceled lock is passed in, and it won't be an unlock.
3937 */
3938 fl->fl_type = F_UNLCK;
3939
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003940 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3941 if (data == NULL) {
3942 nfs_free_seqid(seqid);
3943 return ERR_PTR(-ENOMEM);
3944 }
3945
Trond Myklebust5138fde2007-07-14 15:40:01 -04003946 msg.rpc_argp = &data->arg,
3947 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003948 task_setup_data.callback_data = data;
3949 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003950}
3951
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3953{
Trond Myklebust19e03c52008-12-23 15:21:44 -05003954 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003955 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003956 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01003957 struct rpc_task *task;
3958 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003959 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960
Trond Myklebust9b073572006-06-29 16:38:34 -04003961 status = nfs4_set_lock_state(state, request);
3962 /* Unlock _before_ we do the RPC call */
3963 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003964 down_read(&nfsi->rwsem);
3965 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
3966 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003967 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003968 }
3969 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003970 if (status != 0)
3971 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003972 /* Is this a delegated lock? */
3973 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04003974 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003975 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003976 seqid = nfs_alloc_seqid(&lsp->ls_seqid);
Trond Myklebust9b073572006-06-29 16:38:34 -04003977 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003978 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04003979 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04003980 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003981 status = PTR_ERR(task);
3982 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04003983 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003984 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003985 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04003986out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003987 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003988 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989}
3990
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003991struct nfs4_lockdata {
3992 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003993 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003994 struct nfs4_lock_state *lsp;
3995 struct nfs_open_context *ctx;
3996 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003997 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003998 int rpc_status;
3999 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004000 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004001};
4002
4003static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
4004 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp)
4005{
4006 struct nfs4_lockdata *p;
4007 struct inode *inode = lsp->ls_state->inode;
4008 struct nfs_server *server = NFS_SERVER(inode);
4009
4010 p = kzalloc(sizeof(*p), GFP_KERNEL);
4011 if (p == NULL)
4012 return NULL;
4013
4014 p->arg.fh = NFS_FH(inode);
4015 p->arg.fl = &p->fl;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004016 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid);
4017 if (p->arg.open_seqid == NULL)
4018 goto out_free;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004019 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
4020 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004021 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004022 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004023 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04004024 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004025 p->res.lock_seqid = p->arg.lock_seqid;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04004026 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004027 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004028 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004029 atomic_inc(&lsp->ls_count);
4030 p->ctx = get_nfs_open_context(ctx);
4031 memcpy(&p->fl, fl, sizeof(p->fl));
4032 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004033out_free_seqid:
4034 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004035out_free:
4036 kfree(p);
4037 return NULL;
4038}
4039
4040static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4041{
4042 struct nfs4_lockdata *data = calldata;
4043 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044
Harvey Harrison3110ff82008-05-02 13:42:44 -07004045 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004046 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4047 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004048 /* Do we need to do an open_to_lock_owner? */
4049 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004050 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4051 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004052 data->arg.open_stateid = &state->stateid;
4053 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004054 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004055 } else
4056 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004057 data->timestamp = jiffies;
Andy Adamson66179ef2009-04-01 09:22:22 -04004058 if (nfs4_setup_sequence(data->server->nfs_client, &data->arg.seq_args,
4059 &data->res.seq_res, 1, task))
4060 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004061 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004062 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004063}
4064
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004065static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4066{
4067 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4068 nfs4_lock_prepare(task, calldata);
4069}
4070
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004071static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4072{
4073 struct nfs4_lockdata *data = calldata;
4074
Harvey Harrison3110ff82008-05-02 13:42:44 -07004075 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004076
Trond Myklebustd61e6122009-12-05 19:32:19 -05004077 nfs4_sequence_done(data->server, &data->res.seq_res,
4078 task->tk_status);
Andy Adamson66179ef2009-04-01 09:22:22 -04004079
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004080 data->rpc_status = task->tk_status;
4081 if (RPC_ASSASSINATED(task))
4082 goto out;
4083 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004084 if (data->rpc_status == 0)
4085 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4086 else
4087 goto out;
4088 }
4089 if (data->rpc_status == 0) {
4090 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4091 sizeof(data->lsp->ls_stateid.data));
4092 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Trond Myklebust88be9f92007-06-05 10:42:27 -04004093 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004094 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004095out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004096 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004097}
4098
4099static void nfs4_lock_release(void *calldata)
4100{
4101 struct nfs4_lockdata *data = calldata;
4102
Harvey Harrison3110ff82008-05-02 13:42:44 -07004103 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004104 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004105 if (data->cancelled != 0) {
4106 struct rpc_task *task;
4107 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4108 data->arg.lock_seqid);
4109 if (!IS_ERR(task))
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004110 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004111 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004112 } else
4113 nfs_free_seqid(data->arg.lock_seqid);
4114 nfs4_put_lock_state(data->lsp);
4115 put_nfs_open_context(data->ctx);
4116 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004117 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004118}
4119
4120static const struct rpc_call_ops nfs4_lock_ops = {
4121 .rpc_call_prepare = nfs4_lock_prepare,
4122 .rpc_call_done = nfs4_lock_done,
4123 .rpc_release = nfs4_lock_release,
4124};
4125
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004126static const struct rpc_call_ops nfs4_recover_lock_ops = {
4127 .rpc_call_prepare = nfs4_recover_lock_prepare,
4128 .rpc_call_done = nfs4_lock_done,
4129 .rpc_release = nfs4_lock_release,
4130};
4131
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004132static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4133{
4134 struct nfs_client *clp = server->nfs_client;
4135 struct nfs4_state *state = lsp->ls_state;
4136
4137 switch (error) {
4138 case -NFS4ERR_ADMIN_REVOKED:
4139 case -NFS4ERR_BAD_STATEID:
4140 case -NFS4ERR_EXPIRED:
4141 if (new_lock_owner != 0 ||
4142 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4143 nfs4_state_mark_reclaim_nograce(clp, state);
4144 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004145 break;
4146 case -NFS4ERR_STALE_STATEID:
4147 if (new_lock_owner != 0 ||
4148 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4149 nfs4_state_mark_reclaim_reboot(clp, state);
4150 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004151 };
4152}
4153
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004154static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004155{
4156 struct nfs4_lockdata *data;
4157 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004158 struct rpc_message msg = {
4159 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4160 .rpc_cred = state->owner->so_cred,
4161 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004162 struct rpc_task_setup task_setup_data = {
4163 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004164 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004165 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004166 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004167 .flags = RPC_TASK_ASYNC,
4168 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004169 int ret;
4170
Harvey Harrison3110ff82008-05-02 13:42:44 -07004171 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004172 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004173 fl->fl_u.nfs4_fl.owner);
4174 if (data == NULL)
4175 return -ENOMEM;
4176 if (IS_SETLKW(cmd))
4177 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004178 if (recovery_type > NFS_LOCK_NEW) {
4179 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004180 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004181 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4182 }
Trond Myklebust5138fde2007-07-14 15:40:01 -04004183 msg.rpc_argp = &data->arg,
4184 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004185 task_setup_data.callback_data = data;
4186 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004187 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004188 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004189 ret = nfs4_wait_for_completion_rpc_task(task);
4190 if (ret == 0) {
4191 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004192 if (ret)
4193 nfs4_handle_setlk_error(data->server, data->lsp,
4194 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004195 } else
4196 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004197 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004198 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004199 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200}
4201
4202static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4203{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004204 struct nfs_server *server = NFS_SERVER(state->inode);
4205 struct nfs4_exception exception = { };
4206 int err;
4207
4208 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004209 /* Cache the lock if possible... */
4210 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4211 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004212 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Jeff Layton2c643482010-01-07 09:42:03 -05004213 if (err != -NFS4ERR_DELAY && err != -EKEYEXPIRED)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004214 break;
4215 nfs4_handle_exception(server, err, &exception);
4216 } while (exception.retry);
4217 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218}
4219
4220static int nfs4_lock_expired(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
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004226 err = nfs4_set_lock_state(state, request);
4227 if (err != 0)
4228 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004229 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004230 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4231 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004232 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004233 switch (err) {
4234 default:
4235 goto out;
4236 case -NFS4ERR_GRACE:
4237 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -05004238 case -EKEYEXPIRED:
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004239 nfs4_handle_exception(server, err, &exception);
4240 err = 0;
4241 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004242 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004243out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004244 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245}
4246
4247static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4248{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004249 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004250 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004251 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004253 if ((fl_flags & FL_POSIX) &&
4254 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4255 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004256 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004257 status = nfs4_set_lock_state(state, request);
4258 if (status != 0)
4259 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004260 request->fl_flags |= FL_ACCESS;
4261 status = do_vfs_lock(request->fl_file, request);
4262 if (status < 0)
4263 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004264 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004265 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004266 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004267 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004268 request->fl_flags = fl_flags & ~FL_SLEEP;
4269 status = do_vfs_lock(request->fl_file, request);
4270 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004271 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004272 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004273 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004274 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004275 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004276 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004277 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07004278 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004279out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004280 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004281out:
4282 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 return status;
4284}
4285
4286static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4287{
4288 struct nfs4_exception exception = { };
4289 int err;
4290
4291 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004292 err = _nfs4_proc_setlk(state, cmd, request);
4293 if (err == -NFS4ERR_DENIED)
4294 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004296 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 } while (exception.retry);
4298 return err;
4299}
4300
4301static int
4302nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4303{
4304 struct nfs_open_context *ctx;
4305 struct nfs4_state *state;
4306 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4307 int status;
4308
4309 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004310 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 state = ctx->state;
4312
4313 if (request->fl_start < 0 || request->fl_end < 0)
4314 return -EINVAL;
4315
Trond Myklebustd9531262009-07-21 19:22:38 -04004316 if (IS_GETLK(cmd)) {
4317 if (state != NULL)
4318 return nfs4_proc_getlk(state, F_GETLK, request);
4319 return 0;
4320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321
4322 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4323 return -EINVAL;
4324
Trond Myklebustd9531262009-07-21 19:22:38 -04004325 if (request->fl_type == F_UNLCK) {
4326 if (state != NULL)
4327 return nfs4_proc_unlck(state, cmd, request);
4328 return 0;
4329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330
Trond Myklebustd9531262009-07-21 19:22:38 -04004331 if (state == NULL)
4332 return -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333 do {
4334 status = nfs4_proc_setlk(state, cmd, request);
4335 if ((status != -EAGAIN) || IS_SETLK(cmd))
4336 break;
4337 timeout = nfs4_set_lock_task_retry(timeout);
4338 status = -ERESTARTSYS;
4339 if (signalled())
4340 break;
4341 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 return status;
4343}
4344
Trond Myklebust888e6942005-11-04 15:38:11 -05004345int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4346{
4347 struct nfs_server *server = NFS_SERVER(state->inode);
4348 struct nfs4_exception exception = { };
4349 int err;
4350
4351 err = nfs4_set_lock_state(state, fl);
4352 if (err != 0)
4353 goto out;
4354 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004355 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004356 switch (err) {
4357 default:
4358 printk(KERN_ERR "%s: unhandled error %d.\n",
4359 __func__, err);
4360 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004361 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004362 goto out;
4363 case -NFS4ERR_EXPIRED:
4364 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004365 case -NFS4ERR_STALE_STATEID:
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004366 case -NFS4ERR_BADSESSION:
4367 case -NFS4ERR_BADSLOT:
4368 case -NFS4ERR_BAD_HIGH_SLOT:
4369 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4370 case -NFS4ERR_DEADSESSION:
Trond Myklebustd5122202009-06-17 13:22:58 -07004371 nfs4_schedule_state_recovery(server->nfs_client);
4372 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004373 case -ERESTARTSYS:
4374 /*
4375 * The show must go on: exit, but mark the
4376 * stateid as needing recovery.
4377 */
4378 case -NFS4ERR_ADMIN_REVOKED:
4379 case -NFS4ERR_BAD_STATEID:
4380 case -NFS4ERR_OPENMODE:
4381 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
4382 err = 0;
4383 goto out;
4384 case -ENOMEM:
4385 case -NFS4ERR_DENIED:
4386 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4387 err = 0;
4388 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004389 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -05004390 case -EKEYEXPIRED:
Trond Myklebustd5122202009-06-17 13:22:58 -07004391 break;
4392 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004393 err = nfs4_handle_exception(server, err, &exception);
4394 } while (exception.retry);
4395out:
4396 return err;
4397}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004398
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004399#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4400
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004401int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
4402 size_t buflen, int flags)
4403{
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004404 struct inode *inode = dentry->d_inode;
4405
4406 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4407 return -EOPNOTSUPP;
4408
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004409 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004410}
4411
4412/* The getxattr man page suggests returning -ENODATA for unknown attributes,
4413 * and that's what we'll do for e.g. user attributes that haven't been set.
4414 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
4415 * attributes in kernel-managed attribute namespaces. */
4416ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
4417 size_t buflen)
4418{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004419 struct inode *inode = dentry->d_inode;
4420
4421 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4422 return -EOPNOTSUPP;
4423
4424 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004425}
4426
4427ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
4428{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004429 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004430
J. Bruce Fields096455a2006-03-20 23:23:42 -05004431 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4432 return 0;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004433 if (buf && buflen < len)
4434 return -ERANGE;
4435 if (buf)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004436 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
4437 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004438}
4439
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004440static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4441{
4442 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
4443 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4444 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4445 return;
4446
4447 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4448 NFS_ATTR_FATTR_NLINK;
4449 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4450 fattr->nlink = 2;
4451}
4452
Trond Myklebust56659e92007-07-17 21:52:39 -04004453int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004454 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004455{
4456 struct nfs_server *server = NFS_SERVER(dir);
4457 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004458 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4459 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004460 };
4461 struct nfs4_fs_locations_arg args = {
4462 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004463 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004464 .page = page,
4465 .bitmask = bitmask,
4466 };
Benny Halevy22958462009-04-01 09:22:02 -04004467 struct nfs4_fs_locations_res res = {
4468 .fs_locations = fs_locations,
4469 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004470 struct rpc_message msg = {
4471 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4472 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004473 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004474 };
4475 int status;
4476
Harvey Harrison3110ff82008-05-02 13:42:44 -07004477 dprintk("%s: start\n", __func__);
Trond Myklebustc228fd32007-01-13 02:28:11 -05004478 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004479 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004480 fs_locations->nlocations = 0;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04004481 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004482 nfs_fixup_referral_attributes(&fs_locations->fattr);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004483 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004484 return status;
4485}
4486
Andy Adamson557134a2009-04-01 09:21:53 -04004487#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04004488/*
4489 * nfs4_proc_exchange_id()
4490 *
4491 * Since the clientid has expired, all compounds using sessions
4492 * associated with the stale clientid will be returning
4493 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4494 * be in some phase of session reset.
4495 */
Andy Adamson4d643d12009-12-04 15:52:24 -05004496int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004497{
4498 nfs4_verifier verifier;
4499 struct nfs41_exchange_id_args args = {
4500 .client = clp,
4501 .flags = clp->cl_exchange_flags,
4502 };
4503 struct nfs41_exchange_id_res res = {
4504 .client = clp,
4505 };
4506 int status;
4507 struct rpc_message msg = {
4508 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4509 .rpc_argp = &args,
4510 .rpc_resp = &res,
4511 .rpc_cred = cred,
4512 };
4513 __be32 *p;
4514
4515 dprintk("--> %s\n", __func__);
4516 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04004517
Alexandros Batsakis7b183d02009-12-05 13:33:25 -05004518 /* Remove server-only flags */
4519 args.flags &= ~EXCHGID4_FLAG_CONFIRMED_R;
4520
Benny Halevy99fe60d2009-04-01 09:22:29 -04004521 p = (u32 *)verifier.data;
4522 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4523 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4524 args.verifier = &verifier;
4525
4526 while (1) {
4527 args.id_len = scnprintf(args.id, sizeof(args.id),
4528 "%s/%s %u",
4529 clp->cl_ipaddr,
4530 rpc_peeraddr2str(clp->cl_rpcclient,
4531 RPC_DISPLAY_ADDR),
4532 clp->cl_id_uniquifier);
4533
4534 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
4535
Andy Adamson180b62a2010-03-02 13:19:36 -05004536 if (status != -NFS4ERR_CLID_INUSE)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004537 break;
4538
4539 if (signalled())
4540 break;
4541
4542 if (++clp->cl_id_uniquifier == 0)
4543 break;
4544 }
4545
4546 dprintk("<-- %s status= %d\n", __func__, status);
4547 return status;
4548}
4549
Andy Adamson2050f0c2009-04-01 09:22:30 -04004550struct nfs4_get_lease_time_data {
4551 struct nfs4_get_lease_time_args *args;
4552 struct nfs4_get_lease_time_res *res;
4553 struct nfs_client *clp;
4554};
4555
4556static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4557 void *calldata)
4558{
4559 int ret;
4560 struct nfs4_get_lease_time_data *data =
4561 (struct nfs4_get_lease_time_data *)calldata;
4562
4563 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004564 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004565 /* just setup sequence, do not trigger session recovery
4566 since we're invoked within one */
4567 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004568 &data->args->la_seq_args,
4569 &data->res->lr_seq_res, 0, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004570
4571 BUG_ON(ret == -EAGAIN);
4572 rpc_call_start(task);
4573 dprintk("<-- %s\n", __func__);
4574}
4575
4576/*
4577 * Called from nfs4_state_manager thread for session setup, so don't recover
4578 * from sequence operation or clientid errors.
4579 */
4580static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4581{
4582 struct nfs4_get_lease_time_data *data =
4583 (struct nfs4_get_lease_time_data *)calldata;
4584
4585 dprintk("--> %s\n", __func__);
4586 nfs41_sequence_done(data->clp, &data->res->lr_seq_res, task->tk_status);
4587 switch (task->tk_status) {
4588 case -NFS4ERR_DELAY:
4589 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05004590 case -EKEYEXPIRED:
Andy Adamson2050f0c2009-04-01 09:22:30 -04004591 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4592 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4593 task->tk_status = 0;
Trond Myklebust0110ee12009-12-07 09:00:24 -05004594 nfs_restart_rpc(task, data->clp);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004595 return;
4596 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04004597 dprintk("<-- %s\n", __func__);
4598}
4599
4600struct rpc_call_ops nfs4_get_lease_time_ops = {
4601 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4602 .rpc_call_done = nfs4_get_lease_time_done,
4603};
4604
4605int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4606{
4607 struct rpc_task *task;
4608 struct nfs4_get_lease_time_args args;
4609 struct nfs4_get_lease_time_res res = {
4610 .lr_fsinfo = fsinfo,
4611 };
4612 struct nfs4_get_lease_time_data data = {
4613 .args = &args,
4614 .res = &res,
4615 .clp = clp,
4616 };
4617 struct rpc_message msg = {
4618 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4619 .rpc_argp = &args,
4620 .rpc_resp = &res,
4621 };
4622 struct rpc_task_setup task_setup = {
4623 .rpc_client = clp->cl_rpcclient,
4624 .rpc_message = &msg,
4625 .callback_ops = &nfs4_get_lease_time_ops,
4626 .callback_data = &data
4627 };
4628 int status;
4629
4630 res.lr_seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
4631 dprintk("--> %s\n", __func__);
4632 task = rpc_run_task(&task_setup);
4633
4634 if (IS_ERR(task))
4635 status = PTR_ERR(task);
4636 else {
4637 status = task->tk_status;
4638 rpc_put_task(task);
4639 }
4640 dprintk("<-- %s return %d\n", __func__, status);
4641
4642 return status;
4643}
4644
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004645/*
4646 * Reset a slot table
4647 */
Andy Adamson104aeba2010-01-14 17:45:10 -05004648static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
4649 int ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004650{
Andy Adamson104aeba2010-01-14 17:45:10 -05004651 struct nfs4_slot *new = NULL;
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004652 int i;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004653 int ret = 0;
4654
Andy Adamson104aeba2010-01-14 17:45:10 -05004655 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
4656 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004657
Andy Adamson104aeba2010-01-14 17:45:10 -05004658 /* Does the newly negotiated max_reqs match the existing slot table? */
4659 if (max_reqs != tbl->max_slots) {
4660 ret = -ENOMEM;
4661 new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
4662 GFP_KERNEL);
4663 if (!new)
4664 goto out;
4665 ret = 0;
4666 kfree(tbl->slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004667 }
4668 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson104aeba2010-01-14 17:45:10 -05004669 if (new) {
4670 tbl->slots = new;
4671 tbl->max_slots = max_reqs;
4672 }
4673 for (i = 0; i < tbl->max_slots; ++i)
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004674 tbl->slots[i].seq_nr = ivalue;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004675 spin_unlock(&tbl->slot_tbl_lock);
4676 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4677 tbl, tbl->slots, tbl->max_slots);
4678out:
4679 dprintk("<-- %s: return %d\n", __func__, ret);
4680 return ret;
4681}
4682
Andy Adamsonfc931582009-04-01 09:22:31 -04004683/*
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004684 * Reset the forechannel and backchannel slot tables
4685 */
4686static int nfs4_reset_slot_tables(struct nfs4_session *session)
4687{
4688 int status;
4689
4690 status = nfs4_reset_slot_table(&session->fc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05004691 session->fc_attrs.max_reqs, 1);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004692 if (status)
4693 return status;
4694
4695 status = nfs4_reset_slot_table(&session->bc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05004696 session->bc_attrs.max_reqs, 0);
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004697 return status;
4698}
4699
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004700/* Destroy the slot table */
4701static void nfs4_destroy_slot_tables(struct nfs4_session *session)
4702{
4703 if (session->fc_slot_table.slots != NULL) {
4704 kfree(session->fc_slot_table.slots);
4705 session->fc_slot_table.slots = NULL;
4706 }
4707 if (session->bc_slot_table.slots != NULL) {
4708 kfree(session->bc_slot_table.slots);
4709 session->bc_slot_table.slots = NULL;
4710 }
4711 return;
4712}
4713
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004714/*
Andy Adamsonfc931582009-04-01 09:22:31 -04004715 * Initialize slot table
4716 */
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004717static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
4718 int max_slots, int ivalue)
Andy Adamsonfc931582009-04-01 09:22:31 -04004719{
Andy Adamsonfc931582009-04-01 09:22:31 -04004720 struct nfs4_slot *slot;
4721 int ret = -ENOMEM;
4722
4723 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
4724
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004725 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
Andy Adamsonfc931582009-04-01 09:22:31 -04004726
4727 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_KERNEL);
4728 if (!slot)
4729 goto out;
Andy Adamsonfc931582009-04-01 09:22:31 -04004730 ret = 0;
4731
4732 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonfc931582009-04-01 09:22:31 -04004733 tbl->max_slots = max_slots;
4734 tbl->slots = slot;
4735 tbl->highest_used_slotid = -1; /* no slot is currently used */
4736 spin_unlock(&tbl->slot_tbl_lock);
4737 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4738 tbl, tbl->slots, tbl->max_slots);
4739out:
4740 dprintk("<-- %s: return %d\n", __func__, ret);
4741 return ret;
Andy Adamsonfc931582009-04-01 09:22:31 -04004742}
4743
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004744/*
4745 * Initialize the forechannel and backchannel tables
4746 */
4747static int nfs4_init_slot_tables(struct nfs4_session *session)
4748{
Trond Myklebustf26468f2009-12-05 19:32:11 -05004749 struct nfs4_slot_table *tbl;
4750 int status = 0;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004751
Trond Myklebustf26468f2009-12-05 19:32:11 -05004752 tbl = &session->fc_slot_table;
4753 if (tbl->slots == NULL) {
4754 status = nfs4_init_slot_table(tbl,
4755 session->fc_attrs.max_reqs, 1);
4756 if (status)
4757 return status;
4758 }
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004759
Trond Myklebustf26468f2009-12-05 19:32:11 -05004760 tbl = &session->bc_slot_table;
4761 if (tbl->slots == NULL) {
4762 status = nfs4_init_slot_table(tbl,
4763 session->bc_attrs.max_reqs, 0);
4764 if (status)
4765 nfs4_destroy_slot_tables(session);
4766 }
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004767
4768 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04004769}
4770
4771struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
4772{
4773 struct nfs4_session *session;
4774 struct nfs4_slot_table *tbl;
4775
4776 session = kzalloc(sizeof(struct nfs4_session), GFP_KERNEL);
4777 if (!session)
4778 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04004779
Andy Adamson76db6d92009-04-01 09:22:38 -04004780 /*
4781 * The create session reply races with the server back
4782 * channel probe. Mark the client NFS_CS_SESSION_INITING
4783 * so that the client back channel can find the
4784 * nfs_client struct
4785 */
4786 clp->cl_cons_state = NFS_CS_SESSION_INITING;
Andy Adamsonea028ac2009-12-04 15:55:38 -05004787 init_completion(&session->complete);
Andy Adamson76db6d92009-04-01 09:22:38 -04004788
Andy Adamson557134a2009-04-01 09:21:53 -04004789 tbl = &session->fc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05004790 tbl->highest_used_slotid = -1;
Andy Adamson557134a2009-04-01 09:21:53 -04004791 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004792 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004793
4794 tbl = &session->bc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05004795 tbl->highest_used_slotid = -1;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004796 spin_lock_init(&tbl->slot_tbl_lock);
4797 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
4798
Andy Adamson557134a2009-04-01 09:21:53 -04004799 session->clp = clp;
4800 return session;
4801}
4802
4803void nfs4_destroy_session(struct nfs4_session *session)
4804{
Andy Adamson5a0ffe52009-04-01 09:23:18 -04004805 nfs4_proc_destroy_session(session);
4806 dprintk("%s Destroy backchannel for xprt %p\n",
4807 __func__, session->clp->cl_rpcclient->cl_xprt);
4808 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
4809 NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004810 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04004811 kfree(session);
4812}
4813
Andy Adamsonfc931582009-04-01 09:22:31 -04004814/*
4815 * Initialize the values to be used by the client in CREATE_SESSION
4816 * If nfs4_init_session set the fore channel request and response sizes,
4817 * use them.
4818 *
4819 * Set the back channel max_resp_sz_cached to zero to force the client to
4820 * always set csa_cachethis to FALSE because the current implementation
4821 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
4822 */
4823static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
4824{
4825 struct nfs4_session *session = args->client->cl_session;
4826 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
4827 mxresp_sz = session->fc_attrs.max_resp_sz;
4828
4829 if (mxrqst_sz == 0)
4830 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
4831 if (mxresp_sz == 0)
4832 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
4833 /* Fore channel attributes */
4834 args->fc_attrs.headerpadsz = 0;
4835 args->fc_attrs.max_rqst_sz = mxrqst_sz;
4836 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04004837 args->fc_attrs.max_ops = NFS4_MAX_OPS;
4838 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
4839
4840 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05004841 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04004842 __func__,
4843 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05004844 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04004845
4846 /* Back channel attributes */
4847 args->bc_attrs.headerpadsz = 0;
4848 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
4849 args->bc_attrs.max_resp_sz = PAGE_SIZE;
4850 args->bc_attrs.max_resp_sz_cached = 0;
4851 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
4852 args->bc_attrs.max_reqs = 1;
4853
4854 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
4855 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4856 __func__,
4857 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
4858 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
4859 args->bc_attrs.max_reqs);
4860}
4861
Andy Adamson8d353012009-04-01 09:22:32 -04004862static int _verify_channel_attr(char *chan, char *attr_name, u32 sent, u32 rcvd)
4863{
4864 if (rcvd <= sent)
4865 return 0;
4866 printk(KERN_WARNING "%s: Session INVALID: %s channel %s increased. "
4867 "sent=%u rcvd=%u\n", __func__, chan, attr_name, sent, rcvd);
4868 return -EINVAL;
4869}
4870
4871#define _verify_fore_channel_attr(_name_) \
4872 _verify_channel_attr("fore", #_name_, \
4873 args->fc_attrs._name_, \
4874 session->fc_attrs._name_)
4875
4876#define _verify_back_channel_attr(_name_) \
4877 _verify_channel_attr("back", #_name_, \
4878 args->bc_attrs._name_, \
4879 session->bc_attrs._name_)
4880
4881/*
4882 * The server is not allowed to increase the fore channel header pad size,
4883 * maximum response size, or maximum number of operations.
4884 *
4885 * The back channel attributes are only negotiatied down: We send what the
4886 * (back channel) server insists upon.
4887 */
4888static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
4889 struct nfs4_session *session)
4890{
4891 int ret = 0;
4892
4893 ret |= _verify_fore_channel_attr(headerpadsz);
4894 ret |= _verify_fore_channel_attr(max_resp_sz);
4895 ret |= _verify_fore_channel_attr(max_ops);
4896
4897 ret |= _verify_back_channel_attr(headerpadsz);
4898 ret |= _verify_back_channel_attr(max_rqst_sz);
4899 ret |= _verify_back_channel_attr(max_resp_sz);
4900 ret |= _verify_back_channel_attr(max_resp_sz_cached);
4901 ret |= _verify_back_channel_attr(max_ops);
4902 ret |= _verify_back_channel_attr(max_reqs);
4903
4904 return ret;
4905}
4906
Andy Adamsonfc931582009-04-01 09:22:31 -04004907static int _nfs4_proc_create_session(struct nfs_client *clp)
4908{
4909 struct nfs4_session *session = clp->cl_session;
4910 struct nfs41_create_session_args args = {
4911 .client = clp,
4912 .cb_program = NFS4_CALLBACK,
4913 };
4914 struct nfs41_create_session_res res = {
4915 .client = clp,
4916 };
4917 struct rpc_message msg = {
4918 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
4919 .rpc_argp = &args,
4920 .rpc_resp = &res,
4921 };
4922 int status;
4923
4924 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04004925 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04004926
4927 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4928
Andy Adamson8d353012009-04-01 09:22:32 -04004929 if (!status)
4930 /* Verify the session's negotiated channel_attrs values */
4931 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04004932 if (!status) {
4933 /* Increment the clientid slot sequence id */
4934 clp->cl_seqid++;
4935 }
4936
4937 return status;
4938}
4939
4940/*
4941 * Issues a CREATE_SESSION operation to the server.
4942 * It is the responsibility of the caller to verify the session is
4943 * expired before calling this routine.
4944 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05004945int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04004946{
4947 int status;
4948 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04004949 struct nfs4_session *session = clp->cl_session;
4950
4951 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
4952
4953 status = _nfs4_proc_create_session(clp);
4954 if (status)
4955 goto out;
4956
Trond Myklebustf26468f2009-12-05 19:32:11 -05004957 /* Init and reset the fore channel */
4958 status = nfs4_init_slot_tables(session);
4959 dprintk("slot table initialization returned %d\n", status);
4960 if (status)
4961 goto out;
4962 status = nfs4_reset_slot_tables(session);
4963 dprintk("slot table reset returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04004964 if (status)
4965 goto out;
4966
4967 ptr = (unsigned *)&session->sess_id.data[0];
4968 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
4969 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04004970out:
4971 dprintk("<-- %s\n", __func__);
4972 return status;
4973}
4974
Andy Adamson0f3e66c2009-04-01 09:22:34 -04004975/*
4976 * Issue the over-the-wire RPC DESTROY_SESSION.
4977 * The caller must serialize access to this routine.
4978 */
4979int nfs4_proc_destroy_session(struct nfs4_session *session)
4980{
4981 int status = 0;
4982 struct rpc_message msg;
4983
4984 dprintk("--> nfs4_proc_destroy_session\n");
4985
4986 /* session is still being setup */
4987 if (session->clp->cl_cons_state != NFS_CS_READY)
4988 return status;
4989
4990 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
4991 msg.rpc_argp = session;
4992 msg.rpc_resp = NULL;
4993 msg.rpc_cred = NULL;
4994 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4995
4996 if (status)
4997 printk(KERN_WARNING
4998 "Got error %d from the server on DESTROY_SESSION. "
4999 "Session has been destroyed regardless...\n", status);
5000
5001 dprintk("<-- nfs4_proc_destroy_session\n");
5002 return status;
5003}
5004
Trond Myklebustfccba802009-07-21 16:48:07 -04005005int nfs4_init_session(struct nfs_server *server)
5006{
5007 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005008 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005009 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005010 int ret;
5011
5012 if (!nfs4_has_session(clp))
5013 return 0;
5014
Andy Adamson68bf05e2009-12-15 12:55:02 -05005015 rsize = server->rsize;
5016 if (rsize == 0)
5017 rsize = NFS_MAX_FILE_IO_SIZE;
5018 wsize = server->wsize;
5019 if (wsize == 0)
5020 wsize = NFS_MAX_FILE_IO_SIZE;
5021
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005022 session = clp->cl_session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005023 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5024 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005025
Trond Myklebustfccba802009-07-21 16:48:07 -04005026 ret = nfs4_recover_expired_lease(server);
5027 if (!ret)
5028 ret = nfs4_check_client_ready(clp);
5029 return ret;
5030}
5031
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005032/*
5033 * Renew the cl_session lease.
5034 */
5035static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5036{
5037 struct nfs4_sequence_args args;
5038 struct nfs4_sequence_res res;
5039
5040 struct rpc_message msg = {
5041 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5042 .rpc_argp = &args,
5043 .rpc_resp = &res,
5044 .rpc_cred = cred,
5045 };
5046
5047 args.sa_cache_this = 0;
5048
5049 return nfs4_call_sync_sequence(clp, clp->cl_rpcclient, &msg, &args,
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005050 &res, args.sa_cache_this, 1);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005051}
5052
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005053static void nfs41_sequence_release(void *data)
5054{
5055 struct nfs_client *clp = (struct nfs_client *)data;
5056
Alexandros Batsakis71358402010-02-05 03:45:05 -08005057 if (atomic_read(&clp->cl_count) > 1)
5058 nfs4_schedule_state_renewal(clp);
5059 nfs_put_client(clp);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005060}
5061
5062static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005063{
5064 struct nfs_client *clp = (struct nfs_client *)data;
5065
5066 nfs41_sequence_done(clp, task->tk_msg.rpc_resp, task->tk_status);
5067
5068 if (task->tk_status < 0) {
5069 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005070 if (atomic_read(&clp->cl_count) == 1)
5071 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005072
5073 if (_nfs4_async_handle_error(task, NULL, clp, NULL)
5074 == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05005075 nfs_restart_rpc(task, clp);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005076 return;
5077 }
5078 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005079 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005080out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005081 kfree(task->tk_msg.rpc_argp);
5082 kfree(task->tk_msg.rpc_resp);
5083
5084 dprintk("<-- %s\n", __func__);
5085}
5086
5087static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5088{
5089 struct nfs_client *clp;
5090 struct nfs4_sequence_args *args;
5091 struct nfs4_sequence_res *res;
5092
5093 clp = (struct nfs_client *)data;
5094 args = task->tk_msg.rpc_argp;
5095 res = task->tk_msg.rpc_resp;
5096
5097 if (nfs4_setup_sequence(clp, args, res, 0, task))
5098 return;
5099 rpc_call_start(task);
5100}
5101
5102static const struct rpc_call_ops nfs41_sequence_ops = {
5103 .rpc_call_done = nfs41_sequence_call_done,
5104 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005105 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005106};
5107
5108static int nfs41_proc_async_sequence(struct nfs_client *clp,
5109 struct rpc_cred *cred)
5110{
5111 struct nfs4_sequence_args *args;
5112 struct nfs4_sequence_res *res;
5113 struct rpc_message msg = {
5114 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5115 .rpc_cred = cred,
5116 };
5117
Alexandros Batsakis71358402010-02-05 03:45:05 -08005118 if (!atomic_inc_not_zero(&clp->cl_count))
5119 return -EIO;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005120 args = kzalloc(sizeof(*args), GFP_KERNEL);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005121 res = kzalloc(sizeof(*res), GFP_KERNEL);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005122 if (!args || !res) {
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005123 kfree(args);
Dan Carpenter7dd08a52010-03-06 15:02:22 +03005124 kfree(res);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005125 nfs_put_client(clp);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005126 return -ENOMEM;
5127 }
5128 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
5129 msg.rpc_argp = args;
5130 msg.rpc_resp = res;
5131
5132 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
5133 &nfs41_sequence_ops, (void *)clp);
5134}
5135
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005136struct nfs4_reclaim_complete_data {
5137 struct nfs_client *clp;
5138 struct nfs41_reclaim_complete_args arg;
5139 struct nfs41_reclaim_complete_res res;
5140};
5141
5142static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5143{
5144 struct nfs4_reclaim_complete_data *calldata = data;
5145
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005146 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005147 if (nfs4_setup_sequence(calldata->clp, &calldata->arg.seq_args,
5148 &calldata->res.seq_res, 0, task))
5149 return;
5150
5151 rpc_call_start(task);
5152}
5153
5154static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5155{
5156 struct nfs4_reclaim_complete_data *calldata = data;
5157 struct nfs_client *clp = calldata->clp;
5158 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5159
5160 dprintk("--> %s\n", __func__);
5161 nfs41_sequence_done(clp, res, task->tk_status);
5162 switch (task->tk_status) {
5163 case 0:
5164 case -NFS4ERR_COMPLETE_ALREADY:
5165 break;
5166 case -NFS4ERR_BADSESSION:
5167 case -NFS4ERR_DEADSESSION:
5168 /*
5169 * Handle the session error, but do not retry the operation, as
5170 * we have no way of telling whether the clientid had to be
5171 * reset before we got our reply. If reset, a new wave of
5172 * reclaim operations will follow, containing their own reclaim
5173 * complete. We don't want our retry to get on the way of
5174 * recovery by incorrectly indicating to the server that we're
5175 * done reclaiming state since the process had to be restarted.
5176 */
5177 _nfs4_async_handle_error(task, NULL, clp, NULL);
5178 break;
5179 default:
5180 if (_nfs4_async_handle_error(
5181 task, NULL, clp, NULL) == -EAGAIN) {
5182 rpc_restart_call_prepare(task);
5183 return;
5184 }
5185 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005186
5187 dprintk("<-- %s\n", __func__);
5188}
5189
5190static void nfs4_free_reclaim_complete_data(void *data)
5191{
5192 struct nfs4_reclaim_complete_data *calldata = data;
5193
5194 kfree(calldata);
5195}
5196
5197static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5198 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5199 .rpc_call_done = nfs4_reclaim_complete_done,
5200 .rpc_release = nfs4_free_reclaim_complete_data,
5201};
5202
5203/*
5204 * Issue a global reclaim complete.
5205 */
5206static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5207{
5208 struct nfs4_reclaim_complete_data *calldata;
5209 struct rpc_task *task;
5210 struct rpc_message msg = {
5211 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5212 };
5213 struct rpc_task_setup task_setup_data = {
5214 .rpc_client = clp->cl_rpcclient,
5215 .rpc_message = &msg,
5216 .callback_ops = &nfs4_reclaim_complete_call_ops,
5217 .flags = RPC_TASK_ASYNC,
5218 };
5219 int status = -ENOMEM;
5220
5221 dprintk("--> %s\n", __func__);
5222 calldata = kzalloc(sizeof(*calldata), GFP_KERNEL);
5223 if (calldata == NULL)
5224 goto out;
5225 calldata->clp = clp;
5226 calldata->arg.one_fs = 0;
5227 calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
5228
5229 msg.rpc_argp = &calldata->arg;
5230 msg.rpc_resp = &calldata->res;
5231 task_setup_data.callback_data = calldata;
5232 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005233 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005234 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005235 goto out;
5236 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005237 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005238 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005239out:
5240 dprintk("<-- %s status=%d\n", __func__, status);
5241 return status;
5242}
Andy Adamson557134a2009-04-01 09:21:53 -04005243#endif /* CONFIG_NFS_V4_1 */
5244
Andy Adamson591d71c2009-04-01 09:22:47 -04005245struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005246 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005247 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005248 .recover_open = nfs4_open_reclaim,
5249 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04005250 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005251 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252};
5253
Andy Adamson591d71c2009-04-01 09:22:47 -04005254#if defined(CONFIG_NFS_V4_1)
5255struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
5256 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5257 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5258 .recover_open = nfs4_open_reclaim,
5259 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05005260 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005261 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005262 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04005263};
5264#endif /* CONFIG_NFS_V4_1 */
5265
5266struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005267 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005268 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005269 .recover_open = nfs4_open_expired,
5270 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04005271 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005272 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273};
5274
Andy Adamson591d71c2009-04-01 09:22:47 -04005275#if defined(CONFIG_NFS_V4_1)
5276struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
5277 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5278 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5279 .recover_open = nfs4_open_expired,
5280 .recover_lock = nfs4_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05005281 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005282 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04005283};
5284#endif /* CONFIG_NFS_V4_1 */
5285
Benny Halevy29fba382009-04-01 09:22:44 -04005286struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
5287 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04005288 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005289 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04005290};
5291
5292#if defined(CONFIG_NFS_V4_1)
5293struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
5294 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04005295 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005296 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04005297};
5298#endif
5299
5300/*
5301 * Per minor version reboot and network partition recovery ops
5302 */
5303
Andy Adamson591d71c2009-04-01 09:22:47 -04005304struct nfs4_state_recovery_ops *nfs4_reboot_recovery_ops[] = {
5305 &nfs40_reboot_recovery_ops,
5306#if defined(CONFIG_NFS_V4_1)
5307 &nfs41_reboot_recovery_ops,
5308#endif
5309};
5310
5311struct nfs4_state_recovery_ops *nfs4_nograce_recovery_ops[] = {
5312 &nfs40_nograce_recovery_ops,
5313#if defined(CONFIG_NFS_V4_1)
5314 &nfs41_nograce_recovery_ops,
5315#endif
5316};
5317
Benny Halevy29fba382009-04-01 09:22:44 -04005318struct nfs4_state_maintenance_ops *nfs4_state_renewal_ops[] = {
5319 &nfs40_state_renewal_ops,
5320#if defined(CONFIG_NFS_V4_1)
5321 &nfs41_state_renewal_ops,
5322#endif
5323};
5324
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08005325static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005326 .permission = nfs_permission,
5327 .getattr = nfs_getattr,
5328 .setattr = nfs_setattr,
5329 .getxattr = nfs4_getxattr,
5330 .setxattr = nfs4_setxattr,
5331 .listxattr = nfs4_listxattr,
5332};
5333
David Howells509de812006-08-22 20:06:11 -04005334const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005335 .version = 4, /* protocol version */
5336 .dentry_ops = &nfs4_dentry_operations,
5337 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005338 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339 .getroot = nfs4_proc_get_root,
5340 .getattr = nfs4_proc_getattr,
5341 .setattr = nfs4_proc_setattr,
David Howells2b3de442006-08-22 20:06:09 -04005342 .lookupfh = nfs4_proc_lookupfh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005343 .lookup = nfs4_proc_lookup,
5344 .access = nfs4_proc_access,
5345 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346 .create = nfs4_proc_create,
5347 .remove = nfs4_proc_remove,
5348 .unlink_setup = nfs4_proc_unlink_setup,
5349 .unlink_done = nfs4_proc_unlink_done,
5350 .rename = nfs4_proc_rename,
5351 .link = nfs4_proc_link,
5352 .symlink = nfs4_proc_symlink,
5353 .mkdir = nfs4_proc_mkdir,
5354 .rmdir = nfs4_proc_remove,
5355 .readdir = nfs4_proc_readdir,
5356 .mknod = nfs4_proc_mknod,
5357 .statfs = nfs4_proc_statfs,
5358 .fsinfo = nfs4_proc_fsinfo,
5359 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04005360 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361 .decode_dirent = nfs4_decode_dirent,
5362 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05005363 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005365 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005367 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005369 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04005370 .close_context = nfs4_close_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371};
5372
5373/*
5374 * Local variables:
5375 * c-basic-offset: 8
5376 * End:
5377 */