blob: be044b58e811a185884506ca9a1bda32d6ccbc68 [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 Myklebust65de8722008-12-23 15:21:44 -0500252 case -NFS4ERR_STALE_CLIENTID:
253 case -NFS4ERR_STALE_STATEID:
254 case -NFS4ERR_EXPIRED:
255 nfs4_schedule_state_recovery(clp);
256 ret = nfs4_wait_clnt_recover(clp);
257 if (ret == 0)
258 exception->retry = 1;
Andy Adamson4745e312009-04-01 09:22:42 -0400259#if !defined(CONFIG_NFS_V4_1)
Trond Myklebust65de8722008-12-23 15:21:44 -0500260 break;
Andy Adamson4745e312009-04-01 09:22:42 -0400261#else /* !defined(CONFIG_NFS_V4_1) */
262 if (!nfs4_has_session(server->nfs_client))
263 break;
264 /* FALLTHROUGH */
265 case -NFS4ERR_BADSESSION:
266 case -NFS4ERR_BADSLOT:
267 case -NFS4ERR_BAD_HIGH_SLOT:
268 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
269 case -NFS4ERR_DEADSESSION:
270 case -NFS4ERR_SEQ_FALSE_RETRY:
271 case -NFS4ERR_SEQ_MISORDERED:
272 dprintk("%s ERROR: %d Reset session\n", __func__,
273 errorcode);
Andy Adamson0b9e2d42009-12-04 16:02:14 -0500274 nfs4_schedule_state_recovery(clp);
Andy Adamson4745e312009-04-01 09:22:42 -0400275 exception->retry = 1;
Andy Adamsonb9179232009-12-04 15:55:32 -0500276 break;
Andy Adamson4745e312009-04-01 09:22:42 -0400277#endif /* !defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500278 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500279 if (exception->timeout > HZ) {
280 /* We have retried a decent amount, time to
281 * fail
282 */
283 ret = -EBUSY;
284 break;
285 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500286 case -NFS4ERR_GRACE:
287 case -NFS4ERR_DELAY:
288 ret = nfs4_delay(server->client, &exception->timeout);
289 if (ret != 0)
290 break;
291 case -NFS4ERR_OLD_STATEID:
292 exception->retry = 1;
293 }
294 /* We failed to handle the error */
295 return nfs4_map_errors(ret);
296}
297
298
Trond Myklebust26e976a2006-01-03 09:55:21 +0100299static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
David Howells7539bba2006-08-22 20:06:09 -0400301 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 spin_lock(&clp->cl_lock);
303 if (time_before(clp->cl_last_renewal,timestamp))
304 clp->cl_last_renewal = timestamp;
305 spin_unlock(&clp->cl_lock);
306}
307
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400308#if defined(CONFIG_NFS_V4_1)
309
Benny Halevy510b8172009-04-01 09:22:14 -0400310/*
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400311 * nfs4_free_slot - free a slot and efficiently update slot table.
312 *
313 * freeing a slot is trivially done by clearing its respective bit
314 * in the bitmap.
315 * If the freed slotid equals highest_used_slotid we want to update it
316 * so that the server would be able to size down the slot table if needed,
317 * otherwise we know that the highest_used_slotid is still in use.
318 * When updating highest_used_slotid there may be "holes" in the bitmap
319 * so we need to scan down from highest_used_slotid to 0 looking for the now
320 * highest slotid in use.
321 * If none found, highest_used_slotid is set to -1.
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500322 *
323 * Must be called while holding tbl->slot_tbl_lock
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400324 */
325static void
326nfs4_free_slot(struct nfs4_slot_table *tbl, u8 free_slotid)
327{
328 int slotid = free_slotid;
329
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400330 /* clear used bit in bitmap */
331 __clear_bit(slotid, tbl->used_slots);
332
333 /* update highest_used_slotid when it is freed */
334 if (slotid == tbl->highest_used_slotid) {
335 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500336 if (slotid < tbl->max_slots)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400337 tbl->highest_used_slotid = slotid;
338 else
339 tbl->highest_used_slotid = -1;
340 }
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400341 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
342 free_slotid, tbl->highest_used_slotid);
343}
344
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800345/*
346 * Signal state manager thread if session is drained
347 */
348static void nfs41_check_drain_session_complete(struct nfs4_session *ses)
349{
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800350 struct rpc_task *task;
351
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800352 if (!test_bit(NFS4CLNT_SESSION_DRAINING, &ses->clp->cl_state)) {
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800353 task = rpc_wake_up_next(&ses->fc_slot_table.slot_tbl_waitq);
354 if (task)
355 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800356 return;
357 }
358
359 if (ses->fc_slot_table.highest_used_slotid != -1)
360 return;
361
362 dprintk("%s COMPLETE: Session Drained\n", __func__);
363 complete(&ses->complete);
364}
365
Trond Myklebustd61e6122009-12-05 19:32:19 -0500366static void nfs41_sequence_free_slot(const struct nfs_client *clp,
Andy Adamson13615872009-04-01 09:22:17 -0400367 struct nfs4_sequence_res *res)
368{
369 struct nfs4_slot_table *tbl;
370
Andy Adamson13615872009-04-01 09:22:17 -0400371 tbl = &clp->cl_session->fc_slot_table;
372 if (res->sr_slotid == NFS4_MAX_SLOT_TABLE) {
Andy Adamson13615872009-04-01 09:22:17 -0400373 /* just wake up the next guy waiting since
374 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500375 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500376 return;
Andy Adamson13615872009-04-01 09:22:17 -0400377 }
Andy Adamsonea028ac2009-12-04 15:55:38 -0500378
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500379 spin_lock(&tbl->slot_tbl_lock);
380 nfs4_free_slot(tbl, res->sr_slotid);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800381 nfs41_check_drain_session_complete(clp->cl_session);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500382 spin_unlock(&tbl->slot_tbl_lock);
383 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
Andy Adamson13615872009-04-01 09:22:17 -0400384}
385
Andy Adamsonb0df8062009-04-01 09:22:18 -0400386static void nfs41_sequence_done(struct nfs_client *clp,
387 struct nfs4_sequence_res *res,
388 int rpc_status)
389{
390 unsigned long timestamp;
391 struct nfs4_slot_table *tbl;
392 struct nfs4_slot *slot;
393
394 /*
395 * sr_status remains 1 if an RPC level error occurred. The server
396 * may or may not have processed the sequence operation..
397 * Proceed as if the server received and processed the sequence
398 * operation.
399 */
400 if (res->sr_status == 1)
401 res->sr_status = NFS_OK;
402
403 /* -ERESTARTSYS can result in skipping nfs41_sequence_setup */
404 if (res->sr_slotid == NFS4_MAX_SLOT_TABLE)
405 goto out;
406
Andy Adamson691daf32009-12-04 15:55:39 -0500407 /* Check the SEQUENCE operation status */
Andy Adamsonb0df8062009-04-01 09:22:18 -0400408 if (res->sr_status == 0) {
Andy Adamson691daf32009-12-04 15:55:39 -0500409 tbl = &clp->cl_session->fc_slot_table;
410 slot = tbl->slots + res->sr_slotid;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400411 /* Update the slot's sequence and clientid lease timer */
412 ++slot->seq_nr;
413 timestamp = res->sr_renewal_time;
414 spin_lock(&clp->cl_lock);
415 if (time_before(clp->cl_last_renewal, timestamp))
416 clp->cl_last_renewal = timestamp;
417 spin_unlock(&clp->cl_lock);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500418 /* Check sequence flags */
419 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400420 }
421out:
422 /* The session may be reset by one of the error handlers. */
423 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
424 nfs41_sequence_free_slot(clp, res);
425}
426
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400427/*
Benny Halevy510b8172009-04-01 09:22:14 -0400428 * nfs4_find_slot - efficiently look for a free slot
429 *
430 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
431 * If found, we mark the slot as used, update the highest_used_slotid,
432 * and respectively set up the sequence operation args.
433 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400434 *
435 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400436 */
437static u8
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800438nfs4_find_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400439{
440 int slotid;
441 u8 ret_id = NFS4_MAX_SLOT_TABLE;
442 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
443
Benny Halevy510b8172009-04-01 09:22:14 -0400444 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
445 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
446 tbl->max_slots);
447 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
448 if (slotid >= tbl->max_slots)
449 goto out;
450 __set_bit(slotid, tbl->used_slots);
451 if (slotid > tbl->highest_used_slotid)
452 tbl->highest_used_slotid = slotid;
453 ret_id = slotid;
454out:
455 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
456 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400457 return ret_id;
458}
459
Andy Adamsonce5039c2009-04-01 09:22:13 -0400460static int nfs41_setup_sequence(struct nfs4_session *session,
461 struct nfs4_sequence_args *args,
462 struct nfs4_sequence_res *res,
463 int cache_reply,
464 struct rpc_task *task)
465{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400466 struct nfs4_slot *slot;
467 struct nfs4_slot_table *tbl;
468 u8 slotid;
469
470 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400471 /* slot already allocated? */
472 if (res->sr_slotid != NFS4_MAX_SLOT_TABLE)
473 return 0;
474
475 memset(res, 0, sizeof(*res));
476 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400477 tbl = &session->fc_slot_table;
478
479 spin_lock(&tbl->slot_tbl_lock);
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800480 if (test_bit(NFS4CLNT_SESSION_DRAINING, &session->clp->cl_state) &&
481 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamsonea028ac2009-12-04 15:55:38 -0500482 /*
483 * The state manager will wait until the slot table is empty.
484 * Schedule the reset thread
485 */
Andy Adamson05f0d232009-12-04 15:55:37 -0500486 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400487 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500488 dprintk("%s Schedule Session Reset\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500489 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400490 }
491
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800492 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
493 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
494 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
495 spin_unlock(&tbl->slot_tbl_lock);
496 dprintk("%s enforce FIFO order\n", __func__);
497 return -EAGAIN;
498 }
499
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800500 slotid = nfs4_find_slot(tbl);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400501 if (slotid == NFS4_MAX_SLOT_TABLE) {
502 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
503 spin_unlock(&tbl->slot_tbl_lock);
504 dprintk("<-- %s: no free slots\n", __func__);
505 return -EAGAIN;
506 }
507 spin_unlock(&tbl->slot_tbl_lock);
508
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800509 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400510 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400511 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400512 args->sa_slotid = slotid;
513 args->sa_cache_this = cache_reply;
514
515 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
516
Benny Halevy99fe60d2009-04-01 09:22:29 -0400517 res->sr_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400518 res->sr_slotid = slotid;
519 res->sr_renewal_time = jiffies;
520 /*
521 * sr_status is only set in decode_sequence, and so will remain
522 * set to 1 if an rpc level failure occurs.
523 */
524 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400525 return 0;
526}
527
528int nfs4_setup_sequence(struct nfs_client *clp,
529 struct nfs4_sequence_args *args,
530 struct nfs4_sequence_res *res,
531 int cache_reply,
532 struct rpc_task *task)
533{
534 int ret = 0;
535
536 dprintk("--> %s clp %p session %p sr_slotid %d\n",
537 __func__, clp, clp->cl_session, res->sr_slotid);
538
539 if (!nfs4_has_session(clp))
540 goto out;
541 ret = nfs41_setup_sequence(clp->cl_session, args, res, cache_reply,
542 task);
Andy Adamson2628edd2009-12-04 15:55:35 -0500543 if (ret && ret != -EAGAIN) {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400544 /* terminate rpc task */
545 task->tk_status = ret;
546 task->tk_action = NULL;
547 }
548out:
549 dprintk("<-- %s status=%d\n", __func__, ret);
550 return ret;
551}
552
553struct nfs41_call_sync_data {
554 struct nfs_client *clp;
555 struct nfs4_sequence_args *seq_args;
556 struct nfs4_sequence_res *seq_res;
557 int cache_reply;
558};
559
560static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
561{
562 struct nfs41_call_sync_data *data = calldata;
563
564 dprintk("--> %s data->clp->cl_session %p\n", __func__,
565 data->clp->cl_session);
566 if (nfs4_setup_sequence(data->clp, data->seq_args,
567 data->seq_res, data->cache_reply, task))
568 return;
569 rpc_call_start(task);
570}
571
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800572static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
573{
574 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
575 nfs41_call_sync_prepare(task, calldata);
576}
577
Andy Adamson69ab40c2009-04-01 09:22:19 -0400578static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
579{
580 struct nfs41_call_sync_data *data = calldata;
581
582 nfs41_sequence_done(data->clp, data->seq_res, task->tk_status);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400583}
584
Andy Adamsonce5039c2009-04-01 09:22:13 -0400585struct rpc_call_ops nfs41_call_sync_ops = {
586 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400587 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400588};
589
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800590struct rpc_call_ops nfs41_call_priv_sync_ops = {
591 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
592 .rpc_call_done = nfs41_call_sync_done,
593};
594
Andy Adamsonce5039c2009-04-01 09:22:13 -0400595static int nfs4_call_sync_sequence(struct nfs_client *clp,
596 struct rpc_clnt *clnt,
597 struct rpc_message *msg,
598 struct nfs4_sequence_args *args,
599 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800600 int cache_reply,
601 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400602{
603 int ret;
604 struct rpc_task *task;
605 struct nfs41_call_sync_data data = {
606 .clp = clp,
607 .seq_args = args,
608 .seq_res = res,
609 .cache_reply = cache_reply,
610 };
611 struct rpc_task_setup task_setup = {
612 .rpc_client = clnt,
613 .rpc_message = msg,
614 .callback_ops = &nfs41_call_sync_ops,
615 .callback_data = &data
616 };
617
618 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800619 if (privileged)
620 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400621 task = rpc_run_task(&task_setup);
622 if (IS_ERR(task))
623 ret = PTR_ERR(task);
624 else {
625 ret = task->tk_status;
626 rpc_put_task(task);
627 }
628 return ret;
629}
630
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400631int _nfs4_call_sync_session(struct nfs_server *server,
632 struct rpc_message *msg,
633 struct nfs4_sequence_args *args,
634 struct nfs4_sequence_res *res,
635 int cache_reply)
636{
Andy Adamsonce5039c2009-04-01 09:22:13 -0400637 return nfs4_call_sync_sequence(server->nfs_client, server->client,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800638 msg, args, res, cache_reply, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400639}
640
641#endif /* CONFIG_NFS_V4_1 */
642
643int _nfs4_call_sync(struct nfs_server *server,
644 struct rpc_message *msg,
645 struct nfs4_sequence_args *args,
646 struct nfs4_sequence_res *res,
647 int cache_reply)
648{
Benny Halevyf3752972009-04-01 09:22:04 -0400649 args->sa_session = res->sr_session = NULL;
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400650 return rpc_call_sync(server->client, msg, 0);
651}
652
653#define nfs4_call_sync(server, msg, args, res, cache_reply) \
654 (server)->nfs_client->cl_call_sync((server), (msg), &(args)->seq_args, \
655 &(res)->seq_res, (cache_reply))
656
Andy Adamsonb0df8062009-04-01 09:22:18 -0400657static void nfs4_sequence_done(const struct nfs_server *server,
658 struct nfs4_sequence_res *res, int rpc_status)
659{
660#ifdef CONFIG_NFS_V4_1
661 if (nfs4_has_session(server->nfs_client))
662 nfs41_sequence_done(server->nfs_client, res, rpc_status);
663#endif /* CONFIG_NFS_V4_1 */
664}
665
Trond Myklebust38478b22006-05-25 01:40:57 -0400666static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
Trond Myklebust38478b22006-05-25 01:40:57 -0400668 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Trond Myklebust38478b22006-05-25 01:40:57 -0400670 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400671 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
672 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400673 nfs_force_lookup_revalidate(dir);
Trond Myklebust40d24702007-10-08 09:24:22 -0400674 nfsi->change_attr = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400675 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676}
677
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100678struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400679 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100680 struct nfs_openargs o_arg;
681 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100682 struct nfs_open_confirmargs c_arg;
683 struct nfs_open_confirmres c_res;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100684 struct nfs_fattr f_attr;
685 struct nfs_fattr dir_attr;
Trond Myklebustad389da2007-06-05 12:30:00 -0400686 struct path path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100687 struct dentry *dir;
688 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400689 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100690 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100691 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400692 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100693 int rpc_status;
694 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100695};
696
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400697
698static void nfs4_init_opendata_res(struct nfs4_opendata *p)
699{
700 p->o_res.f_attr = &p->f_attr;
701 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400702 p->o_res.seqid = p->o_arg.seqid;
703 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400704 p->o_res.server = p->o_arg.server;
705 nfs_fattr_init(&p->f_attr);
706 nfs_fattr_init(&p->dir_attr);
Benny Halevy578e4582009-06-18 22:01:23 -0400707 p->o_res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400708}
709
Trond Myklebustad389da2007-06-05 12:30:00 -0400710static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500711 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100712 const struct iattr *attrs)
713{
Trond Myklebustad389da2007-06-05 12:30:00 -0400714 struct dentry *parent = dget_parent(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100715 struct inode *dir = parent->d_inode;
716 struct nfs_server *server = NFS_SERVER(dir);
717 struct nfs4_opendata *p;
718
719 p = kzalloc(sizeof(*p), GFP_KERNEL);
720 if (p == NULL)
721 goto err;
722 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
723 if (p->o_arg.seqid == NULL)
724 goto err_free;
Trond Myklebustad389da2007-06-05 12:30:00 -0400725 p->path.mnt = mntget(path->mnt);
726 p->path.dentry = dget(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100727 p->dir = parent;
728 p->owner = sp;
729 atomic_inc(&sp->so_count);
730 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500731 p->o_arg.open_flags = flags;
732 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400733 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400734 p->o_arg.id = sp->so_owner_id.id;
Trond Myklebustad389da2007-06-05 12:30:00 -0400735 p->o_arg.name = &p->path.dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100736 p->o_arg.server = server;
737 p->o_arg.bitmask = server->attr_bitmask;
738 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100739 if (flags & O_EXCL) {
Alexandros Batsakis4882ef72009-12-05 13:30:21 -0500740 if (nfs4_has_persistent_session(server->nfs_client)) {
741 /* GUARDED */
742 p->o_arg.u.attrs = &p->attrs;
743 memcpy(&p->attrs, attrs, sizeof(p->attrs));
744 } else { /* EXCLUSIVE4_1 */
745 u32 *s = (u32 *) p->o_arg.u.verifier.data;
746 s[0] = jiffies;
747 s[1] = current->pid;
748 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100749 } else if (flags & O_CREAT) {
750 p->o_arg.u.attrs = &p->attrs;
751 memcpy(&p->attrs, attrs, sizeof(p->attrs));
752 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100753 p->c_arg.fh = &p->o_res.fh;
754 p->c_arg.stateid = &p->o_res.stateid;
755 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400756 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400757 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100758 return p;
759err_free:
760 kfree(p);
761err:
762 dput(parent);
763 return NULL;
764}
765
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400766static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100767{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400768 struct nfs4_opendata *p = container_of(kref,
769 struct nfs4_opendata, kref);
770
771 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400772 if (p->state != NULL)
773 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400774 nfs4_put_state_owner(p->owner);
775 dput(p->dir);
Jan Blunck31f31db12008-05-02 13:42:45 -0700776 path_put(&p->path);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400777 kfree(p);
778}
779
780static void nfs4_opendata_put(struct nfs4_opendata *p)
781{
782 if (p != NULL)
783 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100784}
785
Trond Myklebust06f814a2006-01-03 09:55:07 +0100786static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
787{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100788 int ret;
789
Trond Myklebust06f814a2006-01-03 09:55:07 +0100790 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100791 return ret;
792}
793
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500794static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400795{
796 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500797
798 if (open_mode & O_EXCL)
799 goto out;
800 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400801 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500802 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
803 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400804 break;
805 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500806 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
807 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400808 break;
809 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500810 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
811 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400812 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500813out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400814 return ret;
815}
816
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500817static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400818{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500819 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400820 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500821 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400822 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500823 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400824 return 1;
825}
826
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500827static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100828{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500829 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100830 case FMODE_WRITE:
831 state->n_wronly++;
832 break;
833 case FMODE_READ:
834 state->n_rdonly++;
835 break;
836 case FMODE_READ|FMODE_WRITE:
837 state->n_rdwr++;
838 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500839 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100840}
841
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500842static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400843{
844 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
845 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
846 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500847 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400848 case FMODE_READ:
849 set_bit(NFS_O_RDONLY_STATE, &state->flags);
850 break;
851 case FMODE_WRITE:
852 set_bit(NFS_O_WRONLY_STATE, &state->flags);
853 break;
854 case FMODE_READ|FMODE_WRITE:
855 set_bit(NFS_O_RDWR_STATE, &state->flags);
856 }
857}
858
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500859static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400860{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400861 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500862 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400863 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400864}
865
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500866static 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 -0700867{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400868 /*
869 * Protect the call to nfs4_state_set_mode_locked and
870 * serialise the stateid update
871 */
872 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400873 if (deleg_stateid != NULL) {
874 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
875 set_bit(NFS_DELEGATED_STATE, &state->flags);
876 }
877 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500878 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400879 write_sequnlock(&state->seqlock);
880 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500881 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700882 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883}
884
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500885static 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 -0500886{
887 struct nfs_inode *nfsi = NFS_I(state->inode);
888 struct nfs_delegation *deleg_cur;
889 int ret = 0;
890
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500891 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500892
893 rcu_read_lock();
894 deleg_cur = rcu_dereference(nfsi->delegation);
895 if (deleg_cur == NULL)
896 goto no_delegation;
897
898 spin_lock(&deleg_cur->lock);
899 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500900 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500901 goto no_delegation_unlock;
902
903 if (delegation == NULL)
904 delegation = &deleg_cur->stateid;
905 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
906 goto no_delegation_unlock;
907
Trond Myklebustb7391f42008-12-23 15:21:52 -0500908 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500909 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500910 ret = 1;
911no_delegation_unlock:
912 spin_unlock(&deleg_cur->lock);
913no_delegation:
914 rcu_read_unlock();
915
916 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500917 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500918 ret = 1;
919 }
920
921 return ret;
922}
923
924
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500925static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400926{
927 struct nfs_delegation *delegation;
928
929 rcu_read_lock();
930 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500931 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -0400932 rcu_read_unlock();
933 return;
934 }
935 rcu_read_unlock();
936 nfs_inode_return_delegation(inode);
937}
938
Trond Myklebust6ee41262007-07-08 14:11:36 -0400939static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400940{
941 struct nfs4_state *state = opendata->state;
942 struct nfs_inode *nfsi = NFS_I(state->inode);
943 struct nfs_delegation *delegation;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500944 int open_mode = opendata->o_arg.open_flags & O_EXCL;
945 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400946 nfs4_stateid stateid;
947 int ret = -EAGAIN;
948
Trond Myklebustaac00a82007-07-05 19:02:21 -0400949 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500950 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400951 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500952 if (can_open_cached(state, fmode, open_mode)) {
953 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400954 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400955 goto out_return_state;
956 }
957 spin_unlock(&state->owner->so_lock);
958 }
Trond Myklebust34310432008-12-23 15:21:38 -0500959 rcu_read_lock();
960 delegation = rcu_dereference(nfsi->delegation);
961 if (delegation == NULL ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500962 !can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -0500963 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -0400964 break;
Trond Myklebust34310432008-12-23 15:21:38 -0500965 }
Trond Myklebustaac00a82007-07-05 19:02:21 -0400966 /* Save the delegation */
967 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
968 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -0400969 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400970 if (ret != 0)
971 goto out;
972 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -0500973
974 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500975 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -0500976 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400977 }
Trond Myklebustaac00a82007-07-05 19:02:21 -0400978out:
979 return ERR_PTR(ret);
980out_return_state:
981 atomic_inc(&state->count);
982 return state;
983}
984
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100985static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
986{
987 struct inode *inode;
988 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -0400989 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400990 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100991
Trond Myklebustaac00a82007-07-05 19:02:21 -0400992 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400993 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400994 goto out;
995 }
996
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400997 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100998 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400999 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001000 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001001 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001002 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001003 goto err;
1004 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001005 state = nfs4_get_open_state(inode, data->owner);
1006 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001007 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001008 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001009 int delegation_flags = 0;
1010
Trond Myklebust003707c2007-07-05 18:07:55 -04001011 rcu_read_lock();
1012 delegation = rcu_dereference(NFS_I(inode)->delegation);
1013 if (delegation)
1014 delegation_flags = delegation->flags;
1015 rcu_read_unlock();
Trond Myklebust15c831b2008-12-23 15:21:39 -05001016 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001017 nfs_inode_set_delegation(state->inode,
1018 data->owner->so_cred,
1019 &data->o_res);
1020 else
1021 nfs_inode_reclaim_delegation(state->inode,
1022 data->owner->so_cred,
1023 &data->o_res);
1024 }
Trond Myklebust34310432008-12-23 15:21:38 -05001025
1026 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001027 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001028 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001029out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001030 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001031err_put_inode:
1032 iput(inode);
1033err:
1034 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001035}
1036
Trond Myklebust864472e2006-01-03 09:55:15 +01001037static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1038{
1039 struct nfs_inode *nfsi = NFS_I(state->inode);
1040 struct nfs_open_context *ctx;
1041
1042 spin_lock(&state->inode->i_lock);
1043 list_for_each_entry(ctx, &nfsi->open_files, list) {
1044 if (ctx->state != state)
1045 continue;
1046 get_nfs_open_context(ctx);
1047 spin_unlock(&state->inode->i_lock);
1048 return ctx;
1049 }
1050 spin_unlock(&state->inode->i_lock);
1051 return ERR_PTR(-ENOENT);
1052}
1053
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001054static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1055{
1056 struct nfs4_opendata *opendata;
1057
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001058 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001059 if (opendata == NULL)
1060 return ERR_PTR(-ENOMEM);
1061 opendata->state = state;
1062 atomic_inc(&state->count);
1063 return opendata;
1064}
1065
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001066static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001067{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001068 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001069 int ret;
1070
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001071 opendata->o_arg.open_flags = 0;
1072 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001073 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1074 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1075 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001076 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001077 if (ret != 0)
1078 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001079 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001080 if (IS_ERR(newstate))
1081 return PTR_ERR(newstate);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001082 nfs4_close_state(&opendata->path, newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001083 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001084 return 0;
1085}
1086
1087static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1088{
Trond Myklebust864472e2006-01-03 09:55:15 +01001089 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001090 int ret;
1091
1092 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001093 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001094 smp_rmb();
1095 if (state->n_rdwr != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001096 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001097 if (ret != 0)
1098 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001099 if (newstate != state)
1100 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001101 }
1102 if (state->n_wronly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001103 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001104 if (ret != 0)
1105 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001106 if (newstate != state)
1107 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001108 }
1109 if (state->n_rdonly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001110 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001111 if (ret != 0)
1112 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001113 if (newstate != state)
1114 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001115 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001116 /*
1117 * We may have performed cached opens for all three recoveries.
1118 * Check if we need to update the current stateid.
1119 */
1120 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1121 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001122 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001123 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1124 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001125 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001126 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001127 return 0;
1128}
1129
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130/*
1131 * OPEN_RECLAIM:
1132 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001134static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001136 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001137 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001138 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 int status;
1140
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001141 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1142 if (IS_ERR(opendata))
1143 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001144 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1145 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001146 rcu_read_lock();
1147 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001148 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001149 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001150 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001151 opendata->o_arg.u.delegation_type = delegation_type;
1152 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001153 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 return status;
1155}
1156
Trond Myklebust539cd032007-06-05 11:46:42 -04001157static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158{
1159 struct nfs_server *server = NFS_SERVER(state->inode);
1160 struct nfs4_exception exception = { };
1161 int err;
1162 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001163 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001164 if (err != -NFS4ERR_DELAY)
1165 break;
1166 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 } while (exception.retry);
1168 return err;
1169}
1170
Trond Myklebust864472e2006-01-03 09:55:15 +01001171static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1172{
1173 struct nfs_open_context *ctx;
1174 int ret;
1175
1176 ctx = nfs4_state_find_open_context(state);
1177 if (IS_ERR(ctx))
1178 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001179 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001180 put_nfs_open_context(ctx);
1181 return ret;
1182}
1183
Trond Myklebust13437e12007-07-06 15:10:43 -04001184static 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 -07001185{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001186 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001187 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001189 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1190 if (IS_ERR(opendata))
1191 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001192 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -04001193 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001194 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +01001195 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001196 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001197 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198}
1199
Trond Myklebust13437e12007-07-06 15:10:43 -04001200int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201{
1202 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001203 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 int err;
1205 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001206 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 switch (err) {
1208 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001209 case -ENOENT:
1210 case -ESTALE:
1211 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001212 case -NFS4ERR_BADSESSION:
1213 case -NFS4ERR_BADSLOT:
1214 case -NFS4ERR_BAD_HIGH_SLOT:
1215 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1216 case -NFS4ERR_DEADSESSION:
1217 nfs4_schedule_state_recovery(
1218 server->nfs_client);
1219 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 case -NFS4ERR_STALE_CLIENTID:
1221 case -NFS4ERR_STALE_STATEID:
1222 case -NFS4ERR_EXPIRED:
1223 /* Don't recall a delegation if it was lost */
David Howells7539bba2006-08-22 20:06:09 -04001224 nfs4_schedule_state_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001225 goto out;
1226 case -ERESTARTSYS:
1227 /*
1228 * The show must go on: exit, but mark the
1229 * stateid as needing recovery.
1230 */
1231 case -NFS4ERR_ADMIN_REVOKED:
1232 case -NFS4ERR_BAD_STATEID:
1233 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
1234 case -ENOMEM:
1235 err = 0;
1236 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 }
1238 err = nfs4_handle_exception(server, err, &exception);
1239 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001240out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 return err;
1242}
1243
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001244static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1245{
1246 struct nfs4_opendata *data = calldata;
1247
1248 data->rpc_status = task->tk_status;
1249 if (RPC_ASSASSINATED(task))
1250 return;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001251 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001252 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1253 sizeof(data->o_res.stateid.data));
Trond Myklebustbb226292008-01-02 15:19:18 -05001254 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001255 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001256 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001257 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001258}
1259
1260static void nfs4_open_confirm_release(void *calldata)
1261{
1262 struct nfs4_opendata *data = calldata;
1263 struct nfs4_state *state = NULL;
1264
1265 /* If this request hasn't been cancelled, do nothing */
1266 if (data->cancelled == 0)
1267 goto out_free;
1268 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001269 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001270 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001271 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001272 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001273 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001274out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001275 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001276}
1277
1278static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001279 .rpc_call_done = nfs4_open_confirm_done,
1280 .rpc_release = nfs4_open_confirm_release,
1281};
1282
1283/*
1284 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1285 */
1286static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1287{
1288 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1289 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001290 struct rpc_message msg = {
1291 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1292 .rpc_argp = &data->c_arg,
1293 .rpc_resp = &data->c_res,
1294 .rpc_cred = data->owner->so_cred,
1295 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001296 struct rpc_task_setup task_setup_data = {
1297 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001298 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001299 .callback_ops = &nfs4_open_confirm_ops,
1300 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001301 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001302 .flags = RPC_TASK_ASYNC,
1303 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 int status;
1305
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001306 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001307 data->rpc_done = 0;
1308 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001309 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001310 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001311 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001312 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001313 status = nfs4_wait_for_completion_rpc_task(task);
1314 if (status != 0) {
1315 data->cancelled = 1;
1316 smp_wmb();
1317 } else
1318 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001319 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 return status;
1321}
1322
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001323static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001325 struct nfs4_opendata *data = calldata;
1326 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001327
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001328 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1329 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001330 /*
1331 * Check if we still need to send an OPEN call, or if we can use
1332 * a delegation instead.
1333 */
1334 if (data->state != NULL) {
1335 struct nfs_delegation *delegation;
1336
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001337 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001338 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001339 rcu_read_lock();
1340 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1341 if (delegation != NULL &&
Trond Myklebust15c831b2008-12-23 15:21:39 -05001342 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001343 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001344 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001345 }
1346 rcu_read_unlock();
1347 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001348 /* Update sequence id. */
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001349 data->o_arg.id = sp->so_owner_id.id;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001350 data->o_arg.clientid = sp->so_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001351 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001352 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001353 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1354 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001355 data->timestamp = jiffies;
Andy Adamsond8985282009-04-01 09:22:21 -04001356 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
1357 &data->o_arg.seq_args,
1358 &data->o_res.seq_res, 1, task))
1359 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001360 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001361 return;
1362out_no_action:
1363 task->tk_action = NULL;
1364
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001365}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001367static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1368{
1369 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1370 nfs4_open_prepare(task, calldata);
1371}
1372
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001373static void nfs4_open_done(struct rpc_task *task, void *calldata)
1374{
1375 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001377 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001378
Trond Myklebustd61e6122009-12-05 19:32:19 -05001379 nfs4_sequence_done(data->o_arg.server, &data->o_res.seq_res,
1380 task->tk_status);
Andy Adamsond8985282009-04-01 09:22:21 -04001381
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001382 if (RPC_ASSASSINATED(task))
1383 return;
1384 if (task->tk_status == 0) {
1385 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001386 case S_IFREG:
1387 break;
1388 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001389 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001390 break;
1391 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001392 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001393 break;
1394 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001395 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001396 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001397 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001398 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1399 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001400 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001401 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001402}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001403
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001404static void nfs4_open_release(void *calldata)
1405{
1406 struct nfs4_opendata *data = calldata;
1407 struct nfs4_state *state = NULL;
1408
1409 /* If this request hasn't been cancelled, do nothing */
1410 if (data->cancelled == 0)
1411 goto out_free;
1412 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001413 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001414 goto out_free;
1415 /* In case we need an open_confirm, no cleanup! */
1416 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1417 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001418 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001419 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001420 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001421out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001422 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001423}
1424
1425static const struct rpc_call_ops nfs4_open_ops = {
1426 .rpc_call_prepare = nfs4_open_prepare,
1427 .rpc_call_done = nfs4_open_done,
1428 .rpc_release = nfs4_open_release,
1429};
1430
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001431static const struct rpc_call_ops nfs4_recover_open_ops = {
1432 .rpc_call_prepare = nfs4_recover_open_prepare,
1433 .rpc_call_done = nfs4_open_done,
1434 .rpc_release = nfs4_open_release,
1435};
1436
1437static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001438{
1439 struct inode *dir = data->dir->d_inode;
1440 struct nfs_server *server = NFS_SERVER(dir);
1441 struct nfs_openargs *o_arg = &data->o_arg;
1442 struct nfs_openres *o_res = &data->o_res;
1443 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001444 struct rpc_message msg = {
1445 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1446 .rpc_argp = o_arg,
1447 .rpc_resp = o_res,
1448 .rpc_cred = data->owner->so_cred,
1449 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001450 struct rpc_task_setup task_setup_data = {
1451 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001452 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001453 .callback_ops = &nfs4_open_ops,
1454 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001455 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001456 .flags = RPC_TASK_ASYNC,
1457 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001458 int status;
1459
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001460 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001461 data->rpc_done = 0;
1462 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001463 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001464 if (isrecover)
1465 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001466 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001467 if (IS_ERR(task))
1468 return PTR_ERR(task);
1469 status = nfs4_wait_for_completion_rpc_task(task);
1470 if (status != 0) {
1471 data->cancelled = 1;
1472 smp_wmb();
1473 } else
1474 status = data->rpc_status;
1475 rpc_put_task(task);
1476
1477 return status;
1478}
1479
1480static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1481{
1482 struct inode *dir = data->dir->d_inode;
1483 struct nfs_openres *o_res = &data->o_res;
1484 int status;
1485
1486 status = nfs4_run_open_task(data, 1);
1487 if (status != 0 || !data->rpc_done)
1488 return status;
1489
1490 nfs_refresh_inode(dir, o_res->dir_attr);
1491
1492 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1493 status = _nfs4_proc_open_confirm(data);
1494 if (status != 0)
1495 return status;
1496 }
1497
1498 return status;
1499}
1500
1501/*
1502 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1503 */
1504static int _nfs4_proc_open(struct nfs4_opendata *data)
1505{
1506 struct inode *dir = data->dir->d_inode;
1507 struct nfs_server *server = NFS_SERVER(dir);
1508 struct nfs_openargs *o_arg = &data->o_arg;
1509 struct nfs_openres *o_res = &data->o_res;
1510 int status;
1511
1512 status = nfs4_run_open_task(data, 0);
Trond Myklebust3e309912007-07-07 13:19:59 -04001513 if (status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001514 return status;
1515
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001516 if (o_arg->open_flags & O_CREAT) {
1517 update_changeattr(dir, &o_res->cinfo);
1518 nfs_post_op_update_inode(dir, o_res->dir_attr);
1519 } else
1520 nfs_refresh_inode(dir, o_res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001522 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001524 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 }
1526 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001527 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001528 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529}
1530
Trond Myklebust10afec92007-05-14 17:16:04 -04001531static int nfs4_recover_expired_lease(struct nfs_server *server)
Trond Myklebust58d97142006-01-03 09:55:24 +01001532{
David Howells7539bba2006-08-22 20:06:09 -04001533 struct nfs_client *clp = server->nfs_client;
Trond Myklebusta78cb572009-08-09 15:06:19 -04001534 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001535 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001536
Trond Myklebusta78cb572009-08-09 15:06:19 -04001537 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001538 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001539 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001540 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001541 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1542 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001543 break;
Trond Myklebust58d97142006-01-03 09:55:24 +01001544 nfs4_schedule_state_recovery(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001545 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001546 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001547 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001548}
1549
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550/*
1551 * OPEN_EXPIRED:
1552 * reclaim state on the server after a network partition.
1553 * Assumes caller holds the appropriate lock
1554 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001555static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001557 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001558 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001560 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1561 if (IS_ERR(opendata))
1562 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001563 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001564 if (ret == -ESTALE)
Trond Myklebust539cd032007-06-05 11:46:42 -04001565 d_drop(ctx->path.dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001566 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001567 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568}
1569
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001570static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001571{
Trond Myklebust539cd032007-06-05 11:46:42 -04001572 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001573 struct nfs4_exception exception = { };
1574 int err;
1575
1576 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001577 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001578 switch (err) {
1579 default:
1580 goto out;
1581 case -NFS4ERR_GRACE:
1582 case -NFS4ERR_DELAY:
1583 nfs4_handle_exception(server, err, &exception);
1584 err = 0;
1585 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001586 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001587out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001588 return err;
1589}
1590
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1592{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001594 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
Trond Myklebust864472e2006-01-03 09:55:15 +01001596 ctx = nfs4_state_find_open_context(state);
1597 if (IS_ERR(ctx))
1598 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001599 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001600 put_nfs_open_context(ctx);
1601 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602}
1603
1604/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001605 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1606 * fields corresponding to attributes that were used to store the verifier.
1607 * Make sure we clobber those fields in the later setattr call
1608 */
1609static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1610{
1611 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1612 !(sattr->ia_valid & ATTR_ATIME_SET))
1613 sattr->ia_valid |= ATTR_ATIME;
1614
1615 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1616 !(sattr->ia_valid & ATTR_MTIME_SET))
1617 sattr->ia_valid |= ATTR_MTIME;
1618}
1619
1620/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001621 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001623static 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 -07001624{
1625 struct nfs4_state_owner *sp;
1626 struct nfs4_state *state = NULL;
1627 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001628 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001629 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
1631 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 status = -ENOMEM;
1633 if (!(sp = nfs4_get_state_owner(server, cred))) {
1634 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1635 goto out_err;
1636 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001637 status = nfs4_recover_expired_lease(server);
1638 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001639 goto err_put_state_owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001640 if (path->dentry->d_inode != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001641 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001642 status = -ENOMEM;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001643 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001644 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001645 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
Trond Myklebustaac00a82007-07-05 19:02:21 -04001647 if (path->dentry->d_inode != NULL)
1648 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1649
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001650 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001652 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001654 if (opendata->o_arg.open_flags & O_EXCL)
1655 nfs4_exclusive_attrset(opendata, sattr);
1656
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001657 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001658 status = PTR_ERR(state);
1659 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001660 goto err_opendata_put;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001661 if ((opendata->o_res.rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) != 0)
1662 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001663 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 *res = state;
1666 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001667err_opendata_put:
1668 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001669err_put_state_owner:
1670 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 *res = NULL;
1673 return status;
1674}
1675
1676
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001677static 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 -07001678{
1679 struct nfs4_exception exception = { };
1680 struct nfs4_state *res;
1681 int status;
1682
1683 do {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001684 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 if (status == 0)
1686 break;
1687 /* NOTE: BAD_SEQID means the server and client disagree about the
1688 * book-keeping w.r.t. state-changing operations
1689 * (OPEN/CLOSE/LOCK/LOCKU...)
1690 * It is actually a sign of a bug on the client or on the server.
1691 *
1692 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001693 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 * have unhashed the old state_owner for us, and that we can
1695 * therefore safely retry using a new one. We should still warn
1696 * the user though...
1697 */
1698 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001699 printk(KERN_WARNING "NFS: v4 server %s "
1700 " returned a bad sequence-id error!\n",
1701 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 exception.retry = 1;
1703 continue;
1704 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001705 /*
1706 * BAD_STATEID on OPEN means that the server cancelled our
1707 * state before it received the OPEN_CONFIRM.
1708 * Recover by retrying the request as per the discussion
1709 * on Page 181 of RFC3530.
1710 */
1711 if (status == -NFS4ERR_BAD_STATEID) {
1712 exception.retry = 1;
1713 continue;
1714 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001715 if (status == -EAGAIN) {
1716 /* We must have found a delegation */
1717 exception.retry = 1;
1718 continue;
1719 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1721 status, &exception));
1722 } while (exception.retry);
1723 return res;
1724}
1725
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001726static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1727 struct nfs_fattr *fattr, struct iattr *sattr,
1728 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001730 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001732 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 .iap = sattr,
1734 .server = server,
1735 .bitmask = server->attr_bitmask,
1736 };
1737 struct nfs_setattrres res = {
1738 .fattr = fattr,
1739 .server = server,
1740 };
1741 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001742 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1743 .rpc_argp = &arg,
1744 .rpc_resp = &res,
1745 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001747 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001748 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
Trond Myklebust0e574af2005-10-27 22:12:38 -04001750 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001752 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1753 /* Use that stateid */
1754 } else if (state != NULL) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001755 nfs4_copy_stateid(&arg.stateid, state, current->files);
1756 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1758
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001759 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001760 if (status == 0 && state != NULL)
1761 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001762 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763}
1764
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001765static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1766 struct nfs_fattr *fattr, struct iattr *sattr,
1767 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001769 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 struct nfs4_exception exception = { };
1771 int err;
1772 do {
1773 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001774 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 &exception);
1776 } while (exception.retry);
1777 return err;
1778}
1779
1780struct nfs4_closedata {
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001781 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 struct inode *inode;
1783 struct nfs4_state *state;
1784 struct nfs_closeargs arg;
1785 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001786 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001787 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788};
1789
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001790static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001791{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001792 struct nfs4_closedata *calldata = data;
1793 struct nfs4_state_owner *sp = calldata->state->owner;
Trond Myklebust95121352005-10-18 14:20:12 -07001794
1795 nfs4_put_open_state(calldata->state);
1796 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001797 nfs4_put_state_owner(sp);
Jan Blunck31f31db12008-05-02 13:42:45 -07001798 path_put(&calldata->path);
Trond Myklebust95121352005-10-18 14:20:12 -07001799 kfree(calldata);
1800}
1801
Trond Myklebust88069f72009-12-08 08:33:16 -05001802static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1803 fmode_t fmode)
1804{
1805 spin_lock(&state->owner->so_lock);
1806 if (!(fmode & FMODE_READ))
1807 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1808 if (!(fmode & FMODE_WRITE))
1809 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1810 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1811 spin_unlock(&state->owner->so_lock);
1812}
1813
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001814static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001816 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 struct nfs_server *server = NFS_SERVER(calldata->inode);
1819
Andy Adamson19ddab02009-04-01 09:22:20 -04001820 nfs4_sequence_done(server, &calldata->res.seq_res, task->tk_status);
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001821 if (RPC_ASSASSINATED(task))
1822 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 /* hmm. we are done with the inode, and in the process of freeing
1824 * the state_owner. we keep this around to process errors
1825 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 switch (task->tk_status) {
1827 case 0:
Trond Myklebust45328c32007-07-26 17:47:34 -04001828 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001829 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05001830 nfs4_close_clear_stateid_flags(state,
1831 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 break;
1833 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001834 case -NFS4ERR_OLD_STATEID:
1835 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001837 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001838 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05001840 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1841 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 }
Trond Myklebust72211db2009-12-15 14:47:36 -05001843 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04001844 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845}
1846
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001847static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001849 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001850 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05001851 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07001852
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001853 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001854 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001855
Trond Myklebust88069f72009-12-08 08:33:16 -05001856 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1857 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05001858 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001859 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01001860 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001861 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001862 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
1863 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1864 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04001865 }
1866 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001867 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
1868 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1869 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04001870 }
Trond Myklebuste7616922006-01-03 09:55:13 +01001871 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05001872 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05001873
1874 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001875 /* Note: exit _without_ calling nfs4_close_done */
1876 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07001877 return;
1878 }
Trond Myklebust88069f72009-12-08 08:33:16 -05001879
1880 if (calldata->arg.fmode == 0)
1881 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
1882
Trond Myklebust516a6af2005-10-27 22:12:41 -04001883 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001884 calldata->timestamp = jiffies;
Andy Adamson19ddab02009-04-01 09:22:20 -04001885 if (nfs4_setup_sequence((NFS_SERVER(calldata->inode))->nfs_client,
1886 &calldata->arg.seq_args, &calldata->res.seq_res,
1887 1, task))
1888 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001889 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890}
1891
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001892static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001893 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001894 .rpc_call_done = nfs4_close_done,
1895 .rpc_release = nfs4_free_closedata,
1896};
1897
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898/*
1899 * It is possible for data to be read/written from a mem-mapped file
1900 * after the sys_close call (which hits the vfs layer as a flush).
1901 * This means that we can't safely call nfsv4 close on a file until
1902 * the inode is cleared. This in turn means that we are not good
1903 * NFSv4 citizens - we do not indicate to the server to update the file's
1904 * share state even when we are done with one of the three share
1905 * stateid's in the inode.
1906 *
1907 * NOTE: Caller must be holding the sp->so_owner semaphore!
1908 */
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001909int nfs4_do_close(struct path *path, struct nfs4_state *state, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001911 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04001913 struct nfs4_state_owner *sp = state->owner;
1914 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001915 struct rpc_message msg = {
1916 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1917 .rpc_cred = state->owner->so_cred,
1918 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001919 struct rpc_task_setup task_setup_data = {
1920 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001921 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001922 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05001923 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001924 .flags = RPC_TASK_ASYNC,
1925 };
Trond Myklebust95121352005-10-18 14:20:12 -07001926 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
Andy Adamson19ddab02009-04-01 09:22:20 -04001928 calldata = kzalloc(sizeof(*calldata), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07001930 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001931 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001933 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04001934 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 /* Serialization for the sequence id */
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001936 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001937 if (calldata->arg.seqid == NULL)
1938 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001939 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04001940 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001941 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04001942 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001943 calldata->res.server = server;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04001944 calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001945 calldata->path.mnt = mntget(path->mnt);
1946 calldata->path.dentry = dget(path->dentry);
Trond Myklebust95121352005-10-18 14:20:12 -07001947
Trond Myklebust5138fde2007-07-14 15:40:01 -04001948 msg.rpc_argp = &calldata->arg,
1949 msg.rpc_resp = &calldata->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001950 task_setup_data.callback_data = calldata;
1951 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001952 if (IS_ERR(task))
1953 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001954 status = 0;
1955 if (wait)
1956 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001957 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001958 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07001959out_free_calldata:
1960 kfree(calldata);
1961out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04001962 nfs4_put_open_state(state);
1963 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07001964 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965}
1966
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001967static 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 -07001968{
1969 struct file *filp;
Trond Myklebust1b45c462007-07-03 13:04:56 -04001970 int ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001971
Trond Myklebust1b45c462007-07-03 13:04:56 -04001972 /* If the open_intent is for execute, we have an extra check to make */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001973 if (fmode & FMODE_EXEC) {
Trond Myklebustaf22f942007-08-10 17:45:10 -04001974 ret = nfs_may_open(state->inode,
Trond Myklebust1b45c462007-07-03 13:04:56 -04001975 state->owner->so_cred,
1976 nd->intent.open.flags);
1977 if (ret < 0)
1978 goto out_close;
1979 }
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001980 filp = lookup_instantiate_filp(nd, path->dentry, NULL);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001981 if (!IS_ERR(filp)) {
1982 struct nfs_open_context *ctx;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04001983 ctx = nfs_file_open_context(filp);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001984 ctx->state = state;
Trond Myklebust95cf9592006-04-18 13:14:06 -04001985 return 0;
1986 }
Trond Myklebust1b45c462007-07-03 13:04:56 -04001987 ret = PTR_ERR(filp);
1988out_close:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001989 nfs4_close_sync(path, state, fmode & (FMODE_READ|FMODE_WRITE));
Trond Myklebust1b45c462007-07-03 13:04:56 -04001990 return ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001991}
1992
1993struct dentry *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1995{
Trond Myklebustad389da2007-06-05 12:30:00 -04001996 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08001997 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001998 .dentry = dentry,
1999 };
Jan Blunck4ac91372008-02-14 19:34:32 -08002000 struct dentry *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 struct iattr attr;
2002 struct rpc_cred *cred;
2003 struct nfs4_state *state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002004 struct dentry *res;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002005 fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006
2007 if (nd->flags & LOOKUP_CREATE) {
2008 attr.ia_mode = nd->intent.open.create_mode;
2009 attr.ia_valid = ATTR_MODE;
2010 if (!IS_POSIXACL(dir))
Al Viroce3b0f82009-03-29 19:08:22 -04002011 attr.ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 } else {
2013 attr.ia_valid = 0;
2014 BUG_ON(nd->intent.open.flags & O_CREAT);
2015 }
2016
Trond Myklebust98a8e322008-03-12 12:25:28 -04002017 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 if (IS_ERR(cred))
Trond Myklebust02a913a2005-10-18 14:20:17 -07002019 return (struct dentry *)cred;
Trond Myklebust565277f2007-10-15 18:17:53 -04002020 parent = dentry->d_parent;
2021 /* Protect against concurrent sillydeletes */
2022 nfs_block_sillyrename(parent);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002023 state = nfs4_do_open(dir, &path, fmode, nd->intent.open.flags, &attr, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002025 if (IS_ERR(state)) {
Trond Myklebustd75340c2007-10-01 21:42:01 -04002026 if (PTR_ERR(state) == -ENOENT) {
Trond Myklebust02a913a2005-10-18 14:20:17 -07002027 d_add(dentry, NULL);
Trond Myklebustd75340c2007-10-01 21:42:01 -04002028 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2029 }
Trond Myklebust565277f2007-10-15 18:17:53 -04002030 nfs_unblock_sillyrename(parent);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002031 return (struct dentry *)state;
2032 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002033 res = d_add_unique(dentry, igrab(state->inode));
Trond Myklebust02a913a2005-10-18 14:20:17 -07002034 if (res != NULL)
Trond Myklebustdeee9362007-08-27 11:33:00 -04002035 path.dentry = res;
Trond Myklebustd75340c2007-10-01 21:42:01 -04002036 nfs_set_verifier(path.dentry, nfs_save_change_attribute(dir));
Trond Myklebust565277f2007-10-15 18:17:53 -04002037 nfs_unblock_sillyrename(parent);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002038 nfs4_intent_set_file(nd, &path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002039 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040}
2041
2042int
Trond Myklebust02a913a2005-10-18 14:20:17 -07002043nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044{
Trond Myklebustad389da2007-06-05 12:30:00 -04002045 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08002046 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04002047 .dentry = dentry,
2048 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 struct rpc_cred *cred;
2050 struct nfs4_state *state;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002051 fmode_t fmode = openflags & (FMODE_READ | FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052
Trond Myklebust98a8e322008-03-12 12:25:28 -04002053 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 if (IS_ERR(cred))
2055 return PTR_ERR(cred);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002056 state = nfs4_do_open(dir, &path, fmode, openflags, NULL, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002058 if (IS_ERR(state)) {
2059 switch (PTR_ERR(state)) {
2060 case -EPERM:
2061 case -EACCES:
2062 case -EDQUOT:
2063 case -ENOSPC:
2064 case -EROFS:
2065 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
2066 return 1;
Chuck Leverb87c0ad2006-10-19 23:28:42 -07002067 default:
2068 goto out_drop;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002069 }
Trond Myklebust02a913a2005-10-18 14:20:17 -07002070 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002071 if (state->inode == dentry->d_inode) {
Trond Myklebustd75340c2007-10-01 21:42:01 -04002072 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002073 nfs4_intent_set_file(nd, &path, state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 return 1;
2075 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002076 nfs4_close_sync(&path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002077out_drop:
2078 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 return 0;
2080}
2081
Trond Myklebust1185a552009-12-03 15:54:02 -05002082static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002083{
2084 if (ctx->state == NULL)
2085 return;
2086 if (is_sync)
2087 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
2088 else
2089 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
2090}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091
2092static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2093{
Benny Halevy43652ad2009-04-01 09:21:54 -04002094 struct nfs4_server_caps_arg args = {
2095 .fhandle = fhandle,
2096 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 struct nfs4_server_caps_res res = {};
2098 struct rpc_message msg = {
2099 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002100 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 .rpc_resp = &res,
2102 };
2103 int status;
2104
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002105 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 if (status == 0) {
2107 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002108 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2109 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2110 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2111 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2112 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2114 server->caps |= NFS_CAP_ACLS;
2115 if (res.has_links != 0)
2116 server->caps |= NFS_CAP_HARDLINKS;
2117 if (res.has_symlinks != 0)
2118 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002119 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2120 server->caps |= NFS_CAP_FILEID;
2121 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2122 server->caps |= NFS_CAP_MODE;
2123 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2124 server->caps |= NFS_CAP_NLINK;
2125 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2126 server->caps |= NFS_CAP_OWNER;
2127 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2128 server->caps |= NFS_CAP_OWNER_GROUP;
2129 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2130 server->caps |= NFS_CAP_ATIME;
2131 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2132 server->caps |= NFS_CAP_CTIME;
2133 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2134 server->caps |= NFS_CAP_MTIME;
2135
Trond Myklebusta65318b2009-03-11 14:10:28 -04002136 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2137 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2138 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 server->acl_bitmask = res.acl_bitmask;
2140 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002141
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 return status;
2143}
2144
Trond Myklebust55a97592006-06-09 09:34:19 -04002145int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146{
2147 struct nfs4_exception exception = { };
2148 int err;
2149 do {
2150 err = nfs4_handle_exception(server,
2151 _nfs4_server_capabilities(server, fhandle),
2152 &exception);
2153 } while (exception.retry);
2154 return err;
2155}
2156
2157static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2158 struct nfs_fsinfo *info)
2159{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 struct nfs4_lookup_root_arg args = {
2161 .bitmask = nfs4_fattr_bitmap,
2162 };
2163 struct nfs4_lookup_res res = {
2164 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002165 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 .fh = fhandle,
2167 };
2168 struct rpc_message msg = {
2169 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2170 .rpc_argp = &args,
2171 .rpc_resp = &res,
2172 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002173
Trond Myklebust0e574af2005-10-27 22:12:38 -04002174 nfs_fattr_init(info->fattr);
Trond Myklebustfccba802009-07-21 16:48:07 -04002175 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176}
2177
2178static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2179 struct nfs_fsinfo *info)
2180{
2181 struct nfs4_exception exception = { };
2182 int err;
2183 do {
2184 err = nfs4_handle_exception(server,
2185 _nfs4_lookup_root(server, fhandle, info),
2186 &exception);
2187 } while (exception.retry);
2188 return err;
2189}
2190
David Howells54ceac42006-08-22 20:06:13 -04002191/*
2192 * get the file handle for the "/" directory on the server
2193 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002195 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 int status;
2198
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 status = nfs4_lookup_root(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 if (status == 0)
2201 status = nfs4_server_capabilities(server, fhandle);
2202 if (status == 0)
2203 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002204 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205}
2206
Manoj Naik6b97fd32006-06-09 09:34:29 -04002207/*
2208 * Get locations and (maybe) other attributes of a referral.
2209 * Note that we'll actually follow the referral later when
2210 * we detect fsid mismatch in inode revalidation
2211 */
Trond Myklebust56659e92007-07-17 21:52:39 -04002212static 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 -04002213{
2214 int status = -ENOMEM;
2215 struct page *page = NULL;
2216 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002217
2218 page = alloc_page(GFP_KERNEL);
2219 if (page == NULL)
2220 goto out;
2221 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2222 if (locations == NULL)
2223 goto out;
2224
Trond Myklebustc228fd32007-01-13 02:28:11 -05002225 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002226 if (status != 0)
2227 goto out;
2228 /* Make sure server returned a different fsid for the referral */
2229 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -07002230 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 -04002231 status = -EIO;
2232 goto out;
2233 }
2234
2235 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2236 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
2237 if (!fattr->mode)
2238 fattr->mode = S_IFDIR;
2239 memset(fhandle, 0, sizeof(struct nfs_fh));
2240out:
2241 if (page)
2242 __free_page(page);
2243 if (locations)
2244 kfree(locations);
2245 return status;
2246}
2247
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2249{
2250 struct nfs4_getattr_arg args = {
2251 .fh = fhandle,
2252 .bitmask = server->attr_bitmask,
2253 };
2254 struct nfs4_getattr_res res = {
2255 .fattr = fattr,
2256 .server = server,
2257 };
2258 struct rpc_message msg = {
2259 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2260 .rpc_argp = &args,
2261 .rpc_resp = &res,
2262 };
2263
Trond Myklebust0e574af2005-10-27 22:12:38 -04002264 nfs_fattr_init(fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002265 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266}
2267
2268static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2269{
2270 struct nfs4_exception exception = { };
2271 int err;
2272 do {
2273 err = nfs4_handle_exception(server,
2274 _nfs4_proc_getattr(server, fhandle, fattr),
2275 &exception);
2276 } while (exception.retry);
2277 return err;
2278}
2279
2280/*
2281 * The file is not closed if it is opened due to the a request to change
2282 * the size of the file. The open call will not be needed once the
2283 * VFS layer lookup-intents are implemented.
2284 *
2285 * Close is called when the inode is destroyed.
2286 * If we haven't opened the file for O_WRONLY, we
2287 * need to in the size_change case to obtain a stateid.
2288 *
2289 * Got race?
2290 * Because OPEN is always done by name in nfsv4, it is
2291 * possible that we opened a different file by the same
2292 * name. We can recognize this race condition, but we
2293 * can't do anything about it besides returning an error.
2294 *
2295 * This will be fixed with VFS changes (lookup-intent).
2296 */
2297static int
2298nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2299 struct iattr *sattr)
2300{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002301 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002302 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002303 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 int status;
2305
Trond Myklebust0e574af2005-10-27 22:12:38 -04002306 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307
Trond Myklebustd5308382005-11-04 15:33:38 -05002308 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002309 if (sattr->ia_valid & ATTR_FILE) {
2310 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002311
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002312 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002313 if (ctx) {
2314 cred = ctx->cred;
2315 state = ctx->state;
2316 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002317 }
2318
2319 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002320 if (status == 0)
2321 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 return status;
2323}
2324
Trond Myklebust56659e92007-07-17 21:52:39 -04002325static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
2326 const struct qstr *name, struct nfs_fh *fhandle,
David Howells2b3de442006-08-22 20:06:09 -04002327 struct nfs_fattr *fattr)
2328{
2329 int status;
2330 struct nfs4_lookup_arg args = {
2331 .bitmask = server->attr_bitmask,
2332 .dir_fh = dirfh,
2333 .name = name,
2334 };
2335 struct nfs4_lookup_res res = {
2336 .server = server,
2337 .fattr = fattr,
2338 .fh = fhandle,
2339 };
2340 struct rpc_message msg = {
2341 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2342 .rpc_argp = &args,
2343 .rpc_resp = &res,
2344 };
2345
2346 nfs_fattr_init(fattr);
2347
2348 dprintk("NFS call lookupfh %s\n", name->name);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002349 status = nfs4_call_sync(server, &msg, &args, &res, 0);
David Howells2b3de442006-08-22 20:06:09 -04002350 dprintk("NFS reply lookupfh: %d\n", status);
David Howells2b3de442006-08-22 20:06:09 -04002351 return status;
2352}
2353
2354static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2355 struct qstr *name, struct nfs_fh *fhandle,
2356 struct nfs_fattr *fattr)
2357{
2358 struct nfs4_exception exception = { };
2359 int err;
2360 do {
Trond Myklebust4e56e082007-07-01 18:13:52 -04002361 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
2362 /* FIXME: !!!! */
2363 if (err == -NFS4ERR_MOVED) {
2364 err = -EREMOTE;
2365 break;
2366 }
2367 err = nfs4_handle_exception(server, err, &exception);
David Howells2b3de442006-08-22 20:06:09 -04002368 } while (exception.retry);
2369 return err;
2370}
2371
Trond Myklebust56659e92007-07-17 21:52:39 -04002372static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2374{
Trond Myklebust4e56e082007-07-01 18:13:52 -04002375 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376
2377 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust4e56e082007-07-01 18:13:52 -04002378 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002379 if (status == -NFS4ERR_MOVED)
2380 status = nfs4_get_referral(dir, name, fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 dprintk("NFS reply lookup: %d\n", status);
2382 return status;
2383}
2384
2385static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2386{
2387 struct nfs4_exception exception = { };
2388 int err;
2389 do {
2390 err = nfs4_handle_exception(NFS_SERVER(dir),
2391 _nfs4_proc_lookup(dir, name, fhandle, fattr),
2392 &exception);
2393 } while (exception.retry);
2394 return err;
2395}
2396
2397static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2398{
Trond Myklebust76b32992007-08-10 17:45:11 -04002399 struct nfs_server *server = NFS_SERVER(inode);
2400 struct nfs_fattr fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 struct nfs4_accessargs args = {
2402 .fh = NFS_FH(inode),
Trond Myklebust76b32992007-08-10 17:45:11 -04002403 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002405 struct nfs4_accessres res = {
2406 .server = server,
2407 .fattr = &fattr,
2408 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 struct rpc_message msg = {
2410 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2411 .rpc_argp = &args,
2412 .rpc_resp = &res,
2413 .rpc_cred = entry->cred,
2414 };
2415 int mode = entry->mask;
2416 int status;
2417
2418 /*
2419 * Determine which access bits we want to ask for...
2420 */
2421 if (mode & MAY_READ)
2422 args.access |= NFS4_ACCESS_READ;
2423 if (S_ISDIR(inode->i_mode)) {
2424 if (mode & MAY_WRITE)
2425 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2426 if (mode & MAY_EXEC)
2427 args.access |= NFS4_ACCESS_LOOKUP;
2428 } else {
2429 if (mode & MAY_WRITE)
2430 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2431 if (mode & MAY_EXEC)
2432 args.access |= NFS4_ACCESS_EXECUTE;
2433 }
Trond Myklebust76b32992007-08-10 17:45:11 -04002434 nfs_fattr_init(&fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002435 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 if (!status) {
2437 entry->mask = 0;
2438 if (res.access & NFS4_ACCESS_READ)
2439 entry->mask |= MAY_READ;
2440 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2441 entry->mask |= MAY_WRITE;
2442 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2443 entry->mask |= MAY_EXEC;
Trond Myklebust76b32992007-08-10 17:45:11 -04002444 nfs_refresh_inode(inode, &fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 }
2446 return status;
2447}
2448
2449static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2450{
2451 struct nfs4_exception exception = { };
2452 int err;
2453 do {
2454 err = nfs4_handle_exception(NFS_SERVER(inode),
2455 _nfs4_proc_access(inode, entry),
2456 &exception);
2457 } while (exception.retry);
2458 return err;
2459}
2460
2461/*
2462 * TODO: For the time being, we don't try to get any attributes
2463 * along with any of the zero-copy operations READ, READDIR,
2464 * READLINK, WRITE.
2465 *
2466 * In the case of the first three, we want to put the GETATTR
2467 * after the read-type operation -- this is because it is hard
2468 * to predict the length of a GETATTR response in v4, and thus
2469 * align the READ data correctly. This means that the GETATTR
2470 * may end up partially falling into the page cache, and we should
2471 * shift it into the 'tail' of the xdr_buf before processing.
2472 * To do this efficiently, we need to know the total length
2473 * of data received, which doesn't seem to be available outside
2474 * of the RPC layer.
2475 *
2476 * In the case of WRITE, we also want to put the GETATTR after
2477 * the operation -- in this case because we want to make sure
2478 * we get the post-operation mtime and size. This means that
2479 * we can't use xdr_encode_pages() as written: we need a variant
2480 * of it which would leave room in the 'tail' iovec.
2481 *
2482 * Both of these changes to the XDR layer would in fact be quite
2483 * minor, but I decided to leave them for a subsequent patch.
2484 */
2485static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2486 unsigned int pgbase, unsigned int pglen)
2487{
2488 struct nfs4_readlink args = {
2489 .fh = NFS_FH(inode),
2490 .pgbase = pgbase,
2491 .pglen = pglen,
2492 .pages = &page,
2493 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002494 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 struct rpc_message msg = {
2496 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2497 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002498 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 };
2500
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002501 return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502}
2503
2504static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2505 unsigned int pgbase, unsigned int pglen)
2506{
2507 struct nfs4_exception exception = { };
2508 int err;
2509 do {
2510 err = nfs4_handle_exception(NFS_SERVER(inode),
2511 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2512 &exception);
2513 } while (exception.retry);
2514 return err;
2515}
2516
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517/*
2518 * Got race?
2519 * We will need to arrange for the VFS layer to provide an atomic open.
2520 * Until then, this create/open method is prone to inefficiency and race
2521 * conditions due to the lookup, create, and open VFS calls from sys_open()
2522 * placed on the wire.
2523 *
2524 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2525 * The file will be opened again in the subsequent VFS open call
2526 * (nfs4_proc_file_open).
2527 *
2528 * The open for read will just hang around to be used by any process that
2529 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2530 */
2531
2532static int
2533nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebust02a913a2005-10-18 14:20:17 -07002534 int flags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535{
Trond Myklebustad389da2007-06-05 12:30:00 -04002536 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08002537 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04002538 .dentry = dentry,
2539 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 struct nfs4_state *state;
2541 struct rpc_cred *cred;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002542 fmode_t fmode = flags & (FMODE_READ | FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 int status = 0;
2544
Trond Myklebust98a8e322008-03-12 12:25:28 -04002545 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 if (IS_ERR(cred)) {
2547 status = PTR_ERR(cred);
2548 goto out;
2549 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002550 state = nfs4_do_open(dir, &path, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002551 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 if (IS_ERR(state)) {
2553 status = PTR_ERR(state);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002554 goto out_putcred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002556 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002557 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 if (flags & O_EXCL) {
2559 struct nfs_fattr fattr;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002560 status = nfs4_do_setattr(state->inode, cred, &fattr, sattr, state);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002561 if (status == 0)
Trond Myklebust65e43082005-08-16 11:49:44 -04002562 nfs_setattr_update_inode(state->inode, sattr);
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002563 nfs_post_op_update_inode(state->inode, &fattr);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002564 }
Trond Myklebustad389da2007-06-05 12:30:00 -04002565 if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002566 status = nfs4_intent_set_file(nd, &path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002567 else
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002568 nfs4_close_sync(&path, state, fmode);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002569out_putcred:
2570 put_rpccred(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571out:
2572 return status;
2573}
2574
2575static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2576{
Trond Myklebust16e42952005-10-27 22:12:44 -04002577 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002578 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002580 .name.len = name->len,
2581 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002582 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002584 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002585 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002586 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002588 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2589 .rpc_argp = &args,
2590 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 };
2592 int status;
2593
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002594 nfs_fattr_init(&res.dir_attr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002595 status = nfs4_call_sync(server, &msg, &args, &res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002596 if (status == 0) {
2597 update_changeattr(dir, &res.cinfo);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002598 nfs_post_op_update_inode(dir, &res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002599 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 return status;
2601}
2602
2603static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2604{
2605 struct nfs4_exception exception = { };
2606 int err;
2607 do {
2608 err = nfs4_handle_exception(NFS_SERVER(dir),
2609 _nfs4_proc_remove(dir, name),
2610 &exception);
2611 } while (exception.retry);
2612 return err;
2613}
2614
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002615static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002617 struct nfs_server *server = NFS_SERVER(dir);
2618 struct nfs_removeargs *args = msg->rpc_argp;
2619 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620
Trond Myklebusta65318b2009-03-11 14:10:28 -04002621 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002622 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624}
2625
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002626static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002628 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2629
Andy Adamson472cfbd2009-04-01 09:22:24 -04002630 nfs4_sequence_done(res->server, &res->seq_res, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002631 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002632 return 0;
2633 update_changeattr(dir, &res->cinfo);
2634 nfs_post_op_update_inode(dir, &res->dir_attr);
2635 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636}
2637
2638static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2639 struct inode *new_dir, struct qstr *new_name)
2640{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002641 struct nfs_server *server = NFS_SERVER(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 struct nfs4_rename_arg arg = {
2643 .old_dir = NFS_FH(old_dir),
2644 .new_dir = NFS_FH(new_dir),
2645 .old_name = old_name,
2646 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002647 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 };
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002649 struct nfs_fattr old_fattr, new_fattr;
2650 struct nfs4_rename_res res = {
2651 .server = server,
2652 .old_fattr = &old_fattr,
2653 .new_fattr = &new_fattr,
2654 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 struct rpc_message msg = {
2656 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2657 .rpc_argp = &arg,
2658 .rpc_resp = &res,
2659 };
2660 int status;
2661
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002662 nfs_fattr_init(res.old_fattr);
2663 nfs_fattr_init(res.new_fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002664 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665
2666 if (!status) {
2667 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002668 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002670 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 }
2672 return status;
2673}
2674
2675static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2676 struct inode *new_dir, struct qstr *new_name)
2677{
2678 struct nfs4_exception exception = { };
2679 int err;
2680 do {
2681 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2682 _nfs4_proc_rename(old_dir, old_name,
2683 new_dir, new_name),
2684 &exception);
2685 } while (exception.retry);
2686 return err;
2687}
2688
2689static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2690{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002691 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 struct nfs4_link_arg arg = {
2693 .fh = NFS_FH(inode),
2694 .dir_fh = NFS_FH(dir),
2695 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002696 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002698 struct nfs_fattr fattr, dir_attr;
2699 struct nfs4_link_res res = {
2700 .server = server,
2701 .fattr = &fattr,
2702 .dir_attr = &dir_attr,
2703 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 struct rpc_message msg = {
2705 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2706 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002707 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 };
2709 int status;
2710
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002711 nfs_fattr_init(res.fattr);
2712 nfs_fattr_init(res.dir_attr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002713 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002714 if (!status) {
2715 update_changeattr(dir, &res.cinfo);
2716 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002717 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002718 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719
2720 return status;
2721}
2722
2723static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2724{
2725 struct nfs4_exception exception = { };
2726 int err;
2727 do {
2728 err = nfs4_handle_exception(NFS_SERVER(inode),
2729 _nfs4_proc_link(inode, dir, name),
2730 &exception);
2731 } while (exception.retry);
2732 return err;
2733}
2734
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002735struct nfs4_createdata {
2736 struct rpc_message msg;
2737 struct nfs4_create_arg arg;
2738 struct nfs4_create_res res;
2739 struct nfs_fh fh;
2740 struct nfs_fattr fattr;
2741 struct nfs_fattr dir_fattr;
2742};
2743
2744static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2745 struct qstr *name, struct iattr *sattr, u32 ftype)
2746{
2747 struct nfs4_createdata *data;
2748
2749 data = kzalloc(sizeof(*data), GFP_KERNEL);
2750 if (data != NULL) {
2751 struct nfs_server *server = NFS_SERVER(dir);
2752
2753 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2754 data->msg.rpc_argp = &data->arg;
2755 data->msg.rpc_resp = &data->res;
2756 data->arg.dir_fh = NFS_FH(dir);
2757 data->arg.server = server;
2758 data->arg.name = name;
2759 data->arg.attrs = sattr;
2760 data->arg.ftype = ftype;
2761 data->arg.bitmask = server->attr_bitmask;
2762 data->res.server = server;
2763 data->res.fh = &data->fh;
2764 data->res.fattr = &data->fattr;
2765 data->res.dir_fattr = &data->dir_fattr;
2766 nfs_fattr_init(data->res.fattr);
2767 nfs_fattr_init(data->res.dir_fattr);
2768 }
2769 return data;
2770}
2771
2772static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2773{
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002774 int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
2775 &data->arg, &data->res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002776 if (status == 0) {
2777 update_changeattr(dir, &data->res.dir_cinfo);
2778 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2779 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2780 }
2781 return status;
2782}
2783
2784static void nfs4_free_createdata(struct nfs4_createdata *data)
2785{
2786 kfree(data);
2787}
2788
Chuck Lever4f390c12006-08-22 20:06:22 -04002789static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002790 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002792 struct nfs4_createdata *data;
2793 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794
Chuck Lever94a6d752006-08-22 20:06:23 -04002795 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002796 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002797
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002798 status = -ENOMEM;
2799 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2800 if (data == NULL)
2801 goto out;
2802
2803 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2804 data->arg.u.symlink.pages = &page;
2805 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002807 status = nfs4_do_create(dir, dentry, data);
2808
2809 nfs4_free_createdata(data);
2810out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 return status;
2812}
2813
Chuck Lever4f390c12006-08-22 20:06:22 -04002814static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002815 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816{
2817 struct nfs4_exception exception = { };
2818 int err;
2819 do {
2820 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002821 _nfs4_proc_symlink(dir, dentry, page,
2822 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 &exception);
2824 } while (exception.retry);
2825 return err;
2826}
2827
2828static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2829 struct iattr *sattr)
2830{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002831 struct nfs4_createdata *data;
2832 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002834 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2835 if (data == NULL)
2836 goto out;
2837
2838 status = nfs4_do_create(dir, dentry, data);
2839
2840 nfs4_free_createdata(data);
2841out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 return status;
2843}
2844
2845static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2846 struct iattr *sattr)
2847{
2848 struct nfs4_exception exception = { };
2849 int err;
2850 do {
2851 err = nfs4_handle_exception(NFS_SERVER(dir),
2852 _nfs4_proc_mkdir(dir, dentry, sattr),
2853 &exception);
2854 } while (exception.retry);
2855 return err;
2856}
2857
2858static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2859 u64 cookie, struct page *page, unsigned int count, int plus)
2860{
2861 struct inode *dir = dentry->d_inode;
2862 struct nfs4_readdir_arg args = {
2863 .fh = NFS_FH(dir),
2864 .pages = &page,
2865 .pgbase = 0,
2866 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09002867 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 };
2869 struct nfs4_readdir_res res;
2870 struct rpc_message msg = {
2871 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2872 .rpc_argp = &args,
2873 .rpc_resp = &res,
2874 .rpc_cred = cred,
2875 };
2876 int status;
2877
Harvey Harrison3110ff82008-05-02 13:42:44 -07002878 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00002879 dentry->d_parent->d_name.name,
2880 dentry->d_name.name,
2881 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2883 res.pgbase = args.pgbase;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002884 status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 if (status == 0)
2886 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustc4812992007-09-28 17:11:45 -04002887
2888 nfs_invalidate_atime(dir);
2889
Harvey Harrison3110ff82008-05-02 13:42:44 -07002890 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 return status;
2892}
2893
2894static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2895 u64 cookie, struct page *page, unsigned int count, int plus)
2896{
2897 struct nfs4_exception exception = { };
2898 int err;
2899 do {
2900 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2901 _nfs4_proc_readdir(dentry, cred, cookie,
2902 page, count, plus),
2903 &exception);
2904 } while (exception.retry);
2905 return err;
2906}
2907
2908static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2909 struct iattr *sattr, dev_t rdev)
2910{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002911 struct nfs4_createdata *data;
2912 int mode = sattr->ia_mode;
2913 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914
2915 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2916 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002917
2918 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2919 if (data == NULL)
2920 goto out;
2921
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002923 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002925 data->arg.ftype = NF4BLK;
2926 data->arg.u.device.specdata1 = MAJOR(rdev);
2927 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 }
2929 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002930 data->arg.ftype = NF4CHR;
2931 data->arg.u.device.specdata1 = MAJOR(rdev);
2932 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002935 status = nfs4_do_create(dir, dentry, data);
2936
2937 nfs4_free_createdata(data);
2938out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 return status;
2940}
2941
2942static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2943 struct iattr *sattr, dev_t rdev)
2944{
2945 struct nfs4_exception exception = { };
2946 int err;
2947 do {
2948 err = nfs4_handle_exception(NFS_SERVER(dir),
2949 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2950 &exception);
2951 } while (exception.retry);
2952 return err;
2953}
2954
2955static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2956 struct nfs_fsstat *fsstat)
2957{
2958 struct nfs4_statfs_arg args = {
2959 .fh = fhandle,
2960 .bitmask = server->attr_bitmask,
2961 };
Benny Halevy24ad1482009-04-01 09:21:56 -04002962 struct nfs4_statfs_res res = {
2963 .fsstat = fsstat,
2964 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 struct rpc_message msg = {
2966 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2967 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04002968 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 };
2970
Trond Myklebust0e574af2005-10-27 22:12:38 -04002971 nfs_fattr_init(fsstat->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002972 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973}
2974
2975static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2976{
2977 struct nfs4_exception exception = { };
2978 int err;
2979 do {
2980 err = nfs4_handle_exception(server,
2981 _nfs4_proc_statfs(server, fhandle, fsstat),
2982 &exception);
2983 } while (exception.retry);
2984 return err;
2985}
2986
2987static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2988 struct nfs_fsinfo *fsinfo)
2989{
2990 struct nfs4_fsinfo_arg args = {
2991 .fh = fhandle,
2992 .bitmask = server->attr_bitmask,
2993 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04002994 struct nfs4_fsinfo_res res = {
2995 .fsinfo = fsinfo,
2996 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 struct rpc_message msg = {
2998 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2999 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003000 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 };
3002
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003003 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004}
3005
3006static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3007{
3008 struct nfs4_exception exception = { };
3009 int err;
3010
3011 do {
3012 err = nfs4_handle_exception(server,
3013 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3014 &exception);
3015 } while (exception.retry);
3016 return err;
3017}
3018
3019static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3020{
Trond Myklebust0e574af2005-10-27 22:12:38 -04003021 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3023}
3024
3025static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3026 struct nfs_pathconf *pathconf)
3027{
3028 struct nfs4_pathconf_arg args = {
3029 .fh = fhandle,
3030 .bitmask = server->attr_bitmask,
3031 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003032 struct nfs4_pathconf_res res = {
3033 .pathconf = pathconf,
3034 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 struct rpc_message msg = {
3036 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3037 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003038 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 };
3040
3041 /* None of the pathconf attributes are mandatory to implement */
3042 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3043 memset(pathconf, 0, sizeof(*pathconf));
3044 return 0;
3045 }
3046
Trond Myklebust0e574af2005-10-27 22:12:38 -04003047 nfs_fattr_init(pathconf->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003048 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049}
3050
3051static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3052 struct nfs_pathconf *pathconf)
3053{
3054 struct nfs4_exception exception = { };
3055 int err;
3056
3057 do {
3058 err = nfs4_handle_exception(server,
3059 _nfs4_proc_pathconf(server, fhandle, pathconf),
3060 &exception);
3061 } while (exception.retry);
3062 return err;
3063}
3064
Trond Myklebustec06c092006-03-20 13:44:27 -05003065static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066{
Trond Myklebustec06c092006-03-20 13:44:27 -05003067 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068
Andy Adamsonf11c88a2009-04-01 09:22:25 -04003069 dprintk("--> %s\n", __func__);
3070
Andy Adamsonf11c88a2009-04-01 09:22:25 -04003071 nfs4_sequence_done(server, &data->res.seq_res, task->tk_status);
3072
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003073 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003074 nfs_restart_rpc(task, server->nfs_client);
Trond Myklebustec06c092006-03-20 13:44:27 -05003075 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003077
3078 nfs_invalidate_atime(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003080 renew_lease(server, data->timestamp);
3081 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082}
3083
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003084static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 data->timestamp = jiffies;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003087 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088}
3089
Trond Myklebust788e7a82006-03-20 13:44:27 -05003090static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 struct inode *inode = data->inode;
3093
Andy Adamsondef6ed72009-04-01 09:22:26 -04003094 nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
3095 task->tk_status);
3096
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003097 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003098 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003099 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003101 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003103 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003104 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003105 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106}
3107
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003108static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003110 struct nfs_server *server = NFS_SERVER(data->inode);
3111
Trond Myklebusta65318b2009-03-11 14:10:28 -04003112 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003113 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 data->timestamp = jiffies;
3115
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003116 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117}
3118
Trond Myklebust788e7a82006-03-20 13:44:27 -05003119static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 struct inode *inode = data->inode;
3122
Andy Adamson21d9a852009-04-01 09:22:27 -04003123 nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
3124 task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003125 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003126 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003127 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003129 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003130 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131}
3132
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003133static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003135 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136
Trond Myklebusta65318b2009-03-11 14:10:28 -04003137 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003138 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003139 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140}
3141
3142/*
3143 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3144 * standalone procedure for queueing an asynchronous RENEW.
3145 */
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003146static void nfs4_renew_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147{
David Howellsadfa6f92006-08-22 20:06:08 -04003148 struct nfs_client *clp = (struct nfs_client *)task->tk_msg.rpc_argp;
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003149 unsigned long timestamp = (unsigned long)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150
3151 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003152 /* Unless we're shutting down, schedule state recovery! */
3153 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
3154 nfs4_schedule_state_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 return;
3156 }
3157 spin_lock(&clp->cl_lock);
3158 if (time_before(clp->cl_last_renewal,timestamp))
3159 clp->cl_last_renewal = timestamp;
3160 spin_unlock(&clp->cl_lock);
3161}
3162
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003163static const struct rpc_call_ops nfs4_renew_ops = {
3164 .rpc_call_done = nfs4_renew_done,
3165};
3166
David Howellsadfa6f92006-08-22 20:06:08 -04003167int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168{
3169 struct rpc_message msg = {
3170 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3171 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003172 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 };
3174
3175 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003176 &nfs4_renew_ops, (void *)jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177}
3178
David Howellsadfa6f92006-08-22 20:06:08 -04003179int nfs4_proc_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 unsigned long now = jiffies;
3187 int status;
3188
3189 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3190 if (status < 0)
3191 return status;
3192 spin_lock(&clp->cl_lock);
3193 if (time_before(clp->cl_last_renewal,now))
3194 clp->cl_last_renewal = now;
3195 spin_unlock(&clp->cl_lock);
3196 return 0;
3197}
3198
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003199static inline int nfs4_server_supports_acls(struct nfs_server *server)
3200{
3201 return (server->caps & NFS_CAP_ACLS)
3202 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3203 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3204}
3205
3206/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3207 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3208 * the stack.
3209 */
3210#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3211
3212static void buf_to_pages(const void *buf, size_t buflen,
3213 struct page **pages, unsigned int *pgbase)
3214{
3215 const void *p = buf;
3216
3217 *pgbase = offset_in_page(buf);
3218 p -= *pgbase;
3219 while (p < buf + buflen) {
3220 *(pages++) = virt_to_page(p);
3221 p += PAGE_CACHE_SIZE;
3222 }
3223}
3224
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003225struct nfs4_cached_acl {
3226 int cached;
3227 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003228 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003229};
3230
3231static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003232{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003233 struct nfs_inode *nfsi = NFS_I(inode);
3234
3235 spin_lock(&inode->i_lock);
3236 kfree(nfsi->nfs4_acl);
3237 nfsi->nfs4_acl = acl;
3238 spin_unlock(&inode->i_lock);
3239}
3240
3241static void nfs4_zap_acl_attr(struct inode *inode)
3242{
3243 nfs4_set_cached_acl(inode, NULL);
3244}
3245
3246static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3247{
3248 struct nfs_inode *nfsi = NFS_I(inode);
3249 struct nfs4_cached_acl *acl;
3250 int ret = -ENOENT;
3251
3252 spin_lock(&inode->i_lock);
3253 acl = nfsi->nfs4_acl;
3254 if (acl == NULL)
3255 goto out;
3256 if (buf == NULL) /* user is just asking for length */
3257 goto out_len;
3258 if (acl->cached == 0)
3259 goto out;
3260 ret = -ERANGE; /* see getxattr(2) man page */
3261 if (acl->len > buflen)
3262 goto out;
3263 memcpy(buf, acl->data, acl->len);
3264out_len:
3265 ret = acl->len;
3266out:
3267 spin_unlock(&inode->i_lock);
3268 return ret;
3269}
3270
3271static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3272{
3273 struct nfs4_cached_acl *acl;
3274
3275 if (buf && acl_len <= PAGE_SIZE) {
3276 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3277 if (acl == NULL)
3278 goto out;
3279 acl->cached = 1;
3280 memcpy(acl->data, buf, acl_len);
3281 } else {
3282 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3283 if (acl == NULL)
3284 goto out;
3285 acl->cached = 0;
3286 }
3287 acl->len = acl_len;
3288out:
3289 nfs4_set_cached_acl(inode, acl);
3290}
3291
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003292static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003293{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003294 struct page *pages[NFS4ACL_MAXPAGES];
3295 struct nfs_getaclargs args = {
3296 .fh = NFS_FH(inode),
3297 .acl_pages = pages,
3298 .acl_len = buflen,
3299 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003300 struct nfs_getaclres res = {
3301 .acl_len = buflen,
3302 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003303 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003304 struct rpc_message msg = {
3305 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3306 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003307 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003308 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003309 struct page *localpage = NULL;
3310 int ret;
3311
3312 if (buflen < PAGE_SIZE) {
3313 /* As long as we're doing a round trip to the server anyway,
3314 * let's be prepared for a page of acl data. */
3315 localpage = alloc_page(GFP_KERNEL);
3316 resp_buf = page_address(localpage);
3317 if (localpage == NULL)
3318 return -ENOMEM;
3319 args.acl_pages[0] = localpage;
3320 args.acl_pgbase = 0;
Benny Halevy663c79b2009-04-01 09:21:59 -04003321 args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003322 } else {
3323 resp_buf = buf;
3324 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3325 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003326 ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003327 if (ret)
3328 goto out_free;
Benny Halevy663c79b2009-04-01 09:21:59 -04003329 if (res.acl_len > args.acl_len)
3330 nfs4_write_cached_acl(inode, NULL, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003331 else
Benny Halevy663c79b2009-04-01 09:21:59 -04003332 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003333 if (buf) {
3334 ret = -ERANGE;
Benny Halevy663c79b2009-04-01 09:21:59 -04003335 if (res.acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003336 goto out_free;
3337 if (localpage)
Benny Halevy663c79b2009-04-01 09:21:59 -04003338 memcpy(buf, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003339 }
Benny Halevy663c79b2009-04-01 09:21:59 -04003340 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003341out_free:
3342 if (localpage)
3343 __free_page(localpage);
3344 return ret;
3345}
3346
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003347static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3348{
3349 struct nfs4_exception exception = { };
3350 ssize_t ret;
3351 do {
3352 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3353 if (ret >= 0)
3354 break;
3355 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3356 } while (exception.retry);
3357 return ret;
3358}
3359
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003360static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3361{
3362 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003363 int ret;
3364
3365 if (!nfs4_server_supports_acls(server))
3366 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003367 ret = nfs_revalidate_inode(server, inode);
3368 if (ret < 0)
3369 return ret;
3370 ret = nfs4_read_cached_acl(inode, buf, buflen);
3371 if (ret != -ENOENT)
3372 return ret;
3373 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003374}
3375
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003376static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003377{
3378 struct nfs_server *server = NFS_SERVER(inode);
3379 struct page *pages[NFS4ACL_MAXPAGES];
3380 struct nfs_setaclargs arg = {
3381 .fh = NFS_FH(inode),
3382 .acl_pages = pages,
3383 .acl_len = buflen,
3384 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003385 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003386 struct rpc_message msg = {
3387 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3388 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003389 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003390 };
3391 int ret;
3392
3393 if (!nfs4_server_supports_acls(server))
3394 return -EOPNOTSUPP;
Trond Myklebust642ac542005-10-18 14:20:19 -07003395 nfs_inode_return_delegation(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003396 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003397 ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003398 nfs_access_zap_cache(inode);
3399 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003400 return ret;
3401}
3402
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003403static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3404{
3405 struct nfs4_exception exception = { };
3406 int err;
3407 do {
3408 err = nfs4_handle_exception(NFS_SERVER(inode),
3409 __nfs4_proc_set_acl(inode, buf, buflen),
3410 &exception);
3411 } while (exception.retry);
3412 return err;
3413}
3414
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415static int
Andy Adamson8328d592009-04-01 09:22:35 -04003416_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 -07003417{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 if (!clp || task->tk_status >= 0)
3419 return 0;
3420 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003421 case -NFS4ERR_ADMIN_REVOKED:
3422 case -NFS4ERR_BAD_STATEID:
3423 case -NFS4ERR_OPENMODE:
3424 if (state == NULL)
3425 break;
3426 nfs4_state_mark_reclaim_nograce(clp, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 case -NFS4ERR_STALE_CLIENTID:
3428 case -NFS4ERR_STALE_STATEID:
3429 case -NFS4ERR_EXPIRED:
Trond Myklebust5d008372008-02-22 16:34:17 -05003430 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 nfs4_schedule_state_recovery(clp);
Trond Myklebuste005e802008-12-23 15:21:48 -05003432 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
Trond Myklebustfda13932008-02-22 16:34:12 -05003433 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 task->tk_status = 0;
3435 return -EAGAIN;
Andy Adamson4745e312009-04-01 09:22:42 -04003436#if defined(CONFIG_NFS_V4_1)
3437 case -NFS4ERR_BADSESSION:
3438 case -NFS4ERR_BADSLOT:
3439 case -NFS4ERR_BAD_HIGH_SLOT:
3440 case -NFS4ERR_DEADSESSION:
3441 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3442 case -NFS4ERR_SEQ_FALSE_RETRY:
3443 case -NFS4ERR_SEQ_MISORDERED:
3444 dprintk("%s ERROR %d, Reset session\n", __func__,
3445 task->tk_status);
Andy Adamson0b9e2d42009-12-04 16:02:14 -05003446 nfs4_schedule_state_recovery(clp);
Andy Adamson4745e312009-04-01 09:22:42 -04003447 task->tk_status = 0;
3448 return -EAGAIN;
3449#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 case -NFS4ERR_DELAY:
Andy Adamson8328d592009-04-01 09:22:35 -04003451 if (server)
3452 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003453 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3455 task->tk_status = 0;
3456 return -EAGAIN;
3457 case -NFS4ERR_OLD_STATEID:
3458 task->tk_status = 0;
3459 return -EAGAIN;
3460 }
3461 task->tk_status = nfs4_map_errors(task->tk_status);
3462 return 0;
3463}
3464
Andy Adamson8328d592009-04-01 09:22:35 -04003465static int
3466nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3467{
3468 return _nfs4_async_handle_error(task, server, server->nfs_client, state);
3469}
3470
David Howellsadfa6f92006-08-22 20:06:08 -04003471int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, unsigned short port, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472{
3473 nfs4_verifier sc_verifier;
3474 struct nfs4_setclientid setclientid = {
3475 .sc_verifier = &sc_verifier,
3476 .sc_prog = program,
3477 };
3478 struct rpc_message msg = {
3479 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3480 .rpc_argp = &setclientid,
3481 .rpc_resp = clp,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003482 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483 };
Al Virobc4785c2006-10-19 23:28:51 -07003484 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 int loop = 0;
3486 int status;
3487
Al Virobc4785c2006-10-19 23:28:51 -07003488 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3490 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3491
3492 for(;;) {
3493 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003494 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003495 clp->cl_ipaddr,
3496 rpc_peeraddr2str(clp->cl_rpcclient,
3497 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003498 rpc_peeraddr2str(clp->cl_rpcclient,
3499 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003500 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 clp->cl_id_uniquifier);
3502 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003503 sizeof(setclientid.sc_netid),
3504 rpc_peeraddr2str(clp->cl_rpcclient,
3505 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003507 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 clp->cl_ipaddr, port >> 8, port & 255);
3509
3510 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3511 if (status != -NFS4ERR_CLID_INUSE)
3512 break;
3513 if (signalled())
3514 break;
3515 if (loop++ & 1)
3516 ssleep(clp->cl_lease_time + 1);
3517 else
3518 if (++clp->cl_id_uniquifier == 0)
3519 break;
3520 }
3521 return status;
3522}
3523
David Howellsadfa6f92006-08-22 20:06:08 -04003524static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525{
3526 struct nfs_fsinfo fsinfo;
3527 struct rpc_message msg = {
3528 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
3529 .rpc_argp = clp,
3530 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003531 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 };
3533 unsigned long now;
3534 int status;
3535
3536 now = jiffies;
3537 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3538 if (status == 0) {
3539 spin_lock(&clp->cl_lock);
3540 clp->cl_lease_time = fsinfo.lease_time * HZ;
3541 clp->cl_last_renewal = now;
3542 spin_unlock(&clp->cl_lock);
3543 }
3544 return status;
3545}
3546
David Howellsadfa6f92006-08-22 20:06:08 -04003547int nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
Trond Myklebust51581f32006-03-20 13:44:47 -05003548{
Benny Halevy77e03672008-06-24 20:25:57 +03003549 long timeout = 0;
Trond Myklebust51581f32006-03-20 13:44:47 -05003550 int err;
3551 do {
3552 err = _nfs4_proc_setclientid_confirm(clp, cred);
3553 switch (err) {
3554 case 0:
3555 return err;
3556 case -NFS4ERR_RESOURCE:
3557 /* The IBM lawyers misread another document! */
3558 case -NFS4ERR_DELAY:
3559 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3560 }
3561 } while (err == 0);
3562 return err;
3563}
3564
Trond Myklebustfe650402006-01-03 09:55:18 +01003565struct nfs4_delegreturndata {
3566 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003567 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003568 struct nfs_fh fh;
3569 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003570 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003571 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003572 int rpc_status;
3573};
3574
Trond Myklebustfe650402006-01-03 09:55:18 +01003575static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3576{
3577 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003578
Trond Myklebustd61e6122009-12-05 19:32:19 -05003579 nfs4_sequence_done(data->res.server, &data->res.seq_res,
3580 task->tk_status);
Andy Adamson938e1012009-04-01 09:22:28 -04003581
Ricardo Labiaga79708862009-12-07 09:23:21 -05003582 switch (task->tk_status) {
3583 case -NFS4ERR_STALE_STATEID:
3584 case -NFS4ERR_EXPIRED:
3585 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01003586 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003587 break;
3588 default:
3589 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3590 -EAGAIN) {
3591 nfs_restart_rpc(task, data->res.server->nfs_client);
3592 return;
3593 }
3594 }
3595 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01003596}
3597
3598static void nfs4_delegreturn_release(void *calldata)
3599{
Trond Myklebustfe650402006-01-03 09:55:18 +01003600 kfree(calldata);
3601}
3602
Andy Adamson938e1012009-04-01 09:22:28 -04003603#if defined(CONFIG_NFS_V4_1)
3604static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3605{
3606 struct nfs4_delegreturndata *d_data;
3607
3608 d_data = (struct nfs4_delegreturndata *)data;
3609
3610 if (nfs4_setup_sequence(d_data->res.server->nfs_client,
3611 &d_data->args.seq_args,
3612 &d_data->res.seq_res, 1, task))
3613 return;
3614 rpc_call_start(task);
3615}
3616#endif /* CONFIG_NFS_V4_1 */
3617
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003618static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04003619#if defined(CONFIG_NFS_V4_1)
3620 .rpc_call_prepare = nfs4_delegreturn_prepare,
3621#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01003622 .rpc_call_done = nfs4_delegreturn_done,
3623 .rpc_release = nfs4_delegreturn_release,
3624};
3625
Trond Myklebuste6f81072008-01-24 18:14:34 -05003626static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003627{
3628 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003629 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003630 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003631 struct rpc_message msg = {
3632 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3633 .rpc_cred = cred,
3634 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003635 struct rpc_task_setup task_setup_data = {
3636 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003637 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003638 .callback_ops = &nfs4_delegreturn_ops,
3639 .flags = RPC_TASK_ASYNC,
3640 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003641 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003642
Andy Adamson938e1012009-04-01 09:22:28 -04003643 data = kzalloc(sizeof(*data), GFP_KERNEL);
Trond Myklebustfe650402006-01-03 09:55:18 +01003644 if (data == NULL)
3645 return -ENOMEM;
3646 data->args.fhandle = &data->fh;
3647 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003648 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003649 nfs_copy_fh(&data->fh, NFS_FH(inode));
3650 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003651 data->res.fattr = &data->fattr;
3652 data->res.server = server;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003653 data->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003654 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003655 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003656 data->rpc_status = 0;
3657
Trond Myklebustc970aa82007-07-14 15:39:59 -04003658 task_setup_data.callback_data = data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003659 msg.rpc_argp = &data->args,
3660 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003661 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003662 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003663 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003664 if (!issync)
3665 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003666 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003667 if (status != 0)
3668 goto out;
3669 status = data->rpc_status;
3670 if (status != 0)
3671 goto out;
3672 nfs_refresh_inode(inode, &data->fattr);
3673out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003674 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003675 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676}
3677
Trond Myklebuste6f81072008-01-24 18:14:34 -05003678int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679{
3680 struct nfs_server *server = NFS_SERVER(inode);
3681 struct nfs4_exception exception = { };
3682 int err;
3683 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003684 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 switch (err) {
3686 case -NFS4ERR_STALE_STATEID:
3687 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688 case 0:
3689 return 0;
3690 }
3691 err = nfs4_handle_exception(server, err, &exception);
3692 } while (exception.retry);
3693 return err;
3694}
3695
3696#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3697#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3698
3699/*
3700 * sleep, with exponential backoff, and retry the LOCK operation.
3701 */
3702static unsigned long
3703nfs4_set_lock_task_retry(unsigned long timeout)
3704{
Matthew Wilcox150030b2007-12-06 16:24:39 -05003705 schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 timeout <<= 1;
3707 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3708 return NFS4_LOCK_MAXTIMEOUT;
3709 return timeout;
3710}
3711
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3713{
3714 struct inode *inode = state->inode;
3715 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003716 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003717 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003719 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003721 struct nfs_lockt_res res = {
3722 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 };
3724 struct rpc_message msg = {
3725 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3726 .rpc_argp = &arg,
3727 .rpc_resp = &res,
3728 .rpc_cred = state->owner->so_cred,
3729 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730 struct nfs4_lock_state *lsp;
3731 int status;
3732
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003733 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003734 status = nfs4_set_lock_state(state, request);
3735 if (status != 0)
3736 goto out;
3737 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003738 arg.lock_owner.id = lsp->ls_id.id;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003739 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003740 switch (status) {
3741 case 0:
3742 request->fl_type = F_UNLCK;
3743 break;
3744 case -NFS4ERR_DENIED:
3745 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05003747 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003748out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749 return status;
3750}
3751
3752static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3753{
3754 struct nfs4_exception exception = { };
3755 int err;
3756
3757 do {
3758 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3759 _nfs4_proc_getlk(state, cmd, request),
3760 &exception);
3761 } while (exception.retry);
3762 return err;
3763}
3764
3765static int do_vfs_lock(struct file *file, struct file_lock *fl)
3766{
3767 int res = 0;
3768 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3769 case FL_POSIX:
3770 res = posix_lock_file_wait(file, fl);
3771 break;
3772 case FL_FLOCK:
3773 res = flock_lock_file_wait(file, fl);
3774 break;
3775 default:
3776 BUG();
3777 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778 return res;
3779}
3780
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003781struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003782 struct nfs_locku_args arg;
3783 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003784 struct nfs4_lock_state *lsp;
3785 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003786 struct file_lock fl;
3787 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003788 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003789};
3790
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003791static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3792 struct nfs_open_context *ctx,
3793 struct nfs4_lock_state *lsp,
3794 struct nfs_seqid *seqid)
3795{
3796 struct nfs4_unlockdata *p;
3797 struct inode *inode = lsp->ls_state->inode;
3798
Andy Adamsona8936932009-04-01 09:22:23 -04003799 p = kzalloc(sizeof(*p), GFP_KERNEL);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003800 if (p == NULL)
3801 return NULL;
3802 p->arg.fh = NFS_FH(inode);
3803 p->arg.fl = &p->fl;
3804 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003805 p->res.seqid = seqid;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003806 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003807 p->arg.stateid = &lsp->ls_stateid;
3808 p->lsp = lsp;
3809 atomic_inc(&lsp->ls_count);
3810 /* Ensure we don't close file until we're done freeing locks! */
3811 p->ctx = get_nfs_open_context(ctx);
3812 memcpy(&p->fl, fl, sizeof(p->fl));
3813 p->server = NFS_SERVER(inode);
3814 return p;
3815}
3816
Trond Myklebust06f814a2006-01-03 09:55:07 +01003817static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003818{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003819 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003820 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003821 nfs4_put_lock_state(calldata->lsp);
3822 put_nfs_open_context(calldata->ctx);
3823 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003824}
3825
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003826static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003827{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003828 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003829
Andy Adamsona8936932009-04-01 09:22:23 -04003830 nfs4_sequence_done(calldata->server, &calldata->res.seq_res,
3831 task->tk_status);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003832 if (RPC_ASSASSINATED(task))
3833 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003834 switch (task->tk_status) {
3835 case 0:
3836 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003837 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003838 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01003839 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003840 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003841 case -NFS4ERR_BAD_STATEID:
3842 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003843 case -NFS4ERR_STALE_STATEID:
3844 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003845 break;
3846 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003847 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebust0110ee12009-12-07 09:00:24 -05003848 nfs_restart_rpc(task,
Trond Myklebustd61e6122009-12-05 19:32:19 -05003849 calldata->server->nfs_client);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003850 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003851}
3852
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003853static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003854{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003855 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003857 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003858 return;
3859 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003860 /* Note: exit _without_ running nfs4_locku_done */
3861 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003862 return;
3863 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01003864 calldata->timestamp = jiffies;
Andy Adamsona8936932009-04-01 09:22:23 -04003865 if (nfs4_setup_sequence(calldata->server->nfs_client,
3866 &calldata->arg.seq_args,
3867 &calldata->res.seq_res, 1, task))
3868 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003869 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870}
3871
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003872static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003873 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003874 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01003875 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003876};
3877
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003878static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3879 struct nfs_open_context *ctx,
3880 struct nfs4_lock_state *lsp,
3881 struct nfs_seqid *seqid)
3882{
3883 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003884 struct rpc_message msg = {
3885 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3886 .rpc_cred = ctx->cred,
3887 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003888 struct rpc_task_setup task_setup_data = {
3889 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04003890 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003891 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003892 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003893 .flags = RPC_TASK_ASYNC,
3894 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003895
Frank Filz137d6ac2007-07-09 15:32:29 -07003896 /* Ensure this is an unlock - when canceling a lock, the
3897 * canceled lock is passed in, and it won't be an unlock.
3898 */
3899 fl->fl_type = F_UNLCK;
3900
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003901 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3902 if (data == NULL) {
3903 nfs_free_seqid(seqid);
3904 return ERR_PTR(-ENOMEM);
3905 }
3906
Trond Myklebust5138fde2007-07-14 15:40:01 -04003907 msg.rpc_argp = &data->arg,
3908 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003909 task_setup_data.callback_data = data;
3910 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003911}
3912
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3914{
Trond Myklebust19e03c52008-12-23 15:21:44 -05003915 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003916 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003917 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01003918 struct rpc_task *task;
3919 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003920 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921
Trond Myklebust9b073572006-06-29 16:38:34 -04003922 status = nfs4_set_lock_state(state, request);
3923 /* Unlock _before_ we do the RPC call */
3924 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003925 down_read(&nfsi->rwsem);
3926 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
3927 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003928 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003929 }
3930 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003931 if (status != 0)
3932 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003933 /* Is this a delegated lock? */
3934 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04003935 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003936 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003937 seqid = nfs_alloc_seqid(&lsp->ls_seqid);
Trond Myklebust9b073572006-06-29 16:38:34 -04003938 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003939 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04003940 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04003941 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003942 status = PTR_ERR(task);
3943 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04003944 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003945 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003946 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04003947out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003948 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003949 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950}
3951
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003952struct nfs4_lockdata {
3953 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003954 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003955 struct nfs4_lock_state *lsp;
3956 struct nfs_open_context *ctx;
3957 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003958 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003959 int rpc_status;
3960 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04003961 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003962};
3963
3964static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
3965 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp)
3966{
3967 struct nfs4_lockdata *p;
3968 struct inode *inode = lsp->ls_state->inode;
3969 struct nfs_server *server = NFS_SERVER(inode);
3970
3971 p = kzalloc(sizeof(*p), GFP_KERNEL);
3972 if (p == NULL)
3973 return NULL;
3974
3975 p->arg.fh = NFS_FH(inode);
3976 p->arg.fl = &p->fl;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003977 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid);
3978 if (p->arg.open_seqid == NULL)
3979 goto out_free;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003980 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3981 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003982 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003983 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04003984 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003985 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003986 p->res.lock_seqid = p->arg.lock_seqid;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003987 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003988 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04003989 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003990 atomic_inc(&lsp->ls_count);
3991 p->ctx = get_nfs_open_context(ctx);
3992 memcpy(&p->fl, fl, sizeof(p->fl));
3993 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003994out_free_seqid:
3995 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003996out_free:
3997 kfree(p);
3998 return NULL;
3999}
4000
4001static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4002{
4003 struct nfs4_lockdata *data = calldata;
4004 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005
Harvey Harrison3110ff82008-05-02 13:42:44 -07004006 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004007 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4008 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004009 /* Do we need to do an open_to_lock_owner? */
4010 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004011 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4012 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004013 data->arg.open_stateid = &state->stateid;
4014 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004015 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004016 } else
4017 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004018 data->timestamp = jiffies;
Andy Adamson66179ef2009-04-01 09:22:22 -04004019 if (nfs4_setup_sequence(data->server->nfs_client, &data->arg.seq_args,
4020 &data->res.seq_res, 1, task))
4021 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004022 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004023 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004024}
4025
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004026static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4027{
4028 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4029 nfs4_lock_prepare(task, calldata);
4030}
4031
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004032static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4033{
4034 struct nfs4_lockdata *data = calldata;
4035
Harvey Harrison3110ff82008-05-02 13:42:44 -07004036 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004037
Trond Myklebustd61e6122009-12-05 19:32:19 -05004038 nfs4_sequence_done(data->server, &data->res.seq_res,
4039 task->tk_status);
Andy Adamson66179ef2009-04-01 09:22:22 -04004040
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004041 data->rpc_status = task->tk_status;
4042 if (RPC_ASSASSINATED(task))
4043 goto out;
4044 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004045 if (data->rpc_status == 0)
4046 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4047 else
4048 goto out;
4049 }
4050 if (data->rpc_status == 0) {
4051 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4052 sizeof(data->lsp->ls_stateid.data));
4053 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Trond Myklebust88be9f92007-06-05 10:42:27 -04004054 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004055 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004056out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004057 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004058}
4059
4060static void nfs4_lock_release(void *calldata)
4061{
4062 struct nfs4_lockdata *data = calldata;
4063
Harvey Harrison3110ff82008-05-02 13:42:44 -07004064 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004065 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004066 if (data->cancelled != 0) {
4067 struct rpc_task *task;
4068 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4069 data->arg.lock_seqid);
4070 if (!IS_ERR(task))
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004071 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004072 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004073 } else
4074 nfs_free_seqid(data->arg.lock_seqid);
4075 nfs4_put_lock_state(data->lsp);
4076 put_nfs_open_context(data->ctx);
4077 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004078 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004079}
4080
4081static const struct rpc_call_ops nfs4_lock_ops = {
4082 .rpc_call_prepare = nfs4_lock_prepare,
4083 .rpc_call_done = nfs4_lock_done,
4084 .rpc_release = nfs4_lock_release,
4085};
4086
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004087static const struct rpc_call_ops nfs4_recover_lock_ops = {
4088 .rpc_call_prepare = nfs4_recover_lock_prepare,
4089 .rpc_call_done = nfs4_lock_done,
4090 .rpc_release = nfs4_lock_release,
4091};
4092
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004093static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4094{
4095 struct nfs_client *clp = server->nfs_client;
4096 struct nfs4_state *state = lsp->ls_state;
4097
4098 switch (error) {
4099 case -NFS4ERR_ADMIN_REVOKED:
4100 case -NFS4ERR_BAD_STATEID:
4101 case -NFS4ERR_EXPIRED:
4102 if (new_lock_owner != 0 ||
4103 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4104 nfs4_state_mark_reclaim_nograce(clp, state);
4105 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4106 };
4107}
4108
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004109static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004110{
4111 struct nfs4_lockdata *data;
4112 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004113 struct rpc_message msg = {
4114 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4115 .rpc_cred = state->owner->so_cred,
4116 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004117 struct rpc_task_setup task_setup_data = {
4118 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004119 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004120 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004121 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004122 .flags = RPC_TASK_ASYNC,
4123 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004124 int ret;
4125
Harvey Harrison3110ff82008-05-02 13:42:44 -07004126 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004127 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004128 fl->fl_u.nfs4_fl.owner);
4129 if (data == NULL)
4130 return -ENOMEM;
4131 if (IS_SETLKW(cmd))
4132 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004133 if (recovery_type > NFS_LOCK_NEW) {
4134 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004135 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004136 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4137 }
Trond Myklebust5138fde2007-07-14 15:40:01 -04004138 msg.rpc_argp = &data->arg,
4139 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004140 task_setup_data.callback_data = data;
4141 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004142 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004143 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004144 ret = nfs4_wait_for_completion_rpc_task(task);
4145 if (ret == 0) {
4146 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004147 if (ret)
4148 nfs4_handle_setlk_error(data->server, data->lsp,
4149 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004150 } else
4151 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004152 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004153 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004154 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155}
4156
4157static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4158{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004159 struct nfs_server *server = NFS_SERVER(state->inode);
4160 struct nfs4_exception exception = { };
4161 int err;
4162
4163 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004164 /* Cache the lock if possible... */
4165 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4166 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004167 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust202b50d2005-06-22 17:16:29 +00004168 if (err != -NFS4ERR_DELAY)
4169 break;
4170 nfs4_handle_exception(server, err, &exception);
4171 } while (exception.retry);
4172 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173}
4174
4175static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4176{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004177 struct nfs_server *server = NFS_SERVER(state->inode);
4178 struct nfs4_exception exception = { };
4179 int err;
4180
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004181 err = nfs4_set_lock_state(state, request);
4182 if (err != 0)
4183 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004184 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004185 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4186 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004187 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004188 switch (err) {
4189 default:
4190 goto out;
4191 case -NFS4ERR_GRACE:
4192 case -NFS4ERR_DELAY:
4193 nfs4_handle_exception(server, err, &exception);
4194 err = 0;
4195 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004196 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004197out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004198 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199}
4200
4201static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4202{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004203 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004204 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004205 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004207 if ((fl_flags & FL_POSIX) &&
4208 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4209 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004210 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004211 status = nfs4_set_lock_state(state, request);
4212 if (status != 0)
4213 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004214 request->fl_flags |= FL_ACCESS;
4215 status = do_vfs_lock(request->fl_file, request);
4216 if (status < 0)
4217 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004218 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004219 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004220 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004221 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004222 request->fl_flags = fl_flags & ~FL_SLEEP;
4223 status = do_vfs_lock(request->fl_file, request);
4224 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004225 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004226 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004227 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004228 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004229 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004230 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004231 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07004232 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004233out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004234 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004235out:
4236 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237 return status;
4238}
4239
4240static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4241{
4242 struct nfs4_exception exception = { };
4243 int err;
4244
4245 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004246 err = _nfs4_proc_setlk(state, cmd, request);
4247 if (err == -NFS4ERR_DENIED)
4248 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004250 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251 } while (exception.retry);
4252 return err;
4253}
4254
4255static int
4256nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4257{
4258 struct nfs_open_context *ctx;
4259 struct nfs4_state *state;
4260 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4261 int status;
4262
4263 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004264 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 state = ctx->state;
4266
4267 if (request->fl_start < 0 || request->fl_end < 0)
4268 return -EINVAL;
4269
Trond Myklebustd9531262009-07-21 19:22:38 -04004270 if (IS_GETLK(cmd)) {
4271 if (state != NULL)
4272 return nfs4_proc_getlk(state, F_GETLK, request);
4273 return 0;
4274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275
4276 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4277 return -EINVAL;
4278
Trond Myklebustd9531262009-07-21 19:22:38 -04004279 if (request->fl_type == F_UNLCK) {
4280 if (state != NULL)
4281 return nfs4_proc_unlck(state, cmd, request);
4282 return 0;
4283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284
Trond Myklebustd9531262009-07-21 19:22:38 -04004285 if (state == NULL)
4286 return -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 do {
4288 status = nfs4_proc_setlk(state, cmd, request);
4289 if ((status != -EAGAIN) || IS_SETLK(cmd))
4290 break;
4291 timeout = nfs4_set_lock_task_retry(timeout);
4292 status = -ERESTARTSYS;
4293 if (signalled())
4294 break;
4295 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 return status;
4297}
4298
Trond Myklebust888e6942005-11-04 15:38:11 -05004299int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4300{
4301 struct nfs_server *server = NFS_SERVER(state->inode);
4302 struct nfs4_exception exception = { };
4303 int err;
4304
4305 err = nfs4_set_lock_state(state, fl);
4306 if (err != 0)
4307 goto out;
4308 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004309 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004310 switch (err) {
4311 default:
4312 printk(KERN_ERR "%s: unhandled error %d.\n",
4313 __func__, err);
4314 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004315 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004316 goto out;
4317 case -NFS4ERR_EXPIRED:
4318 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004319 case -NFS4ERR_STALE_STATEID:
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004320 case -NFS4ERR_BADSESSION:
4321 case -NFS4ERR_BADSLOT:
4322 case -NFS4ERR_BAD_HIGH_SLOT:
4323 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4324 case -NFS4ERR_DEADSESSION:
Trond Myklebustd5122202009-06-17 13:22:58 -07004325 nfs4_schedule_state_recovery(server->nfs_client);
4326 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004327 case -ERESTARTSYS:
4328 /*
4329 * The show must go on: exit, but mark the
4330 * stateid as needing recovery.
4331 */
4332 case -NFS4ERR_ADMIN_REVOKED:
4333 case -NFS4ERR_BAD_STATEID:
4334 case -NFS4ERR_OPENMODE:
4335 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
4336 err = 0;
4337 goto out;
4338 case -ENOMEM:
4339 case -NFS4ERR_DENIED:
4340 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4341 err = 0;
4342 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004343 case -NFS4ERR_DELAY:
4344 break;
4345 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004346 err = nfs4_handle_exception(server, err, &exception);
4347 } while (exception.retry);
4348out:
4349 return err;
4350}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004351
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004352#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4353
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004354int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
4355 size_t buflen, int flags)
4356{
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004357 struct inode *inode = dentry->d_inode;
4358
4359 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4360 return -EOPNOTSUPP;
4361
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004362 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004363}
4364
4365/* The getxattr man page suggests returning -ENODATA for unknown attributes,
4366 * and that's what we'll do for e.g. user attributes that haven't been set.
4367 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
4368 * attributes in kernel-managed attribute namespaces. */
4369ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
4370 size_t buflen)
4371{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004372 struct inode *inode = dentry->d_inode;
4373
4374 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4375 return -EOPNOTSUPP;
4376
4377 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004378}
4379
4380ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
4381{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004382 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004383
J. Bruce Fields096455a2006-03-20 23:23:42 -05004384 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4385 return 0;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004386 if (buf && buflen < len)
4387 return -ERANGE;
4388 if (buf)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004389 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
4390 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004391}
4392
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004393static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4394{
4395 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
4396 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4397 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4398 return;
4399
4400 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4401 NFS_ATTR_FATTR_NLINK;
4402 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4403 fattr->nlink = 2;
4404}
4405
Trond Myklebust56659e92007-07-17 21:52:39 -04004406int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004407 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004408{
4409 struct nfs_server *server = NFS_SERVER(dir);
4410 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004411 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4412 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004413 };
4414 struct nfs4_fs_locations_arg args = {
4415 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004416 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004417 .page = page,
4418 .bitmask = bitmask,
4419 };
Benny Halevy22958462009-04-01 09:22:02 -04004420 struct nfs4_fs_locations_res res = {
4421 .fs_locations = fs_locations,
4422 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004423 struct rpc_message msg = {
4424 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4425 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004426 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004427 };
4428 int status;
4429
Harvey Harrison3110ff82008-05-02 13:42:44 -07004430 dprintk("%s: start\n", __func__);
Trond Myklebustc228fd32007-01-13 02:28:11 -05004431 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004432 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004433 fs_locations->nlocations = 0;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04004434 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004435 nfs_fixup_referral_attributes(&fs_locations->fattr);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004436 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004437 return status;
4438}
4439
Andy Adamson557134a2009-04-01 09:21:53 -04004440#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04004441/*
4442 * nfs4_proc_exchange_id()
4443 *
4444 * Since the clientid has expired, all compounds using sessions
4445 * associated with the stale clientid will be returning
4446 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4447 * be in some phase of session reset.
4448 */
Andy Adamson4d643d12009-12-04 15:52:24 -05004449int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004450{
4451 nfs4_verifier verifier;
4452 struct nfs41_exchange_id_args args = {
4453 .client = clp,
4454 .flags = clp->cl_exchange_flags,
4455 };
4456 struct nfs41_exchange_id_res res = {
4457 .client = clp,
4458 };
4459 int status;
4460 struct rpc_message msg = {
4461 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4462 .rpc_argp = &args,
4463 .rpc_resp = &res,
4464 .rpc_cred = cred,
4465 };
4466 __be32 *p;
4467
4468 dprintk("--> %s\n", __func__);
4469 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04004470
Alexandros Batsakis7b183d02009-12-05 13:33:25 -05004471 /* Remove server-only flags */
4472 args.flags &= ~EXCHGID4_FLAG_CONFIRMED_R;
4473
Benny Halevy99fe60d2009-04-01 09:22:29 -04004474 p = (u32 *)verifier.data;
4475 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4476 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4477 args.verifier = &verifier;
4478
4479 while (1) {
4480 args.id_len = scnprintf(args.id, sizeof(args.id),
4481 "%s/%s %u",
4482 clp->cl_ipaddr,
4483 rpc_peeraddr2str(clp->cl_rpcclient,
4484 RPC_DISPLAY_ADDR),
4485 clp->cl_id_uniquifier);
4486
4487 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
4488
4489 if (status != NFS4ERR_CLID_INUSE)
4490 break;
4491
4492 if (signalled())
4493 break;
4494
4495 if (++clp->cl_id_uniquifier == 0)
4496 break;
4497 }
4498
4499 dprintk("<-- %s status= %d\n", __func__, status);
4500 return status;
4501}
4502
Andy Adamson2050f0c2009-04-01 09:22:30 -04004503struct nfs4_get_lease_time_data {
4504 struct nfs4_get_lease_time_args *args;
4505 struct nfs4_get_lease_time_res *res;
4506 struct nfs_client *clp;
4507};
4508
4509static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4510 void *calldata)
4511{
4512 int ret;
4513 struct nfs4_get_lease_time_data *data =
4514 (struct nfs4_get_lease_time_data *)calldata;
4515
4516 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004517 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004518 /* just setup sequence, do not trigger session recovery
4519 since we're invoked within one */
4520 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004521 &data->args->la_seq_args,
4522 &data->res->lr_seq_res, 0, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004523
4524 BUG_ON(ret == -EAGAIN);
4525 rpc_call_start(task);
4526 dprintk("<-- %s\n", __func__);
4527}
4528
4529/*
4530 * Called from nfs4_state_manager thread for session setup, so don't recover
4531 * from sequence operation or clientid errors.
4532 */
4533static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4534{
4535 struct nfs4_get_lease_time_data *data =
4536 (struct nfs4_get_lease_time_data *)calldata;
4537
4538 dprintk("--> %s\n", __func__);
4539 nfs41_sequence_done(data->clp, &data->res->lr_seq_res, task->tk_status);
4540 switch (task->tk_status) {
4541 case -NFS4ERR_DELAY:
4542 case -NFS4ERR_GRACE:
4543 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4544 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4545 task->tk_status = 0;
Trond Myklebust0110ee12009-12-07 09:00:24 -05004546 nfs_restart_rpc(task, data->clp);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004547 return;
4548 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04004549 dprintk("<-- %s\n", __func__);
4550}
4551
4552struct rpc_call_ops nfs4_get_lease_time_ops = {
4553 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4554 .rpc_call_done = nfs4_get_lease_time_done,
4555};
4556
4557int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4558{
4559 struct rpc_task *task;
4560 struct nfs4_get_lease_time_args args;
4561 struct nfs4_get_lease_time_res res = {
4562 .lr_fsinfo = fsinfo,
4563 };
4564 struct nfs4_get_lease_time_data data = {
4565 .args = &args,
4566 .res = &res,
4567 .clp = clp,
4568 };
4569 struct rpc_message msg = {
4570 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4571 .rpc_argp = &args,
4572 .rpc_resp = &res,
4573 };
4574 struct rpc_task_setup task_setup = {
4575 .rpc_client = clp->cl_rpcclient,
4576 .rpc_message = &msg,
4577 .callback_ops = &nfs4_get_lease_time_ops,
4578 .callback_data = &data
4579 };
4580 int status;
4581
4582 res.lr_seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
4583 dprintk("--> %s\n", __func__);
4584 task = rpc_run_task(&task_setup);
4585
4586 if (IS_ERR(task))
4587 status = PTR_ERR(task);
4588 else {
4589 status = task->tk_status;
4590 rpc_put_task(task);
4591 }
4592 dprintk("<-- %s return %d\n", __func__, status);
4593
4594 return status;
4595}
4596
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004597/*
4598 * Reset a slot table
4599 */
4600static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, int max_slots,
4601 int old_max_slots, int ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004602{
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004603 int i;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004604 int ret = 0;
4605
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004606 dprintk("--> %s: max_reqs=%u, tbl %p\n", __func__, max_slots, tbl);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004607
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004608 /*
4609 * Until we have dynamic slot table adjustment, insist
4610 * upon the same slot table size
4611 */
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004612 if (max_slots != old_max_slots) {
4613 dprintk("%s reset slot table does't match old\n",
4614 __func__);
4615 ret = -EINVAL; /*XXX NFS4ERR_REQ_TOO_BIG ? */
4616 goto out;
4617 }
4618 spin_lock(&tbl->slot_tbl_lock);
4619 for (i = 0; i < max_slots; ++i)
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004620 tbl->slots[i].seq_nr = ivalue;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004621 spin_unlock(&tbl->slot_tbl_lock);
4622 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4623 tbl, tbl->slots, tbl->max_slots);
4624out:
4625 dprintk("<-- %s: return %d\n", __func__, ret);
4626 return ret;
4627}
4628
Andy Adamsonfc931582009-04-01 09:22:31 -04004629/*
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004630 * Reset the forechannel and backchannel slot tables
4631 */
4632static int nfs4_reset_slot_tables(struct nfs4_session *session)
4633{
4634 int status;
4635
4636 status = nfs4_reset_slot_table(&session->fc_slot_table,
4637 session->fc_attrs.max_reqs,
4638 session->fc_slot_table.max_slots,
4639 1);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004640 if (status)
4641 return status;
4642
4643 status = nfs4_reset_slot_table(&session->bc_slot_table,
4644 session->bc_attrs.max_reqs,
4645 session->bc_slot_table.max_slots,
4646 0);
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004647 return status;
4648}
4649
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004650/* Destroy the slot table */
4651static void nfs4_destroy_slot_tables(struct nfs4_session *session)
4652{
4653 if (session->fc_slot_table.slots != NULL) {
4654 kfree(session->fc_slot_table.slots);
4655 session->fc_slot_table.slots = NULL;
4656 }
4657 if (session->bc_slot_table.slots != NULL) {
4658 kfree(session->bc_slot_table.slots);
4659 session->bc_slot_table.slots = NULL;
4660 }
4661 return;
4662}
4663
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004664/*
Andy Adamsonfc931582009-04-01 09:22:31 -04004665 * Initialize slot table
4666 */
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004667static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
4668 int max_slots, int ivalue)
Andy Adamsonfc931582009-04-01 09:22:31 -04004669{
Andy Adamsonfc931582009-04-01 09:22:31 -04004670 struct nfs4_slot *slot;
4671 int ret = -ENOMEM;
4672
4673 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
4674
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004675 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
Andy Adamsonfc931582009-04-01 09:22:31 -04004676
4677 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_KERNEL);
4678 if (!slot)
4679 goto out;
Andy Adamsonfc931582009-04-01 09:22:31 -04004680 ret = 0;
4681
4682 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonfc931582009-04-01 09:22:31 -04004683 tbl->max_slots = max_slots;
4684 tbl->slots = slot;
4685 tbl->highest_used_slotid = -1; /* no slot is currently used */
4686 spin_unlock(&tbl->slot_tbl_lock);
4687 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4688 tbl, tbl->slots, tbl->max_slots);
4689out:
4690 dprintk("<-- %s: return %d\n", __func__, ret);
4691 return ret;
Andy Adamsonfc931582009-04-01 09:22:31 -04004692}
4693
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004694/*
4695 * Initialize the forechannel and backchannel tables
4696 */
4697static int nfs4_init_slot_tables(struct nfs4_session *session)
4698{
Trond Myklebustf26468f2009-12-05 19:32:11 -05004699 struct nfs4_slot_table *tbl;
4700 int status = 0;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004701
Trond Myklebustf26468f2009-12-05 19:32:11 -05004702 tbl = &session->fc_slot_table;
4703 if (tbl->slots == NULL) {
4704 status = nfs4_init_slot_table(tbl,
4705 session->fc_attrs.max_reqs, 1);
4706 if (status)
4707 return status;
4708 }
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004709
Trond Myklebustf26468f2009-12-05 19:32:11 -05004710 tbl = &session->bc_slot_table;
4711 if (tbl->slots == NULL) {
4712 status = nfs4_init_slot_table(tbl,
4713 session->bc_attrs.max_reqs, 0);
4714 if (status)
4715 nfs4_destroy_slot_tables(session);
4716 }
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004717
4718 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04004719}
4720
4721struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
4722{
4723 struct nfs4_session *session;
4724 struct nfs4_slot_table *tbl;
4725
4726 session = kzalloc(sizeof(struct nfs4_session), GFP_KERNEL);
4727 if (!session)
4728 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04004729
Andy Adamson76db6d92009-04-01 09:22:38 -04004730 /*
4731 * The create session reply races with the server back
4732 * channel probe. Mark the client NFS_CS_SESSION_INITING
4733 * so that the client back channel can find the
4734 * nfs_client struct
4735 */
4736 clp->cl_cons_state = NFS_CS_SESSION_INITING;
Andy Adamsonea028ac2009-12-04 15:55:38 -05004737 init_completion(&session->complete);
Andy Adamson76db6d92009-04-01 09:22:38 -04004738
Andy Adamson557134a2009-04-01 09:21:53 -04004739 tbl = &session->fc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05004740 tbl->highest_used_slotid = -1;
Andy Adamson557134a2009-04-01 09:21:53 -04004741 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004742 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004743
4744 tbl = &session->bc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05004745 tbl->highest_used_slotid = -1;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004746 spin_lock_init(&tbl->slot_tbl_lock);
4747 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
4748
Andy Adamson557134a2009-04-01 09:21:53 -04004749 session->clp = clp;
4750 return session;
4751}
4752
4753void nfs4_destroy_session(struct nfs4_session *session)
4754{
Andy Adamson5a0ffe52009-04-01 09:23:18 -04004755 nfs4_proc_destroy_session(session);
4756 dprintk("%s Destroy backchannel for xprt %p\n",
4757 __func__, session->clp->cl_rpcclient->cl_xprt);
4758 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
4759 NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004760 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04004761 kfree(session);
4762}
4763
Andy Adamsonfc931582009-04-01 09:22:31 -04004764/*
4765 * Initialize the values to be used by the client in CREATE_SESSION
4766 * If nfs4_init_session set the fore channel request and response sizes,
4767 * use them.
4768 *
4769 * Set the back channel max_resp_sz_cached to zero to force the client to
4770 * always set csa_cachethis to FALSE because the current implementation
4771 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
4772 */
4773static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
4774{
4775 struct nfs4_session *session = args->client->cl_session;
4776 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
4777 mxresp_sz = session->fc_attrs.max_resp_sz;
4778
4779 if (mxrqst_sz == 0)
4780 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
4781 if (mxresp_sz == 0)
4782 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
4783 /* Fore channel attributes */
4784 args->fc_attrs.headerpadsz = 0;
4785 args->fc_attrs.max_rqst_sz = mxrqst_sz;
4786 args->fc_attrs.max_resp_sz = mxresp_sz;
4787 args->fc_attrs.max_resp_sz_cached = mxresp_sz;
4788 args->fc_attrs.max_ops = NFS4_MAX_OPS;
4789 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
4790
4791 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
4792 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4793 __func__,
4794 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
4795 args->fc_attrs.max_resp_sz_cached, args->fc_attrs.max_ops,
4796 args->fc_attrs.max_reqs);
4797
4798 /* Back channel attributes */
4799 args->bc_attrs.headerpadsz = 0;
4800 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
4801 args->bc_attrs.max_resp_sz = PAGE_SIZE;
4802 args->bc_attrs.max_resp_sz_cached = 0;
4803 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
4804 args->bc_attrs.max_reqs = 1;
4805
4806 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
4807 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4808 __func__,
4809 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
4810 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
4811 args->bc_attrs.max_reqs);
4812}
4813
Andy Adamson8d353012009-04-01 09:22:32 -04004814static int _verify_channel_attr(char *chan, char *attr_name, u32 sent, u32 rcvd)
4815{
4816 if (rcvd <= sent)
4817 return 0;
4818 printk(KERN_WARNING "%s: Session INVALID: %s channel %s increased. "
4819 "sent=%u rcvd=%u\n", __func__, chan, attr_name, sent, rcvd);
4820 return -EINVAL;
4821}
4822
4823#define _verify_fore_channel_attr(_name_) \
4824 _verify_channel_attr("fore", #_name_, \
4825 args->fc_attrs._name_, \
4826 session->fc_attrs._name_)
4827
4828#define _verify_back_channel_attr(_name_) \
4829 _verify_channel_attr("back", #_name_, \
4830 args->bc_attrs._name_, \
4831 session->bc_attrs._name_)
4832
4833/*
4834 * The server is not allowed to increase the fore channel header pad size,
4835 * maximum response size, or maximum number of operations.
4836 *
4837 * The back channel attributes are only negotiatied down: We send what the
4838 * (back channel) server insists upon.
4839 */
4840static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
4841 struct nfs4_session *session)
4842{
4843 int ret = 0;
4844
4845 ret |= _verify_fore_channel_attr(headerpadsz);
4846 ret |= _verify_fore_channel_attr(max_resp_sz);
4847 ret |= _verify_fore_channel_attr(max_ops);
4848
4849 ret |= _verify_back_channel_attr(headerpadsz);
4850 ret |= _verify_back_channel_attr(max_rqst_sz);
4851 ret |= _verify_back_channel_attr(max_resp_sz);
4852 ret |= _verify_back_channel_attr(max_resp_sz_cached);
4853 ret |= _verify_back_channel_attr(max_ops);
4854 ret |= _verify_back_channel_attr(max_reqs);
4855
4856 return ret;
4857}
4858
Andy Adamsonfc931582009-04-01 09:22:31 -04004859static int _nfs4_proc_create_session(struct nfs_client *clp)
4860{
4861 struct nfs4_session *session = clp->cl_session;
4862 struct nfs41_create_session_args args = {
4863 .client = clp,
4864 .cb_program = NFS4_CALLBACK,
4865 };
4866 struct nfs41_create_session_res res = {
4867 .client = clp,
4868 };
4869 struct rpc_message msg = {
4870 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
4871 .rpc_argp = &args,
4872 .rpc_resp = &res,
4873 };
4874 int status;
4875
4876 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04004877 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04004878
4879 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4880
Andy Adamson8d353012009-04-01 09:22:32 -04004881 if (!status)
4882 /* Verify the session's negotiated channel_attrs values */
4883 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04004884 if (!status) {
4885 /* Increment the clientid slot sequence id */
4886 clp->cl_seqid++;
4887 }
4888
4889 return status;
4890}
4891
4892/*
4893 * Issues a CREATE_SESSION operation to the server.
4894 * It is the responsibility of the caller to verify the session is
4895 * expired before calling this routine.
4896 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05004897int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04004898{
4899 int status;
4900 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04004901 struct nfs4_session *session = clp->cl_session;
4902
4903 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
4904
4905 status = _nfs4_proc_create_session(clp);
4906 if (status)
4907 goto out;
4908
Trond Myklebustf26468f2009-12-05 19:32:11 -05004909 /* Init and reset the fore channel */
4910 status = nfs4_init_slot_tables(session);
4911 dprintk("slot table initialization returned %d\n", status);
4912 if (status)
4913 goto out;
4914 status = nfs4_reset_slot_tables(session);
4915 dprintk("slot table reset returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04004916 if (status)
4917 goto out;
4918
4919 ptr = (unsigned *)&session->sess_id.data[0];
4920 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
4921 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04004922out:
4923 dprintk("<-- %s\n", __func__);
4924 return status;
4925}
4926
Andy Adamson0f3e66c2009-04-01 09:22:34 -04004927/*
4928 * Issue the over-the-wire RPC DESTROY_SESSION.
4929 * The caller must serialize access to this routine.
4930 */
4931int nfs4_proc_destroy_session(struct nfs4_session *session)
4932{
4933 int status = 0;
4934 struct rpc_message msg;
4935
4936 dprintk("--> nfs4_proc_destroy_session\n");
4937
4938 /* session is still being setup */
4939 if (session->clp->cl_cons_state != NFS_CS_READY)
4940 return status;
4941
4942 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
4943 msg.rpc_argp = session;
4944 msg.rpc_resp = NULL;
4945 msg.rpc_cred = NULL;
4946 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4947
4948 if (status)
4949 printk(KERN_WARNING
4950 "Got error %d from the server on DESTROY_SESSION. "
4951 "Session has been destroyed regardless...\n", status);
4952
4953 dprintk("<-- nfs4_proc_destroy_session\n");
4954 return status;
4955}
4956
Trond Myklebustfccba802009-07-21 16:48:07 -04004957int nfs4_init_session(struct nfs_server *server)
4958{
4959 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05004960 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05004961 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04004962 int ret;
4963
4964 if (!nfs4_has_session(clp))
4965 return 0;
4966
Andy Adamson68bf05e2009-12-15 12:55:02 -05004967 rsize = server->rsize;
4968 if (rsize == 0)
4969 rsize = NFS_MAX_FILE_IO_SIZE;
4970 wsize = server->wsize;
4971 if (wsize == 0)
4972 wsize = NFS_MAX_FILE_IO_SIZE;
4973
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05004974 session = clp->cl_session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05004975 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
4976 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05004977
Trond Myklebustfccba802009-07-21 16:48:07 -04004978 ret = nfs4_recover_expired_lease(server);
4979 if (!ret)
4980 ret = nfs4_check_client_ready(clp);
4981 return ret;
4982}
4983
Andy Adamsonfc01cea2009-04-01 09:22:36 -04004984/*
4985 * Renew the cl_session lease.
4986 */
4987static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
4988{
4989 struct nfs4_sequence_args args;
4990 struct nfs4_sequence_res res;
4991
4992 struct rpc_message msg = {
4993 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
4994 .rpc_argp = &args,
4995 .rpc_resp = &res,
4996 .rpc_cred = cred,
4997 };
4998
4999 args.sa_cache_this = 0;
5000
5001 return nfs4_call_sync_sequence(clp, clp->cl_rpcclient, &msg, &args,
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005002 &res, args.sa_cache_this, 1);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005003}
5004
5005void nfs41_sequence_call_done(struct rpc_task *task, void *data)
5006{
5007 struct nfs_client *clp = (struct nfs_client *)data;
5008
5009 nfs41_sequence_done(clp, task->tk_msg.rpc_resp, task->tk_status);
5010
5011 if (task->tk_status < 0) {
5012 dprintk("%s ERROR %d\n", __func__, task->tk_status);
5013
5014 if (_nfs4_async_handle_error(task, NULL, clp, NULL)
5015 == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05005016 nfs_restart_rpc(task, clp);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005017 return;
5018 }
5019 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005020 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
5021
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005022 kfree(task->tk_msg.rpc_argp);
5023 kfree(task->tk_msg.rpc_resp);
5024
5025 dprintk("<-- %s\n", __func__);
5026}
5027
5028static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5029{
5030 struct nfs_client *clp;
5031 struct nfs4_sequence_args *args;
5032 struct nfs4_sequence_res *res;
5033
5034 clp = (struct nfs_client *)data;
5035 args = task->tk_msg.rpc_argp;
5036 res = task->tk_msg.rpc_resp;
5037
5038 if (nfs4_setup_sequence(clp, args, res, 0, task))
5039 return;
5040 rpc_call_start(task);
5041}
5042
5043static const struct rpc_call_ops nfs41_sequence_ops = {
5044 .rpc_call_done = nfs41_sequence_call_done,
5045 .rpc_call_prepare = nfs41_sequence_prepare,
5046};
5047
5048static int nfs41_proc_async_sequence(struct nfs_client *clp,
5049 struct rpc_cred *cred)
5050{
5051 struct nfs4_sequence_args *args;
5052 struct nfs4_sequence_res *res;
5053 struct rpc_message msg = {
5054 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5055 .rpc_cred = cred,
5056 };
5057
5058 args = kzalloc(sizeof(*args), GFP_KERNEL);
5059 if (!args)
5060 return -ENOMEM;
5061 res = kzalloc(sizeof(*res), GFP_KERNEL);
5062 if (!res) {
5063 kfree(args);
5064 return -ENOMEM;
5065 }
5066 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
5067 msg.rpc_argp = args;
5068 msg.rpc_resp = res;
5069
5070 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
5071 &nfs41_sequence_ops, (void *)clp);
5072}
5073
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005074struct nfs4_reclaim_complete_data {
5075 struct nfs_client *clp;
5076 struct nfs41_reclaim_complete_args arg;
5077 struct nfs41_reclaim_complete_res res;
5078};
5079
5080static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5081{
5082 struct nfs4_reclaim_complete_data *calldata = data;
5083
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005084 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005085 if (nfs4_setup_sequence(calldata->clp, &calldata->arg.seq_args,
5086 &calldata->res.seq_res, 0, task))
5087 return;
5088
5089 rpc_call_start(task);
5090}
5091
5092static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5093{
5094 struct nfs4_reclaim_complete_data *calldata = data;
5095 struct nfs_client *clp = calldata->clp;
5096 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5097
5098 dprintk("--> %s\n", __func__);
5099 nfs41_sequence_done(clp, res, task->tk_status);
5100 switch (task->tk_status) {
5101 case 0:
5102 case -NFS4ERR_COMPLETE_ALREADY:
5103 break;
5104 case -NFS4ERR_BADSESSION:
5105 case -NFS4ERR_DEADSESSION:
5106 /*
5107 * Handle the session error, but do not retry the operation, as
5108 * we have no way of telling whether the clientid had to be
5109 * reset before we got our reply. If reset, a new wave of
5110 * reclaim operations will follow, containing their own reclaim
5111 * complete. We don't want our retry to get on the way of
5112 * recovery by incorrectly indicating to the server that we're
5113 * done reclaiming state since the process had to be restarted.
5114 */
5115 _nfs4_async_handle_error(task, NULL, clp, NULL);
5116 break;
5117 default:
5118 if (_nfs4_async_handle_error(
5119 task, NULL, clp, NULL) == -EAGAIN) {
5120 rpc_restart_call_prepare(task);
5121 return;
5122 }
5123 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005124
5125 dprintk("<-- %s\n", __func__);
5126}
5127
5128static void nfs4_free_reclaim_complete_data(void *data)
5129{
5130 struct nfs4_reclaim_complete_data *calldata = data;
5131
5132 kfree(calldata);
5133}
5134
5135static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5136 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5137 .rpc_call_done = nfs4_reclaim_complete_done,
5138 .rpc_release = nfs4_free_reclaim_complete_data,
5139};
5140
5141/*
5142 * Issue a global reclaim complete.
5143 */
5144static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5145{
5146 struct nfs4_reclaim_complete_data *calldata;
5147 struct rpc_task *task;
5148 struct rpc_message msg = {
5149 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5150 };
5151 struct rpc_task_setup task_setup_data = {
5152 .rpc_client = clp->cl_rpcclient,
5153 .rpc_message = &msg,
5154 .callback_ops = &nfs4_reclaim_complete_call_ops,
5155 .flags = RPC_TASK_ASYNC,
5156 };
5157 int status = -ENOMEM;
5158
5159 dprintk("--> %s\n", __func__);
5160 calldata = kzalloc(sizeof(*calldata), GFP_KERNEL);
5161 if (calldata == NULL)
5162 goto out;
5163 calldata->clp = clp;
5164 calldata->arg.one_fs = 0;
5165 calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
5166
5167 msg.rpc_argp = &calldata->arg;
5168 msg.rpc_resp = &calldata->res;
5169 task_setup_data.callback_data = calldata;
5170 task = rpc_run_task(&task_setup_data);
5171 if (IS_ERR(task))
5172 status = PTR_ERR(task);
5173 rpc_put_task(task);
5174out:
5175 dprintk("<-- %s status=%d\n", __func__, status);
5176 return status;
5177}
Andy Adamson557134a2009-04-01 09:21:53 -04005178#endif /* CONFIG_NFS_V4_1 */
5179
Andy Adamson591d71c2009-04-01 09:22:47 -04005180struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005181 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005182 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183 .recover_open = nfs4_open_reclaim,
5184 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04005185 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005186 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187};
5188
Andy Adamson591d71c2009-04-01 09:22:47 -04005189#if defined(CONFIG_NFS_V4_1)
5190struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
5191 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5192 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5193 .recover_open = nfs4_open_reclaim,
5194 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05005195 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005196 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005197 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04005198};
5199#endif /* CONFIG_NFS_V4_1 */
5200
5201struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005202 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005203 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204 .recover_open = nfs4_open_expired,
5205 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04005206 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005207 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208};
5209
Andy Adamson591d71c2009-04-01 09:22:47 -04005210#if defined(CONFIG_NFS_V4_1)
5211struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
5212 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5213 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5214 .recover_open = nfs4_open_expired,
5215 .recover_lock = nfs4_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05005216 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005217 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04005218};
5219#endif /* CONFIG_NFS_V4_1 */
5220
Benny Halevy29fba382009-04-01 09:22:44 -04005221struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
5222 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04005223 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005224 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04005225};
5226
5227#if defined(CONFIG_NFS_V4_1)
5228struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
5229 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04005230 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005231 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04005232};
5233#endif
5234
5235/*
5236 * Per minor version reboot and network partition recovery ops
5237 */
5238
Andy Adamson591d71c2009-04-01 09:22:47 -04005239struct nfs4_state_recovery_ops *nfs4_reboot_recovery_ops[] = {
5240 &nfs40_reboot_recovery_ops,
5241#if defined(CONFIG_NFS_V4_1)
5242 &nfs41_reboot_recovery_ops,
5243#endif
5244};
5245
5246struct nfs4_state_recovery_ops *nfs4_nograce_recovery_ops[] = {
5247 &nfs40_nograce_recovery_ops,
5248#if defined(CONFIG_NFS_V4_1)
5249 &nfs41_nograce_recovery_ops,
5250#endif
5251};
5252
Benny Halevy29fba382009-04-01 09:22:44 -04005253struct nfs4_state_maintenance_ops *nfs4_state_renewal_ops[] = {
5254 &nfs40_state_renewal_ops,
5255#if defined(CONFIG_NFS_V4_1)
5256 &nfs41_state_renewal_ops,
5257#endif
5258};
5259
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08005260static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005261 .permission = nfs_permission,
5262 .getattr = nfs_getattr,
5263 .setattr = nfs_setattr,
5264 .getxattr = nfs4_getxattr,
5265 .setxattr = nfs4_setxattr,
5266 .listxattr = nfs4_listxattr,
5267};
5268
David Howells509de812006-08-22 20:06:11 -04005269const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270 .version = 4, /* protocol version */
5271 .dentry_ops = &nfs4_dentry_operations,
5272 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005273 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274 .getroot = nfs4_proc_get_root,
5275 .getattr = nfs4_proc_getattr,
5276 .setattr = nfs4_proc_setattr,
David Howells2b3de442006-08-22 20:06:09 -04005277 .lookupfh = nfs4_proc_lookupfh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005278 .lookup = nfs4_proc_lookup,
5279 .access = nfs4_proc_access,
5280 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005281 .create = nfs4_proc_create,
5282 .remove = nfs4_proc_remove,
5283 .unlink_setup = nfs4_proc_unlink_setup,
5284 .unlink_done = nfs4_proc_unlink_done,
5285 .rename = nfs4_proc_rename,
5286 .link = nfs4_proc_link,
5287 .symlink = nfs4_proc_symlink,
5288 .mkdir = nfs4_proc_mkdir,
5289 .rmdir = nfs4_proc_remove,
5290 .readdir = nfs4_proc_readdir,
5291 .mknod = nfs4_proc_mknod,
5292 .statfs = nfs4_proc_statfs,
5293 .fsinfo = nfs4_proc_fsinfo,
5294 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04005295 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005296 .decode_dirent = nfs4_decode_dirent,
5297 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05005298 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005300 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005301 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005302 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005303 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005304 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04005305 .close_context = nfs4_close_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005306};
5307
5308/*
5309 * Local variables:
5310 * c-basic-offset: 8
5311 * End:
5312 */