blob: f9254fb0c9d0c11750ab8406edb4b1774604a63b [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>
42#include <linux/sunrpc/clnt.h>
43#include <linux/nfs.h>
44#include <linux/nfs4.h>
45#include <linux/nfs_fs.h>
46#include <linux/nfs_page.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070048#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040049#include <linux/module.h>
Andy Adamson5a0ffe52009-04-01 09:23:18 -040050#include <linux/sunrpc/bc_xprt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Trond Myklebust4ce79712005-06-22 17:16:21 +000052#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050054#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050055#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040056#include "callback.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58#define NFSDBG_FACILITY NFSDBG_PROC
59
Trond Myklebust2066fe82006-09-15 08:30:46 -040060#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#define NFS4_POLL_RETRY_MAX (15*HZ)
62
Trond Myklebusta78cb572009-08-09 15:06:19 -040063#define NFS4_MAX_LOOP_ON_RECOVER (10)
64
Trond Myklebustcdd4e682006-01-03 09:55:12 +010065struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010066static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080067static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050069static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Trond Myklebust99367812007-07-17 21:52:41 -040070static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
71static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Linus Torvalds1da177e2005-04-16 15:20:36 -070073/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050074static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
Trond Myklebust52567b02009-10-23 14:46:42 -040076 if (err >= -1000)
77 return err;
78 switch (err) {
79 case -NFS4ERR_RESOURCE:
80 return -EREMOTEIO;
81 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -070083 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -040084 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 }
Trond Myklebust52567b02009-10-23 14:46:42 -040086 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087}
88
89/*
90 * This is our standard bitmap for GETATTR requests.
91 */
92const u32 nfs4_fattr_bitmap[2] = {
93 FATTR4_WORD0_TYPE
94 | FATTR4_WORD0_CHANGE
95 | FATTR4_WORD0_SIZE
96 | FATTR4_WORD0_FSID
97 | FATTR4_WORD0_FILEID,
98 FATTR4_WORD1_MODE
99 | FATTR4_WORD1_NUMLINKS
100 | FATTR4_WORD1_OWNER
101 | FATTR4_WORD1_OWNER_GROUP
102 | FATTR4_WORD1_RAWDEV
103 | FATTR4_WORD1_SPACE_USED
104 | FATTR4_WORD1_TIME_ACCESS
105 | FATTR4_WORD1_TIME_METADATA
106 | FATTR4_WORD1_TIME_MODIFY
107};
108
109const u32 nfs4_statfs_bitmap[2] = {
110 FATTR4_WORD0_FILES_AVAIL
111 | FATTR4_WORD0_FILES_FREE
112 | FATTR4_WORD0_FILES_TOTAL,
113 FATTR4_WORD1_SPACE_AVAIL
114 | FATTR4_WORD1_SPACE_FREE
115 | FATTR4_WORD1_SPACE_TOTAL
116};
117
Trond Myklebust4ce79712005-06-22 17:16:21 +0000118const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 FATTR4_WORD0_MAXLINK
120 | FATTR4_WORD0_MAXNAME,
121 0
122};
123
124const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
125 | FATTR4_WORD0_MAXREAD
126 | FATTR4_WORD0_MAXWRITE
127 | FATTR4_WORD0_LEASE_TIME,
128 0
129};
130
Manoj Naik830b8e32006-06-09 09:34:25 -0400131const u32 nfs4_fs_locations_bitmap[2] = {
132 FATTR4_WORD0_TYPE
133 | FATTR4_WORD0_CHANGE
134 | FATTR4_WORD0_SIZE
135 | FATTR4_WORD0_FSID
136 | FATTR4_WORD0_FILEID
137 | FATTR4_WORD0_FS_LOCATIONS,
138 FATTR4_WORD1_MODE
139 | FATTR4_WORD1_NUMLINKS
140 | FATTR4_WORD1_OWNER
141 | FATTR4_WORD1_OWNER_GROUP
142 | FATTR4_WORD1_RAWDEV
143 | FATTR4_WORD1_SPACE_USED
144 | FATTR4_WORD1_TIME_ACCESS
145 | FATTR4_WORD1_TIME_METADATA
146 | FATTR4_WORD1_TIME_MODIFY
147 | FATTR4_WORD1_MOUNTED_ON_FILEID
148};
149
Al Virobc4785c2006-10-19 23:28:51 -0700150static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 struct nfs4_readdir_arg *readdir)
152{
Al Viro0dbb4c62006-10-19 23:28:49 -0700153 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155 BUG_ON(readdir->count < 80);
156 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000157 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
159 return;
160 }
161
162 readdir->cookie = 0;
163 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
164 if (cookie == 2)
165 return;
166
167 /*
168 * NFSv4 servers do not return entries for '.' and '..'
169 * Therefore, we fake these entries here. We let '.'
170 * have cookie 0 and '..' have cookie 1. Note that
171 * when talking to the server, we always send cookie 0
172 * instead of 1 or 2.
173 */
Al Viro0dbb4c62006-10-19 23:28:49 -0700174 start = p = kmap_atomic(*readdir->pages, KM_USER0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176 if (cookie == 0) {
177 *p++ = xdr_one; /* next */
178 *p++ = xdr_zero; /* cookie, first word */
179 *p++ = xdr_one; /* cookie, second word */
180 *p++ = xdr_one; /* entry len */
181 memcpy(p, ".\0\0\0", 4); /* entry */
182 p++;
183 *p++ = xdr_one; /* bitmap length */
184 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
185 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400186 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 }
188
189 *p++ = xdr_one; /* next */
190 *p++ = xdr_zero; /* cookie, first word */
191 *p++ = xdr_two; /* cookie, second word */
192 *p++ = xdr_two; /* entry len */
193 memcpy(p, "..\0\0", 4); /* entry */
194 p++;
195 *p++ = xdr_one; /* bitmap length */
196 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
197 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400198 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200 readdir->pgbase = (char *)p - (char *)start;
201 readdir->count -= readdir->pgbase;
202 kunmap_atomic(start, KM_USER0);
203}
204
Trond Myklebust65de8722008-12-23 15:21:44 -0500205static int nfs4_wait_clnt_recover(struct nfs_client *clp)
206{
207 int res;
208
209 might_sleep();
210
Trond Myklebuste005e802008-12-23 15:21:48 -0500211 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400212 nfs_wait_bit_killable, TASK_KILLABLE);
Trond Myklebust65de8722008-12-23 15:21:44 -0500213 return res;
214}
215
216static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
217{
218 int res = 0;
219
220 might_sleep();
221
222 if (*timeout <= 0)
223 *timeout = NFS4_POLL_RETRY_MIN;
224 if (*timeout > NFS4_POLL_RETRY_MAX)
225 *timeout = NFS4_POLL_RETRY_MAX;
226 schedule_timeout_killable(*timeout);
227 if (fatal_signal_pending(current))
228 res = -ERESTARTSYS;
229 *timeout <<= 1;
230 return res;
231}
232
233/* This is the error handling routine for processes that are allowed
234 * to sleep.
235 */
236static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
237{
238 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500239 struct nfs4_state *state = exception->state;
Trond Myklebust65de8722008-12-23 15:21:44 -0500240 int ret = errorcode;
241
242 exception->retry = 0;
243 switch(errorcode) {
244 case 0:
245 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500246 case -NFS4ERR_ADMIN_REVOKED:
247 case -NFS4ERR_BAD_STATEID:
248 case -NFS4ERR_OPENMODE:
249 if (state == NULL)
250 break;
251 nfs4_state_mark_reclaim_nograce(clp, state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500252 goto do_state_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500253 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500254 if (state == NULL)
255 break;
256 nfs4_state_mark_reclaim_reboot(clp, state);
257 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust65de8722008-12-23 15:21:44 -0500258 case -NFS4ERR_EXPIRED:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500259 goto do_state_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500260#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400261 case -NFS4ERR_BADSESSION:
262 case -NFS4ERR_BADSLOT:
263 case -NFS4ERR_BAD_HIGH_SLOT:
264 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
265 case -NFS4ERR_DEADSESSION:
266 case -NFS4ERR_SEQ_FALSE_RETRY:
267 case -NFS4ERR_SEQ_MISORDERED:
268 dprintk("%s ERROR: %d Reset session\n", __func__,
269 errorcode);
Andy Adamson0b9e2d42009-12-04 16:02:14 -0500270 nfs4_schedule_state_recovery(clp);
Andy Adamson4745e312009-04-01 09:22:42 -0400271 exception->retry = 1;
Andy Adamsonb9179232009-12-04 15:55:32 -0500272 break;
Trond Myklebust03391692010-01-26 15:42:38 -0500273#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500274 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500275 if (exception->timeout > HZ) {
276 /* We have retried a decent amount, time to
277 * fail
278 */
279 ret = -EBUSY;
280 break;
281 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500282 case -NFS4ERR_GRACE:
283 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -0500284 case -EKEYEXPIRED:
Trond Myklebust65de8722008-12-23 15:21:44 -0500285 ret = nfs4_delay(server->client, &exception->timeout);
286 if (ret != 0)
287 break;
288 case -NFS4ERR_OLD_STATEID:
289 exception->retry = 1;
290 }
291 /* We failed to handle the error */
292 return nfs4_map_errors(ret);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500293do_state_recovery:
294 nfs4_schedule_state_recovery(clp);
295 ret = nfs4_wait_clnt_recover(clp);
296 if (ret == 0)
297 exception->retry = 1;
298 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500299}
300
301
Trond Myklebust26e976a2006-01-03 09:55:21 +0100302static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
David Howells7539bba2006-08-22 20:06:09 -0400304 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 spin_lock(&clp->cl_lock);
306 if (time_before(clp->cl_last_renewal,timestamp))
307 clp->cl_last_renewal = timestamp;
308 spin_unlock(&clp->cl_lock);
309}
310
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400311#if defined(CONFIG_NFS_V4_1)
312
Benny Halevy510b8172009-04-01 09:22:14 -0400313/*
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400314 * nfs4_free_slot - free a slot and efficiently update slot table.
315 *
316 * freeing a slot is trivially done by clearing its respective bit
317 * in the bitmap.
318 * If the freed slotid equals highest_used_slotid we want to update it
319 * so that the server would be able to size down the slot table if needed,
320 * otherwise we know that the highest_used_slotid is still in use.
321 * When updating highest_used_slotid there may be "holes" in the bitmap
322 * so we need to scan down from highest_used_slotid to 0 looking for the now
323 * highest slotid in use.
324 * If none found, highest_used_slotid is set to -1.
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500325 *
326 * Must be called while holding tbl->slot_tbl_lock
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400327 */
328static void
329nfs4_free_slot(struct nfs4_slot_table *tbl, u8 free_slotid)
330{
331 int slotid = free_slotid;
332
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400333 /* clear used bit in bitmap */
334 __clear_bit(slotid, tbl->used_slots);
335
336 /* update highest_used_slotid when it is freed */
337 if (slotid == tbl->highest_used_slotid) {
338 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500339 if (slotid < tbl->max_slots)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400340 tbl->highest_used_slotid = slotid;
341 else
342 tbl->highest_used_slotid = -1;
343 }
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400344 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
345 free_slotid, tbl->highest_used_slotid);
346}
347
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800348/*
349 * Signal state manager thread if session is drained
350 */
351static void nfs41_check_drain_session_complete(struct nfs4_session *ses)
352{
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800353 struct rpc_task *task;
354
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800355 if (!test_bit(NFS4CLNT_SESSION_DRAINING, &ses->clp->cl_state)) {
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800356 task = rpc_wake_up_next(&ses->fc_slot_table.slot_tbl_waitq);
357 if (task)
358 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800359 return;
360 }
361
362 if (ses->fc_slot_table.highest_used_slotid != -1)
363 return;
364
365 dprintk("%s COMPLETE: Session Drained\n", __func__);
366 complete(&ses->complete);
367}
368
Trond Myklebustd61e6122009-12-05 19:32:19 -0500369static void nfs41_sequence_free_slot(const struct nfs_client *clp,
Andy Adamson13615872009-04-01 09:22:17 -0400370 struct nfs4_sequence_res *res)
371{
372 struct nfs4_slot_table *tbl;
373
Andy Adamson13615872009-04-01 09:22:17 -0400374 tbl = &clp->cl_session->fc_slot_table;
375 if (res->sr_slotid == NFS4_MAX_SLOT_TABLE) {
Andy Adamson13615872009-04-01 09:22:17 -0400376 /* just wake up the next guy waiting since
377 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500378 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500379 return;
Andy Adamson13615872009-04-01 09:22:17 -0400380 }
Andy Adamsonea028ac2009-12-04 15:55:38 -0500381
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500382 spin_lock(&tbl->slot_tbl_lock);
383 nfs4_free_slot(tbl, res->sr_slotid);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800384 nfs41_check_drain_session_complete(clp->cl_session);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500385 spin_unlock(&tbl->slot_tbl_lock);
386 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
Andy Adamson13615872009-04-01 09:22:17 -0400387}
388
Andy Adamsonb0df8062009-04-01 09:22:18 -0400389static void nfs41_sequence_done(struct nfs_client *clp,
390 struct nfs4_sequence_res *res,
391 int rpc_status)
392{
393 unsigned long timestamp;
394 struct nfs4_slot_table *tbl;
395 struct nfs4_slot *slot;
396
397 /*
398 * sr_status remains 1 if an RPC level error occurred. The server
399 * may or may not have processed the sequence operation..
400 * Proceed as if the server received and processed the sequence
401 * operation.
402 */
403 if (res->sr_status == 1)
404 res->sr_status = NFS_OK;
405
406 /* -ERESTARTSYS can result in skipping nfs41_sequence_setup */
407 if (res->sr_slotid == NFS4_MAX_SLOT_TABLE)
408 goto out;
409
Andy Adamson691daf32009-12-04 15:55:39 -0500410 /* Check the SEQUENCE operation status */
Andy Adamsonb0df8062009-04-01 09:22:18 -0400411 if (res->sr_status == 0) {
Andy Adamson691daf32009-12-04 15:55:39 -0500412 tbl = &clp->cl_session->fc_slot_table;
413 slot = tbl->slots + res->sr_slotid;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400414 /* Update the slot's sequence and clientid lease timer */
415 ++slot->seq_nr;
416 timestamp = res->sr_renewal_time;
417 spin_lock(&clp->cl_lock);
418 if (time_before(clp->cl_last_renewal, timestamp))
419 clp->cl_last_renewal = timestamp;
420 spin_unlock(&clp->cl_lock);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500421 /* Check sequence flags */
Alexandros Batsakis71358402010-02-05 03:45:05 -0800422 if (atomic_read(&clp->cl_count) > 1)
423 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400424 }
425out:
426 /* The session may be reset by one of the error handlers. */
427 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
428 nfs41_sequence_free_slot(clp, res);
429}
430
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400431/*
Benny Halevy510b8172009-04-01 09:22:14 -0400432 * nfs4_find_slot - efficiently look for a free slot
433 *
434 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
435 * If found, we mark the slot as used, update the highest_used_slotid,
436 * and respectively set up the sequence operation args.
437 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400438 *
439 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400440 */
441static u8
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800442nfs4_find_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400443{
444 int slotid;
445 u8 ret_id = NFS4_MAX_SLOT_TABLE;
446 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
447
Benny Halevy510b8172009-04-01 09:22:14 -0400448 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
449 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
450 tbl->max_slots);
451 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
452 if (slotid >= tbl->max_slots)
453 goto out;
454 __set_bit(slotid, tbl->used_slots);
455 if (slotid > tbl->highest_used_slotid)
456 tbl->highest_used_slotid = slotid;
457 ret_id = slotid;
458out:
459 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
460 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400461 return ret_id;
462}
463
Andy Adamsonce5039c2009-04-01 09:22:13 -0400464static int nfs41_setup_sequence(struct nfs4_session *session,
465 struct nfs4_sequence_args *args,
466 struct nfs4_sequence_res *res,
467 int cache_reply,
468 struct rpc_task *task)
469{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400470 struct nfs4_slot *slot;
471 struct nfs4_slot_table *tbl;
472 u8 slotid;
473
474 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400475 /* slot already allocated? */
476 if (res->sr_slotid != NFS4_MAX_SLOT_TABLE)
477 return 0;
478
479 memset(res, 0, sizeof(*res));
480 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400481 tbl = &session->fc_slot_table;
482
483 spin_lock(&tbl->slot_tbl_lock);
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800484 if (test_bit(NFS4CLNT_SESSION_DRAINING, &session->clp->cl_state) &&
485 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamsonea028ac2009-12-04 15:55:38 -0500486 /*
487 * The state manager will wait until the slot table is empty.
488 * Schedule the reset thread
489 */
Andy Adamson05f0d232009-12-04 15:55:37 -0500490 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400491 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500492 dprintk("%s Schedule Session Reset\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500493 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400494 }
495
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800496 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
497 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
498 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
499 spin_unlock(&tbl->slot_tbl_lock);
500 dprintk("%s enforce FIFO order\n", __func__);
501 return -EAGAIN;
502 }
503
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800504 slotid = nfs4_find_slot(tbl);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400505 if (slotid == NFS4_MAX_SLOT_TABLE) {
506 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
507 spin_unlock(&tbl->slot_tbl_lock);
508 dprintk("<-- %s: no free slots\n", __func__);
509 return -EAGAIN;
510 }
511 spin_unlock(&tbl->slot_tbl_lock);
512
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800513 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400514 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400515 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400516 args->sa_slotid = slotid;
517 args->sa_cache_this = cache_reply;
518
519 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
520
Benny Halevy99fe60d2009-04-01 09:22:29 -0400521 res->sr_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400522 res->sr_slotid = slotid;
523 res->sr_renewal_time = jiffies;
524 /*
525 * sr_status is only set in decode_sequence, and so will remain
526 * set to 1 if an rpc level failure occurs.
527 */
528 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400529 return 0;
530}
531
532int nfs4_setup_sequence(struct nfs_client *clp,
533 struct nfs4_sequence_args *args,
534 struct nfs4_sequence_res *res,
535 int cache_reply,
536 struct rpc_task *task)
537{
538 int ret = 0;
539
540 dprintk("--> %s clp %p session %p sr_slotid %d\n",
541 __func__, clp, clp->cl_session, res->sr_slotid);
542
543 if (!nfs4_has_session(clp))
544 goto out;
545 ret = nfs41_setup_sequence(clp->cl_session, args, res, cache_reply,
546 task);
Andy Adamson2628edd2009-12-04 15:55:35 -0500547 if (ret && ret != -EAGAIN) {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400548 /* terminate rpc task */
549 task->tk_status = ret;
550 task->tk_action = NULL;
551 }
552out:
553 dprintk("<-- %s status=%d\n", __func__, ret);
554 return ret;
555}
556
557struct nfs41_call_sync_data {
558 struct nfs_client *clp;
559 struct nfs4_sequence_args *seq_args;
560 struct nfs4_sequence_res *seq_res;
561 int cache_reply;
562};
563
564static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
565{
566 struct nfs41_call_sync_data *data = calldata;
567
568 dprintk("--> %s data->clp->cl_session %p\n", __func__,
569 data->clp->cl_session);
570 if (nfs4_setup_sequence(data->clp, data->seq_args,
571 data->seq_res, data->cache_reply, task))
572 return;
573 rpc_call_start(task);
574}
575
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800576static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
577{
578 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
579 nfs41_call_sync_prepare(task, calldata);
580}
581
Andy Adamson69ab40c2009-04-01 09:22:19 -0400582static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
583{
584 struct nfs41_call_sync_data *data = calldata;
585
586 nfs41_sequence_done(data->clp, data->seq_res, task->tk_status);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400587}
588
Andy Adamsonce5039c2009-04-01 09:22:13 -0400589struct rpc_call_ops nfs41_call_sync_ops = {
590 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400591 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400592};
593
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800594struct rpc_call_ops nfs41_call_priv_sync_ops = {
595 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
596 .rpc_call_done = nfs41_call_sync_done,
597};
598
Andy Adamsonce5039c2009-04-01 09:22:13 -0400599static int nfs4_call_sync_sequence(struct nfs_client *clp,
600 struct rpc_clnt *clnt,
601 struct rpc_message *msg,
602 struct nfs4_sequence_args *args,
603 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800604 int cache_reply,
605 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400606{
607 int ret;
608 struct rpc_task *task;
609 struct nfs41_call_sync_data data = {
610 .clp = clp,
611 .seq_args = args,
612 .seq_res = res,
613 .cache_reply = cache_reply,
614 };
615 struct rpc_task_setup task_setup = {
616 .rpc_client = clnt,
617 .rpc_message = msg,
618 .callback_ops = &nfs41_call_sync_ops,
619 .callback_data = &data
620 };
621
622 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800623 if (privileged)
624 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400625 task = rpc_run_task(&task_setup);
626 if (IS_ERR(task))
627 ret = PTR_ERR(task);
628 else {
629 ret = task->tk_status;
630 rpc_put_task(task);
631 }
632 return ret;
633}
634
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400635int _nfs4_call_sync_session(struct nfs_server *server,
636 struct rpc_message *msg,
637 struct nfs4_sequence_args *args,
638 struct nfs4_sequence_res *res,
639 int cache_reply)
640{
Andy Adamsonce5039c2009-04-01 09:22:13 -0400641 return nfs4_call_sync_sequence(server->nfs_client, server->client,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800642 msg, args, res, cache_reply, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400643}
644
645#endif /* CONFIG_NFS_V4_1 */
646
647int _nfs4_call_sync(struct nfs_server *server,
648 struct rpc_message *msg,
649 struct nfs4_sequence_args *args,
650 struct nfs4_sequence_res *res,
651 int cache_reply)
652{
Benny Halevyf3752972009-04-01 09:22:04 -0400653 args->sa_session = res->sr_session = NULL;
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400654 return rpc_call_sync(server->client, msg, 0);
655}
656
657#define nfs4_call_sync(server, msg, args, res, cache_reply) \
658 (server)->nfs_client->cl_call_sync((server), (msg), &(args)->seq_args, \
659 &(res)->seq_res, (cache_reply))
660
Andy Adamsonb0df8062009-04-01 09:22:18 -0400661static void nfs4_sequence_done(const struct nfs_server *server,
662 struct nfs4_sequence_res *res, int rpc_status)
663{
664#ifdef CONFIG_NFS_V4_1
665 if (nfs4_has_session(server->nfs_client))
666 nfs41_sequence_done(server->nfs_client, res, rpc_status);
667#endif /* CONFIG_NFS_V4_1 */
668}
669
Trond Myklebust38478b22006-05-25 01:40:57 -0400670static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
Trond Myklebust38478b22006-05-25 01:40:57 -0400672 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Trond Myklebust38478b22006-05-25 01:40:57 -0400674 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400675 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
676 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400677 nfs_force_lookup_revalidate(dir);
Trond Myklebust40d24702007-10-08 09:24:22 -0400678 nfsi->change_attr = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400679 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680}
681
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100682struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400683 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100684 struct nfs_openargs o_arg;
685 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100686 struct nfs_open_confirmargs c_arg;
687 struct nfs_open_confirmres c_res;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100688 struct nfs_fattr f_attr;
689 struct nfs_fattr dir_attr;
Trond Myklebustad389da2007-06-05 12:30:00 -0400690 struct path path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100691 struct dentry *dir;
692 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400693 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100694 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100695 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400696 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100697 int rpc_status;
698 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100699};
700
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400701
702static void nfs4_init_opendata_res(struct nfs4_opendata *p)
703{
704 p->o_res.f_attr = &p->f_attr;
705 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400706 p->o_res.seqid = p->o_arg.seqid;
707 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400708 p->o_res.server = p->o_arg.server;
709 nfs_fattr_init(&p->f_attr);
710 nfs_fattr_init(&p->dir_attr);
Benny Halevy578e4582009-06-18 22:01:23 -0400711 p->o_res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400712}
713
Trond Myklebustad389da2007-06-05 12:30:00 -0400714static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500715 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100716 const struct iattr *attrs)
717{
Trond Myklebustad389da2007-06-05 12:30:00 -0400718 struct dentry *parent = dget_parent(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100719 struct inode *dir = parent->d_inode;
720 struct nfs_server *server = NFS_SERVER(dir);
721 struct nfs4_opendata *p;
722
723 p = kzalloc(sizeof(*p), GFP_KERNEL);
724 if (p == NULL)
725 goto err;
726 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
727 if (p->o_arg.seqid == NULL)
728 goto err_free;
Al Virof6948692010-01-30 13:51:04 -0500729 path_get(path);
730 p->path = *path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100731 p->dir = parent;
732 p->owner = sp;
733 atomic_inc(&sp->so_count);
734 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500735 p->o_arg.open_flags = flags;
736 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400737 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400738 p->o_arg.id = sp->so_owner_id.id;
Trond Myklebustad389da2007-06-05 12:30:00 -0400739 p->o_arg.name = &p->path.dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100740 p->o_arg.server = server;
741 p->o_arg.bitmask = server->attr_bitmask;
742 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100743 if (flags & O_EXCL) {
Alexandros Batsakis4882ef72009-12-05 13:30:21 -0500744 if (nfs4_has_persistent_session(server->nfs_client)) {
745 /* GUARDED */
746 p->o_arg.u.attrs = &p->attrs;
747 memcpy(&p->attrs, attrs, sizeof(p->attrs));
748 } else { /* EXCLUSIVE4_1 */
749 u32 *s = (u32 *) p->o_arg.u.verifier.data;
750 s[0] = jiffies;
751 s[1] = current->pid;
752 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100753 } else if (flags & O_CREAT) {
754 p->o_arg.u.attrs = &p->attrs;
755 memcpy(&p->attrs, attrs, sizeof(p->attrs));
756 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100757 p->c_arg.fh = &p->o_res.fh;
758 p->c_arg.stateid = &p->o_res.stateid;
759 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400760 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400761 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100762 return p;
763err_free:
764 kfree(p);
765err:
766 dput(parent);
767 return NULL;
768}
769
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400770static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100771{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400772 struct nfs4_opendata *p = container_of(kref,
773 struct nfs4_opendata, kref);
774
775 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400776 if (p->state != NULL)
777 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400778 nfs4_put_state_owner(p->owner);
779 dput(p->dir);
Jan Blunck31f31db12008-05-02 13:42:45 -0700780 path_put(&p->path);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400781 kfree(p);
782}
783
784static void nfs4_opendata_put(struct nfs4_opendata *p)
785{
786 if (p != NULL)
787 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100788}
789
Trond Myklebust06f814a2006-01-03 09:55:07 +0100790static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
791{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100792 int ret;
793
Trond Myklebust06f814a2006-01-03 09:55:07 +0100794 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100795 return ret;
796}
797
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500798static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400799{
800 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500801
802 if (open_mode & O_EXCL)
803 goto out;
804 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400805 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500806 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
807 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400808 break;
809 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500810 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
811 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400812 break;
813 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500814 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
815 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400816 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500817out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400818 return ret;
819}
820
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500821static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400822{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500823 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400824 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500825 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400826 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500827 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400828 return 1;
829}
830
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500831static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100832{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500833 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100834 case FMODE_WRITE:
835 state->n_wronly++;
836 break;
837 case FMODE_READ:
838 state->n_rdonly++;
839 break;
840 case FMODE_READ|FMODE_WRITE:
841 state->n_rdwr++;
842 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500843 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100844}
845
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500846static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400847{
848 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
849 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
850 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500851 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400852 case FMODE_READ:
853 set_bit(NFS_O_RDONLY_STATE, &state->flags);
854 break;
855 case FMODE_WRITE:
856 set_bit(NFS_O_WRONLY_STATE, &state->flags);
857 break;
858 case FMODE_READ|FMODE_WRITE:
859 set_bit(NFS_O_RDWR_STATE, &state->flags);
860 }
861}
862
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500863static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400864{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400865 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500866 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400867 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400868}
869
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500870static 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 -0700871{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400872 /*
873 * Protect the call to nfs4_state_set_mode_locked and
874 * serialise the stateid update
875 */
876 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400877 if (deleg_stateid != NULL) {
878 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
879 set_bit(NFS_DELEGATED_STATE, &state->flags);
880 }
881 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500882 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400883 write_sequnlock(&state->seqlock);
884 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500885 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700886 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887}
888
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500889static 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 -0500890{
891 struct nfs_inode *nfsi = NFS_I(state->inode);
892 struct nfs_delegation *deleg_cur;
893 int ret = 0;
894
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500895 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500896
897 rcu_read_lock();
898 deleg_cur = rcu_dereference(nfsi->delegation);
899 if (deleg_cur == NULL)
900 goto no_delegation;
901
902 spin_lock(&deleg_cur->lock);
903 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500904 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500905 goto no_delegation_unlock;
906
907 if (delegation == NULL)
908 delegation = &deleg_cur->stateid;
909 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
910 goto no_delegation_unlock;
911
Trond Myklebustb7391f42008-12-23 15:21:52 -0500912 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500913 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500914 ret = 1;
915no_delegation_unlock:
916 spin_unlock(&deleg_cur->lock);
917no_delegation:
918 rcu_read_unlock();
919
920 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500921 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500922 ret = 1;
923 }
924
925 return ret;
926}
927
928
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500929static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400930{
931 struct nfs_delegation *delegation;
932
933 rcu_read_lock();
934 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500935 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -0400936 rcu_read_unlock();
937 return;
938 }
939 rcu_read_unlock();
940 nfs_inode_return_delegation(inode);
941}
942
Trond Myklebust6ee41262007-07-08 14:11:36 -0400943static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400944{
945 struct nfs4_state *state = opendata->state;
946 struct nfs_inode *nfsi = NFS_I(state->inode);
947 struct nfs_delegation *delegation;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500948 int open_mode = opendata->o_arg.open_flags & O_EXCL;
949 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400950 nfs4_stateid stateid;
951 int ret = -EAGAIN;
952
Trond Myklebustaac00a82007-07-05 19:02:21 -0400953 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500954 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400955 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500956 if (can_open_cached(state, fmode, open_mode)) {
957 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400958 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400959 goto out_return_state;
960 }
961 spin_unlock(&state->owner->so_lock);
962 }
Trond Myklebust34310432008-12-23 15:21:38 -0500963 rcu_read_lock();
964 delegation = rcu_dereference(nfsi->delegation);
965 if (delegation == NULL ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500966 !can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -0500967 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -0400968 break;
Trond Myklebust34310432008-12-23 15:21:38 -0500969 }
Trond Myklebustaac00a82007-07-05 19:02:21 -0400970 /* Save the delegation */
971 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
972 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -0400973 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400974 if (ret != 0)
975 goto out;
976 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -0500977
978 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500979 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -0500980 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400981 }
Trond Myklebustaac00a82007-07-05 19:02:21 -0400982out:
983 return ERR_PTR(ret);
984out_return_state:
985 atomic_inc(&state->count);
986 return state;
987}
988
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100989static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
990{
991 struct inode *inode;
992 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -0400993 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400994 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100995
Trond Myklebustaac00a82007-07-05 19:02:21 -0400996 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400997 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400998 goto out;
999 }
1000
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001001 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001002 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001003 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001004 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001005 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001006 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001007 goto err;
1008 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001009 state = nfs4_get_open_state(inode, data->owner);
1010 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001011 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001012 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001013 int delegation_flags = 0;
1014
Trond Myklebust003707c2007-07-05 18:07:55 -04001015 rcu_read_lock();
1016 delegation = rcu_dereference(NFS_I(inode)->delegation);
1017 if (delegation)
1018 delegation_flags = delegation->flags;
1019 rcu_read_unlock();
Trond Myklebust15c831b2008-12-23 15:21:39 -05001020 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001021 nfs_inode_set_delegation(state->inode,
1022 data->owner->so_cred,
1023 &data->o_res);
1024 else
1025 nfs_inode_reclaim_delegation(state->inode,
1026 data->owner->so_cred,
1027 &data->o_res);
1028 }
Trond Myklebust34310432008-12-23 15:21:38 -05001029
1030 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001031 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001032 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001033out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001034 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001035err_put_inode:
1036 iput(inode);
1037err:
1038 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001039}
1040
Trond Myklebust864472e2006-01-03 09:55:15 +01001041static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1042{
1043 struct nfs_inode *nfsi = NFS_I(state->inode);
1044 struct nfs_open_context *ctx;
1045
1046 spin_lock(&state->inode->i_lock);
1047 list_for_each_entry(ctx, &nfsi->open_files, list) {
1048 if (ctx->state != state)
1049 continue;
1050 get_nfs_open_context(ctx);
1051 spin_unlock(&state->inode->i_lock);
1052 return ctx;
1053 }
1054 spin_unlock(&state->inode->i_lock);
1055 return ERR_PTR(-ENOENT);
1056}
1057
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001058static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1059{
1060 struct nfs4_opendata *opendata;
1061
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001062 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001063 if (opendata == NULL)
1064 return ERR_PTR(-ENOMEM);
1065 opendata->state = state;
1066 atomic_inc(&state->count);
1067 return opendata;
1068}
1069
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001070static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001071{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001072 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001073 int ret;
1074
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001075 opendata->o_arg.open_flags = 0;
1076 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001077 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1078 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1079 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001080 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001081 if (ret != 0)
1082 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001083 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001084 if (IS_ERR(newstate))
1085 return PTR_ERR(newstate);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001086 nfs4_close_state(&opendata->path, newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001087 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001088 return 0;
1089}
1090
1091static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1092{
Trond Myklebust864472e2006-01-03 09:55:15 +01001093 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001094 int ret;
1095
1096 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001097 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001098 smp_rmb();
1099 if (state->n_rdwr != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001100 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001101 if (ret != 0)
1102 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001103 if (newstate != state)
1104 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001105 }
1106 if (state->n_wronly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001107 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001108 if (ret != 0)
1109 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001110 if (newstate != state)
1111 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001112 }
1113 if (state->n_rdonly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001114 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001115 if (ret != 0)
1116 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001117 if (newstate != state)
1118 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001119 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001120 /*
1121 * We may have performed cached opens for all three recoveries.
1122 * Check if we need to update the current stateid.
1123 */
1124 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1125 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001126 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001127 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1128 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001129 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001130 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001131 return 0;
1132}
1133
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134/*
1135 * OPEN_RECLAIM:
1136 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001138static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001140 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001141 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001142 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 int status;
1144
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001145 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1146 if (IS_ERR(opendata))
1147 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001148 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1149 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001150 rcu_read_lock();
1151 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001152 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001153 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001154 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001155 opendata->o_arg.u.delegation_type = delegation_type;
1156 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001157 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 return status;
1159}
1160
Trond Myklebust539cd032007-06-05 11:46:42 -04001161static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162{
1163 struct nfs_server *server = NFS_SERVER(state->inode);
1164 struct nfs4_exception exception = { };
1165 int err;
1166 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001167 err = _nfs4_do_open_reclaim(ctx, state);
Jeff Layton2c643482010-01-07 09:42:03 -05001168 if (err != -NFS4ERR_DELAY && err != -EKEYEXPIRED)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001169 break;
1170 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 } while (exception.retry);
1172 return err;
1173}
1174
Trond Myklebust864472e2006-01-03 09:55:15 +01001175static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1176{
1177 struct nfs_open_context *ctx;
1178 int ret;
1179
1180 ctx = nfs4_state_find_open_context(state);
1181 if (IS_ERR(ctx))
1182 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001183 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001184 put_nfs_open_context(ctx);
1185 return ret;
1186}
1187
Trond Myklebust13437e12007-07-06 15:10:43 -04001188static 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 -07001189{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001190 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001191 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001193 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1194 if (IS_ERR(opendata))
1195 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001196 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -04001197 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001198 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +01001199 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001200 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001201 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202}
1203
Trond Myklebust13437e12007-07-06 15:10:43 -04001204int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205{
1206 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001207 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 int err;
1209 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001210 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 switch (err) {
1212 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001213 case -ENOENT:
1214 case -ESTALE:
1215 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001216 case -NFS4ERR_BADSESSION:
1217 case -NFS4ERR_BADSLOT:
1218 case -NFS4ERR_BAD_HIGH_SLOT:
1219 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1220 case -NFS4ERR_DEADSESSION:
1221 nfs4_schedule_state_recovery(
1222 server->nfs_client);
1223 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 case -NFS4ERR_STALE_CLIENTID:
1225 case -NFS4ERR_STALE_STATEID:
1226 case -NFS4ERR_EXPIRED:
1227 /* Don't recall a delegation if it was lost */
David Howells7539bba2006-08-22 20:06:09 -04001228 nfs4_schedule_state_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001229 goto out;
1230 case -ERESTARTSYS:
1231 /*
1232 * The show must go on: exit, but mark the
1233 * stateid as needing recovery.
1234 */
1235 case -NFS4ERR_ADMIN_REVOKED:
1236 case -NFS4ERR_BAD_STATEID:
1237 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
1238 case -ENOMEM:
1239 err = 0;
1240 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 }
1242 err = nfs4_handle_exception(server, err, &exception);
1243 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001244out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 return err;
1246}
1247
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001248static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1249{
1250 struct nfs4_opendata *data = calldata;
1251
1252 data->rpc_status = task->tk_status;
1253 if (RPC_ASSASSINATED(task))
1254 return;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001255 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001256 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1257 sizeof(data->o_res.stateid.data));
Trond Myklebustbb226292008-01-02 15:19:18 -05001258 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001259 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001260 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001261 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001262}
1263
1264static void nfs4_open_confirm_release(void *calldata)
1265{
1266 struct nfs4_opendata *data = calldata;
1267 struct nfs4_state *state = NULL;
1268
1269 /* If this request hasn't been cancelled, do nothing */
1270 if (data->cancelled == 0)
1271 goto out_free;
1272 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001273 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001274 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001275 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001276 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001277 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001278out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001279 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001280}
1281
1282static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001283 .rpc_call_done = nfs4_open_confirm_done,
1284 .rpc_release = nfs4_open_confirm_release,
1285};
1286
1287/*
1288 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1289 */
1290static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1291{
1292 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1293 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001294 struct rpc_message msg = {
1295 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1296 .rpc_argp = &data->c_arg,
1297 .rpc_resp = &data->c_res,
1298 .rpc_cred = data->owner->so_cred,
1299 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001300 struct rpc_task_setup task_setup_data = {
1301 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001302 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001303 .callback_ops = &nfs4_open_confirm_ops,
1304 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001305 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001306 .flags = RPC_TASK_ASYNC,
1307 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 int status;
1309
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001310 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001311 data->rpc_done = 0;
1312 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001313 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001314 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001315 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001316 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001317 status = nfs4_wait_for_completion_rpc_task(task);
1318 if (status != 0) {
1319 data->cancelled = 1;
1320 smp_wmb();
1321 } else
1322 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001323 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 return status;
1325}
1326
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001327static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001329 struct nfs4_opendata *data = calldata;
1330 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001331
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001332 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1333 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001334 /*
1335 * Check if we still need to send an OPEN call, or if we can use
1336 * a delegation instead.
1337 */
1338 if (data->state != NULL) {
1339 struct nfs_delegation *delegation;
1340
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001341 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001342 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001343 rcu_read_lock();
1344 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1345 if (delegation != NULL &&
Trond Myklebust15c831b2008-12-23 15:21:39 -05001346 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001347 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001348 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001349 }
1350 rcu_read_unlock();
1351 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001352 /* Update sequence id. */
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001353 data->o_arg.id = sp->so_owner_id.id;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001354 data->o_arg.clientid = sp->so_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001355 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001356 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001357 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1358 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001359 data->timestamp = jiffies;
Andy Adamsond8985282009-04-01 09:22:21 -04001360 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
1361 &data->o_arg.seq_args,
1362 &data->o_res.seq_res, 1, task))
1363 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001364 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001365 return;
1366out_no_action:
1367 task->tk_action = NULL;
1368
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001369}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001371static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1372{
1373 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1374 nfs4_open_prepare(task, calldata);
1375}
1376
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001377static void nfs4_open_done(struct rpc_task *task, void *calldata)
1378{
1379 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001381 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001382
Trond Myklebustd61e6122009-12-05 19:32:19 -05001383 nfs4_sequence_done(data->o_arg.server, &data->o_res.seq_res,
1384 task->tk_status);
Andy Adamsond8985282009-04-01 09:22:21 -04001385
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001386 if (RPC_ASSASSINATED(task))
1387 return;
1388 if (task->tk_status == 0) {
1389 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001390 case S_IFREG:
1391 break;
1392 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001393 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001394 break;
1395 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001396 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001397 break;
1398 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001399 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001400 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001401 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001402 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1403 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001404 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001405 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001406}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001407
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001408static void nfs4_open_release(void *calldata)
1409{
1410 struct nfs4_opendata *data = calldata;
1411 struct nfs4_state *state = NULL;
1412
1413 /* If this request hasn't been cancelled, do nothing */
1414 if (data->cancelled == 0)
1415 goto out_free;
1416 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001417 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001418 goto out_free;
1419 /* In case we need an open_confirm, no cleanup! */
1420 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1421 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001422 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001423 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001424 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001425out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001426 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001427}
1428
1429static const struct rpc_call_ops nfs4_open_ops = {
1430 .rpc_call_prepare = nfs4_open_prepare,
1431 .rpc_call_done = nfs4_open_done,
1432 .rpc_release = nfs4_open_release,
1433};
1434
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001435static const struct rpc_call_ops nfs4_recover_open_ops = {
1436 .rpc_call_prepare = nfs4_recover_open_prepare,
1437 .rpc_call_done = nfs4_open_done,
1438 .rpc_release = nfs4_open_release,
1439};
1440
1441static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001442{
1443 struct inode *dir = data->dir->d_inode;
1444 struct nfs_server *server = NFS_SERVER(dir);
1445 struct nfs_openargs *o_arg = &data->o_arg;
1446 struct nfs_openres *o_res = &data->o_res;
1447 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001448 struct rpc_message msg = {
1449 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1450 .rpc_argp = o_arg,
1451 .rpc_resp = o_res,
1452 .rpc_cred = data->owner->so_cred,
1453 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001454 struct rpc_task_setup task_setup_data = {
1455 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001456 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001457 .callback_ops = &nfs4_open_ops,
1458 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001459 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001460 .flags = RPC_TASK_ASYNC,
1461 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001462 int status;
1463
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001464 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001465 data->rpc_done = 0;
1466 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001467 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001468 if (isrecover)
1469 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001470 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001471 if (IS_ERR(task))
1472 return PTR_ERR(task);
1473 status = nfs4_wait_for_completion_rpc_task(task);
1474 if (status != 0) {
1475 data->cancelled = 1;
1476 smp_wmb();
1477 } else
1478 status = data->rpc_status;
1479 rpc_put_task(task);
1480
1481 return status;
1482}
1483
1484static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1485{
1486 struct inode *dir = data->dir->d_inode;
1487 struct nfs_openres *o_res = &data->o_res;
1488 int status;
1489
1490 status = nfs4_run_open_task(data, 1);
1491 if (status != 0 || !data->rpc_done)
1492 return status;
1493
1494 nfs_refresh_inode(dir, o_res->dir_attr);
1495
1496 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1497 status = _nfs4_proc_open_confirm(data);
1498 if (status != 0)
1499 return status;
1500 }
1501
1502 return status;
1503}
1504
1505/*
1506 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1507 */
1508static int _nfs4_proc_open(struct nfs4_opendata *data)
1509{
1510 struct inode *dir = data->dir->d_inode;
1511 struct nfs_server *server = NFS_SERVER(dir);
1512 struct nfs_openargs *o_arg = &data->o_arg;
1513 struct nfs_openres *o_res = &data->o_res;
1514 int status;
1515
1516 status = nfs4_run_open_task(data, 0);
Trond Myklebust3e309912007-07-07 13:19:59 -04001517 if (status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001518 return status;
1519
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001520 if (o_arg->open_flags & O_CREAT) {
1521 update_changeattr(dir, &o_res->cinfo);
1522 nfs_post_op_update_inode(dir, o_res->dir_attr);
1523 } else
1524 nfs_refresh_inode(dir, o_res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001526 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001528 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 }
1530 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001531 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001532 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533}
1534
Trond Myklebust10afec92007-05-14 17:16:04 -04001535static int nfs4_recover_expired_lease(struct nfs_server *server)
Trond Myklebust58d97142006-01-03 09:55:24 +01001536{
David Howells7539bba2006-08-22 20:06:09 -04001537 struct nfs_client *clp = server->nfs_client;
Trond Myklebusta78cb572009-08-09 15:06:19 -04001538 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001539 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001540
Trond Myklebusta78cb572009-08-09 15:06:19 -04001541 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001542 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001543 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001544 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001545 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1546 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001547 break;
Trond Myklebust58d97142006-01-03 09:55:24 +01001548 nfs4_schedule_state_recovery(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001549 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001550 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001551 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001552}
1553
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554/*
1555 * OPEN_EXPIRED:
1556 * reclaim state on the server after a network partition.
1557 * Assumes caller holds the appropriate lock
1558 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001559static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001561 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001562 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001564 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1565 if (IS_ERR(opendata))
1566 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001567 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001568 if (ret == -ESTALE)
Trond Myklebust539cd032007-06-05 11:46:42 -04001569 d_drop(ctx->path.dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001570 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001571 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572}
1573
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001574static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001575{
Trond Myklebust539cd032007-06-05 11:46:42 -04001576 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001577 struct nfs4_exception exception = { };
1578 int err;
1579
1580 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001581 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001582 switch (err) {
1583 default:
1584 goto out;
1585 case -NFS4ERR_GRACE:
1586 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -05001587 case -EKEYEXPIRED:
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001588 nfs4_handle_exception(server, err, &exception);
1589 err = 0;
1590 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001591 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001592out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001593 return err;
1594}
1595
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1597{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001599 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
Trond Myklebust864472e2006-01-03 09:55:15 +01001601 ctx = nfs4_state_find_open_context(state);
1602 if (IS_ERR(ctx))
1603 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001604 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001605 put_nfs_open_context(ctx);
1606 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607}
1608
1609/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001610 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1611 * fields corresponding to attributes that were used to store the verifier.
1612 * Make sure we clobber those fields in the later setattr call
1613 */
1614static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1615{
1616 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1617 !(sattr->ia_valid & ATTR_ATIME_SET))
1618 sattr->ia_valid |= ATTR_ATIME;
1619
1620 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1621 !(sattr->ia_valid & ATTR_MTIME_SET))
1622 sattr->ia_valid |= ATTR_MTIME;
1623}
1624
1625/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001626 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001628static int _nfs4_do_open(struct inode *dir, struct path *path, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629{
1630 struct nfs4_state_owner *sp;
1631 struct nfs4_state *state = NULL;
1632 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001633 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001634 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
1636 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 status = -ENOMEM;
1638 if (!(sp = nfs4_get_state_owner(server, cred))) {
1639 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1640 goto out_err;
1641 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001642 status = nfs4_recover_expired_lease(server);
1643 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001644 goto err_put_state_owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001645 if (path->dentry->d_inode != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001646 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001647 status = -ENOMEM;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001648 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001649 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001650 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
Trond Myklebustaac00a82007-07-05 19:02:21 -04001652 if (path->dentry->d_inode != NULL)
1653 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1654
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001655 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001657 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001659 if (opendata->o_arg.open_flags & O_EXCL)
1660 nfs4_exclusive_attrset(opendata, sattr);
1661
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001662 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001663 status = PTR_ERR(state);
1664 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001665 goto err_opendata_put;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001666 if ((opendata->o_res.rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) != 0)
1667 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001668 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 *res = state;
1671 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001672err_opendata_put:
1673 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001674err_put_state_owner:
1675 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 *res = NULL;
1678 return status;
1679}
1680
1681
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001682static 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 -07001683{
1684 struct nfs4_exception exception = { };
1685 struct nfs4_state *res;
1686 int status;
1687
1688 do {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001689 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 if (status == 0)
1691 break;
1692 /* NOTE: BAD_SEQID means the server and client disagree about the
1693 * book-keeping w.r.t. state-changing operations
1694 * (OPEN/CLOSE/LOCK/LOCKU...)
1695 * It is actually a sign of a bug on the client or on the server.
1696 *
1697 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001698 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 * have unhashed the old state_owner for us, and that we can
1700 * therefore safely retry using a new one. We should still warn
1701 * the user though...
1702 */
1703 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001704 printk(KERN_WARNING "NFS: v4 server %s "
1705 " returned a bad sequence-id error!\n",
1706 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 exception.retry = 1;
1708 continue;
1709 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001710 /*
1711 * BAD_STATEID on OPEN means that the server cancelled our
1712 * state before it received the OPEN_CONFIRM.
1713 * Recover by retrying the request as per the discussion
1714 * on Page 181 of RFC3530.
1715 */
1716 if (status == -NFS4ERR_BAD_STATEID) {
1717 exception.retry = 1;
1718 continue;
1719 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001720 if (status == -EAGAIN) {
1721 /* We must have found a delegation */
1722 exception.retry = 1;
1723 continue;
1724 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1726 status, &exception));
1727 } while (exception.retry);
1728 return res;
1729}
1730
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001731static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1732 struct nfs_fattr *fattr, struct iattr *sattr,
1733 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001735 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001737 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 .iap = sattr,
1739 .server = server,
1740 .bitmask = server->attr_bitmask,
1741 };
1742 struct nfs_setattrres res = {
1743 .fattr = fattr,
1744 .server = server,
1745 };
1746 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001747 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1748 .rpc_argp = &arg,
1749 .rpc_resp = &res,
1750 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001752 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001753 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
Trond Myklebust0e574af2005-10-27 22:12:38 -04001755 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001757 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1758 /* Use that stateid */
1759 } else if (state != NULL) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001760 nfs4_copy_stateid(&arg.stateid, state, current->files);
1761 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1763
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001764 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001765 if (status == 0 && state != NULL)
1766 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001767 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768}
1769
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001770static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1771 struct nfs_fattr *fattr, struct iattr *sattr,
1772 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001774 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 struct nfs4_exception exception = { };
1776 int err;
1777 do {
1778 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001779 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 &exception);
1781 } while (exception.retry);
1782 return err;
1783}
1784
1785struct nfs4_closedata {
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001786 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 struct inode *inode;
1788 struct nfs4_state *state;
1789 struct nfs_closeargs arg;
1790 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001791 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001792 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793};
1794
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001795static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001796{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001797 struct nfs4_closedata *calldata = data;
1798 struct nfs4_state_owner *sp = calldata->state->owner;
Trond Myklebust95121352005-10-18 14:20:12 -07001799
1800 nfs4_put_open_state(calldata->state);
1801 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001802 nfs4_put_state_owner(sp);
Jan Blunck31f31db12008-05-02 13:42:45 -07001803 path_put(&calldata->path);
Trond Myklebust95121352005-10-18 14:20:12 -07001804 kfree(calldata);
1805}
1806
Trond Myklebust88069f72009-12-08 08:33:16 -05001807static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1808 fmode_t fmode)
1809{
1810 spin_lock(&state->owner->so_lock);
1811 if (!(fmode & FMODE_READ))
1812 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1813 if (!(fmode & FMODE_WRITE))
1814 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1815 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1816 spin_unlock(&state->owner->so_lock);
1817}
1818
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001819static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001821 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 struct nfs_server *server = NFS_SERVER(calldata->inode);
1824
Andy Adamson19ddab02009-04-01 09:22:20 -04001825 nfs4_sequence_done(server, &calldata->res.seq_res, task->tk_status);
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001826 if (RPC_ASSASSINATED(task))
1827 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 /* hmm. we are done with the inode, and in the process of freeing
1829 * the state_owner. we keep this around to process errors
1830 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 switch (task->tk_status) {
1832 case 0:
Trond Myklebust45328c32007-07-26 17:47:34 -04001833 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001834 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05001835 nfs4_close_clear_stateid_flags(state,
1836 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 break;
1838 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001839 case -NFS4ERR_OLD_STATEID:
1840 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001842 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001843 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05001845 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1846 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 }
Trond Myklebust72211db2009-12-15 14:47:36 -05001848 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04001849 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850}
1851
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001852static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001854 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001855 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05001856 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07001857
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001858 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001859 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001860
Trond Myklebust88069f72009-12-08 08:33:16 -05001861 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1862 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05001863 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001864 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01001865 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001866 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001867 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
1868 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1869 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04001870 }
1871 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001872 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
1873 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1874 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04001875 }
Trond Myklebuste7616922006-01-03 09:55:13 +01001876 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05001877 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05001878
1879 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001880 /* Note: exit _without_ calling nfs4_close_done */
1881 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07001882 return;
1883 }
Trond Myklebust88069f72009-12-08 08:33:16 -05001884
1885 if (calldata->arg.fmode == 0)
1886 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
1887
Trond Myklebust516a6af2005-10-27 22:12:41 -04001888 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001889 calldata->timestamp = jiffies;
Andy Adamson19ddab02009-04-01 09:22:20 -04001890 if (nfs4_setup_sequence((NFS_SERVER(calldata->inode))->nfs_client,
1891 &calldata->arg.seq_args, &calldata->res.seq_res,
1892 1, task))
1893 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001894 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895}
1896
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001897static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001898 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001899 .rpc_call_done = nfs4_close_done,
1900 .rpc_release = nfs4_free_closedata,
1901};
1902
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903/*
1904 * It is possible for data to be read/written from a mem-mapped file
1905 * after the sys_close call (which hits the vfs layer as a flush).
1906 * This means that we can't safely call nfsv4 close on a file until
1907 * the inode is cleared. This in turn means that we are not good
1908 * NFSv4 citizens - we do not indicate to the server to update the file's
1909 * share state even when we are done with one of the three share
1910 * stateid's in the inode.
1911 *
1912 * NOTE: Caller must be holding the sp->so_owner semaphore!
1913 */
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001914int nfs4_do_close(struct path *path, struct nfs4_state *state, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001916 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04001918 struct nfs4_state_owner *sp = state->owner;
1919 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001920 struct rpc_message msg = {
1921 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1922 .rpc_cred = state->owner->so_cred,
1923 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001924 struct rpc_task_setup task_setup_data = {
1925 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001926 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001927 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05001928 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001929 .flags = RPC_TASK_ASYNC,
1930 };
Trond Myklebust95121352005-10-18 14:20:12 -07001931 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
Andy Adamson19ddab02009-04-01 09:22:20 -04001933 calldata = kzalloc(sizeof(*calldata), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07001935 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001936 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001938 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04001939 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 /* Serialization for the sequence id */
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001941 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001942 if (calldata->arg.seqid == NULL)
1943 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001944 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04001945 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001946 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04001947 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001948 calldata->res.server = server;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04001949 calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Al Virof6948692010-01-30 13:51:04 -05001950 path_get(path);
1951 calldata->path = *path;
Trond Myklebust95121352005-10-18 14:20:12 -07001952
Trond Myklebust5138fde2007-07-14 15:40:01 -04001953 msg.rpc_argp = &calldata->arg,
1954 msg.rpc_resp = &calldata->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001955 task_setup_data.callback_data = calldata;
1956 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001957 if (IS_ERR(task))
1958 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001959 status = 0;
1960 if (wait)
1961 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001962 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001963 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07001964out_free_calldata:
1965 kfree(calldata);
1966out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04001967 nfs4_put_open_state(state);
1968 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07001969 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970}
1971
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001972static 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 -07001973{
1974 struct file *filp;
Trond Myklebust1b45c462007-07-03 13:04:56 -04001975 int ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001976
Trond Myklebust1b45c462007-07-03 13:04:56 -04001977 /* If the open_intent is for execute, we have an extra check to make */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001978 if (fmode & FMODE_EXEC) {
Trond Myklebustaf22f942007-08-10 17:45:10 -04001979 ret = nfs_may_open(state->inode,
Trond Myklebust1b45c462007-07-03 13:04:56 -04001980 state->owner->so_cred,
1981 nd->intent.open.flags);
1982 if (ret < 0)
1983 goto out_close;
1984 }
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001985 filp = lookup_instantiate_filp(nd, path->dentry, NULL);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001986 if (!IS_ERR(filp)) {
1987 struct nfs_open_context *ctx;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04001988 ctx = nfs_file_open_context(filp);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001989 ctx->state = state;
Trond Myklebust95cf9592006-04-18 13:14:06 -04001990 return 0;
1991 }
Trond Myklebust1b45c462007-07-03 13:04:56 -04001992 ret = PTR_ERR(filp);
1993out_close:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001994 nfs4_close_sync(path, state, fmode & (FMODE_READ|FMODE_WRITE));
Trond Myklebust1b45c462007-07-03 13:04:56 -04001995 return ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001996}
1997
1998struct dentry *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
2000{
Trond Myklebustad389da2007-06-05 12:30:00 -04002001 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08002002 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04002003 .dentry = dentry,
2004 };
Jan Blunck4ac91372008-02-14 19:34:32 -08002005 struct dentry *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 struct iattr attr;
2007 struct rpc_cred *cred;
2008 struct nfs4_state *state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002009 struct dentry *res;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002010 fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011
2012 if (nd->flags & LOOKUP_CREATE) {
2013 attr.ia_mode = nd->intent.open.create_mode;
2014 attr.ia_valid = ATTR_MODE;
2015 if (!IS_POSIXACL(dir))
Al Viroce3b0f82009-03-29 19:08:22 -04002016 attr.ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 } else {
2018 attr.ia_valid = 0;
2019 BUG_ON(nd->intent.open.flags & O_CREAT);
2020 }
2021
Trond Myklebust98a8e322008-03-12 12:25:28 -04002022 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 if (IS_ERR(cred))
Trond Myklebust02a913a2005-10-18 14:20:17 -07002024 return (struct dentry *)cred;
Trond Myklebust565277f2007-10-15 18:17:53 -04002025 parent = dentry->d_parent;
2026 /* Protect against concurrent sillydeletes */
2027 nfs_block_sillyrename(parent);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002028 state = nfs4_do_open(dir, &path, fmode, nd->intent.open.flags, &attr, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002030 if (IS_ERR(state)) {
Trond Myklebustd75340c2007-10-01 21:42:01 -04002031 if (PTR_ERR(state) == -ENOENT) {
Trond Myklebust02a913a2005-10-18 14:20:17 -07002032 d_add(dentry, NULL);
Trond Myklebustd75340c2007-10-01 21:42:01 -04002033 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2034 }
Trond Myklebust565277f2007-10-15 18:17:53 -04002035 nfs_unblock_sillyrename(parent);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002036 return (struct dentry *)state;
2037 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002038 res = d_add_unique(dentry, igrab(state->inode));
Trond Myklebust02a913a2005-10-18 14:20:17 -07002039 if (res != NULL)
Trond Myklebustdeee9362007-08-27 11:33:00 -04002040 path.dentry = res;
Trond Myklebustd75340c2007-10-01 21:42:01 -04002041 nfs_set_verifier(path.dentry, nfs_save_change_attribute(dir));
Trond Myklebust565277f2007-10-15 18:17:53 -04002042 nfs_unblock_sillyrename(parent);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002043 nfs4_intent_set_file(nd, &path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002044 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045}
2046
2047int
Trond Myklebust02a913a2005-10-18 14:20:17 -07002048nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049{
Trond Myklebustad389da2007-06-05 12:30:00 -04002050 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08002051 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04002052 .dentry = dentry,
2053 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 struct rpc_cred *cred;
2055 struct nfs4_state *state;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002056 fmode_t fmode = openflags & (FMODE_READ | FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057
Trond Myklebust98a8e322008-03-12 12:25:28 -04002058 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 if (IS_ERR(cred))
2060 return PTR_ERR(cred);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002061 state = nfs4_do_open(dir, &path, fmode, openflags, NULL, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002063 if (IS_ERR(state)) {
2064 switch (PTR_ERR(state)) {
2065 case -EPERM:
2066 case -EACCES:
2067 case -EDQUOT:
2068 case -ENOSPC:
2069 case -EROFS:
2070 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
2071 return 1;
Chuck Leverb87c0ad2006-10-19 23:28:42 -07002072 default:
2073 goto out_drop;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002074 }
Trond Myklebust02a913a2005-10-18 14:20:17 -07002075 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002076 if (state->inode == dentry->d_inode) {
Trond Myklebustd75340c2007-10-01 21:42:01 -04002077 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002078 nfs4_intent_set_file(nd, &path, state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 return 1;
2080 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002081 nfs4_close_sync(&path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002082out_drop:
2083 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 return 0;
2085}
2086
Trond Myklebust1185a552009-12-03 15:54:02 -05002087static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002088{
2089 if (ctx->state == NULL)
2090 return;
2091 if (is_sync)
2092 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
2093 else
2094 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
2095}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
2097static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2098{
Benny Halevy43652ad2009-04-01 09:21:54 -04002099 struct nfs4_server_caps_arg args = {
2100 .fhandle = fhandle,
2101 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 struct nfs4_server_caps_res res = {};
2103 struct rpc_message msg = {
2104 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002105 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 .rpc_resp = &res,
2107 };
2108 int status;
2109
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002110 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 if (status == 0) {
2112 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002113 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2114 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2115 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2116 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2117 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2119 server->caps |= NFS_CAP_ACLS;
2120 if (res.has_links != 0)
2121 server->caps |= NFS_CAP_HARDLINKS;
2122 if (res.has_symlinks != 0)
2123 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002124 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2125 server->caps |= NFS_CAP_FILEID;
2126 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2127 server->caps |= NFS_CAP_MODE;
2128 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2129 server->caps |= NFS_CAP_NLINK;
2130 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2131 server->caps |= NFS_CAP_OWNER;
2132 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2133 server->caps |= NFS_CAP_OWNER_GROUP;
2134 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2135 server->caps |= NFS_CAP_ATIME;
2136 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2137 server->caps |= NFS_CAP_CTIME;
2138 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2139 server->caps |= NFS_CAP_MTIME;
2140
Trond Myklebusta65318b2009-03-11 14:10:28 -04002141 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2142 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2143 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 server->acl_bitmask = res.acl_bitmask;
2145 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002146
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 return status;
2148}
2149
Trond Myklebust55a97592006-06-09 09:34:19 -04002150int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151{
2152 struct nfs4_exception exception = { };
2153 int err;
2154 do {
2155 err = nfs4_handle_exception(server,
2156 _nfs4_server_capabilities(server, fhandle),
2157 &exception);
2158 } while (exception.retry);
2159 return err;
2160}
2161
2162static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2163 struct nfs_fsinfo *info)
2164{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 struct nfs4_lookup_root_arg args = {
2166 .bitmask = nfs4_fattr_bitmap,
2167 };
2168 struct nfs4_lookup_res res = {
2169 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002170 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 .fh = fhandle,
2172 };
2173 struct rpc_message msg = {
2174 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2175 .rpc_argp = &args,
2176 .rpc_resp = &res,
2177 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002178
Trond Myklebust0e574af2005-10-27 22:12:38 -04002179 nfs_fattr_init(info->fattr);
Trond Myklebustfccba802009-07-21 16:48:07 -04002180 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181}
2182
2183static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2184 struct nfs_fsinfo *info)
2185{
2186 struct nfs4_exception exception = { };
2187 int err;
2188 do {
2189 err = nfs4_handle_exception(server,
2190 _nfs4_lookup_root(server, fhandle, info),
2191 &exception);
2192 } while (exception.retry);
2193 return err;
2194}
2195
David Howells54ceac42006-08-22 20:06:13 -04002196/*
2197 * get the file handle for the "/" directory on the server
2198 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002200 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 int status;
2203
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 status = nfs4_lookup_root(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 if (status == 0)
2206 status = nfs4_server_capabilities(server, fhandle);
2207 if (status == 0)
2208 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002209 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210}
2211
Manoj Naik6b97fd32006-06-09 09:34:29 -04002212/*
2213 * Get locations and (maybe) other attributes of a referral.
2214 * Note that we'll actually follow the referral later when
2215 * we detect fsid mismatch in inode revalidation
2216 */
Trond Myklebust56659e92007-07-17 21:52:39 -04002217static 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 -04002218{
2219 int status = -ENOMEM;
2220 struct page *page = NULL;
2221 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002222
2223 page = alloc_page(GFP_KERNEL);
2224 if (page == NULL)
2225 goto out;
2226 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2227 if (locations == NULL)
2228 goto out;
2229
Trond Myklebustc228fd32007-01-13 02:28:11 -05002230 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002231 if (status != 0)
2232 goto out;
2233 /* Make sure server returned a different fsid for the referral */
2234 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -07002235 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 -04002236 status = -EIO;
2237 goto out;
2238 }
2239
2240 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2241 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
2242 if (!fattr->mode)
2243 fattr->mode = S_IFDIR;
2244 memset(fhandle, 0, sizeof(struct nfs_fh));
2245out:
2246 if (page)
2247 __free_page(page);
2248 if (locations)
2249 kfree(locations);
2250 return status;
2251}
2252
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2254{
2255 struct nfs4_getattr_arg args = {
2256 .fh = fhandle,
2257 .bitmask = server->attr_bitmask,
2258 };
2259 struct nfs4_getattr_res res = {
2260 .fattr = fattr,
2261 .server = server,
2262 };
2263 struct rpc_message msg = {
2264 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2265 .rpc_argp = &args,
2266 .rpc_resp = &res,
2267 };
2268
Trond Myklebust0e574af2005-10-27 22:12:38 -04002269 nfs_fattr_init(fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002270 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271}
2272
2273static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2274{
2275 struct nfs4_exception exception = { };
2276 int err;
2277 do {
2278 err = nfs4_handle_exception(server,
2279 _nfs4_proc_getattr(server, fhandle, fattr),
2280 &exception);
2281 } while (exception.retry);
2282 return err;
2283}
2284
2285/*
2286 * The file is not closed if it is opened due to the a request to change
2287 * the size of the file. The open call will not be needed once the
2288 * VFS layer lookup-intents are implemented.
2289 *
2290 * Close is called when the inode is destroyed.
2291 * If we haven't opened the file for O_WRONLY, we
2292 * need to in the size_change case to obtain a stateid.
2293 *
2294 * Got race?
2295 * Because OPEN is always done by name in nfsv4, it is
2296 * possible that we opened a different file by the same
2297 * name. We can recognize this race condition, but we
2298 * can't do anything about it besides returning an error.
2299 *
2300 * This will be fixed with VFS changes (lookup-intent).
2301 */
2302static int
2303nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2304 struct iattr *sattr)
2305{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002306 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002307 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002308 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 int status;
2310
Trond Myklebust0e574af2005-10-27 22:12:38 -04002311 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
Trond Myklebustd5308382005-11-04 15:33:38 -05002313 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002314 if (sattr->ia_valid & ATTR_FILE) {
2315 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002316
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002317 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002318 if (ctx) {
2319 cred = ctx->cred;
2320 state = ctx->state;
2321 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002322 }
2323
2324 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002325 if (status == 0)
2326 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 return status;
2328}
2329
Trond Myklebust56659e92007-07-17 21:52:39 -04002330static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
2331 const struct qstr *name, struct nfs_fh *fhandle,
David Howells2b3de442006-08-22 20:06:09 -04002332 struct nfs_fattr *fattr)
2333{
2334 int status;
2335 struct nfs4_lookup_arg args = {
2336 .bitmask = server->attr_bitmask,
2337 .dir_fh = dirfh,
2338 .name = name,
2339 };
2340 struct nfs4_lookup_res res = {
2341 .server = server,
2342 .fattr = fattr,
2343 .fh = fhandle,
2344 };
2345 struct rpc_message msg = {
2346 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2347 .rpc_argp = &args,
2348 .rpc_resp = &res,
2349 };
2350
2351 nfs_fattr_init(fattr);
2352
2353 dprintk("NFS call lookupfh %s\n", name->name);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002354 status = nfs4_call_sync(server, &msg, &args, &res, 0);
David Howells2b3de442006-08-22 20:06:09 -04002355 dprintk("NFS reply lookupfh: %d\n", status);
David Howells2b3de442006-08-22 20:06:09 -04002356 return status;
2357}
2358
2359static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2360 struct qstr *name, struct nfs_fh *fhandle,
2361 struct nfs_fattr *fattr)
2362{
2363 struct nfs4_exception exception = { };
2364 int err;
2365 do {
Trond Myklebust4e56e082007-07-01 18:13:52 -04002366 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
2367 /* FIXME: !!!! */
2368 if (err == -NFS4ERR_MOVED) {
2369 err = -EREMOTE;
2370 break;
2371 }
2372 err = nfs4_handle_exception(server, err, &exception);
David Howells2b3de442006-08-22 20:06:09 -04002373 } while (exception.retry);
2374 return err;
2375}
2376
Trond Myklebust56659e92007-07-17 21:52:39 -04002377static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2379{
Trond Myklebust4e56e082007-07-01 18:13:52 -04002380 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381
2382 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust4e56e082007-07-01 18:13:52 -04002383 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002384 if (status == -NFS4ERR_MOVED)
2385 status = nfs4_get_referral(dir, name, fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 dprintk("NFS reply lookup: %d\n", status);
2387 return status;
2388}
2389
2390static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2391{
2392 struct nfs4_exception exception = { };
2393 int err;
2394 do {
2395 err = nfs4_handle_exception(NFS_SERVER(dir),
2396 _nfs4_proc_lookup(dir, name, fhandle, fattr),
2397 &exception);
2398 } while (exception.retry);
2399 return err;
2400}
2401
2402static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2403{
Trond Myklebust76b32992007-08-10 17:45:11 -04002404 struct nfs_server *server = NFS_SERVER(inode);
2405 struct nfs_fattr fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 struct nfs4_accessargs args = {
2407 .fh = NFS_FH(inode),
Trond Myklebust76b32992007-08-10 17:45:11 -04002408 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002410 struct nfs4_accessres res = {
2411 .server = server,
2412 .fattr = &fattr,
2413 };
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 Myklebust76b32992007-08-10 17:45:11 -04002439 nfs_fattr_init(&fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002440 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 if (!status) {
2442 entry->mask = 0;
2443 if (res.access & NFS4_ACCESS_READ)
2444 entry->mask |= MAY_READ;
2445 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2446 entry->mask |= MAY_WRITE;
2447 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2448 entry->mask |= MAY_EXEC;
Trond Myklebust76b32992007-08-10 17:45:11 -04002449 nfs_refresh_inode(inode, &fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 }
2451 return status;
2452}
2453
2454static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2455{
2456 struct nfs4_exception exception = { };
2457 int err;
2458 do {
2459 err = nfs4_handle_exception(NFS_SERVER(inode),
2460 _nfs4_proc_access(inode, entry),
2461 &exception);
2462 } while (exception.retry);
2463 return err;
2464}
2465
2466/*
2467 * TODO: For the time being, we don't try to get any attributes
2468 * along with any of the zero-copy operations READ, READDIR,
2469 * READLINK, WRITE.
2470 *
2471 * In the case of the first three, we want to put the GETATTR
2472 * after the read-type operation -- this is because it is hard
2473 * to predict the length of a GETATTR response in v4, and thus
2474 * align the READ data correctly. This means that the GETATTR
2475 * may end up partially falling into the page cache, and we should
2476 * shift it into the 'tail' of the xdr_buf before processing.
2477 * To do this efficiently, we need to know the total length
2478 * of data received, which doesn't seem to be available outside
2479 * of the RPC layer.
2480 *
2481 * In the case of WRITE, we also want to put the GETATTR after
2482 * the operation -- in this case because we want to make sure
2483 * we get the post-operation mtime and size. This means that
2484 * we can't use xdr_encode_pages() as written: we need a variant
2485 * of it which would leave room in the 'tail' iovec.
2486 *
2487 * Both of these changes to the XDR layer would in fact be quite
2488 * minor, but I decided to leave them for a subsequent patch.
2489 */
2490static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2491 unsigned int pgbase, unsigned int pglen)
2492{
2493 struct nfs4_readlink args = {
2494 .fh = NFS_FH(inode),
2495 .pgbase = pgbase,
2496 .pglen = pglen,
2497 .pages = &page,
2498 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002499 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 struct rpc_message msg = {
2501 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2502 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002503 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 };
2505
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002506 return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507}
2508
2509static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2510 unsigned int pgbase, unsigned int pglen)
2511{
2512 struct nfs4_exception exception = { };
2513 int err;
2514 do {
2515 err = nfs4_handle_exception(NFS_SERVER(inode),
2516 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2517 &exception);
2518 } while (exception.retry);
2519 return err;
2520}
2521
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522/*
2523 * Got race?
2524 * We will need to arrange for the VFS layer to provide an atomic open.
2525 * Until then, this create/open method is prone to inefficiency and race
2526 * conditions due to the lookup, create, and open VFS calls from sys_open()
2527 * placed on the wire.
2528 *
2529 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2530 * The file will be opened again in the subsequent VFS open call
2531 * (nfs4_proc_file_open).
2532 *
2533 * The open for read will just hang around to be used by any process that
2534 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2535 */
2536
2537static int
2538nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebust02a913a2005-10-18 14:20:17 -07002539 int flags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540{
Trond Myklebustad389da2007-06-05 12:30:00 -04002541 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08002542 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04002543 .dentry = dentry,
2544 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 struct nfs4_state *state;
2546 struct rpc_cred *cred;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002547 fmode_t fmode = flags & (FMODE_READ | FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 int status = 0;
2549
Trond Myklebust98a8e322008-03-12 12:25:28 -04002550 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 if (IS_ERR(cred)) {
2552 status = PTR_ERR(cred);
2553 goto out;
2554 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002555 state = nfs4_do_open(dir, &path, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002556 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 if (IS_ERR(state)) {
2558 status = PTR_ERR(state);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002559 goto out_putcred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002561 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002562 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 if (flags & O_EXCL) {
2564 struct nfs_fattr fattr;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002565 status = nfs4_do_setattr(state->inode, cred, &fattr, sattr, state);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002566 if (status == 0)
Trond Myklebust65e43082005-08-16 11:49:44 -04002567 nfs_setattr_update_inode(state->inode, sattr);
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002568 nfs_post_op_update_inode(state->inode, &fattr);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002569 }
Trond Myklebustad389da2007-06-05 12:30:00 -04002570 if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002571 status = nfs4_intent_set_file(nd, &path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002572 else
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002573 nfs4_close_sync(&path, state, fmode);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002574out_putcred:
2575 put_rpccred(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576out:
2577 return status;
2578}
2579
2580static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2581{
Trond Myklebust16e42952005-10-27 22:12:44 -04002582 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002583 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002585 .name.len = name->len,
2586 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002587 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002589 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002590 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002591 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002593 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2594 .rpc_argp = &args,
2595 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 };
2597 int status;
2598
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002599 nfs_fattr_init(&res.dir_attr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002600 status = nfs4_call_sync(server, &msg, &args, &res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002601 if (status == 0) {
2602 update_changeattr(dir, &res.cinfo);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002603 nfs_post_op_update_inode(dir, &res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 return status;
2606}
2607
2608static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2609{
2610 struct nfs4_exception exception = { };
2611 int err;
2612 do {
2613 err = nfs4_handle_exception(NFS_SERVER(dir),
2614 _nfs4_proc_remove(dir, name),
2615 &exception);
2616 } while (exception.retry);
2617 return err;
2618}
2619
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002620static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002622 struct nfs_server *server = NFS_SERVER(dir);
2623 struct nfs_removeargs *args = msg->rpc_argp;
2624 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625
Trond Myklebusta65318b2009-03-11 14:10:28 -04002626 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002627 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629}
2630
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002631static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002633 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2634
Andy Adamson472cfbd2009-04-01 09:22:24 -04002635 nfs4_sequence_done(res->server, &res->seq_res, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002636 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002637 return 0;
2638 update_changeattr(dir, &res->cinfo);
2639 nfs_post_op_update_inode(dir, &res->dir_attr);
2640 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641}
2642
2643static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2644 struct inode *new_dir, struct qstr *new_name)
2645{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002646 struct nfs_server *server = NFS_SERVER(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 struct nfs4_rename_arg arg = {
2648 .old_dir = NFS_FH(old_dir),
2649 .new_dir = NFS_FH(new_dir),
2650 .old_name = old_name,
2651 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002652 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 };
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002654 struct nfs_fattr old_fattr, new_fattr;
2655 struct nfs4_rename_res res = {
2656 .server = server,
2657 .old_fattr = &old_fattr,
2658 .new_fattr = &new_fattr,
2659 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 struct rpc_message msg = {
2661 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2662 .rpc_argp = &arg,
2663 .rpc_resp = &res,
2664 };
2665 int status;
2666
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002667 nfs_fattr_init(res.old_fattr);
2668 nfs_fattr_init(res.new_fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002669 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670
2671 if (!status) {
2672 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002673 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002675 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 }
2677 return status;
2678}
2679
2680static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2681 struct inode *new_dir, struct qstr *new_name)
2682{
2683 struct nfs4_exception exception = { };
2684 int err;
2685 do {
2686 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2687 _nfs4_proc_rename(old_dir, old_name,
2688 new_dir, new_name),
2689 &exception);
2690 } while (exception.retry);
2691 return err;
2692}
2693
2694static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2695{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002696 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 struct nfs4_link_arg arg = {
2698 .fh = NFS_FH(inode),
2699 .dir_fh = NFS_FH(dir),
2700 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002701 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002703 struct nfs_fattr fattr, dir_attr;
2704 struct nfs4_link_res res = {
2705 .server = server,
2706 .fattr = &fattr,
2707 .dir_attr = &dir_attr,
2708 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 struct rpc_message msg = {
2710 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2711 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002712 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 };
2714 int status;
2715
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002716 nfs_fattr_init(res.fattr);
2717 nfs_fattr_init(res.dir_attr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002718 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002719 if (!status) {
2720 update_changeattr(dir, &res.cinfo);
2721 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002722 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724
2725 return status;
2726}
2727
2728static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2729{
2730 struct nfs4_exception exception = { };
2731 int err;
2732 do {
2733 err = nfs4_handle_exception(NFS_SERVER(inode),
2734 _nfs4_proc_link(inode, dir, name),
2735 &exception);
2736 } while (exception.retry);
2737 return err;
2738}
2739
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002740struct nfs4_createdata {
2741 struct rpc_message msg;
2742 struct nfs4_create_arg arg;
2743 struct nfs4_create_res res;
2744 struct nfs_fh fh;
2745 struct nfs_fattr fattr;
2746 struct nfs_fattr dir_fattr;
2747};
2748
2749static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2750 struct qstr *name, struct iattr *sattr, u32 ftype)
2751{
2752 struct nfs4_createdata *data;
2753
2754 data = kzalloc(sizeof(*data), GFP_KERNEL);
2755 if (data != NULL) {
2756 struct nfs_server *server = NFS_SERVER(dir);
2757
2758 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2759 data->msg.rpc_argp = &data->arg;
2760 data->msg.rpc_resp = &data->res;
2761 data->arg.dir_fh = NFS_FH(dir);
2762 data->arg.server = server;
2763 data->arg.name = name;
2764 data->arg.attrs = sattr;
2765 data->arg.ftype = ftype;
2766 data->arg.bitmask = server->attr_bitmask;
2767 data->res.server = server;
2768 data->res.fh = &data->fh;
2769 data->res.fattr = &data->fattr;
2770 data->res.dir_fattr = &data->dir_fattr;
2771 nfs_fattr_init(data->res.fattr);
2772 nfs_fattr_init(data->res.dir_fattr);
2773 }
2774 return data;
2775}
2776
2777static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2778{
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002779 int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
2780 &data->arg, &data->res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002781 if (status == 0) {
2782 update_changeattr(dir, &data->res.dir_cinfo);
2783 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2784 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2785 }
2786 return status;
2787}
2788
2789static void nfs4_free_createdata(struct nfs4_createdata *data)
2790{
2791 kfree(data);
2792}
2793
Chuck Lever4f390c12006-08-22 20:06:22 -04002794static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002795 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002797 struct nfs4_createdata *data;
2798 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799
Chuck Lever94a6d752006-08-22 20:06:23 -04002800 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002801 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002802
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002803 status = -ENOMEM;
2804 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2805 if (data == NULL)
2806 goto out;
2807
2808 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2809 data->arg.u.symlink.pages = &page;
2810 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002812 status = nfs4_do_create(dir, dentry, data);
2813
2814 nfs4_free_createdata(data);
2815out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 return status;
2817}
2818
Chuck Lever4f390c12006-08-22 20:06:22 -04002819static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002820 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821{
2822 struct nfs4_exception exception = { };
2823 int err;
2824 do {
2825 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002826 _nfs4_proc_symlink(dir, dentry, page,
2827 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 &exception);
2829 } while (exception.retry);
2830 return err;
2831}
2832
2833static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2834 struct iattr *sattr)
2835{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002836 struct nfs4_createdata *data;
2837 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002839 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2840 if (data == NULL)
2841 goto out;
2842
2843 status = nfs4_do_create(dir, dentry, data);
2844
2845 nfs4_free_createdata(data);
2846out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 return status;
2848}
2849
2850static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2851 struct iattr *sattr)
2852{
2853 struct nfs4_exception exception = { };
2854 int err;
2855 do {
2856 err = nfs4_handle_exception(NFS_SERVER(dir),
2857 _nfs4_proc_mkdir(dir, dentry, sattr),
2858 &exception);
2859 } while (exception.retry);
2860 return err;
2861}
2862
2863static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2864 u64 cookie, struct page *page, unsigned int count, int plus)
2865{
2866 struct inode *dir = dentry->d_inode;
2867 struct nfs4_readdir_arg args = {
2868 .fh = NFS_FH(dir),
2869 .pages = &page,
2870 .pgbase = 0,
2871 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09002872 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 };
2874 struct nfs4_readdir_res res;
2875 struct rpc_message msg = {
2876 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2877 .rpc_argp = &args,
2878 .rpc_resp = &res,
2879 .rpc_cred = cred,
2880 };
2881 int status;
2882
Harvey Harrison3110ff82008-05-02 13:42:44 -07002883 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00002884 dentry->d_parent->d_name.name,
2885 dentry->d_name.name,
2886 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2888 res.pgbase = args.pgbase;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002889 status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 if (status == 0)
2891 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustc4812992007-09-28 17:11:45 -04002892
2893 nfs_invalidate_atime(dir);
2894
Harvey Harrison3110ff82008-05-02 13:42:44 -07002895 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 return status;
2897}
2898
2899static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2900 u64 cookie, struct page *page, unsigned int count, int plus)
2901{
2902 struct nfs4_exception exception = { };
2903 int err;
2904 do {
2905 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2906 _nfs4_proc_readdir(dentry, cred, cookie,
2907 page, count, plus),
2908 &exception);
2909 } while (exception.retry);
2910 return err;
2911}
2912
2913static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2914 struct iattr *sattr, dev_t rdev)
2915{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002916 struct nfs4_createdata *data;
2917 int mode = sattr->ia_mode;
2918 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919
2920 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2921 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002922
2923 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2924 if (data == NULL)
2925 goto out;
2926
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002928 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002930 data->arg.ftype = NF4BLK;
2931 data->arg.u.device.specdata1 = MAJOR(rdev);
2932 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 }
2934 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002935 data->arg.ftype = NF4CHR;
2936 data->arg.u.device.specdata1 = MAJOR(rdev);
2937 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002940 status = nfs4_do_create(dir, dentry, data);
2941
2942 nfs4_free_createdata(data);
2943out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 return status;
2945}
2946
2947static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2948 struct iattr *sattr, dev_t rdev)
2949{
2950 struct nfs4_exception exception = { };
2951 int err;
2952 do {
2953 err = nfs4_handle_exception(NFS_SERVER(dir),
2954 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2955 &exception);
2956 } while (exception.retry);
2957 return err;
2958}
2959
2960static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2961 struct nfs_fsstat *fsstat)
2962{
2963 struct nfs4_statfs_arg args = {
2964 .fh = fhandle,
2965 .bitmask = server->attr_bitmask,
2966 };
Benny Halevy24ad1482009-04-01 09:21:56 -04002967 struct nfs4_statfs_res res = {
2968 .fsstat = fsstat,
2969 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 struct rpc_message msg = {
2971 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2972 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04002973 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 };
2975
Trond Myklebust0e574af2005-10-27 22:12:38 -04002976 nfs_fattr_init(fsstat->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002977 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978}
2979
2980static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2981{
2982 struct nfs4_exception exception = { };
2983 int err;
2984 do {
2985 err = nfs4_handle_exception(server,
2986 _nfs4_proc_statfs(server, fhandle, fsstat),
2987 &exception);
2988 } while (exception.retry);
2989 return err;
2990}
2991
2992static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2993 struct nfs_fsinfo *fsinfo)
2994{
2995 struct nfs4_fsinfo_arg args = {
2996 .fh = fhandle,
2997 .bitmask = server->attr_bitmask,
2998 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04002999 struct nfs4_fsinfo_res res = {
3000 .fsinfo = fsinfo,
3001 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 struct rpc_message msg = {
3003 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3004 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003005 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 };
3007
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003008 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009}
3010
3011static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3012{
3013 struct nfs4_exception exception = { };
3014 int err;
3015
3016 do {
3017 err = nfs4_handle_exception(server,
3018 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3019 &exception);
3020 } while (exception.retry);
3021 return err;
3022}
3023
3024static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3025{
Trond Myklebust0e574af2005-10-27 22:12:38 -04003026 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3028}
3029
3030static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3031 struct nfs_pathconf *pathconf)
3032{
3033 struct nfs4_pathconf_arg args = {
3034 .fh = fhandle,
3035 .bitmask = server->attr_bitmask,
3036 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003037 struct nfs4_pathconf_res res = {
3038 .pathconf = pathconf,
3039 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 struct rpc_message msg = {
3041 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3042 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003043 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 };
3045
3046 /* None of the pathconf attributes are mandatory to implement */
3047 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3048 memset(pathconf, 0, sizeof(*pathconf));
3049 return 0;
3050 }
3051
Trond Myklebust0e574af2005-10-27 22:12:38 -04003052 nfs_fattr_init(pathconf->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003053 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054}
3055
3056static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3057 struct nfs_pathconf *pathconf)
3058{
3059 struct nfs4_exception exception = { };
3060 int err;
3061
3062 do {
3063 err = nfs4_handle_exception(server,
3064 _nfs4_proc_pathconf(server, fhandle, pathconf),
3065 &exception);
3066 } while (exception.retry);
3067 return err;
3068}
3069
Trond Myklebustec06c092006-03-20 13:44:27 -05003070static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071{
Trond Myklebustec06c092006-03-20 13:44:27 -05003072 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073
Andy Adamsonf11c88a2009-04-01 09:22:25 -04003074 dprintk("--> %s\n", __func__);
3075
Andy Adamsonf11c88a2009-04-01 09:22:25 -04003076 nfs4_sequence_done(server, &data->res.seq_res, task->tk_status);
3077
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003078 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003079 nfs_restart_rpc(task, server->nfs_client);
Trond Myklebustec06c092006-03-20 13:44:27 -05003080 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003082
3083 nfs_invalidate_atime(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003085 renew_lease(server, data->timestamp);
3086 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087}
3088
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003089static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 data->timestamp = jiffies;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003092 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093}
3094
Trond Myklebust788e7a82006-03-20 13:44:27 -05003095static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 struct inode *inode = data->inode;
3098
Andy Adamsondef6ed72009-04-01 09:22:26 -04003099 nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
3100 task->tk_status);
3101
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003102 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003103 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003104 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003106 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003108 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003109 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003110 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111}
3112
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003113static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003115 struct nfs_server *server = NFS_SERVER(data->inode);
3116
Trond Myklebusta65318b2009-03-11 14:10:28 -04003117 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003118 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 data->timestamp = jiffies;
3120
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003121 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122}
3123
Trond Myklebust788e7a82006-03-20 13:44:27 -05003124static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 struct inode *inode = data->inode;
3127
Andy Adamson21d9a852009-04-01 09:22:27 -04003128 nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
3129 task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003130 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003131 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003132 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003134 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003135 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136}
3137
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003138static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003140 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141
Trond Myklebusta65318b2009-03-11 14:10:28 -04003142 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003143 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003144 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145}
3146
3147/*
3148 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3149 * standalone procedure for queueing an asynchronous RENEW.
3150 */
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003151static void nfs4_renew_release(void *data)
3152{
3153 struct nfs_client *clp = data;
3154
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003155 if (atomic_read(&clp->cl_count) > 1)
3156 nfs4_schedule_state_renewal(clp);
3157 nfs_put_client(clp);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003158}
3159
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003160static void nfs4_renew_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161{
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003162 struct nfs_client *clp = data;
3163 unsigned long timestamp = task->tk_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164
3165 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003166 /* Unless we're shutting down, schedule state recovery! */
3167 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
3168 nfs4_schedule_state_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 return;
3170 }
3171 spin_lock(&clp->cl_lock);
3172 if (time_before(clp->cl_last_renewal,timestamp))
3173 clp->cl_last_renewal = timestamp;
3174 spin_unlock(&clp->cl_lock);
3175}
3176
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003177static const struct rpc_call_ops nfs4_renew_ops = {
3178 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003179 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003180};
3181
David Howellsadfa6f92006-08-22 20:06:08 -04003182int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183{
3184 struct rpc_message msg = {
3185 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3186 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003187 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 };
3189
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003190 if (!atomic_inc_not_zero(&clp->cl_count))
3191 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003193 &nfs4_renew_ops, clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194}
3195
David Howellsadfa6f92006-08-22 20:06:08 -04003196int nfs4_proc_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 unsigned long now = jiffies;
3204 int status;
3205
3206 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3207 if (status < 0)
3208 return status;
3209 spin_lock(&clp->cl_lock);
3210 if (time_before(clp->cl_last_renewal,now))
3211 clp->cl_last_renewal = now;
3212 spin_unlock(&clp->cl_lock);
3213 return 0;
3214}
3215
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003216static inline int nfs4_server_supports_acls(struct nfs_server *server)
3217{
3218 return (server->caps & NFS_CAP_ACLS)
3219 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3220 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3221}
3222
3223/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3224 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3225 * the stack.
3226 */
3227#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3228
3229static void buf_to_pages(const void *buf, size_t buflen,
3230 struct page **pages, unsigned int *pgbase)
3231{
3232 const void *p = buf;
3233
3234 *pgbase = offset_in_page(buf);
3235 p -= *pgbase;
3236 while (p < buf + buflen) {
3237 *(pages++) = virt_to_page(p);
3238 p += PAGE_CACHE_SIZE;
3239 }
3240}
3241
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003242struct nfs4_cached_acl {
3243 int cached;
3244 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003245 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003246};
3247
3248static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003249{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003250 struct nfs_inode *nfsi = NFS_I(inode);
3251
3252 spin_lock(&inode->i_lock);
3253 kfree(nfsi->nfs4_acl);
3254 nfsi->nfs4_acl = acl;
3255 spin_unlock(&inode->i_lock);
3256}
3257
3258static void nfs4_zap_acl_attr(struct inode *inode)
3259{
3260 nfs4_set_cached_acl(inode, NULL);
3261}
3262
3263static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3264{
3265 struct nfs_inode *nfsi = NFS_I(inode);
3266 struct nfs4_cached_acl *acl;
3267 int ret = -ENOENT;
3268
3269 spin_lock(&inode->i_lock);
3270 acl = nfsi->nfs4_acl;
3271 if (acl == NULL)
3272 goto out;
3273 if (buf == NULL) /* user is just asking for length */
3274 goto out_len;
3275 if (acl->cached == 0)
3276 goto out;
3277 ret = -ERANGE; /* see getxattr(2) man page */
3278 if (acl->len > buflen)
3279 goto out;
3280 memcpy(buf, acl->data, acl->len);
3281out_len:
3282 ret = acl->len;
3283out:
3284 spin_unlock(&inode->i_lock);
3285 return ret;
3286}
3287
3288static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3289{
3290 struct nfs4_cached_acl *acl;
3291
3292 if (buf && acl_len <= PAGE_SIZE) {
3293 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3294 if (acl == NULL)
3295 goto out;
3296 acl->cached = 1;
3297 memcpy(acl->data, buf, acl_len);
3298 } else {
3299 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3300 if (acl == NULL)
3301 goto out;
3302 acl->cached = 0;
3303 }
3304 acl->len = acl_len;
3305out:
3306 nfs4_set_cached_acl(inode, acl);
3307}
3308
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003309static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003310{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003311 struct page *pages[NFS4ACL_MAXPAGES];
3312 struct nfs_getaclargs args = {
3313 .fh = NFS_FH(inode),
3314 .acl_pages = pages,
3315 .acl_len = buflen,
3316 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003317 struct nfs_getaclres res = {
3318 .acl_len = buflen,
3319 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003320 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003321 struct rpc_message msg = {
3322 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3323 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003324 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003325 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003326 struct page *localpage = NULL;
3327 int ret;
3328
3329 if (buflen < PAGE_SIZE) {
3330 /* As long as we're doing a round trip to the server anyway,
3331 * let's be prepared for a page of acl data. */
3332 localpage = alloc_page(GFP_KERNEL);
3333 resp_buf = page_address(localpage);
3334 if (localpage == NULL)
3335 return -ENOMEM;
3336 args.acl_pages[0] = localpage;
3337 args.acl_pgbase = 0;
Benny Halevy663c79b2009-04-01 09:21:59 -04003338 args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003339 } else {
3340 resp_buf = buf;
3341 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3342 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003343 ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003344 if (ret)
3345 goto out_free;
Benny Halevy663c79b2009-04-01 09:21:59 -04003346 if (res.acl_len > args.acl_len)
3347 nfs4_write_cached_acl(inode, NULL, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003348 else
Benny Halevy663c79b2009-04-01 09:21:59 -04003349 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003350 if (buf) {
3351 ret = -ERANGE;
Benny Halevy663c79b2009-04-01 09:21:59 -04003352 if (res.acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003353 goto out_free;
3354 if (localpage)
Benny Halevy663c79b2009-04-01 09:21:59 -04003355 memcpy(buf, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003356 }
Benny Halevy663c79b2009-04-01 09:21:59 -04003357 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003358out_free:
3359 if (localpage)
3360 __free_page(localpage);
3361 return ret;
3362}
3363
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003364static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3365{
3366 struct nfs4_exception exception = { };
3367 ssize_t ret;
3368 do {
3369 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3370 if (ret >= 0)
3371 break;
3372 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3373 } while (exception.retry);
3374 return ret;
3375}
3376
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003377static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3378{
3379 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003380 int ret;
3381
3382 if (!nfs4_server_supports_acls(server))
3383 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003384 ret = nfs_revalidate_inode(server, inode);
3385 if (ret < 0)
3386 return ret;
3387 ret = nfs4_read_cached_acl(inode, buf, buflen);
3388 if (ret != -ENOENT)
3389 return ret;
3390 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003391}
3392
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003393static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003394{
3395 struct nfs_server *server = NFS_SERVER(inode);
3396 struct page *pages[NFS4ACL_MAXPAGES];
3397 struct nfs_setaclargs arg = {
3398 .fh = NFS_FH(inode),
3399 .acl_pages = pages,
3400 .acl_len = buflen,
3401 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003402 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003403 struct rpc_message msg = {
3404 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3405 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003406 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003407 };
3408 int ret;
3409
3410 if (!nfs4_server_supports_acls(server))
3411 return -EOPNOTSUPP;
Trond Myklebust642ac542005-10-18 14:20:19 -07003412 nfs_inode_return_delegation(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003413 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003414 ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003415 nfs_access_zap_cache(inode);
3416 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003417 return ret;
3418}
3419
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003420static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3421{
3422 struct nfs4_exception exception = { };
3423 int err;
3424 do {
3425 err = nfs4_handle_exception(NFS_SERVER(inode),
3426 __nfs4_proc_set_acl(inode, buf, buflen),
3427 &exception);
3428 } while (exception.retry);
3429 return err;
3430}
3431
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432static int
Andy Adamson8328d592009-04-01 09:22:35 -04003433_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 -07003434{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 if (!clp || task->tk_status >= 0)
3436 return 0;
3437 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003438 case -NFS4ERR_ADMIN_REVOKED:
3439 case -NFS4ERR_BAD_STATEID:
3440 case -NFS4ERR_OPENMODE:
3441 if (state == NULL)
3442 break;
3443 nfs4_state_mark_reclaim_nograce(clp, state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003444 goto do_state_recovery;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003446 if (state == NULL)
3447 break;
3448 nfs4_state_mark_reclaim_reboot(clp, state);
3449 case -NFS4ERR_STALE_CLIENTID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 case -NFS4ERR_EXPIRED:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003451 goto do_state_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003452#if defined(CONFIG_NFS_V4_1)
3453 case -NFS4ERR_BADSESSION:
3454 case -NFS4ERR_BADSLOT:
3455 case -NFS4ERR_BAD_HIGH_SLOT:
3456 case -NFS4ERR_DEADSESSION:
3457 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3458 case -NFS4ERR_SEQ_FALSE_RETRY:
3459 case -NFS4ERR_SEQ_MISORDERED:
3460 dprintk("%s ERROR %d, Reset session\n", __func__,
3461 task->tk_status);
Andy Adamson0b9e2d42009-12-04 16:02:14 -05003462 nfs4_schedule_state_recovery(clp);
Andy Adamson4745e312009-04-01 09:22:42 -04003463 task->tk_status = 0;
3464 return -EAGAIN;
3465#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 case -NFS4ERR_DELAY:
Andy Adamson8328d592009-04-01 09:22:35 -04003467 if (server)
3468 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003469 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003470 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3472 task->tk_status = 0;
3473 return -EAGAIN;
3474 case -NFS4ERR_OLD_STATEID:
3475 task->tk_status = 0;
3476 return -EAGAIN;
3477 }
3478 task->tk_status = nfs4_map_errors(task->tk_status);
3479 return 0;
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003480do_state_recovery:
3481 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
3482 nfs4_schedule_state_recovery(clp);
3483 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3484 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3485 task->tk_status = 0;
3486 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487}
3488
Andy Adamson8328d592009-04-01 09:22:35 -04003489static int
3490nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3491{
3492 return _nfs4_async_handle_error(task, server, server->nfs_client, state);
3493}
3494
David Howellsadfa6f92006-08-22 20:06:08 -04003495int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, unsigned short port, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496{
3497 nfs4_verifier sc_verifier;
3498 struct nfs4_setclientid setclientid = {
3499 .sc_verifier = &sc_verifier,
3500 .sc_prog = program,
3501 };
3502 struct rpc_message msg = {
3503 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3504 .rpc_argp = &setclientid,
3505 .rpc_resp = clp,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003506 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 };
Al Virobc4785c2006-10-19 23:28:51 -07003508 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 int loop = 0;
3510 int status;
3511
Al Virobc4785c2006-10-19 23:28:51 -07003512 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3514 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3515
3516 for(;;) {
3517 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003518 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003519 clp->cl_ipaddr,
3520 rpc_peeraddr2str(clp->cl_rpcclient,
3521 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003522 rpc_peeraddr2str(clp->cl_rpcclient,
3523 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003524 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 clp->cl_id_uniquifier);
3526 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003527 sizeof(setclientid.sc_netid),
3528 rpc_peeraddr2str(clp->cl_rpcclient,
3529 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003531 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 clp->cl_ipaddr, port >> 8, port & 255);
3533
3534 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3535 if (status != -NFS4ERR_CLID_INUSE)
3536 break;
3537 if (signalled())
3538 break;
3539 if (loop++ & 1)
3540 ssleep(clp->cl_lease_time + 1);
3541 else
3542 if (++clp->cl_id_uniquifier == 0)
3543 break;
3544 }
3545 return status;
3546}
3547
David Howellsadfa6f92006-08-22 20:06:08 -04003548static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549{
3550 struct nfs_fsinfo fsinfo;
3551 struct rpc_message msg = {
3552 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
3553 .rpc_argp = clp,
3554 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003555 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 };
3557 unsigned long now;
3558 int status;
3559
3560 now = jiffies;
3561 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3562 if (status == 0) {
3563 spin_lock(&clp->cl_lock);
3564 clp->cl_lease_time = fsinfo.lease_time * HZ;
3565 clp->cl_last_renewal = now;
3566 spin_unlock(&clp->cl_lock);
3567 }
3568 return status;
3569}
3570
David Howellsadfa6f92006-08-22 20:06:08 -04003571int nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
Trond Myklebust51581f32006-03-20 13:44:47 -05003572{
Benny Halevy77e03672008-06-24 20:25:57 +03003573 long timeout = 0;
Trond Myklebust51581f32006-03-20 13:44:47 -05003574 int err;
3575 do {
3576 err = _nfs4_proc_setclientid_confirm(clp, cred);
3577 switch (err) {
3578 case 0:
3579 return err;
3580 case -NFS4ERR_RESOURCE:
3581 /* The IBM lawyers misread another document! */
3582 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -05003583 case -EKEYEXPIRED:
Trond Myklebust51581f32006-03-20 13:44:47 -05003584 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3585 }
3586 } while (err == 0);
3587 return err;
3588}
3589
Trond Myklebustfe650402006-01-03 09:55:18 +01003590struct nfs4_delegreturndata {
3591 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003592 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003593 struct nfs_fh fh;
3594 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003595 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003596 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003597 int rpc_status;
3598};
3599
Trond Myklebustfe650402006-01-03 09:55:18 +01003600static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3601{
3602 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003603
Trond Myklebustd61e6122009-12-05 19:32:19 -05003604 nfs4_sequence_done(data->res.server, &data->res.seq_res,
3605 task->tk_status);
Andy Adamson938e1012009-04-01 09:22:28 -04003606
Ricardo Labiaga79708862009-12-07 09:23:21 -05003607 switch (task->tk_status) {
3608 case -NFS4ERR_STALE_STATEID:
3609 case -NFS4ERR_EXPIRED:
3610 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01003611 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003612 break;
3613 default:
3614 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3615 -EAGAIN) {
3616 nfs_restart_rpc(task, data->res.server->nfs_client);
3617 return;
3618 }
3619 }
3620 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01003621}
3622
3623static void nfs4_delegreturn_release(void *calldata)
3624{
Trond Myklebustfe650402006-01-03 09:55:18 +01003625 kfree(calldata);
3626}
3627
Andy Adamson938e1012009-04-01 09:22:28 -04003628#if defined(CONFIG_NFS_V4_1)
3629static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3630{
3631 struct nfs4_delegreturndata *d_data;
3632
3633 d_data = (struct nfs4_delegreturndata *)data;
3634
3635 if (nfs4_setup_sequence(d_data->res.server->nfs_client,
3636 &d_data->args.seq_args,
3637 &d_data->res.seq_res, 1, task))
3638 return;
3639 rpc_call_start(task);
3640}
3641#endif /* CONFIG_NFS_V4_1 */
3642
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003643static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04003644#if defined(CONFIG_NFS_V4_1)
3645 .rpc_call_prepare = nfs4_delegreturn_prepare,
3646#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01003647 .rpc_call_done = nfs4_delegreturn_done,
3648 .rpc_release = nfs4_delegreturn_release,
3649};
3650
Trond Myklebuste6f81072008-01-24 18:14:34 -05003651static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003652{
3653 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003654 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003655 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003656 struct rpc_message msg = {
3657 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3658 .rpc_cred = cred,
3659 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003660 struct rpc_task_setup task_setup_data = {
3661 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003662 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003663 .callback_ops = &nfs4_delegreturn_ops,
3664 .flags = RPC_TASK_ASYNC,
3665 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003666 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003667
Andy Adamson938e1012009-04-01 09:22:28 -04003668 data = kzalloc(sizeof(*data), GFP_KERNEL);
Trond Myklebustfe650402006-01-03 09:55:18 +01003669 if (data == NULL)
3670 return -ENOMEM;
3671 data->args.fhandle = &data->fh;
3672 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003673 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003674 nfs_copy_fh(&data->fh, NFS_FH(inode));
3675 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003676 data->res.fattr = &data->fattr;
3677 data->res.server = server;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003678 data->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003679 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003680 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003681 data->rpc_status = 0;
3682
Trond Myklebustc970aa82007-07-14 15:39:59 -04003683 task_setup_data.callback_data = data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003684 msg.rpc_argp = &data->args,
3685 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003686 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003687 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003688 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003689 if (!issync)
3690 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003691 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003692 if (status != 0)
3693 goto out;
3694 status = data->rpc_status;
3695 if (status != 0)
3696 goto out;
3697 nfs_refresh_inode(inode, &data->fattr);
3698out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003699 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003700 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701}
3702
Trond Myklebuste6f81072008-01-24 18:14:34 -05003703int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704{
3705 struct nfs_server *server = NFS_SERVER(inode);
3706 struct nfs4_exception exception = { };
3707 int err;
3708 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003709 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710 switch (err) {
3711 case -NFS4ERR_STALE_STATEID:
3712 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 case 0:
3714 return 0;
3715 }
3716 err = nfs4_handle_exception(server, err, &exception);
3717 } while (exception.retry);
3718 return err;
3719}
3720
3721#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3722#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3723
3724/*
3725 * sleep, with exponential backoff, and retry the LOCK operation.
3726 */
3727static unsigned long
3728nfs4_set_lock_task_retry(unsigned long timeout)
3729{
Matthew Wilcox150030b2007-12-06 16:24:39 -05003730 schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731 timeout <<= 1;
3732 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3733 return NFS4_LOCK_MAXTIMEOUT;
3734 return timeout;
3735}
3736
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3738{
3739 struct inode *inode = state->inode;
3740 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003741 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003742 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003744 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003746 struct nfs_lockt_res res = {
3747 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748 };
3749 struct rpc_message msg = {
3750 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3751 .rpc_argp = &arg,
3752 .rpc_resp = &res,
3753 .rpc_cred = state->owner->so_cred,
3754 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755 struct nfs4_lock_state *lsp;
3756 int status;
3757
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003758 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003759 status = nfs4_set_lock_state(state, request);
3760 if (status != 0)
3761 goto out;
3762 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003763 arg.lock_owner.id = lsp->ls_id.id;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003764 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003765 switch (status) {
3766 case 0:
3767 request->fl_type = F_UNLCK;
3768 break;
3769 case -NFS4ERR_DENIED:
3770 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05003772 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003773out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774 return status;
3775}
3776
3777static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3778{
3779 struct nfs4_exception exception = { };
3780 int err;
3781
3782 do {
3783 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3784 _nfs4_proc_getlk(state, cmd, request),
3785 &exception);
3786 } while (exception.retry);
3787 return err;
3788}
3789
3790static int do_vfs_lock(struct file *file, struct file_lock *fl)
3791{
3792 int res = 0;
3793 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3794 case FL_POSIX:
3795 res = posix_lock_file_wait(file, fl);
3796 break;
3797 case FL_FLOCK:
3798 res = flock_lock_file_wait(file, fl);
3799 break;
3800 default:
3801 BUG();
3802 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 return res;
3804}
3805
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003806struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003807 struct nfs_locku_args arg;
3808 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003809 struct nfs4_lock_state *lsp;
3810 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003811 struct file_lock fl;
3812 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003813 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003814};
3815
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003816static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3817 struct nfs_open_context *ctx,
3818 struct nfs4_lock_state *lsp,
3819 struct nfs_seqid *seqid)
3820{
3821 struct nfs4_unlockdata *p;
3822 struct inode *inode = lsp->ls_state->inode;
3823
Andy Adamsona8936932009-04-01 09:22:23 -04003824 p = kzalloc(sizeof(*p), GFP_KERNEL);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003825 if (p == NULL)
3826 return NULL;
3827 p->arg.fh = NFS_FH(inode);
3828 p->arg.fl = &p->fl;
3829 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003830 p->res.seqid = seqid;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003831 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003832 p->arg.stateid = &lsp->ls_stateid;
3833 p->lsp = lsp;
3834 atomic_inc(&lsp->ls_count);
3835 /* Ensure we don't close file until we're done freeing locks! */
3836 p->ctx = get_nfs_open_context(ctx);
3837 memcpy(&p->fl, fl, sizeof(p->fl));
3838 p->server = NFS_SERVER(inode);
3839 return p;
3840}
3841
Trond Myklebust06f814a2006-01-03 09:55:07 +01003842static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003843{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003844 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003845 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003846 nfs4_put_lock_state(calldata->lsp);
3847 put_nfs_open_context(calldata->ctx);
3848 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003849}
3850
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003851static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003852{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003853 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003854
Andy Adamsona8936932009-04-01 09:22:23 -04003855 nfs4_sequence_done(calldata->server, &calldata->res.seq_res,
3856 task->tk_status);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003857 if (RPC_ASSASSINATED(task))
3858 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003859 switch (task->tk_status) {
3860 case 0:
3861 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003862 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003863 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01003864 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003865 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003866 case -NFS4ERR_BAD_STATEID:
3867 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003868 case -NFS4ERR_STALE_STATEID:
3869 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003870 break;
3871 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003872 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebust0110ee12009-12-07 09:00:24 -05003873 nfs_restart_rpc(task,
Trond Myklebustd61e6122009-12-05 19:32:19 -05003874 calldata->server->nfs_client);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003875 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003876}
3877
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003878static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003879{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003880 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003882 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003883 return;
3884 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003885 /* Note: exit _without_ running nfs4_locku_done */
3886 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003887 return;
3888 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01003889 calldata->timestamp = jiffies;
Andy Adamsona8936932009-04-01 09:22:23 -04003890 if (nfs4_setup_sequence(calldata->server->nfs_client,
3891 &calldata->arg.seq_args,
3892 &calldata->res.seq_res, 1, task))
3893 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003894 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895}
3896
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003897static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003898 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003899 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01003900 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003901};
3902
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003903static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3904 struct nfs_open_context *ctx,
3905 struct nfs4_lock_state *lsp,
3906 struct nfs_seqid *seqid)
3907{
3908 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003909 struct rpc_message msg = {
3910 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3911 .rpc_cred = ctx->cred,
3912 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003913 struct rpc_task_setup task_setup_data = {
3914 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04003915 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003916 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003917 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003918 .flags = RPC_TASK_ASYNC,
3919 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003920
Frank Filz137d6ac2007-07-09 15:32:29 -07003921 /* Ensure this is an unlock - when canceling a lock, the
3922 * canceled lock is passed in, and it won't be an unlock.
3923 */
3924 fl->fl_type = F_UNLCK;
3925
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003926 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3927 if (data == NULL) {
3928 nfs_free_seqid(seqid);
3929 return ERR_PTR(-ENOMEM);
3930 }
3931
Trond Myklebust5138fde2007-07-14 15:40:01 -04003932 msg.rpc_argp = &data->arg,
3933 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003934 task_setup_data.callback_data = data;
3935 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003936}
3937
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3939{
Trond Myklebust19e03c52008-12-23 15:21:44 -05003940 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003941 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003942 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01003943 struct rpc_task *task;
3944 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003945 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946
Trond Myklebust9b073572006-06-29 16:38:34 -04003947 status = nfs4_set_lock_state(state, request);
3948 /* Unlock _before_ we do the RPC call */
3949 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003950 down_read(&nfsi->rwsem);
3951 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
3952 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003953 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003954 }
3955 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003956 if (status != 0)
3957 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003958 /* Is this a delegated lock? */
3959 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04003960 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003961 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003962 seqid = nfs_alloc_seqid(&lsp->ls_seqid);
Trond Myklebust9b073572006-06-29 16:38:34 -04003963 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003964 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04003965 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04003966 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003967 status = PTR_ERR(task);
3968 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04003969 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003970 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003971 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04003972out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003973 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003974 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975}
3976
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003977struct nfs4_lockdata {
3978 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003979 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003980 struct nfs4_lock_state *lsp;
3981 struct nfs_open_context *ctx;
3982 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003983 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003984 int rpc_status;
3985 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04003986 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003987};
3988
3989static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
3990 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp)
3991{
3992 struct nfs4_lockdata *p;
3993 struct inode *inode = lsp->ls_state->inode;
3994 struct nfs_server *server = NFS_SERVER(inode);
3995
3996 p = kzalloc(sizeof(*p), GFP_KERNEL);
3997 if (p == NULL)
3998 return NULL;
3999
4000 p->arg.fh = NFS_FH(inode);
4001 p->arg.fl = &p->fl;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004002 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid);
4003 if (p->arg.open_seqid == NULL)
4004 goto out_free;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004005 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
4006 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004007 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004008 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004009 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04004010 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004011 p->res.lock_seqid = p->arg.lock_seqid;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04004012 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004013 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004014 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004015 atomic_inc(&lsp->ls_count);
4016 p->ctx = get_nfs_open_context(ctx);
4017 memcpy(&p->fl, fl, sizeof(p->fl));
4018 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004019out_free_seqid:
4020 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004021out_free:
4022 kfree(p);
4023 return NULL;
4024}
4025
4026static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4027{
4028 struct nfs4_lockdata *data = calldata;
4029 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030
Harvey Harrison3110ff82008-05-02 13:42:44 -07004031 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004032 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4033 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004034 /* Do we need to do an open_to_lock_owner? */
4035 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004036 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4037 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004038 data->arg.open_stateid = &state->stateid;
4039 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004040 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004041 } else
4042 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004043 data->timestamp = jiffies;
Andy Adamson66179ef2009-04-01 09:22:22 -04004044 if (nfs4_setup_sequence(data->server->nfs_client, &data->arg.seq_args,
4045 &data->res.seq_res, 1, task))
4046 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004047 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004048 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004049}
4050
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004051static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4052{
4053 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4054 nfs4_lock_prepare(task, calldata);
4055}
4056
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004057static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4058{
4059 struct nfs4_lockdata *data = calldata;
4060
Harvey Harrison3110ff82008-05-02 13:42:44 -07004061 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004062
Trond Myklebustd61e6122009-12-05 19:32:19 -05004063 nfs4_sequence_done(data->server, &data->res.seq_res,
4064 task->tk_status);
Andy Adamson66179ef2009-04-01 09:22:22 -04004065
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004066 data->rpc_status = task->tk_status;
4067 if (RPC_ASSASSINATED(task))
4068 goto out;
4069 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004070 if (data->rpc_status == 0)
4071 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4072 else
4073 goto out;
4074 }
4075 if (data->rpc_status == 0) {
4076 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4077 sizeof(data->lsp->ls_stateid.data));
4078 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Trond Myklebust88be9f92007-06-05 10:42:27 -04004079 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004080 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004081out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004082 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004083}
4084
4085static void nfs4_lock_release(void *calldata)
4086{
4087 struct nfs4_lockdata *data = calldata;
4088
Harvey Harrison3110ff82008-05-02 13:42:44 -07004089 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004090 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004091 if (data->cancelled != 0) {
4092 struct rpc_task *task;
4093 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4094 data->arg.lock_seqid);
4095 if (!IS_ERR(task))
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004096 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004097 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004098 } else
4099 nfs_free_seqid(data->arg.lock_seqid);
4100 nfs4_put_lock_state(data->lsp);
4101 put_nfs_open_context(data->ctx);
4102 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004103 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004104}
4105
4106static const struct rpc_call_ops nfs4_lock_ops = {
4107 .rpc_call_prepare = nfs4_lock_prepare,
4108 .rpc_call_done = nfs4_lock_done,
4109 .rpc_release = nfs4_lock_release,
4110};
4111
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004112static const struct rpc_call_ops nfs4_recover_lock_ops = {
4113 .rpc_call_prepare = nfs4_recover_lock_prepare,
4114 .rpc_call_done = nfs4_lock_done,
4115 .rpc_release = nfs4_lock_release,
4116};
4117
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004118static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4119{
4120 struct nfs_client *clp = server->nfs_client;
4121 struct nfs4_state *state = lsp->ls_state;
4122
4123 switch (error) {
4124 case -NFS4ERR_ADMIN_REVOKED:
4125 case -NFS4ERR_BAD_STATEID:
4126 case -NFS4ERR_EXPIRED:
4127 if (new_lock_owner != 0 ||
4128 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4129 nfs4_state_mark_reclaim_nograce(clp, state);
4130 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004131 break;
4132 case -NFS4ERR_STALE_STATEID:
4133 if (new_lock_owner != 0 ||
4134 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4135 nfs4_state_mark_reclaim_reboot(clp, state);
4136 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004137 };
4138}
4139
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004140static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004141{
4142 struct nfs4_lockdata *data;
4143 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004144 struct rpc_message msg = {
4145 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4146 .rpc_cred = state->owner->so_cred,
4147 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004148 struct rpc_task_setup task_setup_data = {
4149 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004150 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004151 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004152 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004153 .flags = RPC_TASK_ASYNC,
4154 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004155 int ret;
4156
Harvey Harrison3110ff82008-05-02 13:42:44 -07004157 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004158 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004159 fl->fl_u.nfs4_fl.owner);
4160 if (data == NULL)
4161 return -ENOMEM;
4162 if (IS_SETLKW(cmd))
4163 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004164 if (recovery_type > NFS_LOCK_NEW) {
4165 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004166 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004167 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4168 }
Trond Myklebust5138fde2007-07-14 15:40:01 -04004169 msg.rpc_argp = &data->arg,
4170 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004171 task_setup_data.callback_data = data;
4172 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004173 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004174 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004175 ret = nfs4_wait_for_completion_rpc_task(task);
4176 if (ret == 0) {
4177 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004178 if (ret)
4179 nfs4_handle_setlk_error(data->server, data->lsp,
4180 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004181 } else
4182 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004183 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004184 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004185 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186}
4187
4188static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4189{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004190 struct nfs_server *server = NFS_SERVER(state->inode);
4191 struct nfs4_exception exception = { };
4192 int err;
4193
4194 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004195 /* Cache the lock if possible... */
4196 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4197 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004198 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Jeff Layton2c643482010-01-07 09:42:03 -05004199 if (err != -NFS4ERR_DELAY && err != -EKEYEXPIRED)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004200 break;
4201 nfs4_handle_exception(server, err, &exception);
4202 } while (exception.retry);
4203 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204}
4205
4206static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4207{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004208 struct nfs_server *server = NFS_SERVER(state->inode);
4209 struct nfs4_exception exception = { };
4210 int err;
4211
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004212 err = nfs4_set_lock_state(state, request);
4213 if (err != 0)
4214 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004215 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004216 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4217 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004218 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004219 switch (err) {
4220 default:
4221 goto out;
4222 case -NFS4ERR_GRACE:
4223 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -05004224 case -EKEYEXPIRED:
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004225 nfs4_handle_exception(server, err, &exception);
4226 err = 0;
4227 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004228 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004229out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004230 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231}
4232
4233static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4234{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004235 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004236 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004237 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004239 if ((fl_flags & FL_POSIX) &&
4240 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4241 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004242 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004243 status = nfs4_set_lock_state(state, request);
4244 if (status != 0)
4245 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004246 request->fl_flags |= FL_ACCESS;
4247 status = do_vfs_lock(request->fl_file, request);
4248 if (status < 0)
4249 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004250 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004251 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004252 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004253 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004254 request->fl_flags = fl_flags & ~FL_SLEEP;
4255 status = do_vfs_lock(request->fl_file, request);
4256 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004257 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004258 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004259 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004260 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004261 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004262 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004263 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07004264 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004265out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004266 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004267out:
4268 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269 return status;
4270}
4271
4272static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4273{
4274 struct nfs4_exception exception = { };
4275 int err;
4276
4277 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004278 err = _nfs4_proc_setlk(state, cmd, request);
4279 if (err == -NFS4ERR_DENIED)
4280 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004282 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 } while (exception.retry);
4284 return err;
4285}
4286
4287static int
4288nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4289{
4290 struct nfs_open_context *ctx;
4291 struct nfs4_state *state;
4292 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4293 int status;
4294
4295 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004296 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 state = ctx->state;
4298
4299 if (request->fl_start < 0 || request->fl_end < 0)
4300 return -EINVAL;
4301
Trond Myklebustd9531262009-07-21 19:22:38 -04004302 if (IS_GETLK(cmd)) {
4303 if (state != NULL)
4304 return nfs4_proc_getlk(state, F_GETLK, request);
4305 return 0;
4306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307
4308 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4309 return -EINVAL;
4310
Trond Myklebustd9531262009-07-21 19:22:38 -04004311 if (request->fl_type == F_UNLCK) {
4312 if (state != NULL)
4313 return nfs4_proc_unlck(state, cmd, request);
4314 return 0;
4315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316
Trond Myklebustd9531262009-07-21 19:22:38 -04004317 if (state == NULL)
4318 return -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319 do {
4320 status = nfs4_proc_setlk(state, cmd, request);
4321 if ((status != -EAGAIN) || IS_SETLK(cmd))
4322 break;
4323 timeout = nfs4_set_lock_task_retry(timeout);
4324 status = -ERESTARTSYS;
4325 if (signalled())
4326 break;
4327 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328 return status;
4329}
4330
Trond Myklebust888e6942005-11-04 15:38:11 -05004331int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4332{
4333 struct nfs_server *server = NFS_SERVER(state->inode);
4334 struct nfs4_exception exception = { };
4335 int err;
4336
4337 err = nfs4_set_lock_state(state, fl);
4338 if (err != 0)
4339 goto out;
4340 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004341 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004342 switch (err) {
4343 default:
4344 printk(KERN_ERR "%s: unhandled error %d.\n",
4345 __func__, err);
4346 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004347 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004348 goto out;
4349 case -NFS4ERR_EXPIRED:
4350 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004351 case -NFS4ERR_STALE_STATEID:
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004352 case -NFS4ERR_BADSESSION:
4353 case -NFS4ERR_BADSLOT:
4354 case -NFS4ERR_BAD_HIGH_SLOT:
4355 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4356 case -NFS4ERR_DEADSESSION:
Trond Myklebustd5122202009-06-17 13:22:58 -07004357 nfs4_schedule_state_recovery(server->nfs_client);
4358 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004359 case -ERESTARTSYS:
4360 /*
4361 * The show must go on: exit, but mark the
4362 * stateid as needing recovery.
4363 */
4364 case -NFS4ERR_ADMIN_REVOKED:
4365 case -NFS4ERR_BAD_STATEID:
4366 case -NFS4ERR_OPENMODE:
4367 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
4368 err = 0;
4369 goto out;
4370 case -ENOMEM:
4371 case -NFS4ERR_DENIED:
4372 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4373 err = 0;
4374 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004375 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -05004376 case -EKEYEXPIRED:
Trond Myklebustd5122202009-06-17 13:22:58 -07004377 break;
4378 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004379 err = nfs4_handle_exception(server, err, &exception);
4380 } while (exception.retry);
4381out:
4382 return err;
4383}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004384
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004385#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4386
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004387int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
4388 size_t buflen, int flags)
4389{
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004390 struct inode *inode = dentry->d_inode;
4391
4392 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4393 return -EOPNOTSUPP;
4394
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004395 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004396}
4397
4398/* The getxattr man page suggests returning -ENODATA for unknown attributes,
4399 * and that's what we'll do for e.g. user attributes that haven't been set.
4400 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
4401 * attributes in kernel-managed attribute namespaces. */
4402ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
4403 size_t buflen)
4404{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004405 struct inode *inode = dentry->d_inode;
4406
4407 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4408 return -EOPNOTSUPP;
4409
4410 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004411}
4412
4413ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
4414{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004415 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004416
J. Bruce Fields096455a2006-03-20 23:23:42 -05004417 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4418 return 0;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004419 if (buf && buflen < len)
4420 return -ERANGE;
4421 if (buf)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004422 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
4423 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004424}
4425
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004426static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4427{
4428 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
4429 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4430 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4431 return;
4432
4433 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4434 NFS_ATTR_FATTR_NLINK;
4435 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4436 fattr->nlink = 2;
4437}
4438
Trond Myklebust56659e92007-07-17 21:52:39 -04004439int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004440 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004441{
4442 struct nfs_server *server = NFS_SERVER(dir);
4443 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004444 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4445 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004446 };
4447 struct nfs4_fs_locations_arg args = {
4448 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004449 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004450 .page = page,
4451 .bitmask = bitmask,
4452 };
Benny Halevy22958462009-04-01 09:22:02 -04004453 struct nfs4_fs_locations_res res = {
4454 .fs_locations = fs_locations,
4455 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004456 struct rpc_message msg = {
4457 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4458 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004459 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004460 };
4461 int status;
4462
Harvey Harrison3110ff82008-05-02 13:42:44 -07004463 dprintk("%s: start\n", __func__);
Trond Myklebustc228fd32007-01-13 02:28:11 -05004464 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004465 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004466 fs_locations->nlocations = 0;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04004467 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004468 nfs_fixup_referral_attributes(&fs_locations->fattr);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004469 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004470 return status;
4471}
4472
Andy Adamson557134a2009-04-01 09:21:53 -04004473#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04004474/*
4475 * nfs4_proc_exchange_id()
4476 *
4477 * Since the clientid has expired, all compounds using sessions
4478 * associated with the stale clientid will be returning
4479 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4480 * be in some phase of session reset.
4481 */
Andy Adamson4d643d12009-12-04 15:52:24 -05004482int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004483{
4484 nfs4_verifier verifier;
4485 struct nfs41_exchange_id_args args = {
4486 .client = clp,
4487 .flags = clp->cl_exchange_flags,
4488 };
4489 struct nfs41_exchange_id_res res = {
4490 .client = clp,
4491 };
4492 int status;
4493 struct rpc_message msg = {
4494 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4495 .rpc_argp = &args,
4496 .rpc_resp = &res,
4497 .rpc_cred = cred,
4498 };
4499 __be32 *p;
4500
4501 dprintk("--> %s\n", __func__);
4502 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04004503
Alexandros Batsakis7b183d02009-12-05 13:33:25 -05004504 /* Remove server-only flags */
4505 args.flags &= ~EXCHGID4_FLAG_CONFIRMED_R;
4506
Benny Halevy99fe60d2009-04-01 09:22:29 -04004507 p = (u32 *)verifier.data;
4508 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4509 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4510 args.verifier = &verifier;
4511
4512 while (1) {
4513 args.id_len = scnprintf(args.id, sizeof(args.id),
4514 "%s/%s %u",
4515 clp->cl_ipaddr,
4516 rpc_peeraddr2str(clp->cl_rpcclient,
4517 RPC_DISPLAY_ADDR),
4518 clp->cl_id_uniquifier);
4519
4520 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
4521
Andy Adamson180b62a2010-03-02 13:19:36 -05004522 if (status != -NFS4ERR_CLID_INUSE)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004523 break;
4524
4525 if (signalled())
4526 break;
4527
4528 if (++clp->cl_id_uniquifier == 0)
4529 break;
4530 }
4531
4532 dprintk("<-- %s status= %d\n", __func__, status);
4533 return status;
4534}
4535
Andy Adamson2050f0c2009-04-01 09:22:30 -04004536struct nfs4_get_lease_time_data {
4537 struct nfs4_get_lease_time_args *args;
4538 struct nfs4_get_lease_time_res *res;
4539 struct nfs_client *clp;
4540};
4541
4542static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4543 void *calldata)
4544{
4545 int ret;
4546 struct nfs4_get_lease_time_data *data =
4547 (struct nfs4_get_lease_time_data *)calldata;
4548
4549 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004550 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004551 /* just setup sequence, do not trigger session recovery
4552 since we're invoked within one */
4553 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004554 &data->args->la_seq_args,
4555 &data->res->lr_seq_res, 0, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004556
4557 BUG_ON(ret == -EAGAIN);
4558 rpc_call_start(task);
4559 dprintk("<-- %s\n", __func__);
4560}
4561
4562/*
4563 * Called from nfs4_state_manager thread for session setup, so don't recover
4564 * from sequence operation or clientid errors.
4565 */
4566static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4567{
4568 struct nfs4_get_lease_time_data *data =
4569 (struct nfs4_get_lease_time_data *)calldata;
4570
4571 dprintk("--> %s\n", __func__);
4572 nfs41_sequence_done(data->clp, &data->res->lr_seq_res, task->tk_status);
4573 switch (task->tk_status) {
4574 case -NFS4ERR_DELAY:
4575 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05004576 case -EKEYEXPIRED:
Andy Adamson2050f0c2009-04-01 09:22:30 -04004577 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4578 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4579 task->tk_status = 0;
Trond Myklebust0110ee12009-12-07 09:00:24 -05004580 nfs_restart_rpc(task, data->clp);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004581 return;
4582 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04004583 dprintk("<-- %s\n", __func__);
4584}
4585
4586struct rpc_call_ops nfs4_get_lease_time_ops = {
4587 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4588 .rpc_call_done = nfs4_get_lease_time_done,
4589};
4590
4591int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4592{
4593 struct rpc_task *task;
4594 struct nfs4_get_lease_time_args args;
4595 struct nfs4_get_lease_time_res res = {
4596 .lr_fsinfo = fsinfo,
4597 };
4598 struct nfs4_get_lease_time_data data = {
4599 .args = &args,
4600 .res = &res,
4601 .clp = clp,
4602 };
4603 struct rpc_message msg = {
4604 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4605 .rpc_argp = &args,
4606 .rpc_resp = &res,
4607 };
4608 struct rpc_task_setup task_setup = {
4609 .rpc_client = clp->cl_rpcclient,
4610 .rpc_message = &msg,
4611 .callback_ops = &nfs4_get_lease_time_ops,
4612 .callback_data = &data
4613 };
4614 int status;
4615
4616 res.lr_seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
4617 dprintk("--> %s\n", __func__);
4618 task = rpc_run_task(&task_setup);
4619
4620 if (IS_ERR(task))
4621 status = PTR_ERR(task);
4622 else {
4623 status = task->tk_status;
4624 rpc_put_task(task);
4625 }
4626 dprintk("<-- %s return %d\n", __func__, status);
4627
4628 return status;
4629}
4630
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004631/*
4632 * Reset a slot table
4633 */
Andy Adamson104aeba2010-01-14 17:45:10 -05004634static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
4635 int ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004636{
Andy Adamson104aeba2010-01-14 17:45:10 -05004637 struct nfs4_slot *new = NULL;
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004638 int i;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004639 int ret = 0;
4640
Andy Adamson104aeba2010-01-14 17:45:10 -05004641 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
4642 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004643
Andy Adamson104aeba2010-01-14 17:45:10 -05004644 /* Does the newly negotiated max_reqs match the existing slot table? */
4645 if (max_reqs != tbl->max_slots) {
4646 ret = -ENOMEM;
4647 new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
4648 GFP_KERNEL);
4649 if (!new)
4650 goto out;
4651 ret = 0;
4652 kfree(tbl->slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004653 }
4654 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson104aeba2010-01-14 17:45:10 -05004655 if (new) {
4656 tbl->slots = new;
4657 tbl->max_slots = max_reqs;
4658 }
4659 for (i = 0; i < tbl->max_slots; ++i)
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004660 tbl->slots[i].seq_nr = ivalue;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004661 spin_unlock(&tbl->slot_tbl_lock);
4662 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4663 tbl, tbl->slots, tbl->max_slots);
4664out:
4665 dprintk("<-- %s: return %d\n", __func__, ret);
4666 return ret;
4667}
4668
Andy Adamsonfc931582009-04-01 09:22:31 -04004669/*
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004670 * Reset the forechannel and backchannel slot tables
4671 */
4672static int nfs4_reset_slot_tables(struct nfs4_session *session)
4673{
4674 int status;
4675
4676 status = nfs4_reset_slot_table(&session->fc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05004677 session->fc_attrs.max_reqs, 1);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004678 if (status)
4679 return status;
4680
4681 status = nfs4_reset_slot_table(&session->bc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05004682 session->bc_attrs.max_reqs, 0);
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004683 return status;
4684}
4685
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004686/* Destroy the slot table */
4687static void nfs4_destroy_slot_tables(struct nfs4_session *session)
4688{
4689 if (session->fc_slot_table.slots != NULL) {
4690 kfree(session->fc_slot_table.slots);
4691 session->fc_slot_table.slots = NULL;
4692 }
4693 if (session->bc_slot_table.slots != NULL) {
4694 kfree(session->bc_slot_table.slots);
4695 session->bc_slot_table.slots = NULL;
4696 }
4697 return;
4698}
4699
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004700/*
Andy Adamsonfc931582009-04-01 09:22:31 -04004701 * Initialize slot table
4702 */
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004703static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
4704 int max_slots, int ivalue)
Andy Adamsonfc931582009-04-01 09:22:31 -04004705{
Andy Adamsonfc931582009-04-01 09:22:31 -04004706 struct nfs4_slot *slot;
4707 int ret = -ENOMEM;
4708
4709 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
4710
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004711 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
Andy Adamsonfc931582009-04-01 09:22:31 -04004712
4713 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_KERNEL);
4714 if (!slot)
4715 goto out;
Andy Adamsonfc931582009-04-01 09:22:31 -04004716 ret = 0;
4717
4718 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonfc931582009-04-01 09:22:31 -04004719 tbl->max_slots = max_slots;
4720 tbl->slots = slot;
4721 tbl->highest_used_slotid = -1; /* no slot is currently used */
4722 spin_unlock(&tbl->slot_tbl_lock);
4723 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4724 tbl, tbl->slots, tbl->max_slots);
4725out:
4726 dprintk("<-- %s: return %d\n", __func__, ret);
4727 return ret;
Andy Adamsonfc931582009-04-01 09:22:31 -04004728}
4729
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004730/*
4731 * Initialize the forechannel and backchannel tables
4732 */
4733static int nfs4_init_slot_tables(struct nfs4_session *session)
4734{
Trond Myklebustf26468f2009-12-05 19:32:11 -05004735 struct nfs4_slot_table *tbl;
4736 int status = 0;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004737
Trond Myklebustf26468f2009-12-05 19:32:11 -05004738 tbl = &session->fc_slot_table;
4739 if (tbl->slots == NULL) {
4740 status = nfs4_init_slot_table(tbl,
4741 session->fc_attrs.max_reqs, 1);
4742 if (status)
4743 return status;
4744 }
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004745
Trond Myklebustf26468f2009-12-05 19:32:11 -05004746 tbl = &session->bc_slot_table;
4747 if (tbl->slots == NULL) {
4748 status = nfs4_init_slot_table(tbl,
4749 session->bc_attrs.max_reqs, 0);
4750 if (status)
4751 nfs4_destroy_slot_tables(session);
4752 }
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004753
4754 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04004755}
4756
4757struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
4758{
4759 struct nfs4_session *session;
4760 struct nfs4_slot_table *tbl;
4761
4762 session = kzalloc(sizeof(struct nfs4_session), GFP_KERNEL);
4763 if (!session)
4764 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04004765
Andy Adamson76db6d92009-04-01 09:22:38 -04004766 /*
4767 * The create session reply races with the server back
4768 * channel probe. Mark the client NFS_CS_SESSION_INITING
4769 * so that the client back channel can find the
4770 * nfs_client struct
4771 */
4772 clp->cl_cons_state = NFS_CS_SESSION_INITING;
Andy Adamsonea028ac2009-12-04 15:55:38 -05004773 init_completion(&session->complete);
Andy Adamson76db6d92009-04-01 09:22:38 -04004774
Andy Adamson557134a2009-04-01 09:21:53 -04004775 tbl = &session->fc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05004776 tbl->highest_used_slotid = -1;
Andy Adamson557134a2009-04-01 09:21:53 -04004777 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004778 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004779
4780 tbl = &session->bc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05004781 tbl->highest_used_slotid = -1;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004782 spin_lock_init(&tbl->slot_tbl_lock);
4783 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
4784
Andy Adamson557134a2009-04-01 09:21:53 -04004785 session->clp = clp;
4786 return session;
4787}
4788
4789void nfs4_destroy_session(struct nfs4_session *session)
4790{
Andy Adamson5a0ffe52009-04-01 09:23:18 -04004791 nfs4_proc_destroy_session(session);
4792 dprintk("%s Destroy backchannel for xprt %p\n",
4793 __func__, session->clp->cl_rpcclient->cl_xprt);
4794 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
4795 NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004796 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04004797 kfree(session);
4798}
4799
Andy Adamsonfc931582009-04-01 09:22:31 -04004800/*
4801 * Initialize the values to be used by the client in CREATE_SESSION
4802 * If nfs4_init_session set the fore channel request and response sizes,
4803 * use them.
4804 *
4805 * Set the back channel max_resp_sz_cached to zero to force the client to
4806 * always set csa_cachethis to FALSE because the current implementation
4807 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
4808 */
4809static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
4810{
4811 struct nfs4_session *session = args->client->cl_session;
4812 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
4813 mxresp_sz = session->fc_attrs.max_resp_sz;
4814
4815 if (mxrqst_sz == 0)
4816 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
4817 if (mxresp_sz == 0)
4818 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
4819 /* Fore channel attributes */
4820 args->fc_attrs.headerpadsz = 0;
4821 args->fc_attrs.max_rqst_sz = mxrqst_sz;
4822 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04004823 args->fc_attrs.max_ops = NFS4_MAX_OPS;
4824 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
4825
4826 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05004827 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04004828 __func__,
4829 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05004830 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04004831
4832 /* Back channel attributes */
4833 args->bc_attrs.headerpadsz = 0;
4834 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
4835 args->bc_attrs.max_resp_sz = PAGE_SIZE;
4836 args->bc_attrs.max_resp_sz_cached = 0;
4837 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
4838 args->bc_attrs.max_reqs = 1;
4839
4840 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
4841 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4842 __func__,
4843 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
4844 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
4845 args->bc_attrs.max_reqs);
4846}
4847
Andy Adamson8d353012009-04-01 09:22:32 -04004848static int _verify_channel_attr(char *chan, char *attr_name, u32 sent, u32 rcvd)
4849{
4850 if (rcvd <= sent)
4851 return 0;
4852 printk(KERN_WARNING "%s: Session INVALID: %s channel %s increased. "
4853 "sent=%u rcvd=%u\n", __func__, chan, attr_name, sent, rcvd);
4854 return -EINVAL;
4855}
4856
4857#define _verify_fore_channel_attr(_name_) \
4858 _verify_channel_attr("fore", #_name_, \
4859 args->fc_attrs._name_, \
4860 session->fc_attrs._name_)
4861
4862#define _verify_back_channel_attr(_name_) \
4863 _verify_channel_attr("back", #_name_, \
4864 args->bc_attrs._name_, \
4865 session->bc_attrs._name_)
4866
4867/*
4868 * The server is not allowed to increase the fore channel header pad size,
4869 * maximum response size, or maximum number of operations.
4870 *
4871 * The back channel attributes are only negotiatied down: We send what the
4872 * (back channel) server insists upon.
4873 */
4874static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
4875 struct nfs4_session *session)
4876{
4877 int ret = 0;
4878
4879 ret |= _verify_fore_channel_attr(headerpadsz);
4880 ret |= _verify_fore_channel_attr(max_resp_sz);
4881 ret |= _verify_fore_channel_attr(max_ops);
4882
4883 ret |= _verify_back_channel_attr(headerpadsz);
4884 ret |= _verify_back_channel_attr(max_rqst_sz);
4885 ret |= _verify_back_channel_attr(max_resp_sz);
4886 ret |= _verify_back_channel_attr(max_resp_sz_cached);
4887 ret |= _verify_back_channel_attr(max_ops);
4888 ret |= _verify_back_channel_attr(max_reqs);
4889
4890 return ret;
4891}
4892
Andy Adamsonfc931582009-04-01 09:22:31 -04004893static int _nfs4_proc_create_session(struct nfs_client *clp)
4894{
4895 struct nfs4_session *session = clp->cl_session;
4896 struct nfs41_create_session_args args = {
4897 .client = clp,
4898 .cb_program = NFS4_CALLBACK,
4899 };
4900 struct nfs41_create_session_res res = {
4901 .client = clp,
4902 };
4903 struct rpc_message msg = {
4904 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
4905 .rpc_argp = &args,
4906 .rpc_resp = &res,
4907 };
4908 int status;
4909
4910 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04004911 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04004912
4913 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4914
Andy Adamson8d353012009-04-01 09:22:32 -04004915 if (!status)
4916 /* Verify the session's negotiated channel_attrs values */
4917 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04004918 if (!status) {
4919 /* Increment the clientid slot sequence id */
4920 clp->cl_seqid++;
4921 }
4922
4923 return status;
4924}
4925
4926/*
4927 * Issues a CREATE_SESSION operation to the server.
4928 * It is the responsibility of the caller to verify the session is
4929 * expired before calling this routine.
4930 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05004931int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04004932{
4933 int status;
4934 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04004935 struct nfs4_session *session = clp->cl_session;
4936
4937 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
4938
4939 status = _nfs4_proc_create_session(clp);
4940 if (status)
4941 goto out;
4942
Trond Myklebustf26468f2009-12-05 19:32:11 -05004943 /* Init and reset the fore channel */
4944 status = nfs4_init_slot_tables(session);
4945 dprintk("slot table initialization returned %d\n", status);
4946 if (status)
4947 goto out;
4948 status = nfs4_reset_slot_tables(session);
4949 dprintk("slot table reset returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04004950 if (status)
4951 goto out;
4952
4953 ptr = (unsigned *)&session->sess_id.data[0];
4954 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
4955 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04004956out:
4957 dprintk("<-- %s\n", __func__);
4958 return status;
4959}
4960
Andy Adamson0f3e66c2009-04-01 09:22:34 -04004961/*
4962 * Issue the over-the-wire RPC DESTROY_SESSION.
4963 * The caller must serialize access to this routine.
4964 */
4965int nfs4_proc_destroy_session(struct nfs4_session *session)
4966{
4967 int status = 0;
4968 struct rpc_message msg;
4969
4970 dprintk("--> nfs4_proc_destroy_session\n");
4971
4972 /* session is still being setup */
4973 if (session->clp->cl_cons_state != NFS_CS_READY)
4974 return status;
4975
4976 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
4977 msg.rpc_argp = session;
4978 msg.rpc_resp = NULL;
4979 msg.rpc_cred = NULL;
4980 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4981
4982 if (status)
4983 printk(KERN_WARNING
4984 "Got error %d from the server on DESTROY_SESSION. "
4985 "Session has been destroyed regardless...\n", status);
4986
4987 dprintk("<-- nfs4_proc_destroy_session\n");
4988 return status;
4989}
4990
Trond Myklebustfccba802009-07-21 16:48:07 -04004991int nfs4_init_session(struct nfs_server *server)
4992{
4993 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05004994 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05004995 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04004996 int ret;
4997
4998 if (!nfs4_has_session(clp))
4999 return 0;
5000
Andy Adamson68bf05e2009-12-15 12:55:02 -05005001 rsize = server->rsize;
5002 if (rsize == 0)
5003 rsize = NFS_MAX_FILE_IO_SIZE;
5004 wsize = server->wsize;
5005 if (wsize == 0)
5006 wsize = NFS_MAX_FILE_IO_SIZE;
5007
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005008 session = clp->cl_session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005009 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5010 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005011
Trond Myklebustfccba802009-07-21 16:48:07 -04005012 ret = nfs4_recover_expired_lease(server);
5013 if (!ret)
5014 ret = nfs4_check_client_ready(clp);
5015 return ret;
5016}
5017
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005018/*
5019 * Renew the cl_session lease.
5020 */
5021static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5022{
5023 struct nfs4_sequence_args args;
5024 struct nfs4_sequence_res res;
5025
5026 struct rpc_message msg = {
5027 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5028 .rpc_argp = &args,
5029 .rpc_resp = &res,
5030 .rpc_cred = cred,
5031 };
5032
5033 args.sa_cache_this = 0;
5034
5035 return nfs4_call_sync_sequence(clp, clp->cl_rpcclient, &msg, &args,
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005036 &res, args.sa_cache_this, 1);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005037}
5038
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005039static void nfs41_sequence_release(void *data)
5040{
5041 struct nfs_client *clp = (struct nfs_client *)data;
5042
Alexandros Batsakis71358402010-02-05 03:45:05 -08005043 if (atomic_read(&clp->cl_count) > 1)
5044 nfs4_schedule_state_renewal(clp);
5045 nfs_put_client(clp);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005046}
5047
5048static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005049{
5050 struct nfs_client *clp = (struct nfs_client *)data;
5051
5052 nfs41_sequence_done(clp, task->tk_msg.rpc_resp, task->tk_status);
5053
5054 if (task->tk_status < 0) {
5055 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005056 if (atomic_read(&clp->cl_count) == 1)
5057 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005058
5059 if (_nfs4_async_handle_error(task, NULL, clp, NULL)
5060 == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05005061 nfs_restart_rpc(task, clp);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005062 return;
5063 }
5064 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005065 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005066out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005067 kfree(task->tk_msg.rpc_argp);
5068 kfree(task->tk_msg.rpc_resp);
5069
5070 dprintk("<-- %s\n", __func__);
5071}
5072
5073static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5074{
5075 struct nfs_client *clp;
5076 struct nfs4_sequence_args *args;
5077 struct nfs4_sequence_res *res;
5078
5079 clp = (struct nfs_client *)data;
5080 args = task->tk_msg.rpc_argp;
5081 res = task->tk_msg.rpc_resp;
5082
5083 if (nfs4_setup_sequence(clp, args, res, 0, task))
5084 return;
5085 rpc_call_start(task);
5086}
5087
5088static const struct rpc_call_ops nfs41_sequence_ops = {
5089 .rpc_call_done = nfs41_sequence_call_done,
5090 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005091 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005092};
5093
5094static int nfs41_proc_async_sequence(struct nfs_client *clp,
5095 struct rpc_cred *cred)
5096{
5097 struct nfs4_sequence_args *args;
5098 struct nfs4_sequence_res *res;
5099 struct rpc_message msg = {
5100 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5101 .rpc_cred = cred,
5102 };
5103
Alexandros Batsakis71358402010-02-05 03:45:05 -08005104 if (!atomic_inc_not_zero(&clp->cl_count))
5105 return -EIO;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005106 args = kzalloc(sizeof(*args), GFP_KERNEL);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005107 res = kzalloc(sizeof(*res), GFP_KERNEL);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005108 if (!args || !res) {
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005109 kfree(args);
Dan Carpenter7dd08a52010-03-06 15:02:22 +03005110 kfree(res);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005111 nfs_put_client(clp);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005112 return -ENOMEM;
5113 }
5114 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
5115 msg.rpc_argp = args;
5116 msg.rpc_resp = res;
5117
5118 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
5119 &nfs41_sequence_ops, (void *)clp);
5120}
5121
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005122struct nfs4_reclaim_complete_data {
5123 struct nfs_client *clp;
5124 struct nfs41_reclaim_complete_args arg;
5125 struct nfs41_reclaim_complete_res res;
5126};
5127
5128static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5129{
5130 struct nfs4_reclaim_complete_data *calldata = data;
5131
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005132 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005133 if (nfs4_setup_sequence(calldata->clp, &calldata->arg.seq_args,
5134 &calldata->res.seq_res, 0, task))
5135 return;
5136
5137 rpc_call_start(task);
5138}
5139
5140static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5141{
5142 struct nfs4_reclaim_complete_data *calldata = data;
5143 struct nfs_client *clp = calldata->clp;
5144 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5145
5146 dprintk("--> %s\n", __func__);
5147 nfs41_sequence_done(clp, res, task->tk_status);
5148 switch (task->tk_status) {
5149 case 0:
5150 case -NFS4ERR_COMPLETE_ALREADY:
5151 break;
5152 case -NFS4ERR_BADSESSION:
5153 case -NFS4ERR_DEADSESSION:
5154 /*
5155 * Handle the session error, but do not retry the operation, as
5156 * we have no way of telling whether the clientid had to be
5157 * reset before we got our reply. If reset, a new wave of
5158 * reclaim operations will follow, containing their own reclaim
5159 * complete. We don't want our retry to get on the way of
5160 * recovery by incorrectly indicating to the server that we're
5161 * done reclaiming state since the process had to be restarted.
5162 */
5163 _nfs4_async_handle_error(task, NULL, clp, NULL);
5164 break;
5165 default:
5166 if (_nfs4_async_handle_error(
5167 task, NULL, clp, NULL) == -EAGAIN) {
5168 rpc_restart_call_prepare(task);
5169 return;
5170 }
5171 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005172
5173 dprintk("<-- %s\n", __func__);
5174}
5175
5176static void nfs4_free_reclaim_complete_data(void *data)
5177{
5178 struct nfs4_reclaim_complete_data *calldata = data;
5179
5180 kfree(calldata);
5181}
5182
5183static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5184 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5185 .rpc_call_done = nfs4_reclaim_complete_done,
5186 .rpc_release = nfs4_free_reclaim_complete_data,
5187};
5188
5189/*
5190 * Issue a global reclaim complete.
5191 */
5192static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5193{
5194 struct nfs4_reclaim_complete_data *calldata;
5195 struct rpc_task *task;
5196 struct rpc_message msg = {
5197 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5198 };
5199 struct rpc_task_setup task_setup_data = {
5200 .rpc_client = clp->cl_rpcclient,
5201 .rpc_message = &msg,
5202 .callback_ops = &nfs4_reclaim_complete_call_ops,
5203 .flags = RPC_TASK_ASYNC,
5204 };
5205 int status = -ENOMEM;
5206
5207 dprintk("--> %s\n", __func__);
5208 calldata = kzalloc(sizeof(*calldata), GFP_KERNEL);
5209 if (calldata == NULL)
5210 goto out;
5211 calldata->clp = clp;
5212 calldata->arg.one_fs = 0;
5213 calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
5214
5215 msg.rpc_argp = &calldata->arg;
5216 msg.rpc_resp = &calldata->res;
5217 task_setup_data.callback_data = calldata;
5218 task = rpc_run_task(&task_setup_data);
5219 if (IS_ERR(task))
5220 status = PTR_ERR(task);
5221 rpc_put_task(task);
5222out:
5223 dprintk("<-- %s status=%d\n", __func__, status);
5224 return status;
5225}
Andy Adamson557134a2009-04-01 09:21:53 -04005226#endif /* CONFIG_NFS_V4_1 */
5227
Andy Adamson591d71c2009-04-01 09:22:47 -04005228struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005229 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005230 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231 .recover_open = nfs4_open_reclaim,
5232 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04005233 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005234 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235};
5236
Andy Adamson591d71c2009-04-01 09:22:47 -04005237#if defined(CONFIG_NFS_V4_1)
5238struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
5239 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5240 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5241 .recover_open = nfs4_open_reclaim,
5242 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05005243 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005244 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005245 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04005246};
5247#endif /* CONFIG_NFS_V4_1 */
5248
5249struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005250 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005251 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252 .recover_open = nfs4_open_expired,
5253 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04005254 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005255 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005256};
5257
Andy Adamson591d71c2009-04-01 09:22:47 -04005258#if defined(CONFIG_NFS_V4_1)
5259struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
5260 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5261 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5262 .recover_open = nfs4_open_expired,
5263 .recover_lock = nfs4_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05005264 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005265 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04005266};
5267#endif /* CONFIG_NFS_V4_1 */
5268
Benny Halevy29fba382009-04-01 09:22:44 -04005269struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
5270 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04005271 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005272 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04005273};
5274
5275#if defined(CONFIG_NFS_V4_1)
5276struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
5277 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04005278 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005279 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04005280};
5281#endif
5282
5283/*
5284 * Per minor version reboot and network partition recovery ops
5285 */
5286
Andy Adamson591d71c2009-04-01 09:22:47 -04005287struct nfs4_state_recovery_ops *nfs4_reboot_recovery_ops[] = {
5288 &nfs40_reboot_recovery_ops,
5289#if defined(CONFIG_NFS_V4_1)
5290 &nfs41_reboot_recovery_ops,
5291#endif
5292};
5293
5294struct nfs4_state_recovery_ops *nfs4_nograce_recovery_ops[] = {
5295 &nfs40_nograce_recovery_ops,
5296#if defined(CONFIG_NFS_V4_1)
5297 &nfs41_nograce_recovery_ops,
5298#endif
5299};
5300
Benny Halevy29fba382009-04-01 09:22:44 -04005301struct nfs4_state_maintenance_ops *nfs4_state_renewal_ops[] = {
5302 &nfs40_state_renewal_ops,
5303#if defined(CONFIG_NFS_V4_1)
5304 &nfs41_state_renewal_ops,
5305#endif
5306};
5307
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08005308static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005309 .permission = nfs_permission,
5310 .getattr = nfs_getattr,
5311 .setattr = nfs_setattr,
5312 .getxattr = nfs4_getxattr,
5313 .setxattr = nfs4_setxattr,
5314 .listxattr = nfs4_listxattr,
5315};
5316
David Howells509de812006-08-22 20:06:11 -04005317const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005318 .version = 4, /* protocol version */
5319 .dentry_ops = &nfs4_dentry_operations,
5320 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005321 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322 .getroot = nfs4_proc_get_root,
5323 .getattr = nfs4_proc_getattr,
5324 .setattr = nfs4_proc_setattr,
David Howells2b3de442006-08-22 20:06:09 -04005325 .lookupfh = nfs4_proc_lookupfh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326 .lookup = nfs4_proc_lookup,
5327 .access = nfs4_proc_access,
5328 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005329 .create = nfs4_proc_create,
5330 .remove = nfs4_proc_remove,
5331 .unlink_setup = nfs4_proc_unlink_setup,
5332 .unlink_done = nfs4_proc_unlink_done,
5333 .rename = nfs4_proc_rename,
5334 .link = nfs4_proc_link,
5335 .symlink = nfs4_proc_symlink,
5336 .mkdir = nfs4_proc_mkdir,
5337 .rmdir = nfs4_proc_remove,
5338 .readdir = nfs4_proc_readdir,
5339 .mknod = nfs4_proc_mknod,
5340 .statfs = nfs4_proc_statfs,
5341 .fsinfo = nfs4_proc_fsinfo,
5342 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04005343 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344 .decode_dirent = nfs4_decode_dirent,
5345 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05005346 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005347 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005348 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005350 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005351 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005352 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04005353 .close_context = nfs4_close_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354};
5355
5356/*
5357 * Local variables:
5358 * c-basic-offset: 8
5359 * End:
5360 */