blob: 84d83be25a98b4d8456fdaeb321f086d313b0e3f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
42#include <linux/sunrpc/clnt.h>
43#include <linux/nfs.h>
44#include <linux/nfs4.h>
45#include <linux/nfs_fs.h>
46#include <linux/nfs_page.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070048#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040049#include <linux/module.h>
Andy Adamson5a0ffe52009-04-01 09:23:18 -040050#include <linux/sunrpc/bc_xprt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Trond Myklebust4ce79712005-06-22 17:16:21 +000052#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050054#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050055#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040056#include "callback.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58#define NFSDBG_FACILITY NFSDBG_PROC
59
Trond Myklebust2066fe82006-09-15 08:30:46 -040060#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#define NFS4_POLL_RETRY_MAX (15*HZ)
62
Trond Myklebusta78cb572009-08-09 15:06:19 -040063#define NFS4_MAX_LOOP_ON_RECOVER (10)
64
Trond Myklebustcdd4e682006-01-03 09:55:12 +010065struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010066static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080067static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050069static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Trond Myklebust99367812007-07-17 21:52:41 -040070static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
71static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Linus Torvalds1da177e2005-04-16 15:20:36 -070073/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050074static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
Trond Myklebust52567b02009-10-23 14:46:42 -040076 if (err >= -1000)
77 return err;
78 switch (err) {
79 case -NFS4ERR_RESOURCE:
80 return -EREMOTEIO;
81 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -070083 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -040084 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 }
Trond Myklebust52567b02009-10-23 14:46:42 -040086 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087}
88
89/*
90 * This is our standard bitmap for GETATTR requests.
91 */
92const u32 nfs4_fattr_bitmap[2] = {
93 FATTR4_WORD0_TYPE
94 | FATTR4_WORD0_CHANGE
95 | FATTR4_WORD0_SIZE
96 | FATTR4_WORD0_FSID
97 | FATTR4_WORD0_FILEID,
98 FATTR4_WORD1_MODE
99 | FATTR4_WORD1_NUMLINKS
100 | FATTR4_WORD1_OWNER
101 | FATTR4_WORD1_OWNER_GROUP
102 | FATTR4_WORD1_RAWDEV
103 | FATTR4_WORD1_SPACE_USED
104 | FATTR4_WORD1_TIME_ACCESS
105 | FATTR4_WORD1_TIME_METADATA
106 | FATTR4_WORD1_TIME_MODIFY
107};
108
109const u32 nfs4_statfs_bitmap[2] = {
110 FATTR4_WORD0_FILES_AVAIL
111 | FATTR4_WORD0_FILES_FREE
112 | FATTR4_WORD0_FILES_TOTAL,
113 FATTR4_WORD1_SPACE_AVAIL
114 | FATTR4_WORD1_SPACE_FREE
115 | FATTR4_WORD1_SPACE_TOTAL
116};
117
Trond Myklebust4ce79712005-06-22 17:16:21 +0000118const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 FATTR4_WORD0_MAXLINK
120 | FATTR4_WORD0_MAXNAME,
121 0
122};
123
124const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
125 | FATTR4_WORD0_MAXREAD
126 | FATTR4_WORD0_MAXWRITE
127 | FATTR4_WORD0_LEASE_TIME,
128 0
129};
130
Manoj Naik830b8e32006-06-09 09:34:25 -0400131const u32 nfs4_fs_locations_bitmap[2] = {
132 FATTR4_WORD0_TYPE
133 | FATTR4_WORD0_CHANGE
134 | FATTR4_WORD0_SIZE
135 | FATTR4_WORD0_FSID
136 | FATTR4_WORD0_FILEID
137 | FATTR4_WORD0_FS_LOCATIONS,
138 FATTR4_WORD1_MODE
139 | FATTR4_WORD1_NUMLINKS
140 | FATTR4_WORD1_OWNER
141 | FATTR4_WORD1_OWNER_GROUP
142 | FATTR4_WORD1_RAWDEV
143 | FATTR4_WORD1_SPACE_USED
144 | FATTR4_WORD1_TIME_ACCESS
145 | FATTR4_WORD1_TIME_METADATA
146 | FATTR4_WORD1_TIME_MODIFY
147 | FATTR4_WORD1_MOUNTED_ON_FILEID
148};
149
Al Virobc4785c2006-10-19 23:28:51 -0700150static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 struct nfs4_readdir_arg *readdir)
152{
Al Viro0dbb4c62006-10-19 23:28:49 -0700153 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155 BUG_ON(readdir->count < 80);
156 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000157 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
159 return;
160 }
161
162 readdir->cookie = 0;
163 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
164 if (cookie == 2)
165 return;
166
167 /*
168 * NFSv4 servers do not return entries for '.' and '..'
169 * Therefore, we fake these entries here. We let '.'
170 * have cookie 0 and '..' have cookie 1. Note that
171 * when talking to the server, we always send cookie 0
172 * instead of 1 or 2.
173 */
Al Viro0dbb4c62006-10-19 23:28:49 -0700174 start = p = kmap_atomic(*readdir->pages, KM_USER0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176 if (cookie == 0) {
177 *p++ = xdr_one; /* next */
178 *p++ = xdr_zero; /* cookie, first word */
179 *p++ = xdr_one; /* cookie, second word */
180 *p++ = xdr_one; /* entry len */
181 memcpy(p, ".\0\0\0", 4); /* entry */
182 p++;
183 *p++ = xdr_one; /* bitmap length */
184 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
185 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400186 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 }
188
189 *p++ = xdr_one; /* next */
190 *p++ = xdr_zero; /* cookie, first word */
191 *p++ = xdr_two; /* cookie, second word */
192 *p++ = xdr_two; /* entry len */
193 memcpy(p, "..\0\0", 4); /* entry */
194 p++;
195 *p++ = xdr_one; /* bitmap length */
196 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
197 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400198 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200 readdir->pgbase = (char *)p - (char *)start;
201 readdir->count -= readdir->pgbase;
202 kunmap_atomic(start, KM_USER0);
203}
204
Trond Myklebust65de8722008-12-23 15:21:44 -0500205static int nfs4_wait_clnt_recover(struct nfs_client *clp)
206{
207 int res;
208
209 might_sleep();
210
Trond Myklebuste005e802008-12-23 15:21:48 -0500211 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400212 nfs_wait_bit_killable, TASK_KILLABLE);
Trond Myklebust65de8722008-12-23 15:21:44 -0500213 return res;
214}
215
216static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
217{
218 int res = 0;
219
220 might_sleep();
221
222 if (*timeout <= 0)
223 *timeout = NFS4_POLL_RETRY_MIN;
224 if (*timeout > NFS4_POLL_RETRY_MAX)
225 *timeout = NFS4_POLL_RETRY_MAX;
226 schedule_timeout_killable(*timeout);
227 if (fatal_signal_pending(current))
228 res = -ERESTARTSYS;
229 *timeout <<= 1;
230 return res;
231}
232
233/* This is the error handling routine for processes that are allowed
234 * to sleep.
235 */
236static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
237{
238 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500239 struct nfs4_state *state = exception->state;
Trond Myklebust65de8722008-12-23 15:21:44 -0500240 int ret = errorcode;
241
242 exception->retry = 0;
243 switch(errorcode) {
244 case 0:
245 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500246 case -NFS4ERR_ADMIN_REVOKED:
247 case -NFS4ERR_BAD_STATEID:
248 case -NFS4ERR_OPENMODE:
249 if (state == NULL)
250 break;
251 nfs4_state_mark_reclaim_nograce(clp, state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500252 goto do_state_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500253 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500254 if (state == NULL)
255 break;
256 nfs4_state_mark_reclaim_reboot(clp, state);
257 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust65de8722008-12-23 15:21:44 -0500258 case -NFS4ERR_EXPIRED:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500259 goto do_state_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500260#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400261 case -NFS4ERR_BADSESSION:
262 case -NFS4ERR_BADSLOT:
263 case -NFS4ERR_BAD_HIGH_SLOT:
264 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
265 case -NFS4ERR_DEADSESSION:
266 case -NFS4ERR_SEQ_FALSE_RETRY:
267 case -NFS4ERR_SEQ_MISORDERED:
268 dprintk("%s ERROR: %d Reset session\n", __func__,
269 errorcode);
Andy Adamson0b9e2d42009-12-04 16:02:14 -0500270 nfs4_schedule_state_recovery(clp);
Andy Adamson4745e312009-04-01 09:22:42 -0400271 exception->retry = 1;
Andy Adamsonb9179232009-12-04 15:55:32 -0500272 break;
Trond Myklebust03391692010-01-26 15:42:38 -0500273#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500274 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500275 if (exception->timeout > HZ) {
276 /* We have retried a decent amount, time to
277 * fail
278 */
279 ret = -EBUSY;
280 break;
281 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500282 case -NFS4ERR_GRACE:
283 case -NFS4ERR_DELAY:
284 ret = nfs4_delay(server->client, &exception->timeout);
285 if (ret != 0)
286 break;
287 case -NFS4ERR_OLD_STATEID:
288 exception->retry = 1;
289 }
290 /* We failed to handle the error */
291 return nfs4_map_errors(ret);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500292do_state_recovery:
293 nfs4_schedule_state_recovery(clp);
294 ret = nfs4_wait_clnt_recover(clp);
295 if (ret == 0)
296 exception->retry = 1;
297 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500298}
299
300
Trond Myklebust26e976a2006-01-03 09:55:21 +0100301static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
David Howells7539bba2006-08-22 20:06:09 -0400303 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 spin_lock(&clp->cl_lock);
305 if (time_before(clp->cl_last_renewal,timestamp))
306 clp->cl_last_renewal = timestamp;
307 spin_unlock(&clp->cl_lock);
308}
309
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400310#if defined(CONFIG_NFS_V4_1)
311
Benny Halevy510b8172009-04-01 09:22:14 -0400312/*
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400313 * nfs4_free_slot - free a slot and efficiently update slot table.
314 *
315 * freeing a slot is trivially done by clearing its respective bit
316 * in the bitmap.
317 * If the freed slotid equals highest_used_slotid we want to update it
318 * so that the server would be able to size down the slot table if needed,
319 * otherwise we know that the highest_used_slotid is still in use.
320 * When updating highest_used_slotid there may be "holes" in the bitmap
321 * so we need to scan down from highest_used_slotid to 0 looking for the now
322 * highest slotid in use.
323 * If none found, highest_used_slotid is set to -1.
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500324 *
325 * Must be called while holding tbl->slot_tbl_lock
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400326 */
327static void
328nfs4_free_slot(struct nfs4_slot_table *tbl, u8 free_slotid)
329{
330 int slotid = free_slotid;
331
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400332 /* clear used bit in bitmap */
333 __clear_bit(slotid, tbl->used_slots);
334
335 /* update highest_used_slotid when it is freed */
336 if (slotid == tbl->highest_used_slotid) {
337 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500338 if (slotid < tbl->max_slots)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400339 tbl->highest_used_slotid = slotid;
340 else
341 tbl->highest_used_slotid = -1;
342 }
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400343 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
344 free_slotid, tbl->highest_used_slotid);
345}
346
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800347/*
348 * Signal state manager thread if session is drained
349 */
350static void nfs41_check_drain_session_complete(struct nfs4_session *ses)
351{
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800352 struct rpc_task *task;
353
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800354 if (!test_bit(NFS4CLNT_SESSION_DRAINING, &ses->clp->cl_state)) {
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800355 task = rpc_wake_up_next(&ses->fc_slot_table.slot_tbl_waitq);
356 if (task)
357 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800358 return;
359 }
360
361 if (ses->fc_slot_table.highest_used_slotid != -1)
362 return;
363
364 dprintk("%s COMPLETE: Session Drained\n", __func__);
365 complete(&ses->complete);
366}
367
Trond Myklebustd61e6122009-12-05 19:32:19 -0500368static void nfs41_sequence_free_slot(const struct nfs_client *clp,
Andy Adamson13615872009-04-01 09:22:17 -0400369 struct nfs4_sequence_res *res)
370{
371 struct nfs4_slot_table *tbl;
372
Andy Adamson13615872009-04-01 09:22:17 -0400373 tbl = &clp->cl_session->fc_slot_table;
374 if (res->sr_slotid == NFS4_MAX_SLOT_TABLE) {
Andy Adamson13615872009-04-01 09:22:17 -0400375 /* just wake up the next guy waiting since
376 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500377 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500378 return;
Andy Adamson13615872009-04-01 09:22:17 -0400379 }
Andy Adamsonea028ac2009-12-04 15:55:38 -0500380
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500381 spin_lock(&tbl->slot_tbl_lock);
382 nfs4_free_slot(tbl, res->sr_slotid);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800383 nfs41_check_drain_session_complete(clp->cl_session);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500384 spin_unlock(&tbl->slot_tbl_lock);
385 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
Andy Adamson13615872009-04-01 09:22:17 -0400386}
387
Andy Adamsonb0df8062009-04-01 09:22:18 -0400388static void nfs41_sequence_done(struct nfs_client *clp,
389 struct nfs4_sequence_res *res,
390 int rpc_status)
391{
392 unsigned long timestamp;
393 struct nfs4_slot_table *tbl;
394 struct nfs4_slot *slot;
395
396 /*
397 * sr_status remains 1 if an RPC level error occurred. The server
398 * may or may not have processed the sequence operation..
399 * Proceed as if the server received and processed the sequence
400 * operation.
401 */
402 if (res->sr_status == 1)
403 res->sr_status = NFS_OK;
404
405 /* -ERESTARTSYS can result in skipping nfs41_sequence_setup */
406 if (res->sr_slotid == NFS4_MAX_SLOT_TABLE)
407 goto out;
408
Andy Adamson691daf32009-12-04 15:55:39 -0500409 /* Check the SEQUENCE operation status */
Andy Adamsonb0df8062009-04-01 09:22:18 -0400410 if (res->sr_status == 0) {
Andy Adamson691daf32009-12-04 15:55:39 -0500411 tbl = &clp->cl_session->fc_slot_table;
412 slot = tbl->slots + res->sr_slotid;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400413 /* Update the slot's sequence and clientid lease timer */
414 ++slot->seq_nr;
415 timestamp = res->sr_renewal_time;
416 spin_lock(&clp->cl_lock);
417 if (time_before(clp->cl_last_renewal, timestamp))
418 clp->cl_last_renewal = timestamp;
419 spin_unlock(&clp->cl_lock);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500420 /* Check sequence flags */
421 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400422 }
423out:
424 /* The session may be reset by one of the error handlers. */
425 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
426 nfs41_sequence_free_slot(clp, res);
427}
428
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400429/*
Benny Halevy510b8172009-04-01 09:22:14 -0400430 * nfs4_find_slot - efficiently look for a free slot
431 *
432 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
433 * If found, we mark the slot as used, update the highest_used_slotid,
434 * and respectively set up the sequence operation args.
435 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400436 *
437 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400438 */
439static u8
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800440nfs4_find_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400441{
442 int slotid;
443 u8 ret_id = NFS4_MAX_SLOT_TABLE;
444 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
445
Benny Halevy510b8172009-04-01 09:22:14 -0400446 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
447 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
448 tbl->max_slots);
449 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
450 if (slotid >= tbl->max_slots)
451 goto out;
452 __set_bit(slotid, tbl->used_slots);
453 if (slotid > tbl->highest_used_slotid)
454 tbl->highest_used_slotid = slotid;
455 ret_id = slotid;
456out:
457 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
458 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400459 return ret_id;
460}
461
Andy Adamsonce5039c2009-04-01 09:22:13 -0400462static int nfs41_setup_sequence(struct nfs4_session *session,
463 struct nfs4_sequence_args *args,
464 struct nfs4_sequence_res *res,
465 int cache_reply,
466 struct rpc_task *task)
467{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400468 struct nfs4_slot *slot;
469 struct nfs4_slot_table *tbl;
470 u8 slotid;
471
472 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400473 /* slot already allocated? */
474 if (res->sr_slotid != NFS4_MAX_SLOT_TABLE)
475 return 0;
476
477 memset(res, 0, sizeof(*res));
478 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400479 tbl = &session->fc_slot_table;
480
481 spin_lock(&tbl->slot_tbl_lock);
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800482 if (test_bit(NFS4CLNT_SESSION_DRAINING, &session->clp->cl_state) &&
483 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamsonea028ac2009-12-04 15:55:38 -0500484 /*
485 * The state manager will wait until the slot table is empty.
486 * Schedule the reset thread
487 */
Andy Adamson05f0d232009-12-04 15:55:37 -0500488 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400489 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500490 dprintk("%s Schedule Session Reset\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500491 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400492 }
493
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800494 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
495 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
496 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
497 spin_unlock(&tbl->slot_tbl_lock);
498 dprintk("%s enforce FIFO order\n", __func__);
499 return -EAGAIN;
500 }
501
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800502 slotid = nfs4_find_slot(tbl);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400503 if (slotid == NFS4_MAX_SLOT_TABLE) {
504 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
505 spin_unlock(&tbl->slot_tbl_lock);
506 dprintk("<-- %s: no free slots\n", __func__);
507 return -EAGAIN;
508 }
509 spin_unlock(&tbl->slot_tbl_lock);
510
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800511 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400512 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400513 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400514 args->sa_slotid = slotid;
515 args->sa_cache_this = cache_reply;
516
517 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
518
Benny Halevy99fe60d2009-04-01 09:22:29 -0400519 res->sr_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400520 res->sr_slotid = slotid;
521 res->sr_renewal_time = jiffies;
522 /*
523 * sr_status is only set in decode_sequence, and so will remain
524 * set to 1 if an rpc level failure occurs.
525 */
526 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400527 return 0;
528}
529
530int nfs4_setup_sequence(struct nfs_client *clp,
531 struct nfs4_sequence_args *args,
532 struct nfs4_sequence_res *res,
533 int cache_reply,
534 struct rpc_task *task)
535{
536 int ret = 0;
537
538 dprintk("--> %s clp %p session %p sr_slotid %d\n",
539 __func__, clp, clp->cl_session, res->sr_slotid);
540
541 if (!nfs4_has_session(clp))
542 goto out;
543 ret = nfs41_setup_sequence(clp->cl_session, args, res, cache_reply,
544 task);
Andy Adamson2628edd2009-12-04 15:55:35 -0500545 if (ret && ret != -EAGAIN) {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400546 /* terminate rpc task */
547 task->tk_status = ret;
548 task->tk_action = NULL;
549 }
550out:
551 dprintk("<-- %s status=%d\n", __func__, ret);
552 return ret;
553}
554
555struct nfs41_call_sync_data {
556 struct nfs_client *clp;
557 struct nfs4_sequence_args *seq_args;
558 struct nfs4_sequence_res *seq_res;
559 int cache_reply;
560};
561
562static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
563{
564 struct nfs41_call_sync_data *data = calldata;
565
566 dprintk("--> %s data->clp->cl_session %p\n", __func__,
567 data->clp->cl_session);
568 if (nfs4_setup_sequence(data->clp, data->seq_args,
569 data->seq_res, data->cache_reply, task))
570 return;
571 rpc_call_start(task);
572}
573
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800574static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
575{
576 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
577 nfs41_call_sync_prepare(task, calldata);
578}
579
Andy Adamson69ab40c2009-04-01 09:22:19 -0400580static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
581{
582 struct nfs41_call_sync_data *data = calldata;
583
584 nfs41_sequence_done(data->clp, data->seq_res, task->tk_status);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400585}
586
Andy Adamsonce5039c2009-04-01 09:22:13 -0400587struct rpc_call_ops nfs41_call_sync_ops = {
588 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400589 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400590};
591
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800592struct rpc_call_ops nfs41_call_priv_sync_ops = {
593 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
594 .rpc_call_done = nfs41_call_sync_done,
595};
596
Andy Adamsonce5039c2009-04-01 09:22:13 -0400597static int nfs4_call_sync_sequence(struct nfs_client *clp,
598 struct rpc_clnt *clnt,
599 struct rpc_message *msg,
600 struct nfs4_sequence_args *args,
601 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800602 int cache_reply,
603 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400604{
605 int ret;
606 struct rpc_task *task;
607 struct nfs41_call_sync_data data = {
608 .clp = clp,
609 .seq_args = args,
610 .seq_res = res,
611 .cache_reply = cache_reply,
612 };
613 struct rpc_task_setup task_setup = {
614 .rpc_client = clnt,
615 .rpc_message = msg,
616 .callback_ops = &nfs41_call_sync_ops,
617 .callback_data = &data
618 };
619
620 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800621 if (privileged)
622 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400623 task = rpc_run_task(&task_setup);
624 if (IS_ERR(task))
625 ret = PTR_ERR(task);
626 else {
627 ret = task->tk_status;
628 rpc_put_task(task);
629 }
630 return ret;
631}
632
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400633int _nfs4_call_sync_session(struct nfs_server *server,
634 struct rpc_message *msg,
635 struct nfs4_sequence_args *args,
636 struct nfs4_sequence_res *res,
637 int cache_reply)
638{
Andy Adamsonce5039c2009-04-01 09:22:13 -0400639 return nfs4_call_sync_sequence(server->nfs_client, server->client,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800640 msg, args, res, cache_reply, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400641}
642
643#endif /* CONFIG_NFS_V4_1 */
644
645int _nfs4_call_sync(struct nfs_server *server,
646 struct rpc_message *msg,
647 struct nfs4_sequence_args *args,
648 struct nfs4_sequence_res *res,
649 int cache_reply)
650{
Benny Halevyf3752972009-04-01 09:22:04 -0400651 args->sa_session = res->sr_session = NULL;
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400652 return rpc_call_sync(server->client, msg, 0);
653}
654
655#define nfs4_call_sync(server, msg, args, res, cache_reply) \
656 (server)->nfs_client->cl_call_sync((server), (msg), &(args)->seq_args, \
657 &(res)->seq_res, (cache_reply))
658
Andy Adamsonb0df8062009-04-01 09:22:18 -0400659static void nfs4_sequence_done(const struct nfs_server *server,
660 struct nfs4_sequence_res *res, int rpc_status)
661{
662#ifdef CONFIG_NFS_V4_1
663 if (nfs4_has_session(server->nfs_client))
664 nfs41_sequence_done(server->nfs_client, res, rpc_status);
665#endif /* CONFIG_NFS_V4_1 */
666}
667
Trond Myklebust38478b22006-05-25 01:40:57 -0400668static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
Trond Myklebust38478b22006-05-25 01:40:57 -0400670 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Trond Myklebust38478b22006-05-25 01:40:57 -0400672 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400673 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
674 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400675 nfs_force_lookup_revalidate(dir);
Trond Myklebust40d24702007-10-08 09:24:22 -0400676 nfsi->change_attr = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400677 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678}
679
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100680struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400681 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100682 struct nfs_openargs o_arg;
683 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100684 struct nfs_open_confirmargs c_arg;
685 struct nfs_open_confirmres c_res;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100686 struct nfs_fattr f_attr;
687 struct nfs_fattr dir_attr;
Trond Myklebustad389da2007-06-05 12:30:00 -0400688 struct path path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100689 struct dentry *dir;
690 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400691 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100692 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100693 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400694 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100695 int rpc_status;
696 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100697};
698
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400699
700static void nfs4_init_opendata_res(struct nfs4_opendata *p)
701{
702 p->o_res.f_attr = &p->f_attr;
703 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400704 p->o_res.seqid = p->o_arg.seqid;
705 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400706 p->o_res.server = p->o_arg.server;
707 nfs_fattr_init(&p->f_attr);
708 nfs_fattr_init(&p->dir_attr);
Benny Halevy578e4582009-06-18 22:01:23 -0400709 p->o_res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400710}
711
Trond Myklebustad389da2007-06-05 12:30:00 -0400712static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500713 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100714 const struct iattr *attrs)
715{
Trond Myklebustad389da2007-06-05 12:30:00 -0400716 struct dentry *parent = dget_parent(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100717 struct inode *dir = parent->d_inode;
718 struct nfs_server *server = NFS_SERVER(dir);
719 struct nfs4_opendata *p;
720
721 p = kzalloc(sizeof(*p), GFP_KERNEL);
722 if (p == NULL)
723 goto err;
724 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
725 if (p->o_arg.seqid == NULL)
726 goto err_free;
Al Virof6948692010-01-30 13:51:04 -0500727 path_get(path);
728 p->path = *path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100729 p->dir = parent;
730 p->owner = sp;
731 atomic_inc(&sp->so_count);
732 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500733 p->o_arg.open_flags = flags;
734 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400735 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400736 p->o_arg.id = sp->so_owner_id.id;
Trond Myklebustad389da2007-06-05 12:30:00 -0400737 p->o_arg.name = &p->path.dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100738 p->o_arg.server = server;
739 p->o_arg.bitmask = server->attr_bitmask;
740 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100741 if (flags & O_EXCL) {
Alexandros Batsakis4882ef72009-12-05 13:30:21 -0500742 if (nfs4_has_persistent_session(server->nfs_client)) {
743 /* GUARDED */
744 p->o_arg.u.attrs = &p->attrs;
745 memcpy(&p->attrs, attrs, sizeof(p->attrs));
746 } else { /* EXCLUSIVE4_1 */
747 u32 *s = (u32 *) p->o_arg.u.verifier.data;
748 s[0] = jiffies;
749 s[1] = current->pid;
750 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100751 } else if (flags & O_CREAT) {
752 p->o_arg.u.attrs = &p->attrs;
753 memcpy(&p->attrs, attrs, sizeof(p->attrs));
754 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100755 p->c_arg.fh = &p->o_res.fh;
756 p->c_arg.stateid = &p->o_res.stateid;
757 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400758 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400759 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100760 return p;
761err_free:
762 kfree(p);
763err:
764 dput(parent);
765 return NULL;
766}
767
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400768static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100769{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400770 struct nfs4_opendata *p = container_of(kref,
771 struct nfs4_opendata, kref);
772
773 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400774 if (p->state != NULL)
775 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400776 nfs4_put_state_owner(p->owner);
777 dput(p->dir);
Jan Blunck31f31db2008-05-02 13:42:45 -0700778 path_put(&p->path);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400779 kfree(p);
780}
781
782static void nfs4_opendata_put(struct nfs4_opendata *p)
783{
784 if (p != NULL)
785 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100786}
787
Trond Myklebust06f814a2006-01-03 09:55:07 +0100788static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
789{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100790 int ret;
791
Trond Myklebust06f814a2006-01-03 09:55:07 +0100792 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100793 return ret;
794}
795
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500796static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400797{
798 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500799
800 if (open_mode & O_EXCL)
801 goto out;
802 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400803 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500804 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
805 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400806 break;
807 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500808 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
809 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400810 break;
811 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500812 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
813 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400814 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500815out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400816 return ret;
817}
818
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500819static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400820{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500821 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400822 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500823 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400824 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500825 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400826 return 1;
827}
828
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500829static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100830{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500831 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100832 case FMODE_WRITE:
833 state->n_wronly++;
834 break;
835 case FMODE_READ:
836 state->n_rdonly++;
837 break;
838 case FMODE_READ|FMODE_WRITE:
839 state->n_rdwr++;
840 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500841 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100842}
843
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500844static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400845{
846 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
847 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
848 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500849 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400850 case FMODE_READ:
851 set_bit(NFS_O_RDONLY_STATE, &state->flags);
852 break;
853 case FMODE_WRITE:
854 set_bit(NFS_O_WRONLY_STATE, &state->flags);
855 break;
856 case FMODE_READ|FMODE_WRITE:
857 set_bit(NFS_O_RDWR_STATE, &state->flags);
858 }
859}
860
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500861static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400862{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400863 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500864 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400865 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400866}
867
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500868static 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 -0700869{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400870 /*
871 * Protect the call to nfs4_state_set_mode_locked and
872 * serialise the stateid update
873 */
874 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400875 if (deleg_stateid != NULL) {
876 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
877 set_bit(NFS_DELEGATED_STATE, &state->flags);
878 }
879 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500880 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400881 write_sequnlock(&state->seqlock);
882 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500883 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700884 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885}
886
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500887static 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 -0500888{
889 struct nfs_inode *nfsi = NFS_I(state->inode);
890 struct nfs_delegation *deleg_cur;
891 int ret = 0;
892
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500893 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500894
895 rcu_read_lock();
896 deleg_cur = rcu_dereference(nfsi->delegation);
897 if (deleg_cur == NULL)
898 goto no_delegation;
899
900 spin_lock(&deleg_cur->lock);
901 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500902 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500903 goto no_delegation_unlock;
904
905 if (delegation == NULL)
906 delegation = &deleg_cur->stateid;
907 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
908 goto no_delegation_unlock;
909
Trond Myklebustb7391f42008-12-23 15:21:52 -0500910 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500911 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500912 ret = 1;
913no_delegation_unlock:
914 spin_unlock(&deleg_cur->lock);
915no_delegation:
916 rcu_read_unlock();
917
918 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500919 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500920 ret = 1;
921 }
922
923 return ret;
924}
925
926
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500927static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400928{
929 struct nfs_delegation *delegation;
930
931 rcu_read_lock();
932 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500933 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -0400934 rcu_read_unlock();
935 return;
936 }
937 rcu_read_unlock();
938 nfs_inode_return_delegation(inode);
939}
940
Trond Myklebust6ee41262007-07-08 14:11:36 -0400941static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400942{
943 struct nfs4_state *state = opendata->state;
944 struct nfs_inode *nfsi = NFS_I(state->inode);
945 struct nfs_delegation *delegation;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500946 int open_mode = opendata->o_arg.open_flags & O_EXCL;
947 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400948 nfs4_stateid stateid;
949 int ret = -EAGAIN;
950
Trond Myklebustaac00a82007-07-05 19:02:21 -0400951 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500952 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400953 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500954 if (can_open_cached(state, fmode, open_mode)) {
955 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400956 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400957 goto out_return_state;
958 }
959 spin_unlock(&state->owner->so_lock);
960 }
Trond Myklebust34310432008-12-23 15:21:38 -0500961 rcu_read_lock();
962 delegation = rcu_dereference(nfsi->delegation);
963 if (delegation == NULL ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500964 !can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -0500965 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -0400966 break;
Trond Myklebust34310432008-12-23 15:21:38 -0500967 }
Trond Myklebustaac00a82007-07-05 19:02:21 -0400968 /* Save the delegation */
969 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
970 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -0400971 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400972 if (ret != 0)
973 goto out;
974 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -0500975
976 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500977 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -0500978 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400979 }
Trond Myklebustaac00a82007-07-05 19:02:21 -0400980out:
981 return ERR_PTR(ret);
982out_return_state:
983 atomic_inc(&state->count);
984 return state;
985}
986
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100987static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
988{
989 struct inode *inode;
990 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -0400991 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400992 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100993
Trond Myklebustaac00a82007-07-05 19:02:21 -0400994 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400995 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400996 goto out;
997 }
998
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400999 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001000 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001001 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001002 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001003 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001004 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001005 goto err;
1006 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001007 state = nfs4_get_open_state(inode, data->owner);
1008 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001009 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001010 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001011 int delegation_flags = 0;
1012
Trond Myklebust003707c2007-07-05 18:07:55 -04001013 rcu_read_lock();
1014 delegation = rcu_dereference(NFS_I(inode)->delegation);
1015 if (delegation)
1016 delegation_flags = delegation->flags;
1017 rcu_read_unlock();
Trond Myklebust15c831b2008-12-23 15:21:39 -05001018 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001019 nfs_inode_set_delegation(state->inode,
1020 data->owner->so_cred,
1021 &data->o_res);
1022 else
1023 nfs_inode_reclaim_delegation(state->inode,
1024 data->owner->so_cred,
1025 &data->o_res);
1026 }
Trond Myklebust34310432008-12-23 15:21:38 -05001027
1028 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001029 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001030 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001031out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001032 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001033err_put_inode:
1034 iput(inode);
1035err:
1036 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001037}
1038
Trond Myklebust864472e2006-01-03 09:55:15 +01001039static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1040{
1041 struct nfs_inode *nfsi = NFS_I(state->inode);
1042 struct nfs_open_context *ctx;
1043
1044 spin_lock(&state->inode->i_lock);
1045 list_for_each_entry(ctx, &nfsi->open_files, list) {
1046 if (ctx->state != state)
1047 continue;
1048 get_nfs_open_context(ctx);
1049 spin_unlock(&state->inode->i_lock);
1050 return ctx;
1051 }
1052 spin_unlock(&state->inode->i_lock);
1053 return ERR_PTR(-ENOENT);
1054}
1055
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001056static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1057{
1058 struct nfs4_opendata *opendata;
1059
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001060 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001061 if (opendata == NULL)
1062 return ERR_PTR(-ENOMEM);
1063 opendata->state = state;
1064 atomic_inc(&state->count);
1065 return opendata;
1066}
1067
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001068static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001069{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001070 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001071 int ret;
1072
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001073 opendata->o_arg.open_flags = 0;
1074 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001075 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1076 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1077 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001078 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001079 if (ret != 0)
1080 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001081 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001082 if (IS_ERR(newstate))
1083 return PTR_ERR(newstate);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001084 nfs4_close_state(&opendata->path, newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001085 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001086 return 0;
1087}
1088
1089static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1090{
Trond Myklebust864472e2006-01-03 09:55:15 +01001091 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001092 int ret;
1093
1094 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001095 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001096 smp_rmb();
1097 if (state->n_rdwr != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001098 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001099 if (ret != 0)
1100 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001101 if (newstate != state)
1102 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001103 }
1104 if (state->n_wronly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001105 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001106 if (ret != 0)
1107 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001108 if (newstate != state)
1109 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001110 }
1111 if (state->n_rdonly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001112 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001113 if (ret != 0)
1114 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001115 if (newstate != state)
1116 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001117 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001118 /*
1119 * We may have performed cached opens for all three recoveries.
1120 * Check if we need to update the current stateid.
1121 */
1122 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1123 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001124 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001125 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1126 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001127 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001128 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001129 return 0;
1130}
1131
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132/*
1133 * OPEN_RECLAIM:
1134 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001136static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001138 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001139 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001140 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 int status;
1142
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001143 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1144 if (IS_ERR(opendata))
1145 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001146 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1147 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001148 rcu_read_lock();
1149 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001150 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001151 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001152 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001153 opendata->o_arg.u.delegation_type = delegation_type;
1154 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001155 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 return status;
1157}
1158
Trond Myklebust539cd032007-06-05 11:46:42 -04001159static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160{
1161 struct nfs_server *server = NFS_SERVER(state->inode);
1162 struct nfs4_exception exception = { };
1163 int err;
1164 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001165 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001166 if (err != -NFS4ERR_DELAY)
1167 break;
1168 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 } while (exception.retry);
1170 return err;
1171}
1172
Trond Myklebust864472e2006-01-03 09:55:15 +01001173static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1174{
1175 struct nfs_open_context *ctx;
1176 int ret;
1177
1178 ctx = nfs4_state_find_open_context(state);
1179 if (IS_ERR(ctx))
1180 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001181 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001182 put_nfs_open_context(ctx);
1183 return ret;
1184}
1185
Trond Myklebust13437e12007-07-06 15:10:43 -04001186static 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 -07001187{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001188 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001189 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001191 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1192 if (IS_ERR(opendata))
1193 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001194 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -04001195 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001196 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +01001197 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001198 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001199 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200}
1201
Trond Myklebust13437e12007-07-06 15:10:43 -04001202int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203{
1204 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001205 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 int err;
1207 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001208 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 switch (err) {
1210 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001211 case -ENOENT:
1212 case -ESTALE:
1213 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001214 case -NFS4ERR_BADSESSION:
1215 case -NFS4ERR_BADSLOT:
1216 case -NFS4ERR_BAD_HIGH_SLOT:
1217 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1218 case -NFS4ERR_DEADSESSION:
1219 nfs4_schedule_state_recovery(
1220 server->nfs_client);
1221 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 case -NFS4ERR_STALE_CLIENTID:
1223 case -NFS4ERR_STALE_STATEID:
1224 case -NFS4ERR_EXPIRED:
1225 /* Don't recall a delegation if it was lost */
David Howells7539bba2006-08-22 20:06:09 -04001226 nfs4_schedule_state_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001227 goto out;
1228 case -ERESTARTSYS:
1229 /*
1230 * The show must go on: exit, but mark the
1231 * stateid as needing recovery.
1232 */
1233 case -NFS4ERR_ADMIN_REVOKED:
1234 case -NFS4ERR_BAD_STATEID:
1235 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
1236 case -ENOMEM:
1237 err = 0;
1238 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 }
1240 err = nfs4_handle_exception(server, err, &exception);
1241 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001242out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 return err;
1244}
1245
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001246static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1247{
1248 struct nfs4_opendata *data = calldata;
1249
1250 data->rpc_status = task->tk_status;
1251 if (RPC_ASSASSINATED(task))
1252 return;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001253 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001254 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1255 sizeof(data->o_res.stateid.data));
Trond Myklebustbb226292008-01-02 15:19:18 -05001256 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001257 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001258 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001259 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001260}
1261
1262static void nfs4_open_confirm_release(void *calldata)
1263{
1264 struct nfs4_opendata *data = calldata;
1265 struct nfs4_state *state = NULL;
1266
1267 /* If this request hasn't been cancelled, do nothing */
1268 if (data->cancelled == 0)
1269 goto out_free;
1270 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001271 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001272 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001273 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001274 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001275 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001276out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001277 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001278}
1279
1280static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001281 .rpc_call_done = nfs4_open_confirm_done,
1282 .rpc_release = nfs4_open_confirm_release,
1283};
1284
1285/*
1286 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1287 */
1288static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1289{
1290 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1291 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001292 struct rpc_message msg = {
1293 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1294 .rpc_argp = &data->c_arg,
1295 .rpc_resp = &data->c_res,
1296 .rpc_cred = data->owner->so_cred,
1297 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001298 struct rpc_task_setup task_setup_data = {
1299 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001300 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001301 .callback_ops = &nfs4_open_confirm_ops,
1302 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001303 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001304 .flags = RPC_TASK_ASYNC,
1305 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 int status;
1307
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001308 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001309 data->rpc_done = 0;
1310 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001311 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001312 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001313 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001314 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001315 status = nfs4_wait_for_completion_rpc_task(task);
1316 if (status != 0) {
1317 data->cancelled = 1;
1318 smp_wmb();
1319 } else
1320 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001321 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 return status;
1323}
1324
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001325static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001327 struct nfs4_opendata *data = calldata;
1328 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001329
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001330 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1331 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001332 /*
1333 * Check if we still need to send an OPEN call, or if we can use
1334 * a delegation instead.
1335 */
1336 if (data->state != NULL) {
1337 struct nfs_delegation *delegation;
1338
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001339 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001340 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001341 rcu_read_lock();
1342 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1343 if (delegation != NULL &&
Trond Myklebust15c831b2008-12-23 15:21:39 -05001344 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001345 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001346 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001347 }
1348 rcu_read_unlock();
1349 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001350 /* Update sequence id. */
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001351 data->o_arg.id = sp->so_owner_id.id;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001352 data->o_arg.clientid = sp->so_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001353 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001354 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001355 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1356 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001357 data->timestamp = jiffies;
Andy Adamsond8985282009-04-01 09:22:21 -04001358 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
1359 &data->o_arg.seq_args,
1360 &data->o_res.seq_res, 1, task))
1361 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001362 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001363 return;
1364out_no_action:
1365 task->tk_action = NULL;
1366
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001367}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001369static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1370{
1371 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1372 nfs4_open_prepare(task, calldata);
1373}
1374
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001375static void nfs4_open_done(struct rpc_task *task, void *calldata)
1376{
1377 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001379 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001380
Trond Myklebustd61e6122009-12-05 19:32:19 -05001381 nfs4_sequence_done(data->o_arg.server, &data->o_res.seq_res,
1382 task->tk_status);
Andy Adamsond8985282009-04-01 09:22:21 -04001383
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001384 if (RPC_ASSASSINATED(task))
1385 return;
1386 if (task->tk_status == 0) {
1387 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001388 case S_IFREG:
1389 break;
1390 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001391 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001392 break;
1393 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001394 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001395 break;
1396 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001397 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001398 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001399 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001400 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1401 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001402 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001403 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001404}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001405
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001406static void nfs4_open_release(void *calldata)
1407{
1408 struct nfs4_opendata *data = calldata;
1409 struct nfs4_state *state = NULL;
1410
1411 /* If this request hasn't been cancelled, do nothing */
1412 if (data->cancelled == 0)
1413 goto out_free;
1414 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001415 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001416 goto out_free;
1417 /* In case we need an open_confirm, no cleanup! */
1418 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1419 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001420 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001421 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001422 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001423out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001424 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001425}
1426
1427static const struct rpc_call_ops nfs4_open_ops = {
1428 .rpc_call_prepare = nfs4_open_prepare,
1429 .rpc_call_done = nfs4_open_done,
1430 .rpc_release = nfs4_open_release,
1431};
1432
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001433static const struct rpc_call_ops nfs4_recover_open_ops = {
1434 .rpc_call_prepare = nfs4_recover_open_prepare,
1435 .rpc_call_done = nfs4_open_done,
1436 .rpc_release = nfs4_open_release,
1437};
1438
1439static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001440{
1441 struct inode *dir = data->dir->d_inode;
1442 struct nfs_server *server = NFS_SERVER(dir);
1443 struct nfs_openargs *o_arg = &data->o_arg;
1444 struct nfs_openres *o_res = &data->o_res;
1445 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001446 struct rpc_message msg = {
1447 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1448 .rpc_argp = o_arg,
1449 .rpc_resp = o_res,
1450 .rpc_cred = data->owner->so_cred,
1451 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001452 struct rpc_task_setup task_setup_data = {
1453 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001454 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001455 .callback_ops = &nfs4_open_ops,
1456 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001457 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001458 .flags = RPC_TASK_ASYNC,
1459 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001460 int status;
1461
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001462 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001463 data->rpc_done = 0;
1464 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001465 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001466 if (isrecover)
1467 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001468 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001469 if (IS_ERR(task))
1470 return PTR_ERR(task);
1471 status = nfs4_wait_for_completion_rpc_task(task);
1472 if (status != 0) {
1473 data->cancelled = 1;
1474 smp_wmb();
1475 } else
1476 status = data->rpc_status;
1477 rpc_put_task(task);
1478
1479 return status;
1480}
1481
1482static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1483{
1484 struct inode *dir = data->dir->d_inode;
1485 struct nfs_openres *o_res = &data->o_res;
1486 int status;
1487
1488 status = nfs4_run_open_task(data, 1);
1489 if (status != 0 || !data->rpc_done)
1490 return status;
1491
1492 nfs_refresh_inode(dir, o_res->dir_attr);
1493
1494 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1495 status = _nfs4_proc_open_confirm(data);
1496 if (status != 0)
1497 return status;
1498 }
1499
1500 return status;
1501}
1502
1503/*
1504 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1505 */
1506static int _nfs4_proc_open(struct nfs4_opendata *data)
1507{
1508 struct inode *dir = data->dir->d_inode;
1509 struct nfs_server *server = NFS_SERVER(dir);
1510 struct nfs_openargs *o_arg = &data->o_arg;
1511 struct nfs_openres *o_res = &data->o_res;
1512 int status;
1513
1514 status = nfs4_run_open_task(data, 0);
Trond Myklebust3e309912007-07-07 13:19:59 -04001515 if (status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001516 return status;
1517
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001518 if (o_arg->open_flags & O_CREAT) {
1519 update_changeattr(dir, &o_res->cinfo);
1520 nfs_post_op_update_inode(dir, o_res->dir_attr);
1521 } else
1522 nfs_refresh_inode(dir, o_res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001524 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001526 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 }
1528 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001529 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001530 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531}
1532
Trond Myklebust10afec92007-05-14 17:16:04 -04001533static int nfs4_recover_expired_lease(struct nfs_server *server)
Trond Myklebust58d9714a2006-01-03 09:55:24 +01001534{
David Howells7539bba2006-08-22 20:06:09 -04001535 struct nfs_client *clp = server->nfs_client;
Trond Myklebusta78cb572009-08-09 15:06:19 -04001536 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001537 int ret;
Trond Myklebust58d9714a2006-01-03 09:55:24 +01001538
Trond Myklebusta78cb572009-08-09 15:06:19 -04001539 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001540 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001541 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001542 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001543 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1544 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001545 break;
Trond Myklebust58d9714a2006-01-03 09:55:24 +01001546 nfs4_schedule_state_recovery(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001547 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001548 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001549 return ret;
Trond Myklebust58d9714a2006-01-03 09:55:24 +01001550}
1551
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552/*
1553 * OPEN_EXPIRED:
1554 * reclaim state on the server after a network partition.
1555 * Assumes caller holds the appropriate lock
1556 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001557static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001559 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001560 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001562 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1563 if (IS_ERR(opendata))
1564 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001565 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001566 if (ret == -ESTALE)
Trond Myklebust539cd032007-06-05 11:46:42 -04001567 d_drop(ctx->path.dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001568 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001569 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570}
1571
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001572static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001573{
Trond Myklebust539cd032007-06-05 11:46:42 -04001574 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001575 struct nfs4_exception exception = { };
1576 int err;
1577
1578 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001579 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001580 switch (err) {
1581 default:
1582 goto out;
1583 case -NFS4ERR_GRACE:
1584 case -NFS4ERR_DELAY:
1585 nfs4_handle_exception(server, err, &exception);
1586 err = 0;
1587 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001588 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001589out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001590 return err;
1591}
1592
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1594{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001596 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
Trond Myklebust864472e2006-01-03 09:55:15 +01001598 ctx = nfs4_state_find_open_context(state);
1599 if (IS_ERR(ctx))
1600 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001601 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001602 put_nfs_open_context(ctx);
1603 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604}
1605
1606/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001607 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1608 * fields corresponding to attributes that were used to store the verifier.
1609 * Make sure we clobber those fields in the later setattr call
1610 */
1611static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1612{
1613 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1614 !(sattr->ia_valid & ATTR_ATIME_SET))
1615 sattr->ia_valid |= ATTR_ATIME;
1616
1617 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1618 !(sattr->ia_valid & ATTR_MTIME_SET))
1619 sattr->ia_valid |= ATTR_MTIME;
1620}
1621
1622/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001623 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001625static 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 -07001626{
1627 struct nfs4_state_owner *sp;
1628 struct nfs4_state *state = NULL;
1629 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001630 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001631 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
1633 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 status = -ENOMEM;
1635 if (!(sp = nfs4_get_state_owner(server, cred))) {
1636 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1637 goto out_err;
1638 }
Trond Myklebust58d9714a2006-01-03 09:55:24 +01001639 status = nfs4_recover_expired_lease(server);
1640 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001641 goto err_put_state_owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001642 if (path->dentry->d_inode != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001643 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
Trond Myklebust58d9714a2006-01-03 09:55:24 +01001644 status = -ENOMEM;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001645 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001646 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001647 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
Trond Myklebustaac00a82007-07-05 19:02:21 -04001649 if (path->dentry->d_inode != NULL)
1650 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1651
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001652 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001654 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001656 if (opendata->o_arg.open_flags & O_EXCL)
1657 nfs4_exclusive_attrset(opendata, sattr);
1658
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001659 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001660 status = PTR_ERR(state);
1661 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001662 goto err_opendata_put;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001663 if ((opendata->o_res.rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) != 0)
1664 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001665 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 *res = state;
1668 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001669err_opendata_put:
1670 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001671err_put_state_owner:
1672 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 *res = NULL;
1675 return status;
1676}
1677
1678
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001679static 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 -07001680{
1681 struct nfs4_exception exception = { };
1682 struct nfs4_state *res;
1683 int status;
1684
1685 do {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001686 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 if (status == 0)
1688 break;
1689 /* NOTE: BAD_SEQID means the server and client disagree about the
1690 * book-keeping w.r.t. state-changing operations
1691 * (OPEN/CLOSE/LOCK/LOCKU...)
1692 * It is actually a sign of a bug on the client or on the server.
1693 *
1694 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001695 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 * have unhashed the old state_owner for us, and that we can
1697 * therefore safely retry using a new one. We should still warn
1698 * the user though...
1699 */
1700 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001701 printk(KERN_WARNING "NFS: v4 server %s "
1702 " returned a bad sequence-id error!\n",
1703 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 exception.retry = 1;
1705 continue;
1706 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001707 /*
1708 * BAD_STATEID on OPEN means that the server cancelled our
1709 * state before it received the OPEN_CONFIRM.
1710 * Recover by retrying the request as per the discussion
1711 * on Page 181 of RFC3530.
1712 */
1713 if (status == -NFS4ERR_BAD_STATEID) {
1714 exception.retry = 1;
1715 continue;
1716 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001717 if (status == -EAGAIN) {
1718 /* We must have found a delegation */
1719 exception.retry = 1;
1720 continue;
1721 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1723 status, &exception));
1724 } while (exception.retry);
1725 return res;
1726}
1727
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001728static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1729 struct nfs_fattr *fattr, struct iattr *sattr,
1730 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001732 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001734 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 .iap = sattr,
1736 .server = server,
1737 .bitmask = server->attr_bitmask,
1738 };
1739 struct nfs_setattrres res = {
1740 .fattr = fattr,
1741 .server = server,
1742 };
1743 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001744 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1745 .rpc_argp = &arg,
1746 .rpc_resp = &res,
1747 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001749 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001750 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
Trond Myklebust0e574af2005-10-27 22:12:38 -04001752 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001754 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1755 /* Use that stateid */
1756 } else if (state != NULL) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001757 nfs4_copy_stateid(&arg.stateid, state, current->files);
1758 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1760
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001761 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001762 if (status == 0 && state != NULL)
1763 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001764 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765}
1766
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001767static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1768 struct nfs_fattr *fattr, struct iattr *sattr,
1769 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001771 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 struct nfs4_exception exception = { };
1773 int err;
1774 do {
1775 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001776 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 &exception);
1778 } while (exception.retry);
1779 return err;
1780}
1781
1782struct nfs4_closedata {
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001783 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 struct inode *inode;
1785 struct nfs4_state *state;
1786 struct nfs_closeargs arg;
1787 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001788 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001789 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790};
1791
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001792static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001793{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001794 struct nfs4_closedata *calldata = data;
1795 struct nfs4_state_owner *sp = calldata->state->owner;
Trond Myklebust95121352005-10-18 14:20:12 -07001796
1797 nfs4_put_open_state(calldata->state);
1798 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001799 nfs4_put_state_owner(sp);
Jan Blunck31f31db2008-05-02 13:42:45 -07001800 path_put(&calldata->path);
Trond Myklebust95121352005-10-18 14:20:12 -07001801 kfree(calldata);
1802}
1803
Trond Myklebust88069f72009-12-08 08:33:16 -05001804static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1805 fmode_t fmode)
1806{
1807 spin_lock(&state->owner->so_lock);
1808 if (!(fmode & FMODE_READ))
1809 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1810 if (!(fmode & FMODE_WRITE))
1811 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1812 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1813 spin_unlock(&state->owner->so_lock);
1814}
1815
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001816static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001818 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 struct nfs_server *server = NFS_SERVER(calldata->inode);
1821
Andy Adamson19ddab02009-04-01 09:22:20 -04001822 nfs4_sequence_done(server, &calldata->res.seq_res, task->tk_status);
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001823 if (RPC_ASSASSINATED(task))
1824 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 /* hmm. we are done with the inode, and in the process of freeing
1826 * the state_owner. we keep this around to process errors
1827 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 switch (task->tk_status) {
1829 case 0:
Trond Myklebust45328c32007-07-26 17:47:34 -04001830 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001831 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05001832 nfs4_close_clear_stateid_flags(state,
1833 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 break;
1835 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001836 case -NFS4ERR_OLD_STATEID:
1837 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001839 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001840 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05001842 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1843 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 }
Trond Myklebust72211db2009-12-15 14:47:36 -05001845 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04001846 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847}
1848
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001849static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001851 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001852 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05001853 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07001854
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001855 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001856 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001857
Trond Myklebust88069f72009-12-08 08:33:16 -05001858 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1859 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05001860 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001861 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01001862 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001863 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001864 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
1865 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1866 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04001867 }
1868 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001869 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
1870 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1871 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04001872 }
Trond Myklebuste7616922006-01-03 09:55:13 +01001873 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05001874 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05001875
1876 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001877 /* Note: exit _without_ calling nfs4_close_done */
1878 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07001879 return;
1880 }
Trond Myklebust88069f72009-12-08 08:33:16 -05001881
1882 if (calldata->arg.fmode == 0)
1883 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
1884
Trond Myklebust516a6af2005-10-27 22:12:41 -04001885 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001886 calldata->timestamp = jiffies;
Andy Adamson19ddab02009-04-01 09:22:20 -04001887 if (nfs4_setup_sequence((NFS_SERVER(calldata->inode))->nfs_client,
1888 &calldata->arg.seq_args, &calldata->res.seq_res,
1889 1, task))
1890 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001891 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892}
1893
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001894static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001895 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001896 .rpc_call_done = nfs4_close_done,
1897 .rpc_release = nfs4_free_closedata,
1898};
1899
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900/*
1901 * It is possible for data to be read/written from a mem-mapped file
1902 * after the sys_close call (which hits the vfs layer as a flush).
1903 * This means that we can't safely call nfsv4 close on a file until
1904 * the inode is cleared. This in turn means that we are not good
1905 * NFSv4 citizens - we do not indicate to the server to update the file's
1906 * share state even when we are done with one of the three share
1907 * stateid's in the inode.
1908 *
1909 * NOTE: Caller must be holding the sp->so_owner semaphore!
1910 */
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001911int nfs4_do_close(struct path *path, struct nfs4_state *state, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001913 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04001915 struct nfs4_state_owner *sp = state->owner;
1916 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001917 struct rpc_message msg = {
1918 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1919 .rpc_cred = state->owner->so_cred,
1920 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001921 struct rpc_task_setup task_setup_data = {
1922 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001923 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001924 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05001925 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001926 .flags = RPC_TASK_ASYNC,
1927 };
Trond Myklebust95121352005-10-18 14:20:12 -07001928 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
Andy Adamson19ddab02009-04-01 09:22:20 -04001930 calldata = kzalloc(sizeof(*calldata), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07001932 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001933 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001935 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04001936 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 /* Serialization for the sequence id */
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001938 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001939 if (calldata->arg.seqid == NULL)
1940 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001941 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04001942 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001943 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04001944 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001945 calldata->res.server = server;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04001946 calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Al Virof6948692010-01-30 13:51:04 -05001947 path_get(path);
1948 calldata->path = *path;
Trond Myklebust95121352005-10-18 14:20:12 -07001949
Trond Myklebust5138fde2007-07-14 15:40:01 -04001950 msg.rpc_argp = &calldata->arg,
1951 msg.rpc_resp = &calldata->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001952 task_setup_data.callback_data = calldata;
1953 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001954 if (IS_ERR(task))
1955 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001956 status = 0;
1957 if (wait)
1958 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001959 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001960 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07001961out_free_calldata:
1962 kfree(calldata);
1963out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04001964 nfs4_put_open_state(state);
1965 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07001966 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967}
1968
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001969static 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 -07001970{
1971 struct file *filp;
Trond Myklebust1b45c462007-07-03 13:04:56 -04001972 int ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001973
Trond Myklebust1b45c462007-07-03 13:04:56 -04001974 /* If the open_intent is for execute, we have an extra check to make */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001975 if (fmode & FMODE_EXEC) {
Trond Myklebustaf22f942007-08-10 17:45:10 -04001976 ret = nfs_may_open(state->inode,
Trond Myklebust1b45c462007-07-03 13:04:56 -04001977 state->owner->so_cred,
1978 nd->intent.open.flags);
1979 if (ret < 0)
1980 goto out_close;
1981 }
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001982 filp = lookup_instantiate_filp(nd, path->dentry, NULL);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001983 if (!IS_ERR(filp)) {
1984 struct nfs_open_context *ctx;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04001985 ctx = nfs_file_open_context(filp);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001986 ctx->state = state;
Trond Myklebust95cf9592006-04-18 13:14:06 -04001987 return 0;
1988 }
Trond Myklebust1b45c462007-07-03 13:04:56 -04001989 ret = PTR_ERR(filp);
1990out_close:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001991 nfs4_close_sync(path, state, fmode & (FMODE_READ|FMODE_WRITE));
Trond Myklebust1b45c462007-07-03 13:04:56 -04001992 return ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001993}
1994
1995struct dentry *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1997{
Trond Myklebustad389da2007-06-05 12:30:00 -04001998 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08001999 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04002000 .dentry = dentry,
2001 };
Jan Blunck4ac91372008-02-14 19:34:32 -08002002 struct dentry *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 struct iattr attr;
2004 struct rpc_cred *cred;
2005 struct nfs4_state *state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002006 struct dentry *res;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002007 fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008
2009 if (nd->flags & LOOKUP_CREATE) {
2010 attr.ia_mode = nd->intent.open.create_mode;
2011 attr.ia_valid = ATTR_MODE;
2012 if (!IS_POSIXACL(dir))
Al Viroce3b0f82009-03-29 19:08:22 -04002013 attr.ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 } else {
2015 attr.ia_valid = 0;
2016 BUG_ON(nd->intent.open.flags & O_CREAT);
2017 }
2018
Trond Myklebust98a8e322008-03-12 12:25:28 -04002019 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 if (IS_ERR(cred))
Trond Myklebust02a913a2005-10-18 14:20:17 -07002021 return (struct dentry *)cred;
Trond Myklebust565277f2007-10-15 18:17:53 -04002022 parent = dentry->d_parent;
2023 /* Protect against concurrent sillydeletes */
2024 nfs_block_sillyrename(parent);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002025 state = nfs4_do_open(dir, &path, fmode, nd->intent.open.flags, &attr, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002027 if (IS_ERR(state)) {
Trond Myklebustd75340c2007-10-01 21:42:01 -04002028 if (PTR_ERR(state) == -ENOENT) {
Trond Myklebust02a913a2005-10-18 14:20:17 -07002029 d_add(dentry, NULL);
Trond Myklebustd75340c2007-10-01 21:42:01 -04002030 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2031 }
Trond Myklebust565277f2007-10-15 18:17:53 -04002032 nfs_unblock_sillyrename(parent);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002033 return (struct dentry *)state;
2034 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002035 res = d_add_unique(dentry, igrab(state->inode));
Trond Myklebust02a913a2005-10-18 14:20:17 -07002036 if (res != NULL)
Trond Myklebustdeee9362007-08-27 11:33:00 -04002037 path.dentry = res;
Trond Myklebustd75340c2007-10-01 21:42:01 -04002038 nfs_set_verifier(path.dentry, nfs_save_change_attribute(dir));
Trond Myklebust565277f2007-10-15 18:17:53 -04002039 nfs_unblock_sillyrename(parent);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002040 nfs4_intent_set_file(nd, &path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002041 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042}
2043
2044int
Trond Myklebust02a913a2005-10-18 14:20:17 -07002045nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046{
Trond Myklebustad389da2007-06-05 12:30:00 -04002047 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08002048 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04002049 .dentry = dentry,
2050 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 struct rpc_cred *cred;
2052 struct nfs4_state *state;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002053 fmode_t fmode = openflags & (FMODE_READ | FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
Trond Myklebust98a8e322008-03-12 12:25:28 -04002055 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 if (IS_ERR(cred))
2057 return PTR_ERR(cred);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002058 state = nfs4_do_open(dir, &path, fmode, openflags, NULL, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002060 if (IS_ERR(state)) {
2061 switch (PTR_ERR(state)) {
2062 case -EPERM:
2063 case -EACCES:
2064 case -EDQUOT:
2065 case -ENOSPC:
2066 case -EROFS:
2067 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
2068 return 1;
Chuck Leverb87c0ad2006-10-19 23:28:42 -07002069 default:
2070 goto out_drop;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002071 }
Trond Myklebust02a913a2005-10-18 14:20:17 -07002072 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002073 if (state->inode == dentry->d_inode) {
Trond Myklebustd75340c2007-10-01 21:42:01 -04002074 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002075 nfs4_intent_set_file(nd, &path, state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 return 1;
2077 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002078 nfs4_close_sync(&path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002079out_drop:
2080 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 return 0;
2082}
2083
Trond Myklebust1185a552009-12-03 15:54:02 -05002084static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002085{
2086 if (ctx->state == NULL)
2087 return;
2088 if (is_sync)
2089 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
2090 else
2091 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
2092}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
2094static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2095{
Benny Halevy43652ad2009-04-01 09:21:54 -04002096 struct nfs4_server_caps_arg args = {
2097 .fhandle = fhandle,
2098 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 struct nfs4_server_caps_res res = {};
2100 struct rpc_message msg = {
2101 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002102 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 .rpc_resp = &res,
2104 };
2105 int status;
2106
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002107 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 if (status == 0) {
2109 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04002110 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2111 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2112 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2113 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2114 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2116 server->caps |= NFS_CAP_ACLS;
2117 if (res.has_links != 0)
2118 server->caps |= NFS_CAP_HARDLINKS;
2119 if (res.has_symlinks != 0)
2120 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04002121 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2122 server->caps |= NFS_CAP_FILEID;
2123 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2124 server->caps |= NFS_CAP_MODE;
2125 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2126 server->caps |= NFS_CAP_NLINK;
2127 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2128 server->caps |= NFS_CAP_OWNER;
2129 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2130 server->caps |= NFS_CAP_OWNER_GROUP;
2131 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2132 server->caps |= NFS_CAP_ATIME;
2133 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2134 server->caps |= NFS_CAP_CTIME;
2135 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2136 server->caps |= NFS_CAP_MTIME;
2137
Trond Myklebusta65318b2009-03-11 14:10:28 -04002138 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2139 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2140 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 server->acl_bitmask = res.acl_bitmask;
2142 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002143
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 return status;
2145}
2146
Trond Myklebust55a97592006-06-09 09:34:19 -04002147int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148{
2149 struct nfs4_exception exception = { };
2150 int err;
2151 do {
2152 err = nfs4_handle_exception(server,
2153 _nfs4_server_capabilities(server, fhandle),
2154 &exception);
2155 } while (exception.retry);
2156 return err;
2157}
2158
2159static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2160 struct nfs_fsinfo *info)
2161{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 struct nfs4_lookup_root_arg args = {
2163 .bitmask = nfs4_fattr_bitmap,
2164 };
2165 struct nfs4_lookup_res res = {
2166 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002167 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 .fh = fhandle,
2169 };
2170 struct rpc_message msg = {
2171 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2172 .rpc_argp = &args,
2173 .rpc_resp = &res,
2174 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002175
Trond Myklebust0e574af2005-10-27 22:12:38 -04002176 nfs_fattr_init(info->fattr);
Trond Myklebustfccba802009-07-21 16:48:07 -04002177 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178}
2179
2180static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2181 struct nfs_fsinfo *info)
2182{
2183 struct nfs4_exception exception = { };
2184 int err;
2185 do {
2186 err = nfs4_handle_exception(server,
2187 _nfs4_lookup_root(server, fhandle, info),
2188 &exception);
2189 } while (exception.retry);
2190 return err;
2191}
2192
David Howells54ceac42006-08-22 20:06:13 -04002193/*
2194 * get the file handle for the "/" directory on the server
2195 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002197 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 int status;
2200
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 status = nfs4_lookup_root(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 if (status == 0)
2203 status = nfs4_server_capabilities(server, fhandle);
2204 if (status == 0)
2205 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002206 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207}
2208
Manoj Naik6b97fd32006-06-09 09:34:29 -04002209/*
2210 * Get locations and (maybe) other attributes of a referral.
2211 * Note that we'll actually follow the referral later when
2212 * we detect fsid mismatch in inode revalidation
2213 */
Trond Myklebust56659e92007-07-17 21:52:39 -04002214static 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 -04002215{
2216 int status = -ENOMEM;
2217 struct page *page = NULL;
2218 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002219
2220 page = alloc_page(GFP_KERNEL);
2221 if (page == NULL)
2222 goto out;
2223 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2224 if (locations == NULL)
2225 goto out;
2226
Trond Myklebustc228fd32007-01-13 02:28:11 -05002227 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002228 if (status != 0)
2229 goto out;
2230 /* Make sure server returned a different fsid for the referral */
2231 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -07002232 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 -04002233 status = -EIO;
2234 goto out;
2235 }
2236
2237 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2238 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
2239 if (!fattr->mode)
2240 fattr->mode = S_IFDIR;
2241 memset(fhandle, 0, sizeof(struct nfs_fh));
2242out:
2243 if (page)
2244 __free_page(page);
2245 if (locations)
2246 kfree(locations);
2247 return status;
2248}
2249
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2251{
2252 struct nfs4_getattr_arg args = {
2253 .fh = fhandle,
2254 .bitmask = server->attr_bitmask,
2255 };
2256 struct nfs4_getattr_res res = {
2257 .fattr = fattr,
2258 .server = server,
2259 };
2260 struct rpc_message msg = {
2261 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2262 .rpc_argp = &args,
2263 .rpc_resp = &res,
2264 };
2265
Trond Myklebust0e574af2005-10-27 22:12:38 -04002266 nfs_fattr_init(fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002267 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268}
2269
2270static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2271{
2272 struct nfs4_exception exception = { };
2273 int err;
2274 do {
2275 err = nfs4_handle_exception(server,
2276 _nfs4_proc_getattr(server, fhandle, fattr),
2277 &exception);
2278 } while (exception.retry);
2279 return err;
2280}
2281
2282/*
2283 * The file is not closed if it is opened due to the a request to change
2284 * the size of the file. The open call will not be needed once the
2285 * VFS layer lookup-intents are implemented.
2286 *
2287 * Close is called when the inode is destroyed.
2288 * If we haven't opened the file for O_WRONLY, we
2289 * need to in the size_change case to obtain a stateid.
2290 *
2291 * Got race?
2292 * Because OPEN is always done by name in nfsv4, it is
2293 * possible that we opened a different file by the same
2294 * name. We can recognize this race condition, but we
2295 * can't do anything about it besides returning an error.
2296 *
2297 * This will be fixed with VFS changes (lookup-intent).
2298 */
2299static int
2300nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2301 struct iattr *sattr)
2302{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002303 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002304 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002305 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 int status;
2307
Trond Myklebust0e574af2005-10-27 22:12:38 -04002308 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309
Trond Myklebustd5308382005-11-04 15:33:38 -05002310 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002311 if (sattr->ia_valid & ATTR_FILE) {
2312 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002313
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002314 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002315 if (ctx) {
2316 cred = ctx->cred;
2317 state = ctx->state;
2318 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002319 }
2320
2321 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002322 if (status == 0)
2323 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 return status;
2325}
2326
Trond Myklebust56659e92007-07-17 21:52:39 -04002327static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
2328 const struct qstr *name, struct nfs_fh *fhandle,
David Howells2b3de442006-08-22 20:06:09 -04002329 struct nfs_fattr *fattr)
2330{
2331 int status;
2332 struct nfs4_lookup_arg args = {
2333 .bitmask = server->attr_bitmask,
2334 .dir_fh = dirfh,
2335 .name = name,
2336 };
2337 struct nfs4_lookup_res res = {
2338 .server = server,
2339 .fattr = fattr,
2340 .fh = fhandle,
2341 };
2342 struct rpc_message msg = {
2343 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2344 .rpc_argp = &args,
2345 .rpc_resp = &res,
2346 };
2347
2348 nfs_fattr_init(fattr);
2349
2350 dprintk("NFS call lookupfh %s\n", name->name);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002351 status = nfs4_call_sync(server, &msg, &args, &res, 0);
David Howells2b3de442006-08-22 20:06:09 -04002352 dprintk("NFS reply lookupfh: %d\n", status);
David Howells2b3de442006-08-22 20:06:09 -04002353 return status;
2354}
2355
2356static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2357 struct qstr *name, struct nfs_fh *fhandle,
2358 struct nfs_fattr *fattr)
2359{
2360 struct nfs4_exception exception = { };
2361 int err;
2362 do {
Trond Myklebust4e56e082007-07-01 18:13:52 -04002363 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
2364 /* FIXME: !!!! */
2365 if (err == -NFS4ERR_MOVED) {
2366 err = -EREMOTE;
2367 break;
2368 }
2369 err = nfs4_handle_exception(server, err, &exception);
David Howells2b3de442006-08-22 20:06:09 -04002370 } while (exception.retry);
2371 return err;
2372}
2373
Trond Myklebust56659e92007-07-17 21:52:39 -04002374static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2376{
Trond Myklebust4e56e082007-07-01 18:13:52 -04002377 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378
2379 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust4e56e082007-07-01 18:13:52 -04002380 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002381 if (status == -NFS4ERR_MOVED)
2382 status = nfs4_get_referral(dir, name, fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 dprintk("NFS reply lookup: %d\n", status);
2384 return status;
2385}
2386
2387static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2388{
2389 struct nfs4_exception exception = { };
2390 int err;
2391 do {
2392 err = nfs4_handle_exception(NFS_SERVER(dir),
2393 _nfs4_proc_lookup(dir, name, fhandle, fattr),
2394 &exception);
2395 } while (exception.retry);
2396 return err;
2397}
2398
2399static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2400{
Trond Myklebust76b32992007-08-10 17:45:11 -04002401 struct nfs_server *server = NFS_SERVER(inode);
2402 struct nfs_fattr fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 struct nfs4_accessargs args = {
2404 .fh = NFS_FH(inode),
Trond Myklebust76b32992007-08-10 17:45:11 -04002405 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002407 struct nfs4_accessres res = {
2408 .server = server,
2409 .fattr = &fattr,
2410 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 struct rpc_message msg = {
2412 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2413 .rpc_argp = &args,
2414 .rpc_resp = &res,
2415 .rpc_cred = entry->cred,
2416 };
2417 int mode = entry->mask;
2418 int status;
2419
2420 /*
2421 * Determine which access bits we want to ask for...
2422 */
2423 if (mode & MAY_READ)
2424 args.access |= NFS4_ACCESS_READ;
2425 if (S_ISDIR(inode->i_mode)) {
2426 if (mode & MAY_WRITE)
2427 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2428 if (mode & MAY_EXEC)
2429 args.access |= NFS4_ACCESS_LOOKUP;
2430 } else {
2431 if (mode & MAY_WRITE)
2432 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2433 if (mode & MAY_EXEC)
2434 args.access |= NFS4_ACCESS_EXECUTE;
2435 }
Trond Myklebust76b32992007-08-10 17:45:11 -04002436 nfs_fattr_init(&fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002437 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 if (!status) {
2439 entry->mask = 0;
2440 if (res.access & NFS4_ACCESS_READ)
2441 entry->mask |= MAY_READ;
2442 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2443 entry->mask |= MAY_WRITE;
2444 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2445 entry->mask |= MAY_EXEC;
Trond Myklebust76b32992007-08-10 17:45:11 -04002446 nfs_refresh_inode(inode, &fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 }
2448 return status;
2449}
2450
2451static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2452{
2453 struct nfs4_exception exception = { };
2454 int err;
2455 do {
2456 err = nfs4_handle_exception(NFS_SERVER(inode),
2457 _nfs4_proc_access(inode, entry),
2458 &exception);
2459 } while (exception.retry);
2460 return err;
2461}
2462
2463/*
2464 * TODO: For the time being, we don't try to get any attributes
2465 * along with any of the zero-copy operations READ, READDIR,
2466 * READLINK, WRITE.
2467 *
2468 * In the case of the first three, we want to put the GETATTR
2469 * after the read-type operation -- this is because it is hard
2470 * to predict the length of a GETATTR response in v4, and thus
2471 * align the READ data correctly. This means that the GETATTR
2472 * may end up partially falling into the page cache, and we should
2473 * shift it into the 'tail' of the xdr_buf before processing.
2474 * To do this efficiently, we need to know the total length
2475 * of data received, which doesn't seem to be available outside
2476 * of the RPC layer.
2477 *
2478 * In the case of WRITE, we also want to put the GETATTR after
2479 * the operation -- in this case because we want to make sure
2480 * we get the post-operation mtime and size. This means that
2481 * we can't use xdr_encode_pages() as written: we need a variant
2482 * of it which would leave room in the 'tail' iovec.
2483 *
2484 * Both of these changes to the XDR layer would in fact be quite
2485 * minor, but I decided to leave them for a subsequent patch.
2486 */
2487static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2488 unsigned int pgbase, unsigned int pglen)
2489{
2490 struct nfs4_readlink args = {
2491 .fh = NFS_FH(inode),
2492 .pgbase = pgbase,
2493 .pglen = pglen,
2494 .pages = &page,
2495 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002496 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 struct rpc_message msg = {
2498 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2499 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002500 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 };
2502
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002503 return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504}
2505
2506static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2507 unsigned int pgbase, unsigned int pglen)
2508{
2509 struct nfs4_exception exception = { };
2510 int err;
2511 do {
2512 err = nfs4_handle_exception(NFS_SERVER(inode),
2513 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2514 &exception);
2515 } while (exception.retry);
2516 return err;
2517}
2518
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519/*
2520 * Got race?
2521 * We will need to arrange for the VFS layer to provide an atomic open.
2522 * Until then, this create/open method is prone to inefficiency and race
2523 * conditions due to the lookup, create, and open VFS calls from sys_open()
2524 * placed on the wire.
2525 *
2526 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2527 * The file will be opened again in the subsequent VFS open call
2528 * (nfs4_proc_file_open).
2529 *
2530 * The open for read will just hang around to be used by any process that
2531 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2532 */
2533
2534static int
2535nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebust02a913a2005-10-18 14:20:17 -07002536 int flags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537{
Trond Myklebustad389da2007-06-05 12:30:00 -04002538 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08002539 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04002540 .dentry = dentry,
2541 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 struct nfs4_state *state;
2543 struct rpc_cred *cred;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002544 fmode_t fmode = flags & (FMODE_READ | FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 int status = 0;
2546
Trond Myklebust98a8e322008-03-12 12:25:28 -04002547 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 if (IS_ERR(cred)) {
2549 status = PTR_ERR(cred);
2550 goto out;
2551 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002552 state = nfs4_do_open(dir, &path, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002553 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 if (IS_ERR(state)) {
2555 status = PTR_ERR(state);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002556 goto out_putcred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002558 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002559 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 if (flags & O_EXCL) {
2561 struct nfs_fattr fattr;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002562 status = nfs4_do_setattr(state->inode, cred, &fattr, sattr, state);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002563 if (status == 0)
Trond Myklebust65e43082005-08-16 11:49:44 -04002564 nfs_setattr_update_inode(state->inode, sattr);
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002565 nfs_post_op_update_inode(state->inode, &fattr);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002566 }
Trond Myklebustad389da2007-06-05 12:30:00 -04002567 if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002568 status = nfs4_intent_set_file(nd, &path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002569 else
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002570 nfs4_close_sync(&path, state, fmode);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002571out_putcred:
2572 put_rpccred(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573out:
2574 return status;
2575}
2576
2577static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2578{
Trond Myklebust16e42952005-10-27 22:12:44 -04002579 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002580 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002582 .name.len = name->len,
2583 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002584 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002586 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002587 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002588 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002590 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2591 .rpc_argp = &args,
2592 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 };
2594 int status;
2595
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002596 nfs_fattr_init(&res.dir_attr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002597 status = nfs4_call_sync(server, &msg, &args, &res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002598 if (status == 0) {
2599 update_changeattr(dir, &res.cinfo);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002600 nfs_post_op_update_inode(dir, &res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002601 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 return status;
2603}
2604
2605static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2606{
2607 struct nfs4_exception exception = { };
2608 int err;
2609 do {
2610 err = nfs4_handle_exception(NFS_SERVER(dir),
2611 _nfs4_proc_remove(dir, name),
2612 &exception);
2613 } while (exception.retry);
2614 return err;
2615}
2616
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002617static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002619 struct nfs_server *server = NFS_SERVER(dir);
2620 struct nfs_removeargs *args = msg->rpc_argp;
2621 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622
Trond Myklebusta65318b2009-03-11 14:10:28 -04002623 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002624 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626}
2627
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002628static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002630 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2631
Andy Adamson472cfbd2009-04-01 09:22:24 -04002632 nfs4_sequence_done(res->server, &res->seq_res, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002633 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002634 return 0;
2635 update_changeattr(dir, &res->cinfo);
2636 nfs_post_op_update_inode(dir, &res->dir_attr);
2637 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638}
2639
2640static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2641 struct inode *new_dir, struct qstr *new_name)
2642{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002643 struct nfs_server *server = NFS_SERVER(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 struct nfs4_rename_arg arg = {
2645 .old_dir = NFS_FH(old_dir),
2646 .new_dir = NFS_FH(new_dir),
2647 .old_name = old_name,
2648 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002649 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 };
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002651 struct nfs_fattr old_fattr, new_fattr;
2652 struct nfs4_rename_res res = {
2653 .server = server,
2654 .old_fattr = &old_fattr,
2655 .new_fattr = &new_fattr,
2656 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 struct rpc_message msg = {
2658 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2659 .rpc_argp = &arg,
2660 .rpc_resp = &res,
2661 };
2662 int status;
2663
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002664 nfs_fattr_init(res.old_fattr);
2665 nfs_fattr_init(res.new_fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002666 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667
2668 if (!status) {
2669 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002670 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002672 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 }
2674 return status;
2675}
2676
2677static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2678 struct inode *new_dir, struct qstr *new_name)
2679{
2680 struct nfs4_exception exception = { };
2681 int err;
2682 do {
2683 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2684 _nfs4_proc_rename(old_dir, old_name,
2685 new_dir, new_name),
2686 &exception);
2687 } while (exception.retry);
2688 return err;
2689}
2690
2691static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2692{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002693 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 struct nfs4_link_arg arg = {
2695 .fh = NFS_FH(inode),
2696 .dir_fh = NFS_FH(dir),
2697 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002698 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002700 struct nfs_fattr fattr, dir_attr;
2701 struct nfs4_link_res res = {
2702 .server = server,
2703 .fattr = &fattr,
2704 .dir_attr = &dir_attr,
2705 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 struct rpc_message msg = {
2707 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2708 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002709 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 };
2711 int status;
2712
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002713 nfs_fattr_init(res.fattr);
2714 nfs_fattr_init(res.dir_attr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002715 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002716 if (!status) {
2717 update_changeattr(dir, &res.cinfo);
2718 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002719 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721
2722 return status;
2723}
2724
2725static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2726{
2727 struct nfs4_exception exception = { };
2728 int err;
2729 do {
2730 err = nfs4_handle_exception(NFS_SERVER(inode),
2731 _nfs4_proc_link(inode, dir, name),
2732 &exception);
2733 } while (exception.retry);
2734 return err;
2735}
2736
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002737struct nfs4_createdata {
2738 struct rpc_message msg;
2739 struct nfs4_create_arg arg;
2740 struct nfs4_create_res res;
2741 struct nfs_fh fh;
2742 struct nfs_fattr fattr;
2743 struct nfs_fattr dir_fattr;
2744};
2745
2746static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2747 struct qstr *name, struct iattr *sattr, u32 ftype)
2748{
2749 struct nfs4_createdata *data;
2750
2751 data = kzalloc(sizeof(*data), GFP_KERNEL);
2752 if (data != NULL) {
2753 struct nfs_server *server = NFS_SERVER(dir);
2754
2755 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2756 data->msg.rpc_argp = &data->arg;
2757 data->msg.rpc_resp = &data->res;
2758 data->arg.dir_fh = NFS_FH(dir);
2759 data->arg.server = server;
2760 data->arg.name = name;
2761 data->arg.attrs = sattr;
2762 data->arg.ftype = ftype;
2763 data->arg.bitmask = server->attr_bitmask;
2764 data->res.server = server;
2765 data->res.fh = &data->fh;
2766 data->res.fattr = &data->fattr;
2767 data->res.dir_fattr = &data->dir_fattr;
2768 nfs_fattr_init(data->res.fattr);
2769 nfs_fattr_init(data->res.dir_fattr);
2770 }
2771 return data;
2772}
2773
2774static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2775{
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002776 int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
2777 &data->arg, &data->res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002778 if (status == 0) {
2779 update_changeattr(dir, &data->res.dir_cinfo);
2780 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2781 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2782 }
2783 return status;
2784}
2785
2786static void nfs4_free_createdata(struct nfs4_createdata *data)
2787{
2788 kfree(data);
2789}
2790
Chuck Lever4f390c12006-08-22 20:06:22 -04002791static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002792 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002794 struct nfs4_createdata *data;
2795 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796
Chuck Lever94a6d752006-08-22 20:06:23 -04002797 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002798 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002799
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002800 status = -ENOMEM;
2801 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2802 if (data == NULL)
2803 goto out;
2804
2805 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2806 data->arg.u.symlink.pages = &page;
2807 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002809 status = nfs4_do_create(dir, dentry, data);
2810
2811 nfs4_free_createdata(data);
2812out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 return status;
2814}
2815
Chuck Lever4f390c12006-08-22 20:06:22 -04002816static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002817 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818{
2819 struct nfs4_exception exception = { };
2820 int err;
2821 do {
2822 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002823 _nfs4_proc_symlink(dir, dentry, page,
2824 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 &exception);
2826 } while (exception.retry);
2827 return err;
2828}
2829
2830static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2831 struct iattr *sattr)
2832{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002833 struct nfs4_createdata *data;
2834 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002836 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2837 if (data == NULL)
2838 goto out;
2839
2840 status = nfs4_do_create(dir, dentry, data);
2841
2842 nfs4_free_createdata(data);
2843out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 return status;
2845}
2846
2847static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2848 struct iattr *sattr)
2849{
2850 struct nfs4_exception exception = { };
2851 int err;
2852 do {
2853 err = nfs4_handle_exception(NFS_SERVER(dir),
2854 _nfs4_proc_mkdir(dir, dentry, sattr),
2855 &exception);
2856 } while (exception.retry);
2857 return err;
2858}
2859
2860static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2861 u64 cookie, struct page *page, unsigned int count, int plus)
2862{
2863 struct inode *dir = dentry->d_inode;
2864 struct nfs4_readdir_arg args = {
2865 .fh = NFS_FH(dir),
2866 .pages = &page,
2867 .pgbase = 0,
2868 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09002869 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 };
2871 struct nfs4_readdir_res res;
2872 struct rpc_message msg = {
2873 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2874 .rpc_argp = &args,
2875 .rpc_resp = &res,
2876 .rpc_cred = cred,
2877 };
2878 int status;
2879
Harvey Harrison3110ff82008-05-02 13:42:44 -07002880 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00002881 dentry->d_parent->d_name.name,
2882 dentry->d_name.name,
2883 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2885 res.pgbase = args.pgbase;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002886 status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 if (status == 0)
2888 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustc4812992007-09-28 17:11:45 -04002889
2890 nfs_invalidate_atime(dir);
2891
Harvey Harrison3110ff82008-05-02 13:42:44 -07002892 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 return status;
2894}
2895
2896static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2897 u64 cookie, struct page *page, unsigned int count, int plus)
2898{
2899 struct nfs4_exception exception = { };
2900 int err;
2901 do {
2902 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2903 _nfs4_proc_readdir(dentry, cred, cookie,
2904 page, count, plus),
2905 &exception);
2906 } while (exception.retry);
2907 return err;
2908}
2909
2910static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2911 struct iattr *sattr, dev_t rdev)
2912{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002913 struct nfs4_createdata *data;
2914 int mode = sattr->ia_mode;
2915 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916
2917 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2918 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002919
2920 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2921 if (data == NULL)
2922 goto out;
2923
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002925 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002927 data->arg.ftype = NF4BLK;
2928 data->arg.u.device.specdata1 = MAJOR(rdev);
2929 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 }
2931 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002932 data->arg.ftype = NF4CHR;
2933 data->arg.u.device.specdata1 = MAJOR(rdev);
2934 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002937 status = nfs4_do_create(dir, dentry, data);
2938
2939 nfs4_free_createdata(data);
2940out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 return status;
2942}
2943
2944static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2945 struct iattr *sattr, dev_t rdev)
2946{
2947 struct nfs4_exception exception = { };
2948 int err;
2949 do {
2950 err = nfs4_handle_exception(NFS_SERVER(dir),
2951 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2952 &exception);
2953 } while (exception.retry);
2954 return err;
2955}
2956
2957static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2958 struct nfs_fsstat *fsstat)
2959{
2960 struct nfs4_statfs_arg args = {
2961 .fh = fhandle,
2962 .bitmask = server->attr_bitmask,
2963 };
Benny Halevy24ad1482009-04-01 09:21:56 -04002964 struct nfs4_statfs_res res = {
2965 .fsstat = fsstat,
2966 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 struct rpc_message msg = {
2968 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2969 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04002970 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 };
2972
Trond Myklebust0e574af2005-10-27 22:12:38 -04002973 nfs_fattr_init(fsstat->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002974 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975}
2976
2977static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2978{
2979 struct nfs4_exception exception = { };
2980 int err;
2981 do {
2982 err = nfs4_handle_exception(server,
2983 _nfs4_proc_statfs(server, fhandle, fsstat),
2984 &exception);
2985 } while (exception.retry);
2986 return err;
2987}
2988
2989static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2990 struct nfs_fsinfo *fsinfo)
2991{
2992 struct nfs4_fsinfo_arg args = {
2993 .fh = fhandle,
2994 .bitmask = server->attr_bitmask,
2995 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04002996 struct nfs4_fsinfo_res res = {
2997 .fsinfo = fsinfo,
2998 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 struct rpc_message msg = {
3000 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3001 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003002 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 };
3004
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003005 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006}
3007
3008static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3009{
3010 struct nfs4_exception exception = { };
3011 int err;
3012
3013 do {
3014 err = nfs4_handle_exception(server,
3015 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3016 &exception);
3017 } while (exception.retry);
3018 return err;
3019}
3020
3021static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3022{
Trond Myklebust0e574af2005-10-27 22:12:38 -04003023 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3025}
3026
3027static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3028 struct nfs_pathconf *pathconf)
3029{
3030 struct nfs4_pathconf_arg args = {
3031 .fh = fhandle,
3032 .bitmask = server->attr_bitmask,
3033 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003034 struct nfs4_pathconf_res res = {
3035 .pathconf = pathconf,
3036 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 struct rpc_message msg = {
3038 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3039 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003040 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 };
3042
3043 /* None of the pathconf attributes are mandatory to implement */
3044 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3045 memset(pathconf, 0, sizeof(*pathconf));
3046 return 0;
3047 }
3048
Trond Myklebust0e574af2005-10-27 22:12:38 -04003049 nfs_fattr_init(pathconf->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003050 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051}
3052
3053static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3054 struct nfs_pathconf *pathconf)
3055{
3056 struct nfs4_exception exception = { };
3057 int err;
3058
3059 do {
3060 err = nfs4_handle_exception(server,
3061 _nfs4_proc_pathconf(server, fhandle, pathconf),
3062 &exception);
3063 } while (exception.retry);
3064 return err;
3065}
3066
Trond Myklebustec06c092006-03-20 13:44:27 -05003067static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068{
Trond Myklebustec06c092006-03-20 13:44:27 -05003069 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070
Andy Adamsonf11c88a2009-04-01 09:22:25 -04003071 dprintk("--> %s\n", __func__);
3072
Andy Adamsonf11c88a2009-04-01 09:22:25 -04003073 nfs4_sequence_done(server, &data->res.seq_res, task->tk_status);
3074
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003075 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003076 nfs_restart_rpc(task, server->nfs_client);
Trond Myklebustec06c092006-03-20 13:44:27 -05003077 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003079
3080 nfs_invalidate_atime(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003082 renew_lease(server, data->timestamp);
3083 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084}
3085
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003086static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 data->timestamp = jiffies;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003089 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090}
3091
Trond Myklebust788e7a82006-03-20 13:44:27 -05003092static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 struct inode *inode = data->inode;
3095
Andy Adamsondef6ed72009-04-01 09:22:26 -04003096 nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
3097 task->tk_status);
3098
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003099 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003100 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003101 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003103 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003105 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003106 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003107 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108}
3109
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003110static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003112 struct nfs_server *server = NFS_SERVER(data->inode);
3113
Trond Myklebusta65318b2009-03-11 14:10:28 -04003114 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003115 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 data->timestamp = jiffies;
3117
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003118 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119}
3120
Trond Myklebust788e7a82006-03-20 13:44:27 -05003121static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 struct inode *inode = data->inode;
3124
Andy Adamson21d9a852009-04-01 09:22:27 -04003125 nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
3126 task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003127 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003128 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003129 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003131 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003132 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133}
3134
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003135static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003137 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138
Trond Myklebusta65318b2009-03-11 14:10:28 -04003139 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003140 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003141 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142}
3143
3144/*
3145 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3146 * standalone procedure for queueing an asynchronous RENEW.
3147 */
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003148static void nfs4_renew_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149{
David Howellsadfa6f92006-08-22 20:06:08 -04003150 struct nfs_client *clp = (struct nfs_client *)task->tk_msg.rpc_argp;
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003151 unsigned long timestamp = (unsigned long)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152
3153 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003154 /* Unless we're shutting down, schedule state recovery! */
3155 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
3156 nfs4_schedule_state_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 return;
3158 }
3159 spin_lock(&clp->cl_lock);
3160 if (time_before(clp->cl_last_renewal,timestamp))
3161 clp->cl_last_renewal = timestamp;
3162 spin_unlock(&clp->cl_lock);
3163}
3164
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003165static const struct rpc_call_ops nfs4_renew_ops = {
3166 .rpc_call_done = nfs4_renew_done,
3167};
3168
David Howellsadfa6f92006-08-22 20:06:08 -04003169int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170{
3171 struct rpc_message msg = {
3172 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3173 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003174 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 };
3176
3177 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003178 &nfs4_renew_ops, (void *)jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179}
3180
David Howellsadfa6f92006-08-22 20:06:08 -04003181int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182{
3183 struct rpc_message msg = {
3184 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3185 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003186 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 };
3188 unsigned long now = jiffies;
3189 int status;
3190
3191 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3192 if (status < 0)
3193 return status;
3194 spin_lock(&clp->cl_lock);
3195 if (time_before(clp->cl_last_renewal,now))
3196 clp->cl_last_renewal = now;
3197 spin_unlock(&clp->cl_lock);
3198 return 0;
3199}
3200
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003201static inline int nfs4_server_supports_acls(struct nfs_server *server)
3202{
3203 return (server->caps & NFS_CAP_ACLS)
3204 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3205 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3206}
3207
3208/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3209 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3210 * the stack.
3211 */
3212#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3213
3214static void buf_to_pages(const void *buf, size_t buflen,
3215 struct page **pages, unsigned int *pgbase)
3216{
3217 const void *p = buf;
3218
3219 *pgbase = offset_in_page(buf);
3220 p -= *pgbase;
3221 while (p < buf + buflen) {
3222 *(pages++) = virt_to_page(p);
3223 p += PAGE_CACHE_SIZE;
3224 }
3225}
3226
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003227struct nfs4_cached_acl {
3228 int cached;
3229 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003230 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003231};
3232
3233static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003234{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003235 struct nfs_inode *nfsi = NFS_I(inode);
3236
3237 spin_lock(&inode->i_lock);
3238 kfree(nfsi->nfs4_acl);
3239 nfsi->nfs4_acl = acl;
3240 spin_unlock(&inode->i_lock);
3241}
3242
3243static void nfs4_zap_acl_attr(struct inode *inode)
3244{
3245 nfs4_set_cached_acl(inode, NULL);
3246}
3247
3248static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3249{
3250 struct nfs_inode *nfsi = NFS_I(inode);
3251 struct nfs4_cached_acl *acl;
3252 int ret = -ENOENT;
3253
3254 spin_lock(&inode->i_lock);
3255 acl = nfsi->nfs4_acl;
3256 if (acl == NULL)
3257 goto out;
3258 if (buf == NULL) /* user is just asking for length */
3259 goto out_len;
3260 if (acl->cached == 0)
3261 goto out;
3262 ret = -ERANGE; /* see getxattr(2) man page */
3263 if (acl->len > buflen)
3264 goto out;
3265 memcpy(buf, acl->data, acl->len);
3266out_len:
3267 ret = acl->len;
3268out:
3269 spin_unlock(&inode->i_lock);
3270 return ret;
3271}
3272
3273static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3274{
3275 struct nfs4_cached_acl *acl;
3276
3277 if (buf && acl_len <= PAGE_SIZE) {
3278 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3279 if (acl == NULL)
3280 goto out;
3281 acl->cached = 1;
3282 memcpy(acl->data, buf, acl_len);
3283 } else {
3284 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3285 if (acl == NULL)
3286 goto out;
3287 acl->cached = 0;
3288 }
3289 acl->len = acl_len;
3290out:
3291 nfs4_set_cached_acl(inode, acl);
3292}
3293
Trond Myklebust16b42892006-08-24 12:27:15 -04003294static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003295{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003296 struct page *pages[NFS4ACL_MAXPAGES];
3297 struct nfs_getaclargs args = {
3298 .fh = NFS_FH(inode),
3299 .acl_pages = pages,
3300 .acl_len = buflen,
3301 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003302 struct nfs_getaclres res = {
3303 .acl_len = buflen,
3304 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003305 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003306 struct rpc_message msg = {
3307 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3308 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003309 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003310 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003311 struct page *localpage = NULL;
3312 int ret;
3313
3314 if (buflen < PAGE_SIZE) {
3315 /* As long as we're doing a round trip to the server anyway,
3316 * let's be prepared for a page of acl data. */
3317 localpage = alloc_page(GFP_KERNEL);
3318 resp_buf = page_address(localpage);
3319 if (localpage == NULL)
3320 return -ENOMEM;
3321 args.acl_pages[0] = localpage;
3322 args.acl_pgbase = 0;
Benny Halevy663c79b2009-04-01 09:21:59 -04003323 args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003324 } else {
3325 resp_buf = buf;
3326 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3327 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003328 ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003329 if (ret)
3330 goto out_free;
Benny Halevy663c79b2009-04-01 09:21:59 -04003331 if (res.acl_len > args.acl_len)
3332 nfs4_write_cached_acl(inode, NULL, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003333 else
Benny Halevy663c79b2009-04-01 09:21:59 -04003334 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003335 if (buf) {
3336 ret = -ERANGE;
Benny Halevy663c79b2009-04-01 09:21:59 -04003337 if (res.acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003338 goto out_free;
3339 if (localpage)
Benny Halevy663c79b2009-04-01 09:21:59 -04003340 memcpy(buf, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003341 }
Benny Halevy663c79b2009-04-01 09:21:59 -04003342 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003343out_free:
3344 if (localpage)
3345 __free_page(localpage);
3346 return ret;
3347}
3348
Trond Myklebust16b42892006-08-24 12:27:15 -04003349static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3350{
3351 struct nfs4_exception exception = { };
3352 ssize_t ret;
3353 do {
3354 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3355 if (ret >= 0)
3356 break;
3357 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3358 } while (exception.retry);
3359 return ret;
3360}
3361
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003362static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3363{
3364 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003365 int ret;
3366
3367 if (!nfs4_server_supports_acls(server))
3368 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003369 ret = nfs_revalidate_inode(server, inode);
3370 if (ret < 0)
3371 return ret;
3372 ret = nfs4_read_cached_acl(inode, buf, buflen);
3373 if (ret != -ENOENT)
3374 return ret;
3375 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003376}
3377
Trond Myklebust16b42892006-08-24 12:27:15 -04003378static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003379{
3380 struct nfs_server *server = NFS_SERVER(inode);
3381 struct page *pages[NFS4ACL_MAXPAGES];
3382 struct nfs_setaclargs arg = {
3383 .fh = NFS_FH(inode),
3384 .acl_pages = pages,
3385 .acl_len = buflen,
3386 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003387 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003388 struct rpc_message msg = {
3389 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3390 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003391 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003392 };
3393 int ret;
3394
3395 if (!nfs4_server_supports_acls(server))
3396 return -EOPNOTSUPP;
Trond Myklebust642ac542005-10-18 14:20:19 -07003397 nfs_inode_return_delegation(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003398 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003399 ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003400 nfs_access_zap_cache(inode);
3401 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003402 return ret;
3403}
3404
Trond Myklebust16b42892006-08-24 12:27:15 -04003405static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3406{
3407 struct nfs4_exception exception = { };
3408 int err;
3409 do {
3410 err = nfs4_handle_exception(NFS_SERVER(inode),
3411 __nfs4_proc_set_acl(inode, buf, buflen),
3412 &exception);
3413 } while (exception.retry);
3414 return err;
3415}
3416
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417static int
Andy Adamson8328d592009-04-01 09:22:35 -04003418_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 -07003419{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 if (!clp || task->tk_status >= 0)
3421 return 0;
3422 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003423 case -NFS4ERR_ADMIN_REVOKED:
3424 case -NFS4ERR_BAD_STATEID:
3425 case -NFS4ERR_OPENMODE:
3426 if (state == NULL)
3427 break;
3428 nfs4_state_mark_reclaim_nograce(clp, state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003429 goto do_state_recovery;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003431 if (state == NULL)
3432 break;
3433 nfs4_state_mark_reclaim_reboot(clp, state);
3434 case -NFS4ERR_STALE_CLIENTID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 case -NFS4ERR_EXPIRED:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003436 goto do_state_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003437#if defined(CONFIG_NFS_V4_1)
3438 case -NFS4ERR_BADSESSION:
3439 case -NFS4ERR_BADSLOT:
3440 case -NFS4ERR_BAD_HIGH_SLOT:
3441 case -NFS4ERR_DEADSESSION:
3442 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3443 case -NFS4ERR_SEQ_FALSE_RETRY:
3444 case -NFS4ERR_SEQ_MISORDERED:
3445 dprintk("%s ERROR %d, Reset session\n", __func__,
3446 task->tk_status);
Andy Adamson0b9e2d42009-12-04 16:02:14 -05003447 nfs4_schedule_state_recovery(clp);
Andy Adamson4745e312009-04-01 09:22:42 -04003448 task->tk_status = 0;
3449 return -EAGAIN;
3450#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 case -NFS4ERR_DELAY:
Andy Adamson8328d592009-04-01 09:22:35 -04003452 if (server)
3453 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003454 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3456 task->tk_status = 0;
3457 return -EAGAIN;
3458 case -NFS4ERR_OLD_STATEID:
3459 task->tk_status = 0;
3460 return -EAGAIN;
3461 }
3462 task->tk_status = nfs4_map_errors(task->tk_status);
3463 return 0;
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003464do_state_recovery:
3465 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
3466 nfs4_schedule_state_recovery(clp);
3467 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3468 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3469 task->tk_status = 0;
3470 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471}
3472
Andy Adamson8328d592009-04-01 09:22:35 -04003473static int
3474nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3475{
3476 return _nfs4_async_handle_error(task, server, server->nfs_client, state);
3477}
3478
David Howellsadfa6f92006-08-22 20:06:08 -04003479int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, unsigned short port, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480{
3481 nfs4_verifier sc_verifier;
3482 struct nfs4_setclientid setclientid = {
3483 .sc_verifier = &sc_verifier,
3484 .sc_prog = program,
3485 };
3486 struct rpc_message msg = {
3487 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3488 .rpc_argp = &setclientid,
3489 .rpc_resp = clp,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003490 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 };
Al Virobc4785c2006-10-19 23:28:51 -07003492 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 int loop = 0;
3494 int status;
3495
Al Virobc4785c2006-10-19 23:28:51 -07003496 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3498 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3499
3500 for(;;) {
3501 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003502 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003503 clp->cl_ipaddr,
3504 rpc_peeraddr2str(clp->cl_rpcclient,
3505 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003506 rpc_peeraddr2str(clp->cl_rpcclient,
3507 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003508 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 clp->cl_id_uniquifier);
3510 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003511 sizeof(setclientid.sc_netid),
3512 rpc_peeraddr2str(clp->cl_rpcclient,
3513 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003515 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 clp->cl_ipaddr, port >> 8, port & 255);
3517
3518 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3519 if (status != -NFS4ERR_CLID_INUSE)
3520 break;
3521 if (signalled())
3522 break;
3523 if (loop++ & 1)
3524 ssleep(clp->cl_lease_time + 1);
3525 else
3526 if (++clp->cl_id_uniquifier == 0)
3527 break;
3528 }
3529 return status;
3530}
3531
David Howellsadfa6f92006-08-22 20:06:08 -04003532static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533{
3534 struct nfs_fsinfo fsinfo;
3535 struct rpc_message msg = {
3536 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
3537 .rpc_argp = clp,
3538 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003539 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 };
3541 unsigned long now;
3542 int status;
3543
3544 now = jiffies;
3545 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3546 if (status == 0) {
3547 spin_lock(&clp->cl_lock);
3548 clp->cl_lease_time = fsinfo.lease_time * HZ;
3549 clp->cl_last_renewal = now;
3550 spin_unlock(&clp->cl_lock);
3551 }
3552 return status;
3553}
3554
David Howellsadfa6f92006-08-22 20:06:08 -04003555int nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
Trond Myklebust51581f32006-03-20 13:44:47 -05003556{
Benny Halevy77e03672008-06-24 20:25:57 +03003557 long timeout = 0;
Trond Myklebust51581f32006-03-20 13:44:47 -05003558 int err;
3559 do {
3560 err = _nfs4_proc_setclientid_confirm(clp, cred);
3561 switch (err) {
3562 case 0:
3563 return err;
3564 case -NFS4ERR_RESOURCE:
3565 /* The IBM lawyers misread another document! */
3566 case -NFS4ERR_DELAY:
3567 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3568 }
3569 } while (err == 0);
3570 return err;
3571}
3572
Trond Myklebustfe650402006-01-03 09:55:18 +01003573struct nfs4_delegreturndata {
3574 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003575 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003576 struct nfs_fh fh;
3577 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003578 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003579 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003580 int rpc_status;
3581};
3582
Trond Myklebustfe650402006-01-03 09:55:18 +01003583static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3584{
3585 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003586
Trond Myklebustd61e6122009-12-05 19:32:19 -05003587 nfs4_sequence_done(data->res.server, &data->res.seq_res,
3588 task->tk_status);
Andy Adamson938e1012009-04-01 09:22:28 -04003589
Ricardo Labiaga79708862009-12-07 09:23:21 -05003590 switch (task->tk_status) {
3591 case -NFS4ERR_STALE_STATEID:
3592 case -NFS4ERR_EXPIRED:
3593 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01003594 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003595 break;
3596 default:
3597 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3598 -EAGAIN) {
3599 nfs_restart_rpc(task, data->res.server->nfs_client);
3600 return;
3601 }
3602 }
3603 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01003604}
3605
3606static void nfs4_delegreturn_release(void *calldata)
3607{
Trond Myklebustfe650402006-01-03 09:55:18 +01003608 kfree(calldata);
3609}
3610
Andy Adamson938e1012009-04-01 09:22:28 -04003611#if defined(CONFIG_NFS_V4_1)
3612static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3613{
3614 struct nfs4_delegreturndata *d_data;
3615
3616 d_data = (struct nfs4_delegreturndata *)data;
3617
3618 if (nfs4_setup_sequence(d_data->res.server->nfs_client,
3619 &d_data->args.seq_args,
3620 &d_data->res.seq_res, 1, task))
3621 return;
3622 rpc_call_start(task);
3623}
3624#endif /* CONFIG_NFS_V4_1 */
3625
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003626static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04003627#if defined(CONFIG_NFS_V4_1)
3628 .rpc_call_prepare = nfs4_delegreturn_prepare,
3629#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01003630 .rpc_call_done = nfs4_delegreturn_done,
3631 .rpc_release = nfs4_delegreturn_release,
3632};
3633
Trond Myklebuste6f81072008-01-24 18:14:34 -05003634static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003635{
3636 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003637 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003638 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003639 struct rpc_message msg = {
3640 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3641 .rpc_cred = cred,
3642 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003643 struct rpc_task_setup task_setup_data = {
3644 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003645 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003646 .callback_ops = &nfs4_delegreturn_ops,
3647 .flags = RPC_TASK_ASYNC,
3648 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003649 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003650
Andy Adamson938e1012009-04-01 09:22:28 -04003651 data = kzalloc(sizeof(*data), GFP_KERNEL);
Trond Myklebustfe650402006-01-03 09:55:18 +01003652 if (data == NULL)
3653 return -ENOMEM;
3654 data->args.fhandle = &data->fh;
3655 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003656 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003657 nfs_copy_fh(&data->fh, NFS_FH(inode));
3658 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003659 data->res.fattr = &data->fattr;
3660 data->res.server = server;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003661 data->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003662 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003663 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003664 data->rpc_status = 0;
3665
Trond Myklebustc970aa82007-07-14 15:39:59 -04003666 task_setup_data.callback_data = data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003667 msg.rpc_argp = &data->args,
3668 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003669 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003670 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003671 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003672 if (!issync)
3673 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003674 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003675 if (status != 0)
3676 goto out;
3677 status = data->rpc_status;
3678 if (status != 0)
3679 goto out;
3680 nfs_refresh_inode(inode, &data->fattr);
3681out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003682 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003683 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684}
3685
Trond Myklebuste6f81072008-01-24 18:14:34 -05003686int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687{
3688 struct nfs_server *server = NFS_SERVER(inode);
3689 struct nfs4_exception exception = { };
3690 int err;
3691 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003692 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693 switch (err) {
3694 case -NFS4ERR_STALE_STATEID:
3695 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 case 0:
3697 return 0;
3698 }
3699 err = nfs4_handle_exception(server, err, &exception);
3700 } while (exception.retry);
3701 return err;
3702}
3703
3704#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3705#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3706
3707/*
3708 * sleep, with exponential backoff, and retry the LOCK operation.
3709 */
3710static unsigned long
3711nfs4_set_lock_task_retry(unsigned long timeout)
3712{
Matthew Wilcox150030b2007-12-06 16:24:39 -05003713 schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714 timeout <<= 1;
3715 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3716 return NFS4_LOCK_MAXTIMEOUT;
3717 return timeout;
3718}
3719
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3721{
3722 struct inode *inode = state->inode;
3723 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003724 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003725 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003727 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003729 struct nfs_lockt_res res = {
3730 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731 };
3732 struct rpc_message msg = {
3733 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3734 .rpc_argp = &arg,
3735 .rpc_resp = &res,
3736 .rpc_cred = state->owner->so_cred,
3737 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738 struct nfs4_lock_state *lsp;
3739 int status;
3740
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003741 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003742 status = nfs4_set_lock_state(state, request);
3743 if (status != 0)
3744 goto out;
3745 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003746 arg.lock_owner.id = lsp->ls_id.id;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003747 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003748 switch (status) {
3749 case 0:
3750 request->fl_type = F_UNLCK;
3751 break;
3752 case -NFS4ERR_DENIED:
3753 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05003755 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003756out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 return status;
3758}
3759
3760static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3761{
3762 struct nfs4_exception exception = { };
3763 int err;
3764
3765 do {
3766 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3767 _nfs4_proc_getlk(state, cmd, request),
3768 &exception);
3769 } while (exception.retry);
3770 return err;
3771}
3772
3773static int do_vfs_lock(struct file *file, struct file_lock *fl)
3774{
3775 int res = 0;
3776 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3777 case FL_POSIX:
3778 res = posix_lock_file_wait(file, fl);
3779 break;
3780 case FL_FLOCK:
3781 res = flock_lock_file_wait(file, fl);
3782 break;
3783 default:
3784 BUG();
3785 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 return res;
3787}
3788
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003789struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003790 struct nfs_locku_args arg;
3791 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003792 struct nfs4_lock_state *lsp;
3793 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003794 struct file_lock fl;
3795 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003796 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003797};
3798
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003799static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3800 struct nfs_open_context *ctx,
3801 struct nfs4_lock_state *lsp,
3802 struct nfs_seqid *seqid)
3803{
3804 struct nfs4_unlockdata *p;
3805 struct inode *inode = lsp->ls_state->inode;
3806
Andy Adamsona8936932009-04-01 09:22:23 -04003807 p = kzalloc(sizeof(*p), GFP_KERNEL);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003808 if (p == NULL)
3809 return NULL;
3810 p->arg.fh = NFS_FH(inode);
3811 p->arg.fl = &p->fl;
3812 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003813 p->res.seqid = seqid;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003814 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003815 p->arg.stateid = &lsp->ls_stateid;
3816 p->lsp = lsp;
3817 atomic_inc(&lsp->ls_count);
3818 /* Ensure we don't close file until we're done freeing locks! */
3819 p->ctx = get_nfs_open_context(ctx);
3820 memcpy(&p->fl, fl, sizeof(p->fl));
3821 p->server = NFS_SERVER(inode);
3822 return p;
3823}
3824
Trond Myklebust06f814a2006-01-03 09:55:07 +01003825static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003826{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003827 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003828 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003829 nfs4_put_lock_state(calldata->lsp);
3830 put_nfs_open_context(calldata->ctx);
3831 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003832}
3833
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003834static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003835{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003836 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003837
Andy Adamsona8936932009-04-01 09:22:23 -04003838 nfs4_sequence_done(calldata->server, &calldata->res.seq_res,
3839 task->tk_status);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003840 if (RPC_ASSASSINATED(task))
3841 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003842 switch (task->tk_status) {
3843 case 0:
3844 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003845 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003846 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01003847 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003848 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003849 case -NFS4ERR_BAD_STATEID:
3850 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003851 case -NFS4ERR_STALE_STATEID:
3852 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003853 break;
3854 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003855 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebust0110ee12009-12-07 09:00:24 -05003856 nfs_restart_rpc(task,
Trond Myklebustd61e6122009-12-05 19:32:19 -05003857 calldata->server->nfs_client);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003858 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003859}
3860
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003861static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003862{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003863 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003865 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003866 return;
3867 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003868 /* Note: exit _without_ running nfs4_locku_done */
3869 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003870 return;
3871 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01003872 calldata->timestamp = jiffies;
Andy Adamsona8936932009-04-01 09:22:23 -04003873 if (nfs4_setup_sequence(calldata->server->nfs_client,
3874 &calldata->arg.seq_args,
3875 &calldata->res.seq_res, 1, task))
3876 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003877 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878}
3879
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003880static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003881 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003882 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01003883 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003884};
3885
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003886static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3887 struct nfs_open_context *ctx,
3888 struct nfs4_lock_state *lsp,
3889 struct nfs_seqid *seqid)
3890{
3891 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003892 struct rpc_message msg = {
3893 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3894 .rpc_cred = ctx->cred,
3895 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003896 struct rpc_task_setup task_setup_data = {
3897 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04003898 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003899 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003900 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003901 .flags = RPC_TASK_ASYNC,
3902 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003903
Frank Filz137d6ac2007-07-09 15:32:29 -07003904 /* Ensure this is an unlock - when canceling a lock, the
3905 * canceled lock is passed in, and it won't be an unlock.
3906 */
3907 fl->fl_type = F_UNLCK;
3908
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003909 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3910 if (data == NULL) {
3911 nfs_free_seqid(seqid);
3912 return ERR_PTR(-ENOMEM);
3913 }
3914
Trond Myklebust5138fde2007-07-14 15:40:01 -04003915 msg.rpc_argp = &data->arg,
3916 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003917 task_setup_data.callback_data = data;
3918 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003919}
3920
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3922{
Trond Myklebust19e03c52008-12-23 15:21:44 -05003923 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003924 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003925 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01003926 struct rpc_task *task;
3927 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003928 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929
Trond Myklebust9b073572006-06-29 16:38:34 -04003930 status = nfs4_set_lock_state(state, request);
3931 /* Unlock _before_ we do the RPC call */
3932 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003933 down_read(&nfsi->rwsem);
3934 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
3935 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003936 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003937 }
3938 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003939 if (status != 0)
3940 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003941 /* Is this a delegated lock? */
3942 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04003943 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003944 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003945 seqid = nfs_alloc_seqid(&lsp->ls_seqid);
Trond Myklebust9b073572006-06-29 16:38:34 -04003946 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003947 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04003948 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04003949 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003950 status = PTR_ERR(task);
3951 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04003952 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003953 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003954 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04003955out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003956 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003957 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958}
3959
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003960struct nfs4_lockdata {
3961 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003962 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003963 struct nfs4_lock_state *lsp;
3964 struct nfs_open_context *ctx;
3965 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003966 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003967 int rpc_status;
3968 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04003969 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003970};
3971
3972static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
3973 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp)
3974{
3975 struct nfs4_lockdata *p;
3976 struct inode *inode = lsp->ls_state->inode;
3977 struct nfs_server *server = NFS_SERVER(inode);
3978
3979 p = kzalloc(sizeof(*p), GFP_KERNEL);
3980 if (p == NULL)
3981 return NULL;
3982
3983 p->arg.fh = NFS_FH(inode);
3984 p->arg.fl = &p->fl;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003985 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid);
3986 if (p->arg.open_seqid == NULL)
3987 goto out_free;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003988 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3989 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003990 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003991 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04003992 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003993 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003994 p->res.lock_seqid = p->arg.lock_seqid;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003995 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003996 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04003997 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003998 atomic_inc(&lsp->ls_count);
3999 p->ctx = get_nfs_open_context(ctx);
4000 memcpy(&p->fl, fl, sizeof(p->fl));
4001 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004002out_free_seqid:
4003 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004004out_free:
4005 kfree(p);
4006 return NULL;
4007}
4008
4009static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4010{
4011 struct nfs4_lockdata *data = calldata;
4012 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013
Harvey Harrison3110ff82008-05-02 13:42:44 -07004014 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004015 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4016 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004017 /* Do we need to do an open_to_lock_owner? */
4018 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004019 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4020 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004021 data->arg.open_stateid = &state->stateid;
4022 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004023 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004024 } else
4025 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004026 data->timestamp = jiffies;
Andy Adamson66179ef2009-04-01 09:22:22 -04004027 if (nfs4_setup_sequence(data->server->nfs_client, &data->arg.seq_args,
4028 &data->res.seq_res, 1, task))
4029 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004030 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004031 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004032}
4033
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004034static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4035{
4036 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4037 nfs4_lock_prepare(task, calldata);
4038}
4039
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004040static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4041{
4042 struct nfs4_lockdata *data = calldata;
4043
Harvey Harrison3110ff82008-05-02 13:42:44 -07004044 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004045
Trond Myklebustd61e6122009-12-05 19:32:19 -05004046 nfs4_sequence_done(data->server, &data->res.seq_res,
4047 task->tk_status);
Andy Adamson66179ef2009-04-01 09:22:22 -04004048
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004049 data->rpc_status = task->tk_status;
4050 if (RPC_ASSASSINATED(task))
4051 goto out;
4052 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004053 if (data->rpc_status == 0)
4054 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4055 else
4056 goto out;
4057 }
4058 if (data->rpc_status == 0) {
4059 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4060 sizeof(data->lsp->ls_stateid.data));
4061 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Trond Myklebust88be9f92007-06-05 10:42:27 -04004062 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004063 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004064out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004065 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004066}
4067
4068static void nfs4_lock_release(void *calldata)
4069{
4070 struct nfs4_lockdata *data = calldata;
4071
Harvey Harrison3110ff82008-05-02 13:42:44 -07004072 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004073 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004074 if (data->cancelled != 0) {
4075 struct rpc_task *task;
4076 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4077 data->arg.lock_seqid);
4078 if (!IS_ERR(task))
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004079 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004080 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004081 } else
4082 nfs_free_seqid(data->arg.lock_seqid);
4083 nfs4_put_lock_state(data->lsp);
4084 put_nfs_open_context(data->ctx);
4085 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004086 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004087}
4088
4089static const struct rpc_call_ops nfs4_lock_ops = {
4090 .rpc_call_prepare = nfs4_lock_prepare,
4091 .rpc_call_done = nfs4_lock_done,
4092 .rpc_release = nfs4_lock_release,
4093};
4094
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004095static const struct rpc_call_ops nfs4_recover_lock_ops = {
4096 .rpc_call_prepare = nfs4_recover_lock_prepare,
4097 .rpc_call_done = nfs4_lock_done,
4098 .rpc_release = nfs4_lock_release,
4099};
4100
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004101static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4102{
4103 struct nfs_client *clp = server->nfs_client;
4104 struct nfs4_state *state = lsp->ls_state;
4105
4106 switch (error) {
4107 case -NFS4ERR_ADMIN_REVOKED:
4108 case -NFS4ERR_BAD_STATEID:
4109 case -NFS4ERR_EXPIRED:
4110 if (new_lock_owner != 0 ||
4111 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4112 nfs4_state_mark_reclaim_nograce(clp, state);
4113 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004114 break;
4115 case -NFS4ERR_STALE_STATEID:
4116 if (new_lock_owner != 0 ||
4117 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4118 nfs4_state_mark_reclaim_reboot(clp, state);
4119 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004120 };
4121}
4122
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004123static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004124{
4125 struct nfs4_lockdata *data;
4126 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004127 struct rpc_message msg = {
4128 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4129 .rpc_cred = state->owner->so_cred,
4130 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004131 struct rpc_task_setup task_setup_data = {
4132 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004133 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004134 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004135 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004136 .flags = RPC_TASK_ASYNC,
4137 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004138 int ret;
4139
Harvey Harrison3110ff82008-05-02 13:42:44 -07004140 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004141 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004142 fl->fl_u.nfs4_fl.owner);
4143 if (data == NULL)
4144 return -ENOMEM;
4145 if (IS_SETLKW(cmd))
4146 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004147 if (recovery_type > NFS_LOCK_NEW) {
4148 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004149 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004150 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4151 }
Trond Myklebust5138fde2007-07-14 15:40:01 -04004152 msg.rpc_argp = &data->arg,
4153 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004154 task_setup_data.callback_data = data;
4155 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004156 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004157 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004158 ret = nfs4_wait_for_completion_rpc_task(task);
4159 if (ret == 0) {
4160 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004161 if (ret)
4162 nfs4_handle_setlk_error(data->server, data->lsp,
4163 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004164 } else
4165 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004166 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004167 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004168 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169}
4170
4171static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4172{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004173 struct nfs_server *server = NFS_SERVER(state->inode);
4174 struct nfs4_exception exception = { };
4175 int err;
4176
4177 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004178 /* Cache the lock if possible... */
4179 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4180 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004181 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust202b50d2005-06-22 17:16:29 +00004182 if (err != -NFS4ERR_DELAY)
4183 break;
4184 nfs4_handle_exception(server, err, &exception);
4185 } while (exception.retry);
4186 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187}
4188
4189static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4190{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004191 struct nfs_server *server = NFS_SERVER(state->inode);
4192 struct nfs4_exception exception = { };
4193 int err;
4194
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004195 err = nfs4_set_lock_state(state, request);
4196 if (err != 0)
4197 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004198 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004199 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4200 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004201 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004202 switch (err) {
4203 default:
4204 goto out;
4205 case -NFS4ERR_GRACE:
4206 case -NFS4ERR_DELAY:
4207 nfs4_handle_exception(server, err, &exception);
4208 err = 0;
4209 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004210 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004211out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004212 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213}
4214
4215static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4216{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004217 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004218 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004219 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004221 if ((fl_flags & FL_POSIX) &&
4222 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4223 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004224 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004225 status = nfs4_set_lock_state(state, request);
4226 if (status != 0)
4227 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004228 request->fl_flags |= FL_ACCESS;
4229 status = do_vfs_lock(request->fl_file, request);
4230 if (status < 0)
4231 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004232 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004233 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004234 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004235 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004236 request->fl_flags = fl_flags & ~FL_SLEEP;
4237 status = do_vfs_lock(request->fl_file, request);
4238 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004239 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004240 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004241 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004242 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004243 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004244 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004245 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07004246 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004247out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004248 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004249out:
4250 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251 return status;
4252}
4253
4254static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4255{
4256 struct nfs4_exception exception = { };
4257 int err;
4258
4259 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004260 err = _nfs4_proc_setlk(state, cmd, request);
4261 if (err == -NFS4ERR_DENIED)
4262 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004264 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 } while (exception.retry);
4266 return err;
4267}
4268
4269static int
4270nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4271{
4272 struct nfs_open_context *ctx;
4273 struct nfs4_state *state;
4274 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4275 int status;
4276
4277 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004278 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279 state = ctx->state;
4280
4281 if (request->fl_start < 0 || request->fl_end < 0)
4282 return -EINVAL;
4283
Trond Myklebustd9531262009-07-21 19:22:38 -04004284 if (IS_GETLK(cmd)) {
4285 if (state != NULL)
4286 return nfs4_proc_getlk(state, F_GETLK, request);
4287 return 0;
4288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289
4290 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4291 return -EINVAL;
4292
Trond Myklebustd9531262009-07-21 19:22:38 -04004293 if (request->fl_type == F_UNLCK) {
4294 if (state != NULL)
4295 return nfs4_proc_unlck(state, cmd, request);
4296 return 0;
4297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298
Trond Myklebustd9531262009-07-21 19:22:38 -04004299 if (state == NULL)
4300 return -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301 do {
4302 status = nfs4_proc_setlk(state, cmd, request);
4303 if ((status != -EAGAIN) || IS_SETLK(cmd))
4304 break;
4305 timeout = nfs4_set_lock_task_retry(timeout);
4306 status = -ERESTARTSYS;
4307 if (signalled())
4308 break;
4309 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310 return status;
4311}
4312
Trond Myklebust888e6942005-11-04 15:38:11 -05004313int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4314{
4315 struct nfs_server *server = NFS_SERVER(state->inode);
4316 struct nfs4_exception exception = { };
4317 int err;
4318
4319 err = nfs4_set_lock_state(state, fl);
4320 if (err != 0)
4321 goto out;
4322 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004323 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004324 switch (err) {
4325 default:
4326 printk(KERN_ERR "%s: unhandled error %d.\n",
4327 __func__, err);
4328 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004329 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004330 goto out;
4331 case -NFS4ERR_EXPIRED:
4332 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004333 case -NFS4ERR_STALE_STATEID:
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004334 case -NFS4ERR_BADSESSION:
4335 case -NFS4ERR_BADSLOT:
4336 case -NFS4ERR_BAD_HIGH_SLOT:
4337 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4338 case -NFS4ERR_DEADSESSION:
Trond Myklebustd5122202009-06-17 13:22:58 -07004339 nfs4_schedule_state_recovery(server->nfs_client);
4340 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004341 case -ERESTARTSYS:
4342 /*
4343 * The show must go on: exit, but mark the
4344 * stateid as needing recovery.
4345 */
4346 case -NFS4ERR_ADMIN_REVOKED:
4347 case -NFS4ERR_BAD_STATEID:
4348 case -NFS4ERR_OPENMODE:
4349 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
4350 err = 0;
4351 goto out;
4352 case -ENOMEM:
4353 case -NFS4ERR_DENIED:
4354 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4355 err = 0;
4356 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004357 case -NFS4ERR_DELAY:
4358 break;
4359 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004360 err = nfs4_handle_exception(server, err, &exception);
4361 } while (exception.retry);
4362out:
4363 return err;
4364}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004365
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004366#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4367
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004368int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
4369 size_t buflen, int flags)
4370{
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004371 struct inode *inode = dentry->d_inode;
4372
4373 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4374 return -EOPNOTSUPP;
4375
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004376 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004377}
4378
4379/* The getxattr man page suggests returning -ENODATA for unknown attributes,
4380 * and that's what we'll do for e.g. user attributes that haven't been set.
4381 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
4382 * attributes in kernel-managed attribute namespaces. */
4383ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
4384 size_t buflen)
4385{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004386 struct inode *inode = dentry->d_inode;
4387
4388 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4389 return -EOPNOTSUPP;
4390
4391 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004392}
4393
4394ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
4395{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004396 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004397
J. Bruce Fields096455a2006-03-20 23:23:42 -05004398 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4399 return 0;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004400 if (buf && buflen < len)
4401 return -ERANGE;
4402 if (buf)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004403 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
4404 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004405}
4406
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004407static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4408{
4409 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
4410 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4411 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4412 return;
4413
4414 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4415 NFS_ATTR_FATTR_NLINK;
4416 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4417 fattr->nlink = 2;
4418}
4419
Trond Myklebust56659e92007-07-17 21:52:39 -04004420int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004421 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004422{
4423 struct nfs_server *server = NFS_SERVER(dir);
4424 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004425 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4426 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004427 };
4428 struct nfs4_fs_locations_arg args = {
4429 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004430 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004431 .page = page,
4432 .bitmask = bitmask,
4433 };
Benny Halevy22958462009-04-01 09:22:02 -04004434 struct nfs4_fs_locations_res res = {
4435 .fs_locations = fs_locations,
4436 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004437 struct rpc_message msg = {
4438 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4439 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004440 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004441 };
4442 int status;
4443
Harvey Harrison3110ff82008-05-02 13:42:44 -07004444 dprintk("%s: start\n", __func__);
Trond Myklebustc228fd32007-01-13 02:28:11 -05004445 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004446 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004447 fs_locations->nlocations = 0;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04004448 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004449 nfs_fixup_referral_attributes(&fs_locations->fattr);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004450 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004451 return status;
4452}
4453
Andy Adamson557134a2009-04-01 09:21:53 -04004454#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04004455/*
4456 * nfs4_proc_exchange_id()
4457 *
4458 * Since the clientid has expired, all compounds using sessions
4459 * associated with the stale clientid will be returning
4460 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4461 * be in some phase of session reset.
4462 */
Andy Adamson4d643d12009-12-04 15:52:24 -05004463int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004464{
4465 nfs4_verifier verifier;
4466 struct nfs41_exchange_id_args args = {
4467 .client = clp,
4468 .flags = clp->cl_exchange_flags,
4469 };
4470 struct nfs41_exchange_id_res res = {
4471 .client = clp,
4472 };
4473 int status;
4474 struct rpc_message msg = {
4475 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4476 .rpc_argp = &args,
4477 .rpc_resp = &res,
4478 .rpc_cred = cred,
4479 };
4480 __be32 *p;
4481
4482 dprintk("--> %s\n", __func__);
4483 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04004484
Alexandros Batsakis7b183d02009-12-05 13:33:25 -05004485 /* Remove server-only flags */
4486 args.flags &= ~EXCHGID4_FLAG_CONFIRMED_R;
4487
Benny Halevy99fe60d2009-04-01 09:22:29 -04004488 p = (u32 *)verifier.data;
4489 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4490 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4491 args.verifier = &verifier;
4492
4493 while (1) {
4494 args.id_len = scnprintf(args.id, sizeof(args.id),
4495 "%s/%s %u",
4496 clp->cl_ipaddr,
4497 rpc_peeraddr2str(clp->cl_rpcclient,
4498 RPC_DISPLAY_ADDR),
4499 clp->cl_id_uniquifier);
4500
4501 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
4502
4503 if (status != NFS4ERR_CLID_INUSE)
4504 break;
4505
4506 if (signalled())
4507 break;
4508
4509 if (++clp->cl_id_uniquifier == 0)
4510 break;
4511 }
4512
4513 dprintk("<-- %s status= %d\n", __func__, status);
4514 return status;
4515}
4516
Andy Adamson2050f0c2009-04-01 09:22:30 -04004517struct nfs4_get_lease_time_data {
4518 struct nfs4_get_lease_time_args *args;
4519 struct nfs4_get_lease_time_res *res;
4520 struct nfs_client *clp;
4521};
4522
4523static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4524 void *calldata)
4525{
4526 int ret;
4527 struct nfs4_get_lease_time_data *data =
4528 (struct nfs4_get_lease_time_data *)calldata;
4529
4530 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004531 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004532 /* just setup sequence, do not trigger session recovery
4533 since we're invoked within one */
4534 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004535 &data->args->la_seq_args,
4536 &data->res->lr_seq_res, 0, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004537
4538 BUG_ON(ret == -EAGAIN);
4539 rpc_call_start(task);
4540 dprintk("<-- %s\n", __func__);
4541}
4542
4543/*
4544 * Called from nfs4_state_manager thread for session setup, so don't recover
4545 * from sequence operation or clientid errors.
4546 */
4547static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4548{
4549 struct nfs4_get_lease_time_data *data =
4550 (struct nfs4_get_lease_time_data *)calldata;
4551
4552 dprintk("--> %s\n", __func__);
4553 nfs41_sequence_done(data->clp, &data->res->lr_seq_res, task->tk_status);
4554 switch (task->tk_status) {
4555 case -NFS4ERR_DELAY:
4556 case -NFS4ERR_GRACE:
4557 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4558 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4559 task->tk_status = 0;
Trond Myklebust0110ee12009-12-07 09:00:24 -05004560 nfs_restart_rpc(task, data->clp);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004561 return;
4562 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04004563 dprintk("<-- %s\n", __func__);
4564}
4565
4566struct rpc_call_ops nfs4_get_lease_time_ops = {
4567 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4568 .rpc_call_done = nfs4_get_lease_time_done,
4569};
4570
4571int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4572{
4573 struct rpc_task *task;
4574 struct nfs4_get_lease_time_args args;
4575 struct nfs4_get_lease_time_res res = {
4576 .lr_fsinfo = fsinfo,
4577 };
4578 struct nfs4_get_lease_time_data data = {
4579 .args = &args,
4580 .res = &res,
4581 .clp = clp,
4582 };
4583 struct rpc_message msg = {
4584 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4585 .rpc_argp = &args,
4586 .rpc_resp = &res,
4587 };
4588 struct rpc_task_setup task_setup = {
4589 .rpc_client = clp->cl_rpcclient,
4590 .rpc_message = &msg,
4591 .callback_ops = &nfs4_get_lease_time_ops,
4592 .callback_data = &data
4593 };
4594 int status;
4595
4596 res.lr_seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
4597 dprintk("--> %s\n", __func__);
4598 task = rpc_run_task(&task_setup);
4599
4600 if (IS_ERR(task))
4601 status = PTR_ERR(task);
4602 else {
4603 status = task->tk_status;
4604 rpc_put_task(task);
4605 }
4606 dprintk("<-- %s return %d\n", __func__, status);
4607
4608 return status;
4609}
4610
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004611/*
4612 * Reset a slot table
4613 */
4614static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, int max_slots,
4615 int old_max_slots, int ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004616{
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004617 int i;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004618 int ret = 0;
4619
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004620 dprintk("--> %s: max_reqs=%u, tbl %p\n", __func__, max_slots, tbl);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004621
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004622 /*
4623 * Until we have dynamic slot table adjustment, insist
4624 * upon the same slot table size
4625 */
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004626 if (max_slots != old_max_slots) {
4627 dprintk("%s reset slot table does't match old\n",
4628 __func__);
4629 ret = -EINVAL; /*XXX NFS4ERR_REQ_TOO_BIG ? */
4630 goto out;
4631 }
4632 spin_lock(&tbl->slot_tbl_lock);
4633 for (i = 0; i < max_slots; ++i)
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004634 tbl->slots[i].seq_nr = ivalue;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004635 spin_unlock(&tbl->slot_tbl_lock);
4636 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4637 tbl, tbl->slots, tbl->max_slots);
4638out:
4639 dprintk("<-- %s: return %d\n", __func__, ret);
4640 return ret;
4641}
4642
Andy Adamsonfc931582009-04-01 09:22:31 -04004643/*
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004644 * Reset the forechannel and backchannel slot tables
4645 */
4646static int nfs4_reset_slot_tables(struct nfs4_session *session)
4647{
4648 int status;
4649
4650 status = nfs4_reset_slot_table(&session->fc_slot_table,
4651 session->fc_attrs.max_reqs,
4652 session->fc_slot_table.max_slots,
4653 1);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004654 if (status)
4655 return status;
4656
4657 status = nfs4_reset_slot_table(&session->bc_slot_table,
4658 session->bc_attrs.max_reqs,
4659 session->bc_slot_table.max_slots,
4660 0);
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004661 return status;
4662}
4663
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004664/* Destroy the slot table */
4665static void nfs4_destroy_slot_tables(struct nfs4_session *session)
4666{
4667 if (session->fc_slot_table.slots != NULL) {
4668 kfree(session->fc_slot_table.slots);
4669 session->fc_slot_table.slots = NULL;
4670 }
4671 if (session->bc_slot_table.slots != NULL) {
4672 kfree(session->bc_slot_table.slots);
4673 session->bc_slot_table.slots = NULL;
4674 }
4675 return;
4676}
4677
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004678/*
Andy Adamsonfc931582009-04-01 09:22:31 -04004679 * Initialize slot table
4680 */
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004681static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
4682 int max_slots, int ivalue)
Andy Adamsonfc931582009-04-01 09:22:31 -04004683{
Andy Adamsonfc931582009-04-01 09:22:31 -04004684 struct nfs4_slot *slot;
4685 int ret = -ENOMEM;
4686
4687 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
4688
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004689 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
Andy Adamsonfc931582009-04-01 09:22:31 -04004690
4691 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_KERNEL);
4692 if (!slot)
4693 goto out;
Andy Adamsonfc931582009-04-01 09:22:31 -04004694 ret = 0;
4695
4696 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonfc931582009-04-01 09:22:31 -04004697 tbl->max_slots = max_slots;
4698 tbl->slots = slot;
4699 tbl->highest_used_slotid = -1; /* no slot is currently used */
4700 spin_unlock(&tbl->slot_tbl_lock);
4701 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4702 tbl, tbl->slots, tbl->max_slots);
4703out:
4704 dprintk("<-- %s: return %d\n", __func__, ret);
4705 return ret;
Andy Adamsonfc931582009-04-01 09:22:31 -04004706}
4707
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004708/*
4709 * Initialize the forechannel and backchannel tables
4710 */
4711static int nfs4_init_slot_tables(struct nfs4_session *session)
4712{
Trond Myklebustf26468f2009-12-05 19:32:11 -05004713 struct nfs4_slot_table *tbl;
4714 int status = 0;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004715
Trond Myklebustf26468f2009-12-05 19:32:11 -05004716 tbl = &session->fc_slot_table;
4717 if (tbl->slots == NULL) {
4718 status = nfs4_init_slot_table(tbl,
4719 session->fc_attrs.max_reqs, 1);
4720 if (status)
4721 return status;
4722 }
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004723
Trond Myklebustf26468f2009-12-05 19:32:11 -05004724 tbl = &session->bc_slot_table;
4725 if (tbl->slots == NULL) {
4726 status = nfs4_init_slot_table(tbl,
4727 session->bc_attrs.max_reqs, 0);
4728 if (status)
4729 nfs4_destroy_slot_tables(session);
4730 }
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004731
4732 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04004733}
4734
4735struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
4736{
4737 struct nfs4_session *session;
4738 struct nfs4_slot_table *tbl;
4739
4740 session = kzalloc(sizeof(struct nfs4_session), GFP_KERNEL);
4741 if (!session)
4742 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04004743
Andy Adamson76db6d92009-04-01 09:22:38 -04004744 /*
4745 * The create session reply races with the server back
4746 * channel probe. Mark the client NFS_CS_SESSION_INITING
4747 * so that the client back channel can find the
4748 * nfs_client struct
4749 */
4750 clp->cl_cons_state = NFS_CS_SESSION_INITING;
Andy Adamsonea028ac2009-12-04 15:55:38 -05004751 init_completion(&session->complete);
Andy Adamson76db6d92009-04-01 09:22:38 -04004752
Andy Adamson557134a2009-04-01 09:21:53 -04004753 tbl = &session->fc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05004754 tbl->highest_used_slotid = -1;
Andy Adamson557134a2009-04-01 09:21:53 -04004755 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004756 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004757
4758 tbl = &session->bc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05004759 tbl->highest_used_slotid = -1;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004760 spin_lock_init(&tbl->slot_tbl_lock);
4761 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
4762
Andy Adamson557134a2009-04-01 09:21:53 -04004763 session->clp = clp;
4764 return session;
4765}
4766
4767void nfs4_destroy_session(struct nfs4_session *session)
4768{
Andy Adamson5a0ffe52009-04-01 09:23:18 -04004769 nfs4_proc_destroy_session(session);
4770 dprintk("%s Destroy backchannel for xprt %p\n",
4771 __func__, session->clp->cl_rpcclient->cl_xprt);
4772 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
4773 NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004774 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04004775 kfree(session);
4776}
4777
Andy Adamsonfc931582009-04-01 09:22:31 -04004778/*
4779 * Initialize the values to be used by the client in CREATE_SESSION
4780 * If nfs4_init_session set the fore channel request and response sizes,
4781 * use them.
4782 *
4783 * Set the back channel max_resp_sz_cached to zero to force the client to
4784 * always set csa_cachethis to FALSE because the current implementation
4785 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
4786 */
4787static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
4788{
4789 struct nfs4_session *session = args->client->cl_session;
4790 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
4791 mxresp_sz = session->fc_attrs.max_resp_sz;
4792
4793 if (mxrqst_sz == 0)
4794 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
4795 if (mxresp_sz == 0)
4796 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
4797 /* Fore channel attributes */
4798 args->fc_attrs.headerpadsz = 0;
4799 args->fc_attrs.max_rqst_sz = mxrqst_sz;
4800 args->fc_attrs.max_resp_sz = mxresp_sz;
4801 args->fc_attrs.max_resp_sz_cached = mxresp_sz;
4802 args->fc_attrs.max_ops = NFS4_MAX_OPS;
4803 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
4804
4805 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
4806 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4807 __func__,
4808 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
4809 args->fc_attrs.max_resp_sz_cached, args->fc_attrs.max_ops,
4810 args->fc_attrs.max_reqs);
4811
4812 /* Back channel attributes */
4813 args->bc_attrs.headerpadsz = 0;
4814 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
4815 args->bc_attrs.max_resp_sz = PAGE_SIZE;
4816 args->bc_attrs.max_resp_sz_cached = 0;
4817 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
4818 args->bc_attrs.max_reqs = 1;
4819
4820 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
4821 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4822 __func__,
4823 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
4824 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
4825 args->bc_attrs.max_reqs);
4826}
4827
Andy Adamson8d353012009-04-01 09:22:32 -04004828static int _verify_channel_attr(char *chan, char *attr_name, u32 sent, u32 rcvd)
4829{
4830 if (rcvd <= sent)
4831 return 0;
4832 printk(KERN_WARNING "%s: Session INVALID: %s channel %s increased. "
4833 "sent=%u rcvd=%u\n", __func__, chan, attr_name, sent, rcvd);
4834 return -EINVAL;
4835}
4836
4837#define _verify_fore_channel_attr(_name_) \
4838 _verify_channel_attr("fore", #_name_, \
4839 args->fc_attrs._name_, \
4840 session->fc_attrs._name_)
4841
4842#define _verify_back_channel_attr(_name_) \
4843 _verify_channel_attr("back", #_name_, \
4844 args->bc_attrs._name_, \
4845 session->bc_attrs._name_)
4846
4847/*
4848 * The server is not allowed to increase the fore channel header pad size,
4849 * maximum response size, or maximum number of operations.
4850 *
4851 * The back channel attributes are only negotiatied down: We send what the
4852 * (back channel) server insists upon.
4853 */
4854static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
4855 struct nfs4_session *session)
4856{
4857 int ret = 0;
4858
4859 ret |= _verify_fore_channel_attr(headerpadsz);
4860 ret |= _verify_fore_channel_attr(max_resp_sz);
4861 ret |= _verify_fore_channel_attr(max_ops);
4862
4863 ret |= _verify_back_channel_attr(headerpadsz);
4864 ret |= _verify_back_channel_attr(max_rqst_sz);
4865 ret |= _verify_back_channel_attr(max_resp_sz);
4866 ret |= _verify_back_channel_attr(max_resp_sz_cached);
4867 ret |= _verify_back_channel_attr(max_ops);
4868 ret |= _verify_back_channel_attr(max_reqs);
4869
4870 return ret;
4871}
4872
Andy Adamsonfc931582009-04-01 09:22:31 -04004873static int _nfs4_proc_create_session(struct nfs_client *clp)
4874{
4875 struct nfs4_session *session = clp->cl_session;
4876 struct nfs41_create_session_args args = {
4877 .client = clp,
4878 .cb_program = NFS4_CALLBACK,
4879 };
4880 struct nfs41_create_session_res res = {
4881 .client = clp,
4882 };
4883 struct rpc_message msg = {
4884 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
4885 .rpc_argp = &args,
4886 .rpc_resp = &res,
4887 };
4888 int status;
4889
4890 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04004891 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04004892
4893 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4894
Andy Adamson8d353012009-04-01 09:22:32 -04004895 if (!status)
4896 /* Verify the session's negotiated channel_attrs values */
4897 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04004898 if (!status) {
4899 /* Increment the clientid slot sequence id */
4900 clp->cl_seqid++;
4901 }
4902
4903 return status;
4904}
4905
4906/*
4907 * Issues a CREATE_SESSION operation to the server.
4908 * It is the responsibility of the caller to verify the session is
4909 * expired before calling this routine.
4910 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05004911int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04004912{
4913 int status;
4914 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04004915 struct nfs4_session *session = clp->cl_session;
4916
4917 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
4918
4919 status = _nfs4_proc_create_session(clp);
4920 if (status)
4921 goto out;
4922
Trond Myklebustf26468f2009-12-05 19:32:11 -05004923 /* Init and reset the fore channel */
4924 status = nfs4_init_slot_tables(session);
4925 dprintk("slot table initialization returned %d\n", status);
4926 if (status)
4927 goto out;
4928 status = nfs4_reset_slot_tables(session);
4929 dprintk("slot table reset returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04004930 if (status)
4931 goto out;
4932
4933 ptr = (unsigned *)&session->sess_id.data[0];
4934 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
4935 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04004936out:
4937 dprintk("<-- %s\n", __func__);
4938 return status;
4939}
4940
Andy Adamson0f3e66c2009-04-01 09:22:34 -04004941/*
4942 * Issue the over-the-wire RPC DESTROY_SESSION.
4943 * The caller must serialize access to this routine.
4944 */
4945int nfs4_proc_destroy_session(struct nfs4_session *session)
4946{
4947 int status = 0;
4948 struct rpc_message msg;
4949
4950 dprintk("--> nfs4_proc_destroy_session\n");
4951
4952 /* session is still being setup */
4953 if (session->clp->cl_cons_state != NFS_CS_READY)
4954 return status;
4955
4956 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
4957 msg.rpc_argp = session;
4958 msg.rpc_resp = NULL;
4959 msg.rpc_cred = NULL;
4960 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4961
4962 if (status)
4963 printk(KERN_WARNING
4964 "Got error %d from the server on DESTROY_SESSION. "
4965 "Session has been destroyed regardless...\n", status);
4966
4967 dprintk("<-- nfs4_proc_destroy_session\n");
4968 return status;
4969}
4970
Trond Myklebustfccba802009-07-21 16:48:07 -04004971int nfs4_init_session(struct nfs_server *server)
4972{
4973 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05004974 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05004975 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04004976 int ret;
4977
4978 if (!nfs4_has_session(clp))
4979 return 0;
4980
Andy Adamson68bf05e2009-12-15 12:55:02 -05004981 rsize = server->rsize;
4982 if (rsize == 0)
4983 rsize = NFS_MAX_FILE_IO_SIZE;
4984 wsize = server->wsize;
4985 if (wsize == 0)
4986 wsize = NFS_MAX_FILE_IO_SIZE;
4987
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05004988 session = clp->cl_session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05004989 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
4990 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05004991
Trond Myklebustfccba802009-07-21 16:48:07 -04004992 ret = nfs4_recover_expired_lease(server);
4993 if (!ret)
4994 ret = nfs4_check_client_ready(clp);
4995 return ret;
4996}
4997
Andy Adamsonfc01cea2009-04-01 09:22:36 -04004998/*
4999 * Renew the cl_session lease.
5000 */
5001static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5002{
5003 struct nfs4_sequence_args args;
5004 struct nfs4_sequence_res res;
5005
5006 struct rpc_message msg = {
5007 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5008 .rpc_argp = &args,
5009 .rpc_resp = &res,
5010 .rpc_cred = cred,
5011 };
5012
5013 args.sa_cache_this = 0;
5014
5015 return nfs4_call_sync_sequence(clp, clp->cl_rpcclient, &msg, &args,
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005016 &res, args.sa_cache_this, 1);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005017}
5018
5019void nfs41_sequence_call_done(struct rpc_task *task, void *data)
5020{
5021 struct nfs_client *clp = (struct nfs_client *)data;
5022
5023 nfs41_sequence_done(clp, task->tk_msg.rpc_resp, task->tk_status);
5024
5025 if (task->tk_status < 0) {
5026 dprintk("%s ERROR %d\n", __func__, task->tk_status);
5027
5028 if (_nfs4_async_handle_error(task, NULL, clp, NULL)
5029 == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05005030 nfs_restart_rpc(task, clp);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005031 return;
5032 }
5033 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005034 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
5035
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005036 kfree(task->tk_msg.rpc_argp);
5037 kfree(task->tk_msg.rpc_resp);
5038
5039 dprintk("<-- %s\n", __func__);
5040}
5041
5042static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5043{
5044 struct nfs_client *clp;
5045 struct nfs4_sequence_args *args;
5046 struct nfs4_sequence_res *res;
5047
5048 clp = (struct nfs_client *)data;
5049 args = task->tk_msg.rpc_argp;
5050 res = task->tk_msg.rpc_resp;
5051
5052 if (nfs4_setup_sequence(clp, args, res, 0, task))
5053 return;
5054 rpc_call_start(task);
5055}
5056
5057static const struct rpc_call_ops nfs41_sequence_ops = {
5058 .rpc_call_done = nfs41_sequence_call_done,
5059 .rpc_call_prepare = nfs41_sequence_prepare,
5060};
5061
5062static int nfs41_proc_async_sequence(struct nfs_client *clp,
5063 struct rpc_cred *cred)
5064{
5065 struct nfs4_sequence_args *args;
5066 struct nfs4_sequence_res *res;
5067 struct rpc_message msg = {
5068 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5069 .rpc_cred = cred,
5070 };
5071
5072 args = kzalloc(sizeof(*args), GFP_KERNEL);
5073 if (!args)
5074 return -ENOMEM;
5075 res = kzalloc(sizeof(*res), GFP_KERNEL);
5076 if (!res) {
5077 kfree(args);
5078 return -ENOMEM;
5079 }
5080 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
5081 msg.rpc_argp = args;
5082 msg.rpc_resp = res;
5083
5084 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
5085 &nfs41_sequence_ops, (void *)clp);
5086}
5087
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005088struct nfs4_reclaim_complete_data {
5089 struct nfs_client *clp;
5090 struct nfs41_reclaim_complete_args arg;
5091 struct nfs41_reclaim_complete_res res;
5092};
5093
5094static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5095{
5096 struct nfs4_reclaim_complete_data *calldata = data;
5097
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005098 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005099 if (nfs4_setup_sequence(calldata->clp, &calldata->arg.seq_args,
5100 &calldata->res.seq_res, 0, task))
5101 return;
5102
5103 rpc_call_start(task);
5104}
5105
5106static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5107{
5108 struct nfs4_reclaim_complete_data *calldata = data;
5109 struct nfs_client *clp = calldata->clp;
5110 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5111
5112 dprintk("--> %s\n", __func__);
5113 nfs41_sequence_done(clp, res, task->tk_status);
5114 switch (task->tk_status) {
5115 case 0:
5116 case -NFS4ERR_COMPLETE_ALREADY:
5117 break;
5118 case -NFS4ERR_BADSESSION:
5119 case -NFS4ERR_DEADSESSION:
5120 /*
5121 * Handle the session error, but do not retry the operation, as
5122 * we have no way of telling whether the clientid had to be
5123 * reset before we got our reply. If reset, a new wave of
5124 * reclaim operations will follow, containing their own reclaim
5125 * complete. We don't want our retry to get on the way of
5126 * recovery by incorrectly indicating to the server that we're
5127 * done reclaiming state since the process had to be restarted.
5128 */
5129 _nfs4_async_handle_error(task, NULL, clp, NULL);
5130 break;
5131 default:
5132 if (_nfs4_async_handle_error(
5133 task, NULL, clp, NULL) == -EAGAIN) {
5134 rpc_restart_call_prepare(task);
5135 return;
5136 }
5137 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005138
5139 dprintk("<-- %s\n", __func__);
5140}
5141
5142static void nfs4_free_reclaim_complete_data(void *data)
5143{
5144 struct nfs4_reclaim_complete_data *calldata = data;
5145
5146 kfree(calldata);
5147}
5148
5149static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5150 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5151 .rpc_call_done = nfs4_reclaim_complete_done,
5152 .rpc_release = nfs4_free_reclaim_complete_data,
5153};
5154
5155/*
5156 * Issue a global reclaim complete.
5157 */
5158static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5159{
5160 struct nfs4_reclaim_complete_data *calldata;
5161 struct rpc_task *task;
5162 struct rpc_message msg = {
5163 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5164 };
5165 struct rpc_task_setup task_setup_data = {
5166 .rpc_client = clp->cl_rpcclient,
5167 .rpc_message = &msg,
5168 .callback_ops = &nfs4_reclaim_complete_call_ops,
5169 .flags = RPC_TASK_ASYNC,
5170 };
5171 int status = -ENOMEM;
5172
5173 dprintk("--> %s\n", __func__);
5174 calldata = kzalloc(sizeof(*calldata), GFP_KERNEL);
5175 if (calldata == NULL)
5176 goto out;
5177 calldata->clp = clp;
5178 calldata->arg.one_fs = 0;
5179 calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
5180
5181 msg.rpc_argp = &calldata->arg;
5182 msg.rpc_resp = &calldata->res;
5183 task_setup_data.callback_data = calldata;
5184 task = rpc_run_task(&task_setup_data);
5185 if (IS_ERR(task))
5186 status = PTR_ERR(task);
5187 rpc_put_task(task);
5188out:
5189 dprintk("<-- %s status=%d\n", __func__, status);
5190 return status;
5191}
Andy Adamson557134a2009-04-01 09:21:53 -04005192#endif /* CONFIG_NFS_V4_1 */
5193
Andy Adamson591d71c2009-04-01 09:22:47 -04005194struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005195 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005196 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005197 .recover_open = nfs4_open_reclaim,
5198 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04005199 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005200 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005201};
5202
Andy Adamson591d71c2009-04-01 09:22:47 -04005203#if defined(CONFIG_NFS_V4_1)
5204struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
5205 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5206 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5207 .recover_open = nfs4_open_reclaim,
5208 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05005209 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005210 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005211 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04005212};
5213#endif /* CONFIG_NFS_V4_1 */
5214
5215struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005216 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005217 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218 .recover_open = nfs4_open_expired,
5219 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04005220 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005221 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222};
5223
Andy Adamson591d71c2009-04-01 09:22:47 -04005224#if defined(CONFIG_NFS_V4_1)
5225struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
5226 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5227 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5228 .recover_open = nfs4_open_expired,
5229 .recover_lock = nfs4_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05005230 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005231 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04005232};
5233#endif /* CONFIG_NFS_V4_1 */
5234
Benny Halevy29fba382009-04-01 09:22:44 -04005235struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
5236 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04005237 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005238 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04005239};
5240
5241#if defined(CONFIG_NFS_V4_1)
5242struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
5243 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04005244 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005245 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04005246};
5247#endif
5248
5249/*
5250 * Per minor version reboot and network partition recovery ops
5251 */
5252
Andy Adamson591d71c2009-04-01 09:22:47 -04005253struct nfs4_state_recovery_ops *nfs4_reboot_recovery_ops[] = {
5254 &nfs40_reboot_recovery_ops,
5255#if defined(CONFIG_NFS_V4_1)
5256 &nfs41_reboot_recovery_ops,
5257#endif
5258};
5259
5260struct nfs4_state_recovery_ops *nfs4_nograce_recovery_ops[] = {
5261 &nfs40_nograce_recovery_ops,
5262#if defined(CONFIG_NFS_V4_1)
5263 &nfs41_nograce_recovery_ops,
5264#endif
5265};
5266
Benny Halevy29fba382009-04-01 09:22:44 -04005267struct nfs4_state_maintenance_ops *nfs4_state_renewal_ops[] = {
5268 &nfs40_state_renewal_ops,
5269#if defined(CONFIG_NFS_V4_1)
5270 &nfs41_state_renewal_ops,
5271#endif
5272};
5273
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08005274static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005275 .permission = nfs_permission,
5276 .getattr = nfs_getattr,
5277 .setattr = nfs_setattr,
5278 .getxattr = nfs4_getxattr,
5279 .setxattr = nfs4_setxattr,
5280 .listxattr = nfs4_listxattr,
5281};
5282
David Howells509de812006-08-22 20:06:11 -04005283const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284 .version = 4, /* protocol version */
5285 .dentry_ops = &nfs4_dentry_operations,
5286 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005287 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005288 .getroot = nfs4_proc_get_root,
5289 .getattr = nfs4_proc_getattr,
5290 .setattr = nfs4_proc_setattr,
David Howells2b3de442006-08-22 20:06:09 -04005291 .lookupfh = nfs4_proc_lookupfh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292 .lookup = nfs4_proc_lookup,
5293 .access = nfs4_proc_access,
5294 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295 .create = nfs4_proc_create,
5296 .remove = nfs4_proc_remove,
5297 .unlink_setup = nfs4_proc_unlink_setup,
5298 .unlink_done = nfs4_proc_unlink_done,
5299 .rename = nfs4_proc_rename,
5300 .link = nfs4_proc_link,
5301 .symlink = nfs4_proc_symlink,
5302 .mkdir = nfs4_proc_mkdir,
5303 .rmdir = nfs4_proc_remove,
5304 .readdir = nfs4_proc_readdir,
5305 .mknod = nfs4_proc_mknod,
5306 .statfs = nfs4_proc_statfs,
5307 .fsinfo = nfs4_proc_fsinfo,
5308 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04005309 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005310 .decode_dirent = nfs4_decode_dirent,
5311 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05005312 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005313 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005314 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005315 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005316 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005317 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005318 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04005319 .close_context = nfs4_close_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320};
5321
5322/*
5323 * Local variables:
5324 * c-basic-offset: 8
5325 * End:
5326 */