blob: 726bc195039d9c49ae233ec4156f6200ae30ed85 [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 */
422 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400423 }
424out:
425 /* The session may be reset by one of the error handlers. */
426 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
427 nfs41_sequence_free_slot(clp, res);
428}
429
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400430/*
Benny Halevy510b8172009-04-01 09:22:14 -0400431 * nfs4_find_slot - efficiently look for a free slot
432 *
433 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
434 * If found, we mark the slot as used, update the highest_used_slotid,
435 * and respectively set up the sequence operation args.
436 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400437 *
438 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400439 */
440static u8
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800441nfs4_find_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400442{
443 int slotid;
444 u8 ret_id = NFS4_MAX_SLOT_TABLE;
445 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
446
Benny Halevy510b8172009-04-01 09:22:14 -0400447 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
448 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
449 tbl->max_slots);
450 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
451 if (slotid >= tbl->max_slots)
452 goto out;
453 __set_bit(slotid, tbl->used_slots);
454 if (slotid > tbl->highest_used_slotid)
455 tbl->highest_used_slotid = slotid;
456 ret_id = slotid;
457out:
458 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
459 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400460 return ret_id;
461}
462
Andy Adamsonce5039c2009-04-01 09:22:13 -0400463static int nfs41_setup_sequence(struct nfs4_session *session,
464 struct nfs4_sequence_args *args,
465 struct nfs4_sequence_res *res,
466 int cache_reply,
467 struct rpc_task *task)
468{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400469 struct nfs4_slot *slot;
470 struct nfs4_slot_table *tbl;
471 u8 slotid;
472
473 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400474 /* slot already allocated? */
475 if (res->sr_slotid != NFS4_MAX_SLOT_TABLE)
476 return 0;
477
478 memset(res, 0, sizeof(*res));
479 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400480 tbl = &session->fc_slot_table;
481
482 spin_lock(&tbl->slot_tbl_lock);
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800483 if (test_bit(NFS4CLNT_SESSION_DRAINING, &session->clp->cl_state) &&
484 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamsonea028ac2009-12-04 15:55:38 -0500485 /*
486 * The state manager will wait until the slot table is empty.
487 * Schedule the reset thread
488 */
Andy Adamson05f0d232009-12-04 15:55:37 -0500489 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400490 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500491 dprintk("%s Schedule Session Reset\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500492 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400493 }
494
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800495 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
496 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
497 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
498 spin_unlock(&tbl->slot_tbl_lock);
499 dprintk("%s enforce FIFO order\n", __func__);
500 return -EAGAIN;
501 }
502
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800503 slotid = nfs4_find_slot(tbl);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400504 if (slotid == NFS4_MAX_SLOT_TABLE) {
505 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
506 spin_unlock(&tbl->slot_tbl_lock);
507 dprintk("<-- %s: no free slots\n", __func__);
508 return -EAGAIN;
509 }
510 spin_unlock(&tbl->slot_tbl_lock);
511
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800512 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400513 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400514 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400515 args->sa_slotid = slotid;
516 args->sa_cache_this = cache_reply;
517
518 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
519
Benny Halevy99fe60d2009-04-01 09:22:29 -0400520 res->sr_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400521 res->sr_slotid = slotid;
522 res->sr_renewal_time = jiffies;
523 /*
524 * sr_status is only set in decode_sequence, and so will remain
525 * set to 1 if an rpc level failure occurs.
526 */
527 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400528 return 0;
529}
530
531int nfs4_setup_sequence(struct nfs_client *clp,
532 struct nfs4_sequence_args *args,
533 struct nfs4_sequence_res *res,
534 int cache_reply,
535 struct rpc_task *task)
536{
537 int ret = 0;
538
539 dprintk("--> %s clp %p session %p sr_slotid %d\n",
540 __func__, clp, clp->cl_session, res->sr_slotid);
541
542 if (!nfs4_has_session(clp))
543 goto out;
544 ret = nfs41_setup_sequence(clp->cl_session, args, res, cache_reply,
545 task);
Andy Adamson2628edd2009-12-04 15:55:35 -0500546 if (ret && ret != -EAGAIN) {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400547 /* terminate rpc task */
548 task->tk_status = ret;
549 task->tk_action = NULL;
550 }
551out:
552 dprintk("<-- %s status=%d\n", __func__, ret);
553 return ret;
554}
555
556struct nfs41_call_sync_data {
557 struct nfs_client *clp;
558 struct nfs4_sequence_args *seq_args;
559 struct nfs4_sequence_res *seq_res;
560 int cache_reply;
561};
562
563static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
564{
565 struct nfs41_call_sync_data *data = calldata;
566
567 dprintk("--> %s data->clp->cl_session %p\n", __func__,
568 data->clp->cl_session);
569 if (nfs4_setup_sequence(data->clp, data->seq_args,
570 data->seq_res, data->cache_reply, task))
571 return;
572 rpc_call_start(task);
573}
574
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800575static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
576{
577 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
578 nfs41_call_sync_prepare(task, calldata);
579}
580
Andy Adamson69ab40c2009-04-01 09:22:19 -0400581static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
582{
583 struct nfs41_call_sync_data *data = calldata;
584
585 nfs41_sequence_done(data->clp, data->seq_res, task->tk_status);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400586}
587
Andy Adamsonce5039c2009-04-01 09:22:13 -0400588struct rpc_call_ops nfs41_call_sync_ops = {
589 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400590 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400591};
592
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800593struct rpc_call_ops nfs41_call_priv_sync_ops = {
594 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
595 .rpc_call_done = nfs41_call_sync_done,
596};
597
Andy Adamsonce5039c2009-04-01 09:22:13 -0400598static int nfs4_call_sync_sequence(struct nfs_client *clp,
599 struct rpc_clnt *clnt,
600 struct rpc_message *msg,
601 struct nfs4_sequence_args *args,
602 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800603 int cache_reply,
604 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400605{
606 int ret;
607 struct rpc_task *task;
608 struct nfs41_call_sync_data data = {
609 .clp = clp,
610 .seq_args = args,
611 .seq_res = res,
612 .cache_reply = cache_reply,
613 };
614 struct rpc_task_setup task_setup = {
615 .rpc_client = clnt,
616 .rpc_message = msg,
617 .callback_ops = &nfs41_call_sync_ops,
618 .callback_data = &data
619 };
620
621 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800622 if (privileged)
623 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400624 task = rpc_run_task(&task_setup);
625 if (IS_ERR(task))
626 ret = PTR_ERR(task);
627 else {
628 ret = task->tk_status;
629 rpc_put_task(task);
630 }
631 return ret;
632}
633
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400634int _nfs4_call_sync_session(struct nfs_server *server,
635 struct rpc_message *msg,
636 struct nfs4_sequence_args *args,
637 struct nfs4_sequence_res *res,
638 int cache_reply)
639{
Andy Adamsonce5039c2009-04-01 09:22:13 -0400640 return nfs4_call_sync_sequence(server->nfs_client, server->client,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800641 msg, args, res, cache_reply, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400642}
643
644#endif /* CONFIG_NFS_V4_1 */
645
646int _nfs4_call_sync(struct nfs_server *server,
647 struct rpc_message *msg,
648 struct nfs4_sequence_args *args,
649 struct nfs4_sequence_res *res,
650 int cache_reply)
651{
Benny Halevyf3752972009-04-01 09:22:04 -0400652 args->sa_session = res->sr_session = NULL;
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400653 return rpc_call_sync(server->client, msg, 0);
654}
655
656#define nfs4_call_sync(server, msg, args, res, cache_reply) \
657 (server)->nfs_client->cl_call_sync((server), (msg), &(args)->seq_args, \
658 &(res)->seq_res, (cache_reply))
659
Andy Adamsonb0df8062009-04-01 09:22:18 -0400660static void nfs4_sequence_done(const struct nfs_server *server,
661 struct nfs4_sequence_res *res, int rpc_status)
662{
663#ifdef CONFIG_NFS_V4_1
664 if (nfs4_has_session(server->nfs_client))
665 nfs41_sequence_done(server->nfs_client, res, rpc_status);
666#endif /* CONFIG_NFS_V4_1 */
667}
668
Trond Myklebust38478b22006-05-25 01:40:57 -0400669static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
Trond Myklebust38478b22006-05-25 01:40:57 -0400671 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Trond Myklebust38478b22006-05-25 01:40:57 -0400673 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400674 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
675 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400676 nfs_force_lookup_revalidate(dir);
Trond Myklebust40d24702007-10-08 09:24:22 -0400677 nfsi->change_attr = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400678 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679}
680
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100681struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400682 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100683 struct nfs_openargs o_arg;
684 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100685 struct nfs_open_confirmargs c_arg;
686 struct nfs_open_confirmres c_res;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100687 struct nfs_fattr f_attr;
688 struct nfs_fattr dir_attr;
Trond Myklebustad389da2007-06-05 12:30:00 -0400689 struct path path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100690 struct dentry *dir;
691 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400692 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100693 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100694 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400695 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100696 int rpc_status;
697 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100698};
699
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400700
701static void nfs4_init_opendata_res(struct nfs4_opendata *p)
702{
703 p->o_res.f_attr = &p->f_attr;
704 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400705 p->o_res.seqid = p->o_arg.seqid;
706 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400707 p->o_res.server = p->o_arg.server;
708 nfs_fattr_init(&p->f_attr);
709 nfs_fattr_init(&p->dir_attr);
Benny Halevy578e4582009-06-18 22:01:23 -0400710 p->o_res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400711}
712
Trond Myklebustad389da2007-06-05 12:30:00 -0400713static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500714 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100715 const struct iattr *attrs)
716{
Trond Myklebustad389da2007-06-05 12:30:00 -0400717 struct dentry *parent = dget_parent(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100718 struct inode *dir = parent->d_inode;
719 struct nfs_server *server = NFS_SERVER(dir);
720 struct nfs4_opendata *p;
721
722 p = kzalloc(sizeof(*p), GFP_KERNEL);
723 if (p == NULL)
724 goto err;
725 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
726 if (p->o_arg.seqid == NULL)
727 goto err_free;
Trond Myklebustad389da2007-06-05 12:30:00 -0400728 p->path.mnt = mntget(path->mnt);
729 p->path.dentry = dget(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100730 p->dir = parent;
731 p->owner = sp;
732 atomic_inc(&sp->so_count);
733 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500734 p->o_arg.open_flags = flags;
735 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400736 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400737 p->o_arg.id = sp->so_owner_id.id;
Trond Myklebustad389da2007-06-05 12:30:00 -0400738 p->o_arg.name = &p->path.dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100739 p->o_arg.server = server;
740 p->o_arg.bitmask = server->attr_bitmask;
741 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100742 if (flags & O_EXCL) {
Alexandros Batsakis4882ef72009-12-05 13:30:21 -0500743 if (nfs4_has_persistent_session(server->nfs_client)) {
744 /* GUARDED */
745 p->o_arg.u.attrs = &p->attrs;
746 memcpy(&p->attrs, attrs, sizeof(p->attrs));
747 } else { /* EXCLUSIVE4_1 */
748 u32 *s = (u32 *) p->o_arg.u.verifier.data;
749 s[0] = jiffies;
750 s[1] = current->pid;
751 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100752 } else if (flags & O_CREAT) {
753 p->o_arg.u.attrs = &p->attrs;
754 memcpy(&p->attrs, attrs, sizeof(p->attrs));
755 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100756 p->c_arg.fh = &p->o_res.fh;
757 p->c_arg.stateid = &p->o_res.stateid;
758 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400759 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400760 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100761 return p;
762err_free:
763 kfree(p);
764err:
765 dput(parent);
766 return NULL;
767}
768
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400769static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100770{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400771 struct nfs4_opendata *p = container_of(kref,
772 struct nfs4_opendata, kref);
773
774 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400775 if (p->state != NULL)
776 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400777 nfs4_put_state_owner(p->owner);
778 dput(p->dir);
Jan Blunck31f31db12008-05-02 13:42:45 -0700779 path_put(&p->path);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400780 kfree(p);
781}
782
783static void nfs4_opendata_put(struct nfs4_opendata *p)
784{
785 if (p != NULL)
786 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100787}
788
Trond Myklebust06f814a2006-01-03 09:55:07 +0100789static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
790{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100791 int ret;
792
Trond Myklebust06f814a2006-01-03 09:55:07 +0100793 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100794 return ret;
795}
796
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500797static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400798{
799 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500800
801 if (open_mode & O_EXCL)
802 goto out;
803 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400804 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500805 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
806 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400807 break;
808 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500809 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
810 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400811 break;
812 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500813 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
814 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400815 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500816out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400817 return ret;
818}
819
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500820static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400821{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500822 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400823 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500824 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400825 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500826 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400827 return 1;
828}
829
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500830static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100831{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500832 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100833 case FMODE_WRITE:
834 state->n_wronly++;
835 break;
836 case FMODE_READ:
837 state->n_rdonly++;
838 break;
839 case FMODE_READ|FMODE_WRITE:
840 state->n_rdwr++;
841 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500842 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100843}
844
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500845static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400846{
847 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
848 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
849 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500850 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400851 case FMODE_READ:
852 set_bit(NFS_O_RDONLY_STATE, &state->flags);
853 break;
854 case FMODE_WRITE:
855 set_bit(NFS_O_WRONLY_STATE, &state->flags);
856 break;
857 case FMODE_READ|FMODE_WRITE:
858 set_bit(NFS_O_RDWR_STATE, &state->flags);
859 }
860}
861
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500862static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400863{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400864 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500865 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400866 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400867}
868
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500869static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400871 /*
872 * Protect the call to nfs4_state_set_mode_locked and
873 * serialise the stateid update
874 */
875 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400876 if (deleg_stateid != NULL) {
877 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
878 set_bit(NFS_DELEGATED_STATE, &state->flags);
879 }
880 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500881 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400882 write_sequnlock(&state->seqlock);
883 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500884 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700885 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886}
887
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500888static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500889{
890 struct nfs_inode *nfsi = NFS_I(state->inode);
891 struct nfs_delegation *deleg_cur;
892 int ret = 0;
893
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500894 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500895
896 rcu_read_lock();
897 deleg_cur = rcu_dereference(nfsi->delegation);
898 if (deleg_cur == NULL)
899 goto no_delegation;
900
901 spin_lock(&deleg_cur->lock);
902 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500903 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500904 goto no_delegation_unlock;
905
906 if (delegation == NULL)
907 delegation = &deleg_cur->stateid;
908 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
909 goto no_delegation_unlock;
910
Trond Myklebustb7391f42008-12-23 15:21:52 -0500911 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500912 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500913 ret = 1;
914no_delegation_unlock:
915 spin_unlock(&deleg_cur->lock);
916no_delegation:
917 rcu_read_unlock();
918
919 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500920 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500921 ret = 1;
922 }
923
924 return ret;
925}
926
927
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500928static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400929{
930 struct nfs_delegation *delegation;
931
932 rcu_read_lock();
933 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500934 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -0400935 rcu_read_unlock();
936 return;
937 }
938 rcu_read_unlock();
939 nfs_inode_return_delegation(inode);
940}
941
Trond Myklebust6ee41262007-07-08 14:11:36 -0400942static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400943{
944 struct nfs4_state *state = opendata->state;
945 struct nfs_inode *nfsi = NFS_I(state->inode);
946 struct nfs_delegation *delegation;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500947 int open_mode = opendata->o_arg.open_flags & O_EXCL;
948 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400949 nfs4_stateid stateid;
950 int ret = -EAGAIN;
951
Trond Myklebustaac00a82007-07-05 19:02:21 -0400952 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500953 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400954 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500955 if (can_open_cached(state, fmode, open_mode)) {
956 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400957 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400958 goto out_return_state;
959 }
960 spin_unlock(&state->owner->so_lock);
961 }
Trond Myklebust34310432008-12-23 15:21:38 -0500962 rcu_read_lock();
963 delegation = rcu_dereference(nfsi->delegation);
964 if (delegation == NULL ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500965 !can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -0500966 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -0400967 break;
Trond Myklebust34310432008-12-23 15:21:38 -0500968 }
Trond Myklebustaac00a82007-07-05 19:02:21 -0400969 /* Save the delegation */
970 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
971 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -0400972 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400973 if (ret != 0)
974 goto out;
975 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -0500976
977 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500978 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -0500979 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400980 }
Trond Myklebustaac00a82007-07-05 19:02:21 -0400981out:
982 return ERR_PTR(ret);
983out_return_state:
984 atomic_inc(&state->count);
985 return state;
986}
987
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100988static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
989{
990 struct inode *inode;
991 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -0400992 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400993 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100994
Trond Myklebustaac00a82007-07-05 19:02:21 -0400995 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400996 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400997 goto out;
998 }
999
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001000 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001001 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001002 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001003 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001004 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001005 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001006 goto err;
1007 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001008 state = nfs4_get_open_state(inode, data->owner);
1009 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001010 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001011 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001012 int delegation_flags = 0;
1013
Trond Myklebust003707c2007-07-05 18:07:55 -04001014 rcu_read_lock();
1015 delegation = rcu_dereference(NFS_I(inode)->delegation);
1016 if (delegation)
1017 delegation_flags = delegation->flags;
1018 rcu_read_unlock();
Trond Myklebust15c831b2008-12-23 15:21:39 -05001019 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001020 nfs_inode_set_delegation(state->inode,
1021 data->owner->so_cred,
1022 &data->o_res);
1023 else
1024 nfs_inode_reclaim_delegation(state->inode,
1025 data->owner->so_cred,
1026 &data->o_res);
1027 }
Trond Myklebust34310432008-12-23 15:21:38 -05001028
1029 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001030 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001031 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001032out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001033 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001034err_put_inode:
1035 iput(inode);
1036err:
1037 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001038}
1039
Trond Myklebust864472e2006-01-03 09:55:15 +01001040static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1041{
1042 struct nfs_inode *nfsi = NFS_I(state->inode);
1043 struct nfs_open_context *ctx;
1044
1045 spin_lock(&state->inode->i_lock);
1046 list_for_each_entry(ctx, &nfsi->open_files, list) {
1047 if (ctx->state != state)
1048 continue;
1049 get_nfs_open_context(ctx);
1050 spin_unlock(&state->inode->i_lock);
1051 return ctx;
1052 }
1053 spin_unlock(&state->inode->i_lock);
1054 return ERR_PTR(-ENOENT);
1055}
1056
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001057static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1058{
1059 struct nfs4_opendata *opendata;
1060
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001061 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001062 if (opendata == NULL)
1063 return ERR_PTR(-ENOMEM);
1064 opendata->state = state;
1065 atomic_inc(&state->count);
1066 return opendata;
1067}
1068
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001069static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001070{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001071 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001072 int ret;
1073
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001074 opendata->o_arg.open_flags = 0;
1075 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001076 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1077 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1078 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001079 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001080 if (ret != 0)
1081 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001082 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001083 if (IS_ERR(newstate))
1084 return PTR_ERR(newstate);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001085 nfs4_close_state(&opendata->path, newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001086 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001087 return 0;
1088}
1089
1090static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1091{
Trond Myklebust864472e2006-01-03 09:55:15 +01001092 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001093 int ret;
1094
1095 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001096 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001097 smp_rmb();
1098 if (state->n_rdwr != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001099 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001100 if (ret != 0)
1101 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001102 if (newstate != state)
1103 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001104 }
1105 if (state->n_wronly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001106 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001107 if (ret != 0)
1108 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001109 if (newstate != state)
1110 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001111 }
1112 if (state->n_rdonly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001113 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001114 if (ret != 0)
1115 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001116 if (newstate != state)
1117 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001118 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001119 /*
1120 * We may have performed cached opens for all three recoveries.
1121 * Check if we need to update the current stateid.
1122 */
1123 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1124 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001125 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001126 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1127 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001128 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001129 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001130 return 0;
1131}
1132
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133/*
1134 * OPEN_RECLAIM:
1135 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001137static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001139 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001140 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001141 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 int status;
1143
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001144 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1145 if (IS_ERR(opendata))
1146 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001147 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1148 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001149 rcu_read_lock();
1150 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001151 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001152 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001153 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001154 opendata->o_arg.u.delegation_type = delegation_type;
1155 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001156 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 return status;
1158}
1159
Trond Myklebust539cd032007-06-05 11:46:42 -04001160static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161{
1162 struct nfs_server *server = NFS_SERVER(state->inode);
1163 struct nfs4_exception exception = { };
1164 int err;
1165 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001166 err = _nfs4_do_open_reclaim(ctx, state);
Jeff Layton2c643482010-01-07 09:42:03 -05001167 if (err != -NFS4ERR_DELAY && err != -EKEYEXPIRED)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001168 break;
1169 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 } while (exception.retry);
1171 return err;
1172}
1173
Trond Myklebust864472e2006-01-03 09:55:15 +01001174static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1175{
1176 struct nfs_open_context *ctx;
1177 int ret;
1178
1179 ctx = nfs4_state_find_open_context(state);
1180 if (IS_ERR(ctx))
1181 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001182 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001183 put_nfs_open_context(ctx);
1184 return ret;
1185}
1186
Trond Myklebust13437e12007-07-06 15:10:43 -04001187static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001189 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001190 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001192 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1193 if (IS_ERR(opendata))
1194 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001195 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -04001196 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001197 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +01001198 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001199 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001200 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201}
1202
Trond Myklebust13437e12007-07-06 15:10:43 -04001203int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204{
1205 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001206 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 int err;
1208 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001209 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 switch (err) {
1211 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001212 case -ENOENT:
1213 case -ESTALE:
1214 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001215 case -NFS4ERR_BADSESSION:
1216 case -NFS4ERR_BADSLOT:
1217 case -NFS4ERR_BAD_HIGH_SLOT:
1218 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1219 case -NFS4ERR_DEADSESSION:
1220 nfs4_schedule_state_recovery(
1221 server->nfs_client);
1222 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 case -NFS4ERR_STALE_CLIENTID:
1224 case -NFS4ERR_STALE_STATEID:
1225 case -NFS4ERR_EXPIRED:
1226 /* Don't recall a delegation if it was lost */
David Howells7539bba2006-08-22 20:06:09 -04001227 nfs4_schedule_state_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001228 goto out;
1229 case -ERESTARTSYS:
1230 /*
1231 * The show must go on: exit, but mark the
1232 * stateid as needing recovery.
1233 */
1234 case -NFS4ERR_ADMIN_REVOKED:
1235 case -NFS4ERR_BAD_STATEID:
1236 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
1237 case -ENOMEM:
1238 err = 0;
1239 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 }
1241 err = nfs4_handle_exception(server, err, &exception);
1242 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001243out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 return err;
1245}
1246
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001247static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1248{
1249 struct nfs4_opendata *data = calldata;
1250
1251 data->rpc_status = task->tk_status;
1252 if (RPC_ASSASSINATED(task))
1253 return;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001254 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001255 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1256 sizeof(data->o_res.stateid.data));
Trond Myklebustbb226292008-01-02 15:19:18 -05001257 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001258 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001259 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001260 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001261}
1262
1263static void nfs4_open_confirm_release(void *calldata)
1264{
1265 struct nfs4_opendata *data = calldata;
1266 struct nfs4_state *state = NULL;
1267
1268 /* If this request hasn't been cancelled, do nothing */
1269 if (data->cancelled == 0)
1270 goto out_free;
1271 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001272 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001273 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001274 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001275 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001276 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001277out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001278 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001279}
1280
1281static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001282 .rpc_call_done = nfs4_open_confirm_done,
1283 .rpc_release = nfs4_open_confirm_release,
1284};
1285
1286/*
1287 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1288 */
1289static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1290{
1291 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1292 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001293 struct rpc_message msg = {
1294 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1295 .rpc_argp = &data->c_arg,
1296 .rpc_resp = &data->c_res,
1297 .rpc_cred = data->owner->so_cred,
1298 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001299 struct rpc_task_setup task_setup_data = {
1300 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001301 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001302 .callback_ops = &nfs4_open_confirm_ops,
1303 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001304 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001305 .flags = RPC_TASK_ASYNC,
1306 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 int status;
1308
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001309 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001310 data->rpc_done = 0;
1311 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001312 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001313 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001314 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001315 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001316 status = nfs4_wait_for_completion_rpc_task(task);
1317 if (status != 0) {
1318 data->cancelled = 1;
1319 smp_wmb();
1320 } else
1321 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001322 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 return status;
1324}
1325
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001326static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001328 struct nfs4_opendata *data = calldata;
1329 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001330
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001331 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1332 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001333 /*
1334 * Check if we still need to send an OPEN call, or if we can use
1335 * a delegation instead.
1336 */
1337 if (data->state != NULL) {
1338 struct nfs_delegation *delegation;
1339
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001340 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001341 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001342 rcu_read_lock();
1343 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1344 if (delegation != NULL &&
Trond Myklebust15c831b2008-12-23 15:21:39 -05001345 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001346 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001347 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001348 }
1349 rcu_read_unlock();
1350 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001351 /* Update sequence id. */
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001352 data->o_arg.id = sp->so_owner_id.id;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001353 data->o_arg.clientid = sp->so_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001354 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001355 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001356 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1357 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001358 data->timestamp = jiffies;
Andy Adamsond8985282009-04-01 09:22:21 -04001359 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
1360 &data->o_arg.seq_args,
1361 &data->o_res.seq_res, 1, task))
1362 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001363 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001364 return;
1365out_no_action:
1366 task->tk_action = NULL;
1367
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001368}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001370static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1371{
1372 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1373 nfs4_open_prepare(task, calldata);
1374}
1375
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001376static void nfs4_open_done(struct rpc_task *task, void *calldata)
1377{
1378 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001380 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001381
Trond Myklebustd61e6122009-12-05 19:32:19 -05001382 nfs4_sequence_done(data->o_arg.server, &data->o_res.seq_res,
1383 task->tk_status);
Andy Adamsond8985282009-04-01 09:22:21 -04001384
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001385 if (RPC_ASSASSINATED(task))
1386 return;
1387 if (task->tk_status == 0) {
1388 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001389 case S_IFREG:
1390 break;
1391 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001392 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001393 break;
1394 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001395 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001396 break;
1397 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001398 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001399 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001400 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001401 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1402 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001403 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001404 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001405}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001406
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001407static void nfs4_open_release(void *calldata)
1408{
1409 struct nfs4_opendata *data = calldata;
1410 struct nfs4_state *state = NULL;
1411
1412 /* If this request hasn't been cancelled, do nothing */
1413 if (data->cancelled == 0)
1414 goto out_free;
1415 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001416 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001417 goto out_free;
1418 /* In case we need an open_confirm, no cleanup! */
1419 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1420 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001421 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001422 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001423 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001424out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001425 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001426}
1427
1428static const struct rpc_call_ops nfs4_open_ops = {
1429 .rpc_call_prepare = nfs4_open_prepare,
1430 .rpc_call_done = nfs4_open_done,
1431 .rpc_release = nfs4_open_release,
1432};
1433
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001434static const struct rpc_call_ops nfs4_recover_open_ops = {
1435 .rpc_call_prepare = nfs4_recover_open_prepare,
1436 .rpc_call_done = nfs4_open_done,
1437 .rpc_release = nfs4_open_release,
1438};
1439
1440static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001441{
1442 struct inode *dir = data->dir->d_inode;
1443 struct nfs_server *server = NFS_SERVER(dir);
1444 struct nfs_openargs *o_arg = &data->o_arg;
1445 struct nfs_openres *o_res = &data->o_res;
1446 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001447 struct rpc_message msg = {
1448 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1449 .rpc_argp = o_arg,
1450 .rpc_resp = o_res,
1451 .rpc_cred = data->owner->so_cred,
1452 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001453 struct rpc_task_setup task_setup_data = {
1454 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001455 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001456 .callback_ops = &nfs4_open_ops,
1457 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001458 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001459 .flags = RPC_TASK_ASYNC,
1460 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001461 int status;
1462
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001463 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001464 data->rpc_done = 0;
1465 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001466 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001467 if (isrecover)
1468 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001469 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001470 if (IS_ERR(task))
1471 return PTR_ERR(task);
1472 status = nfs4_wait_for_completion_rpc_task(task);
1473 if (status != 0) {
1474 data->cancelled = 1;
1475 smp_wmb();
1476 } else
1477 status = data->rpc_status;
1478 rpc_put_task(task);
1479
1480 return status;
1481}
1482
1483static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1484{
1485 struct inode *dir = data->dir->d_inode;
1486 struct nfs_openres *o_res = &data->o_res;
1487 int status;
1488
1489 status = nfs4_run_open_task(data, 1);
1490 if (status != 0 || !data->rpc_done)
1491 return status;
1492
1493 nfs_refresh_inode(dir, o_res->dir_attr);
1494
1495 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1496 status = _nfs4_proc_open_confirm(data);
1497 if (status != 0)
1498 return status;
1499 }
1500
1501 return status;
1502}
1503
1504/*
1505 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1506 */
1507static int _nfs4_proc_open(struct nfs4_opendata *data)
1508{
1509 struct inode *dir = data->dir->d_inode;
1510 struct nfs_server *server = NFS_SERVER(dir);
1511 struct nfs_openargs *o_arg = &data->o_arg;
1512 struct nfs_openres *o_res = &data->o_res;
1513 int status;
1514
1515 status = nfs4_run_open_task(data, 0);
Trond Myklebust3e309912007-07-07 13:19:59 -04001516 if (status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001517 return status;
1518
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001519 if (o_arg->open_flags & O_CREAT) {
1520 update_changeattr(dir, &o_res->cinfo);
1521 nfs_post_op_update_inode(dir, o_res->dir_attr);
1522 } else
1523 nfs_refresh_inode(dir, o_res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001525 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001527 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 }
1529 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001530 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001531 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532}
1533
Trond Myklebust10afec92007-05-14 17:16:04 -04001534static int nfs4_recover_expired_lease(struct nfs_server *server)
Trond Myklebust58d97142006-01-03 09:55:24 +01001535{
David Howells7539bba2006-08-22 20:06:09 -04001536 struct nfs_client *clp = server->nfs_client;
Trond Myklebusta78cb572009-08-09 15:06:19 -04001537 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001538 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001539
Trond Myklebusta78cb572009-08-09 15:06:19 -04001540 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001541 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001542 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001543 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001544 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1545 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001546 break;
Trond Myklebust58d97142006-01-03 09:55:24 +01001547 nfs4_schedule_state_recovery(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001548 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001549 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001550 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001551}
1552
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553/*
1554 * OPEN_EXPIRED:
1555 * reclaim state on the server after a network partition.
1556 * Assumes caller holds the appropriate lock
1557 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001558static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001560 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001561 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001563 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1564 if (IS_ERR(opendata))
1565 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001566 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001567 if (ret == -ESTALE)
Trond Myklebust539cd032007-06-05 11:46:42 -04001568 d_drop(ctx->path.dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001569 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001570 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571}
1572
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001573static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001574{
Trond Myklebust539cd032007-06-05 11:46:42 -04001575 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001576 struct nfs4_exception exception = { };
1577 int err;
1578
1579 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001580 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001581 switch (err) {
1582 default:
1583 goto out;
1584 case -NFS4ERR_GRACE:
1585 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -05001586 case -EKEYEXPIRED:
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001587 nfs4_handle_exception(server, err, &exception);
1588 err = 0;
1589 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001590 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001591out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001592 return err;
1593}
1594
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1596{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001598 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
Trond Myklebust864472e2006-01-03 09:55:15 +01001600 ctx = nfs4_state_find_open_context(state);
1601 if (IS_ERR(ctx))
1602 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001603 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001604 put_nfs_open_context(ctx);
1605 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606}
1607
1608/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001609 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1610 * fields corresponding to attributes that were used to store the verifier.
1611 * Make sure we clobber those fields in the later setattr call
1612 */
1613static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1614{
1615 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1616 !(sattr->ia_valid & ATTR_ATIME_SET))
1617 sattr->ia_valid |= ATTR_ATIME;
1618
1619 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1620 !(sattr->ia_valid & ATTR_MTIME_SET))
1621 sattr->ia_valid |= ATTR_MTIME;
1622}
1623
1624/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001625 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001627static 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 -07001628{
1629 struct nfs4_state_owner *sp;
1630 struct nfs4_state *state = NULL;
1631 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001632 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001633 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
1635 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 status = -ENOMEM;
1637 if (!(sp = nfs4_get_state_owner(server, cred))) {
1638 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1639 goto out_err;
1640 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001641 status = nfs4_recover_expired_lease(server);
1642 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001643 goto err_put_state_owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001644 if (path->dentry->d_inode != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001645 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001646 status = -ENOMEM;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001647 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001648 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001649 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
Trond Myklebustaac00a82007-07-05 19:02:21 -04001651 if (path->dentry->d_inode != NULL)
1652 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1653
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001654 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001656 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001658 if (opendata->o_arg.open_flags & O_EXCL)
1659 nfs4_exclusive_attrset(opendata, sattr);
1660
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001661 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001662 status = PTR_ERR(state);
1663 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001664 goto err_opendata_put;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001665 if ((opendata->o_res.rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) != 0)
1666 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001667 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 *res = state;
1670 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001671err_opendata_put:
1672 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001673err_put_state_owner:
1674 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 *res = NULL;
1677 return status;
1678}
1679
1680
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001681static 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 -07001682{
1683 struct nfs4_exception exception = { };
1684 struct nfs4_state *res;
1685 int status;
1686
1687 do {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001688 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 if (status == 0)
1690 break;
1691 /* NOTE: BAD_SEQID means the server and client disagree about the
1692 * book-keeping w.r.t. state-changing operations
1693 * (OPEN/CLOSE/LOCK/LOCKU...)
1694 * It is actually a sign of a bug on the client or on the server.
1695 *
1696 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001697 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 * have unhashed the old state_owner for us, and that we can
1699 * therefore safely retry using a new one. We should still warn
1700 * the user though...
1701 */
1702 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001703 printk(KERN_WARNING "NFS: v4 server %s "
1704 " returned a bad sequence-id error!\n",
1705 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 exception.retry = 1;
1707 continue;
1708 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001709 /*
1710 * BAD_STATEID on OPEN means that the server cancelled our
1711 * state before it received the OPEN_CONFIRM.
1712 * Recover by retrying the request as per the discussion
1713 * on Page 181 of RFC3530.
1714 */
1715 if (status == -NFS4ERR_BAD_STATEID) {
1716 exception.retry = 1;
1717 continue;
1718 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001719 if (status == -EAGAIN) {
1720 /* We must have found a delegation */
1721 exception.retry = 1;
1722 continue;
1723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1725 status, &exception));
1726 } while (exception.retry);
1727 return res;
1728}
1729
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001730static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1731 struct nfs_fattr *fattr, struct iattr *sattr,
1732 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001734 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001736 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 .iap = sattr,
1738 .server = server,
1739 .bitmask = server->attr_bitmask,
1740 };
1741 struct nfs_setattrres res = {
1742 .fattr = fattr,
1743 .server = server,
1744 };
1745 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001746 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1747 .rpc_argp = &arg,
1748 .rpc_resp = &res,
1749 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001751 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001752 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
Trond Myklebust0e574af2005-10-27 22:12:38 -04001754 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001756 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1757 /* Use that stateid */
1758 } else if (state != NULL) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001759 nfs4_copy_stateid(&arg.stateid, state, current->files);
1760 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1762
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001763 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001764 if (status == 0 && state != NULL)
1765 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001766 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767}
1768
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001769static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1770 struct nfs_fattr *fattr, struct iattr *sattr,
1771 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001773 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 struct nfs4_exception exception = { };
1775 int err;
1776 do {
1777 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001778 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 &exception);
1780 } while (exception.retry);
1781 return err;
1782}
1783
1784struct nfs4_closedata {
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001785 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 struct inode *inode;
1787 struct nfs4_state *state;
1788 struct nfs_closeargs arg;
1789 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001790 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001791 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792};
1793
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001794static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001795{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001796 struct nfs4_closedata *calldata = data;
1797 struct nfs4_state_owner *sp = calldata->state->owner;
Trond Myklebust95121352005-10-18 14:20:12 -07001798
1799 nfs4_put_open_state(calldata->state);
1800 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001801 nfs4_put_state_owner(sp);
Jan Blunck31f31db12008-05-02 13:42:45 -07001802 path_put(&calldata->path);
Trond Myklebust95121352005-10-18 14:20:12 -07001803 kfree(calldata);
1804}
1805
Trond Myklebust88069f72009-12-08 08:33:16 -05001806static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1807 fmode_t fmode)
1808{
1809 spin_lock(&state->owner->so_lock);
1810 if (!(fmode & FMODE_READ))
1811 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1812 if (!(fmode & FMODE_WRITE))
1813 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1814 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1815 spin_unlock(&state->owner->so_lock);
1816}
1817
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001818static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001820 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 struct nfs_server *server = NFS_SERVER(calldata->inode);
1823
Andy Adamson19ddab02009-04-01 09:22:20 -04001824 nfs4_sequence_done(server, &calldata->res.seq_res, task->tk_status);
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001825 if (RPC_ASSASSINATED(task))
1826 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 /* hmm. we are done with the inode, and in the process of freeing
1828 * the state_owner. we keep this around to process errors
1829 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 switch (task->tk_status) {
1831 case 0:
Trond Myklebust45328c32007-07-26 17:47:34 -04001832 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001833 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05001834 nfs4_close_clear_stateid_flags(state,
1835 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 break;
1837 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001838 case -NFS4ERR_OLD_STATEID:
1839 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001841 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001842 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05001844 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1845 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 }
Trond Myklebust72211db2009-12-15 14:47:36 -05001847 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04001848 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849}
1850
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001851static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001853 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001854 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05001855 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07001856
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001857 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001858 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001859
Trond Myklebust88069f72009-12-08 08:33:16 -05001860 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1861 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05001862 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001863 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01001864 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001865 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001866 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
1867 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1868 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04001869 }
1870 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001871 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
1872 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1873 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04001874 }
Trond Myklebuste7616922006-01-03 09:55:13 +01001875 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05001876 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05001877
1878 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001879 /* Note: exit _without_ calling nfs4_close_done */
1880 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07001881 return;
1882 }
Trond Myklebust88069f72009-12-08 08:33:16 -05001883
1884 if (calldata->arg.fmode == 0)
1885 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
1886
Trond Myklebust516a6af2005-10-27 22:12:41 -04001887 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001888 calldata->timestamp = jiffies;
Andy Adamson19ddab02009-04-01 09:22:20 -04001889 if (nfs4_setup_sequence((NFS_SERVER(calldata->inode))->nfs_client,
1890 &calldata->arg.seq_args, &calldata->res.seq_res,
1891 1, task))
1892 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001893 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894}
1895
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001896static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001897 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001898 .rpc_call_done = nfs4_close_done,
1899 .rpc_release = nfs4_free_closedata,
1900};
1901
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902/*
1903 * It is possible for data to be read/written from a mem-mapped file
1904 * after the sys_close call (which hits the vfs layer as a flush).
1905 * This means that we can't safely call nfsv4 close on a file until
1906 * the inode is cleared. This in turn means that we are not good
1907 * NFSv4 citizens - we do not indicate to the server to update the file's
1908 * share state even when we are done with one of the three share
1909 * stateid's in the inode.
1910 *
1911 * NOTE: Caller must be holding the sp->so_owner semaphore!
1912 */
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001913int nfs4_do_close(struct path *path, struct nfs4_state *state, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001915 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04001917 struct nfs4_state_owner *sp = state->owner;
1918 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001919 struct rpc_message msg = {
1920 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1921 .rpc_cred = state->owner->so_cred,
1922 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001923 struct rpc_task_setup task_setup_data = {
1924 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001925 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001926 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05001927 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001928 .flags = RPC_TASK_ASYNC,
1929 };
Trond Myklebust95121352005-10-18 14:20:12 -07001930 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
Andy Adamson19ddab02009-04-01 09:22:20 -04001932 calldata = kzalloc(sizeof(*calldata), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07001934 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001935 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001937 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04001938 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 /* Serialization for the sequence id */
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001940 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001941 if (calldata->arg.seqid == NULL)
1942 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001943 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04001944 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001945 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04001946 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001947 calldata->res.server = server;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04001948 calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001949 calldata->path.mnt = mntget(path->mnt);
1950 calldata->path.dentry = dget(path->dentry);
Trond Myklebust95121352005-10-18 14:20:12 -07001951
Trond Myklebust5138fde2007-07-14 15:40:01 -04001952 msg.rpc_argp = &calldata->arg,
1953 msg.rpc_resp = &calldata->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001954 task_setup_data.callback_data = calldata;
1955 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001956 if (IS_ERR(task))
1957 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001958 status = 0;
1959 if (wait)
1960 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001961 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001962 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07001963out_free_calldata:
1964 kfree(calldata);
1965out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04001966 nfs4_put_open_state(state);
1967 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07001968 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969}
1970
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001971static 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 -07001972{
1973 struct file *filp;
Trond Myklebust1b45c462007-07-03 13:04:56 -04001974 int ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001975
Trond Myklebust1b45c462007-07-03 13:04:56 -04001976 /* If the open_intent is for execute, we have an extra check to make */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001977 if (fmode & FMODE_EXEC) {
Trond Myklebustaf22f942007-08-10 17:45:10 -04001978 ret = nfs_may_open(state->inode,
Trond Myklebust1b45c462007-07-03 13:04:56 -04001979 state->owner->so_cred,
1980 nd->intent.open.flags);
1981 if (ret < 0)
1982 goto out_close;
1983 }
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001984 filp = lookup_instantiate_filp(nd, path->dentry, NULL);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001985 if (!IS_ERR(filp)) {
1986 struct nfs_open_context *ctx;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04001987 ctx = nfs_file_open_context(filp);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001988 ctx->state = state;
Trond Myklebust95cf9592006-04-18 13:14:06 -04001989 return 0;
1990 }
Trond Myklebust1b45c462007-07-03 13:04:56 -04001991 ret = PTR_ERR(filp);
1992out_close:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001993 nfs4_close_sync(path, state, fmode & (FMODE_READ|FMODE_WRITE));
Trond Myklebust1b45c462007-07-03 13:04:56 -04001994 return ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001995}
1996
1997struct dentry *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1999{
Trond Myklebustad389da2007-06-05 12:30:00 -04002000 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08002001 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04002002 .dentry = dentry,
2003 };
Jan Blunck4ac91372008-02-14 19:34:32 -08002004 struct dentry *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 struct iattr attr;
2006 struct rpc_cred *cred;
2007 struct nfs4_state *state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002008 struct dentry *res;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002009 fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
2011 if (nd->flags & LOOKUP_CREATE) {
2012 attr.ia_mode = nd->intent.open.create_mode;
2013 attr.ia_valid = ATTR_MODE;
2014 if (!IS_POSIXACL(dir))
Al Viroce3b0f82009-03-29 19:08:22 -04002015 attr.ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 } else {
2017 attr.ia_valid = 0;
2018 BUG_ON(nd->intent.open.flags & O_CREAT);
2019 }
2020
Trond Myklebust98a8e322008-03-12 12:25:28 -04002021 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 if (IS_ERR(cred))
Trond Myklebust02a913a2005-10-18 14:20:17 -07002023 return (struct dentry *)cred;
Trond Myklebust565277f2007-10-15 18:17:53 -04002024 parent = dentry->d_parent;
2025 /* Protect against concurrent sillydeletes */
2026 nfs_block_sillyrename(parent);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002027 state = nfs4_do_open(dir, &path, fmode, nd->intent.open.flags, &attr, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002029 if (IS_ERR(state)) {
Trond Myklebustd75340c2007-10-01 21:42:01 -04002030 if (PTR_ERR(state) == -ENOENT) {
Trond Myklebust02a913a2005-10-18 14:20:17 -07002031 d_add(dentry, NULL);
Trond Myklebustd75340c2007-10-01 21:42:01 -04002032 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2033 }
Trond Myklebust565277f2007-10-15 18:17:53 -04002034 nfs_unblock_sillyrename(parent);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002035 return (struct dentry *)state;
2036 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002037 res = d_add_unique(dentry, igrab(state->inode));
Trond Myklebust02a913a2005-10-18 14:20:17 -07002038 if (res != NULL)
Trond Myklebustdeee9362007-08-27 11:33:00 -04002039 path.dentry = res;
Trond Myklebustd75340c2007-10-01 21:42:01 -04002040 nfs_set_verifier(path.dentry, nfs_save_change_attribute(dir));
Trond Myklebust565277f2007-10-15 18:17:53 -04002041 nfs_unblock_sillyrename(parent);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002042 nfs4_intent_set_file(nd, &path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002043 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044}
2045
2046int
Trond Myklebust02a913a2005-10-18 14:20:17 -07002047nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048{
Trond Myklebustad389da2007-06-05 12:30:00 -04002049 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08002050 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04002051 .dentry = dentry,
2052 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 struct rpc_cred *cred;
2054 struct nfs4_state *state;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002055 fmode_t fmode = openflags & (FMODE_READ | FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056
Trond Myklebust98a8e322008-03-12 12:25:28 -04002057 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 if (IS_ERR(cred))
2059 return PTR_ERR(cred);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002060 state = nfs4_do_open(dir, &path, fmode, openflags, NULL, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002062 if (IS_ERR(state)) {
2063 switch (PTR_ERR(state)) {
2064 case -EPERM:
2065 case -EACCES:
2066 case -EDQUOT:
2067 case -ENOSPC:
2068 case -EROFS:
2069 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
2070 return 1;
Chuck Leverb87c0ad2006-10-19 23:28:42 -07002071 default:
2072 goto out_drop;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002073 }
Trond Myklebust02a913a2005-10-18 14:20:17 -07002074 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002075 if (state->inode == dentry->d_inode) {
Trond Myklebustd75340c2007-10-01 21:42:01 -04002076 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002077 nfs4_intent_set_file(nd, &path, state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 return 1;
2079 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002080 nfs4_close_sync(&path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002081out_drop:
2082 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 return 0;
2084}
2085
Trond Myklebust1185a552009-12-03 15:54:02 -05002086static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002087{
2088 if (ctx->state == NULL)
2089 return;
2090 if (is_sync)
2091 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
2092 else
2093 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
2094}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095
2096static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2097{
Benny Halevy43652ad2009-04-01 09:21:54 -04002098 struct nfs4_server_caps_arg args = {
2099 .fhandle = fhandle,
2100 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 struct nfs4_server_caps_res res = {};
2102 struct rpc_message msg = {
2103 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002104 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 .rpc_resp = &res,
2106 };
2107 int status;
2108
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002109 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 if (status == 0) {
2111 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002112 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2113 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2114 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2115 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2116 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2118 server->caps |= NFS_CAP_ACLS;
2119 if (res.has_links != 0)
2120 server->caps |= NFS_CAP_HARDLINKS;
2121 if (res.has_symlinks != 0)
2122 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002123 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2124 server->caps |= NFS_CAP_FILEID;
2125 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2126 server->caps |= NFS_CAP_MODE;
2127 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2128 server->caps |= NFS_CAP_NLINK;
2129 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2130 server->caps |= NFS_CAP_OWNER;
2131 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2132 server->caps |= NFS_CAP_OWNER_GROUP;
2133 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2134 server->caps |= NFS_CAP_ATIME;
2135 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2136 server->caps |= NFS_CAP_CTIME;
2137 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2138 server->caps |= NFS_CAP_MTIME;
2139
Trond Myklebusta65318b2009-03-11 14:10:28 -04002140 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2141 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2142 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 server->acl_bitmask = res.acl_bitmask;
2144 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002145
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 return status;
2147}
2148
Trond Myklebust55a97592006-06-09 09:34:19 -04002149int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150{
2151 struct nfs4_exception exception = { };
2152 int err;
2153 do {
2154 err = nfs4_handle_exception(server,
2155 _nfs4_server_capabilities(server, fhandle),
2156 &exception);
2157 } while (exception.retry);
2158 return err;
2159}
2160
2161static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2162 struct nfs_fsinfo *info)
2163{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 struct nfs4_lookup_root_arg args = {
2165 .bitmask = nfs4_fattr_bitmap,
2166 };
2167 struct nfs4_lookup_res res = {
2168 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002169 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 .fh = fhandle,
2171 };
2172 struct rpc_message msg = {
2173 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2174 .rpc_argp = &args,
2175 .rpc_resp = &res,
2176 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002177
Trond Myklebust0e574af2005-10-27 22:12:38 -04002178 nfs_fattr_init(info->fattr);
Trond Myklebustfccba802009-07-21 16:48:07 -04002179 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180}
2181
2182static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2183 struct nfs_fsinfo *info)
2184{
2185 struct nfs4_exception exception = { };
2186 int err;
2187 do {
2188 err = nfs4_handle_exception(server,
2189 _nfs4_lookup_root(server, fhandle, info),
2190 &exception);
2191 } while (exception.retry);
2192 return err;
2193}
2194
David Howells54ceac42006-08-22 20:06:13 -04002195/*
2196 * get the file handle for the "/" directory on the server
2197 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002199 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 int status;
2202
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 status = nfs4_lookup_root(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 if (status == 0)
2205 status = nfs4_server_capabilities(server, fhandle);
2206 if (status == 0)
2207 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002208 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209}
2210
Manoj Naik6b97fd32006-06-09 09:34:29 -04002211/*
2212 * Get locations and (maybe) other attributes of a referral.
2213 * Note that we'll actually follow the referral later when
2214 * we detect fsid mismatch in inode revalidation
2215 */
Trond Myklebust56659e92007-07-17 21:52:39 -04002216static 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 -04002217{
2218 int status = -ENOMEM;
2219 struct page *page = NULL;
2220 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002221
2222 page = alloc_page(GFP_KERNEL);
2223 if (page == NULL)
2224 goto out;
2225 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2226 if (locations == NULL)
2227 goto out;
2228
Trond Myklebustc228fd32007-01-13 02:28:11 -05002229 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002230 if (status != 0)
2231 goto out;
2232 /* Make sure server returned a different fsid for the referral */
2233 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -07002234 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 -04002235 status = -EIO;
2236 goto out;
2237 }
2238
2239 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2240 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
2241 if (!fattr->mode)
2242 fattr->mode = S_IFDIR;
2243 memset(fhandle, 0, sizeof(struct nfs_fh));
2244out:
2245 if (page)
2246 __free_page(page);
2247 if (locations)
2248 kfree(locations);
2249 return status;
2250}
2251
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2253{
2254 struct nfs4_getattr_arg args = {
2255 .fh = fhandle,
2256 .bitmask = server->attr_bitmask,
2257 };
2258 struct nfs4_getattr_res res = {
2259 .fattr = fattr,
2260 .server = server,
2261 };
2262 struct rpc_message msg = {
2263 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2264 .rpc_argp = &args,
2265 .rpc_resp = &res,
2266 };
2267
Trond Myklebust0e574af2005-10-27 22:12:38 -04002268 nfs_fattr_init(fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002269 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270}
2271
2272static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2273{
2274 struct nfs4_exception exception = { };
2275 int err;
2276 do {
2277 err = nfs4_handle_exception(server,
2278 _nfs4_proc_getattr(server, fhandle, fattr),
2279 &exception);
2280 } while (exception.retry);
2281 return err;
2282}
2283
2284/*
2285 * The file is not closed if it is opened due to the a request to change
2286 * the size of the file. The open call will not be needed once the
2287 * VFS layer lookup-intents are implemented.
2288 *
2289 * Close is called when the inode is destroyed.
2290 * If we haven't opened the file for O_WRONLY, we
2291 * need to in the size_change case to obtain a stateid.
2292 *
2293 * Got race?
2294 * Because OPEN is always done by name in nfsv4, it is
2295 * possible that we opened a different file by the same
2296 * name. We can recognize this race condition, but we
2297 * can't do anything about it besides returning an error.
2298 *
2299 * This will be fixed with VFS changes (lookup-intent).
2300 */
2301static int
2302nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2303 struct iattr *sattr)
2304{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002305 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002306 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002307 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 int status;
2309
Trond Myklebust0e574af2005-10-27 22:12:38 -04002310 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311
Trond Myklebustd5308382005-11-04 15:33:38 -05002312 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002313 if (sattr->ia_valid & ATTR_FILE) {
2314 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002315
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002316 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002317 if (ctx) {
2318 cred = ctx->cred;
2319 state = ctx->state;
2320 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002321 }
2322
2323 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002324 if (status == 0)
2325 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 return status;
2327}
2328
Trond Myklebust56659e92007-07-17 21:52:39 -04002329static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
2330 const struct qstr *name, struct nfs_fh *fhandle,
David Howells2b3de442006-08-22 20:06:09 -04002331 struct nfs_fattr *fattr)
2332{
2333 int status;
2334 struct nfs4_lookup_arg args = {
2335 .bitmask = server->attr_bitmask,
2336 .dir_fh = dirfh,
2337 .name = name,
2338 };
2339 struct nfs4_lookup_res res = {
2340 .server = server,
2341 .fattr = fattr,
2342 .fh = fhandle,
2343 };
2344 struct rpc_message msg = {
2345 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2346 .rpc_argp = &args,
2347 .rpc_resp = &res,
2348 };
2349
2350 nfs_fattr_init(fattr);
2351
2352 dprintk("NFS call lookupfh %s\n", name->name);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002353 status = nfs4_call_sync(server, &msg, &args, &res, 0);
David Howells2b3de442006-08-22 20:06:09 -04002354 dprintk("NFS reply lookupfh: %d\n", status);
David Howells2b3de442006-08-22 20:06:09 -04002355 return status;
2356}
2357
2358static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2359 struct qstr *name, struct nfs_fh *fhandle,
2360 struct nfs_fattr *fattr)
2361{
2362 struct nfs4_exception exception = { };
2363 int err;
2364 do {
Trond Myklebust4e56e082007-07-01 18:13:52 -04002365 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
2366 /* FIXME: !!!! */
2367 if (err == -NFS4ERR_MOVED) {
2368 err = -EREMOTE;
2369 break;
2370 }
2371 err = nfs4_handle_exception(server, err, &exception);
David Howells2b3de442006-08-22 20:06:09 -04002372 } while (exception.retry);
2373 return err;
2374}
2375
Trond Myklebust56659e92007-07-17 21:52:39 -04002376static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2378{
Trond Myklebust4e56e082007-07-01 18:13:52 -04002379 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380
2381 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust4e56e082007-07-01 18:13:52 -04002382 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002383 if (status == -NFS4ERR_MOVED)
2384 status = nfs4_get_referral(dir, name, fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 dprintk("NFS reply lookup: %d\n", status);
2386 return status;
2387}
2388
2389static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2390{
2391 struct nfs4_exception exception = { };
2392 int err;
2393 do {
2394 err = nfs4_handle_exception(NFS_SERVER(dir),
2395 _nfs4_proc_lookup(dir, name, fhandle, fattr),
2396 &exception);
2397 } while (exception.retry);
2398 return err;
2399}
2400
2401static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2402{
Trond Myklebust76b32992007-08-10 17:45:11 -04002403 struct nfs_server *server = NFS_SERVER(inode);
2404 struct nfs_fattr fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 struct nfs4_accessargs args = {
2406 .fh = NFS_FH(inode),
Trond Myklebust76b32992007-08-10 17:45:11 -04002407 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002409 struct nfs4_accessres res = {
2410 .server = server,
2411 .fattr = &fattr,
2412 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 struct rpc_message msg = {
2414 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2415 .rpc_argp = &args,
2416 .rpc_resp = &res,
2417 .rpc_cred = entry->cred,
2418 };
2419 int mode = entry->mask;
2420 int status;
2421
2422 /*
2423 * Determine which access bits we want to ask for...
2424 */
2425 if (mode & MAY_READ)
2426 args.access |= NFS4_ACCESS_READ;
2427 if (S_ISDIR(inode->i_mode)) {
2428 if (mode & MAY_WRITE)
2429 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2430 if (mode & MAY_EXEC)
2431 args.access |= NFS4_ACCESS_LOOKUP;
2432 } else {
2433 if (mode & MAY_WRITE)
2434 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2435 if (mode & MAY_EXEC)
2436 args.access |= NFS4_ACCESS_EXECUTE;
2437 }
Trond Myklebust76b32992007-08-10 17:45:11 -04002438 nfs_fattr_init(&fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002439 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 if (!status) {
2441 entry->mask = 0;
2442 if (res.access & NFS4_ACCESS_READ)
2443 entry->mask |= MAY_READ;
2444 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2445 entry->mask |= MAY_WRITE;
2446 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2447 entry->mask |= MAY_EXEC;
Trond Myklebust76b32992007-08-10 17:45:11 -04002448 nfs_refresh_inode(inode, &fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 }
2450 return status;
2451}
2452
2453static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2454{
2455 struct nfs4_exception exception = { };
2456 int err;
2457 do {
2458 err = nfs4_handle_exception(NFS_SERVER(inode),
2459 _nfs4_proc_access(inode, entry),
2460 &exception);
2461 } while (exception.retry);
2462 return err;
2463}
2464
2465/*
2466 * TODO: For the time being, we don't try to get any attributes
2467 * along with any of the zero-copy operations READ, READDIR,
2468 * READLINK, WRITE.
2469 *
2470 * In the case of the first three, we want to put the GETATTR
2471 * after the read-type operation -- this is because it is hard
2472 * to predict the length of a GETATTR response in v4, and thus
2473 * align the READ data correctly. This means that the GETATTR
2474 * may end up partially falling into the page cache, and we should
2475 * shift it into the 'tail' of the xdr_buf before processing.
2476 * To do this efficiently, we need to know the total length
2477 * of data received, which doesn't seem to be available outside
2478 * of the RPC layer.
2479 *
2480 * In the case of WRITE, we also want to put the GETATTR after
2481 * the operation -- in this case because we want to make sure
2482 * we get the post-operation mtime and size. This means that
2483 * we can't use xdr_encode_pages() as written: we need a variant
2484 * of it which would leave room in the 'tail' iovec.
2485 *
2486 * Both of these changes to the XDR layer would in fact be quite
2487 * minor, but I decided to leave them for a subsequent patch.
2488 */
2489static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2490 unsigned int pgbase, unsigned int pglen)
2491{
2492 struct nfs4_readlink args = {
2493 .fh = NFS_FH(inode),
2494 .pgbase = pgbase,
2495 .pglen = pglen,
2496 .pages = &page,
2497 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002498 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 struct rpc_message msg = {
2500 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2501 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002502 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 };
2504
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002505 return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506}
2507
2508static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2509 unsigned int pgbase, unsigned int pglen)
2510{
2511 struct nfs4_exception exception = { };
2512 int err;
2513 do {
2514 err = nfs4_handle_exception(NFS_SERVER(inode),
2515 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2516 &exception);
2517 } while (exception.retry);
2518 return err;
2519}
2520
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521/*
2522 * Got race?
2523 * We will need to arrange for the VFS layer to provide an atomic open.
2524 * Until then, this create/open method is prone to inefficiency and race
2525 * conditions due to the lookup, create, and open VFS calls from sys_open()
2526 * placed on the wire.
2527 *
2528 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2529 * The file will be opened again in the subsequent VFS open call
2530 * (nfs4_proc_file_open).
2531 *
2532 * The open for read will just hang around to be used by any process that
2533 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2534 */
2535
2536static int
2537nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebust02a913a2005-10-18 14:20:17 -07002538 int flags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539{
Trond Myklebustad389da2007-06-05 12:30:00 -04002540 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08002541 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04002542 .dentry = dentry,
2543 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 struct nfs4_state *state;
2545 struct rpc_cred *cred;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002546 fmode_t fmode = flags & (FMODE_READ | FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 int status = 0;
2548
Trond Myklebust98a8e322008-03-12 12:25:28 -04002549 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 if (IS_ERR(cred)) {
2551 status = PTR_ERR(cred);
2552 goto out;
2553 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002554 state = nfs4_do_open(dir, &path, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002555 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 if (IS_ERR(state)) {
2557 status = PTR_ERR(state);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002558 goto out_putcred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002560 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002561 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 if (flags & O_EXCL) {
2563 struct nfs_fattr fattr;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002564 status = nfs4_do_setattr(state->inode, cred, &fattr, sattr, state);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002565 if (status == 0)
Trond Myklebust65e43082005-08-16 11:49:44 -04002566 nfs_setattr_update_inode(state->inode, sattr);
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002567 nfs_post_op_update_inode(state->inode, &fattr);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002568 }
Trond Myklebustad389da2007-06-05 12:30:00 -04002569 if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002570 status = nfs4_intent_set_file(nd, &path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002571 else
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002572 nfs4_close_sync(&path, state, fmode);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002573out_putcred:
2574 put_rpccred(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575out:
2576 return status;
2577}
2578
2579static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2580{
Trond Myklebust16e42952005-10-27 22:12:44 -04002581 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002582 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002584 .name.len = name->len,
2585 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002586 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002588 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002589 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002590 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002592 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2593 .rpc_argp = &args,
2594 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 };
2596 int status;
2597
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002598 nfs_fattr_init(&res.dir_attr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002599 status = nfs4_call_sync(server, &msg, &args, &res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002600 if (status == 0) {
2601 update_changeattr(dir, &res.cinfo);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002602 nfs_post_op_update_inode(dir, &res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 return status;
2605}
2606
2607static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2608{
2609 struct nfs4_exception exception = { };
2610 int err;
2611 do {
2612 err = nfs4_handle_exception(NFS_SERVER(dir),
2613 _nfs4_proc_remove(dir, name),
2614 &exception);
2615 } while (exception.retry);
2616 return err;
2617}
2618
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002619static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002621 struct nfs_server *server = NFS_SERVER(dir);
2622 struct nfs_removeargs *args = msg->rpc_argp;
2623 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624
Trond Myklebusta65318b2009-03-11 14:10:28 -04002625 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002626 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628}
2629
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002630static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002632 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2633
Andy Adamson472cfbd2009-04-01 09:22:24 -04002634 nfs4_sequence_done(res->server, &res->seq_res, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002635 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002636 return 0;
2637 update_changeattr(dir, &res->cinfo);
2638 nfs_post_op_update_inode(dir, &res->dir_attr);
2639 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640}
2641
2642static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2643 struct inode *new_dir, struct qstr *new_name)
2644{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002645 struct nfs_server *server = NFS_SERVER(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 struct nfs4_rename_arg arg = {
2647 .old_dir = NFS_FH(old_dir),
2648 .new_dir = NFS_FH(new_dir),
2649 .old_name = old_name,
2650 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002651 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 };
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002653 struct nfs_fattr old_fattr, new_fattr;
2654 struct nfs4_rename_res res = {
2655 .server = server,
2656 .old_fattr = &old_fattr,
2657 .new_fattr = &new_fattr,
2658 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 struct rpc_message msg = {
2660 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2661 .rpc_argp = &arg,
2662 .rpc_resp = &res,
2663 };
2664 int status;
2665
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002666 nfs_fattr_init(res.old_fattr);
2667 nfs_fattr_init(res.new_fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002668 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669
2670 if (!status) {
2671 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002672 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002674 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 }
2676 return status;
2677}
2678
2679static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2680 struct inode *new_dir, struct qstr *new_name)
2681{
2682 struct nfs4_exception exception = { };
2683 int err;
2684 do {
2685 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2686 _nfs4_proc_rename(old_dir, old_name,
2687 new_dir, new_name),
2688 &exception);
2689 } while (exception.retry);
2690 return err;
2691}
2692
2693static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2694{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002695 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 struct nfs4_link_arg arg = {
2697 .fh = NFS_FH(inode),
2698 .dir_fh = NFS_FH(dir),
2699 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002700 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002702 struct nfs_fattr fattr, dir_attr;
2703 struct nfs4_link_res res = {
2704 .server = server,
2705 .fattr = &fattr,
2706 .dir_attr = &dir_attr,
2707 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 struct rpc_message msg = {
2709 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2710 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002711 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 };
2713 int status;
2714
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002715 nfs_fattr_init(res.fattr);
2716 nfs_fattr_init(res.dir_attr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002717 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002718 if (!status) {
2719 update_changeattr(dir, &res.cinfo);
2720 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002721 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723
2724 return status;
2725}
2726
2727static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2728{
2729 struct nfs4_exception exception = { };
2730 int err;
2731 do {
2732 err = nfs4_handle_exception(NFS_SERVER(inode),
2733 _nfs4_proc_link(inode, dir, name),
2734 &exception);
2735 } while (exception.retry);
2736 return err;
2737}
2738
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002739struct nfs4_createdata {
2740 struct rpc_message msg;
2741 struct nfs4_create_arg arg;
2742 struct nfs4_create_res res;
2743 struct nfs_fh fh;
2744 struct nfs_fattr fattr;
2745 struct nfs_fattr dir_fattr;
2746};
2747
2748static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2749 struct qstr *name, struct iattr *sattr, u32 ftype)
2750{
2751 struct nfs4_createdata *data;
2752
2753 data = kzalloc(sizeof(*data), GFP_KERNEL);
2754 if (data != NULL) {
2755 struct nfs_server *server = NFS_SERVER(dir);
2756
2757 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2758 data->msg.rpc_argp = &data->arg;
2759 data->msg.rpc_resp = &data->res;
2760 data->arg.dir_fh = NFS_FH(dir);
2761 data->arg.server = server;
2762 data->arg.name = name;
2763 data->arg.attrs = sattr;
2764 data->arg.ftype = ftype;
2765 data->arg.bitmask = server->attr_bitmask;
2766 data->res.server = server;
2767 data->res.fh = &data->fh;
2768 data->res.fattr = &data->fattr;
2769 data->res.dir_fattr = &data->dir_fattr;
2770 nfs_fattr_init(data->res.fattr);
2771 nfs_fattr_init(data->res.dir_fattr);
2772 }
2773 return data;
2774}
2775
2776static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2777{
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002778 int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
2779 &data->arg, &data->res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002780 if (status == 0) {
2781 update_changeattr(dir, &data->res.dir_cinfo);
2782 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2783 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2784 }
2785 return status;
2786}
2787
2788static void nfs4_free_createdata(struct nfs4_createdata *data)
2789{
2790 kfree(data);
2791}
2792
Chuck Lever4f390c12006-08-22 20:06:22 -04002793static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002794 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002796 struct nfs4_createdata *data;
2797 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798
Chuck Lever94a6d752006-08-22 20:06:23 -04002799 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002800 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002801
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002802 status = -ENOMEM;
2803 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2804 if (data == NULL)
2805 goto out;
2806
2807 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2808 data->arg.u.symlink.pages = &page;
2809 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002811 status = nfs4_do_create(dir, dentry, data);
2812
2813 nfs4_free_createdata(data);
2814out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 return status;
2816}
2817
Chuck Lever4f390c12006-08-22 20:06:22 -04002818static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002819 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820{
2821 struct nfs4_exception exception = { };
2822 int err;
2823 do {
2824 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002825 _nfs4_proc_symlink(dir, dentry, page,
2826 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 &exception);
2828 } while (exception.retry);
2829 return err;
2830}
2831
2832static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2833 struct iattr *sattr)
2834{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002835 struct nfs4_createdata *data;
2836 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002838 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2839 if (data == NULL)
2840 goto out;
2841
2842 status = nfs4_do_create(dir, dentry, data);
2843
2844 nfs4_free_createdata(data);
2845out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 return status;
2847}
2848
2849static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2850 struct iattr *sattr)
2851{
2852 struct nfs4_exception exception = { };
2853 int err;
2854 do {
2855 err = nfs4_handle_exception(NFS_SERVER(dir),
2856 _nfs4_proc_mkdir(dir, dentry, sattr),
2857 &exception);
2858 } while (exception.retry);
2859 return err;
2860}
2861
2862static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2863 u64 cookie, struct page *page, unsigned int count, int plus)
2864{
2865 struct inode *dir = dentry->d_inode;
2866 struct nfs4_readdir_arg args = {
2867 .fh = NFS_FH(dir),
2868 .pages = &page,
2869 .pgbase = 0,
2870 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09002871 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 };
2873 struct nfs4_readdir_res res;
2874 struct rpc_message msg = {
2875 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2876 .rpc_argp = &args,
2877 .rpc_resp = &res,
2878 .rpc_cred = cred,
2879 };
2880 int status;
2881
Harvey Harrison3110ff82008-05-02 13:42:44 -07002882 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00002883 dentry->d_parent->d_name.name,
2884 dentry->d_name.name,
2885 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2887 res.pgbase = args.pgbase;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002888 status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 if (status == 0)
2890 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustc4812992007-09-28 17:11:45 -04002891
2892 nfs_invalidate_atime(dir);
2893
Harvey Harrison3110ff82008-05-02 13:42:44 -07002894 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 return status;
2896}
2897
2898static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2899 u64 cookie, struct page *page, unsigned int count, int plus)
2900{
2901 struct nfs4_exception exception = { };
2902 int err;
2903 do {
2904 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2905 _nfs4_proc_readdir(dentry, cred, cookie,
2906 page, count, plus),
2907 &exception);
2908 } while (exception.retry);
2909 return err;
2910}
2911
2912static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2913 struct iattr *sattr, dev_t rdev)
2914{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002915 struct nfs4_createdata *data;
2916 int mode = sattr->ia_mode;
2917 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918
2919 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2920 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002921
2922 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2923 if (data == NULL)
2924 goto out;
2925
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002927 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002929 data->arg.ftype = NF4BLK;
2930 data->arg.u.device.specdata1 = MAJOR(rdev);
2931 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 }
2933 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002934 data->arg.ftype = NF4CHR;
2935 data->arg.u.device.specdata1 = MAJOR(rdev);
2936 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002939 status = nfs4_do_create(dir, dentry, data);
2940
2941 nfs4_free_createdata(data);
2942out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 return status;
2944}
2945
2946static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2947 struct iattr *sattr, dev_t rdev)
2948{
2949 struct nfs4_exception exception = { };
2950 int err;
2951 do {
2952 err = nfs4_handle_exception(NFS_SERVER(dir),
2953 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2954 &exception);
2955 } while (exception.retry);
2956 return err;
2957}
2958
2959static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2960 struct nfs_fsstat *fsstat)
2961{
2962 struct nfs4_statfs_arg args = {
2963 .fh = fhandle,
2964 .bitmask = server->attr_bitmask,
2965 };
Benny Halevy24ad1482009-04-01 09:21:56 -04002966 struct nfs4_statfs_res res = {
2967 .fsstat = fsstat,
2968 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 struct rpc_message msg = {
2970 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2971 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04002972 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 };
2974
Trond Myklebust0e574af2005-10-27 22:12:38 -04002975 nfs_fattr_init(fsstat->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002976 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977}
2978
2979static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2980{
2981 struct nfs4_exception exception = { };
2982 int err;
2983 do {
2984 err = nfs4_handle_exception(server,
2985 _nfs4_proc_statfs(server, fhandle, fsstat),
2986 &exception);
2987 } while (exception.retry);
2988 return err;
2989}
2990
2991static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2992 struct nfs_fsinfo *fsinfo)
2993{
2994 struct nfs4_fsinfo_arg args = {
2995 .fh = fhandle,
2996 .bitmask = server->attr_bitmask,
2997 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04002998 struct nfs4_fsinfo_res res = {
2999 .fsinfo = fsinfo,
3000 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 struct rpc_message msg = {
3002 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3003 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003004 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 };
3006
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003007 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008}
3009
3010static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3011{
3012 struct nfs4_exception exception = { };
3013 int err;
3014
3015 do {
3016 err = nfs4_handle_exception(server,
3017 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3018 &exception);
3019 } while (exception.retry);
3020 return err;
3021}
3022
3023static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3024{
Trond Myklebust0e574af2005-10-27 22:12:38 -04003025 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3027}
3028
3029static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3030 struct nfs_pathconf *pathconf)
3031{
3032 struct nfs4_pathconf_arg args = {
3033 .fh = fhandle,
3034 .bitmask = server->attr_bitmask,
3035 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003036 struct nfs4_pathconf_res res = {
3037 .pathconf = pathconf,
3038 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 struct rpc_message msg = {
3040 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3041 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003042 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 };
3044
3045 /* None of the pathconf attributes are mandatory to implement */
3046 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3047 memset(pathconf, 0, sizeof(*pathconf));
3048 return 0;
3049 }
3050
Trond Myklebust0e574af2005-10-27 22:12:38 -04003051 nfs_fattr_init(pathconf->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003052 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053}
3054
3055static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3056 struct nfs_pathconf *pathconf)
3057{
3058 struct nfs4_exception exception = { };
3059 int err;
3060
3061 do {
3062 err = nfs4_handle_exception(server,
3063 _nfs4_proc_pathconf(server, fhandle, pathconf),
3064 &exception);
3065 } while (exception.retry);
3066 return err;
3067}
3068
Trond Myklebustec06c092006-03-20 13:44:27 -05003069static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070{
Trond Myklebustec06c092006-03-20 13:44:27 -05003071 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072
Andy Adamsonf11c88a2009-04-01 09:22:25 -04003073 dprintk("--> %s\n", __func__);
3074
Andy Adamsonf11c88a2009-04-01 09:22:25 -04003075 nfs4_sequence_done(server, &data->res.seq_res, task->tk_status);
3076
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003077 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003078 nfs_restart_rpc(task, server->nfs_client);
Trond Myklebustec06c092006-03-20 13:44:27 -05003079 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003081
3082 nfs_invalidate_atime(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003084 renew_lease(server, data->timestamp);
3085 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086}
3087
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003088static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 data->timestamp = jiffies;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003091 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092}
3093
Trond Myklebust788e7a82006-03-20 13:44:27 -05003094static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 struct inode *inode = data->inode;
3097
Andy Adamsondef6ed72009-04-01 09:22:26 -04003098 nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
3099 task->tk_status);
3100
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003101 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003102 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003103 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003105 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003107 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003108 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003109 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110}
3111
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003112static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003114 struct nfs_server *server = NFS_SERVER(data->inode);
3115
Trond Myklebusta65318b2009-03-11 14:10:28 -04003116 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003117 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 data->timestamp = jiffies;
3119
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003120 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121}
3122
Trond Myklebust788e7a82006-03-20 13:44:27 -05003123static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 struct inode *inode = data->inode;
3126
Andy Adamson21d9a852009-04-01 09:22:27 -04003127 nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
3128 task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003129 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003130 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003131 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003133 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003134 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135}
3136
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003137static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003139 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140
Trond Myklebusta65318b2009-03-11 14:10:28 -04003141 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003142 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003143 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144}
3145
3146/*
3147 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3148 * standalone procedure for queueing an asynchronous RENEW.
3149 */
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003150static void nfs4_renew_release(void *data)
3151{
3152 struct nfs_client *clp = data;
3153
3154 nfs4_schedule_state_renewal(clp);
3155}
3156
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003157static void nfs4_renew_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158{
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003159 struct nfs_client *clp = data;
3160 unsigned long timestamp = task->tk_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161
3162 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003163 /* Unless we're shutting down, schedule state recovery! */
3164 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
3165 nfs4_schedule_state_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 return;
3167 }
3168 spin_lock(&clp->cl_lock);
3169 if (time_before(clp->cl_last_renewal,timestamp))
3170 clp->cl_last_renewal = timestamp;
3171 spin_unlock(&clp->cl_lock);
3172}
3173
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003174static const struct rpc_call_ops nfs4_renew_ops = {
3175 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003176 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003177};
3178
David Howellsadfa6f92006-08-22 20:06:08 -04003179int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180{
3181 struct rpc_message msg = {
3182 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3183 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003184 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 };
3186
3187 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003188 &nfs4_renew_ops, clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189}
3190
David Howellsadfa6f92006-08-22 20:06:08 -04003191int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192{
3193 struct rpc_message msg = {
3194 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3195 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003196 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 };
3198 unsigned long now = jiffies;
3199 int status;
3200
3201 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3202 if (status < 0)
3203 return status;
3204 spin_lock(&clp->cl_lock);
3205 if (time_before(clp->cl_last_renewal,now))
3206 clp->cl_last_renewal = now;
3207 spin_unlock(&clp->cl_lock);
3208 return 0;
3209}
3210
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003211static inline int nfs4_server_supports_acls(struct nfs_server *server)
3212{
3213 return (server->caps & NFS_CAP_ACLS)
3214 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3215 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3216}
3217
3218/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3219 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3220 * the stack.
3221 */
3222#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3223
3224static void buf_to_pages(const void *buf, size_t buflen,
3225 struct page **pages, unsigned int *pgbase)
3226{
3227 const void *p = buf;
3228
3229 *pgbase = offset_in_page(buf);
3230 p -= *pgbase;
3231 while (p < buf + buflen) {
3232 *(pages++) = virt_to_page(p);
3233 p += PAGE_CACHE_SIZE;
3234 }
3235}
3236
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003237struct nfs4_cached_acl {
3238 int cached;
3239 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003240 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003241};
3242
3243static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003244{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003245 struct nfs_inode *nfsi = NFS_I(inode);
3246
3247 spin_lock(&inode->i_lock);
3248 kfree(nfsi->nfs4_acl);
3249 nfsi->nfs4_acl = acl;
3250 spin_unlock(&inode->i_lock);
3251}
3252
3253static void nfs4_zap_acl_attr(struct inode *inode)
3254{
3255 nfs4_set_cached_acl(inode, NULL);
3256}
3257
3258static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3259{
3260 struct nfs_inode *nfsi = NFS_I(inode);
3261 struct nfs4_cached_acl *acl;
3262 int ret = -ENOENT;
3263
3264 spin_lock(&inode->i_lock);
3265 acl = nfsi->nfs4_acl;
3266 if (acl == NULL)
3267 goto out;
3268 if (buf == NULL) /* user is just asking for length */
3269 goto out_len;
3270 if (acl->cached == 0)
3271 goto out;
3272 ret = -ERANGE; /* see getxattr(2) man page */
3273 if (acl->len > buflen)
3274 goto out;
3275 memcpy(buf, acl->data, acl->len);
3276out_len:
3277 ret = acl->len;
3278out:
3279 spin_unlock(&inode->i_lock);
3280 return ret;
3281}
3282
3283static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3284{
3285 struct nfs4_cached_acl *acl;
3286
3287 if (buf && acl_len <= PAGE_SIZE) {
3288 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3289 if (acl == NULL)
3290 goto out;
3291 acl->cached = 1;
3292 memcpy(acl->data, buf, acl_len);
3293 } else {
3294 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3295 if (acl == NULL)
3296 goto out;
3297 acl->cached = 0;
3298 }
3299 acl->len = acl_len;
3300out:
3301 nfs4_set_cached_acl(inode, acl);
3302}
3303
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003304static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003305{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003306 struct page *pages[NFS4ACL_MAXPAGES];
3307 struct nfs_getaclargs args = {
3308 .fh = NFS_FH(inode),
3309 .acl_pages = pages,
3310 .acl_len = buflen,
3311 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003312 struct nfs_getaclres res = {
3313 .acl_len = buflen,
3314 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003315 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003316 struct rpc_message msg = {
3317 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3318 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003319 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003320 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003321 struct page *localpage = NULL;
3322 int ret;
3323
3324 if (buflen < PAGE_SIZE) {
3325 /* As long as we're doing a round trip to the server anyway,
3326 * let's be prepared for a page of acl data. */
3327 localpage = alloc_page(GFP_KERNEL);
3328 resp_buf = page_address(localpage);
3329 if (localpage == NULL)
3330 return -ENOMEM;
3331 args.acl_pages[0] = localpage;
3332 args.acl_pgbase = 0;
Benny Halevy663c79b2009-04-01 09:21:59 -04003333 args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003334 } else {
3335 resp_buf = buf;
3336 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3337 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003338 ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003339 if (ret)
3340 goto out_free;
Benny Halevy663c79b2009-04-01 09:21:59 -04003341 if (res.acl_len > args.acl_len)
3342 nfs4_write_cached_acl(inode, NULL, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003343 else
Benny Halevy663c79b2009-04-01 09:21:59 -04003344 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003345 if (buf) {
3346 ret = -ERANGE;
Benny Halevy663c79b2009-04-01 09:21:59 -04003347 if (res.acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003348 goto out_free;
3349 if (localpage)
Benny Halevy663c79b2009-04-01 09:21:59 -04003350 memcpy(buf, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003351 }
Benny Halevy663c79b2009-04-01 09:21:59 -04003352 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003353out_free:
3354 if (localpage)
3355 __free_page(localpage);
3356 return ret;
3357}
3358
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003359static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3360{
3361 struct nfs4_exception exception = { };
3362 ssize_t ret;
3363 do {
3364 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3365 if (ret >= 0)
3366 break;
3367 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3368 } while (exception.retry);
3369 return ret;
3370}
3371
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003372static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3373{
3374 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003375 int ret;
3376
3377 if (!nfs4_server_supports_acls(server))
3378 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003379 ret = nfs_revalidate_inode(server, inode);
3380 if (ret < 0)
3381 return ret;
3382 ret = nfs4_read_cached_acl(inode, buf, buflen);
3383 if (ret != -ENOENT)
3384 return ret;
3385 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003386}
3387
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003388static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003389{
3390 struct nfs_server *server = NFS_SERVER(inode);
3391 struct page *pages[NFS4ACL_MAXPAGES];
3392 struct nfs_setaclargs arg = {
3393 .fh = NFS_FH(inode),
3394 .acl_pages = pages,
3395 .acl_len = buflen,
3396 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003397 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003398 struct rpc_message msg = {
3399 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3400 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003401 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003402 };
3403 int ret;
3404
3405 if (!nfs4_server_supports_acls(server))
3406 return -EOPNOTSUPP;
Trond Myklebust642ac542005-10-18 14:20:19 -07003407 nfs_inode_return_delegation(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003408 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003409 ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003410 nfs_access_zap_cache(inode);
3411 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003412 return ret;
3413}
3414
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003415static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3416{
3417 struct nfs4_exception exception = { };
3418 int err;
3419 do {
3420 err = nfs4_handle_exception(NFS_SERVER(inode),
3421 __nfs4_proc_set_acl(inode, buf, buflen),
3422 &exception);
3423 } while (exception.retry);
3424 return err;
3425}
3426
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427static int
Andy Adamson8328d592009-04-01 09:22:35 -04003428_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 -07003429{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 if (!clp || task->tk_status >= 0)
3431 return 0;
3432 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003433 case -NFS4ERR_ADMIN_REVOKED:
3434 case -NFS4ERR_BAD_STATEID:
3435 case -NFS4ERR_OPENMODE:
3436 if (state == NULL)
3437 break;
3438 nfs4_state_mark_reclaim_nograce(clp, state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003439 goto do_state_recovery;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003441 if (state == NULL)
3442 break;
3443 nfs4_state_mark_reclaim_reboot(clp, state);
3444 case -NFS4ERR_STALE_CLIENTID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 case -NFS4ERR_EXPIRED:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003446 goto do_state_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003447#if defined(CONFIG_NFS_V4_1)
3448 case -NFS4ERR_BADSESSION:
3449 case -NFS4ERR_BADSLOT:
3450 case -NFS4ERR_BAD_HIGH_SLOT:
3451 case -NFS4ERR_DEADSESSION:
3452 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3453 case -NFS4ERR_SEQ_FALSE_RETRY:
3454 case -NFS4ERR_SEQ_MISORDERED:
3455 dprintk("%s ERROR %d, Reset session\n", __func__,
3456 task->tk_status);
Andy Adamson0b9e2d42009-12-04 16:02:14 -05003457 nfs4_schedule_state_recovery(clp);
Andy Adamson4745e312009-04-01 09:22:42 -04003458 task->tk_status = 0;
3459 return -EAGAIN;
3460#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 case -NFS4ERR_DELAY:
Andy Adamson8328d592009-04-01 09:22:35 -04003462 if (server)
3463 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003464 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003465 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3467 task->tk_status = 0;
3468 return -EAGAIN;
3469 case -NFS4ERR_OLD_STATEID:
3470 task->tk_status = 0;
3471 return -EAGAIN;
3472 }
3473 task->tk_status = nfs4_map_errors(task->tk_status);
3474 return 0;
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003475do_state_recovery:
3476 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
3477 nfs4_schedule_state_recovery(clp);
3478 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3479 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3480 task->tk_status = 0;
3481 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482}
3483
Andy Adamson8328d592009-04-01 09:22:35 -04003484static int
3485nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3486{
3487 return _nfs4_async_handle_error(task, server, server->nfs_client, state);
3488}
3489
David Howellsadfa6f92006-08-22 20:06:08 -04003490int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, unsigned short port, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491{
3492 nfs4_verifier sc_verifier;
3493 struct nfs4_setclientid setclientid = {
3494 .sc_verifier = &sc_verifier,
3495 .sc_prog = program,
3496 };
3497 struct rpc_message msg = {
3498 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3499 .rpc_argp = &setclientid,
3500 .rpc_resp = clp,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003501 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 };
Al Virobc4785c2006-10-19 23:28:51 -07003503 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 int loop = 0;
3505 int status;
3506
Al Virobc4785c2006-10-19 23:28:51 -07003507 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3509 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3510
3511 for(;;) {
3512 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003513 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003514 clp->cl_ipaddr,
3515 rpc_peeraddr2str(clp->cl_rpcclient,
3516 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003517 rpc_peeraddr2str(clp->cl_rpcclient,
3518 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003519 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 clp->cl_id_uniquifier);
3521 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003522 sizeof(setclientid.sc_netid),
3523 rpc_peeraddr2str(clp->cl_rpcclient,
3524 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003526 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527 clp->cl_ipaddr, port >> 8, port & 255);
3528
3529 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3530 if (status != -NFS4ERR_CLID_INUSE)
3531 break;
3532 if (signalled())
3533 break;
3534 if (loop++ & 1)
3535 ssleep(clp->cl_lease_time + 1);
3536 else
3537 if (++clp->cl_id_uniquifier == 0)
3538 break;
3539 }
3540 return status;
3541}
3542
David Howellsadfa6f92006-08-22 20:06:08 -04003543static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544{
3545 struct nfs_fsinfo fsinfo;
3546 struct rpc_message msg = {
3547 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
3548 .rpc_argp = clp,
3549 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003550 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551 };
3552 unsigned long now;
3553 int status;
3554
3555 now = jiffies;
3556 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3557 if (status == 0) {
3558 spin_lock(&clp->cl_lock);
3559 clp->cl_lease_time = fsinfo.lease_time * HZ;
3560 clp->cl_last_renewal = now;
3561 spin_unlock(&clp->cl_lock);
3562 }
3563 return status;
3564}
3565
David Howellsadfa6f92006-08-22 20:06:08 -04003566int nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
Trond Myklebust51581f32006-03-20 13:44:47 -05003567{
Benny Halevy77e03672008-06-24 20:25:57 +03003568 long timeout = 0;
Trond Myklebust51581f32006-03-20 13:44:47 -05003569 int err;
3570 do {
3571 err = _nfs4_proc_setclientid_confirm(clp, cred);
3572 switch (err) {
3573 case 0:
3574 return err;
3575 case -NFS4ERR_RESOURCE:
3576 /* The IBM lawyers misread another document! */
3577 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -05003578 case -EKEYEXPIRED:
Trond Myklebust51581f32006-03-20 13:44:47 -05003579 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3580 }
3581 } while (err == 0);
3582 return err;
3583}
3584
Trond Myklebustfe650402006-01-03 09:55:18 +01003585struct nfs4_delegreturndata {
3586 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003587 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003588 struct nfs_fh fh;
3589 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003590 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003591 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003592 int rpc_status;
3593};
3594
Trond Myklebustfe650402006-01-03 09:55:18 +01003595static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3596{
3597 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003598
Trond Myklebustd61e6122009-12-05 19:32:19 -05003599 nfs4_sequence_done(data->res.server, &data->res.seq_res,
3600 task->tk_status);
Andy Adamson938e1012009-04-01 09:22:28 -04003601
Ricardo Labiaga79708862009-12-07 09:23:21 -05003602 switch (task->tk_status) {
3603 case -NFS4ERR_STALE_STATEID:
3604 case -NFS4ERR_EXPIRED:
3605 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01003606 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003607 break;
3608 default:
3609 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3610 -EAGAIN) {
3611 nfs_restart_rpc(task, data->res.server->nfs_client);
3612 return;
3613 }
3614 }
3615 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01003616}
3617
3618static void nfs4_delegreturn_release(void *calldata)
3619{
Trond Myklebustfe650402006-01-03 09:55:18 +01003620 kfree(calldata);
3621}
3622
Andy Adamson938e1012009-04-01 09:22:28 -04003623#if defined(CONFIG_NFS_V4_1)
3624static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3625{
3626 struct nfs4_delegreturndata *d_data;
3627
3628 d_data = (struct nfs4_delegreturndata *)data;
3629
3630 if (nfs4_setup_sequence(d_data->res.server->nfs_client,
3631 &d_data->args.seq_args,
3632 &d_data->res.seq_res, 1, task))
3633 return;
3634 rpc_call_start(task);
3635}
3636#endif /* CONFIG_NFS_V4_1 */
3637
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003638static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04003639#if defined(CONFIG_NFS_V4_1)
3640 .rpc_call_prepare = nfs4_delegreturn_prepare,
3641#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01003642 .rpc_call_done = nfs4_delegreturn_done,
3643 .rpc_release = nfs4_delegreturn_release,
3644};
3645
Trond Myklebuste6f81072008-01-24 18:14:34 -05003646static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003647{
3648 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003649 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003650 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003651 struct rpc_message msg = {
3652 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3653 .rpc_cred = cred,
3654 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003655 struct rpc_task_setup task_setup_data = {
3656 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003657 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003658 .callback_ops = &nfs4_delegreturn_ops,
3659 .flags = RPC_TASK_ASYNC,
3660 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003661 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003662
Andy Adamson938e1012009-04-01 09:22:28 -04003663 data = kzalloc(sizeof(*data), GFP_KERNEL);
Trond Myklebustfe650402006-01-03 09:55:18 +01003664 if (data == NULL)
3665 return -ENOMEM;
3666 data->args.fhandle = &data->fh;
3667 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003668 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003669 nfs_copy_fh(&data->fh, NFS_FH(inode));
3670 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003671 data->res.fattr = &data->fattr;
3672 data->res.server = server;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003673 data->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003674 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003675 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003676 data->rpc_status = 0;
3677
Trond Myklebustc970aa82007-07-14 15:39:59 -04003678 task_setup_data.callback_data = data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003679 msg.rpc_argp = &data->args,
3680 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003681 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003682 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003683 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003684 if (!issync)
3685 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003686 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003687 if (status != 0)
3688 goto out;
3689 status = data->rpc_status;
3690 if (status != 0)
3691 goto out;
3692 nfs_refresh_inode(inode, &data->fattr);
3693out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003694 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003695 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696}
3697
Trond Myklebuste6f81072008-01-24 18:14:34 -05003698int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699{
3700 struct nfs_server *server = NFS_SERVER(inode);
3701 struct nfs4_exception exception = { };
3702 int err;
3703 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003704 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 switch (err) {
3706 case -NFS4ERR_STALE_STATEID:
3707 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 case 0:
3709 return 0;
3710 }
3711 err = nfs4_handle_exception(server, err, &exception);
3712 } while (exception.retry);
3713 return err;
3714}
3715
3716#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3717#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3718
3719/*
3720 * sleep, with exponential backoff, and retry the LOCK operation.
3721 */
3722static unsigned long
3723nfs4_set_lock_task_retry(unsigned long timeout)
3724{
Matthew Wilcox150030b2007-12-06 16:24:39 -05003725 schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 timeout <<= 1;
3727 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3728 return NFS4_LOCK_MAXTIMEOUT;
3729 return timeout;
3730}
3731
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3733{
3734 struct inode *inode = state->inode;
3735 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003736 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003737 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003739 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003741 struct nfs_lockt_res res = {
3742 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743 };
3744 struct rpc_message msg = {
3745 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3746 .rpc_argp = &arg,
3747 .rpc_resp = &res,
3748 .rpc_cred = state->owner->so_cred,
3749 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750 struct nfs4_lock_state *lsp;
3751 int status;
3752
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003753 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003754 status = nfs4_set_lock_state(state, request);
3755 if (status != 0)
3756 goto out;
3757 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003758 arg.lock_owner.id = lsp->ls_id.id;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003759 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003760 switch (status) {
3761 case 0:
3762 request->fl_type = F_UNLCK;
3763 break;
3764 case -NFS4ERR_DENIED:
3765 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05003767 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003768out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769 return status;
3770}
3771
3772static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3773{
3774 struct nfs4_exception exception = { };
3775 int err;
3776
3777 do {
3778 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3779 _nfs4_proc_getlk(state, cmd, request),
3780 &exception);
3781 } while (exception.retry);
3782 return err;
3783}
3784
3785static int do_vfs_lock(struct file *file, struct file_lock *fl)
3786{
3787 int res = 0;
3788 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3789 case FL_POSIX:
3790 res = posix_lock_file_wait(file, fl);
3791 break;
3792 case FL_FLOCK:
3793 res = flock_lock_file_wait(file, fl);
3794 break;
3795 default:
3796 BUG();
3797 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 return res;
3799}
3800
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003801struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003802 struct nfs_locku_args arg;
3803 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003804 struct nfs4_lock_state *lsp;
3805 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003806 struct file_lock fl;
3807 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003808 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003809};
3810
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003811static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3812 struct nfs_open_context *ctx,
3813 struct nfs4_lock_state *lsp,
3814 struct nfs_seqid *seqid)
3815{
3816 struct nfs4_unlockdata *p;
3817 struct inode *inode = lsp->ls_state->inode;
3818
Andy Adamsona8936932009-04-01 09:22:23 -04003819 p = kzalloc(sizeof(*p), GFP_KERNEL);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003820 if (p == NULL)
3821 return NULL;
3822 p->arg.fh = NFS_FH(inode);
3823 p->arg.fl = &p->fl;
3824 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003825 p->res.seqid = seqid;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003826 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003827 p->arg.stateid = &lsp->ls_stateid;
3828 p->lsp = lsp;
3829 atomic_inc(&lsp->ls_count);
3830 /* Ensure we don't close file until we're done freeing locks! */
3831 p->ctx = get_nfs_open_context(ctx);
3832 memcpy(&p->fl, fl, sizeof(p->fl));
3833 p->server = NFS_SERVER(inode);
3834 return p;
3835}
3836
Trond Myklebust06f814a2006-01-03 09:55:07 +01003837static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003838{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003839 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003840 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003841 nfs4_put_lock_state(calldata->lsp);
3842 put_nfs_open_context(calldata->ctx);
3843 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003844}
3845
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003846static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003847{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003848 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003849
Andy Adamsona8936932009-04-01 09:22:23 -04003850 nfs4_sequence_done(calldata->server, &calldata->res.seq_res,
3851 task->tk_status);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003852 if (RPC_ASSASSINATED(task))
3853 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003854 switch (task->tk_status) {
3855 case 0:
3856 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003857 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003858 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01003859 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003860 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003861 case -NFS4ERR_BAD_STATEID:
3862 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003863 case -NFS4ERR_STALE_STATEID:
3864 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003865 break;
3866 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003867 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebust0110ee12009-12-07 09:00:24 -05003868 nfs_restart_rpc(task,
Trond Myklebustd61e6122009-12-05 19:32:19 -05003869 calldata->server->nfs_client);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003870 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003871}
3872
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003873static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003874{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003875 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003877 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003878 return;
3879 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003880 /* Note: exit _without_ running nfs4_locku_done */
3881 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003882 return;
3883 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01003884 calldata->timestamp = jiffies;
Andy Adamsona8936932009-04-01 09:22:23 -04003885 if (nfs4_setup_sequence(calldata->server->nfs_client,
3886 &calldata->arg.seq_args,
3887 &calldata->res.seq_res, 1, task))
3888 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003889 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890}
3891
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003892static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003893 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003894 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01003895 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003896};
3897
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003898static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3899 struct nfs_open_context *ctx,
3900 struct nfs4_lock_state *lsp,
3901 struct nfs_seqid *seqid)
3902{
3903 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003904 struct rpc_message msg = {
3905 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3906 .rpc_cred = ctx->cred,
3907 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003908 struct rpc_task_setup task_setup_data = {
3909 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04003910 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003911 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003912 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003913 .flags = RPC_TASK_ASYNC,
3914 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003915
Frank Filz137d6ac2007-07-09 15:32:29 -07003916 /* Ensure this is an unlock - when canceling a lock, the
3917 * canceled lock is passed in, and it won't be an unlock.
3918 */
3919 fl->fl_type = F_UNLCK;
3920
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003921 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3922 if (data == NULL) {
3923 nfs_free_seqid(seqid);
3924 return ERR_PTR(-ENOMEM);
3925 }
3926
Trond Myklebust5138fde2007-07-14 15:40:01 -04003927 msg.rpc_argp = &data->arg,
3928 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003929 task_setup_data.callback_data = data;
3930 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003931}
3932
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3934{
Trond Myklebust19e03c52008-12-23 15:21:44 -05003935 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003936 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003937 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01003938 struct rpc_task *task;
3939 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003940 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941
Trond Myklebust9b073572006-06-29 16:38:34 -04003942 status = nfs4_set_lock_state(state, request);
3943 /* Unlock _before_ we do the RPC call */
3944 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003945 down_read(&nfsi->rwsem);
3946 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
3947 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003948 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003949 }
3950 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003951 if (status != 0)
3952 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003953 /* Is this a delegated lock? */
3954 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04003955 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003956 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003957 seqid = nfs_alloc_seqid(&lsp->ls_seqid);
Trond Myklebust9b073572006-06-29 16:38:34 -04003958 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003959 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04003960 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04003961 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003962 status = PTR_ERR(task);
3963 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04003964 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003965 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003966 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04003967out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003968 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003969 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970}
3971
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003972struct nfs4_lockdata {
3973 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003974 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003975 struct nfs4_lock_state *lsp;
3976 struct nfs_open_context *ctx;
3977 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003978 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003979 int rpc_status;
3980 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04003981 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003982};
3983
3984static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
3985 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp)
3986{
3987 struct nfs4_lockdata *p;
3988 struct inode *inode = lsp->ls_state->inode;
3989 struct nfs_server *server = NFS_SERVER(inode);
3990
3991 p = kzalloc(sizeof(*p), GFP_KERNEL);
3992 if (p == NULL)
3993 return NULL;
3994
3995 p->arg.fh = NFS_FH(inode);
3996 p->arg.fl = &p->fl;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003997 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid);
3998 if (p->arg.open_seqid == NULL)
3999 goto out_free;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004000 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
4001 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004002 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004003 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004004 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04004005 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004006 p->res.lock_seqid = p->arg.lock_seqid;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04004007 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004008 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004009 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004010 atomic_inc(&lsp->ls_count);
4011 p->ctx = get_nfs_open_context(ctx);
4012 memcpy(&p->fl, fl, sizeof(p->fl));
4013 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004014out_free_seqid:
4015 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004016out_free:
4017 kfree(p);
4018 return NULL;
4019}
4020
4021static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4022{
4023 struct nfs4_lockdata *data = calldata;
4024 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025
Harvey Harrison3110ff82008-05-02 13:42:44 -07004026 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004027 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4028 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004029 /* Do we need to do an open_to_lock_owner? */
4030 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004031 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4032 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004033 data->arg.open_stateid = &state->stateid;
4034 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004035 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004036 } else
4037 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004038 data->timestamp = jiffies;
Andy Adamson66179ef2009-04-01 09:22:22 -04004039 if (nfs4_setup_sequence(data->server->nfs_client, &data->arg.seq_args,
4040 &data->res.seq_res, 1, task))
4041 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004042 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004043 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004044}
4045
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004046static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4047{
4048 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4049 nfs4_lock_prepare(task, calldata);
4050}
4051
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004052static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4053{
4054 struct nfs4_lockdata *data = calldata;
4055
Harvey Harrison3110ff82008-05-02 13:42:44 -07004056 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004057
Trond Myklebustd61e6122009-12-05 19:32:19 -05004058 nfs4_sequence_done(data->server, &data->res.seq_res,
4059 task->tk_status);
Andy Adamson66179ef2009-04-01 09:22:22 -04004060
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004061 data->rpc_status = task->tk_status;
4062 if (RPC_ASSASSINATED(task))
4063 goto out;
4064 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004065 if (data->rpc_status == 0)
4066 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4067 else
4068 goto out;
4069 }
4070 if (data->rpc_status == 0) {
4071 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4072 sizeof(data->lsp->ls_stateid.data));
4073 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Trond Myklebust88be9f92007-06-05 10:42:27 -04004074 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004075 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004076out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004077 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004078}
4079
4080static void nfs4_lock_release(void *calldata)
4081{
4082 struct nfs4_lockdata *data = calldata;
4083
Harvey Harrison3110ff82008-05-02 13:42:44 -07004084 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004085 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004086 if (data->cancelled != 0) {
4087 struct rpc_task *task;
4088 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4089 data->arg.lock_seqid);
4090 if (!IS_ERR(task))
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004091 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004092 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004093 } else
4094 nfs_free_seqid(data->arg.lock_seqid);
4095 nfs4_put_lock_state(data->lsp);
4096 put_nfs_open_context(data->ctx);
4097 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004098 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004099}
4100
4101static const struct rpc_call_ops nfs4_lock_ops = {
4102 .rpc_call_prepare = nfs4_lock_prepare,
4103 .rpc_call_done = nfs4_lock_done,
4104 .rpc_release = nfs4_lock_release,
4105};
4106
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004107static const struct rpc_call_ops nfs4_recover_lock_ops = {
4108 .rpc_call_prepare = nfs4_recover_lock_prepare,
4109 .rpc_call_done = nfs4_lock_done,
4110 .rpc_release = nfs4_lock_release,
4111};
4112
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004113static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4114{
4115 struct nfs_client *clp = server->nfs_client;
4116 struct nfs4_state *state = lsp->ls_state;
4117
4118 switch (error) {
4119 case -NFS4ERR_ADMIN_REVOKED:
4120 case -NFS4ERR_BAD_STATEID:
4121 case -NFS4ERR_EXPIRED:
4122 if (new_lock_owner != 0 ||
4123 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4124 nfs4_state_mark_reclaim_nograce(clp, state);
4125 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004126 break;
4127 case -NFS4ERR_STALE_STATEID:
4128 if (new_lock_owner != 0 ||
4129 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4130 nfs4_state_mark_reclaim_reboot(clp, state);
4131 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004132 };
4133}
4134
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004135static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004136{
4137 struct nfs4_lockdata *data;
4138 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004139 struct rpc_message msg = {
4140 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4141 .rpc_cred = state->owner->so_cred,
4142 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004143 struct rpc_task_setup task_setup_data = {
4144 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004145 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004146 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004147 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004148 .flags = RPC_TASK_ASYNC,
4149 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004150 int ret;
4151
Harvey Harrison3110ff82008-05-02 13:42:44 -07004152 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004153 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004154 fl->fl_u.nfs4_fl.owner);
4155 if (data == NULL)
4156 return -ENOMEM;
4157 if (IS_SETLKW(cmd))
4158 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004159 if (recovery_type > NFS_LOCK_NEW) {
4160 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004161 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004162 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4163 }
Trond Myklebust5138fde2007-07-14 15:40:01 -04004164 msg.rpc_argp = &data->arg,
4165 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004166 task_setup_data.callback_data = data;
4167 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004168 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004169 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004170 ret = nfs4_wait_for_completion_rpc_task(task);
4171 if (ret == 0) {
4172 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004173 if (ret)
4174 nfs4_handle_setlk_error(data->server, data->lsp,
4175 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004176 } else
4177 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004178 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004179 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004180 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181}
4182
4183static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4184{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004185 struct nfs_server *server = NFS_SERVER(state->inode);
4186 struct nfs4_exception exception = { };
4187 int err;
4188
4189 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004190 /* Cache the lock if possible... */
4191 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4192 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004193 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Jeff Layton2c643482010-01-07 09:42:03 -05004194 if (err != -NFS4ERR_DELAY && err != -EKEYEXPIRED)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004195 break;
4196 nfs4_handle_exception(server, err, &exception);
4197 } while (exception.retry);
4198 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199}
4200
4201static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4202{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004203 struct nfs_server *server = NFS_SERVER(state->inode);
4204 struct nfs4_exception exception = { };
4205 int err;
4206
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004207 err = nfs4_set_lock_state(state, request);
4208 if (err != 0)
4209 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004210 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004211 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4212 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004213 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004214 switch (err) {
4215 default:
4216 goto out;
4217 case -NFS4ERR_GRACE:
4218 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -05004219 case -EKEYEXPIRED:
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004220 nfs4_handle_exception(server, err, &exception);
4221 err = 0;
4222 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004223 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004224out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004225 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226}
4227
4228static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4229{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004230 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004231 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004232 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004234 if ((fl_flags & FL_POSIX) &&
4235 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4236 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004237 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004238 status = nfs4_set_lock_state(state, request);
4239 if (status != 0)
4240 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004241 request->fl_flags |= FL_ACCESS;
4242 status = do_vfs_lock(request->fl_file, request);
4243 if (status < 0)
4244 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004245 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004246 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004247 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004248 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004249 request->fl_flags = fl_flags & ~FL_SLEEP;
4250 status = do_vfs_lock(request->fl_file, request);
4251 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004252 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004253 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004254 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004255 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004256 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004257 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004258 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07004259 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004260out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004261 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004262out:
4263 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264 return status;
4265}
4266
4267static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4268{
4269 struct nfs4_exception exception = { };
4270 int err;
4271
4272 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004273 err = _nfs4_proc_setlk(state, cmd, request);
4274 if (err == -NFS4ERR_DENIED)
4275 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004277 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278 } while (exception.retry);
4279 return err;
4280}
4281
4282static int
4283nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4284{
4285 struct nfs_open_context *ctx;
4286 struct nfs4_state *state;
4287 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4288 int status;
4289
4290 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004291 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292 state = ctx->state;
4293
4294 if (request->fl_start < 0 || request->fl_end < 0)
4295 return -EINVAL;
4296
Trond Myklebustd9531262009-07-21 19:22:38 -04004297 if (IS_GETLK(cmd)) {
4298 if (state != NULL)
4299 return nfs4_proc_getlk(state, F_GETLK, request);
4300 return 0;
4301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302
4303 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4304 return -EINVAL;
4305
Trond Myklebustd9531262009-07-21 19:22:38 -04004306 if (request->fl_type == F_UNLCK) {
4307 if (state != NULL)
4308 return nfs4_proc_unlck(state, cmd, request);
4309 return 0;
4310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311
Trond Myklebustd9531262009-07-21 19:22:38 -04004312 if (state == NULL)
4313 return -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 do {
4315 status = nfs4_proc_setlk(state, cmd, request);
4316 if ((status != -EAGAIN) || IS_SETLK(cmd))
4317 break;
4318 timeout = nfs4_set_lock_task_retry(timeout);
4319 status = -ERESTARTSYS;
4320 if (signalled())
4321 break;
4322 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323 return status;
4324}
4325
Trond Myklebust888e6942005-11-04 15:38:11 -05004326int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4327{
4328 struct nfs_server *server = NFS_SERVER(state->inode);
4329 struct nfs4_exception exception = { };
4330 int err;
4331
4332 err = nfs4_set_lock_state(state, fl);
4333 if (err != 0)
4334 goto out;
4335 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004336 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004337 switch (err) {
4338 default:
4339 printk(KERN_ERR "%s: unhandled error %d.\n",
4340 __func__, err);
4341 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004342 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004343 goto out;
4344 case -NFS4ERR_EXPIRED:
4345 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004346 case -NFS4ERR_STALE_STATEID:
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004347 case -NFS4ERR_BADSESSION:
4348 case -NFS4ERR_BADSLOT:
4349 case -NFS4ERR_BAD_HIGH_SLOT:
4350 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4351 case -NFS4ERR_DEADSESSION:
Trond Myklebustd5122202009-06-17 13:22:58 -07004352 nfs4_schedule_state_recovery(server->nfs_client);
4353 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004354 case -ERESTARTSYS:
4355 /*
4356 * The show must go on: exit, but mark the
4357 * stateid as needing recovery.
4358 */
4359 case -NFS4ERR_ADMIN_REVOKED:
4360 case -NFS4ERR_BAD_STATEID:
4361 case -NFS4ERR_OPENMODE:
4362 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
4363 err = 0;
4364 goto out;
4365 case -ENOMEM:
4366 case -NFS4ERR_DENIED:
4367 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4368 err = 0;
4369 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004370 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -05004371 case -EKEYEXPIRED:
Trond Myklebustd5122202009-06-17 13:22:58 -07004372 break;
4373 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004374 err = nfs4_handle_exception(server, err, &exception);
4375 } while (exception.retry);
4376out:
4377 return err;
4378}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004379
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004380#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4381
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004382int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
4383 size_t buflen, int flags)
4384{
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004385 struct inode *inode = dentry->d_inode;
4386
4387 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4388 return -EOPNOTSUPP;
4389
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004390 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004391}
4392
4393/* The getxattr man page suggests returning -ENODATA for unknown attributes,
4394 * and that's what we'll do for e.g. user attributes that haven't been set.
4395 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
4396 * attributes in kernel-managed attribute namespaces. */
4397ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
4398 size_t buflen)
4399{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004400 struct inode *inode = dentry->d_inode;
4401
4402 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4403 return -EOPNOTSUPP;
4404
4405 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004406}
4407
4408ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
4409{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004410 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004411
J. Bruce Fields096455a2006-03-20 23:23:42 -05004412 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4413 return 0;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004414 if (buf && buflen < len)
4415 return -ERANGE;
4416 if (buf)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004417 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
4418 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004419}
4420
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004421static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4422{
4423 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
4424 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4425 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4426 return;
4427
4428 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4429 NFS_ATTR_FATTR_NLINK;
4430 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4431 fattr->nlink = 2;
4432}
4433
Trond Myklebust56659e92007-07-17 21:52:39 -04004434int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004435 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004436{
4437 struct nfs_server *server = NFS_SERVER(dir);
4438 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004439 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4440 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004441 };
4442 struct nfs4_fs_locations_arg args = {
4443 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004444 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004445 .page = page,
4446 .bitmask = bitmask,
4447 };
Benny Halevy22958462009-04-01 09:22:02 -04004448 struct nfs4_fs_locations_res res = {
4449 .fs_locations = fs_locations,
4450 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004451 struct rpc_message msg = {
4452 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4453 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004454 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004455 };
4456 int status;
4457
Harvey Harrison3110ff82008-05-02 13:42:44 -07004458 dprintk("%s: start\n", __func__);
Trond Myklebustc228fd32007-01-13 02:28:11 -05004459 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004460 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004461 fs_locations->nlocations = 0;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04004462 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004463 nfs_fixup_referral_attributes(&fs_locations->fattr);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004464 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004465 return status;
4466}
4467
Andy Adamson557134a2009-04-01 09:21:53 -04004468#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04004469/*
4470 * nfs4_proc_exchange_id()
4471 *
4472 * Since the clientid has expired, all compounds using sessions
4473 * associated with the stale clientid will be returning
4474 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4475 * be in some phase of session reset.
4476 */
Andy Adamson4d643d12009-12-04 15:52:24 -05004477int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004478{
4479 nfs4_verifier verifier;
4480 struct nfs41_exchange_id_args args = {
4481 .client = clp,
4482 .flags = clp->cl_exchange_flags,
4483 };
4484 struct nfs41_exchange_id_res res = {
4485 .client = clp,
4486 };
4487 int status;
4488 struct rpc_message msg = {
4489 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4490 .rpc_argp = &args,
4491 .rpc_resp = &res,
4492 .rpc_cred = cred,
4493 };
4494 __be32 *p;
4495
4496 dprintk("--> %s\n", __func__);
4497 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04004498
Alexandros Batsakis7b183d02009-12-05 13:33:25 -05004499 /* Remove server-only flags */
4500 args.flags &= ~EXCHGID4_FLAG_CONFIRMED_R;
4501
Benny Halevy99fe60d2009-04-01 09:22:29 -04004502 p = (u32 *)verifier.data;
4503 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4504 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4505 args.verifier = &verifier;
4506
4507 while (1) {
4508 args.id_len = scnprintf(args.id, sizeof(args.id),
4509 "%s/%s %u",
4510 clp->cl_ipaddr,
4511 rpc_peeraddr2str(clp->cl_rpcclient,
4512 RPC_DISPLAY_ADDR),
4513 clp->cl_id_uniquifier);
4514
4515 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
4516
4517 if (status != NFS4ERR_CLID_INUSE)
4518 break;
4519
4520 if (signalled())
4521 break;
4522
4523 if (++clp->cl_id_uniquifier == 0)
4524 break;
4525 }
4526
4527 dprintk("<-- %s status= %d\n", __func__, status);
4528 return status;
4529}
4530
Andy Adamson2050f0c2009-04-01 09:22:30 -04004531struct nfs4_get_lease_time_data {
4532 struct nfs4_get_lease_time_args *args;
4533 struct nfs4_get_lease_time_res *res;
4534 struct nfs_client *clp;
4535};
4536
4537static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4538 void *calldata)
4539{
4540 int ret;
4541 struct nfs4_get_lease_time_data *data =
4542 (struct nfs4_get_lease_time_data *)calldata;
4543
4544 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004545 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004546 /* just setup sequence, do not trigger session recovery
4547 since we're invoked within one */
4548 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004549 &data->args->la_seq_args,
4550 &data->res->lr_seq_res, 0, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004551
4552 BUG_ON(ret == -EAGAIN);
4553 rpc_call_start(task);
4554 dprintk("<-- %s\n", __func__);
4555}
4556
4557/*
4558 * Called from nfs4_state_manager thread for session setup, so don't recover
4559 * from sequence operation or clientid errors.
4560 */
4561static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4562{
4563 struct nfs4_get_lease_time_data *data =
4564 (struct nfs4_get_lease_time_data *)calldata;
4565
4566 dprintk("--> %s\n", __func__);
4567 nfs41_sequence_done(data->clp, &data->res->lr_seq_res, task->tk_status);
4568 switch (task->tk_status) {
4569 case -NFS4ERR_DELAY:
4570 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05004571 case -EKEYEXPIRED:
Andy Adamson2050f0c2009-04-01 09:22:30 -04004572 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4573 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4574 task->tk_status = 0;
Trond Myklebust0110ee12009-12-07 09:00:24 -05004575 nfs_restart_rpc(task, data->clp);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004576 return;
4577 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04004578 dprintk("<-- %s\n", __func__);
4579}
4580
4581struct rpc_call_ops nfs4_get_lease_time_ops = {
4582 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4583 .rpc_call_done = nfs4_get_lease_time_done,
4584};
4585
4586int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4587{
4588 struct rpc_task *task;
4589 struct nfs4_get_lease_time_args args;
4590 struct nfs4_get_lease_time_res res = {
4591 .lr_fsinfo = fsinfo,
4592 };
4593 struct nfs4_get_lease_time_data data = {
4594 .args = &args,
4595 .res = &res,
4596 .clp = clp,
4597 };
4598 struct rpc_message msg = {
4599 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4600 .rpc_argp = &args,
4601 .rpc_resp = &res,
4602 };
4603 struct rpc_task_setup task_setup = {
4604 .rpc_client = clp->cl_rpcclient,
4605 .rpc_message = &msg,
4606 .callback_ops = &nfs4_get_lease_time_ops,
4607 .callback_data = &data
4608 };
4609 int status;
4610
4611 res.lr_seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
4612 dprintk("--> %s\n", __func__);
4613 task = rpc_run_task(&task_setup);
4614
4615 if (IS_ERR(task))
4616 status = PTR_ERR(task);
4617 else {
4618 status = task->tk_status;
4619 rpc_put_task(task);
4620 }
4621 dprintk("<-- %s return %d\n", __func__, status);
4622
4623 return status;
4624}
4625
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004626/*
4627 * Reset a slot table
4628 */
Andy Adamson104aeba2010-01-14 17:45:10 -05004629static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
4630 int ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004631{
Andy Adamson104aeba2010-01-14 17:45:10 -05004632 struct nfs4_slot *new = NULL;
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004633 int i;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004634 int ret = 0;
4635
Andy Adamson104aeba2010-01-14 17:45:10 -05004636 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
4637 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004638
Andy Adamson104aeba2010-01-14 17:45:10 -05004639 /* Does the newly negotiated max_reqs match the existing slot table? */
4640 if (max_reqs != tbl->max_slots) {
4641 ret = -ENOMEM;
4642 new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
4643 GFP_KERNEL);
4644 if (!new)
4645 goto out;
4646 ret = 0;
4647 kfree(tbl->slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004648 }
4649 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson104aeba2010-01-14 17:45:10 -05004650 if (new) {
4651 tbl->slots = new;
4652 tbl->max_slots = max_reqs;
4653 }
4654 for (i = 0; i < tbl->max_slots; ++i)
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004655 tbl->slots[i].seq_nr = ivalue;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004656 spin_unlock(&tbl->slot_tbl_lock);
4657 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4658 tbl, tbl->slots, tbl->max_slots);
4659out:
4660 dprintk("<-- %s: return %d\n", __func__, ret);
4661 return ret;
4662}
4663
Andy Adamsonfc931582009-04-01 09:22:31 -04004664/*
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004665 * Reset the forechannel and backchannel slot tables
4666 */
4667static int nfs4_reset_slot_tables(struct nfs4_session *session)
4668{
4669 int status;
4670
4671 status = nfs4_reset_slot_table(&session->fc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05004672 session->fc_attrs.max_reqs, 1);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004673 if (status)
4674 return status;
4675
4676 status = nfs4_reset_slot_table(&session->bc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05004677 session->bc_attrs.max_reqs, 0);
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004678 return status;
4679}
4680
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004681/* Destroy the slot table */
4682static void nfs4_destroy_slot_tables(struct nfs4_session *session)
4683{
4684 if (session->fc_slot_table.slots != NULL) {
4685 kfree(session->fc_slot_table.slots);
4686 session->fc_slot_table.slots = NULL;
4687 }
4688 if (session->bc_slot_table.slots != NULL) {
4689 kfree(session->bc_slot_table.slots);
4690 session->bc_slot_table.slots = NULL;
4691 }
4692 return;
4693}
4694
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004695/*
Andy Adamsonfc931582009-04-01 09:22:31 -04004696 * Initialize slot table
4697 */
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004698static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
4699 int max_slots, int ivalue)
Andy Adamsonfc931582009-04-01 09:22:31 -04004700{
Andy Adamsonfc931582009-04-01 09:22:31 -04004701 struct nfs4_slot *slot;
4702 int ret = -ENOMEM;
4703
4704 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
4705
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004706 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
Andy Adamsonfc931582009-04-01 09:22:31 -04004707
4708 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_KERNEL);
4709 if (!slot)
4710 goto out;
Andy Adamsonfc931582009-04-01 09:22:31 -04004711 ret = 0;
4712
4713 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonfc931582009-04-01 09:22:31 -04004714 tbl->max_slots = max_slots;
4715 tbl->slots = slot;
4716 tbl->highest_used_slotid = -1; /* no slot is currently used */
4717 spin_unlock(&tbl->slot_tbl_lock);
4718 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4719 tbl, tbl->slots, tbl->max_slots);
4720out:
4721 dprintk("<-- %s: return %d\n", __func__, ret);
4722 return ret;
Andy Adamsonfc931582009-04-01 09:22:31 -04004723}
4724
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004725/*
4726 * Initialize the forechannel and backchannel tables
4727 */
4728static int nfs4_init_slot_tables(struct nfs4_session *session)
4729{
Trond Myklebustf26468f2009-12-05 19:32:11 -05004730 struct nfs4_slot_table *tbl;
4731 int status = 0;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004732
Trond Myklebustf26468f2009-12-05 19:32:11 -05004733 tbl = &session->fc_slot_table;
4734 if (tbl->slots == NULL) {
4735 status = nfs4_init_slot_table(tbl,
4736 session->fc_attrs.max_reqs, 1);
4737 if (status)
4738 return status;
4739 }
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004740
Trond Myklebustf26468f2009-12-05 19:32:11 -05004741 tbl = &session->bc_slot_table;
4742 if (tbl->slots == NULL) {
4743 status = nfs4_init_slot_table(tbl,
4744 session->bc_attrs.max_reqs, 0);
4745 if (status)
4746 nfs4_destroy_slot_tables(session);
4747 }
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004748
4749 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04004750}
4751
4752struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
4753{
4754 struct nfs4_session *session;
4755 struct nfs4_slot_table *tbl;
4756
4757 session = kzalloc(sizeof(struct nfs4_session), GFP_KERNEL);
4758 if (!session)
4759 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04004760
Andy Adamson76db6d92009-04-01 09:22:38 -04004761 /*
4762 * The create session reply races with the server back
4763 * channel probe. Mark the client NFS_CS_SESSION_INITING
4764 * so that the client back channel can find the
4765 * nfs_client struct
4766 */
4767 clp->cl_cons_state = NFS_CS_SESSION_INITING;
Andy Adamsonea028ac2009-12-04 15:55:38 -05004768 init_completion(&session->complete);
Andy Adamson76db6d92009-04-01 09:22:38 -04004769
Andy Adamson557134a2009-04-01 09:21:53 -04004770 tbl = &session->fc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05004771 tbl->highest_used_slotid = -1;
Andy Adamson557134a2009-04-01 09:21:53 -04004772 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004773 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004774
4775 tbl = &session->bc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05004776 tbl->highest_used_slotid = -1;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004777 spin_lock_init(&tbl->slot_tbl_lock);
4778 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
4779
Andy Adamson557134a2009-04-01 09:21:53 -04004780 session->clp = clp;
4781 return session;
4782}
4783
4784void nfs4_destroy_session(struct nfs4_session *session)
4785{
Andy Adamson5a0ffe52009-04-01 09:23:18 -04004786 nfs4_proc_destroy_session(session);
4787 dprintk("%s Destroy backchannel for xprt %p\n",
4788 __func__, session->clp->cl_rpcclient->cl_xprt);
4789 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
4790 NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004791 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04004792 kfree(session);
4793}
4794
Andy Adamsonfc931582009-04-01 09:22:31 -04004795/*
4796 * Initialize the values to be used by the client in CREATE_SESSION
4797 * If nfs4_init_session set the fore channel request and response sizes,
4798 * use them.
4799 *
4800 * Set the back channel max_resp_sz_cached to zero to force the client to
4801 * always set csa_cachethis to FALSE because the current implementation
4802 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
4803 */
4804static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
4805{
4806 struct nfs4_session *session = args->client->cl_session;
4807 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
4808 mxresp_sz = session->fc_attrs.max_resp_sz;
4809
4810 if (mxrqst_sz == 0)
4811 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
4812 if (mxresp_sz == 0)
4813 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
4814 /* Fore channel attributes */
4815 args->fc_attrs.headerpadsz = 0;
4816 args->fc_attrs.max_rqst_sz = mxrqst_sz;
4817 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04004818 args->fc_attrs.max_ops = NFS4_MAX_OPS;
4819 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
4820
4821 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05004822 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04004823 __func__,
4824 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05004825 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04004826
4827 /* Back channel attributes */
4828 args->bc_attrs.headerpadsz = 0;
4829 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
4830 args->bc_attrs.max_resp_sz = PAGE_SIZE;
4831 args->bc_attrs.max_resp_sz_cached = 0;
4832 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
4833 args->bc_attrs.max_reqs = 1;
4834
4835 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
4836 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4837 __func__,
4838 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
4839 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
4840 args->bc_attrs.max_reqs);
4841}
4842
Andy Adamson8d353012009-04-01 09:22:32 -04004843static int _verify_channel_attr(char *chan, char *attr_name, u32 sent, u32 rcvd)
4844{
4845 if (rcvd <= sent)
4846 return 0;
4847 printk(KERN_WARNING "%s: Session INVALID: %s channel %s increased. "
4848 "sent=%u rcvd=%u\n", __func__, chan, attr_name, sent, rcvd);
4849 return -EINVAL;
4850}
4851
4852#define _verify_fore_channel_attr(_name_) \
4853 _verify_channel_attr("fore", #_name_, \
4854 args->fc_attrs._name_, \
4855 session->fc_attrs._name_)
4856
4857#define _verify_back_channel_attr(_name_) \
4858 _verify_channel_attr("back", #_name_, \
4859 args->bc_attrs._name_, \
4860 session->bc_attrs._name_)
4861
4862/*
4863 * The server is not allowed to increase the fore channel header pad size,
4864 * maximum response size, or maximum number of operations.
4865 *
4866 * The back channel attributes are only negotiatied down: We send what the
4867 * (back channel) server insists upon.
4868 */
4869static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
4870 struct nfs4_session *session)
4871{
4872 int ret = 0;
4873
4874 ret |= _verify_fore_channel_attr(headerpadsz);
4875 ret |= _verify_fore_channel_attr(max_resp_sz);
4876 ret |= _verify_fore_channel_attr(max_ops);
4877
4878 ret |= _verify_back_channel_attr(headerpadsz);
4879 ret |= _verify_back_channel_attr(max_rqst_sz);
4880 ret |= _verify_back_channel_attr(max_resp_sz);
4881 ret |= _verify_back_channel_attr(max_resp_sz_cached);
4882 ret |= _verify_back_channel_attr(max_ops);
4883 ret |= _verify_back_channel_attr(max_reqs);
4884
4885 return ret;
4886}
4887
Andy Adamsonfc931582009-04-01 09:22:31 -04004888static int _nfs4_proc_create_session(struct nfs_client *clp)
4889{
4890 struct nfs4_session *session = clp->cl_session;
4891 struct nfs41_create_session_args args = {
4892 .client = clp,
4893 .cb_program = NFS4_CALLBACK,
4894 };
4895 struct nfs41_create_session_res res = {
4896 .client = clp,
4897 };
4898 struct rpc_message msg = {
4899 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
4900 .rpc_argp = &args,
4901 .rpc_resp = &res,
4902 };
4903 int status;
4904
4905 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04004906 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04004907
4908 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4909
Andy Adamson8d353012009-04-01 09:22:32 -04004910 if (!status)
4911 /* Verify the session's negotiated channel_attrs values */
4912 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04004913 if (!status) {
4914 /* Increment the clientid slot sequence id */
4915 clp->cl_seqid++;
4916 }
4917
4918 return status;
4919}
4920
4921/*
4922 * Issues a CREATE_SESSION operation to the server.
4923 * It is the responsibility of the caller to verify the session is
4924 * expired before calling this routine.
4925 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05004926int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04004927{
4928 int status;
4929 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04004930 struct nfs4_session *session = clp->cl_session;
4931
4932 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
4933
4934 status = _nfs4_proc_create_session(clp);
4935 if (status)
4936 goto out;
4937
Trond Myklebustf26468f2009-12-05 19:32:11 -05004938 /* Init and reset the fore channel */
4939 status = nfs4_init_slot_tables(session);
4940 dprintk("slot table initialization returned %d\n", status);
4941 if (status)
4942 goto out;
4943 status = nfs4_reset_slot_tables(session);
4944 dprintk("slot table reset returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04004945 if (status)
4946 goto out;
4947
4948 ptr = (unsigned *)&session->sess_id.data[0];
4949 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
4950 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04004951out:
4952 dprintk("<-- %s\n", __func__);
4953 return status;
4954}
4955
Andy Adamson0f3e66c2009-04-01 09:22:34 -04004956/*
4957 * Issue the over-the-wire RPC DESTROY_SESSION.
4958 * The caller must serialize access to this routine.
4959 */
4960int nfs4_proc_destroy_session(struct nfs4_session *session)
4961{
4962 int status = 0;
4963 struct rpc_message msg;
4964
4965 dprintk("--> nfs4_proc_destroy_session\n");
4966
4967 /* session is still being setup */
4968 if (session->clp->cl_cons_state != NFS_CS_READY)
4969 return status;
4970
4971 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
4972 msg.rpc_argp = session;
4973 msg.rpc_resp = NULL;
4974 msg.rpc_cred = NULL;
4975 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4976
4977 if (status)
4978 printk(KERN_WARNING
4979 "Got error %d from the server on DESTROY_SESSION. "
4980 "Session has been destroyed regardless...\n", status);
4981
4982 dprintk("<-- nfs4_proc_destroy_session\n");
4983 return status;
4984}
4985
Trond Myklebustfccba802009-07-21 16:48:07 -04004986int nfs4_init_session(struct nfs_server *server)
4987{
4988 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05004989 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05004990 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04004991 int ret;
4992
4993 if (!nfs4_has_session(clp))
4994 return 0;
4995
Andy Adamson68bf05e2009-12-15 12:55:02 -05004996 rsize = server->rsize;
4997 if (rsize == 0)
4998 rsize = NFS_MAX_FILE_IO_SIZE;
4999 wsize = server->wsize;
5000 if (wsize == 0)
5001 wsize = NFS_MAX_FILE_IO_SIZE;
5002
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005003 session = clp->cl_session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005004 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5005 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005006
Trond Myklebustfccba802009-07-21 16:48:07 -04005007 ret = nfs4_recover_expired_lease(server);
5008 if (!ret)
5009 ret = nfs4_check_client_ready(clp);
5010 return ret;
5011}
5012
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005013/*
5014 * Renew the cl_session lease.
5015 */
5016static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5017{
5018 struct nfs4_sequence_args args;
5019 struct nfs4_sequence_res res;
5020
5021 struct rpc_message msg = {
5022 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5023 .rpc_argp = &args,
5024 .rpc_resp = &res,
5025 .rpc_cred = cred,
5026 };
5027
5028 args.sa_cache_this = 0;
5029
5030 return nfs4_call_sync_sequence(clp, clp->cl_rpcclient, &msg, &args,
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005031 &res, args.sa_cache_this, 1);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005032}
5033
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005034static void nfs41_sequence_release(void *data)
5035{
5036 struct nfs_client *clp = (struct nfs_client *)data;
5037
5038 nfs4_schedule_state_renewal(clp);
5039}
5040
5041static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005042{
5043 struct nfs_client *clp = (struct nfs_client *)data;
5044
5045 nfs41_sequence_done(clp, task->tk_msg.rpc_resp, task->tk_status);
5046
5047 if (task->tk_status < 0) {
5048 dprintk("%s ERROR %d\n", __func__, task->tk_status);
5049
5050 if (_nfs4_async_handle_error(task, NULL, clp, NULL)
5051 == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05005052 nfs_restart_rpc(task, clp);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005053 return;
5054 }
5055 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005056 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
5057
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005058 kfree(task->tk_msg.rpc_argp);
5059 kfree(task->tk_msg.rpc_resp);
5060
5061 dprintk("<-- %s\n", __func__);
5062}
5063
5064static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5065{
5066 struct nfs_client *clp;
5067 struct nfs4_sequence_args *args;
5068 struct nfs4_sequence_res *res;
5069
5070 clp = (struct nfs_client *)data;
5071 args = task->tk_msg.rpc_argp;
5072 res = task->tk_msg.rpc_resp;
5073
5074 if (nfs4_setup_sequence(clp, args, res, 0, task))
5075 return;
5076 rpc_call_start(task);
5077}
5078
5079static const struct rpc_call_ops nfs41_sequence_ops = {
5080 .rpc_call_done = nfs41_sequence_call_done,
5081 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005082 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005083};
5084
5085static int nfs41_proc_async_sequence(struct nfs_client *clp,
5086 struct rpc_cred *cred)
5087{
5088 struct nfs4_sequence_args *args;
5089 struct nfs4_sequence_res *res;
5090 struct rpc_message msg = {
5091 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5092 .rpc_cred = cred,
5093 };
5094
5095 args = kzalloc(sizeof(*args), GFP_KERNEL);
5096 if (!args)
5097 return -ENOMEM;
5098 res = kzalloc(sizeof(*res), GFP_KERNEL);
5099 if (!res) {
5100 kfree(args);
5101 return -ENOMEM;
5102 }
5103 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
5104 msg.rpc_argp = args;
5105 msg.rpc_resp = res;
5106
5107 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
5108 &nfs41_sequence_ops, (void *)clp);
5109}
5110
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005111struct nfs4_reclaim_complete_data {
5112 struct nfs_client *clp;
5113 struct nfs41_reclaim_complete_args arg;
5114 struct nfs41_reclaim_complete_res res;
5115};
5116
5117static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5118{
5119 struct nfs4_reclaim_complete_data *calldata = data;
5120
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005121 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005122 if (nfs4_setup_sequence(calldata->clp, &calldata->arg.seq_args,
5123 &calldata->res.seq_res, 0, task))
5124 return;
5125
5126 rpc_call_start(task);
5127}
5128
5129static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5130{
5131 struct nfs4_reclaim_complete_data *calldata = data;
5132 struct nfs_client *clp = calldata->clp;
5133 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5134
5135 dprintk("--> %s\n", __func__);
5136 nfs41_sequence_done(clp, res, task->tk_status);
5137 switch (task->tk_status) {
5138 case 0:
5139 case -NFS4ERR_COMPLETE_ALREADY:
5140 break;
5141 case -NFS4ERR_BADSESSION:
5142 case -NFS4ERR_DEADSESSION:
5143 /*
5144 * Handle the session error, but do not retry the operation, as
5145 * we have no way of telling whether the clientid had to be
5146 * reset before we got our reply. If reset, a new wave of
5147 * reclaim operations will follow, containing their own reclaim
5148 * complete. We don't want our retry to get on the way of
5149 * recovery by incorrectly indicating to the server that we're
5150 * done reclaiming state since the process had to be restarted.
5151 */
5152 _nfs4_async_handle_error(task, NULL, clp, NULL);
5153 break;
5154 default:
5155 if (_nfs4_async_handle_error(
5156 task, NULL, clp, NULL) == -EAGAIN) {
5157 rpc_restart_call_prepare(task);
5158 return;
5159 }
5160 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005161
5162 dprintk("<-- %s\n", __func__);
5163}
5164
5165static void nfs4_free_reclaim_complete_data(void *data)
5166{
5167 struct nfs4_reclaim_complete_data *calldata = data;
5168
5169 kfree(calldata);
5170}
5171
5172static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5173 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5174 .rpc_call_done = nfs4_reclaim_complete_done,
5175 .rpc_release = nfs4_free_reclaim_complete_data,
5176};
5177
5178/*
5179 * Issue a global reclaim complete.
5180 */
5181static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5182{
5183 struct nfs4_reclaim_complete_data *calldata;
5184 struct rpc_task *task;
5185 struct rpc_message msg = {
5186 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5187 };
5188 struct rpc_task_setup task_setup_data = {
5189 .rpc_client = clp->cl_rpcclient,
5190 .rpc_message = &msg,
5191 .callback_ops = &nfs4_reclaim_complete_call_ops,
5192 .flags = RPC_TASK_ASYNC,
5193 };
5194 int status = -ENOMEM;
5195
5196 dprintk("--> %s\n", __func__);
5197 calldata = kzalloc(sizeof(*calldata), GFP_KERNEL);
5198 if (calldata == NULL)
5199 goto out;
5200 calldata->clp = clp;
5201 calldata->arg.one_fs = 0;
5202 calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
5203
5204 msg.rpc_argp = &calldata->arg;
5205 msg.rpc_resp = &calldata->res;
5206 task_setup_data.callback_data = calldata;
5207 task = rpc_run_task(&task_setup_data);
5208 if (IS_ERR(task))
5209 status = PTR_ERR(task);
5210 rpc_put_task(task);
5211out:
5212 dprintk("<-- %s status=%d\n", __func__, status);
5213 return status;
5214}
Andy Adamson557134a2009-04-01 09:21:53 -04005215#endif /* CONFIG_NFS_V4_1 */
5216
Andy Adamson591d71c2009-04-01 09:22:47 -04005217struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005218 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005219 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220 .recover_open = nfs4_open_reclaim,
5221 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04005222 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005223 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224};
5225
Andy Adamson591d71c2009-04-01 09:22:47 -04005226#if defined(CONFIG_NFS_V4_1)
5227struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
5228 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5229 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5230 .recover_open = nfs4_open_reclaim,
5231 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05005232 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005233 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005234 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04005235};
5236#endif /* CONFIG_NFS_V4_1 */
5237
5238struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005239 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005240 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005241 .recover_open = nfs4_open_expired,
5242 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04005243 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005244 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245};
5246
Andy Adamson591d71c2009-04-01 09:22:47 -04005247#if defined(CONFIG_NFS_V4_1)
5248struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
5249 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5250 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5251 .recover_open = nfs4_open_expired,
5252 .recover_lock = nfs4_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05005253 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005254 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04005255};
5256#endif /* CONFIG_NFS_V4_1 */
5257
Benny Halevy29fba382009-04-01 09:22:44 -04005258struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
5259 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04005260 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005261 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04005262};
5263
5264#if defined(CONFIG_NFS_V4_1)
5265struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
5266 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04005267 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005268 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04005269};
5270#endif
5271
5272/*
5273 * Per minor version reboot and network partition recovery ops
5274 */
5275
Andy Adamson591d71c2009-04-01 09:22:47 -04005276struct nfs4_state_recovery_ops *nfs4_reboot_recovery_ops[] = {
5277 &nfs40_reboot_recovery_ops,
5278#if defined(CONFIG_NFS_V4_1)
5279 &nfs41_reboot_recovery_ops,
5280#endif
5281};
5282
5283struct nfs4_state_recovery_ops *nfs4_nograce_recovery_ops[] = {
5284 &nfs40_nograce_recovery_ops,
5285#if defined(CONFIG_NFS_V4_1)
5286 &nfs41_nograce_recovery_ops,
5287#endif
5288};
5289
Benny Halevy29fba382009-04-01 09:22:44 -04005290struct nfs4_state_maintenance_ops *nfs4_state_renewal_ops[] = {
5291 &nfs40_state_renewal_ops,
5292#if defined(CONFIG_NFS_V4_1)
5293 &nfs41_state_renewal_ops,
5294#endif
5295};
5296
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08005297static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005298 .permission = nfs_permission,
5299 .getattr = nfs_getattr,
5300 .setattr = nfs_setattr,
5301 .getxattr = nfs4_getxattr,
5302 .setxattr = nfs4_setxattr,
5303 .listxattr = nfs4_listxattr,
5304};
5305
David Howells509de812006-08-22 20:06:11 -04005306const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307 .version = 4, /* protocol version */
5308 .dentry_ops = &nfs4_dentry_operations,
5309 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005310 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005311 .getroot = nfs4_proc_get_root,
5312 .getattr = nfs4_proc_getattr,
5313 .setattr = nfs4_proc_setattr,
David Howells2b3de442006-08-22 20:06:09 -04005314 .lookupfh = nfs4_proc_lookupfh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005315 .lookup = nfs4_proc_lookup,
5316 .access = nfs4_proc_access,
5317 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005318 .create = nfs4_proc_create,
5319 .remove = nfs4_proc_remove,
5320 .unlink_setup = nfs4_proc_unlink_setup,
5321 .unlink_done = nfs4_proc_unlink_done,
5322 .rename = nfs4_proc_rename,
5323 .link = nfs4_proc_link,
5324 .symlink = nfs4_proc_symlink,
5325 .mkdir = nfs4_proc_mkdir,
5326 .rmdir = nfs4_proc_remove,
5327 .readdir = nfs4_proc_readdir,
5328 .mknod = nfs4_proc_mknod,
5329 .statfs = nfs4_proc_statfs,
5330 .fsinfo = nfs4_proc_fsinfo,
5331 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04005332 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005333 .decode_dirent = nfs4_decode_dirent,
5334 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05005335 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005336 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005337 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005339 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005341 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04005342 .close_context = nfs4_close_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005343};
5344
5345/*
5346 * Local variables:
5347 * c-basic-offset: 8
5348 * End:
5349 */