blob: 02513da9a01684ac9a9f85665fad9e59fcc5cf89 [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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050068static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Trond Myklebust99367812007-07-17 21:52:41 -040069static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
70static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Linus Torvalds1da177e2005-04-16 15:20:36 -070072/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050073static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
Trond Myklebust52567b02009-10-23 14:46:42 -040075 if (err >= -1000)
76 return err;
77 switch (err) {
78 case -NFS4ERR_RESOURCE:
79 return -EREMOTEIO;
80 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -070082 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -040083 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 }
Trond Myklebust52567b02009-10-23 14:46:42 -040085 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086}
87
88/*
89 * This is our standard bitmap for GETATTR requests.
90 */
91const u32 nfs4_fattr_bitmap[2] = {
92 FATTR4_WORD0_TYPE
93 | FATTR4_WORD0_CHANGE
94 | FATTR4_WORD0_SIZE
95 | FATTR4_WORD0_FSID
96 | FATTR4_WORD0_FILEID,
97 FATTR4_WORD1_MODE
98 | FATTR4_WORD1_NUMLINKS
99 | FATTR4_WORD1_OWNER
100 | FATTR4_WORD1_OWNER_GROUP
101 | FATTR4_WORD1_RAWDEV
102 | FATTR4_WORD1_SPACE_USED
103 | FATTR4_WORD1_TIME_ACCESS
104 | FATTR4_WORD1_TIME_METADATA
105 | FATTR4_WORD1_TIME_MODIFY
106};
107
108const u32 nfs4_statfs_bitmap[2] = {
109 FATTR4_WORD0_FILES_AVAIL
110 | FATTR4_WORD0_FILES_FREE
111 | FATTR4_WORD0_FILES_TOTAL,
112 FATTR4_WORD1_SPACE_AVAIL
113 | FATTR4_WORD1_SPACE_FREE
114 | FATTR4_WORD1_SPACE_TOTAL
115};
116
Trond Myklebust4ce79712005-06-22 17:16:21 +0000117const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 FATTR4_WORD0_MAXLINK
119 | FATTR4_WORD0_MAXNAME,
120 0
121};
122
123const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
124 | FATTR4_WORD0_MAXREAD
125 | FATTR4_WORD0_MAXWRITE
126 | FATTR4_WORD0_LEASE_TIME,
127 0
128};
129
Manoj Naik830b8e32006-06-09 09:34:25 -0400130const u32 nfs4_fs_locations_bitmap[2] = {
131 FATTR4_WORD0_TYPE
132 | FATTR4_WORD0_CHANGE
133 | FATTR4_WORD0_SIZE
134 | FATTR4_WORD0_FSID
135 | FATTR4_WORD0_FILEID
136 | FATTR4_WORD0_FS_LOCATIONS,
137 FATTR4_WORD1_MODE
138 | FATTR4_WORD1_NUMLINKS
139 | FATTR4_WORD1_OWNER
140 | FATTR4_WORD1_OWNER_GROUP
141 | FATTR4_WORD1_RAWDEV
142 | FATTR4_WORD1_SPACE_USED
143 | FATTR4_WORD1_TIME_ACCESS
144 | FATTR4_WORD1_TIME_METADATA
145 | FATTR4_WORD1_TIME_MODIFY
146 | FATTR4_WORD1_MOUNTED_ON_FILEID
147};
148
Al Virobc4785c2006-10-19 23:28:51 -0700149static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 struct nfs4_readdir_arg *readdir)
151{
Al Viro0dbb4c62006-10-19 23:28:49 -0700152 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154 BUG_ON(readdir->count < 80);
155 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000156 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
158 return;
159 }
160
161 readdir->cookie = 0;
162 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
163 if (cookie == 2)
164 return;
165
166 /*
167 * NFSv4 servers do not return entries for '.' and '..'
168 * Therefore, we fake these entries here. We let '.'
169 * have cookie 0 and '..' have cookie 1. Note that
170 * when talking to the server, we always send cookie 0
171 * instead of 1 or 2.
172 */
Al Viro0dbb4c62006-10-19 23:28:49 -0700173 start = p = kmap_atomic(*readdir->pages, KM_USER0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 if (cookie == 0) {
176 *p++ = xdr_one; /* next */
177 *p++ = xdr_zero; /* cookie, first word */
178 *p++ = xdr_one; /* cookie, second word */
179 *p++ = xdr_one; /* entry len */
180 memcpy(p, ".\0\0\0", 4); /* entry */
181 p++;
182 *p++ = xdr_one; /* bitmap length */
183 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
184 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400185 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 }
187
188 *p++ = xdr_one; /* next */
189 *p++ = xdr_zero; /* cookie, first word */
190 *p++ = xdr_two; /* cookie, second word */
191 *p++ = xdr_two; /* entry len */
192 memcpy(p, "..\0\0", 4); /* entry */
193 p++;
194 *p++ = xdr_one; /* bitmap length */
195 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
196 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400197 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199 readdir->pgbase = (char *)p - (char *)start;
200 readdir->count -= readdir->pgbase;
201 kunmap_atomic(start, KM_USER0);
202}
203
Trond Myklebust65de8722008-12-23 15:21:44 -0500204static int nfs4_wait_clnt_recover(struct nfs_client *clp)
205{
206 int res;
207
208 might_sleep();
209
Trond Myklebuste005e802008-12-23 15:21:48 -0500210 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400211 nfs_wait_bit_killable, TASK_KILLABLE);
Trond Myklebust65de8722008-12-23 15:21:44 -0500212 return res;
213}
214
215static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
216{
217 int res = 0;
218
219 might_sleep();
220
221 if (*timeout <= 0)
222 *timeout = NFS4_POLL_RETRY_MIN;
223 if (*timeout > NFS4_POLL_RETRY_MAX)
224 *timeout = NFS4_POLL_RETRY_MAX;
225 schedule_timeout_killable(*timeout);
226 if (fatal_signal_pending(current))
227 res = -ERESTARTSYS;
228 *timeout <<= 1;
229 return res;
230}
231
232/* This is the error handling routine for processes that are allowed
233 * to sleep.
234 */
235static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
236{
237 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500238 struct nfs4_state *state = exception->state;
Trond Myklebust65de8722008-12-23 15:21:44 -0500239 int ret = errorcode;
240
241 exception->retry = 0;
242 switch(errorcode) {
243 case 0:
244 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500245 case -NFS4ERR_ADMIN_REVOKED:
246 case -NFS4ERR_BAD_STATEID:
247 case -NFS4ERR_OPENMODE:
248 if (state == NULL)
249 break;
250 nfs4_state_mark_reclaim_nograce(clp, state);
Trond Myklebust65de8722008-12-23 15:21:44 -0500251 case -NFS4ERR_STALE_CLIENTID:
252 case -NFS4ERR_STALE_STATEID:
253 case -NFS4ERR_EXPIRED:
254 nfs4_schedule_state_recovery(clp);
255 ret = nfs4_wait_clnt_recover(clp);
256 if (ret == 0)
257 exception->retry = 1;
Andy Adamson4745e312009-04-01 09:22:42 -0400258#if !defined(CONFIG_NFS_V4_1)
Trond Myklebust65de8722008-12-23 15:21:44 -0500259 break;
Andy Adamson4745e312009-04-01 09:22:42 -0400260#else /* !defined(CONFIG_NFS_V4_1) */
261 if (!nfs4_has_session(server->nfs_client))
262 break;
263 /* FALLTHROUGH */
264 case -NFS4ERR_BADSESSION:
265 case -NFS4ERR_BADSLOT:
266 case -NFS4ERR_BAD_HIGH_SLOT:
267 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
268 case -NFS4ERR_DEADSESSION:
269 case -NFS4ERR_SEQ_FALSE_RETRY:
270 case -NFS4ERR_SEQ_MISORDERED:
271 dprintk("%s ERROR: %d Reset session\n", __func__,
272 errorcode);
Andy Adamson0b9e2d42009-12-04 16:02:14 -0500273 nfs4_schedule_state_recovery(clp);
Andy Adamson4745e312009-04-01 09:22:42 -0400274 exception->retry = 1;
Andy Adamsonb9179232009-12-04 15:55:32 -0500275 break;
Andy Adamson4745e312009-04-01 09:22:42 -0400276#endif /* !defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500277 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500278 if (exception->timeout > HZ) {
279 /* We have retried a decent amount, time to
280 * fail
281 */
282 ret = -EBUSY;
283 break;
284 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500285 case -NFS4ERR_GRACE:
286 case -NFS4ERR_DELAY:
287 ret = nfs4_delay(server->client, &exception->timeout);
288 if (ret != 0)
289 break;
290 case -NFS4ERR_OLD_STATEID:
291 exception->retry = 1;
292 }
293 /* We failed to handle the error */
294 return nfs4_map_errors(ret);
295}
296
297
Trond Myklebust26e976a2006-01-03 09:55:21 +0100298static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
David Howells7539bba2006-08-22 20:06:09 -0400300 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 spin_lock(&clp->cl_lock);
302 if (time_before(clp->cl_last_renewal,timestamp))
303 clp->cl_last_renewal = timestamp;
304 spin_unlock(&clp->cl_lock);
305}
306
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400307#if defined(CONFIG_NFS_V4_1)
308
Benny Halevy510b8172009-04-01 09:22:14 -0400309/*
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400310 * nfs4_free_slot - free a slot and efficiently update slot table.
311 *
312 * freeing a slot is trivially done by clearing its respective bit
313 * in the bitmap.
314 * If the freed slotid equals highest_used_slotid we want to update it
315 * so that the server would be able to size down the slot table if needed,
316 * otherwise we know that the highest_used_slotid is still in use.
317 * When updating highest_used_slotid there may be "holes" in the bitmap
318 * so we need to scan down from highest_used_slotid to 0 looking for the now
319 * highest slotid in use.
320 * If none found, highest_used_slotid is set to -1.
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500321 *
322 * Must be called while holding tbl->slot_tbl_lock
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400323 */
324static void
325nfs4_free_slot(struct nfs4_slot_table *tbl, u8 free_slotid)
326{
327 int slotid = free_slotid;
328
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400329 /* clear used bit in bitmap */
330 __clear_bit(slotid, tbl->used_slots);
331
332 /* update highest_used_slotid when it is freed */
333 if (slotid == tbl->highest_used_slotid) {
334 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500335 if (slotid < tbl->max_slots)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400336 tbl->highest_used_slotid = slotid;
337 else
338 tbl->highest_used_slotid = -1;
339 }
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400340 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
341 free_slotid, tbl->highest_used_slotid);
342}
343
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800344/*
345 * Signal state manager thread if session is drained
346 */
347static void nfs41_check_drain_session_complete(struct nfs4_session *ses)
348{
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800349 struct rpc_task *task;
350
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800351 if (!test_bit(NFS4CLNT_SESSION_DRAINING, &ses->clp->cl_state)) {
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800352 task = rpc_wake_up_next(&ses->fc_slot_table.slot_tbl_waitq);
353 if (task)
354 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800355 return;
356 }
357
358 if (ses->fc_slot_table.highest_used_slotid != -1)
359 return;
360
361 dprintk("%s COMPLETE: Session Drained\n", __func__);
362 complete(&ses->complete);
363}
364
Trond Myklebustd61e6122009-12-05 19:32:19 -0500365static void nfs41_sequence_free_slot(const struct nfs_client *clp,
Andy Adamson13615872009-04-01 09:22:17 -0400366 struct nfs4_sequence_res *res)
367{
368 struct nfs4_slot_table *tbl;
369
Andy Adamson13615872009-04-01 09:22:17 -0400370 tbl = &clp->cl_session->fc_slot_table;
371 if (res->sr_slotid == NFS4_MAX_SLOT_TABLE) {
Andy Adamson13615872009-04-01 09:22:17 -0400372 /* just wake up the next guy waiting since
373 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500374 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500375 return;
Andy Adamson13615872009-04-01 09:22:17 -0400376 }
Andy Adamsonea028ac2009-12-04 15:55:38 -0500377
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500378 spin_lock(&tbl->slot_tbl_lock);
379 nfs4_free_slot(tbl, res->sr_slotid);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800380 nfs41_check_drain_session_complete(clp->cl_session);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500381 spin_unlock(&tbl->slot_tbl_lock);
382 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
Andy Adamson13615872009-04-01 09:22:17 -0400383}
384
Andy Adamsonb0df8062009-04-01 09:22:18 -0400385static void nfs41_sequence_done(struct nfs_client *clp,
386 struct nfs4_sequence_res *res,
387 int rpc_status)
388{
389 unsigned long timestamp;
390 struct nfs4_slot_table *tbl;
391 struct nfs4_slot *slot;
392
393 /*
394 * sr_status remains 1 if an RPC level error occurred. The server
395 * may or may not have processed the sequence operation..
396 * Proceed as if the server received and processed the sequence
397 * operation.
398 */
399 if (res->sr_status == 1)
400 res->sr_status = NFS_OK;
401
402 /* -ERESTARTSYS can result in skipping nfs41_sequence_setup */
403 if (res->sr_slotid == NFS4_MAX_SLOT_TABLE)
404 goto out;
405
Andy Adamson691daf32009-12-04 15:55:39 -0500406 /* Check the SEQUENCE operation status */
Andy Adamsonb0df8062009-04-01 09:22:18 -0400407 if (res->sr_status == 0) {
Andy Adamson691daf32009-12-04 15:55:39 -0500408 tbl = &clp->cl_session->fc_slot_table;
409 slot = tbl->slots + res->sr_slotid;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400410 /* Update the slot's sequence and clientid lease timer */
411 ++slot->seq_nr;
412 timestamp = res->sr_renewal_time;
413 spin_lock(&clp->cl_lock);
414 if (time_before(clp->cl_last_renewal, timestamp))
415 clp->cl_last_renewal = timestamp;
416 spin_unlock(&clp->cl_lock);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500417 /* Check sequence flags */
418 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400419 }
420out:
421 /* The session may be reset by one of the error handlers. */
422 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
423 nfs41_sequence_free_slot(clp, res);
424}
425
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400426/*
Benny Halevy510b8172009-04-01 09:22:14 -0400427 * nfs4_find_slot - efficiently look for a free slot
428 *
429 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
430 * If found, we mark the slot as used, update the highest_used_slotid,
431 * and respectively set up the sequence operation args.
432 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400433 *
434 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400435 */
436static u8
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800437nfs4_find_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400438{
439 int slotid;
440 u8 ret_id = NFS4_MAX_SLOT_TABLE;
441 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
442
Benny Halevy510b8172009-04-01 09:22:14 -0400443 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
444 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
445 tbl->max_slots);
446 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
447 if (slotid >= tbl->max_slots)
448 goto out;
449 __set_bit(slotid, tbl->used_slots);
450 if (slotid > tbl->highest_used_slotid)
451 tbl->highest_used_slotid = slotid;
452 ret_id = slotid;
453out:
454 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
455 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400456 return ret_id;
457}
458
Andy Adamsonce5039c2009-04-01 09:22:13 -0400459static int nfs41_setup_sequence(struct nfs4_session *session,
460 struct nfs4_sequence_args *args,
461 struct nfs4_sequence_res *res,
462 int cache_reply,
463 struct rpc_task *task)
464{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400465 struct nfs4_slot *slot;
466 struct nfs4_slot_table *tbl;
467 u8 slotid;
468
469 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400470 /* slot already allocated? */
471 if (res->sr_slotid != NFS4_MAX_SLOT_TABLE)
472 return 0;
473
474 memset(res, 0, sizeof(*res));
475 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400476 tbl = &session->fc_slot_table;
477
478 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson0b9e2d42009-12-04 16:02:14 -0500479 if (test_bit(NFS4CLNT_SESSION_DRAINING, &session->clp->cl_state)) {
Andy Adamsonea028ac2009-12-04 15:55:38 -0500480 /*
481 * The state manager will wait until the slot table is empty.
482 * Schedule the reset thread
483 */
Andy Adamson05f0d232009-12-04 15:55:37 -0500484 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400485 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500486 dprintk("%s Schedule Session Reset\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500487 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400488 }
489
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800490 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
491 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
492 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
493 spin_unlock(&tbl->slot_tbl_lock);
494 dprintk("%s enforce FIFO order\n", __func__);
495 return -EAGAIN;
496 }
497
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800498 slotid = nfs4_find_slot(tbl);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400499 if (slotid == NFS4_MAX_SLOT_TABLE) {
500 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
501 spin_unlock(&tbl->slot_tbl_lock);
502 dprintk("<-- %s: no free slots\n", __func__);
503 return -EAGAIN;
504 }
505 spin_unlock(&tbl->slot_tbl_lock);
506
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800507 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400508 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400509 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400510 args->sa_slotid = slotid;
511 args->sa_cache_this = cache_reply;
512
513 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
514
Benny Halevy99fe60d2009-04-01 09:22:29 -0400515 res->sr_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400516 res->sr_slotid = slotid;
517 res->sr_renewal_time = jiffies;
518 /*
519 * sr_status is only set in decode_sequence, and so will remain
520 * set to 1 if an rpc level failure occurs.
521 */
522 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400523 return 0;
524}
525
526int nfs4_setup_sequence(struct nfs_client *clp,
527 struct nfs4_sequence_args *args,
528 struct nfs4_sequence_res *res,
529 int cache_reply,
530 struct rpc_task *task)
531{
532 int ret = 0;
533
534 dprintk("--> %s clp %p session %p sr_slotid %d\n",
535 __func__, clp, clp->cl_session, res->sr_slotid);
536
537 if (!nfs4_has_session(clp))
538 goto out;
539 ret = nfs41_setup_sequence(clp->cl_session, args, res, cache_reply,
540 task);
Andy Adamson2628edd2009-12-04 15:55:35 -0500541 if (ret && ret != -EAGAIN) {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400542 /* terminate rpc task */
543 task->tk_status = ret;
544 task->tk_action = NULL;
545 }
546out:
547 dprintk("<-- %s status=%d\n", __func__, ret);
548 return ret;
549}
550
551struct nfs41_call_sync_data {
552 struct nfs_client *clp;
553 struct nfs4_sequence_args *seq_args;
554 struct nfs4_sequence_res *seq_res;
555 int cache_reply;
556};
557
558static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
559{
560 struct nfs41_call_sync_data *data = calldata;
561
562 dprintk("--> %s data->clp->cl_session %p\n", __func__,
563 data->clp->cl_session);
564 if (nfs4_setup_sequence(data->clp, data->seq_args,
565 data->seq_res, data->cache_reply, task))
566 return;
567 rpc_call_start(task);
568}
569
Andy Adamson69ab40c2009-04-01 09:22:19 -0400570static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
571{
572 struct nfs41_call_sync_data *data = calldata;
573
574 nfs41_sequence_done(data->clp, data->seq_res, task->tk_status);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400575}
576
Andy Adamsonce5039c2009-04-01 09:22:13 -0400577struct rpc_call_ops nfs41_call_sync_ops = {
578 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400579 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400580};
581
582static int nfs4_call_sync_sequence(struct nfs_client *clp,
583 struct rpc_clnt *clnt,
584 struct rpc_message *msg,
585 struct nfs4_sequence_args *args,
586 struct nfs4_sequence_res *res,
587 int cache_reply)
588{
589 int ret;
590 struct rpc_task *task;
591 struct nfs41_call_sync_data data = {
592 .clp = clp,
593 .seq_args = args,
594 .seq_res = res,
595 .cache_reply = cache_reply,
596 };
597 struct rpc_task_setup task_setup = {
598 .rpc_client = clnt,
599 .rpc_message = msg,
600 .callback_ops = &nfs41_call_sync_ops,
601 .callback_data = &data
602 };
603
604 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
605 task = rpc_run_task(&task_setup);
606 if (IS_ERR(task))
607 ret = PTR_ERR(task);
608 else {
609 ret = task->tk_status;
610 rpc_put_task(task);
611 }
612 return ret;
613}
614
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400615int _nfs4_call_sync_session(struct nfs_server *server,
616 struct rpc_message *msg,
617 struct nfs4_sequence_args *args,
618 struct nfs4_sequence_res *res,
619 int cache_reply)
620{
Andy Adamsonce5039c2009-04-01 09:22:13 -0400621 return nfs4_call_sync_sequence(server->nfs_client, server->client,
622 msg, args, res, cache_reply);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400623}
624
625#endif /* CONFIG_NFS_V4_1 */
626
627int _nfs4_call_sync(struct nfs_server *server,
628 struct rpc_message *msg,
629 struct nfs4_sequence_args *args,
630 struct nfs4_sequence_res *res,
631 int cache_reply)
632{
Benny Halevyf3752972009-04-01 09:22:04 -0400633 args->sa_session = res->sr_session = NULL;
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400634 return rpc_call_sync(server->client, msg, 0);
635}
636
637#define nfs4_call_sync(server, msg, args, res, cache_reply) \
638 (server)->nfs_client->cl_call_sync((server), (msg), &(args)->seq_args, \
639 &(res)->seq_res, (cache_reply))
640
Andy Adamsonb0df8062009-04-01 09:22:18 -0400641static void nfs4_sequence_done(const struct nfs_server *server,
642 struct nfs4_sequence_res *res, int rpc_status)
643{
644#ifdef CONFIG_NFS_V4_1
645 if (nfs4_has_session(server->nfs_client))
646 nfs41_sequence_done(server->nfs_client, res, rpc_status);
647#endif /* CONFIG_NFS_V4_1 */
648}
649
Trond Myklebust38478b22006-05-25 01:40:57 -0400650static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
Trond Myklebust38478b22006-05-25 01:40:57 -0400652 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Trond Myklebust38478b22006-05-25 01:40:57 -0400654 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400655 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
656 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400657 nfs_force_lookup_revalidate(dir);
Trond Myklebust40d24702007-10-08 09:24:22 -0400658 nfsi->change_attr = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400659 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660}
661
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100662struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400663 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100664 struct nfs_openargs o_arg;
665 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100666 struct nfs_open_confirmargs c_arg;
667 struct nfs_open_confirmres c_res;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100668 struct nfs_fattr f_attr;
669 struct nfs_fattr dir_attr;
Trond Myklebustad389da2007-06-05 12:30:00 -0400670 struct path path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100671 struct dentry *dir;
672 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400673 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100674 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100675 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400676 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100677 int rpc_status;
678 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100679};
680
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400681
682static void nfs4_init_opendata_res(struct nfs4_opendata *p)
683{
684 p->o_res.f_attr = &p->f_attr;
685 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400686 p->o_res.seqid = p->o_arg.seqid;
687 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400688 p->o_res.server = p->o_arg.server;
689 nfs_fattr_init(&p->f_attr);
690 nfs_fattr_init(&p->dir_attr);
Benny Halevy578e4582009-06-18 22:01:23 -0400691 p->o_res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400692}
693
Trond Myklebustad389da2007-06-05 12:30:00 -0400694static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500695 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100696 const struct iattr *attrs)
697{
Trond Myklebustad389da2007-06-05 12:30:00 -0400698 struct dentry *parent = dget_parent(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100699 struct inode *dir = parent->d_inode;
700 struct nfs_server *server = NFS_SERVER(dir);
701 struct nfs4_opendata *p;
702
703 p = kzalloc(sizeof(*p), GFP_KERNEL);
704 if (p == NULL)
705 goto err;
706 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
707 if (p->o_arg.seqid == NULL)
708 goto err_free;
Trond Myklebustad389da2007-06-05 12:30:00 -0400709 p->path.mnt = mntget(path->mnt);
710 p->path.dentry = dget(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100711 p->dir = parent;
712 p->owner = sp;
713 atomic_inc(&sp->so_count);
714 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500715 p->o_arg.open_flags = flags;
716 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400717 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400718 p->o_arg.id = sp->so_owner_id.id;
Trond Myklebustad389da2007-06-05 12:30:00 -0400719 p->o_arg.name = &p->path.dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100720 p->o_arg.server = server;
721 p->o_arg.bitmask = server->attr_bitmask;
722 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100723 if (flags & O_EXCL) {
Alexandros Batsakis4882ef72009-12-05 13:30:21 -0500724 if (nfs4_has_persistent_session(server->nfs_client)) {
725 /* GUARDED */
726 p->o_arg.u.attrs = &p->attrs;
727 memcpy(&p->attrs, attrs, sizeof(p->attrs));
728 } else { /* EXCLUSIVE4_1 */
729 u32 *s = (u32 *) p->o_arg.u.verifier.data;
730 s[0] = jiffies;
731 s[1] = current->pid;
732 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100733 } else if (flags & O_CREAT) {
734 p->o_arg.u.attrs = &p->attrs;
735 memcpy(&p->attrs, attrs, sizeof(p->attrs));
736 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100737 p->c_arg.fh = &p->o_res.fh;
738 p->c_arg.stateid = &p->o_res.stateid;
739 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400740 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400741 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100742 return p;
743err_free:
744 kfree(p);
745err:
746 dput(parent);
747 return NULL;
748}
749
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400750static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100751{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400752 struct nfs4_opendata *p = container_of(kref,
753 struct nfs4_opendata, kref);
754
755 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400756 if (p->state != NULL)
757 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400758 nfs4_put_state_owner(p->owner);
759 dput(p->dir);
Jan Blunck31f31db12008-05-02 13:42:45 -0700760 path_put(&p->path);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400761 kfree(p);
762}
763
764static void nfs4_opendata_put(struct nfs4_opendata *p)
765{
766 if (p != NULL)
767 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100768}
769
Trond Myklebust06f814a2006-01-03 09:55:07 +0100770static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
771{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100772 int ret;
773
Trond Myklebust06f814a2006-01-03 09:55:07 +0100774 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100775 return ret;
776}
777
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500778static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400779{
780 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500781
782 if (open_mode & O_EXCL)
783 goto out;
784 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400785 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500786 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
787 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400788 break;
789 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500790 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
791 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400792 break;
793 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500794 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
795 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400796 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500797out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400798 return ret;
799}
800
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500801static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400802{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500803 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400804 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500805 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400806 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500807 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400808 return 1;
809}
810
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500811static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100812{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500813 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100814 case FMODE_WRITE:
815 state->n_wronly++;
816 break;
817 case FMODE_READ:
818 state->n_rdonly++;
819 break;
820 case FMODE_READ|FMODE_WRITE:
821 state->n_rdwr++;
822 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500823 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100824}
825
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500826static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400827{
828 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
829 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
830 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500831 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400832 case FMODE_READ:
833 set_bit(NFS_O_RDONLY_STATE, &state->flags);
834 break;
835 case FMODE_WRITE:
836 set_bit(NFS_O_WRONLY_STATE, &state->flags);
837 break;
838 case FMODE_READ|FMODE_WRITE:
839 set_bit(NFS_O_RDWR_STATE, &state->flags);
840 }
841}
842
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500843static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400844{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400845 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500846 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400847 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400848}
849
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500850static 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 -0700851{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400852 /*
853 * Protect the call to nfs4_state_set_mode_locked and
854 * serialise the stateid update
855 */
856 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400857 if (deleg_stateid != NULL) {
858 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
859 set_bit(NFS_DELEGATED_STATE, &state->flags);
860 }
861 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500862 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400863 write_sequnlock(&state->seqlock);
864 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500865 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700866 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867}
868
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500869static 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 -0500870{
871 struct nfs_inode *nfsi = NFS_I(state->inode);
872 struct nfs_delegation *deleg_cur;
873 int ret = 0;
874
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500875 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500876
877 rcu_read_lock();
878 deleg_cur = rcu_dereference(nfsi->delegation);
879 if (deleg_cur == NULL)
880 goto no_delegation;
881
882 spin_lock(&deleg_cur->lock);
883 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500884 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500885 goto no_delegation_unlock;
886
887 if (delegation == NULL)
888 delegation = &deleg_cur->stateid;
889 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
890 goto no_delegation_unlock;
891
Trond Myklebustb7391f42008-12-23 15:21:52 -0500892 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500893 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500894 ret = 1;
895no_delegation_unlock:
896 spin_unlock(&deleg_cur->lock);
897no_delegation:
898 rcu_read_unlock();
899
900 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500901 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500902 ret = 1;
903 }
904
905 return ret;
906}
907
908
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500909static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400910{
911 struct nfs_delegation *delegation;
912
913 rcu_read_lock();
914 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500915 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -0400916 rcu_read_unlock();
917 return;
918 }
919 rcu_read_unlock();
920 nfs_inode_return_delegation(inode);
921}
922
Trond Myklebust6ee41262007-07-08 14:11:36 -0400923static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400924{
925 struct nfs4_state *state = opendata->state;
926 struct nfs_inode *nfsi = NFS_I(state->inode);
927 struct nfs_delegation *delegation;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500928 int open_mode = opendata->o_arg.open_flags & O_EXCL;
929 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400930 nfs4_stateid stateid;
931 int ret = -EAGAIN;
932
Trond Myklebustaac00a82007-07-05 19:02:21 -0400933 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500934 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400935 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500936 if (can_open_cached(state, fmode, open_mode)) {
937 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400938 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400939 goto out_return_state;
940 }
941 spin_unlock(&state->owner->so_lock);
942 }
Trond Myklebust34310432008-12-23 15:21:38 -0500943 rcu_read_lock();
944 delegation = rcu_dereference(nfsi->delegation);
945 if (delegation == NULL ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500946 !can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -0500947 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -0400948 break;
Trond Myklebust34310432008-12-23 15:21:38 -0500949 }
Trond Myklebustaac00a82007-07-05 19:02:21 -0400950 /* Save the delegation */
951 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
952 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -0400953 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400954 if (ret != 0)
955 goto out;
956 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -0500957
958 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500959 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -0500960 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400961 }
Trond Myklebustaac00a82007-07-05 19:02:21 -0400962out:
963 return ERR_PTR(ret);
964out_return_state:
965 atomic_inc(&state->count);
966 return state;
967}
968
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100969static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
970{
971 struct inode *inode;
972 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -0400973 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400974 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100975
Trond Myklebustaac00a82007-07-05 19:02:21 -0400976 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400977 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400978 goto out;
979 }
980
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400981 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100982 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400983 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100984 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400985 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -0500986 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400987 goto err;
988 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100989 state = nfs4_get_open_state(inode, data->owner);
990 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400991 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -0400992 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -0400993 int delegation_flags = 0;
994
Trond Myklebust003707c2007-07-05 18:07:55 -0400995 rcu_read_lock();
996 delegation = rcu_dereference(NFS_I(inode)->delegation);
997 if (delegation)
998 delegation_flags = delegation->flags;
999 rcu_read_unlock();
Trond Myklebust15c831b2008-12-23 15:21:39 -05001000 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001001 nfs_inode_set_delegation(state->inode,
1002 data->owner->so_cred,
1003 &data->o_res);
1004 else
1005 nfs_inode_reclaim_delegation(state->inode,
1006 data->owner->so_cred,
1007 &data->o_res);
1008 }
Trond Myklebust34310432008-12-23 15:21:38 -05001009
1010 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001011 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001012 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001013out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001014 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001015err_put_inode:
1016 iput(inode);
1017err:
1018 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001019}
1020
Trond Myklebust864472e2006-01-03 09:55:15 +01001021static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1022{
1023 struct nfs_inode *nfsi = NFS_I(state->inode);
1024 struct nfs_open_context *ctx;
1025
1026 spin_lock(&state->inode->i_lock);
1027 list_for_each_entry(ctx, &nfsi->open_files, list) {
1028 if (ctx->state != state)
1029 continue;
1030 get_nfs_open_context(ctx);
1031 spin_unlock(&state->inode->i_lock);
1032 return ctx;
1033 }
1034 spin_unlock(&state->inode->i_lock);
1035 return ERR_PTR(-ENOENT);
1036}
1037
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001038static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1039{
1040 struct nfs4_opendata *opendata;
1041
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001042 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001043 if (opendata == NULL)
1044 return ERR_PTR(-ENOMEM);
1045 opendata->state = state;
1046 atomic_inc(&state->count);
1047 return opendata;
1048}
1049
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001050static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001051{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001052 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001053 int ret;
1054
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001055 opendata->o_arg.open_flags = 0;
1056 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001057 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1058 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1059 nfs4_init_opendata_res(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001060 ret = _nfs4_proc_open(opendata);
1061 if (ret != 0)
1062 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001063 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001064 if (IS_ERR(newstate))
1065 return PTR_ERR(newstate);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001066 nfs4_close_state(&opendata->path, newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001067 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001068 return 0;
1069}
1070
1071static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1072{
Trond Myklebust864472e2006-01-03 09:55:15 +01001073 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001074 int ret;
1075
1076 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001077 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001078 smp_rmb();
1079 if (state->n_rdwr != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001080 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001081 if (ret != 0)
1082 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001083 if (newstate != state)
1084 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001085 }
1086 if (state->n_wronly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001087 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001088 if (ret != 0)
1089 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001090 if (newstate != state)
1091 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001092 }
1093 if (state->n_rdonly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001094 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001095 if (ret != 0)
1096 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001097 if (newstate != state)
1098 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001099 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001100 /*
1101 * We may have performed cached opens for all three recoveries.
1102 * Check if we need to update the current stateid.
1103 */
1104 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1105 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001106 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001107 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1108 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001109 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001110 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001111 return 0;
1112}
1113
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114/*
1115 * OPEN_RECLAIM:
1116 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001118static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001120 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001121 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001122 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 int status;
1124
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001125 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1126 if (IS_ERR(opendata))
1127 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001128 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1129 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001130 rcu_read_lock();
1131 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001132 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001133 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001134 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001135 opendata->o_arg.u.delegation_type = delegation_type;
1136 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001137 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 return status;
1139}
1140
Trond Myklebust539cd032007-06-05 11:46:42 -04001141static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142{
1143 struct nfs_server *server = NFS_SERVER(state->inode);
1144 struct nfs4_exception exception = { };
1145 int err;
1146 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001147 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001148 if (err != -NFS4ERR_DELAY)
1149 break;
1150 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 } while (exception.retry);
1152 return err;
1153}
1154
Trond Myklebust864472e2006-01-03 09:55:15 +01001155static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1156{
1157 struct nfs_open_context *ctx;
1158 int ret;
1159
1160 ctx = nfs4_state_find_open_context(state);
1161 if (IS_ERR(ctx))
1162 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001163 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001164 put_nfs_open_context(ctx);
1165 return ret;
1166}
1167
Trond Myklebust13437e12007-07-06 15:10:43 -04001168static 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 -07001169{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001170 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001171 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001173 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1174 if (IS_ERR(opendata))
1175 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001176 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -04001177 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001178 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +01001179 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001180 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001181 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182}
1183
Trond Myklebust13437e12007-07-06 15:10:43 -04001184int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185{
1186 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001187 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 int err;
1189 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001190 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 switch (err) {
1192 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001193 case -ENOENT:
1194 case -ESTALE:
1195 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001196 case -NFS4ERR_BADSESSION:
1197 case -NFS4ERR_BADSLOT:
1198 case -NFS4ERR_BAD_HIGH_SLOT:
1199 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1200 case -NFS4ERR_DEADSESSION:
1201 nfs4_schedule_state_recovery(
1202 server->nfs_client);
1203 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 case -NFS4ERR_STALE_CLIENTID:
1205 case -NFS4ERR_STALE_STATEID:
1206 case -NFS4ERR_EXPIRED:
1207 /* Don't recall a delegation if it was lost */
David Howells7539bba2006-08-22 20:06:09 -04001208 nfs4_schedule_state_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001209 goto out;
1210 case -ERESTARTSYS:
1211 /*
1212 * The show must go on: exit, but mark the
1213 * stateid as needing recovery.
1214 */
1215 case -NFS4ERR_ADMIN_REVOKED:
1216 case -NFS4ERR_BAD_STATEID:
1217 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
1218 case -ENOMEM:
1219 err = 0;
1220 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 }
1222 err = nfs4_handle_exception(server, err, &exception);
1223 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001224out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 return err;
1226}
1227
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001228static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1229{
1230 struct nfs4_opendata *data = calldata;
1231
1232 data->rpc_status = task->tk_status;
1233 if (RPC_ASSASSINATED(task))
1234 return;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001235 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001236 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1237 sizeof(data->o_res.stateid.data));
Trond Myklebustbb226292008-01-02 15:19:18 -05001238 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001239 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001240 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001241 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001242}
1243
1244static void nfs4_open_confirm_release(void *calldata)
1245{
1246 struct nfs4_opendata *data = calldata;
1247 struct nfs4_state *state = NULL;
1248
1249 /* If this request hasn't been cancelled, do nothing */
1250 if (data->cancelled == 0)
1251 goto out_free;
1252 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001253 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001254 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001255 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001256 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001257 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001258out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001259 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001260}
1261
1262static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001263 .rpc_call_done = nfs4_open_confirm_done,
1264 .rpc_release = nfs4_open_confirm_release,
1265};
1266
1267/*
1268 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1269 */
1270static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1271{
1272 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1273 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001274 struct rpc_message msg = {
1275 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1276 .rpc_argp = &data->c_arg,
1277 .rpc_resp = &data->c_res,
1278 .rpc_cred = data->owner->so_cred,
1279 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001280 struct rpc_task_setup task_setup_data = {
1281 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001282 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001283 .callback_ops = &nfs4_open_confirm_ops,
1284 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001285 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001286 .flags = RPC_TASK_ASYNC,
1287 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 int status;
1289
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001290 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001291 data->rpc_done = 0;
1292 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001293 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001294 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001295 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001296 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001297 status = nfs4_wait_for_completion_rpc_task(task);
1298 if (status != 0) {
1299 data->cancelled = 1;
1300 smp_wmb();
1301 } else
1302 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001303 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 return status;
1305}
1306
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001307static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001309 struct nfs4_opendata *data = calldata;
1310 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001311
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001312 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1313 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001314 /*
1315 * Check if we still need to send an OPEN call, or if we can use
1316 * a delegation instead.
1317 */
1318 if (data->state != NULL) {
1319 struct nfs_delegation *delegation;
1320
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001321 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001322 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001323 rcu_read_lock();
1324 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1325 if (delegation != NULL &&
Trond Myklebust15c831b2008-12-23 15:21:39 -05001326 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001327 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001328 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001329 }
1330 rcu_read_unlock();
1331 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001332 /* Update sequence id. */
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001333 data->o_arg.id = sp->so_owner_id.id;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001334 data->o_arg.clientid = sp->so_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001335 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001336 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001337 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1338 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001339 data->timestamp = jiffies;
Andy Adamsond8985282009-04-01 09:22:21 -04001340 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
1341 &data->o_arg.seq_args,
1342 &data->o_res.seq_res, 1, task))
1343 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001344 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001345 return;
1346out_no_action:
1347 task->tk_action = NULL;
1348
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001349}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001351static void nfs4_open_done(struct rpc_task *task, void *calldata)
1352{
1353 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001355 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001356
Trond Myklebustd61e6122009-12-05 19:32:19 -05001357 nfs4_sequence_done(data->o_arg.server, &data->o_res.seq_res,
1358 task->tk_status);
Andy Adamsond8985282009-04-01 09:22:21 -04001359
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001360 if (RPC_ASSASSINATED(task))
1361 return;
1362 if (task->tk_status == 0) {
1363 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001364 case S_IFREG:
1365 break;
1366 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001367 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001368 break;
1369 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001370 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001371 break;
1372 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001373 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001374 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001375 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001376 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1377 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001378 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001379 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001380}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001381
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001382static void nfs4_open_release(void *calldata)
1383{
1384 struct nfs4_opendata *data = calldata;
1385 struct nfs4_state *state = NULL;
1386
1387 /* If this request hasn't been cancelled, do nothing */
1388 if (data->cancelled == 0)
1389 goto out_free;
1390 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001391 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001392 goto out_free;
1393 /* In case we need an open_confirm, no cleanup! */
1394 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1395 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001396 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001397 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001398 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001399out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001400 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001401}
1402
1403static const struct rpc_call_ops nfs4_open_ops = {
1404 .rpc_call_prepare = nfs4_open_prepare,
1405 .rpc_call_done = nfs4_open_done,
1406 .rpc_release = nfs4_open_release,
1407};
1408
1409/*
1410 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1411 */
1412static int _nfs4_proc_open(struct nfs4_opendata *data)
1413{
1414 struct inode *dir = data->dir->d_inode;
1415 struct nfs_server *server = NFS_SERVER(dir);
1416 struct nfs_openargs *o_arg = &data->o_arg;
1417 struct nfs_openres *o_res = &data->o_res;
1418 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001419 struct rpc_message msg = {
1420 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1421 .rpc_argp = o_arg,
1422 .rpc_resp = o_res,
1423 .rpc_cred = data->owner->so_cred,
1424 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001425 struct rpc_task_setup task_setup_data = {
1426 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001427 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001428 .callback_ops = &nfs4_open_ops,
1429 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001430 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001431 .flags = RPC_TASK_ASYNC,
1432 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001433 int status;
1434
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001435 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001436 data->rpc_done = 0;
1437 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001438 data->cancelled = 0;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001439 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001440 if (IS_ERR(task))
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001441 return PTR_ERR(task);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001442 status = nfs4_wait_for_completion_rpc_task(task);
1443 if (status != 0) {
1444 data->cancelled = 1;
1445 smp_wmb();
1446 } else
1447 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001448 rpc_put_task(task);
Trond Myklebust3e309912007-07-07 13:19:59 -04001449 if (status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001450 return status;
1451
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001452 if (o_arg->open_flags & O_CREAT) {
1453 update_changeattr(dir, &o_res->cinfo);
1454 nfs_post_op_update_inode(dir, o_res->dir_attr);
1455 } else
1456 nfs_refresh_inode(dir, o_res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001458 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001460 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 }
1462 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001463 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001464 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465}
1466
Trond Myklebust10afec92007-05-14 17:16:04 -04001467static int nfs4_recover_expired_lease(struct nfs_server *server)
Trond Myklebust58d97142006-01-03 09:55:24 +01001468{
David Howells7539bba2006-08-22 20:06:09 -04001469 struct nfs_client *clp = server->nfs_client;
Trond Myklebusta78cb572009-08-09 15:06:19 -04001470 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001471 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001472
Trond Myklebusta78cb572009-08-09 15:06:19 -04001473 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001474 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001475 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001476 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001477 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1478 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001479 break;
Trond Myklebust58d97142006-01-03 09:55:24 +01001480 nfs4_schedule_state_recovery(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001481 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001482 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001483 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001484}
1485
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486/*
1487 * OPEN_EXPIRED:
1488 * reclaim state on the server after a network partition.
1489 * Assumes caller holds the appropriate lock
1490 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001491static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001493 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001494 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001496 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1497 if (IS_ERR(opendata))
1498 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001499 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001500 if (ret == -ESTALE)
Trond Myklebust539cd032007-06-05 11:46:42 -04001501 d_drop(ctx->path.dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001502 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001503 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504}
1505
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001506static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001507{
Trond Myklebust539cd032007-06-05 11:46:42 -04001508 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001509 struct nfs4_exception exception = { };
1510 int err;
1511
1512 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001513 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001514 switch (err) {
1515 default:
1516 goto out;
1517 case -NFS4ERR_GRACE:
1518 case -NFS4ERR_DELAY:
1519 nfs4_handle_exception(server, err, &exception);
1520 err = 0;
1521 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001522 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001523out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001524 return err;
1525}
1526
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1528{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001530 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
Trond Myklebust864472e2006-01-03 09:55:15 +01001532 ctx = nfs4_state_find_open_context(state);
1533 if (IS_ERR(ctx))
1534 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001535 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001536 put_nfs_open_context(ctx);
1537 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538}
1539
1540/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001541 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1542 * fields corresponding to attributes that were used to store the verifier.
1543 * Make sure we clobber those fields in the later setattr call
1544 */
1545static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1546{
1547 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1548 !(sattr->ia_valid & ATTR_ATIME_SET))
1549 sattr->ia_valid |= ATTR_ATIME;
1550
1551 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1552 !(sattr->ia_valid & ATTR_MTIME_SET))
1553 sattr->ia_valid |= ATTR_MTIME;
1554}
1555
1556/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001557 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001559static 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 -07001560{
1561 struct nfs4_state_owner *sp;
1562 struct nfs4_state *state = NULL;
1563 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001564 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001565 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
1567 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 status = -ENOMEM;
1569 if (!(sp = nfs4_get_state_owner(server, cred))) {
1570 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1571 goto out_err;
1572 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001573 status = nfs4_recover_expired_lease(server);
1574 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001575 goto err_put_state_owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001576 if (path->dentry->d_inode != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001577 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001578 status = -ENOMEM;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001579 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001580 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001581 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
Trond Myklebustaac00a82007-07-05 19:02:21 -04001583 if (path->dentry->d_inode != NULL)
1584 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1585
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001586 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001588 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001590 if (opendata->o_arg.open_flags & O_EXCL)
1591 nfs4_exclusive_attrset(opendata, sattr);
1592
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001593 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001594 status = PTR_ERR(state);
1595 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001596 goto err_opendata_put;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001597 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 *res = state;
1600 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001601err_opendata_put:
1602 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001603err_put_state_owner:
1604 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 *res = NULL;
1607 return status;
1608}
1609
1610
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001611static 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 -07001612{
1613 struct nfs4_exception exception = { };
1614 struct nfs4_state *res;
1615 int status;
1616
1617 do {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001618 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 if (status == 0)
1620 break;
1621 /* NOTE: BAD_SEQID means the server and client disagree about the
1622 * book-keeping w.r.t. state-changing operations
1623 * (OPEN/CLOSE/LOCK/LOCKU...)
1624 * It is actually a sign of a bug on the client or on the server.
1625 *
1626 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001627 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 * have unhashed the old state_owner for us, and that we can
1629 * therefore safely retry using a new one. We should still warn
1630 * the user though...
1631 */
1632 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001633 printk(KERN_WARNING "NFS: v4 server %s "
1634 " returned a bad sequence-id error!\n",
1635 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 exception.retry = 1;
1637 continue;
1638 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001639 /*
1640 * BAD_STATEID on OPEN means that the server cancelled our
1641 * state before it received the OPEN_CONFIRM.
1642 * Recover by retrying the request as per the discussion
1643 * on Page 181 of RFC3530.
1644 */
1645 if (status == -NFS4ERR_BAD_STATEID) {
1646 exception.retry = 1;
1647 continue;
1648 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001649 if (status == -EAGAIN) {
1650 /* We must have found a delegation */
1651 exception.retry = 1;
1652 continue;
1653 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1655 status, &exception));
1656 } while (exception.retry);
1657 return res;
1658}
1659
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001660static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1661 struct nfs_fattr *fattr, struct iattr *sattr,
1662 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001664 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001666 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 .iap = sattr,
1668 .server = server,
1669 .bitmask = server->attr_bitmask,
1670 };
1671 struct nfs_setattrres res = {
1672 .fattr = fattr,
1673 .server = server,
1674 };
1675 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001676 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1677 .rpc_argp = &arg,
1678 .rpc_resp = &res,
1679 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001681 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001682 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
Trond Myklebust0e574af2005-10-27 22:12:38 -04001684 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001686 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1687 /* Use that stateid */
1688 } else if (state != NULL) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001689 nfs4_copy_stateid(&arg.stateid, state, current->files);
1690 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1692
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001693 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001694 if (status == 0 && state != NULL)
1695 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001696 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697}
1698
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001699static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1700 struct nfs_fattr *fattr, struct iattr *sattr,
1701 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001703 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 struct nfs4_exception exception = { };
1705 int err;
1706 do {
1707 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001708 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 &exception);
1710 } while (exception.retry);
1711 return err;
1712}
1713
1714struct nfs4_closedata {
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001715 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 struct inode *inode;
1717 struct nfs4_state *state;
1718 struct nfs_closeargs arg;
1719 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001720 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001721 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722};
1723
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001724static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001725{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001726 struct nfs4_closedata *calldata = data;
1727 struct nfs4_state_owner *sp = calldata->state->owner;
Trond Myklebust95121352005-10-18 14:20:12 -07001728
1729 nfs4_put_open_state(calldata->state);
1730 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001731 nfs4_put_state_owner(sp);
Jan Blunck31f31db12008-05-02 13:42:45 -07001732 path_put(&calldata->path);
Trond Myklebust95121352005-10-18 14:20:12 -07001733 kfree(calldata);
1734}
1735
Trond Myklebust88069f72009-12-08 08:33:16 -05001736static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1737 fmode_t fmode)
1738{
1739 spin_lock(&state->owner->so_lock);
1740 if (!(fmode & FMODE_READ))
1741 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1742 if (!(fmode & FMODE_WRITE))
1743 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1744 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1745 spin_unlock(&state->owner->so_lock);
1746}
1747
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001748static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001750 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 struct nfs_server *server = NFS_SERVER(calldata->inode);
1753
Andy Adamson19ddab02009-04-01 09:22:20 -04001754 nfs4_sequence_done(server, &calldata->res.seq_res, task->tk_status);
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001755 if (RPC_ASSASSINATED(task))
1756 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 /* hmm. we are done with the inode, and in the process of freeing
1758 * the state_owner. we keep this around to process errors
1759 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 switch (task->tk_status) {
1761 case 0:
Trond Myklebust45328c32007-07-26 17:47:34 -04001762 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001763 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05001764 nfs4_close_clear_stateid_flags(state,
1765 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 break;
1767 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001768 case -NFS4ERR_OLD_STATEID:
1769 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001771 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001772 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001774 if (nfs4_async_handle_error(task, server, state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05001775 nfs_restart_rpc(task, server->nfs_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 return;
1777 }
1778 }
Trond Myklebust516a6af2005-10-27 22:12:41 -04001779 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780}
1781
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001782static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001784 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001785 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05001786 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07001787
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001788 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001789 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001790
Trond Myklebust88069f72009-12-08 08:33:16 -05001791 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1792 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05001793 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001794 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01001795 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001796 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001797 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
1798 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1799 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04001800 }
1801 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001802 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
1803 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1804 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04001805 }
Trond Myklebuste7616922006-01-03 09:55:13 +01001806 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05001807 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05001808
1809 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001810 /* Note: exit _without_ calling nfs4_close_done */
1811 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07001812 return;
1813 }
Trond Myklebust88069f72009-12-08 08:33:16 -05001814
1815 if (calldata->arg.fmode == 0)
1816 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
1817
Trond Myklebust516a6af2005-10-27 22:12:41 -04001818 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001819 calldata->timestamp = jiffies;
Andy Adamson19ddab02009-04-01 09:22:20 -04001820 if (nfs4_setup_sequence((NFS_SERVER(calldata->inode))->nfs_client,
1821 &calldata->arg.seq_args, &calldata->res.seq_res,
1822 1, task))
1823 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001824 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825}
1826
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001827static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001828 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001829 .rpc_call_done = nfs4_close_done,
1830 .rpc_release = nfs4_free_closedata,
1831};
1832
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833/*
1834 * It is possible for data to be read/written from a mem-mapped file
1835 * after the sys_close call (which hits the vfs layer as a flush).
1836 * This means that we can't safely call nfsv4 close on a file until
1837 * the inode is cleared. This in turn means that we are not good
1838 * NFSv4 citizens - we do not indicate to the server to update the file's
1839 * share state even when we are done with one of the three share
1840 * stateid's in the inode.
1841 *
1842 * NOTE: Caller must be holding the sp->so_owner semaphore!
1843 */
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001844int nfs4_do_close(struct path *path, struct nfs4_state *state, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001846 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04001848 struct nfs4_state_owner *sp = state->owner;
1849 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001850 struct rpc_message msg = {
1851 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1852 .rpc_cred = state->owner->so_cred,
1853 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001854 struct rpc_task_setup task_setup_data = {
1855 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001856 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001857 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05001858 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001859 .flags = RPC_TASK_ASYNC,
1860 };
Trond Myklebust95121352005-10-18 14:20:12 -07001861 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
Andy Adamson19ddab02009-04-01 09:22:20 -04001863 calldata = kzalloc(sizeof(*calldata), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07001865 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001866 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001868 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04001869 calldata->arg.stateid = &state->open_stateid;
Benny Halevy34dc1ad2009-04-01 09:22:52 -04001870 if (nfs4_has_session(server->nfs_client))
1871 memset(calldata->arg.stateid->data, 0, 4); /* clear seqid */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 /* Serialization for the sequence id */
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001873 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001874 if (calldata->arg.seqid == NULL)
1875 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001876 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04001877 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001878 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04001879 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001880 calldata->res.server = server;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04001881 calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001882 calldata->path.mnt = mntget(path->mnt);
1883 calldata->path.dentry = dget(path->dentry);
Trond Myklebust95121352005-10-18 14:20:12 -07001884
Trond Myklebust5138fde2007-07-14 15:40:01 -04001885 msg.rpc_argp = &calldata->arg,
1886 msg.rpc_resp = &calldata->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001887 task_setup_data.callback_data = calldata;
1888 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001889 if (IS_ERR(task))
1890 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001891 status = 0;
1892 if (wait)
1893 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001894 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001895 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07001896out_free_calldata:
1897 kfree(calldata);
1898out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04001899 nfs4_put_open_state(state);
1900 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07001901 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902}
1903
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001904static 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 -07001905{
1906 struct file *filp;
Trond Myklebust1b45c462007-07-03 13:04:56 -04001907 int ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001908
Trond Myklebust1b45c462007-07-03 13:04:56 -04001909 /* If the open_intent is for execute, we have an extra check to make */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001910 if (fmode & FMODE_EXEC) {
Trond Myklebustaf22f942007-08-10 17:45:10 -04001911 ret = nfs_may_open(state->inode,
Trond Myklebust1b45c462007-07-03 13:04:56 -04001912 state->owner->so_cred,
1913 nd->intent.open.flags);
1914 if (ret < 0)
1915 goto out_close;
1916 }
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001917 filp = lookup_instantiate_filp(nd, path->dentry, NULL);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001918 if (!IS_ERR(filp)) {
1919 struct nfs_open_context *ctx;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04001920 ctx = nfs_file_open_context(filp);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001921 ctx->state = state;
Trond Myklebust95cf9592006-04-18 13:14:06 -04001922 return 0;
1923 }
Trond Myklebust1b45c462007-07-03 13:04:56 -04001924 ret = PTR_ERR(filp);
1925out_close:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001926 nfs4_close_sync(path, state, fmode & (FMODE_READ|FMODE_WRITE));
Trond Myklebust1b45c462007-07-03 13:04:56 -04001927 return ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001928}
1929
1930struct dentry *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1932{
Trond Myklebustad389da2007-06-05 12:30:00 -04001933 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08001934 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001935 .dentry = dentry,
1936 };
Jan Blunck4ac91372008-02-14 19:34:32 -08001937 struct dentry *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 struct iattr attr;
1939 struct rpc_cred *cred;
1940 struct nfs4_state *state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001941 struct dentry *res;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001942 fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
1944 if (nd->flags & LOOKUP_CREATE) {
1945 attr.ia_mode = nd->intent.open.create_mode;
1946 attr.ia_valid = ATTR_MODE;
1947 if (!IS_POSIXACL(dir))
Al Viroce3b0f82009-03-29 19:08:22 -04001948 attr.ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 } else {
1950 attr.ia_valid = 0;
1951 BUG_ON(nd->intent.open.flags & O_CREAT);
1952 }
1953
Trond Myklebust98a8e322008-03-12 12:25:28 -04001954 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 if (IS_ERR(cred))
Trond Myklebust02a913a2005-10-18 14:20:17 -07001956 return (struct dentry *)cred;
Trond Myklebust565277f2007-10-15 18:17:53 -04001957 parent = dentry->d_parent;
1958 /* Protect against concurrent sillydeletes */
1959 nfs_block_sillyrename(parent);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001960 state = nfs4_do_open(dir, &path, fmode, nd->intent.open.flags, &attr, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001962 if (IS_ERR(state)) {
Trond Myklebustd75340c2007-10-01 21:42:01 -04001963 if (PTR_ERR(state) == -ENOENT) {
Trond Myklebust02a913a2005-10-18 14:20:17 -07001964 d_add(dentry, NULL);
Trond Myklebustd75340c2007-10-01 21:42:01 -04001965 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1966 }
Trond Myklebust565277f2007-10-15 18:17:53 -04001967 nfs_unblock_sillyrename(parent);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001968 return (struct dentry *)state;
1969 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001970 res = d_add_unique(dentry, igrab(state->inode));
Trond Myklebust02a913a2005-10-18 14:20:17 -07001971 if (res != NULL)
Trond Myklebustdeee9362007-08-27 11:33:00 -04001972 path.dentry = res;
Trond Myklebustd75340c2007-10-01 21:42:01 -04001973 nfs_set_verifier(path.dentry, nfs_save_change_attribute(dir));
Trond Myklebust565277f2007-10-15 18:17:53 -04001974 nfs_unblock_sillyrename(parent);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001975 nfs4_intent_set_file(nd, &path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001976 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977}
1978
1979int
Trond Myklebust02a913a2005-10-18 14:20:17 -07001980nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981{
Trond Myklebustad389da2007-06-05 12:30:00 -04001982 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08001983 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001984 .dentry = dentry,
1985 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 struct rpc_cred *cred;
1987 struct nfs4_state *state;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001988 fmode_t fmode = openflags & (FMODE_READ | FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
Trond Myklebust98a8e322008-03-12 12:25:28 -04001990 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 if (IS_ERR(cred))
1992 return PTR_ERR(cred);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001993 state = nfs4_do_open(dir, &path, fmode, openflags, NULL, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001995 if (IS_ERR(state)) {
1996 switch (PTR_ERR(state)) {
1997 case -EPERM:
1998 case -EACCES:
1999 case -EDQUOT:
2000 case -ENOSPC:
2001 case -EROFS:
2002 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
2003 return 1;
Chuck Leverb87c0ad2006-10-19 23:28:42 -07002004 default:
2005 goto out_drop;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002006 }
Trond Myklebust02a913a2005-10-18 14:20:17 -07002007 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002008 if (state->inode == dentry->d_inode) {
Trond Myklebustd75340c2007-10-01 21:42:01 -04002009 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002010 nfs4_intent_set_file(nd, &path, state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 return 1;
2012 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002013 nfs4_close_sync(&path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002014out_drop:
2015 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 return 0;
2017}
2018
Trond Myklebust1185a552009-12-03 15:54:02 -05002019static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002020{
2021 if (ctx->state == NULL)
2022 return;
2023 if (is_sync)
2024 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
2025 else
2026 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
2027}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
2029static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2030{
Benny Halevy43652ad2009-04-01 09:21:54 -04002031 struct nfs4_server_caps_arg args = {
2032 .fhandle = fhandle,
2033 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 struct nfs4_server_caps_res res = {};
2035 struct rpc_message msg = {
2036 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002037 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 .rpc_resp = &res,
2039 };
2040 int status;
2041
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002042 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 if (status == 0) {
2044 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002045 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2046 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2047 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2048 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2049 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2051 server->caps |= NFS_CAP_ACLS;
2052 if (res.has_links != 0)
2053 server->caps |= NFS_CAP_HARDLINKS;
2054 if (res.has_symlinks != 0)
2055 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002056 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2057 server->caps |= NFS_CAP_FILEID;
2058 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2059 server->caps |= NFS_CAP_MODE;
2060 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2061 server->caps |= NFS_CAP_NLINK;
2062 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2063 server->caps |= NFS_CAP_OWNER;
2064 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2065 server->caps |= NFS_CAP_OWNER_GROUP;
2066 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2067 server->caps |= NFS_CAP_ATIME;
2068 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2069 server->caps |= NFS_CAP_CTIME;
2070 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2071 server->caps |= NFS_CAP_MTIME;
2072
Trond Myklebusta65318b2009-03-11 14:10:28 -04002073 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2074 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2075 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 server->acl_bitmask = res.acl_bitmask;
2077 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002078
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 return status;
2080}
2081
Trond Myklebust55a97592006-06-09 09:34:19 -04002082int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083{
2084 struct nfs4_exception exception = { };
2085 int err;
2086 do {
2087 err = nfs4_handle_exception(server,
2088 _nfs4_server_capabilities(server, fhandle),
2089 &exception);
2090 } while (exception.retry);
2091 return err;
2092}
2093
2094static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2095 struct nfs_fsinfo *info)
2096{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 struct nfs4_lookup_root_arg args = {
2098 .bitmask = nfs4_fattr_bitmap,
2099 };
2100 struct nfs4_lookup_res res = {
2101 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002102 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 .fh = fhandle,
2104 };
2105 struct rpc_message msg = {
2106 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2107 .rpc_argp = &args,
2108 .rpc_resp = &res,
2109 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002110
Trond Myklebust0e574af2005-10-27 22:12:38 -04002111 nfs_fattr_init(info->fattr);
Trond Myklebustfccba802009-07-21 16:48:07 -04002112 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113}
2114
2115static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2116 struct nfs_fsinfo *info)
2117{
2118 struct nfs4_exception exception = { };
2119 int err;
2120 do {
2121 err = nfs4_handle_exception(server,
2122 _nfs4_lookup_root(server, fhandle, info),
2123 &exception);
2124 } while (exception.retry);
2125 return err;
2126}
2127
David Howells54ceac42006-08-22 20:06:13 -04002128/*
2129 * get the file handle for the "/" directory on the server
2130 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002132 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 int status;
2135
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 status = nfs4_lookup_root(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 if (status == 0)
2138 status = nfs4_server_capabilities(server, fhandle);
2139 if (status == 0)
2140 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002141 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142}
2143
Manoj Naik6b97fd32006-06-09 09:34:29 -04002144/*
2145 * Get locations and (maybe) other attributes of a referral.
2146 * Note that we'll actually follow the referral later when
2147 * we detect fsid mismatch in inode revalidation
2148 */
Trond Myklebust56659e92007-07-17 21:52:39 -04002149static 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 -04002150{
2151 int status = -ENOMEM;
2152 struct page *page = NULL;
2153 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002154
2155 page = alloc_page(GFP_KERNEL);
2156 if (page == NULL)
2157 goto out;
2158 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2159 if (locations == NULL)
2160 goto out;
2161
Trond Myklebustc228fd32007-01-13 02:28:11 -05002162 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002163 if (status != 0)
2164 goto out;
2165 /* Make sure server returned a different fsid for the referral */
2166 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -07002167 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 -04002168 status = -EIO;
2169 goto out;
2170 }
2171
2172 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2173 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
2174 if (!fattr->mode)
2175 fattr->mode = S_IFDIR;
2176 memset(fhandle, 0, sizeof(struct nfs_fh));
2177out:
2178 if (page)
2179 __free_page(page);
2180 if (locations)
2181 kfree(locations);
2182 return status;
2183}
2184
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2186{
2187 struct nfs4_getattr_arg args = {
2188 .fh = fhandle,
2189 .bitmask = server->attr_bitmask,
2190 };
2191 struct nfs4_getattr_res res = {
2192 .fattr = fattr,
2193 .server = server,
2194 };
2195 struct rpc_message msg = {
2196 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2197 .rpc_argp = &args,
2198 .rpc_resp = &res,
2199 };
2200
Trond Myklebust0e574af2005-10-27 22:12:38 -04002201 nfs_fattr_init(fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002202 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203}
2204
2205static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2206{
2207 struct nfs4_exception exception = { };
2208 int err;
2209 do {
2210 err = nfs4_handle_exception(server,
2211 _nfs4_proc_getattr(server, fhandle, fattr),
2212 &exception);
2213 } while (exception.retry);
2214 return err;
2215}
2216
2217/*
2218 * The file is not closed if it is opened due to the a request to change
2219 * the size of the file. The open call will not be needed once the
2220 * VFS layer lookup-intents are implemented.
2221 *
2222 * Close is called when the inode is destroyed.
2223 * If we haven't opened the file for O_WRONLY, we
2224 * need to in the size_change case to obtain a stateid.
2225 *
2226 * Got race?
2227 * Because OPEN is always done by name in nfsv4, it is
2228 * possible that we opened a different file by the same
2229 * name. We can recognize this race condition, but we
2230 * can't do anything about it besides returning an error.
2231 *
2232 * This will be fixed with VFS changes (lookup-intent).
2233 */
2234static int
2235nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2236 struct iattr *sattr)
2237{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002238 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002239 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002240 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 int status;
2242
Trond Myklebust0e574af2005-10-27 22:12:38 -04002243 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244
Trond Myklebustd5308382005-11-04 15:33:38 -05002245 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002246 if (sattr->ia_valid & ATTR_FILE) {
2247 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002248
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002249 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002250 if (ctx) {
2251 cred = ctx->cred;
2252 state = ctx->state;
2253 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002254 }
2255
2256 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002257 if (status == 0)
2258 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 return status;
2260}
2261
Trond Myklebust56659e92007-07-17 21:52:39 -04002262static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
2263 const struct qstr *name, struct nfs_fh *fhandle,
David Howells2b3de442006-08-22 20:06:09 -04002264 struct nfs_fattr *fattr)
2265{
2266 int status;
2267 struct nfs4_lookup_arg args = {
2268 .bitmask = server->attr_bitmask,
2269 .dir_fh = dirfh,
2270 .name = name,
2271 };
2272 struct nfs4_lookup_res res = {
2273 .server = server,
2274 .fattr = fattr,
2275 .fh = fhandle,
2276 };
2277 struct rpc_message msg = {
2278 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2279 .rpc_argp = &args,
2280 .rpc_resp = &res,
2281 };
2282
2283 nfs_fattr_init(fattr);
2284
2285 dprintk("NFS call lookupfh %s\n", name->name);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002286 status = nfs4_call_sync(server, &msg, &args, &res, 0);
David Howells2b3de442006-08-22 20:06:09 -04002287 dprintk("NFS reply lookupfh: %d\n", status);
David Howells2b3de442006-08-22 20:06:09 -04002288 return status;
2289}
2290
2291static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2292 struct qstr *name, struct nfs_fh *fhandle,
2293 struct nfs_fattr *fattr)
2294{
2295 struct nfs4_exception exception = { };
2296 int err;
2297 do {
Trond Myklebust4e56e082007-07-01 18:13:52 -04002298 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
2299 /* FIXME: !!!! */
2300 if (err == -NFS4ERR_MOVED) {
2301 err = -EREMOTE;
2302 break;
2303 }
2304 err = nfs4_handle_exception(server, err, &exception);
David Howells2b3de442006-08-22 20:06:09 -04002305 } while (exception.retry);
2306 return err;
2307}
2308
Trond Myklebust56659e92007-07-17 21:52:39 -04002309static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2311{
Trond Myklebust4e56e082007-07-01 18:13:52 -04002312 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313
2314 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust4e56e082007-07-01 18:13:52 -04002315 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002316 if (status == -NFS4ERR_MOVED)
2317 status = nfs4_get_referral(dir, name, fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 dprintk("NFS reply lookup: %d\n", status);
2319 return status;
2320}
2321
2322static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2323{
2324 struct nfs4_exception exception = { };
2325 int err;
2326 do {
2327 err = nfs4_handle_exception(NFS_SERVER(dir),
2328 _nfs4_proc_lookup(dir, name, fhandle, fattr),
2329 &exception);
2330 } while (exception.retry);
2331 return err;
2332}
2333
2334static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2335{
Trond Myklebust76b32992007-08-10 17:45:11 -04002336 struct nfs_server *server = NFS_SERVER(inode);
2337 struct nfs_fattr fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 struct nfs4_accessargs args = {
2339 .fh = NFS_FH(inode),
Trond Myklebust76b32992007-08-10 17:45:11 -04002340 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002342 struct nfs4_accessres res = {
2343 .server = server,
2344 .fattr = &fattr,
2345 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 struct rpc_message msg = {
2347 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2348 .rpc_argp = &args,
2349 .rpc_resp = &res,
2350 .rpc_cred = entry->cred,
2351 };
2352 int mode = entry->mask;
2353 int status;
2354
2355 /*
2356 * Determine which access bits we want to ask for...
2357 */
2358 if (mode & MAY_READ)
2359 args.access |= NFS4_ACCESS_READ;
2360 if (S_ISDIR(inode->i_mode)) {
2361 if (mode & MAY_WRITE)
2362 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2363 if (mode & MAY_EXEC)
2364 args.access |= NFS4_ACCESS_LOOKUP;
2365 } else {
2366 if (mode & MAY_WRITE)
2367 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2368 if (mode & MAY_EXEC)
2369 args.access |= NFS4_ACCESS_EXECUTE;
2370 }
Trond Myklebust76b32992007-08-10 17:45:11 -04002371 nfs_fattr_init(&fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002372 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 if (!status) {
2374 entry->mask = 0;
2375 if (res.access & NFS4_ACCESS_READ)
2376 entry->mask |= MAY_READ;
2377 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2378 entry->mask |= MAY_WRITE;
2379 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2380 entry->mask |= MAY_EXEC;
Trond Myklebust76b32992007-08-10 17:45:11 -04002381 nfs_refresh_inode(inode, &fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 }
2383 return status;
2384}
2385
2386static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2387{
2388 struct nfs4_exception exception = { };
2389 int err;
2390 do {
2391 err = nfs4_handle_exception(NFS_SERVER(inode),
2392 _nfs4_proc_access(inode, entry),
2393 &exception);
2394 } while (exception.retry);
2395 return err;
2396}
2397
2398/*
2399 * TODO: For the time being, we don't try to get any attributes
2400 * along with any of the zero-copy operations READ, READDIR,
2401 * READLINK, WRITE.
2402 *
2403 * In the case of the first three, we want to put the GETATTR
2404 * after the read-type operation -- this is because it is hard
2405 * to predict the length of a GETATTR response in v4, and thus
2406 * align the READ data correctly. This means that the GETATTR
2407 * may end up partially falling into the page cache, and we should
2408 * shift it into the 'tail' of the xdr_buf before processing.
2409 * To do this efficiently, we need to know the total length
2410 * of data received, which doesn't seem to be available outside
2411 * of the RPC layer.
2412 *
2413 * In the case of WRITE, we also want to put the GETATTR after
2414 * the operation -- in this case because we want to make sure
2415 * we get the post-operation mtime and size. This means that
2416 * we can't use xdr_encode_pages() as written: we need a variant
2417 * of it which would leave room in the 'tail' iovec.
2418 *
2419 * Both of these changes to the XDR layer would in fact be quite
2420 * minor, but I decided to leave them for a subsequent patch.
2421 */
2422static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2423 unsigned int pgbase, unsigned int pglen)
2424{
2425 struct nfs4_readlink args = {
2426 .fh = NFS_FH(inode),
2427 .pgbase = pgbase,
2428 .pglen = pglen,
2429 .pages = &page,
2430 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002431 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 struct rpc_message msg = {
2433 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2434 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002435 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 };
2437
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002438 return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439}
2440
2441static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2442 unsigned int pgbase, unsigned int pglen)
2443{
2444 struct nfs4_exception exception = { };
2445 int err;
2446 do {
2447 err = nfs4_handle_exception(NFS_SERVER(inode),
2448 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2449 &exception);
2450 } while (exception.retry);
2451 return err;
2452}
2453
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454/*
2455 * Got race?
2456 * We will need to arrange for the VFS layer to provide an atomic open.
2457 * Until then, this create/open method is prone to inefficiency and race
2458 * conditions due to the lookup, create, and open VFS calls from sys_open()
2459 * placed on the wire.
2460 *
2461 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2462 * The file will be opened again in the subsequent VFS open call
2463 * (nfs4_proc_file_open).
2464 *
2465 * The open for read will just hang around to be used by any process that
2466 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2467 */
2468
2469static int
2470nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebust02a913a2005-10-18 14:20:17 -07002471 int flags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472{
Trond Myklebustad389da2007-06-05 12:30:00 -04002473 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08002474 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04002475 .dentry = dentry,
2476 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 struct nfs4_state *state;
2478 struct rpc_cred *cred;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002479 fmode_t fmode = flags & (FMODE_READ | FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 int status = 0;
2481
Trond Myklebust98a8e322008-03-12 12:25:28 -04002482 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 if (IS_ERR(cred)) {
2484 status = PTR_ERR(cred);
2485 goto out;
2486 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002487 state = nfs4_do_open(dir, &path, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002488 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 if (IS_ERR(state)) {
2490 status = PTR_ERR(state);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002491 goto out_putcred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002493 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002494 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 if (flags & O_EXCL) {
2496 struct nfs_fattr fattr;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002497 status = nfs4_do_setattr(state->inode, cred, &fattr, sattr, state);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002498 if (status == 0)
Trond Myklebust65e43082005-08-16 11:49:44 -04002499 nfs_setattr_update_inode(state->inode, sattr);
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002500 nfs_post_op_update_inode(state->inode, &fattr);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002501 }
Trond Myklebustad389da2007-06-05 12:30:00 -04002502 if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002503 status = nfs4_intent_set_file(nd, &path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002504 else
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002505 nfs4_close_sync(&path, state, fmode);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002506out_putcred:
2507 put_rpccred(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508out:
2509 return status;
2510}
2511
2512static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2513{
Trond Myklebust16e42952005-10-27 22:12:44 -04002514 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002515 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002517 .name.len = name->len,
2518 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002519 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002521 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002522 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002523 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002525 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2526 .rpc_argp = &args,
2527 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 };
2529 int status;
2530
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002531 nfs_fattr_init(&res.dir_attr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002532 status = nfs4_call_sync(server, &msg, &args, &res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002533 if (status == 0) {
2534 update_changeattr(dir, &res.cinfo);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002535 nfs_post_op_update_inode(dir, &res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002536 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 return status;
2538}
2539
2540static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2541{
2542 struct nfs4_exception exception = { };
2543 int err;
2544 do {
2545 err = nfs4_handle_exception(NFS_SERVER(dir),
2546 _nfs4_proc_remove(dir, name),
2547 &exception);
2548 } while (exception.retry);
2549 return err;
2550}
2551
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002552static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002554 struct nfs_server *server = NFS_SERVER(dir);
2555 struct nfs_removeargs *args = msg->rpc_argp;
2556 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557
Trond Myklebusta65318b2009-03-11 14:10:28 -04002558 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002559 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561}
2562
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002563static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002565 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2566
Andy Adamson472cfbd2009-04-01 09:22:24 -04002567 nfs4_sequence_done(res->server, &res->seq_res, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002568 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002569 return 0;
2570 update_changeattr(dir, &res->cinfo);
2571 nfs_post_op_update_inode(dir, &res->dir_attr);
2572 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573}
2574
2575static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2576 struct inode *new_dir, struct qstr *new_name)
2577{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002578 struct nfs_server *server = NFS_SERVER(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 struct nfs4_rename_arg arg = {
2580 .old_dir = NFS_FH(old_dir),
2581 .new_dir = NFS_FH(new_dir),
2582 .old_name = old_name,
2583 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002584 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 };
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002586 struct nfs_fattr old_fattr, new_fattr;
2587 struct nfs4_rename_res res = {
2588 .server = server,
2589 .old_fattr = &old_fattr,
2590 .new_fattr = &new_fattr,
2591 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 struct rpc_message msg = {
2593 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2594 .rpc_argp = &arg,
2595 .rpc_resp = &res,
2596 };
2597 int status;
2598
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002599 nfs_fattr_init(res.old_fattr);
2600 nfs_fattr_init(res.new_fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002601 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602
2603 if (!status) {
2604 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002605 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002607 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 }
2609 return status;
2610}
2611
2612static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2613 struct inode *new_dir, struct qstr *new_name)
2614{
2615 struct nfs4_exception exception = { };
2616 int err;
2617 do {
2618 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2619 _nfs4_proc_rename(old_dir, old_name,
2620 new_dir, new_name),
2621 &exception);
2622 } while (exception.retry);
2623 return err;
2624}
2625
2626static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2627{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002628 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 struct nfs4_link_arg arg = {
2630 .fh = NFS_FH(inode),
2631 .dir_fh = NFS_FH(dir),
2632 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002633 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002635 struct nfs_fattr fattr, dir_attr;
2636 struct nfs4_link_res res = {
2637 .server = server,
2638 .fattr = &fattr,
2639 .dir_attr = &dir_attr,
2640 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 struct rpc_message msg = {
2642 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2643 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002644 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 };
2646 int status;
2647
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002648 nfs_fattr_init(res.fattr);
2649 nfs_fattr_init(res.dir_attr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002650 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002651 if (!status) {
2652 update_changeattr(dir, &res.cinfo);
2653 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002654 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002655 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656
2657 return status;
2658}
2659
2660static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2661{
2662 struct nfs4_exception exception = { };
2663 int err;
2664 do {
2665 err = nfs4_handle_exception(NFS_SERVER(inode),
2666 _nfs4_proc_link(inode, dir, name),
2667 &exception);
2668 } while (exception.retry);
2669 return err;
2670}
2671
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002672struct nfs4_createdata {
2673 struct rpc_message msg;
2674 struct nfs4_create_arg arg;
2675 struct nfs4_create_res res;
2676 struct nfs_fh fh;
2677 struct nfs_fattr fattr;
2678 struct nfs_fattr dir_fattr;
2679};
2680
2681static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2682 struct qstr *name, struct iattr *sattr, u32 ftype)
2683{
2684 struct nfs4_createdata *data;
2685
2686 data = kzalloc(sizeof(*data), GFP_KERNEL);
2687 if (data != NULL) {
2688 struct nfs_server *server = NFS_SERVER(dir);
2689
2690 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2691 data->msg.rpc_argp = &data->arg;
2692 data->msg.rpc_resp = &data->res;
2693 data->arg.dir_fh = NFS_FH(dir);
2694 data->arg.server = server;
2695 data->arg.name = name;
2696 data->arg.attrs = sattr;
2697 data->arg.ftype = ftype;
2698 data->arg.bitmask = server->attr_bitmask;
2699 data->res.server = server;
2700 data->res.fh = &data->fh;
2701 data->res.fattr = &data->fattr;
2702 data->res.dir_fattr = &data->dir_fattr;
2703 nfs_fattr_init(data->res.fattr);
2704 nfs_fattr_init(data->res.dir_fattr);
2705 }
2706 return data;
2707}
2708
2709static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2710{
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002711 int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
2712 &data->arg, &data->res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002713 if (status == 0) {
2714 update_changeattr(dir, &data->res.dir_cinfo);
2715 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2716 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2717 }
2718 return status;
2719}
2720
2721static void nfs4_free_createdata(struct nfs4_createdata *data)
2722{
2723 kfree(data);
2724}
2725
Chuck Lever4f390c12006-08-22 20:06:22 -04002726static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002727 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002729 struct nfs4_createdata *data;
2730 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731
Chuck Lever94a6d752006-08-22 20:06:23 -04002732 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002733 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002734
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002735 status = -ENOMEM;
2736 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2737 if (data == NULL)
2738 goto out;
2739
2740 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2741 data->arg.u.symlink.pages = &page;
2742 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002744 status = nfs4_do_create(dir, dentry, data);
2745
2746 nfs4_free_createdata(data);
2747out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 return status;
2749}
2750
Chuck Lever4f390c12006-08-22 20:06:22 -04002751static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002752 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753{
2754 struct nfs4_exception exception = { };
2755 int err;
2756 do {
2757 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002758 _nfs4_proc_symlink(dir, dentry, page,
2759 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 &exception);
2761 } while (exception.retry);
2762 return err;
2763}
2764
2765static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2766 struct iattr *sattr)
2767{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002768 struct nfs4_createdata *data;
2769 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002771 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2772 if (data == NULL)
2773 goto out;
2774
2775 status = nfs4_do_create(dir, dentry, data);
2776
2777 nfs4_free_createdata(data);
2778out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 return status;
2780}
2781
2782static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2783 struct iattr *sattr)
2784{
2785 struct nfs4_exception exception = { };
2786 int err;
2787 do {
2788 err = nfs4_handle_exception(NFS_SERVER(dir),
2789 _nfs4_proc_mkdir(dir, dentry, sattr),
2790 &exception);
2791 } while (exception.retry);
2792 return err;
2793}
2794
2795static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2796 u64 cookie, struct page *page, unsigned int count, int plus)
2797{
2798 struct inode *dir = dentry->d_inode;
2799 struct nfs4_readdir_arg args = {
2800 .fh = NFS_FH(dir),
2801 .pages = &page,
2802 .pgbase = 0,
2803 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09002804 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 };
2806 struct nfs4_readdir_res res;
2807 struct rpc_message msg = {
2808 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2809 .rpc_argp = &args,
2810 .rpc_resp = &res,
2811 .rpc_cred = cred,
2812 };
2813 int status;
2814
Harvey Harrison3110ff82008-05-02 13:42:44 -07002815 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00002816 dentry->d_parent->d_name.name,
2817 dentry->d_name.name,
2818 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2820 res.pgbase = args.pgbase;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002821 status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 if (status == 0)
2823 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustc4812992007-09-28 17:11:45 -04002824
2825 nfs_invalidate_atime(dir);
2826
Harvey Harrison3110ff82008-05-02 13:42:44 -07002827 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 return status;
2829}
2830
2831static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2832 u64 cookie, struct page *page, unsigned int count, int plus)
2833{
2834 struct nfs4_exception exception = { };
2835 int err;
2836 do {
2837 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2838 _nfs4_proc_readdir(dentry, cred, cookie,
2839 page, count, plus),
2840 &exception);
2841 } while (exception.retry);
2842 return err;
2843}
2844
2845static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2846 struct iattr *sattr, dev_t rdev)
2847{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002848 struct nfs4_createdata *data;
2849 int mode = sattr->ia_mode;
2850 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851
2852 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2853 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002854
2855 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2856 if (data == NULL)
2857 goto out;
2858
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002860 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002862 data->arg.ftype = NF4BLK;
2863 data->arg.u.device.specdata1 = MAJOR(rdev);
2864 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 }
2866 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002867 data->arg.ftype = NF4CHR;
2868 data->arg.u.device.specdata1 = MAJOR(rdev);
2869 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002872 status = nfs4_do_create(dir, dentry, data);
2873
2874 nfs4_free_createdata(data);
2875out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 return status;
2877}
2878
2879static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2880 struct iattr *sattr, dev_t rdev)
2881{
2882 struct nfs4_exception exception = { };
2883 int err;
2884 do {
2885 err = nfs4_handle_exception(NFS_SERVER(dir),
2886 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2887 &exception);
2888 } while (exception.retry);
2889 return err;
2890}
2891
2892static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2893 struct nfs_fsstat *fsstat)
2894{
2895 struct nfs4_statfs_arg args = {
2896 .fh = fhandle,
2897 .bitmask = server->attr_bitmask,
2898 };
Benny Halevy24ad1482009-04-01 09:21:56 -04002899 struct nfs4_statfs_res res = {
2900 .fsstat = fsstat,
2901 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 struct rpc_message msg = {
2903 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2904 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04002905 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 };
2907
Trond Myklebust0e574af2005-10-27 22:12:38 -04002908 nfs_fattr_init(fsstat->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002909 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910}
2911
2912static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2913{
2914 struct nfs4_exception exception = { };
2915 int err;
2916 do {
2917 err = nfs4_handle_exception(server,
2918 _nfs4_proc_statfs(server, fhandle, fsstat),
2919 &exception);
2920 } while (exception.retry);
2921 return err;
2922}
2923
2924static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2925 struct nfs_fsinfo *fsinfo)
2926{
2927 struct nfs4_fsinfo_arg args = {
2928 .fh = fhandle,
2929 .bitmask = server->attr_bitmask,
2930 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04002931 struct nfs4_fsinfo_res res = {
2932 .fsinfo = fsinfo,
2933 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 struct rpc_message msg = {
2935 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2936 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04002937 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 };
2939
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002940 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941}
2942
2943static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2944{
2945 struct nfs4_exception exception = { };
2946 int err;
2947
2948 do {
2949 err = nfs4_handle_exception(server,
2950 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2951 &exception);
2952 } while (exception.retry);
2953 return err;
2954}
2955
2956static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2957{
Trond Myklebust0e574af2005-10-27 22:12:38 -04002958 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 return nfs4_do_fsinfo(server, fhandle, fsinfo);
2960}
2961
2962static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2963 struct nfs_pathconf *pathconf)
2964{
2965 struct nfs4_pathconf_arg args = {
2966 .fh = fhandle,
2967 .bitmask = server->attr_bitmask,
2968 };
Benny Halevyd45b2982009-04-01 09:21:58 -04002969 struct nfs4_pathconf_res res = {
2970 .pathconf = pathconf,
2971 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 struct rpc_message msg = {
2973 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
2974 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04002975 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 };
2977
2978 /* None of the pathconf attributes are mandatory to implement */
2979 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
2980 memset(pathconf, 0, sizeof(*pathconf));
2981 return 0;
2982 }
2983
Trond Myklebust0e574af2005-10-27 22:12:38 -04002984 nfs_fattr_init(pathconf->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002985 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986}
2987
2988static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2989 struct nfs_pathconf *pathconf)
2990{
2991 struct nfs4_exception exception = { };
2992 int err;
2993
2994 do {
2995 err = nfs4_handle_exception(server,
2996 _nfs4_proc_pathconf(server, fhandle, pathconf),
2997 &exception);
2998 } while (exception.retry);
2999 return err;
3000}
3001
Trond Myklebustec06c092006-03-20 13:44:27 -05003002static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003{
Trond Myklebustec06c092006-03-20 13:44:27 -05003004 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005
Andy Adamsonf11c88a2009-04-01 09:22:25 -04003006 dprintk("--> %s\n", __func__);
3007
Andy Adamsonf11c88a2009-04-01 09:22:25 -04003008 nfs4_sequence_done(server, &data->res.seq_res, task->tk_status);
3009
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003010 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003011 nfs_restart_rpc(task, server->nfs_client);
Trond Myklebustec06c092006-03-20 13:44:27 -05003012 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003014
3015 nfs_invalidate_atime(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003017 renew_lease(server, data->timestamp);
3018 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019}
3020
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003021static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 data->timestamp = jiffies;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003024 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025}
3026
Trond Myklebust788e7a82006-03-20 13:44:27 -05003027static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 struct inode *inode = data->inode;
3030
Andy Adamsondef6ed72009-04-01 09:22:26 -04003031 nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
3032 task->tk_status);
3033
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003034 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003035 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003036 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003038 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003040 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003041 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003042 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043}
3044
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003045static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003047 struct nfs_server *server = NFS_SERVER(data->inode);
3048
Trond Myklebusta65318b2009-03-11 14:10:28 -04003049 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003050 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 data->timestamp = jiffies;
3052
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003053 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054}
3055
Trond Myklebust788e7a82006-03-20 13:44:27 -05003056static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 struct inode *inode = data->inode;
3059
Andy Adamson21d9a852009-04-01 09:22:27 -04003060 nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
3061 task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003062 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003063 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003064 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003066 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003067 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068}
3069
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003070static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003072 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073
Trond Myklebusta65318b2009-03-11 14:10:28 -04003074 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003075 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003076 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077}
3078
3079/*
3080 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3081 * standalone procedure for queueing an asynchronous RENEW.
3082 */
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003083static void nfs4_renew_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084{
David Howellsadfa6f92006-08-22 20:06:08 -04003085 struct nfs_client *clp = (struct nfs_client *)task->tk_msg.rpc_argp;
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003086 unsigned long timestamp = (unsigned long)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087
3088 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003089 /* Unless we're shutting down, schedule state recovery! */
3090 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
3091 nfs4_schedule_state_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 return;
3093 }
3094 spin_lock(&clp->cl_lock);
3095 if (time_before(clp->cl_last_renewal,timestamp))
3096 clp->cl_last_renewal = timestamp;
3097 spin_unlock(&clp->cl_lock);
3098}
3099
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003100static const struct rpc_call_ops nfs4_renew_ops = {
3101 .rpc_call_done = nfs4_renew_done,
3102};
3103
David Howellsadfa6f92006-08-22 20:06:08 -04003104int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105{
3106 struct rpc_message msg = {
3107 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3108 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003109 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 };
3111
3112 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003113 &nfs4_renew_ops, (void *)jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114}
3115
David Howellsadfa6f92006-08-22 20:06:08 -04003116int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117{
3118 struct rpc_message msg = {
3119 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3120 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003121 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 };
3123 unsigned long now = jiffies;
3124 int status;
3125
3126 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3127 if (status < 0)
3128 return status;
3129 spin_lock(&clp->cl_lock);
3130 if (time_before(clp->cl_last_renewal,now))
3131 clp->cl_last_renewal = now;
3132 spin_unlock(&clp->cl_lock);
3133 return 0;
3134}
3135
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003136static inline int nfs4_server_supports_acls(struct nfs_server *server)
3137{
3138 return (server->caps & NFS_CAP_ACLS)
3139 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3140 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3141}
3142
3143/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3144 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3145 * the stack.
3146 */
3147#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3148
3149static void buf_to_pages(const void *buf, size_t buflen,
3150 struct page **pages, unsigned int *pgbase)
3151{
3152 const void *p = buf;
3153
3154 *pgbase = offset_in_page(buf);
3155 p -= *pgbase;
3156 while (p < buf + buflen) {
3157 *(pages++) = virt_to_page(p);
3158 p += PAGE_CACHE_SIZE;
3159 }
3160}
3161
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003162struct nfs4_cached_acl {
3163 int cached;
3164 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003165 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003166};
3167
3168static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003169{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003170 struct nfs_inode *nfsi = NFS_I(inode);
3171
3172 spin_lock(&inode->i_lock);
3173 kfree(nfsi->nfs4_acl);
3174 nfsi->nfs4_acl = acl;
3175 spin_unlock(&inode->i_lock);
3176}
3177
3178static void nfs4_zap_acl_attr(struct inode *inode)
3179{
3180 nfs4_set_cached_acl(inode, NULL);
3181}
3182
3183static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3184{
3185 struct nfs_inode *nfsi = NFS_I(inode);
3186 struct nfs4_cached_acl *acl;
3187 int ret = -ENOENT;
3188
3189 spin_lock(&inode->i_lock);
3190 acl = nfsi->nfs4_acl;
3191 if (acl == NULL)
3192 goto out;
3193 if (buf == NULL) /* user is just asking for length */
3194 goto out_len;
3195 if (acl->cached == 0)
3196 goto out;
3197 ret = -ERANGE; /* see getxattr(2) man page */
3198 if (acl->len > buflen)
3199 goto out;
3200 memcpy(buf, acl->data, acl->len);
3201out_len:
3202 ret = acl->len;
3203out:
3204 spin_unlock(&inode->i_lock);
3205 return ret;
3206}
3207
3208static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3209{
3210 struct nfs4_cached_acl *acl;
3211
3212 if (buf && acl_len <= PAGE_SIZE) {
3213 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3214 if (acl == NULL)
3215 goto out;
3216 acl->cached = 1;
3217 memcpy(acl->data, buf, acl_len);
3218 } else {
3219 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3220 if (acl == NULL)
3221 goto out;
3222 acl->cached = 0;
3223 }
3224 acl->len = acl_len;
3225out:
3226 nfs4_set_cached_acl(inode, acl);
3227}
3228
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003229static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003230{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003231 struct page *pages[NFS4ACL_MAXPAGES];
3232 struct nfs_getaclargs args = {
3233 .fh = NFS_FH(inode),
3234 .acl_pages = pages,
3235 .acl_len = buflen,
3236 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003237 struct nfs_getaclres res = {
3238 .acl_len = buflen,
3239 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003240 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003241 struct rpc_message msg = {
3242 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3243 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003244 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003245 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003246 struct page *localpage = NULL;
3247 int ret;
3248
3249 if (buflen < PAGE_SIZE) {
3250 /* As long as we're doing a round trip to the server anyway,
3251 * let's be prepared for a page of acl data. */
3252 localpage = alloc_page(GFP_KERNEL);
3253 resp_buf = page_address(localpage);
3254 if (localpage == NULL)
3255 return -ENOMEM;
3256 args.acl_pages[0] = localpage;
3257 args.acl_pgbase = 0;
Benny Halevy663c79b2009-04-01 09:21:59 -04003258 args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003259 } else {
3260 resp_buf = buf;
3261 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3262 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003263 ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003264 if (ret)
3265 goto out_free;
Benny Halevy663c79b2009-04-01 09:21:59 -04003266 if (res.acl_len > args.acl_len)
3267 nfs4_write_cached_acl(inode, NULL, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003268 else
Benny Halevy663c79b2009-04-01 09:21:59 -04003269 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003270 if (buf) {
3271 ret = -ERANGE;
Benny Halevy663c79b2009-04-01 09:21:59 -04003272 if (res.acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003273 goto out_free;
3274 if (localpage)
Benny Halevy663c79b2009-04-01 09:21:59 -04003275 memcpy(buf, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003276 }
Benny Halevy663c79b2009-04-01 09:21:59 -04003277 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003278out_free:
3279 if (localpage)
3280 __free_page(localpage);
3281 return ret;
3282}
3283
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003284static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3285{
3286 struct nfs4_exception exception = { };
3287 ssize_t ret;
3288 do {
3289 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3290 if (ret >= 0)
3291 break;
3292 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3293 } while (exception.retry);
3294 return ret;
3295}
3296
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003297static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3298{
3299 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003300 int ret;
3301
3302 if (!nfs4_server_supports_acls(server))
3303 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003304 ret = nfs_revalidate_inode(server, inode);
3305 if (ret < 0)
3306 return ret;
3307 ret = nfs4_read_cached_acl(inode, buf, buflen);
3308 if (ret != -ENOENT)
3309 return ret;
3310 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003311}
3312
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003313static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003314{
3315 struct nfs_server *server = NFS_SERVER(inode);
3316 struct page *pages[NFS4ACL_MAXPAGES];
3317 struct nfs_setaclargs arg = {
3318 .fh = NFS_FH(inode),
3319 .acl_pages = pages,
3320 .acl_len = buflen,
3321 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003322 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003323 struct rpc_message msg = {
3324 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3325 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003326 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003327 };
3328 int ret;
3329
3330 if (!nfs4_server_supports_acls(server))
3331 return -EOPNOTSUPP;
Trond Myklebust642ac542005-10-18 14:20:19 -07003332 nfs_inode_return_delegation(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003333 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003334 ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003335 nfs_access_zap_cache(inode);
3336 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003337 return ret;
3338}
3339
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003340static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3341{
3342 struct nfs4_exception exception = { };
3343 int err;
3344 do {
3345 err = nfs4_handle_exception(NFS_SERVER(inode),
3346 __nfs4_proc_set_acl(inode, buf, buflen),
3347 &exception);
3348 } while (exception.retry);
3349 return err;
3350}
3351
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352static int
Andy Adamson8328d592009-04-01 09:22:35 -04003353_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 -07003354{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 if (!clp || task->tk_status >= 0)
3356 return 0;
3357 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003358 case -NFS4ERR_ADMIN_REVOKED:
3359 case -NFS4ERR_BAD_STATEID:
3360 case -NFS4ERR_OPENMODE:
3361 if (state == NULL)
3362 break;
3363 nfs4_state_mark_reclaim_nograce(clp, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 case -NFS4ERR_STALE_CLIENTID:
3365 case -NFS4ERR_STALE_STATEID:
3366 case -NFS4ERR_EXPIRED:
Trond Myklebust5d008372008-02-22 16:34:17 -05003367 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 nfs4_schedule_state_recovery(clp);
Trond Myklebuste005e802008-12-23 15:21:48 -05003369 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
Trond Myklebustfda13932008-02-22 16:34:12 -05003370 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 task->tk_status = 0;
3372 return -EAGAIN;
Andy Adamson4745e312009-04-01 09:22:42 -04003373#if defined(CONFIG_NFS_V4_1)
3374 case -NFS4ERR_BADSESSION:
3375 case -NFS4ERR_BADSLOT:
3376 case -NFS4ERR_BAD_HIGH_SLOT:
3377 case -NFS4ERR_DEADSESSION:
3378 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3379 case -NFS4ERR_SEQ_FALSE_RETRY:
3380 case -NFS4ERR_SEQ_MISORDERED:
3381 dprintk("%s ERROR %d, Reset session\n", __func__,
3382 task->tk_status);
Andy Adamson0b9e2d42009-12-04 16:02:14 -05003383 nfs4_schedule_state_recovery(clp);
Andy Adamson4745e312009-04-01 09:22:42 -04003384 task->tk_status = 0;
3385 return -EAGAIN;
3386#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 case -NFS4ERR_DELAY:
Andy Adamson8328d592009-04-01 09:22:35 -04003388 if (server)
3389 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003390 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3392 task->tk_status = 0;
3393 return -EAGAIN;
3394 case -NFS4ERR_OLD_STATEID:
3395 task->tk_status = 0;
3396 return -EAGAIN;
3397 }
3398 task->tk_status = nfs4_map_errors(task->tk_status);
3399 return 0;
3400}
3401
Andy Adamson8328d592009-04-01 09:22:35 -04003402static int
3403nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3404{
3405 return _nfs4_async_handle_error(task, server, server->nfs_client, state);
3406}
3407
David Howellsadfa6f92006-08-22 20:06:08 -04003408int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, unsigned short port, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409{
3410 nfs4_verifier sc_verifier;
3411 struct nfs4_setclientid setclientid = {
3412 .sc_verifier = &sc_verifier,
3413 .sc_prog = program,
3414 };
3415 struct rpc_message msg = {
3416 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3417 .rpc_argp = &setclientid,
3418 .rpc_resp = clp,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003419 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 };
Al Virobc4785c2006-10-19 23:28:51 -07003421 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 int loop = 0;
3423 int status;
3424
Al Virobc4785c2006-10-19 23:28:51 -07003425 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3427 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3428
3429 for(;;) {
3430 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003431 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003432 clp->cl_ipaddr,
3433 rpc_peeraddr2str(clp->cl_rpcclient,
3434 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003435 rpc_peeraddr2str(clp->cl_rpcclient,
3436 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003437 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438 clp->cl_id_uniquifier);
3439 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003440 sizeof(setclientid.sc_netid),
3441 rpc_peeraddr2str(clp->cl_rpcclient,
3442 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003444 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 clp->cl_ipaddr, port >> 8, port & 255);
3446
3447 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3448 if (status != -NFS4ERR_CLID_INUSE)
3449 break;
3450 if (signalled())
3451 break;
3452 if (loop++ & 1)
3453 ssleep(clp->cl_lease_time + 1);
3454 else
3455 if (++clp->cl_id_uniquifier == 0)
3456 break;
3457 }
3458 return status;
3459}
3460
David Howellsadfa6f92006-08-22 20:06:08 -04003461static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462{
3463 struct nfs_fsinfo fsinfo;
3464 struct rpc_message msg = {
3465 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
3466 .rpc_argp = clp,
3467 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003468 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 };
3470 unsigned long now;
3471 int status;
3472
3473 now = jiffies;
3474 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3475 if (status == 0) {
3476 spin_lock(&clp->cl_lock);
3477 clp->cl_lease_time = fsinfo.lease_time * HZ;
3478 clp->cl_last_renewal = now;
3479 spin_unlock(&clp->cl_lock);
3480 }
3481 return status;
3482}
3483
David Howellsadfa6f92006-08-22 20:06:08 -04003484int nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
Trond Myklebust51581f32006-03-20 13:44:47 -05003485{
Benny Halevy77e03672008-06-24 20:25:57 +03003486 long timeout = 0;
Trond Myklebust51581f32006-03-20 13:44:47 -05003487 int err;
3488 do {
3489 err = _nfs4_proc_setclientid_confirm(clp, cred);
3490 switch (err) {
3491 case 0:
3492 return err;
3493 case -NFS4ERR_RESOURCE:
3494 /* The IBM lawyers misread another document! */
3495 case -NFS4ERR_DELAY:
3496 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3497 }
3498 } while (err == 0);
3499 return err;
3500}
3501
Trond Myklebustfe650402006-01-03 09:55:18 +01003502struct nfs4_delegreturndata {
3503 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003504 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003505 struct nfs_fh fh;
3506 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003507 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003508 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003509 int rpc_status;
3510};
3511
Trond Myklebustfe650402006-01-03 09:55:18 +01003512static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3513{
3514 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003515
Trond Myklebustd61e6122009-12-05 19:32:19 -05003516 nfs4_sequence_done(data->res.server, &data->res.seq_res,
3517 task->tk_status);
Andy Adamson938e1012009-04-01 09:22:28 -04003518
Ricardo Labiaga79708862009-12-07 09:23:21 -05003519 switch (task->tk_status) {
3520 case -NFS4ERR_STALE_STATEID:
3521 case -NFS4ERR_EXPIRED:
3522 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01003523 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003524 break;
3525 default:
3526 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3527 -EAGAIN) {
3528 nfs_restart_rpc(task, data->res.server->nfs_client);
3529 return;
3530 }
3531 }
3532 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01003533}
3534
3535static void nfs4_delegreturn_release(void *calldata)
3536{
Trond Myklebustfe650402006-01-03 09:55:18 +01003537 kfree(calldata);
3538}
3539
Andy Adamson938e1012009-04-01 09:22:28 -04003540#if defined(CONFIG_NFS_V4_1)
3541static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3542{
3543 struct nfs4_delegreturndata *d_data;
3544
3545 d_data = (struct nfs4_delegreturndata *)data;
3546
3547 if (nfs4_setup_sequence(d_data->res.server->nfs_client,
3548 &d_data->args.seq_args,
3549 &d_data->res.seq_res, 1, task))
3550 return;
3551 rpc_call_start(task);
3552}
3553#endif /* CONFIG_NFS_V4_1 */
3554
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003555static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04003556#if defined(CONFIG_NFS_V4_1)
3557 .rpc_call_prepare = nfs4_delegreturn_prepare,
3558#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01003559 .rpc_call_done = nfs4_delegreturn_done,
3560 .rpc_release = nfs4_delegreturn_release,
3561};
3562
Trond Myklebuste6f81072008-01-24 18:14:34 -05003563static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003564{
3565 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003566 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003567 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003568 struct rpc_message msg = {
3569 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3570 .rpc_cred = cred,
3571 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003572 struct rpc_task_setup task_setup_data = {
3573 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003574 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003575 .callback_ops = &nfs4_delegreturn_ops,
3576 .flags = RPC_TASK_ASYNC,
3577 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003578 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003579
Andy Adamson938e1012009-04-01 09:22:28 -04003580 data = kzalloc(sizeof(*data), GFP_KERNEL);
Trond Myklebustfe650402006-01-03 09:55:18 +01003581 if (data == NULL)
3582 return -ENOMEM;
3583 data->args.fhandle = &data->fh;
3584 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003585 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003586 nfs_copy_fh(&data->fh, NFS_FH(inode));
3587 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003588 data->res.fattr = &data->fattr;
3589 data->res.server = server;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003590 data->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003591 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003592 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003593 data->rpc_status = 0;
3594
Trond Myklebustc970aa82007-07-14 15:39:59 -04003595 task_setup_data.callback_data = data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003596 msg.rpc_argp = &data->args,
3597 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003598 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003599 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003600 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003601 if (!issync)
3602 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003603 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003604 if (status != 0)
3605 goto out;
3606 status = data->rpc_status;
3607 if (status != 0)
3608 goto out;
3609 nfs_refresh_inode(inode, &data->fattr);
3610out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003611 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003612 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613}
3614
Trond Myklebuste6f81072008-01-24 18:14:34 -05003615int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616{
3617 struct nfs_server *server = NFS_SERVER(inode);
3618 struct nfs4_exception exception = { };
3619 int err;
3620 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003621 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 switch (err) {
3623 case -NFS4ERR_STALE_STATEID:
3624 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625 case 0:
3626 return 0;
3627 }
3628 err = nfs4_handle_exception(server, err, &exception);
3629 } while (exception.retry);
3630 return err;
3631}
3632
3633#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3634#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3635
3636/*
3637 * sleep, with exponential backoff, and retry the LOCK operation.
3638 */
3639static unsigned long
3640nfs4_set_lock_task_retry(unsigned long timeout)
3641{
Matthew Wilcox150030b2007-12-06 16:24:39 -05003642 schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643 timeout <<= 1;
3644 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3645 return NFS4_LOCK_MAXTIMEOUT;
3646 return timeout;
3647}
3648
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3650{
3651 struct inode *inode = state->inode;
3652 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003653 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003654 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003656 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003658 struct nfs_lockt_res res = {
3659 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660 };
3661 struct rpc_message msg = {
3662 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3663 .rpc_argp = &arg,
3664 .rpc_resp = &res,
3665 .rpc_cred = state->owner->so_cred,
3666 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 struct nfs4_lock_state *lsp;
3668 int status;
3669
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003670 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003671 status = nfs4_set_lock_state(state, request);
3672 if (status != 0)
3673 goto out;
3674 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003675 arg.lock_owner.id = lsp->ls_id.id;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003676 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003677 switch (status) {
3678 case 0:
3679 request->fl_type = F_UNLCK;
3680 break;
3681 case -NFS4ERR_DENIED:
3682 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05003684 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003685out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686 return status;
3687}
3688
3689static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3690{
3691 struct nfs4_exception exception = { };
3692 int err;
3693
3694 do {
3695 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3696 _nfs4_proc_getlk(state, cmd, request),
3697 &exception);
3698 } while (exception.retry);
3699 return err;
3700}
3701
3702static int do_vfs_lock(struct file *file, struct file_lock *fl)
3703{
3704 int res = 0;
3705 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3706 case FL_POSIX:
3707 res = posix_lock_file_wait(file, fl);
3708 break;
3709 case FL_FLOCK:
3710 res = flock_lock_file_wait(file, fl);
3711 break;
3712 default:
3713 BUG();
3714 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 return res;
3716}
3717
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003718struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003719 struct nfs_locku_args arg;
3720 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003721 struct nfs4_lock_state *lsp;
3722 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003723 struct file_lock fl;
3724 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003725 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003726};
3727
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003728static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3729 struct nfs_open_context *ctx,
3730 struct nfs4_lock_state *lsp,
3731 struct nfs_seqid *seqid)
3732{
3733 struct nfs4_unlockdata *p;
3734 struct inode *inode = lsp->ls_state->inode;
3735
Andy Adamsona8936932009-04-01 09:22:23 -04003736 p = kzalloc(sizeof(*p), GFP_KERNEL);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003737 if (p == NULL)
3738 return NULL;
3739 p->arg.fh = NFS_FH(inode);
3740 p->arg.fl = &p->fl;
3741 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003742 p->res.seqid = seqid;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003743 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003744 p->arg.stateid = &lsp->ls_stateid;
3745 p->lsp = lsp;
3746 atomic_inc(&lsp->ls_count);
3747 /* Ensure we don't close file until we're done freeing locks! */
3748 p->ctx = get_nfs_open_context(ctx);
3749 memcpy(&p->fl, fl, sizeof(p->fl));
3750 p->server = NFS_SERVER(inode);
3751 return p;
3752}
3753
Trond Myklebust06f814a2006-01-03 09:55:07 +01003754static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003755{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003756 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003757 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003758 nfs4_put_lock_state(calldata->lsp);
3759 put_nfs_open_context(calldata->ctx);
3760 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003761}
3762
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003763static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003764{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003765 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003766
Andy Adamsona8936932009-04-01 09:22:23 -04003767 nfs4_sequence_done(calldata->server, &calldata->res.seq_res,
3768 task->tk_status);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003769 if (RPC_ASSASSINATED(task))
3770 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003771 switch (task->tk_status) {
3772 case 0:
3773 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003774 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003775 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01003776 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003777 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003778 case -NFS4ERR_BAD_STATEID:
3779 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003780 case -NFS4ERR_STALE_STATEID:
3781 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003782 break;
3783 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003784 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebust0110ee12009-12-07 09:00:24 -05003785 nfs_restart_rpc(task,
Trond Myklebustd61e6122009-12-05 19:32:19 -05003786 calldata->server->nfs_client);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003787 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003788}
3789
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003790static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003791{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003792 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003794 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003795 return;
3796 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003797 /* Note: exit _without_ running nfs4_locku_done */
3798 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003799 return;
3800 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01003801 calldata->timestamp = jiffies;
Andy Adamsona8936932009-04-01 09:22:23 -04003802 if (nfs4_setup_sequence(calldata->server->nfs_client,
3803 &calldata->arg.seq_args,
3804 &calldata->res.seq_res, 1, task))
3805 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003806 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807}
3808
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003809static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003810 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003811 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01003812 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003813};
3814
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003815static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3816 struct nfs_open_context *ctx,
3817 struct nfs4_lock_state *lsp,
3818 struct nfs_seqid *seqid)
3819{
3820 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003821 struct rpc_message msg = {
3822 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3823 .rpc_cred = ctx->cred,
3824 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003825 struct rpc_task_setup task_setup_data = {
3826 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04003827 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003828 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003829 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003830 .flags = RPC_TASK_ASYNC,
3831 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003832
Frank Filz137d6ac2007-07-09 15:32:29 -07003833 /* Ensure this is an unlock - when canceling a lock, the
3834 * canceled lock is passed in, and it won't be an unlock.
3835 */
3836 fl->fl_type = F_UNLCK;
3837
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003838 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3839 if (data == NULL) {
3840 nfs_free_seqid(seqid);
3841 return ERR_PTR(-ENOMEM);
3842 }
3843
Trond Myklebust5138fde2007-07-14 15:40:01 -04003844 msg.rpc_argp = &data->arg,
3845 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003846 task_setup_data.callback_data = data;
3847 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003848}
3849
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3851{
Trond Myklebust19e03c52008-12-23 15:21:44 -05003852 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003853 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003854 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01003855 struct rpc_task *task;
3856 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003857 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858
Trond Myklebust9b073572006-06-29 16:38:34 -04003859 status = nfs4_set_lock_state(state, request);
3860 /* Unlock _before_ we do the RPC call */
3861 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003862 down_read(&nfsi->rwsem);
3863 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
3864 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003865 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003866 }
3867 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003868 if (status != 0)
3869 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003870 /* Is this a delegated lock? */
3871 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04003872 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003873 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003874 seqid = nfs_alloc_seqid(&lsp->ls_seqid);
Trond Myklebust9b073572006-06-29 16:38:34 -04003875 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003876 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04003877 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04003878 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003879 status = PTR_ERR(task);
3880 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04003881 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003882 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003883 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04003884out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003885 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003886 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887}
3888
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003889struct nfs4_lockdata {
3890 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003891 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003892 struct nfs4_lock_state *lsp;
3893 struct nfs_open_context *ctx;
3894 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003895 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003896 int rpc_status;
3897 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04003898 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003899};
3900
3901static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
3902 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp)
3903{
3904 struct nfs4_lockdata *p;
3905 struct inode *inode = lsp->ls_state->inode;
3906 struct nfs_server *server = NFS_SERVER(inode);
3907
3908 p = kzalloc(sizeof(*p), GFP_KERNEL);
3909 if (p == NULL)
3910 return NULL;
3911
3912 p->arg.fh = NFS_FH(inode);
3913 p->arg.fl = &p->fl;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003914 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid);
3915 if (p->arg.open_seqid == NULL)
3916 goto out_free;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003917 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3918 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003919 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003920 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04003921 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003922 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003923 p->res.lock_seqid = p->arg.lock_seqid;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003924 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003925 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04003926 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003927 atomic_inc(&lsp->ls_count);
3928 p->ctx = get_nfs_open_context(ctx);
3929 memcpy(&p->fl, fl, sizeof(p->fl));
3930 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003931out_free_seqid:
3932 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003933out_free:
3934 kfree(p);
3935 return NULL;
3936}
3937
3938static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
3939{
3940 struct nfs4_lockdata *data = calldata;
3941 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942
Harvey Harrison3110ff82008-05-02 13:42:44 -07003943 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003944 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
3945 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003946 /* Do we need to do an open_to_lock_owner? */
3947 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05003948 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
3949 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003950 data->arg.open_stateid = &state->stateid;
3951 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003952 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003953 } else
3954 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003955 data->timestamp = jiffies;
Andy Adamson66179ef2009-04-01 09:22:22 -04003956 if (nfs4_setup_sequence(data->server->nfs_client, &data->arg.seq_args,
3957 &data->res.seq_res, 1, task))
3958 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003959 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07003960 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003961}
3962
3963static void nfs4_lock_done(struct rpc_task *task, void *calldata)
3964{
3965 struct nfs4_lockdata *data = calldata;
3966
Harvey Harrison3110ff82008-05-02 13:42:44 -07003967 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003968
Trond Myklebustd61e6122009-12-05 19:32:19 -05003969 nfs4_sequence_done(data->server, &data->res.seq_res,
3970 task->tk_status);
Andy Adamson66179ef2009-04-01 09:22:22 -04003971
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003972 data->rpc_status = task->tk_status;
3973 if (RPC_ASSASSINATED(task))
3974 goto out;
3975 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003976 if (data->rpc_status == 0)
3977 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
3978 else
3979 goto out;
3980 }
3981 if (data->rpc_status == 0) {
3982 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
3983 sizeof(data->lsp->ls_stateid.data));
3984 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Trond Myklebust88be9f92007-06-05 10:42:27 -04003985 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003986 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003987out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07003988 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003989}
3990
3991static void nfs4_lock_release(void *calldata)
3992{
3993 struct nfs4_lockdata *data = calldata;
3994
Harvey Harrison3110ff82008-05-02 13:42:44 -07003995 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003996 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003997 if (data->cancelled != 0) {
3998 struct rpc_task *task;
3999 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4000 data->arg.lock_seqid);
4001 if (!IS_ERR(task))
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004002 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004003 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004004 } else
4005 nfs_free_seqid(data->arg.lock_seqid);
4006 nfs4_put_lock_state(data->lsp);
4007 put_nfs_open_context(data->ctx);
4008 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004009 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004010}
4011
4012static const struct rpc_call_ops nfs4_lock_ops = {
4013 .rpc_call_prepare = nfs4_lock_prepare,
4014 .rpc_call_done = nfs4_lock_done,
4015 .rpc_release = nfs4_lock_release,
4016};
4017
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004018static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004019{
4020 struct nfs4_lockdata *data;
4021 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004022 struct rpc_message msg = {
4023 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4024 .rpc_cred = state->owner->so_cred,
4025 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004026 struct rpc_task_setup task_setup_data = {
4027 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004028 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004029 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004030 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004031 .flags = RPC_TASK_ASYNC,
4032 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004033 int ret;
4034
Harvey Harrison3110ff82008-05-02 13:42:44 -07004035 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004036 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004037 fl->fl_u.nfs4_fl.owner);
4038 if (data == NULL)
4039 return -ENOMEM;
4040 if (IS_SETLKW(cmd))
4041 data->arg.block = 1;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004042 if (recovery_type == NFS_LOCK_RECLAIM)
4043 data->arg.reclaim = NFS_LOCK_RECLAIM;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004044 msg.rpc_argp = &data->arg,
4045 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004046 task_setup_data.callback_data = data;
4047 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004048 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004049 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004050 ret = nfs4_wait_for_completion_rpc_task(task);
4051 if (ret == 0) {
4052 ret = data->rpc_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004053 } else
4054 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004055 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004056 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004057 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058}
4059
4060static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4061{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004062 struct nfs_server *server = NFS_SERVER(state->inode);
4063 struct nfs4_exception exception = { };
4064 int err;
4065
4066 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004067 /* Cache the lock if possible... */
4068 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4069 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004070 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust202b50d2005-06-22 17:16:29 +00004071 if (err != -NFS4ERR_DELAY)
4072 break;
4073 nfs4_handle_exception(server, err, &exception);
4074 } while (exception.retry);
4075 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076}
4077
4078static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4079{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004080 struct nfs_server *server = NFS_SERVER(state->inode);
4081 struct nfs4_exception exception = { };
4082 int err;
4083
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004084 err = nfs4_set_lock_state(state, request);
4085 if (err != 0)
4086 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004087 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004088 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4089 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004090 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004091 switch (err) {
4092 default:
4093 goto out;
4094 case -NFS4ERR_GRACE:
4095 case -NFS4ERR_DELAY:
4096 nfs4_handle_exception(server, err, &exception);
4097 err = 0;
4098 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004099 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004100out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004101 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102}
4103
4104static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4105{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004106 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004107 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108 int status;
4109
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004110 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004111 status = nfs4_set_lock_state(state, request);
4112 if (status != 0)
4113 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004114 request->fl_flags |= FL_ACCESS;
4115 status = do_vfs_lock(request->fl_file, request);
4116 if (status < 0)
4117 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004118 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004119 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004120 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004121 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004122 request->fl_flags = fl_flags & ~FL_SLEEP;
4123 status = do_vfs_lock(request->fl_file, request);
4124 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004125 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004126 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004127 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004128 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004129 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004130 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004131 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07004132 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004133out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004134 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004135out:
4136 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137 return status;
4138}
4139
4140static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4141{
4142 struct nfs4_exception exception = { };
4143 int err;
4144
4145 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004146 err = _nfs4_proc_setlk(state, cmd, request);
4147 if (err == -NFS4ERR_DENIED)
4148 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004150 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151 } while (exception.retry);
4152 return err;
4153}
4154
4155static int
4156nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4157{
4158 struct nfs_open_context *ctx;
4159 struct nfs4_state *state;
4160 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4161 int status;
4162
4163 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004164 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165 state = ctx->state;
4166
4167 if (request->fl_start < 0 || request->fl_end < 0)
4168 return -EINVAL;
4169
Trond Myklebustd9531262009-07-21 19:22:38 -04004170 if (IS_GETLK(cmd)) {
4171 if (state != NULL)
4172 return nfs4_proc_getlk(state, F_GETLK, request);
4173 return 0;
4174 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175
4176 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4177 return -EINVAL;
4178
Trond Myklebustd9531262009-07-21 19:22:38 -04004179 if (request->fl_type == F_UNLCK) {
4180 if (state != NULL)
4181 return nfs4_proc_unlck(state, cmd, request);
4182 return 0;
4183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184
Trond Myklebustd9531262009-07-21 19:22:38 -04004185 if (state == NULL)
4186 return -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187 do {
4188 status = nfs4_proc_setlk(state, cmd, request);
4189 if ((status != -EAGAIN) || IS_SETLK(cmd))
4190 break;
4191 timeout = nfs4_set_lock_task_retry(timeout);
4192 status = -ERESTARTSYS;
4193 if (signalled())
4194 break;
4195 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196 return status;
4197}
4198
Trond Myklebust888e6942005-11-04 15:38:11 -05004199int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4200{
4201 struct nfs_server *server = NFS_SERVER(state->inode);
4202 struct nfs4_exception exception = { };
4203 int err;
4204
4205 err = nfs4_set_lock_state(state, fl);
4206 if (err != 0)
4207 goto out;
4208 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004209 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004210 switch (err) {
4211 default:
4212 printk(KERN_ERR "%s: unhandled error %d.\n",
4213 __func__, err);
4214 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004215 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004216 goto out;
4217 case -NFS4ERR_EXPIRED:
4218 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004219 case -NFS4ERR_STALE_STATEID:
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004220 case -NFS4ERR_BADSESSION:
4221 case -NFS4ERR_BADSLOT:
4222 case -NFS4ERR_BAD_HIGH_SLOT:
4223 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4224 case -NFS4ERR_DEADSESSION:
Trond Myklebustd5122202009-06-17 13:22:58 -07004225 nfs4_schedule_state_recovery(server->nfs_client);
4226 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004227 case -ERESTARTSYS:
4228 /*
4229 * The show must go on: exit, but mark the
4230 * stateid as needing recovery.
4231 */
4232 case -NFS4ERR_ADMIN_REVOKED:
4233 case -NFS4ERR_BAD_STATEID:
4234 case -NFS4ERR_OPENMODE:
4235 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
4236 err = 0;
4237 goto out;
4238 case -ENOMEM:
4239 case -NFS4ERR_DENIED:
4240 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4241 err = 0;
4242 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004243 case -NFS4ERR_DELAY:
4244 break;
4245 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004246 err = nfs4_handle_exception(server, err, &exception);
4247 } while (exception.retry);
4248out:
4249 return err;
4250}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004251
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004252#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4253
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004254int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
4255 size_t buflen, int flags)
4256{
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004257 struct inode *inode = dentry->d_inode;
4258
4259 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4260 return -EOPNOTSUPP;
4261
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004262 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004263}
4264
4265/* The getxattr man page suggests returning -ENODATA for unknown attributes,
4266 * and that's what we'll do for e.g. user attributes that haven't been set.
4267 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
4268 * attributes in kernel-managed attribute namespaces. */
4269ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
4270 size_t buflen)
4271{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004272 struct inode *inode = dentry->d_inode;
4273
4274 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4275 return -EOPNOTSUPP;
4276
4277 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004278}
4279
4280ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
4281{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004282 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004283
J. Bruce Fields096455a2006-03-20 23:23:42 -05004284 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4285 return 0;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004286 if (buf && buflen < len)
4287 return -ERANGE;
4288 if (buf)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004289 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
4290 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004291}
4292
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004293static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4294{
4295 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
4296 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4297 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4298 return;
4299
4300 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4301 NFS_ATTR_FATTR_NLINK;
4302 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4303 fattr->nlink = 2;
4304}
4305
Trond Myklebust56659e92007-07-17 21:52:39 -04004306int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004307 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004308{
4309 struct nfs_server *server = NFS_SERVER(dir);
4310 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004311 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4312 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004313 };
4314 struct nfs4_fs_locations_arg args = {
4315 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004316 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004317 .page = page,
4318 .bitmask = bitmask,
4319 };
Benny Halevy22958462009-04-01 09:22:02 -04004320 struct nfs4_fs_locations_res res = {
4321 .fs_locations = fs_locations,
4322 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004323 struct rpc_message msg = {
4324 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4325 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004326 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004327 };
4328 int status;
4329
Harvey Harrison3110ff82008-05-02 13:42:44 -07004330 dprintk("%s: start\n", __func__);
Trond Myklebustc228fd32007-01-13 02:28:11 -05004331 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004332 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004333 fs_locations->nlocations = 0;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04004334 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004335 nfs_fixup_referral_attributes(&fs_locations->fattr);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004336 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004337 return status;
4338}
4339
Andy Adamson557134a2009-04-01 09:21:53 -04004340#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04004341/*
4342 * nfs4_proc_exchange_id()
4343 *
4344 * Since the clientid has expired, all compounds using sessions
4345 * associated with the stale clientid will be returning
4346 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4347 * be in some phase of session reset.
4348 */
Andy Adamson4d643d12009-12-04 15:52:24 -05004349int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004350{
4351 nfs4_verifier verifier;
4352 struct nfs41_exchange_id_args args = {
4353 .client = clp,
4354 .flags = clp->cl_exchange_flags,
4355 };
4356 struct nfs41_exchange_id_res res = {
4357 .client = clp,
4358 };
4359 int status;
4360 struct rpc_message msg = {
4361 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4362 .rpc_argp = &args,
4363 .rpc_resp = &res,
4364 .rpc_cred = cred,
4365 };
4366 __be32 *p;
4367
4368 dprintk("--> %s\n", __func__);
4369 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04004370
Alexandros Batsakis7b183d02009-12-05 13:33:25 -05004371 /* Remove server-only flags */
4372 args.flags &= ~EXCHGID4_FLAG_CONFIRMED_R;
4373
Benny Halevy99fe60d2009-04-01 09:22:29 -04004374 p = (u32 *)verifier.data;
4375 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4376 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4377 args.verifier = &verifier;
4378
4379 while (1) {
4380 args.id_len = scnprintf(args.id, sizeof(args.id),
4381 "%s/%s %u",
4382 clp->cl_ipaddr,
4383 rpc_peeraddr2str(clp->cl_rpcclient,
4384 RPC_DISPLAY_ADDR),
4385 clp->cl_id_uniquifier);
4386
4387 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
4388
4389 if (status != NFS4ERR_CLID_INUSE)
4390 break;
4391
4392 if (signalled())
4393 break;
4394
4395 if (++clp->cl_id_uniquifier == 0)
4396 break;
4397 }
4398
4399 dprintk("<-- %s status= %d\n", __func__, status);
4400 return status;
4401}
4402
Andy Adamson2050f0c2009-04-01 09:22:30 -04004403struct nfs4_get_lease_time_data {
4404 struct nfs4_get_lease_time_args *args;
4405 struct nfs4_get_lease_time_res *res;
4406 struct nfs_client *clp;
4407};
4408
4409static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4410 void *calldata)
4411{
4412 int ret;
4413 struct nfs4_get_lease_time_data *data =
4414 (struct nfs4_get_lease_time_data *)calldata;
4415
4416 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004417 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004418 /* just setup sequence, do not trigger session recovery
4419 since we're invoked within one */
4420 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004421 &data->args->la_seq_args,
4422 &data->res->lr_seq_res, 0, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004423
4424 BUG_ON(ret == -EAGAIN);
4425 rpc_call_start(task);
4426 dprintk("<-- %s\n", __func__);
4427}
4428
4429/*
4430 * Called from nfs4_state_manager thread for session setup, so don't recover
4431 * from sequence operation or clientid errors.
4432 */
4433static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4434{
4435 struct nfs4_get_lease_time_data *data =
4436 (struct nfs4_get_lease_time_data *)calldata;
4437
4438 dprintk("--> %s\n", __func__);
4439 nfs41_sequence_done(data->clp, &data->res->lr_seq_res, task->tk_status);
4440 switch (task->tk_status) {
4441 case -NFS4ERR_DELAY:
4442 case -NFS4ERR_GRACE:
4443 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4444 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4445 task->tk_status = 0;
Trond Myklebust0110ee12009-12-07 09:00:24 -05004446 nfs_restart_rpc(task, data->clp);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004447 return;
4448 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04004449 dprintk("<-- %s\n", __func__);
4450}
4451
4452struct rpc_call_ops nfs4_get_lease_time_ops = {
4453 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4454 .rpc_call_done = nfs4_get_lease_time_done,
4455};
4456
4457int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4458{
4459 struct rpc_task *task;
4460 struct nfs4_get_lease_time_args args;
4461 struct nfs4_get_lease_time_res res = {
4462 .lr_fsinfo = fsinfo,
4463 };
4464 struct nfs4_get_lease_time_data data = {
4465 .args = &args,
4466 .res = &res,
4467 .clp = clp,
4468 };
4469 struct rpc_message msg = {
4470 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4471 .rpc_argp = &args,
4472 .rpc_resp = &res,
4473 };
4474 struct rpc_task_setup task_setup = {
4475 .rpc_client = clp->cl_rpcclient,
4476 .rpc_message = &msg,
4477 .callback_ops = &nfs4_get_lease_time_ops,
4478 .callback_data = &data
4479 };
4480 int status;
4481
4482 res.lr_seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
4483 dprintk("--> %s\n", __func__);
4484 task = rpc_run_task(&task_setup);
4485
4486 if (IS_ERR(task))
4487 status = PTR_ERR(task);
4488 else {
4489 status = task->tk_status;
4490 rpc_put_task(task);
4491 }
4492 dprintk("<-- %s return %d\n", __func__, status);
4493
4494 return status;
4495}
4496
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004497/*
4498 * Reset a slot table
4499 */
4500static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, int max_slots,
4501 int old_max_slots, int ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004502{
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004503 int i;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004504 int ret = 0;
4505
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004506 dprintk("--> %s: max_reqs=%u, tbl %p\n", __func__, max_slots, tbl);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004507
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004508 /*
4509 * Until we have dynamic slot table adjustment, insist
4510 * upon the same slot table size
4511 */
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004512 if (max_slots != old_max_slots) {
4513 dprintk("%s reset slot table does't match old\n",
4514 __func__);
4515 ret = -EINVAL; /*XXX NFS4ERR_REQ_TOO_BIG ? */
4516 goto out;
4517 }
4518 spin_lock(&tbl->slot_tbl_lock);
4519 for (i = 0; i < max_slots; ++i)
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004520 tbl->slots[i].seq_nr = ivalue;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004521 spin_unlock(&tbl->slot_tbl_lock);
4522 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4523 tbl, tbl->slots, tbl->max_slots);
4524out:
4525 dprintk("<-- %s: return %d\n", __func__, ret);
4526 return ret;
4527}
4528
Andy Adamsonfc931582009-04-01 09:22:31 -04004529/*
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004530 * Reset the forechannel and backchannel slot tables
4531 */
4532static int nfs4_reset_slot_tables(struct nfs4_session *session)
4533{
4534 int status;
4535
4536 status = nfs4_reset_slot_table(&session->fc_slot_table,
4537 session->fc_attrs.max_reqs,
4538 session->fc_slot_table.max_slots,
4539 1);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004540 if (status)
4541 return status;
4542
4543 status = nfs4_reset_slot_table(&session->bc_slot_table,
4544 session->bc_attrs.max_reqs,
4545 session->bc_slot_table.max_slots,
4546 0);
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004547 return status;
4548}
4549
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004550/* Destroy the slot table */
4551static void nfs4_destroy_slot_tables(struct nfs4_session *session)
4552{
4553 if (session->fc_slot_table.slots != NULL) {
4554 kfree(session->fc_slot_table.slots);
4555 session->fc_slot_table.slots = NULL;
4556 }
4557 if (session->bc_slot_table.slots != NULL) {
4558 kfree(session->bc_slot_table.slots);
4559 session->bc_slot_table.slots = NULL;
4560 }
4561 return;
4562}
4563
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004564/*
Andy Adamsonfc931582009-04-01 09:22:31 -04004565 * Initialize slot table
4566 */
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004567static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
4568 int max_slots, int ivalue)
Andy Adamsonfc931582009-04-01 09:22:31 -04004569{
Andy Adamsonfc931582009-04-01 09:22:31 -04004570 struct nfs4_slot *slot;
4571 int ret = -ENOMEM;
4572
4573 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
4574
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004575 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
Andy Adamsonfc931582009-04-01 09:22:31 -04004576
4577 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_KERNEL);
4578 if (!slot)
4579 goto out;
Andy Adamsonfc931582009-04-01 09:22:31 -04004580 ret = 0;
4581
4582 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonfc931582009-04-01 09:22:31 -04004583 tbl->max_slots = max_slots;
4584 tbl->slots = slot;
4585 tbl->highest_used_slotid = -1; /* no slot is currently used */
4586 spin_unlock(&tbl->slot_tbl_lock);
4587 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4588 tbl, tbl->slots, tbl->max_slots);
4589out:
4590 dprintk("<-- %s: return %d\n", __func__, ret);
4591 return ret;
Andy Adamsonfc931582009-04-01 09:22:31 -04004592}
4593
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004594/*
4595 * Initialize the forechannel and backchannel tables
4596 */
4597static int nfs4_init_slot_tables(struct nfs4_session *session)
4598{
Trond Myklebustf26468f2009-12-05 19:32:11 -05004599 struct nfs4_slot_table *tbl;
4600 int status = 0;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004601
Trond Myklebustf26468f2009-12-05 19:32:11 -05004602 tbl = &session->fc_slot_table;
4603 if (tbl->slots == NULL) {
4604 status = nfs4_init_slot_table(tbl,
4605 session->fc_attrs.max_reqs, 1);
4606 if (status)
4607 return status;
4608 }
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004609
Trond Myklebustf26468f2009-12-05 19:32:11 -05004610 tbl = &session->bc_slot_table;
4611 if (tbl->slots == NULL) {
4612 status = nfs4_init_slot_table(tbl,
4613 session->bc_attrs.max_reqs, 0);
4614 if (status)
4615 nfs4_destroy_slot_tables(session);
4616 }
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004617
4618 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04004619}
4620
4621struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
4622{
4623 struct nfs4_session *session;
4624 struct nfs4_slot_table *tbl;
4625
4626 session = kzalloc(sizeof(struct nfs4_session), GFP_KERNEL);
4627 if (!session)
4628 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04004629
Andy Adamson76db6d92009-04-01 09:22:38 -04004630 /*
4631 * The create session reply races with the server back
4632 * channel probe. Mark the client NFS_CS_SESSION_INITING
4633 * so that the client back channel can find the
4634 * nfs_client struct
4635 */
4636 clp->cl_cons_state = NFS_CS_SESSION_INITING;
Andy Adamsonea028ac2009-12-04 15:55:38 -05004637 init_completion(&session->complete);
Andy Adamson76db6d92009-04-01 09:22:38 -04004638
Andy Adamson557134a2009-04-01 09:21:53 -04004639 tbl = &session->fc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05004640 tbl->highest_used_slotid = -1;
Andy Adamson557134a2009-04-01 09:21:53 -04004641 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004642 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004643
4644 tbl = &session->bc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05004645 tbl->highest_used_slotid = -1;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004646 spin_lock_init(&tbl->slot_tbl_lock);
4647 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
4648
Andy Adamson557134a2009-04-01 09:21:53 -04004649 session->clp = clp;
4650 return session;
4651}
4652
4653void nfs4_destroy_session(struct nfs4_session *session)
4654{
Andy Adamson5a0ffe52009-04-01 09:23:18 -04004655 nfs4_proc_destroy_session(session);
4656 dprintk("%s Destroy backchannel for xprt %p\n",
4657 __func__, session->clp->cl_rpcclient->cl_xprt);
4658 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
4659 NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004660 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04004661 kfree(session);
4662}
4663
Andy Adamsonfc931582009-04-01 09:22:31 -04004664/*
4665 * Initialize the values to be used by the client in CREATE_SESSION
4666 * If nfs4_init_session set the fore channel request and response sizes,
4667 * use them.
4668 *
4669 * Set the back channel max_resp_sz_cached to zero to force the client to
4670 * always set csa_cachethis to FALSE because the current implementation
4671 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
4672 */
4673static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
4674{
4675 struct nfs4_session *session = args->client->cl_session;
4676 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
4677 mxresp_sz = session->fc_attrs.max_resp_sz;
4678
4679 if (mxrqst_sz == 0)
4680 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
4681 if (mxresp_sz == 0)
4682 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
4683 /* Fore channel attributes */
4684 args->fc_attrs.headerpadsz = 0;
4685 args->fc_attrs.max_rqst_sz = mxrqst_sz;
4686 args->fc_attrs.max_resp_sz = mxresp_sz;
4687 args->fc_attrs.max_resp_sz_cached = mxresp_sz;
4688 args->fc_attrs.max_ops = NFS4_MAX_OPS;
4689 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
4690
4691 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
4692 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4693 __func__,
4694 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
4695 args->fc_attrs.max_resp_sz_cached, args->fc_attrs.max_ops,
4696 args->fc_attrs.max_reqs);
4697
4698 /* Back channel attributes */
4699 args->bc_attrs.headerpadsz = 0;
4700 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
4701 args->bc_attrs.max_resp_sz = PAGE_SIZE;
4702 args->bc_attrs.max_resp_sz_cached = 0;
4703 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
4704 args->bc_attrs.max_reqs = 1;
4705
4706 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
4707 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4708 __func__,
4709 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
4710 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
4711 args->bc_attrs.max_reqs);
4712}
4713
Andy Adamson8d353012009-04-01 09:22:32 -04004714static int _verify_channel_attr(char *chan, char *attr_name, u32 sent, u32 rcvd)
4715{
4716 if (rcvd <= sent)
4717 return 0;
4718 printk(KERN_WARNING "%s: Session INVALID: %s channel %s increased. "
4719 "sent=%u rcvd=%u\n", __func__, chan, attr_name, sent, rcvd);
4720 return -EINVAL;
4721}
4722
4723#define _verify_fore_channel_attr(_name_) \
4724 _verify_channel_attr("fore", #_name_, \
4725 args->fc_attrs._name_, \
4726 session->fc_attrs._name_)
4727
4728#define _verify_back_channel_attr(_name_) \
4729 _verify_channel_attr("back", #_name_, \
4730 args->bc_attrs._name_, \
4731 session->bc_attrs._name_)
4732
4733/*
4734 * The server is not allowed to increase the fore channel header pad size,
4735 * maximum response size, or maximum number of operations.
4736 *
4737 * The back channel attributes are only negotiatied down: We send what the
4738 * (back channel) server insists upon.
4739 */
4740static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
4741 struct nfs4_session *session)
4742{
4743 int ret = 0;
4744
4745 ret |= _verify_fore_channel_attr(headerpadsz);
4746 ret |= _verify_fore_channel_attr(max_resp_sz);
4747 ret |= _verify_fore_channel_attr(max_ops);
4748
4749 ret |= _verify_back_channel_attr(headerpadsz);
4750 ret |= _verify_back_channel_attr(max_rqst_sz);
4751 ret |= _verify_back_channel_attr(max_resp_sz);
4752 ret |= _verify_back_channel_attr(max_resp_sz_cached);
4753 ret |= _verify_back_channel_attr(max_ops);
4754 ret |= _verify_back_channel_attr(max_reqs);
4755
4756 return ret;
4757}
4758
Andy Adamsonfc931582009-04-01 09:22:31 -04004759static int _nfs4_proc_create_session(struct nfs_client *clp)
4760{
4761 struct nfs4_session *session = clp->cl_session;
4762 struct nfs41_create_session_args args = {
4763 .client = clp,
4764 .cb_program = NFS4_CALLBACK,
4765 };
4766 struct nfs41_create_session_res res = {
4767 .client = clp,
4768 };
4769 struct rpc_message msg = {
4770 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
4771 .rpc_argp = &args,
4772 .rpc_resp = &res,
4773 };
4774 int status;
4775
4776 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04004777 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04004778
4779 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4780
Andy Adamson8d353012009-04-01 09:22:32 -04004781 if (!status)
4782 /* Verify the session's negotiated channel_attrs values */
4783 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04004784 if (!status) {
4785 /* Increment the clientid slot sequence id */
4786 clp->cl_seqid++;
4787 }
4788
4789 return status;
4790}
4791
4792/*
4793 * Issues a CREATE_SESSION operation to the server.
4794 * It is the responsibility of the caller to verify the session is
4795 * expired before calling this routine.
4796 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05004797int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04004798{
4799 int status;
4800 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04004801 struct nfs4_session *session = clp->cl_session;
4802
4803 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
4804
4805 status = _nfs4_proc_create_session(clp);
4806 if (status)
4807 goto out;
4808
Trond Myklebustf26468f2009-12-05 19:32:11 -05004809 /* Init and reset the fore channel */
4810 status = nfs4_init_slot_tables(session);
4811 dprintk("slot table initialization returned %d\n", status);
4812 if (status)
4813 goto out;
4814 status = nfs4_reset_slot_tables(session);
4815 dprintk("slot table reset returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04004816 if (status)
4817 goto out;
4818
4819 ptr = (unsigned *)&session->sess_id.data[0];
4820 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
4821 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04004822out:
4823 dprintk("<-- %s\n", __func__);
4824 return status;
4825}
4826
Andy Adamson0f3e66c2009-04-01 09:22:34 -04004827/*
4828 * Issue the over-the-wire RPC DESTROY_SESSION.
4829 * The caller must serialize access to this routine.
4830 */
4831int nfs4_proc_destroy_session(struct nfs4_session *session)
4832{
4833 int status = 0;
4834 struct rpc_message msg;
4835
4836 dprintk("--> nfs4_proc_destroy_session\n");
4837
4838 /* session is still being setup */
4839 if (session->clp->cl_cons_state != NFS_CS_READY)
4840 return status;
4841
4842 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
4843 msg.rpc_argp = session;
4844 msg.rpc_resp = NULL;
4845 msg.rpc_cred = NULL;
4846 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4847
4848 if (status)
4849 printk(KERN_WARNING
4850 "Got error %d from the server on DESTROY_SESSION. "
4851 "Session has been destroyed regardless...\n", status);
4852
4853 dprintk("<-- nfs4_proc_destroy_session\n");
4854 return status;
4855}
4856
Trond Myklebustfccba802009-07-21 16:48:07 -04004857int nfs4_init_session(struct nfs_server *server)
4858{
4859 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05004860 struct nfs4_session *session;
Trond Myklebustfccba802009-07-21 16:48:07 -04004861 int ret;
4862
4863 if (!nfs4_has_session(clp))
4864 return 0;
4865
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05004866 session = clp->cl_session;
4867 session->fc_attrs.max_rqst_sz = server->wsize + nfs41_maxwrite_overhead;
4868 session->fc_attrs.max_resp_sz = server->rsize + nfs41_maxread_overhead;
4869
Trond Myklebustfccba802009-07-21 16:48:07 -04004870 ret = nfs4_recover_expired_lease(server);
4871 if (!ret)
4872 ret = nfs4_check_client_ready(clp);
4873 return ret;
4874}
4875
Andy Adamsonfc01cea2009-04-01 09:22:36 -04004876/*
4877 * Renew the cl_session lease.
4878 */
4879static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
4880{
4881 struct nfs4_sequence_args args;
4882 struct nfs4_sequence_res res;
4883
4884 struct rpc_message msg = {
4885 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
4886 .rpc_argp = &args,
4887 .rpc_resp = &res,
4888 .rpc_cred = cred,
4889 };
4890
4891 args.sa_cache_this = 0;
4892
4893 return nfs4_call_sync_sequence(clp, clp->cl_rpcclient, &msg, &args,
4894 &res, 0);
4895}
4896
4897void nfs41_sequence_call_done(struct rpc_task *task, void *data)
4898{
4899 struct nfs_client *clp = (struct nfs_client *)data;
4900
4901 nfs41_sequence_done(clp, task->tk_msg.rpc_resp, task->tk_status);
4902
4903 if (task->tk_status < 0) {
4904 dprintk("%s ERROR %d\n", __func__, task->tk_status);
4905
4906 if (_nfs4_async_handle_error(task, NULL, clp, NULL)
4907 == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05004908 nfs_restart_rpc(task, clp);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04004909 return;
4910 }
4911 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04004912 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
4913
Andy Adamsonfc01cea2009-04-01 09:22:36 -04004914 kfree(task->tk_msg.rpc_argp);
4915 kfree(task->tk_msg.rpc_resp);
4916
4917 dprintk("<-- %s\n", __func__);
4918}
4919
4920static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
4921{
4922 struct nfs_client *clp;
4923 struct nfs4_sequence_args *args;
4924 struct nfs4_sequence_res *res;
4925
4926 clp = (struct nfs_client *)data;
4927 args = task->tk_msg.rpc_argp;
4928 res = task->tk_msg.rpc_resp;
4929
4930 if (nfs4_setup_sequence(clp, args, res, 0, task))
4931 return;
4932 rpc_call_start(task);
4933}
4934
4935static const struct rpc_call_ops nfs41_sequence_ops = {
4936 .rpc_call_done = nfs41_sequence_call_done,
4937 .rpc_call_prepare = nfs41_sequence_prepare,
4938};
4939
4940static int nfs41_proc_async_sequence(struct nfs_client *clp,
4941 struct rpc_cred *cred)
4942{
4943 struct nfs4_sequence_args *args;
4944 struct nfs4_sequence_res *res;
4945 struct rpc_message msg = {
4946 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
4947 .rpc_cred = cred,
4948 };
4949
4950 args = kzalloc(sizeof(*args), GFP_KERNEL);
4951 if (!args)
4952 return -ENOMEM;
4953 res = kzalloc(sizeof(*res), GFP_KERNEL);
4954 if (!res) {
4955 kfree(args);
4956 return -ENOMEM;
4957 }
4958 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
4959 msg.rpc_argp = args;
4960 msg.rpc_resp = res;
4961
4962 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
4963 &nfs41_sequence_ops, (void *)clp);
4964}
4965
Ricardo Labiagafce5c832009-12-05 16:08:41 -05004966struct nfs4_reclaim_complete_data {
4967 struct nfs_client *clp;
4968 struct nfs41_reclaim_complete_args arg;
4969 struct nfs41_reclaim_complete_res res;
4970};
4971
4972static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
4973{
4974 struct nfs4_reclaim_complete_data *calldata = data;
4975
4976 if (nfs4_setup_sequence(calldata->clp, &calldata->arg.seq_args,
4977 &calldata->res.seq_res, 0, task))
4978 return;
4979
4980 rpc_call_start(task);
4981}
4982
4983static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
4984{
4985 struct nfs4_reclaim_complete_data *calldata = data;
4986 struct nfs_client *clp = calldata->clp;
4987 struct nfs4_sequence_res *res = &calldata->res.seq_res;
4988
4989 dprintk("--> %s\n", __func__);
4990 nfs41_sequence_done(clp, res, task->tk_status);
4991 switch (task->tk_status) {
4992 case 0:
4993 case -NFS4ERR_COMPLETE_ALREADY:
4994 break;
4995 case -NFS4ERR_BADSESSION:
4996 case -NFS4ERR_DEADSESSION:
4997 /*
4998 * Handle the session error, but do not retry the operation, as
4999 * we have no way of telling whether the clientid had to be
5000 * reset before we got our reply. If reset, a new wave of
5001 * reclaim operations will follow, containing their own reclaim
5002 * complete. We don't want our retry to get on the way of
5003 * recovery by incorrectly indicating to the server that we're
5004 * done reclaiming state since the process had to be restarted.
5005 */
5006 _nfs4_async_handle_error(task, NULL, clp, NULL);
5007 break;
5008 default:
5009 if (_nfs4_async_handle_error(
5010 task, NULL, clp, NULL) == -EAGAIN) {
5011 rpc_restart_call_prepare(task);
5012 return;
5013 }
5014 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005015
5016 dprintk("<-- %s\n", __func__);
5017}
5018
5019static void nfs4_free_reclaim_complete_data(void *data)
5020{
5021 struct nfs4_reclaim_complete_data *calldata = data;
5022
5023 kfree(calldata);
5024}
5025
5026static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5027 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5028 .rpc_call_done = nfs4_reclaim_complete_done,
5029 .rpc_release = nfs4_free_reclaim_complete_data,
5030};
5031
5032/*
5033 * Issue a global reclaim complete.
5034 */
5035static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5036{
5037 struct nfs4_reclaim_complete_data *calldata;
5038 struct rpc_task *task;
5039 struct rpc_message msg = {
5040 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5041 };
5042 struct rpc_task_setup task_setup_data = {
5043 .rpc_client = clp->cl_rpcclient,
5044 .rpc_message = &msg,
5045 .callback_ops = &nfs4_reclaim_complete_call_ops,
5046 .flags = RPC_TASK_ASYNC,
5047 };
5048 int status = -ENOMEM;
5049
5050 dprintk("--> %s\n", __func__);
5051 calldata = kzalloc(sizeof(*calldata), GFP_KERNEL);
5052 if (calldata == NULL)
5053 goto out;
5054 calldata->clp = clp;
5055 calldata->arg.one_fs = 0;
5056 calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
5057
5058 msg.rpc_argp = &calldata->arg;
5059 msg.rpc_resp = &calldata->res;
5060 task_setup_data.callback_data = calldata;
5061 task = rpc_run_task(&task_setup_data);
5062 if (IS_ERR(task))
5063 status = PTR_ERR(task);
5064 rpc_put_task(task);
5065out:
5066 dprintk("<-- %s status=%d\n", __func__, status);
5067 return status;
5068}
Andy Adamson557134a2009-04-01 09:21:53 -04005069#endif /* CONFIG_NFS_V4_1 */
5070
Andy Adamson591d71c2009-04-01 09:22:47 -04005071struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005072 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005073 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074 .recover_open = nfs4_open_reclaim,
5075 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04005076 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005077 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078};
5079
Andy Adamson591d71c2009-04-01 09:22:47 -04005080#if defined(CONFIG_NFS_V4_1)
5081struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
5082 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5083 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5084 .recover_open = nfs4_open_reclaim,
5085 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05005086 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005087 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005088 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04005089};
5090#endif /* CONFIG_NFS_V4_1 */
5091
5092struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005093 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005094 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095 .recover_open = nfs4_open_expired,
5096 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04005097 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005098 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005099};
5100
Andy Adamson591d71c2009-04-01 09:22:47 -04005101#if defined(CONFIG_NFS_V4_1)
5102struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
5103 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5104 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5105 .recover_open = nfs4_open_expired,
5106 .recover_lock = nfs4_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05005107 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005108 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04005109};
5110#endif /* CONFIG_NFS_V4_1 */
5111
Benny Halevy29fba382009-04-01 09:22:44 -04005112struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
5113 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04005114 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005115 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04005116};
5117
5118#if defined(CONFIG_NFS_V4_1)
5119struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
5120 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04005121 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005122 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04005123};
5124#endif
5125
5126/*
5127 * Per minor version reboot and network partition recovery ops
5128 */
5129
Andy Adamson591d71c2009-04-01 09:22:47 -04005130struct nfs4_state_recovery_ops *nfs4_reboot_recovery_ops[] = {
5131 &nfs40_reboot_recovery_ops,
5132#if defined(CONFIG_NFS_V4_1)
5133 &nfs41_reboot_recovery_ops,
5134#endif
5135};
5136
5137struct nfs4_state_recovery_ops *nfs4_nograce_recovery_ops[] = {
5138 &nfs40_nograce_recovery_ops,
5139#if defined(CONFIG_NFS_V4_1)
5140 &nfs41_nograce_recovery_ops,
5141#endif
5142};
5143
Benny Halevy29fba382009-04-01 09:22:44 -04005144struct nfs4_state_maintenance_ops *nfs4_state_renewal_ops[] = {
5145 &nfs40_state_renewal_ops,
5146#if defined(CONFIG_NFS_V4_1)
5147 &nfs41_state_renewal_ops,
5148#endif
5149};
5150
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08005151static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005152 .permission = nfs_permission,
5153 .getattr = nfs_getattr,
5154 .setattr = nfs_setattr,
5155 .getxattr = nfs4_getxattr,
5156 .setxattr = nfs4_setxattr,
5157 .listxattr = nfs4_listxattr,
5158};
5159
David Howells509de812006-08-22 20:06:11 -04005160const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161 .version = 4, /* protocol version */
5162 .dentry_ops = &nfs4_dentry_operations,
5163 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005164 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165 .getroot = nfs4_proc_get_root,
5166 .getattr = nfs4_proc_getattr,
5167 .setattr = nfs4_proc_setattr,
David Howells2b3de442006-08-22 20:06:09 -04005168 .lookupfh = nfs4_proc_lookupfh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005169 .lookup = nfs4_proc_lookup,
5170 .access = nfs4_proc_access,
5171 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172 .create = nfs4_proc_create,
5173 .remove = nfs4_proc_remove,
5174 .unlink_setup = nfs4_proc_unlink_setup,
5175 .unlink_done = nfs4_proc_unlink_done,
5176 .rename = nfs4_proc_rename,
5177 .link = nfs4_proc_link,
5178 .symlink = nfs4_proc_symlink,
5179 .mkdir = nfs4_proc_mkdir,
5180 .rmdir = nfs4_proc_remove,
5181 .readdir = nfs4_proc_readdir,
5182 .mknod = nfs4_proc_mknod,
5183 .statfs = nfs4_proc_statfs,
5184 .fsinfo = nfs4_proc_fsinfo,
5185 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04005186 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187 .decode_dirent = nfs4_decode_dirent,
5188 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05005189 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005191 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005192 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005193 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005195 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04005196 .close_context = nfs4_close_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005197};
5198
5199/*
5200 * Local variables:
5201 * c-basic-offset: 8
5202 * End:
5203 */